JayajP commented on code in PR #29877:
URL: https://github.com/apache/beam/pull/29877#discussion_r1437307913


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/RemoveSafeDeltaCounterCell.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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 java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.annotation.Nonnull;
+import org.apache.beam.runners.core.metrics.CounterCell;
+import org.apache.beam.runners.core.metrics.DirtyState;
+import org.apache.beam.runners.core.metrics.MetricCell;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.MetricName;
+
+/**
+ * Version of {@link CounterCell} supporting multi-thread safe mutations and 
extraction of delta
+ * values. {@link RemoveSafeDeltaCounterCell} allows safe deletions from the 
underlying {@code
+ * countersMap} through the {@code deleteIfZero} method.
+ */
+public class RemoveSafeDeltaCounterCell implements Counter, MetricCell<Long> {
+
+  private final MetricName metricName;
+  private final ConcurrentHashMap<MetricName, AtomicLong> countersMap;
+
+  public RemoveSafeDeltaCounterCell(
+      @Nonnull MetricName metricName, ConcurrentHashMap<MetricName, 
AtomicLong> countersMap) {
+    this.metricName = metricName;
+    this.countersMap = countersMap;
+  }
+
+  @Override
+  public void reset() {
+    countersMap.computeIfPresent(
+        metricName,
+        (unusedName, value) -> {
+          value.set(0);
+          return value;
+        });
+  }
+
+  @Override
+  public void inc(long n) {
+    countersMap.compute(
+        metricName,
+        (name, value) -> {
+          if (value == null) {
+            return new AtomicLong(n);
+          }
+          value.addAndGet(n);
+          return value;
+        });
+  }
+
+  @Override
+  public void inc() {
+    inc(1);
+  }
+
+  @Override
+  public void dec() {
+    inc(-1);
+  }
+
+  @Override
+  public void dec(long n) {
+    inc(-1 * n);
+  }
+
+  @Override
+  public MetricName getName() {
+    return metricName;
+  }
+
+  @Override
+  public DirtyState getDirty() {
+    throw new UnsupportedOperationException(
+        String.format("%s doesn't support the getDirty", 
getClass().getSimpleName()));
+  }
+
+  @Override
+  public Long getCumulative() {
+    throw new UnsupportedOperationException("getCumulative is not supported by 
Streaming Metrics");
+  }
+
+  /** Remove the metric from the {@code countersMap} if the the metric is zero 
valued. */
+  @SuppressWarnings(
+      "nullness") // computeIfPresent's remapping function can return null to 
remove the entry.
+  public void deleteIfZero() {

Review Comment:
   Yes this is next step, need to wait for my v1b3 api changes to be rolled out 
to the public api. 



-- 
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: [email protected]

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

Reply via email to