Skip to content

Commit

Permalink
Restore atomicity of sequence number generation and addition to the e…
Browse files Browse the repository at this point in the history
…vent queue in the BES uploader.

The BES backends expect the sequence number and the order in the queue to coincide and the atomicity was removed in 7c79576 so we're restoring it now with this change.

Fixes #8160

PiperOrigin-RevId: 245416462
  • Loading branch information
lfpino authored and copybara-github committed Apr 26, 2019
1 parent d002e45 commit 4b092cd
Showing 1 changed file with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,28 @@ void enqueueEvent(BuildEvent event) {
}
return;
}
// BuildCompletingEvent marks the end of the build in the BEP event stream.
if (event instanceof BuildCompletingEvent) {
synchronized (lock) {

// The generation of the sequence number and the addition to the {@link #eventQueue} should be
// atomic since BES expects the events in that exact order.
// More details can be found in b/131393380.
// TODO(bazel-team): Consider relaxing this invariant by having a more relaxed order.
synchronized (lock) {
// BuildCompletingEvent marks the end of the build in the BEP event stream.
if (event instanceof BuildCompletingEvent) {
this.buildStatus = extractBuildStatus((BuildCompletingEvent) event);
}
ensureUploadThreadStarted();

// TODO(b/131393380): {@link #nextSeqNum} doesn't need to be an AtomicInteger if it's
// always used under lock. It would be cleaner and more performant to update the sequence
// number when we take the item off the queue.
eventQueue.addLast(
new SendRegularBuildEventCommand(
event,
localFileUploadFuture,
nextSeqNum.getAndIncrement(),
Timestamps.fromMillis(clock.currentTimeMillis())));
}
ensureUploadThreadStarted();
eventQueue.addLast(
new SendRegularBuildEventCommand(
event,
localFileUploadFuture,
nextSeqNum.getAndIncrement(),
Timestamps.fromMillis(clock.currentTimeMillis())));
}

/**
Expand All @@ -201,8 +210,18 @@ public ListenableFuture<Void> close() {

ensureUploadThreadStarted();

// Enqueue the last event which will terminate the upload.
eventQueue.addLast(new SendLastBuildEventCommand(nextSeqNum.getAndIncrement(), currentTime()));
// The generation of the sequence number and the addition to the {@link #eventQueue} should be
// atomic since BES expects the events in that exact order.
// More details can be found in b/131393380.
// TODO(bazel-team): Consider relaxing this invariant by having a more relaxed order.
synchronized (lock) {
// Enqueue the last event which will terminate the upload.
// TODO(b/131393380): {@link #nextSeqNum} doesn't need to be an AtomicInteger if it's
// always used under lock. It would be cleaner and more performant to update the sequence
// number when we take the item off the queue.
eventQueue.addLast(
new SendLastBuildEventCommand(nextSeqNum.getAndIncrement(), currentTime()));
}

final SettableFuture<Void> finalCloseFuture = closeFuture;
closeFuture.addListener(
Expand Down

0 comments on commit 4b092cd

Please sign in to comment.