This is an automated email from the ASF dual-hosted git repository. jxue pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/helix.git
The following commit(s) were added to refs/heads/master by this push: new 0c2b1dba7 Cleanup - ExponenentialBackOff (#2469) 0c2b1dba7 is described below commit 0c2b1dba780067caad7730bf5de6c2c8a806bb43 Author: Komal Desai <98000016+desaiko...@users.noreply.github.com> AuthorDate: Tue May 2 15:11:09 2023 -0700 Cleanup - ExponenentialBackOff (#2469) Co-authored-by: Komal Desai <kde...@kdesai-mn1.linkedin.biz> --- .../helix/util/ExponentialBackoffStrategy.java | 31 ---------------------- .../apache/helix/zookeeper/zkclient/ZkClient.java | 6 ++--- .../zkclient/util/ExponentialBackoffStrategy.java | 23 +++++----------- 3 files changed, 10 insertions(+), 50 deletions(-) diff --git a/helix-core/src/main/java/org/apache/helix/util/ExponentialBackoffStrategy.java b/helix-core/src/main/java/org/apache/helix/util/ExponentialBackoffStrategy.java deleted file mode 100644 index 75c74487b..000000000 --- a/helix-core/src/main/java/org/apache/helix/util/ExponentialBackoffStrategy.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.apache.helix.util; - -/* - * 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. - */ - -/** - * Use ExponentialBackoffStrategy in zookeeper-api module instead. - */ -@Deprecated -public class ExponentialBackoffStrategy - extends org.apache.helix.zookeeper.zkclient.util.ExponentialBackoffStrategy { - public ExponentialBackoffStrategy(long maxRetryInterval, boolean addJitter) { - super(maxRetryInterval, addJitter); - } -} diff --git a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java index 527e46f90..0c896ce7f 100644 --- a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java +++ b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java @@ -1532,8 +1532,7 @@ public class ZkClient implements Watcher { return; } int retryCount = 0; - ExponentialBackoffStrategy retryStrategy = - new ExponentialBackoffStrategy(MAX_RECONNECT_INTERVAL_MS, true); + long currTime = System.currentTimeMillis(); Exception reconnectException = new ZkException("Shutdown triggered."); while (!isClosed()) { @@ -1545,7 +1544,8 @@ public class ZkClient implements Watcher { break; } catch (Exception e) { reconnectException = e; - long waitInterval = retryStrategy.getNextWaitInterval(retryCount++); + long waitInterval = ExponentialBackoffStrategy.getWaitInterval(currTime, + MAX_RECONNECT_INTERVAL_MS, true, retryCount++); LOG.warn("ZkClient {}, reconnect on expiring failed. Will retry after {} ms", _uid, waitInterval, e); try { diff --git a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/util/ExponentialBackoffStrategy.java b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/util/ExponentialBackoffStrategy.java index 13b288fb9..94f2f8da5 100644 --- a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/util/ExponentialBackoffStrategy.java +++ b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/util/ExponentialBackoffStrategy.java @@ -23,31 +23,22 @@ import java.util.Random; public class ExponentialBackoffStrategy { - private final long INIT_RETRY_INTERVAL = 500; - private final long _maxRetryInterval; - private final boolean _addJitter; - private final Random _ran; + private static long INIT_RETRY_INTERVAL = 500; - public ExponentialBackoffStrategy(long maxRetryInterval, boolean addJitter) { - _maxRetryInterval = maxRetryInterval; - _addJitter = addJitter; - _ran = new Random(System.currentTimeMillis()); - } - - public long getNextWaitInterval(int numberOfTriesFailed) { + public static long getWaitInterval(long currTime, long maxRetryInterval, boolean addJitter, int numberOfTriesFailed) { + Random ran = new Random(currTime); double exponentialMultiplier = Math.pow(2.0, numberOfTriesFailed - 1); double result = exponentialMultiplier * INIT_RETRY_INTERVAL; - if (_maxRetryInterval > 0 && result > _maxRetryInterval) { - result = _maxRetryInterval; + if (maxRetryInterval > 0 && result > maxRetryInterval) { + result = maxRetryInterval; } - if (_addJitter) { + if (addJitter) { // Adding jitter so the real result would be 75% to 100% of the original result. // Don't directly add jitter here, since it may exceed the max retry interval setup - result = result * (0.75 + _ran.nextDouble() % 0.25); + result = result * (0.75 + ran.nextDouble() % 0.25); } - return (long) result; } }