public static ArtifactServiceStager overChannel(Channel channel) { return overChannel(channel, DEFAULT_BUFFER_SIZE); }
/** * Create a new ArtifactServiceStager with the specified buffer size. Useful for testing * multi-part uploads. * * @param bufferSize the maximum size of the artifact chunk, in bytes. */ static ArtifactServiceStager overChannel(Channel channel, int bufferSize) { return new ArtifactServiceStager(channel, bufferSize); }
CloseableResource.of( channelFactory.forDescriptor(artifactStagingEndpoint), ManagedChannel::shutdown)) { ArtifactServiceStager stager = ArtifactServiceStager.overChannel(artifactChannel.get()); LOG.debug("Actual files staged: {}", filesToStage); retrievalToken = stager.stage(stagingSessionToken, filesToStage); } catch (CloseableResource.CloseException e) { LOG.warn("Error closing artifact staging channel", e);
stager.stage( stagingSessionToken, ImmutableList.of(
/** * Stages the given artifact files to the staging service. * * @return The artifact staging token returned by the service */ public String stage(String stagingSessionToken, Collection<StagedFile> files) throws IOException, InterruptedException { final Map<StagedFile, CompletionStage<ArtifactMetadata>> futures = new HashMap<>(); LOG.info("Staging {} files (token: {})", files.size(), stagingSessionToken); for (StagedFile file : files) { futures.put( file, MoreFutures.supplyAsync(new StagingCallable(stagingSessionToken, file), executorService)); } CompletionStage<StagingResult> stagingResult = MoreFutures.allAsList(futures.values()) .thenApply(ignored -> new ExtractStagingResultsCallable(futures).call()); return stageManifest(stagingSessionToken, stagingResult); }
@Test public void testStage() throws Exception { String stagingSessionToken = "token"; File file = temp.newFile(); byte[] content = "foo-bar-baz".getBytes(StandardCharsets.UTF_8); String contentSha256 = Hashing.sha256().newHasher().putBytes(content).hash().toString(); try (FileChannel contentChannel = new FileOutputStream(file).getChannel()) { contentChannel.write(ByteBuffer.wrap(content)); } stager.stage(stagingSessionToken, Collections.singleton(StagedFile.of(file, file.getName()))); assertThat(service.getStagedArtifacts().entrySet(), hasSize(1)); byte[] stagedContent = Iterables.getOnlyElement(service.getStagedArtifacts().values()); assertThat(stagedContent, equalTo(content)); ArtifactMetadata staged = service.getManifest().getArtifact(0); assertThat(staged.getName(), equalTo(file.getName())); String manifestSha256 = staged.getSha256(); assertThat(contentSha256, equalTo(manifestSha256)); assertThat(service.getManifest().getArtifactCount(), equalTo(1)); assertThat(staged, equalTo(Iterables.getOnlyElement(service.getStagedArtifacts().keySet()))); }
@Before public void setup() throws IOException { stager = ArtifactServiceStager.overChannel( InProcessChannelBuilder.forName("service_stager").build(), 6); service = new InMemoryArtifactStagerService(); server = InProcessServerBuilder.forName("service_stager") .directExecutor() .addService(service) .build() .start(); }