@Override public void cancel(CancelJobRequest request, StreamObserver<CancelJobResponse> responseObserver) { LOG.trace("{} {}", CancelJobRequest.class.getSimpleName(), request); responseObserver.onError( Status.NOT_FOUND .withDescription(String.format("Unknown Job ID %s", request.getJobId())) .asException()); }
private FileWritingObserver createFile(ArtifactApi.ArtifactMetadata metadata) throws IOException { File destination = location.getArtifactFile(metadata.getName()); if (!destination.createNewFile()) { throw Status.ALREADY_EXISTS .withDescription(String.format("Artifact with name %s already exists", metadata)) .asRuntimeException(); } return new FileWritingObserver( destination, new FileOutputStream(destination), responseObserver); }
Status.INTERNAL .withCause(actual) .withDescription(Throwables.getStackTraceAsString(actual)); responseObserver.onError(status.asException());
@Override public void commitManifest( ArtifactApi.CommitManifestRequest request, StreamObserver<ArtifactApi.CommitManifestResponse> responseObserver) { try { commitManifestOrThrow(request, responseObserver); } catch (StatusRuntimeException e) { responseObserver.onError(e); LOG.error("Failed to commit Manifest {}", request.getManifest(), e); } catch (Exception e) { responseObserver.onError( Status.INTERNAL .withCause(e) .withDescription(Throwables.getStackTraceAsString(e)) .asRuntimeException()); LOG.error("Failed to commit Manifest {}", request.getManifest(), e); } }
private void commitManifestOrThrow( ArtifactApi.CommitManifestRequest request, StreamObserver<ArtifactApi.CommitManifestResponse> responseObserver) throws IOException { Collection<ArtifactApi.ArtifactMetadata> missing = new ArrayList<>(); for (ArtifactApi.ArtifactMetadata artifact : request.getManifest().getArtifactList()) { // TODO: Validate the checksums on the server side, to fail more aggressively if require if (!location.getArtifactFile(artifact.getName()).exists()) { missing.add(artifact); } } if (!missing.isEmpty()) { throw Status.INVALID_ARGUMENT .withDescription( String.format("Attempted to commit manifest with missing Artifacts: [%s]", missing)) .asRuntimeException(); } File mf = location.getManifestFile(); checkState(mf.createNewFile(), "Could not create file to store manifest"); try (OutputStream mfOut = new FileOutputStream(mf)) { request.getManifest().writeTo(mfOut); } responseObserver.onNext( ArtifactApi.CommitManifestResponse.newBuilder() .setRetrievalToken(location.getRootPath()) .build()); responseObserver.onCompleted(); }
@Override public void onNext(ArtifactApi.PutArtifactRequest value) { try { if (value.getData() == null) { StatusRuntimeException e = Status.INVALID_ARGUMENT .withDescription( String.format( "Expected all chunks in the current stream state to contain data, got %s", value.getContentCase())) .asRuntimeException(); throw e; } value.getData().getData().writeTo(target); } catch (Exception e) { cleanedUp(e); } }
@Override public void getArtifact( ArtifactApi.GetArtifactRequest request, StreamObserver<ArtifactApi.ArtifactChunk> responseObserver) { try { ByteBuffer artifact = getArtifact(request.getName()); do { responseObserver.onNext( ArtifactChunk.newBuilder() .setData( ByteString.copyFrom( artifact, Math.min(artifact.remaining(), DEFAULT_CHUNK_SIZE))) .build()); } while (artifact.hasRemaining()); responseObserver.onCompleted(); } catch (FileNotFoundException e) { responseObserver.onError( Status.INVALID_ARGUMENT .withDescription(String.format("No such artifact %s", request.getName())) .withCause(e) .asException()); } catch (Exception e) { responseObserver.onError( Status.INTERNAL .withDescription( String.format("Could not retrieve artifact with name %s", request.getName())) .withCause(e) .asException()); } }
@Override public void onNext(ArtifactApi.PutArtifactRequest value) { try { if (writer == null) { if (!value.getContentCase().equals(ArtifactApi.PutArtifactRequest.ContentCase.METADATA)) { throw Status.INVALID_ARGUMENT .withDescription( String.format( "Expected the first %s to contain the Artifact Name, got %s", ArtifactApi.PutArtifactRequest.class.getSimpleName(), value.getContentCase())) .asRuntimeException(); } writer = createFile(value.getMetadata().getMetadata()); } else { writer.onNext(value); } } catch (StatusRuntimeException e) { responseObserver.onError(e); } catch (Exception e) { responseObserver.onError( Status.INTERNAL .withCause(e) .withDescription(Throwables.getStackTraceAsString(e)) .asRuntimeException()); } }
responseObserver.onError( Status.INVALID_ARGUMENT .withDescription(String.format("Unknown Preparation Id %s", preparationId)) .asException()); return;