ajamato commented on a change in pull request #12609:
URL: https://github.com/apache/beam/pull/12609#discussion_r474338081



##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryServicesImpl.java
##########
@@ -131,7 +133,12 @@ public JobService getJobService(BigQueryOptions options) {
 
   @Override
   public DatasetService getDatasetService(BigQueryOptions options) {
-    return new DatasetServiceImpl(options);
+    return new DatasetServiceImpl(options, null);
+  }
+
+  @Override
+  public DatasetService getDatasetService(BigQueryOptions options, Histogram 
histogram) {

Review comment:
       I see, could you rename the variable from "histogram" to something 
describing what the histogram is for, like "requestLatencies", 
"requestLatenciesHistogram"

##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/util/Histogram.java
##########
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.util;
+
+import com.google.auto.value.AutoValue;
+import java.math.RoundingMode;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.math.DoubleMath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A histogram that supports estimated percentile with linear interpolation.
+ *
+ * <p>We may consider using Apache Commons or HdrHistogram library in the 
future for advanced
+ * features such as sparsely populated histograms.
+ */
+public class Histogram {
+  private static final Logger LOG = LoggerFactory.getLogger(Histogram.class);
+
+  private final BucketType bucketType;
+
+  private long[] buckets;
+  private long numOfRecords;
+  private long numTopRecords;
+  private long numBottomRecords;
+
+  private Histogram(BucketType bucketType) {
+    this.bucketType = bucketType;
+    this.buckets = new long[bucketType.getNumBuckets()];
+    this.numOfRecords = 0;
+    this.numTopRecords = 0;
+    this.numBottomRecords = 0;
+  }
+
+  /**
+   * Create a histogram with linear buckets.
+   *
+   * @param start Lower bound of a starting bucket.
+   * @param width Bucket width. Smaller width implies a better resolution for 
percentile estimation.
+   * @param numBuckets The number of buckets. Upper bound of an ending bucket 
is defined by start +
+   *     width * numBuckets.
+   * @return a new Histogram instance.
+   */
+  public static Histogram linear(double start, double width, int numBuckets) {
+    return new Histogram(LinearBuckets.of(start, width, numBuckets));
+  }
+
+  public void record(double... values) {
+    for (double value : values) {
+      record(value);
+    }
+  }
+
+  public synchronized void clear() {
+    this.buckets = new long[bucketType.getNumBuckets()];
+    this.numOfRecords = 0;
+    this.numTopRecords = 0;
+    this.numBottomRecords = 0;
+  }
+
+  public synchronized void record(double value) {
+    double rangeTo = bucketType.getRangeTo();
+    double rangeFrom = bucketType.getRangeFrom();
+    if (value >= rangeTo) {
+      LOG.warn("record is out of upper bound {}: {}", rangeTo, value);
+      numTopRecords++;
+    } else if (value < rangeFrom) {
+      LOG.warn("record is out of lower bound {}: {}", rangeFrom, value);
+      numBottomRecords++;
+    } else {
+      buckets[bucketType.getBucketIndex(value)]++;
+      numOfRecords++;
+    }
+  }
+
+  public synchronized long getTotalCount() {
+    return numOfRecords + numTopRecords + numBottomRecords;
+  }
+
+  public synchronized long getCount(int bucketIndex) {
+    return buckets[bucketIndex];

Review comment:
       What's the usage pattern here? If you call this one after another for 
each bucket index, then the histogram can be modified between calls, by another 
thread.
   Make this private if its only called within the class (which would mean its 
a non issue)
   
   It would be better to copy and return the buckets in a single get call 
instead
   
   Though, if you think that's bad for performance, then this might be fine. 
Though please add a comment warning about this potential threading issue




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to