use of com.google.cloud.talent.v4.RequestMetadata in project bazel-buildfarm by bazelbuild.
the class ContentAddressableStorages method casMapDecorator.
/**
* decorates a map with a CAS interface, does not react to removals with expirations
*/
public static ContentAddressableStorage casMapDecorator(Map<String, ByteString> map) {
return new ContentAddressableStorage() {
final Writes writes = new Writes(this);
@Override
public boolean contains(Digest digest, Digest.Builder result) {
ByteString data = getData(digest);
if (data != null) {
result.mergeFrom(digest).setSizeBytes(data.size());
return true;
}
return false;
}
@Override
public Iterable<Digest> findMissingBlobs(Iterable<Digest> digests) {
ImmutableList.Builder<Digest> missing = ImmutableList.builder();
for (Digest digest : digests) {
if (getData(digest) == null) {
missing.add(digest);
}
}
return missing.build();
}
@Override
public Write getWrite(Digest digest, UUID uuid, RequestMetadata requestMetadata) {
return writes.get(digest, uuid);
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public InputStream newInput(Digest digest, long offset) throws IOException {
ByteString data = getData(digest);
if (data == null) {
throw new NoSuchFileException(digest.getHash());
}
InputStream in = data.newInput();
in.skip(offset);
return in;
}
@Override
public void get(Digest digest, long offset, long count, ServerCallStreamObserver<ByteString> responseObserver, RequestMetadata requestMetadata) {
ByteString data = getData(digest);
if (data != null) {
responseObserver.onNext(data);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asException());
}
}
private ByteString getData(Digest digest) {
if (digest.getSizeBytes() == 0) {
return ByteString.EMPTY;
}
ByteString data = map.get(digest.getHash());
if (data == null || (digest.getSizeBytes() > 0 && digest.getSizeBytes() != data.size())) {
return null;
}
return data;
}
@Override
public ListenableFuture<Iterable<Response>> getAllFuture(Iterable<Digest> digests) {
return immediateFuture(MemoryCAS.getAll(digests, this::getData));
}
@Override
public Blob get(Digest digest) {
ByteString data = getData(digest);
if (data == null) {
return null;
}
return new Blob(data, digest);
}
@Override
public void put(Blob blob) {
map.put(blob.getDigest().getHash(), blob.getData());
writes.getFuture(blob.getDigest()).set(blob.getData());
}
@Override
public void put(Blob blob, Runnable onExpiration) {
put(blob);
}
@Override
public long maxEntrySize() {
return UNLIMITED_ENTRY_SIZE_MAX;
}
};
}
use of com.google.cloud.talent.v4.RequestMetadata in project bazel-buildfarm by bazelbuild.
the class WriteStreamObserver method errorResponse.
private boolean errorResponse(Throwable t) {
if (exception.compareAndSet(null, t)) {
boolean isEntryLimitException = t instanceof EntryLimitException;
if (isEntryLimitException) {
t = Status.OUT_OF_RANGE.withDescription(t.getMessage()).asException();
}
responseObserver.onError(t);
if (isEntryLimitException) {
RequestMetadata requestMetadata = TracingMetadataUtils.fromCurrentContext();
logger.log(Level.WARNING, format("%s-%s: %s -> %s -> %s: exceeded entry limit for %s", requestMetadata.getToolDetails().getToolName(), requestMetadata.getToolDetails().getToolVersion(), requestMetadata.getCorrelatedInvocationsId(), requestMetadata.getToolInvocationId(), requestMetadata.getActionId(), name));
}
return true;
}
return false;
}
use of com.google.cloud.talent.v4.RequestMetadata in project bazel-buildfarm by bazelbuild.
the class FetchService method fetchBlob.
private void fetchBlob(Instance instance, FetchBlobRequest request, StreamObserver<FetchBlobResponse> responseObserver) {
Digest expectedDigest = null;
RequestMetadata requestMetadata = TracingMetadataUtils.fromCurrentContext();
if (request.getQualifiersCount() == 0) {
throw Status.INVALID_ARGUMENT.withDescription("Empty qualifier list").asRuntimeException();
}
for (Qualifier qualifier : request.getQualifiersList()) {
String name = qualifier.getName();
if (name.equals("checksum.sri")) {
expectedDigest = parseChecksumSRI(qualifier.getValue());
Digest.Builder result = Digest.newBuilder();
if (instance.containsBlob(expectedDigest, result, requestMetadata)) {
responseObserver.onNext(FetchBlobResponse.newBuilder().setBlobDigest(result.build()).build());
}
} else {
throw Status.INVALID_ARGUMENT.withDescription(format("Invalid qualifier '%s'", name)).asRuntimeException();
}
}
if (request.getUrisCount() != 0) {
addCallback(instance.fetchBlob(request.getUrisList(), expectedDigest, requestMetadata), new FutureCallback<Digest>() {
@Override
public void onSuccess(Digest actualDigest) {
logger.log(Level.INFO, format("fetch blob succeeded: %s inserted into CAS", DigestUtil.toString(actualDigest)));
responseObserver.onNext(FetchBlobResponse.newBuilder().setBlobDigest(actualDigest).build());
responseObserver.onCompleted();
}
@SuppressWarnings("NullableProblems")
@Override
public void onFailure(Throwable t) {
// handle NoSuchFileException
logger.log(Level.SEVERE, "fetch blob failed", t);
responseObserver.onError(t);
}
}, directExecutor());
} else {
responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("Empty uris list").asRuntimeException());
}
}
use of com.google.cloud.talent.v4.RequestMetadata 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());
}
use of com.google.cloud.talent.v4.RequestMetadata in project java-talent by googleapis.
the class JobSearchCustomRankingSearch method sampleSearchJobs.
/**
* Search Jobs using custom rankings
*
* @param projectId Your Google Cloud Project ID
* @param tenantId Identifier of the Tenantd
*/
public static void sampleSearchJobs(String projectId, String tenantId) {
try (JobServiceClient jobServiceClient = JobServiceClient.create()) {
TenantOrProjectName parent = TenantName.of(projectId, tenantId);
String domain = "www.example.com";
String sessionId = "Hashed session identifier";
String userId = "Hashed user identifier";
RequestMetadata requestMetadata = RequestMetadata.newBuilder().setDomain(domain).setSessionId(sessionId).setUserId(userId).build();
SearchJobsRequest.CustomRankingInfo.ImportanceLevel importanceLevel = SearchJobsRequest.CustomRankingInfo.ImportanceLevel.EXTREME;
String rankingExpression = "(someFieldLong + 25) * 0.25";
SearchJobsRequest.CustomRankingInfo customRankingInfo = SearchJobsRequest.CustomRankingInfo.newBuilder().setImportanceLevel(importanceLevel).setRankingExpression(rankingExpression).build();
String orderBy = "custom_ranking desc";
SearchJobsRequest request = SearchJobsRequest.newBuilder().setParent(parent.toString()).setRequestMetadata(requestMetadata).setCustomRankingInfo(customRankingInfo).setOrderBy(orderBy).build();
for (SearchJobsResponse.MatchingJob responseItem : jobServiceClient.searchJobs(request).iterateAll()) {
System.out.printf("Job summary: %s\n", responseItem.getJobSummary());
System.out.printf("Job title snippet: %s\n", responseItem.getJobTitleSnippet());
Job job = responseItem.getJob();
System.out.printf("Job name: %s\n", job.getName());
System.out.printf("Job title: %s\n", job.getTitle());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
Aggregations