Skip to content

Commit 0a2bf6c

Browse files
authored
fix: fix race identified by tsan (#2891)
* fix: fix race identified by tsan Change-Id: If07161b71d4b4b22f384470068562406fcb15ae5 * fix Change-Id: Ib80ec6a3b05a779815042576a22a834bc12bd6c6
1 parent 820acbe commit 0a2bf6c

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

google-cloud-bigtable-emulator-core/src/main/java/com/google/cloud/bigtable/emulator/core/EmulatorController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public synchronized void start(int port)
129129
throw e;
130130
}
131131
}
132-
Thread stdoutThread = pipeStreamToLog(process.getInputStream(), Level.INFO);
133-
Thread stderrThread = pipeStreamToLog(process.getErrorStream(), Level.WARNING);
132+
stdoutThread = pipeStreamToLog(process.getInputStream(), Level.INFO);
133+
stderrThread = pipeStreamToLog(process.getErrorStream(), Level.WARNING);
134134
isStopped = false;
135135

136136
shutdownHook =

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/PreparedStatementImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ ApiFuture<PrepareResponse> getFreshPlan() {
154154
* Check the expiry of the current plan, if it's future is resolved. If we are within 1s of
155155
* expiry, call startBackgroundRefresh with the version of the latest PrepareQuery.
156156
*/
157-
void backgroundRefreshIfNeeded() {
157+
synchronized void backgroundRefreshIfNeeded() {
158158
PrepareQueryState localState = this.currentState.get();
159159
if (localState.maybeBackgroundRefresh().isPresent()) {
160160
// We already have an ongoing refresh
@@ -183,7 +183,7 @@ void backgroundRefreshIfNeeded() {
183183
* Returns the most recently refreshed PreparedQueryData. It may still be refreshing if the
184184
* previous plan has expired.
185185
*/
186-
public PreparedQueryData getLatestPrepareResponse() {
186+
public synchronized PreparedQueryData getLatestPrepareResponse() {
187187
PrepareQueryState localState = currentState.get();
188188
if (localState.maybeBackgroundRefresh().isPresent()
189189
&& localState.maybeBackgroundRefresh().get().prepareFuture().isDone()) {

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/exporter/BigtableCloudMonitoringExporter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class BigtableCloudMonitoringExporter implements MetricExporter {
7979
private final MetricServiceClient client;
8080

8181
private final AtomicReference<State> state;
82-
private CompletableResultCode lastExportCode;
82+
private AtomicReference<CompletableResultCode> lastExportCode = new AtomicReference<>();
8383
private final AtomicBoolean exportFailureLogged = new AtomicBoolean(false);
8484

8585
private enum State {
@@ -149,8 +149,9 @@ public void close() {
149149
public CompletableResultCode export(Collection<MetricData> metricData) {
150150
Preconditions.checkState(state.get() != State.Closed, "Exporter is closed");
151151

152-
lastExportCode = doExport(metricData);
153-
return lastExportCode;
152+
CompletableResultCode result = doExport(metricData);
153+
lastExportCode.set(result);
154+
return result;
154155
}
155156

156157
private CompletableResultCode doExport(Collection<MetricData> metricData) {
@@ -194,7 +195,7 @@ public void onFailure(Throwable throwable) {
194195
RuntimeException asyncWrapper = new RuntimeException("export failed", throwable);
195196
asyncWrapper.setStackTrace(stackTrace);
196197

197-
if (state.get() != State.Closing && state.get() != State.Closed) {
198+
if (state.get() != State.Closing || state.get() != State.Closed) {
198199
// ignore the export warning when client is shutting down
199200
LOGGER.log(Level.WARNING, msg, asyncWrapper);
200201
}
@@ -231,8 +232,8 @@ private List<ApiFuture<Empty>> exportTimeSeries(
231232

232233
@Override
233234
public CompletableResultCode flush() {
234-
if (lastExportCode != null) {
235-
return lastExportCode;
235+
if (lastExportCode.get() != null) {
236+
return lastExportCode.get();
236237
}
237238
return CompletableResultCode.ofSuccess();
238239
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/DynamicFlowControlStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ synchronized void update(long value) {
9494
weightedCount += weight;
9595
}
9696

97-
double getMean() {
97+
synchronized double getMean() {
9898
return mean;
9999
}
100100

0 commit comments

Comments
 (0)