下面列出了com.google.common.io.ByteSource#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public StoreRequest(ArtifactInfo info, ByteSource payloadSource) throws IOException {
this.payloadSource = payloadSource;
this.rawKeys = createKeysHeader(info.getRuleKeys());
this.rawMetadata =
createMetadataHeader(info.getRuleKeys(), info.getMetadata(), payloadSource);
this.contentLength =
rawKeys.length + Integer.SIZE / Byte.SIZE + rawMetadata.length + payloadSource.size();
}
public FetchResponse(
ImmutableSet<RuleKey> ruleKeys,
ImmutableMap<String, String> metadata,
ByteSource payloadSource)
throws IOException {
this.payloadSource = payloadSource;
this.rawMetadata = createMetadataHeader(ruleKeys, metadata, payloadSource);
this.contentLength = Integer.SIZE / Byte.SIZE + rawMetadata.length + payloadSource.size();
}
@Override
protected StoreResult storeImpl(ArtifactInfo info, Path file) throws IOException {
ByteSource artifact =
new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return getProjectFilesystem().newFileInputStream(file);
}
};
BuckCacheStoreRequest storeRequest = new BuckCacheStoreRequest();
ArtifactMetadata artifactMetadata =
infoToMetadata(
info,
artifact,
getRepository(),
scheduleType,
producerId,
producerHostname,
artifact.size());
storeRequest.setMetadata(artifactMetadata);
PayloadInfo payloadInfo = new PayloadInfo();
long artifactSizeBytes = artifact.size();
payloadInfo.setSizeBytes(artifactSizeBytes);
BuckCacheRequest cacheRequest = newCacheRequest();
cacheRequest.addToPayloads(payloadInfo);
cacheRequest.setType(BuckCacheRequestType.STORE);
cacheRequest.setStoreRequest(storeRequest);
// Handling for file system issues may lead us to upload empty manifests
if (artifactSizeBytes == 0 && info.isManifest()) {
throw new IOException(
String.format("Trying to upload a 0 size Manifest entry %s", info.getRuleKeys()));
}
if (LOG.isVerboseEnabled()) {
LOG.verbose(
String.format(
"Storing artifact with metadata: [%s].",
ThriftUtil.thriftToDebugJson(artifactMetadata)));
}
ThriftArtifactCacheProtocol.Request request =
ThriftArtifactCacheProtocol.createRequest(PROTOCOL, cacheRequest, artifact);
Request.Builder builder = toOkHttpRequest(request);
ImmutableStoreResult.Builder resultBuilder = ImmutableStoreResult.builder();
resultBuilder.setRequestSizeBytes(request.getRequestLengthBytes());
try (HttpResponse httpResponse = storeClient.makeRequest(hybridThriftEndpoint, builder)) {
if (httpResponse.statusCode() != 200) {
throw new IOException(
String.format(
"Failed to store cache artifact with HTTP status code [%d:%s] "
+ " to url [%s] for build target [%s] that has size [%d] bytes.",
httpResponse.statusCode(),
httpResponse.statusMessage(),
httpResponse.requestUrl(),
info.getBuildTarget().orElse(null),
artifactSizeBytes));
}
try (ThriftArtifactCacheProtocol.Response response =
ThriftArtifactCacheProtocol.parseResponse(PROTOCOL, httpResponse.getBody())) {
BuckCacheResponse cacheResponse = response.getThriftData();
if (!cacheResponse.isWasSuccessful()) {
reportFailureWithFormatKey(
"Failed to store artifact with thriftErrorMessage=[%s] "
+ "url=[%s] artifactSizeBytes=[%d]",
response.getThriftData().getErrorMessage(),
httpResponse.requestUrl(),
artifactSizeBytes);
}
resultBuilder.setArtifactContentHash(storeRequest.getMetadata().artifactPayloadMd5);
resultBuilder.setWasStoreSuccessful(cacheResponse.isWasSuccessful());
if (LOG.isDebugEnabled()) {
LOG.debug(
"Debug info for cache store request: artifactMetadata=[%s] response=[%s]",
ThriftUtil.thriftToDebugJson(artifactMetadata),
ThriftUtil.thriftToDebugJson(cacheResponse));
}
}
}
return resultBuilder.build();
}