Abacn commented on code in PR #29877: URL: https://github.com/apache/beam/pull/29877#discussion_r1437277069
########## 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; Review Comment: Nonnull is default and should not be necessary. Also we use checkframework's Nonnull, not Java one ########## 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; Review Comment: It is important to note that this class does not own countersMap. Its more like a wrapper operating a single key in countersMap with Counter semantics. This looks a little bit unusual to me at first glance. May consider add some comments here ########## 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: Seems this is not yet used in other main scope codes. Is it planned as the next step? -- 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]
