mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
backoff changes
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
parent
f85ac6adf9
commit
864afda935
@ -56,7 +56,6 @@ public class ConstantTimeBackoff extends AdapterBase implements BackoffAlgorithm
|
||||
} finally {
|
||||
_asleep.remove(current.getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -36,7 +36,7 @@ import com.cloud.utils.component.AdapterBase;
|
||||
*
|
||||
**/
|
||||
public class RangeTimeBackoff extends AdapterBase implements BackoffAlgorithm {
|
||||
private static final int DEFAULT_MIN_TIME = 5;
|
||||
protected static final int DEFAULT_MIN_TIME = 5;
|
||||
private int minTime = DEFAULT_MIN_TIME;
|
||||
private int maxTime = 3 * DEFAULT_MIN_TIME;
|
||||
private final Map<String, Thread> asleep = new ConcurrentHashMap<>();
|
||||
@ -44,11 +44,12 @@ public class RangeTimeBackoff extends AdapterBase implements BackoffAlgorithm {
|
||||
|
||||
@Override
|
||||
public void waitBeforeRetry() {
|
||||
long time = minTime * 1000L;
|
||||
Thread current = Thread.currentThread();
|
||||
try {
|
||||
asleep.put(current.getName(), current);
|
||||
long time = ThreadLocalRandom.current().nextInt(minTime, maxTime) * 1000L;
|
||||
LOG.info("Waiting " + current.getName() + " for " + time);
|
||||
time = ThreadLocalRandom.current().nextInt(minTime, maxTime) * 1000L;
|
||||
LOG.debug("Waiting " + current.getName() + " for " + time);
|
||||
Thread.sleep(time);
|
||||
} catch (InterruptedException e) {
|
||||
// JMX or other threads may interrupt this thread, but let's log it
|
||||
@ -57,7 +58,6 @@ public class RangeTimeBackoff extends AdapterBase implements BackoffAlgorithm {
|
||||
} finally {
|
||||
asleep.remove(current.getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
// 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 com.cloud.utils.backoff.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RangeTimeBackoffTest {
|
||||
|
||||
@Test
|
||||
public void testWaitValidValue() {
|
||||
RangeTimeBackoff backoff = new RangeTimeBackoff();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
int min = 5;
|
||||
int max = 10;
|
||||
map.put("minSeconds", String.valueOf(min));
|
||||
map.put("maxSeconds", String.valueOf(max));
|
||||
backoff.configure("RangeTimeBackoff", map);
|
||||
long startTime = System.currentTimeMillis();
|
||||
backoff.waitBeforeRetry();
|
||||
long timeTaken = System.currentTimeMillis() - startTime;
|
||||
Assert.assertTrue(timeTaken >= min * 1000L);
|
||||
Assert.assertTrue(timeTaken <= max * 1000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWaitEmptyValue() {
|
||||
RangeTimeBackoff backoff = new RangeTimeBackoff();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("minSeconds", "");
|
||||
map.put("maxSeconds", "");
|
||||
backoff.configure("RangeTimeBackoff", map);
|
||||
long startTime = System.currentTimeMillis();
|
||||
backoff.waitBeforeRetry();
|
||||
long timeTaken = System.currentTimeMillis() - startTime;
|
||||
Assert.assertTrue(timeTaken >= RangeTimeBackoff.DEFAULT_MIN_TIME * 1000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWaitNullValue() {
|
||||
RangeTimeBackoff backoff = new RangeTimeBackoff();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
backoff.configure("RangeTimeBackoff", map);
|
||||
long startTime = System.currentTimeMillis();
|
||||
backoff.waitBeforeRetry();
|
||||
long timeTaken = System.currentTimeMillis() - startTime;
|
||||
Assert.assertTrue(timeTaken >= RangeTimeBackoff.DEFAULT_MIN_TIME * 1000L);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user