|
| 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 | + |
| 18 | +package org.apache.fluss.client.table.scanner.log; |
| 19 | + |
| 20 | +import org.apache.fluss.metadata.TableBucket; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +/** Tests for {@link BucketFetchRateController}. */ |
| 28 | +class BucketFetchRateControllerTest { |
| 29 | + |
| 30 | + private static final int MAX_SKIP_ROUNDS = 32; |
| 31 | + private BucketFetchRateController controller; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setup() { |
| 35 | + controller = new BucketFetchRateController(MAX_SKIP_ROUNDS); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testNewBucketShouldAlwaysFetch() { |
| 40 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 41 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testBucketWithDataResetsCoolDown() { |
| 46 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 47 | + |
| 48 | + // Record multiple empty fetches to build up cool down |
| 49 | + controller.recordFetchResult(tb, false); |
| 50 | + controller.recordFetchResult(tb, false); |
| 51 | + controller.recordFetchResult(tb, false); |
| 52 | + assertThat(controller.getConsecutiveEmptyFetches(tb)).isEqualTo(3); |
| 53 | + assertThat(controller.getRemainingSkipRounds(tb)).isGreaterThan(0); |
| 54 | + |
| 55 | + // Now record a fetch with data |
| 56 | + controller.recordFetchResult(tb, true); |
| 57 | + assertThat(controller.getConsecutiveEmptyFetches(tb)).isEqualTo(0); |
| 58 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(0); |
| 59 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testExponentialBackoff() { |
| 64 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 65 | + |
| 66 | + // 1st empty fetch: skip 1 round (2^0 = 1) |
| 67 | + controller.recordFetchResult(tb, false); |
| 68 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(1); |
| 69 | + |
| 70 | + // Consume the skip round |
| 71 | + assertThat(controller.shouldFetch(tb)).isFalse(); |
| 72 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 73 | + |
| 74 | + // 2nd empty fetch: skip 2 rounds (2^1 = 2) |
| 75 | + controller.recordFetchResult(tb, false); |
| 76 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(2); |
| 77 | + |
| 78 | + // Consume the 2 skip rounds |
| 79 | + assertThat(controller.shouldFetch(tb)).isFalse(); |
| 80 | + assertThat(controller.shouldFetch(tb)).isFalse(); |
| 81 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 82 | + |
| 83 | + // 3rd empty fetch: skip 4 rounds (2^2 = 4) |
| 84 | + controller.recordFetchResult(tb, false); |
| 85 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(4); |
| 86 | + |
| 87 | + for (int i = 0; i < 4; i++) { |
| 88 | + assertThat(controller.shouldFetch(tb)).isFalse(); |
| 89 | + } |
| 90 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testMaxSkipRoundsCap() { |
| 95 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 96 | + |
| 97 | + // Record many empty fetches to exceed the max backoff shift |
| 98 | + for (int i = 0; i < 20; i++) { |
| 99 | + controller.recordFetchResult(tb, false); |
| 100 | + // Consume all skip rounds |
| 101 | + while (!controller.shouldFetch(tb)) { |
| 102 | + // draining |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // After 20 empty fetches, skip rounds should be capped at MAX_SKIP_ROUNDS |
| 107 | + controller.recordFetchResult(tb, false); |
| 108 | + assertThat(controller.getRemainingSkipRounds(tb)).isLessThanOrEqualTo(MAX_SKIP_ROUNDS); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testMultipleBucketsIndependent() { |
| 113 | + TableBucket tb1 = new TableBucket(1L, 0L, 0); |
| 114 | + TableBucket tb2 = new TableBucket(1L, 1L, 0); |
| 115 | + |
| 116 | + // tb1 gets empty fetches, tb2 gets data |
| 117 | + controller.recordFetchResult(tb1, false); |
| 118 | + controller.recordFetchResult(tb1, false); |
| 119 | + controller.recordFetchResult(tb2, true); |
| 120 | + |
| 121 | + // tb1 should be in cool down |
| 122 | + assertThat(controller.getConsecutiveEmptyFetches(tb1)).isEqualTo(2); |
| 123 | + assertThat(controller.getRemainingSkipRounds(tb1)).isGreaterThan(0); |
| 124 | + |
| 125 | + // tb2 should be fetch able immediately |
| 126 | + assertThat(controller.getConsecutiveEmptyFetches(tb2)).isEqualTo(0); |
| 127 | + assertThat(controller.shouldFetch(tb2)).isTrue(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testRemoveBucket() { |
| 132 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 133 | + |
| 134 | + controller.recordFetchResult(tb, false); |
| 135 | + assertThat(controller.getConsecutiveEmptyFetches(tb)).isEqualTo(1); |
| 136 | + |
| 137 | + controller.removeBucket(tb); |
| 138 | + assertThat(controller.getConsecutiveEmptyFetches(tb)).isEqualTo(0); |
| 139 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void testReset() { |
| 144 | + TableBucket tb1 = new TableBucket(1L, 0L, 0); |
| 145 | + TableBucket tb2 = new TableBucket(1L, 1L, 0); |
| 146 | + |
| 147 | + controller.recordFetchResult(tb1, false); |
| 148 | + controller.recordFetchResult(tb2, false); |
| 149 | + |
| 150 | + controller.reset(); |
| 151 | + |
| 152 | + assertThat(controller.getConsecutiveEmptyFetches(tb1)).isEqualTo(0); |
| 153 | + assertThat(controller.getConsecutiveEmptyFetches(tb2)).isEqualTo(0); |
| 154 | + assertThat(controller.shouldFetch(tb1)).isTrue(); |
| 155 | + assertThat(controller.shouldFetch(tb2)).isTrue(); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testShouldFetchDecrementsCounter() { |
| 160 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 161 | + |
| 162 | + // 1 empty fetch -> 1 skip round |
| 163 | + controller.recordFetchResult(tb, false); |
| 164 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(1); |
| 165 | + |
| 166 | + // shouldFetch returns false and decrements to 0 |
| 167 | + assertThat(controller.shouldFetch(tb)).isFalse(); |
| 168 | + assertThat(controller.getRemainingSkipRounds(tb)).isEqualTo(0); |
| 169 | + |
| 170 | + // Now it should be fetch able |
| 171 | + assertThat(controller.shouldFetch(tb)).isTrue(); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void testSmallMaxSkipRounds() { |
| 176 | + BucketFetchRateController smallController = new BucketFetchRateController(4); |
| 177 | + TableBucket tb = new TableBucket(1L, 0L, 0); |
| 178 | + |
| 179 | + // Record many empty fetches |
| 180 | + for (int i = 0; i < 10; i++) { |
| 181 | + smallController.recordFetchResult(tb, false); |
| 182 | + // Drain skip rounds |
| 183 | + while (!smallController.shouldFetch(tb)) { |
| 184 | + // draining |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // Even after many empty fetches, skip rounds should be capped at 4 |
| 189 | + smallController.recordFetchResult(tb, false); |
| 190 | + assertThat(smallController.getRemainingSkipRounds(tb)).isLessThanOrEqualTo(4); |
| 191 | + } |
| 192 | +} |
0 commit comments