From 6e0fd0869557ce512046bbebae0b7ac943261ce3 Mon Sep 17 00:00:00 2001 From: zhangjunfan Date: Sun, 20 Feb 2022 21:14:28 +0800 Subject: [PATCH] Remove the duplicate logic code Signed-off-by: zhangjunfan --- .../tony/util/CallableWithoutException.java | 26 ++++++++++++++++ .../java/com/linkedin/tony/util/Function.java | 26 ++++++++++++++++ .../java/com/linkedin/tony/util/Utils.java | 30 ++++++------------- 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 tony-core/src/main/java/com/linkedin/tony/util/CallableWithoutException.java create mode 100644 tony-core/src/main/java/com/linkedin/tony/util/Function.java diff --git a/tony-core/src/main/java/com/linkedin/tony/util/CallableWithoutException.java b/tony-core/src/main/java/com/linkedin/tony/util/CallableWithoutException.java new file mode 100644 index 00000000..62716e00 --- /dev/null +++ b/tony-core/src/main/java/com/linkedin/tony/util/CallableWithoutException.java @@ -0,0 +1,26 @@ +/* + * Copyright 2022 LinkedIn Corp. + * + * Licensed 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 com.linkedin.tony.util; + +@FunctionalInterface +public interface CallableWithoutException { + /** + * Computes a result, or throws an exception if unable to do so. + * + * @return computed result + */ + V call(); +} \ No newline at end of file diff --git a/tony-core/src/main/java/com/linkedin/tony/util/Function.java b/tony-core/src/main/java/com/linkedin/tony/util/Function.java new file mode 100644 index 00000000..a94faf29 --- /dev/null +++ b/tony-core/src/main/java/com/linkedin/tony/util/Function.java @@ -0,0 +1,26 @@ +/* + * Copyright 2022 LinkedIn Corp. + * + * Licensed 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 com.linkedin.tony.util; + +@FunctionalInterface +public interface Function { + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + */ + R apply(T t); +} \ No newline at end of file diff --git a/tony-core/src/main/java/com/linkedin/tony/util/Utils.java b/tony-core/src/main/java/com/linkedin/tony/util/Utils.java index 0dbe71f6..2f11a6c5 100644 --- a/tony-core/src/main/java/com/linkedin/tony/util/Utils.java +++ b/tony-core/src/main/java/com/linkedin/tony/util/Utils.java @@ -97,24 +97,7 @@ public class Utils { * @throws IllegalArgumentException if {@code interval} or {@code timeout} is negative */ public static boolean poll(Callable func, int interval, int timeout) { - Preconditions.checkArgument(interval >= 0, "Interval must be non-negative."); - Preconditions.checkArgument(timeout >= 0, "Timeout must be non-negative."); - - int remainingTime = timeout; - try { - while (timeout == 0 || remainingTime >= 0) { - if (func.call()) { - LOG.info("Poll function finished within " + timeout + " seconds"); - return true; - } - Thread.sleep(interval * 1000); - remainingTime -= interval; - } - } catch (Exception e) { - LOG.error("Polled function threw exception.", e); - } - LOG.warn("Function didn't return true within " + timeout + " seconds."); - return false; + return pollTillConditionReached(func, result -> result, () -> false, interval, timeout); } /** @@ -130,6 +113,11 @@ public static boolean poll(Callable func, int interval, int timeout) { * @throws IllegalArgumentException if {@code interval} or {@code timeout} is negative */ public static T pollTillNonNull(Callable func, int interval, int timeout) { + return pollTillConditionReached(func, Objects::nonNull, () -> null, interval, timeout); + } + + public static T pollTillConditionReached(Callable callFunc, Function conditionFunc, + CallableWithoutException defaultReturnedFunc, int interval, int timeout) { Preconditions.checkArgument(interval >= 0, "Interval must be non-negative."); Preconditions.checkArgument(timeout >= 0, "Timeout must be non-negative."); @@ -137,8 +125,8 @@ public static T pollTillNonNull(Callable func, int interval, int timeout) T ret; try { while (timeout == 0 || remainingTime >= 0) { - ret = func.call(); - if (ret != null) { + ret = callFunc.call(); + if (conditionFunc.apply(ret)) { LOG.info("pollTillNonNull function finished within " + timeout + " seconds"); return ret; } @@ -149,7 +137,7 @@ public static T pollTillNonNull(Callable func, int interval, int timeout) LOG.error("pollTillNonNull function threw exception", e); } LOG.warn("Function didn't return non-null within " + timeout + " seconds."); - return null; + return defaultReturnedFunc.call(); } public static String parseMemoryString(String memory) {