Search in sources :

Example 1 with ServiceCallMetric

use of org.apache.beam.runners.core.metrics.ServiceCallMetric in project beam by apache.

the class GcsUtil method open.

/**
 * Opens an object in GCS.
 *
 * <p>Returns a SeekableByteChannel that provides access to data in the bucket.
 *
 * @param path the GCS filename to read from
 * @param readOptions Fine-grained options for behaviors of retries, buffering, etc.
 * @return a SeekableByteChannel that can read the object data
 */
@VisibleForTesting
SeekableByteChannel open(GcsPath path, GoogleCloudStorageReadOptions readOptions) throws IOException {
    HashMap<String, String> baseLabels = new HashMap<>();
    baseLabels.put(MonitoringInfoConstants.Labels.PTRANSFORM, "");
    baseLabels.put(MonitoringInfoConstants.Labels.SERVICE, "Storage");
    baseLabels.put(MonitoringInfoConstants.Labels.METHOD, "GcsGet");
    baseLabels.put(MonitoringInfoConstants.Labels.RESOURCE, GcpResourceIdentifiers.cloudStorageBucket(path.getBucket()));
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_PROJECT_ID, googleCloudStorageOptions.getProjectId());
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_BUCKET, path.getBucket());
    ServiceCallMetric serviceCallMetric = new ServiceCallMetric(MonitoringInfoConstants.Urns.API_REQUEST_COUNT, baseLabels);
    try {
        SeekableByteChannel channel = googleCloudStorage.open(new StorageResourceId(path.getBucket(), path.getObject()), readOptions);
        serviceCallMetric.call("ok");
        return channel;
    } catch (IOException e) {
        if (e.getCause() instanceof GoogleJsonResponseException) {
            serviceCallMetric.call(((GoogleJsonResponseException) e.getCause()).getDetails().getCode());
        }
        throw e;
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HashMap(java.util.HashMap) IOException(java.io.IOException) StorageResourceId(com.google.cloud.hadoop.gcsio.StorageResourceId) ServiceCallMetric(org.apache.beam.runners.core.metrics.ServiceCallMetric) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 2 with ServiceCallMetric

use of org.apache.beam.runners.core.metrics.ServiceCallMetric in project beam by apache.

the class GcsUtil method create.

/**
 * Creates an object in GCS and prepares for uploading its contents.
 *
 * @param path the GCS file to write to
 * @param options to be used for creating and configuring file upload
 * @return a WritableByteChannel that can be used to write data to the object.
 */
public WritableByteChannel create(GcsPath path, CreateOptions options) throws IOException {
    AsyncWriteChannelOptions wcOptions = googleCloudStorageOptions.getWriteChannelOptions();
    @Nullable Integer uploadBufferSizeBytes = options.getUploadBufferSizeBytes() != null ? options.getUploadBufferSizeBytes() : getUploadBufferSizeBytes();
    if (uploadBufferSizeBytes != null) {
        wcOptions = wcOptions.toBuilder().setUploadChunkSize(uploadBufferSizeBytes).build();
    }
    GoogleCloudStorageOptions newGoogleCloudStorageOptions = googleCloudStorageOptions.toBuilder().setWriteChannelOptions(wcOptions).build();
    GoogleCloudStorage gcpStorage = new GoogleCloudStorageImpl(newGoogleCloudStorageOptions, this.storageClient, this.credentials);
    StorageResourceId resourceId = new StorageResourceId(path.getBucket(), path.getObject(), // See {@link GoogleCloudStorage#create(StorageResourceId, GoogleCloudStorageOptions)}
    options.getExpectFileToNotExist() ? 0L : StorageResourceId.UNKNOWN_GENERATION_ID);
    CreateObjectOptions.Builder createBuilder = CreateObjectOptions.builder().setOverwriteExisting(true);
    if (options.getContentType() != null) {
        createBuilder = createBuilder.setContentType(options.getContentType());
    }
    HashMap<String, String> baseLabels = new HashMap<>();
    baseLabels.put(MonitoringInfoConstants.Labels.PTRANSFORM, "");
    baseLabels.put(MonitoringInfoConstants.Labels.SERVICE, "Storage");
    baseLabels.put(MonitoringInfoConstants.Labels.METHOD, "GcsInsert");
    baseLabels.put(MonitoringInfoConstants.Labels.RESOURCE, GcpResourceIdentifiers.cloudStorageBucket(path.getBucket()));
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_PROJECT_ID, googleCloudStorageOptions.getProjectId());
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_BUCKET, path.getBucket());
    ServiceCallMetric serviceCallMetric = new ServiceCallMetric(MonitoringInfoConstants.Urns.API_REQUEST_COUNT, baseLabels);
    try {
        WritableByteChannel channel = gcpStorage.create(resourceId, createBuilder.build());
        serviceCallMetric.call("ok");
        return channel;
    } catch (IOException e) {
        if (e.getCause() instanceof GoogleJsonResponseException) {
            serviceCallMetric.call(((GoogleJsonResponseException) e.getCause()).getDetails().getCode());
        }
        throw e;
    }
}
Also used : GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) HashMap(java.util.HashMap) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) StorageResourceId(com.google.cloud.hadoop.gcsio.StorageResourceId) GoogleCloudStorageImpl(com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl) ServiceCallMetric(org.apache.beam.runners.core.metrics.ServiceCallMetric) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) AsyncWriteChannelOptions(com.google.cloud.hadoop.util.AsyncWriteChannelOptions) GoogleCloudStorageOptions(com.google.cloud.hadoop.gcsio.GoogleCloudStorageOptions) CreateObjectOptions(com.google.cloud.hadoop.gcsio.CreateObjectOptions) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 StorageResourceId (com.google.cloud.hadoop.gcsio.StorageResourceId)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ServiceCallMetric (org.apache.beam.runners.core.metrics.ServiceCallMetric)2 CreateObjectOptions (com.google.cloud.hadoop.gcsio.CreateObjectOptions)1 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)1 GoogleCloudStorageImpl (com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl)1 GoogleCloudStorageOptions (com.google.cloud.hadoop.gcsio.GoogleCloudStorageOptions)1 AsyncWriteChannelOptions (com.google.cloud.hadoop.util.AsyncWriteChannelOptions)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1