use of build.buildfarm.common.grpc.UniformDelegateServerCallStreamObserver in project bazel-buildfarm by bazelbuild.
the class ByteStreamService method onErrorLogReadObserver.
ServerCallStreamObserver<ReadResponse> onErrorLogReadObserver(String name, long offset, ServerCallStreamObserver<ReadResponse> delegate) {
return new UniformDelegateServerCallStreamObserver<ReadResponse>(delegate) {
long responseCount = 0;
long responseBytes = 0;
@Override
public void onNext(ReadResponse response) {
delegate.onNext(response);
responseCount++;
responseBytes += response.getData().size();
}
@Override
public void onError(Throwable t) {
Status status = Status.fromThrowable(t);
if (status.getCode() != Code.NOT_FOUND) {
java.util.logging.Level level = Level.SEVERE;
if (responseCount > 0 && status.getCode() == Code.DEADLINE_EXCEEDED || status.getCode() == Code.CANCELLED) {
level = Level.WARNING;
}
String message = format("error reading %s at offset %d", name, offset);
if (responseCount > 0) {
message += format(" after %d responses and %d bytes of content", responseCount, responseBytes);
}
logger.log(level, message, t);
}
delegate.onError(t);
}
@Override
public void onCompleted() {
delegate.onCompleted();
}
};
}
use of build.buildfarm.common.grpc.UniformDelegateServerCallStreamObserver in project bazel-buildfarm by bazelbuild.
the class ShardInstance method getBlob.
@Override
public void getBlob(Digest blobDigest, long offset, long count, ServerCallStreamObserver<ByteString> blobObserver, RequestMetadata requestMetadata) {
List<String> workersList;
Set<String> workerSet;
Set<String> locationSet;
try {
workerSet = backplane.getWorkers();
locationSet = backplane.getBlobLocationSet(blobDigest);
synchronized (workerSet) {
workersList = new ArrayList<>(Sets.intersection(locationSet, workerSet));
}
} catch (IOException e) {
blobObserver.onError(e);
return;
}
boolean emptyWorkerList = workersList.isEmpty();
final ListenableFuture<List<String>> populatedWorkerListFuture;
if (emptyWorkerList) {
logger.log(Level.FINE, format("worker list was initially empty for %s, attempting to correct", DigestUtil.toString(blobDigest)));
populatedWorkerListFuture = transform(correctMissingBlob(backplane, workerSet, locationSet, this::workerStub, blobDigest, directExecutor(), RequestMetadata.getDefaultInstance()), (foundOnWorkers) -> {
logger.log(Level.FINE, format("worker list was corrected for %s to be %s", DigestUtil.toString(blobDigest), foundOnWorkers.toString()));
Iterables.addAll(workersList, foundOnWorkers);
return workersList;
}, directExecutor());
} else {
populatedWorkerListFuture = immediateFuture(workersList);
}
Context ctx = Context.current();
ServerCallStreamObserver<ByteString> chunkObserver = new UniformDelegateServerCallStreamObserver<ByteString>(blobObserver) {
boolean triedCheck = emptyWorkerList;
@Override
public void onNext(ByteString nextChunk) {
blobObserver.onNext(nextChunk);
}
@Override
public void onError(Throwable t) {
Status status = Status.fromThrowable(t);
if (status.getCode() == Code.NOT_FOUND && !triedCheck) {
triedCheck = true;
workersList.clear();
final ListenableFuture<List<String>> workersListFuture;
logger.log(Level.FINE, format("worker list was depleted for %s, attempting to correct", DigestUtil.toString(blobDigest)));
workersListFuture = transform(correctMissingBlob(backplane, workerSet, locationSet, ShardInstance.this::workerStub, blobDigest, directExecutor(), RequestMetadata.getDefaultInstance()), (foundOnWorkers) -> {
logger.log(Level.FINE, format("worker list was corrected after depletion for %s to be %s", DigestUtil.toString(blobDigest), foundOnWorkers.toString()));
Iterables.addAll(workersList, foundOnWorkers);
return workersList;
}, directExecutor());
final ServerCallStreamObserver<ByteString> checkedChunkObserver = this;
addCallback(workersListFuture, new WorkersCallback(rand) {
@Override
public void onQueue(Deque<String> workers) {
ctx.run(() -> fetchBlobFromWorker(blobDigest, workers, offset, count, checkedChunkObserver, requestMetadata));
}
@Override
public void onFailure(Throwable t) {
blobObserver.onError(t);
}
}, directExecutor());
} else {
blobObserver.onError(t);
}
}
@Override
public void onCompleted() {
blobObserver.onCompleted();
}
};
addCallback(populatedWorkerListFuture, new WorkersCallback(rand) {
@Override
public void onQueue(Deque<String> workers) {
ctx.run(() -> fetchBlobFromWorker(blobDigest, workers, offset, count, chunkObserver, requestMetadata));
}
@Override
public void onFailure(Throwable t) {
blobObserver.onError(t);
}
}, directExecutor());
}
Aggregations