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/14 20:31:47 UTC

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

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


##########
api/src/main/java/org/apache/iceberg/io/CloseableIterable.java:
##########
@@ -75,6 +75,31 @@ public CloseableIterator<E> iterator() {
     };
   }
 
+  /**
+   * Will run the given runnable when {@link CloseableIterable#close()} has been called.
+   *
+   * @param iterable             The underlying {@link CloseableIterable} to iterate over
+   * @param onCompletionRunnable The runnable to run after the underlying iterable was closed
+   * @param <E>                  The type of der underlying iterable
+   * @return A new {@link CloseableIterable} where the runnable will be executed
+   * as the final step after {@link CloseableIterable#close()} has been called
+   */
+  static <E> CloseableIterable<E> whenComplete(CloseableIterable<E> iterable, Runnable onCompletionRunnable) {
+    Preconditions.checkNotNull(onCompletionRunnable, "Cannot execute a null Runnable after completion");
+    return new CloseableIterable<E>() {
+      @Override
+      public void close() throws IOException {
+        iterable.close();
+        onCompletionRunnable.run();

Review Comment:
   Does the `onCompletionRunnable` need to happen in a `try-finally` block in case `iterable.close()` throws?
   
   Might be something you want to add a test for.



##########
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:
   Style / Non-blocking: I'm a big fan of using Guava's `MoreObjects.toStringHelper` like seen here:
   
   https://github.com/apache/iceberg/blob/90fe0edf1a671095e587a53adeb31cc84d01fb89/core/src/main/java/org/apache/iceberg/rest/responses/LoadTableResponse.java#L72-L78
   
   It more or less gives you the same result but is a lot less mental overhead in my opinion. Up to you if you use it or not though.



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