Search in sources :

Example 1 with PayloadInfo

use of com.facebook.buck.artifact_cache.thrift.PayloadInfo in project buck by facebook.

the class ThriftArtifactCacheProtocolTest method serializeData.

// TODO(ruibm): Use Base64 response data generated from the real server implementation.
private BuckCacheResponse serializeData(String errorMessage, OutputStream rawStream, byte[]... payloads) throws IOException, TException {
    BuckCacheResponse cacheResponse = new BuckCacheResponse();
    cacheResponse.setErrorMessage(errorMessage);
    for (byte[] payload : payloads) {
        PayloadInfo info = new PayloadInfo();
        info.setSizeBytes(payload.length);
        cacheResponse.addToPayloads(info);
    }
    try (DataOutputStream stream = new DataOutputStream(rawStream)) {
        byte[] header = ThriftUtil.serialize(PROTOCOL, cacheResponse);
        stream.writeInt(header.length);
        stream.write(header);
        for (byte[] payload : payloads) {
            stream.write(payload);
        }
        stream.flush();
    }
    return cacheResponse;
}
Also used : DataOutputStream(java.io.DataOutputStream) PayloadInfo(com.facebook.buck.artifact_cache.thrift.PayloadInfo) BuckCacheResponse(com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)

Example 2 with PayloadInfo

use of com.facebook.buck.artifact_cache.thrift.PayloadInfo in project buck by facebook.

the class ThriftArtifactCacheProtocolTest method createDefaultRequest.

private BuckCacheRequest createDefaultRequest(long... payloadSizeBytes) {
    BuckCacheRequest cacheRequest = new BuckCacheRequest();
    cacheRequest.setType(BuckCacheRequestType.FETCH);
    for (long sizeBytes : payloadSizeBytes) {
        PayloadInfo info = new PayloadInfo();
        info.setSizeBytes(sizeBytes);
        cacheRequest.addToPayloads(info);
    }
    return cacheRequest;
}
Also used : BuckCacheRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheRequest) PayloadInfo(com.facebook.buck.artifact_cache.thrift.PayloadInfo)

Example 3 with PayloadInfo

use of com.facebook.buck.artifact_cache.thrift.PayloadInfo in project buck by facebook.

the class HybridPayloadGenerator method createStoreRequest.

private BuckCacheRequest createStoreRequest() {
    BuckCacheRequest cacheRequest = new BuckCacheRequest();
    cacheRequest.setType(BuckCacheRequestType.STORE);
    BuckCacheStoreRequest storeRequest = new BuckCacheStoreRequest();
    ArtifactMetadata metadata = new ArtifactMetadata();
    RuleKey ruleKeyOne = new RuleKey();
    ruleKeyOne.setHashString(STORE_RULE_KEY_ONE);
    RuleKey ruleKeyTwo = new RuleKey();
    ruleKeyTwo.setHashString(STORE_RULE_KEY_TWO);
    List<RuleKey> ruleKeys = new ArrayList<>();
    ruleKeys.add(ruleKeyOne);
    ruleKeys.add(ruleKeyTwo);
    metadata.setRuleKeys(ruleKeys);
    Map<String, String> metadataMap = new HashMap<>();
    metadataMap.put(METDATA_KEY_ONE, METDATA_VALUE_ONE);
    metadataMap.put(METDATA_KEY_TWO, METDATA_VALUE_TWO);
    metadata.setRuleKeys(ruleKeys);
    metadata.setMetadata(metadataMap);
    List<PayloadInfo> payloadInfos = new ArrayList<>();
    PayloadInfo payloadInfo = new PayloadInfo();
    payloadInfo.setSizeBytes(PAYLOAD_ONE_BYTES.length);
    payloadInfos.add(payloadInfo);
    storeRequest.setMetadata(metadata);
    cacheRequest.setStoreRequest(storeRequest);
    cacheRequest.setPayloads(payloadInfos);
    return cacheRequest;
}
Also used : RuleKey(com.facebook.buck.artifact_cache.thrift.RuleKey) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BuckCacheStoreRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheStoreRequest) BuckCacheRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheRequest) PayloadInfo(com.facebook.buck.artifact_cache.thrift.PayloadInfo) ArtifactMetadata(com.facebook.buck.artifact_cache.thrift.ArtifactMetadata)

Example 4 with PayloadInfo

use of com.facebook.buck.artifact_cache.thrift.PayloadInfo in project buck by facebook.

the class ThriftArtifactCache method storeImpl.

@Override
protected void storeImpl(final ArtifactInfo info, final Path file, final HttpArtifactCacheEvent.Finished.Builder eventBuilder) throws IOException {
    final ByteSource artifact = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return projectFilesystem.newFileInputStream(file);
        }
    };
    BuckCacheStoreRequest storeRequest = new BuckCacheStoreRequest();
    ArtifactMetadata artifactMetadata = infoToMetadata(info, artifact, repository, scheduleType, distributedBuildModeEnabled);
    storeRequest.setMetadata(artifactMetadata);
    PayloadInfo payloadInfo = new PayloadInfo();
    long artifactSizeBytes = artifact.size();
    payloadInfo.setSizeBytes(artifactSizeBytes);
    BuckCacheRequest cacheRequest = new BuckCacheRequest();
    cacheRequest.addToPayloads(payloadInfo);
    cacheRequest.setType(BuckCacheRequestType.STORE);
    cacheRequest.setStoreRequest(storeRequest);
    if (LOG.isVerboseEnabled()) {
        LOG.verbose(String.format("Storing artifact with metadata: [%s].", ThriftUtil.thriftToDebugJson(artifactMetadata)));
    }
    final ThriftArtifactCacheProtocol.Request request = ThriftArtifactCacheProtocol.createRequest(PROTOCOL, cacheRequest, artifact);
    Request.Builder builder = toOkHttpRequest(request);
    eventBuilder.getStoreBuilder().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()) {
                reportFailure("Failed to store artifact with thriftErrorMessage=[%s] " + "url=[%s] artifactSizeBytes=[%d]", response.getThriftData().getErrorMessage(), httpResponse.requestUrl(), artifactSizeBytes);
            }
            eventBuilder.getStoreBuilder().setArtifactContentHash(storeRequest.getMetadata().artifactPayloadMd5);
            eventBuilder.getStoreBuilder().setWasStoreSuccessful(cacheResponse.isWasSuccessful());
            if (LOG.isDebugEnabled()) {
                LOG.debug("Debug info for cache store request: artifactMetadata=[%s] response=[%s]", ThriftUtil.thriftToDebugJson(artifactMetadata), ThriftUtil.thriftToDebugJson(cacheResponse));
            }
        }
    }
}
Also used : BuckCacheStoreRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheStoreRequest) Request(okhttp3.Request) BuckCacheRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheRequest) BuckCacheFetchRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheFetchRequest) HttpResponse(com.facebook.buck.slb.HttpResponse) BuckCacheRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheRequest) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) BuckCacheStoreRequest(com.facebook.buck.artifact_cache.thrift.BuckCacheStoreRequest) PayloadInfo(com.facebook.buck.artifact_cache.thrift.PayloadInfo) ArtifactMetadata(com.facebook.buck.artifact_cache.thrift.ArtifactMetadata) BuckCacheResponse(com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)

Aggregations

PayloadInfo (com.facebook.buck.artifact_cache.thrift.PayloadInfo)4 BuckCacheRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheRequest)3 ArtifactMetadata (com.facebook.buck.artifact_cache.thrift.ArtifactMetadata)2 BuckCacheResponse (com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)2 BuckCacheStoreRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheStoreRequest)2 BuckCacheFetchRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheFetchRequest)1 RuleKey (com.facebook.buck.artifact_cache.thrift.RuleKey)1 HttpResponse (com.facebook.buck.slb.HttpResponse)1 ByteSource (com.google.common.io.ByteSource)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Request (okhttp3.Request)1