001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import java.util.Set;
023import java.util.TreeSet;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.jupiter.api.Tag;
028import org.junit.jupiter.api.Test;
029
030@Tag(SmallTests.TAG)
031@Tag(ClientTests.TAG)
032public class TestConnectionUtils {
033
034  @Test
035  public void testRetryTimeJitter() {
036    long[] retries = new long[200];
037    long baseTime = 1000000; // Larger number than reality to help test randomness.
038    long maxTimeExpected = (long) (baseTime * 1.01f);
039    for (int i = 0; i < retries.length; i++) {
040      retries[i] = ConnectionUtils.getPauseTime(baseTime, 0);
041    }
042
043    Set<Long> retyTimeSet = new TreeSet<>();
044    for (long l : retries) {
045      /* make sure that there is some jitter but only 1% */
046      assertTrue(l >= baseTime);
047      assertTrue(l <= maxTimeExpected);
048      // Add the long to the set
049      retyTimeSet.add(l);
050    }
051
052    // Make sure that most are unique. some overlap will happen
053    assertTrue(retyTimeSet.size() > (retries.length * 0.80));
054  }
055
056  @Test
057  public void testGetPauseTime() {
058    long pauseTime;
059    long baseTime = 100;
060    pauseTime = ConnectionUtils.getPauseTime(baseTime, -1);
061    assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[0]));
062    assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[0] * 1.01f));
063
064    for (int i = 0; i < HConstants.RETRY_BACKOFF.length; i++) {
065      pauseTime = ConnectionUtils.getPauseTime(baseTime, i);
066      assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[i]));
067      assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[i] * 1.01f));
068    }
069
070    int length = HConstants.RETRY_BACKOFF.length;
071    pauseTime = ConnectionUtils.getPauseTime(baseTime, length);
072    assertTrue(pauseTime >= (baseTime * HConstants.RETRY_BACKOFF[length - 1]));
073    assertTrue(pauseTime <= (baseTime * HConstants.RETRY_BACKOFF[length - 1] * 1.01f));
074  }
075}