rdblue commented on code in PR #5268: URL: https://github.com/apache/iceberg/pull/5268#discussion_r922323024
########## api/src/main/java/org/apache/iceberg/metrics/Timer.java: ########## @@ -0,0 +1,156 @@ +/* + * 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.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +/** + * Generalized Timer interface for creating telemetry related instances for measuring duration of operations. + */ +public interface Timer { + + /** + * The number of times {@link Timer#record(Duration)} was called. + * + * @return The number of times {@link Timer#record(Duration)} was called. + */ + long count(); + + /** + * The total duration that was recorded. + * + * @return The total duration that was recorded. + */ + Duration totalDuration(); + + /** + * Starts the timer and returns a {@link Sample}. Call {@link Sample#stop(Timer)} to complete the timing. + * + * @return A timing {@link Sample} with the start time recorded. + */ + Sample start(); + + CloseableSample startCloseable(); + + /** + * Records a custom amount in the given time unit. + * + * @param amount The amount to record + * @param unit The time unit of the amount + */ + void record(long amount, TimeUnit unit); + + /** + * The duration to record + * + * @param duration The duration to record + */ + default void record(Duration duration) { + record(duration.toNanos(), TimeUnit.NANOSECONDS); + } + + /** + * Executes and measures the given {@link Runnable} instance. + * + * @param runnable The {@link Runnable} to execute and measure. + */ + void record(Runnable runnable); + + /** + * Executes and measures the given {@link Callable} and returns its result. + * + * @param callable The {@link Callable} to execute and measure. + * @param <T> The type of the {@link Callable} + * @return The result of the underlying {@link Callable}. + * @throws Exception In case the {@link Callable} fails. + */ + <T> T recordCallable(Callable<T> callable) throws Exception; + + /** + * Gets the result from the given {@link Supplier} and measures its execution time. + * + * @param supplier The {@link Supplier} to execute and measure. + * @param <T> The type of the {@link Supplier}. + * @return The result of the underlying {@link Supplier}. + */ + <T> T record(Supplier<T> supplier); + + /** + * A timing sample that carries internal state about the Timer's start position. The timing can be completed by + * calling {@link Sample#stop(Timer)}. + */ + interface Sample { + /** + * Stops the timer and records the total duration up until {@link Timer#start()} was called. + */ + void stop(Timer timer); + + Sample NOOP = timer -> { }; + } + + interface CloseableSample extends AutoCloseable { + @Override + void close(); + CloseableSample NOOP = () -> { }; + } + + Timer NOOP = new Timer() { + @Override + public Sample start() { + return Sample.NOOP; + } + + @Override + public CloseableSample startCloseable() { + return CloseableSample.NOOP; + } + + @Override + public long count() { + return 0; + } + + @Override + public Duration totalDuration() { + return Duration.ZERO; + } + + @Override + public void record(long amount, TimeUnit unit) { + } + + @Override + public void record(Runnable runnable) { Review Comment: As I mentioned above, I think that timing the execution of something is a different operation than recording an interval that was timed externally. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
