|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.raft; |
| 18 | + |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
| 21 | +import org.mockito.ArgumentCaptor; |
| 22 | +import org.mockito.Mockito; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Random; |
| 26 | + |
| 27 | +import static org.apache.kafka.raft.KafkaRaftClient.RETRY_BACKOFF_BASE_MS; |
| 28 | +import static org.apache.kafka.raft.RaftUtil.binaryExponentialElectionBackoffMs; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +public class RaftUtilTest { |
| 33 | + @ParameterizedTest |
| 34 | + @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}) |
| 35 | + public void testExponentialBoundOfExponentialElectionBackoffMs(int retries) { |
| 36 | + Random mockedRandom = Mockito.mock(Random.class); |
| 37 | + int electionBackoffMaxMs = 1000; |
| 38 | + |
| 39 | + // test the bound of the method's first call to random.nextInt |
| 40 | + binaryExponentialElectionBackoffMs(electionBackoffMaxMs, RETRY_BACKOFF_BASE_MS, retries, mockedRandom); |
| 41 | + ArgumentCaptor<Integer> nextIntCaptor = ArgumentCaptor.forClass(Integer.class); |
| 42 | + Mockito.verify(mockedRandom, Mockito.times(2)).nextInt(nextIntCaptor.capture()); |
| 43 | + List<Integer> allCapturedBounds = nextIntCaptor.getAllValues(); |
| 44 | + int actualBound = allCapturedBounds.get(0); |
| 45 | + int expectedBound = (int) (2 * Math.pow(2, retries - 1)); |
| 46 | + // after the 10th retry, the bound of the first call to random.nextInt will remain capped to |
| 47 | + // (RETRY_BACKOFF_BASE_MS * 2 << 10)=2048 to prevent overflow |
| 48 | + if (retries > 10) { |
| 49 | + expectedBound = 2048; |
| 50 | + } |
| 51 | + assertEquals(expectedBound, actualBound, "Incorrect bound for retries=" + retries); |
| 52 | + } |
| 53 | + |
| 54 | + // test that the return value of the method is capped to QUORUM_ELECTION_BACKOFF_MAX_MS_CONFIG + jitter |
| 55 | + // any exponential >= (1000 + jitter)/(RETRY_BACKOFF_BASE_MS) - 1 = 20 will result in this cap |
| 56 | + @ParameterizedTest |
| 57 | + @ValueSource(ints = {1, 2, 19, 20, 21, 2048}) |
| 58 | + public void testExponentialElectionBackoffMsIsCapped(int exponential) { |
| 59 | + Random mockedRandom = Mockito.mock(Random.class); |
| 60 | + int electionBackoffMaxMs = 1000; |
| 61 | + // this is the max bound of the method's first call to random.nextInt |
| 62 | + int firstNextIntMaxBound = 2048; |
| 63 | + |
| 64 | + int jitterMs = 50; |
| 65 | + Mockito.when(mockedRandom.nextInt(firstNextIntMaxBound)).thenReturn(exponential); |
| 66 | + Mockito.when(mockedRandom.nextInt(RETRY_BACKOFF_BASE_MS)).thenReturn(jitterMs); |
| 67 | + |
| 68 | + int returnedBackoffMs = binaryExponentialElectionBackoffMs(electionBackoffMaxMs, RETRY_BACKOFF_BASE_MS, 11, mockedRandom); |
| 69 | + |
| 70 | + // verify nextInt was called on both expected bounds |
| 71 | + ArgumentCaptor<Integer> nextIntCaptor = ArgumentCaptor.forClass(Integer.class); |
| 72 | + Mockito.verify(mockedRandom, Mockito.times(2)).nextInt(nextIntCaptor.capture()); |
| 73 | + List<Integer> allCapturedBounds = nextIntCaptor.getAllValues(); |
| 74 | + assertEquals(firstNextIntMaxBound, allCapturedBounds.get(0)); |
| 75 | + assertEquals(RETRY_BACKOFF_BASE_MS, allCapturedBounds.get(1)); |
| 76 | + |
| 77 | + // finally verify the backoff returned is capped to electionBackoffMaxMs + jitterMs |
| 78 | + int backoffValueCap = electionBackoffMaxMs + jitterMs; |
| 79 | + if (exponential < 20) { |
| 80 | + assertEquals(RETRY_BACKOFF_BASE_MS * (exponential + 1), returnedBackoffMs); |
| 81 | + assertTrue(returnedBackoffMs < backoffValueCap); |
| 82 | + } else { |
| 83 | + assertEquals(backoffValueCap, returnedBackoffMs); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments