You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/07/15 07:18:07 UTC

[GitHub] [iceberg] nastra commented on a diff in pull request #5268: Initial Table Scan Reporting support

nastra commented on code in PR #5268:
URL: https://github.com/apache/iceberg/pull/5268#discussion_r921883294


##########
api/src/main/java/org/apache/iceberg/metrics/DefaultTimer.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.iceberg.metrics;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.StringJoiner;
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Stopwatch;
+
+/**
+ * A default {@link Timer} implementation that uses a {@link Stopwatch} instance internally to measure time.
+ */
+public class DefaultTimer implements Timer {
+  private final TimeUnit defaultTimeUnit;
+  private final AtomicLong count = new AtomicLong();
+  private Duration duration = Duration.ZERO;
+  private final AtomicReference<Stopwatch> current = new AtomicReference<>();
+
+  public DefaultTimer(TimeUnit timeUnit) {
+    Preconditions.checkArgument(null != timeUnit, "TimeUnit must be non-null");
+    this.defaultTimeUnit = timeUnit;
+  }
+
+  @Override
+  public long count() {
+    return count.get();
+  }
+
+  @Override
+  public Duration totalDuration() {
+    return duration;
+  }
+
+  @Override
+  public void startTimer() {
+    boolean updated = current.compareAndSet(null, Stopwatch.createStarted());
+    Preconditions.checkState(updated, "startTimer() called multiple times");
+  }
+
+  @Override
+  public void stopTimer() {
+    Stopwatch stopwatch = current.getAndSet(null);
+    Preconditions.checkState(null != stopwatch, "startTimer() was not called");
+    record(stopwatch.stop().elapsed(defaultTimeUnit), defaultTimeUnit);
+  }
+
+  @Override
+  public void record(long amount, TimeUnit unit) {
+    if (amount >= 0) {
+      duration = duration.plus(amount, toChronoUnit(unit));
+      count.incrementAndGet();
+    }
+  }
+
+  @Override
+  public <T> T record(Supplier<T> supplier) {
+    startTimer();
+    try {
+      return supplier.get();
+    } finally {
+      stopTimer();
+    }
+  }
+
+  @Override
+  public <T> T recordCallable(Callable<T> callable) throws Exception {
+    startTimer();
+    try {
+      return callable.call();
+    } finally {
+      stopTimer();
+    }
+  }
+
+  @Override
+  public void record(Runnable runnable) {
+    startTimer();
+    try {
+      runnable.run();
+    } finally {
+      stopTimer();
+    }
+  }
+
+  @Override
+  public String toString() {
+    return new StringJoiner(", ", DefaultTimer.class.getSimpleName() + "[", "]")
+        .add("count=" + count)
+        .add("duration=" + duration)
+        .toString();
+  }

Review Comment:
   agreed, that looks much nicer



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org