You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "m-trieu (via GitHub)" <gi...@apache.org> on 2024/04/04 00:04:28 UTC

Re: [PR] Implement LockFreeHistogram and use it for PerWorkerHistograms [beam]

m-trieu commented on code in PR #30769:
URL: https://github.com/apache/beam/pull/30769#discussion_r1550659948


##########
runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/LockFreeHistogramTest.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.runners.dataflow.worker;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+import java.util.Optional;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import org.apache.beam.sdk.metrics.MetricName;
+import org.apache.beam.sdk.util.HistogramData;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.primitives.ImmutableLongArray;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link LockFreeHistogram}. */
+@RunWith(JUnit4.class)
+public class LockFreeHistogramTest {
+
+  @Test
+  public void testUpdate_OverflowValues() {
+    HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(0, 10, 3);
+    LockFreeHistogram histogram =
+        new LockFreeHistogram(KV.of(MetricName.named("name", "namespace"), bucketType));
+    histogram.update(35, 40, 45);
+    Optional<LockFreeHistogram.Snapshot> snapshot = histogram.getSnapshotAndReset();
+
+    LockFreeHistogram.OutlierStatistic expectedOverflow =
+        LockFreeHistogram.OutlierStatistic.create(120.0, 3L);
+    LockFreeHistogram.OutlierStatistic expectedUnderflow = LockFreeHistogram.OutlierStatistic.EMPTY;
+    ImmutableLongArray expectedBuckets = ImmutableLongArray.of(0L, 0L, 0L);
+    LockFreeHistogram.Snapshot expectedSnapshot =
+        LockFreeHistogram.Snapshot.create(
+            expectedUnderflow, expectedOverflow, expectedBuckets, bucketType);
+
+    assertThat(snapshot.isPresent(), equalTo(true));
+    assertThat(snapshot.get(), equalTo(expectedSnapshot));
+    assertThat(snapshot.get().underflowStatistic().mean(), equalTo(0.0));
+    assertThat(snapshot.get().overflowStatistic(), equalTo(expectedOverflow));
+  }
+
+  @Test
+  public void testUpdate_UnderflowValues() {
+    HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(100, 10, 3);
+    LockFreeHistogram histogram =
+        new LockFreeHistogram(KV.of(MetricName.named("name", "namespace"), bucketType));
+    histogram.update(35, 40, 45);
+    Optional<LockFreeHistogram.Snapshot> snapshot = histogram.getSnapshotAndReset();
+
+    LockFreeHistogram.OutlierStatistic expectedUnderflow =
+        LockFreeHistogram.OutlierStatistic.create(120.0, 3L);
+    LockFreeHistogram.OutlierStatistic expectedOverflow = LockFreeHistogram.OutlierStatistic.EMPTY;
+    ImmutableLongArray expectedBuckets = ImmutableLongArray.of(0L, 0L, 0L);
+    LockFreeHistogram.Snapshot expectedSnapshot =
+        LockFreeHistogram.Snapshot.create(
+            expectedUnderflow, expectedOverflow, expectedBuckets, bucketType);
+
+    assertThat(snapshot.isPresent(), equalTo(true));
+    assertThat(snapshot.get(), equalTo(expectedSnapshot));
+    assertThat(snapshot.get().underflowStatistic(), equalTo(expectedUnderflow));
+  }
+
+  @Test
+  public void testUpdate_InBoundsValues() {
+    HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(0, 10, 3);
+    LockFreeHistogram histogram =
+        new LockFreeHistogram(KV.of(MetricName.named("name", "namespace"), bucketType));
+    histogram.update(5, 15, 25);
+    Optional<LockFreeHistogram.Snapshot> snapshot = histogram.getSnapshotAndReset();
+
+    LockFreeHistogram.OutlierStatistic expectedOverflow = LockFreeHistogram.OutlierStatistic.EMPTY;
+    LockFreeHistogram.OutlierStatistic expectedUnderflow = LockFreeHistogram.OutlierStatistic.EMPTY;
+    ImmutableLongArray expectedBuckets = ImmutableLongArray.of(1L, 1L, 1L);
+    LockFreeHistogram.Snapshot expectedSnapshot =
+        LockFreeHistogram.Snapshot.create(
+            expectedUnderflow, expectedOverflow, expectedBuckets, bucketType);
+
+    assertThat(snapshot.isPresent(), equalTo(true));
+    assertThat(snapshot.get(), equalTo(expectedSnapshot));
+  }
+
+  @Test
+  public void testUpdate_EmptySnapshot() {
+    HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(0, 10, 3);
+    LockFreeHistogram histogram =
+        new LockFreeHistogram(KV.of(MetricName.named("name", "namespace"), bucketType));
+    histogram.update(5, 15, 25);
+    Optional<LockFreeHistogram.Snapshot> snapshot_1 = histogram.getSnapshotAndReset();
+
+    assertThat(snapshot_1.isPresent(), equalTo(true));
+
+    Optional<LockFreeHistogram.Snapshot> snapshot_2 = histogram.getSnapshotAndReset();
+    assertThat(snapshot_2.isPresent(), equalTo(false));
+  }
+
+  /** A runnable records 200 values and then calls getSnapshotAndReset. */
+  private static class UpdateHistogramRunnable implements Runnable {
+    private final LockFreeHistogram histogram;
+    private final int val;
+    private final CountDownLatch startSignal;
+    private final CountDownLatch doneSignal;
+    private Optional<LockFreeHistogram.Snapshot> snapshot;
+
+    private static final long valuesRecorded = 200L;
+
+    public UpdateHistogramRunnable(
+        LockFreeHistogram histogram,
+        int val,
+        CountDownLatch startSignal,
+        CountDownLatch doneSignal) {
+      this.histogram = histogram;
+      this.val = val;
+      this.startSignal = startSignal;
+      this.doneSignal = doneSignal;
+      this.snapshot = Optional.empty();
+    }
+
+    @Override
+    public void run() {
+      try {
+        startSignal.await();
+      } catch (InterruptedException e) {
+        return;
+      }
+
+      for (long j = 0; j < valuesRecorded; j++) {
+        histogram.update(val);
+      }
+      snapshot = histogram.getSnapshotAndReset();
+
+      doneSignal.countDown();
+    }
+
+    public long totalCountInSnapshot() {
+      if (snapshot.isPresent()) {
+        return snapshot.get().totalCount();
+      }
+      return 0;
+    }
+
+    public static long numValuesRecorded() {
+      return valuesRecorded;
+    }
+  }
+
+  @Test
+  public void testUpdateAndSnapshots_MultipleThreads() {
+    int numRunnables = 200;
+    CountDownLatch startSignal = new CountDownLatch(1);
+    CountDownLatch doneSignal = new CountDownLatch(numRunnables);
+    ExecutorService executor = Executors.newFixedThreadPool(numRunnables);
+
+    HistogramData.BucketType bucketType = HistogramData.ExponentialBuckets.of(1, 10);
+    LockFreeHistogram histogram =
+        new LockFreeHistogram(KV.of(MetricName.named("name", "namespace"), bucketType));
+
+    UpdateHistogramRunnable[] runnables = new UpdateHistogramRunnable[numRunnables];
+    for (int i = 0; i < numRunnables; i++) {
+      runnables[i] = new UpdateHistogramRunnable(histogram, i, startSignal, doneSignal);
+      executor.execute(runnables[i]);

Review Comment:
   you will want to use executor.submit in the loop
   
   or create the array and then use executor.invokeAll() here
   
   call execute 1 at a time will still execute it sequentially on 1 thread since execute blocks



-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org