-
Notifications
You must be signed in to change notification settings - Fork 961
#2974: Add hash-based journal selection for improved ledger distribution #4709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jprieto-temporal
wants to merge
3
commits into
apache:master
Choose a base branch
from
jprieto-temporal:jprieto/wxyxqoplswvq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...keeper-server/src/test/java/org/apache/bookkeeper/bookie/JournalIndexComputationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * | ||
| * 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 org.apache.bookkeeper.bookie; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| /** | ||
| * Unit tests for journal index computation in BookieImpl. | ||
| * | ||
| * <p>Verifies that hash-based journal selection provides a near-even | ||
| * distribution of ledgers across journals for various ID patterns. | ||
| */ | ||
| @RunWith(Parameterized.class) | ||
| public class JournalIndexComputationTest { | ||
|
|
||
| private static final double TOLERANCE = 0.10; | ||
| private static final int NUM_LEDGERS = 10000; | ||
|
|
||
| private final int numJournals; | ||
| private final int stride; | ||
|
|
||
| public JournalIndexComputationTest(int numJournals, int stride, String description) { | ||
| this.numJournals = numJournals; | ||
| this.stride = stride; | ||
| } | ||
|
|
||
| @Parameterized.Parameters(name = "{2}") | ||
| public static Collection<Object[]> parameters() { | ||
| return Arrays.asList(new Object[][] { | ||
| // Sequential IDs (stride=1) with various journal counts | ||
| {1, 1, "1 journal"}, | ||
| {3, 1, "3 journals, sequential"}, | ||
| {4, 1, "4 journals, sequential"}, | ||
| {7, 1, "7 journals (prime), sequential"}, | ||
| {8, 1, "8 journals, sequential"}, | ||
| {15, 1, "15 journals, sequential"}, | ||
|
|
||
| // Strided IDs | ||
| {4, 2, "4 journals, stride=2"}, | ||
| {4, 3, "4 journals, stride=3"}, | ||
| {4, 4, "4 journals, stride=4 (worst case without hash)"}, | ||
| {8, 8, "8 journals, stride=8 (worst case without hash)"}, | ||
| {4, 1024, "4 journals, stride=1024 (worst case without hash)"}, | ||
| {8, 1024, "8 journals, stride=1024 (worst case without hash)"}, | ||
| }); | ||
| } | ||
|
|
||
| // Tests that ledger IDs with the given stride distribute evenly across journals | ||
| // when hash-based selection is enabled. | ||
| @Test | ||
| public void testEvenDistributionWithHashing() { | ||
| int[] counts = new int[numJournals]; | ||
|
|
||
| for (int i = 0; i < NUM_LEDGERS; i++) { | ||
| long ledgerId = (long) i * stride; | ||
| int journalIndex = BookieImpl.computeJournalIndex(ledgerId, numJournals, true); | ||
| assertTrue("Index out of bounds", journalIndex >= 0 && journalIndex < numJournals); | ||
| counts[journalIndex]++; | ||
|
|
||
| // Verify determinism | ||
| assertEquals("Hash not deterministic", journalIndex, | ||
| BookieImpl.computeJournalIndex(ledgerId, numJournals, true)); | ||
| } | ||
|
|
||
| assertNearEvenDistribution(counts); | ||
| } | ||
|
|
||
| // Tests that without hashing, journal index is computed via simple modulo | ||
| @Test | ||
| public void testSimpleModuloWithoutHashing() { | ||
| for (int i = 0; i < NUM_LEDGERS; i++) { | ||
| long ledgerId = (long) i * stride; | ||
| int journalIndex = BookieImpl.computeJournalIndex(ledgerId, numJournals, false); | ||
| assertTrue("Index out of bounds", journalIndex >= 0 && journalIndex < numJournals); | ||
|
|
||
| // Verify determinism | ||
| assertEquals("Result not deterministic", journalIndex, | ||
| BookieImpl.computeJournalIndex(ledgerId, numJournals, false)); | ||
|
|
||
| // Verify it matches simple modulo for non-negative IDs | ||
| int expectedIndex = (int) (ledgerId % numJournals); | ||
| assertEquals("Should match simple modulo", expectedIndex, journalIndex); | ||
| } | ||
| } | ||
|
|
||
| // Tests that negative and very large ledger IDs produce valid indices for both | ||
| // hash and non-hash modes. | ||
| @Test | ||
| public void testEdgeCaseLedgerIds() { | ||
| for (boolean useHash : new boolean[] {true, false}) { | ||
| String mode = useHash ? "hash" : "modulo"; | ||
|
|
||
| for (long ledgerId = -100; ledgerId < 0; ledgerId++) { | ||
| int idx = BookieImpl.computeJournalIndex(ledgerId, numJournals, useHash); | ||
| assertTrue("Negative ID produced invalid index (" + mode + ")", | ||
| idx >= 0 && idx < numJournals); | ||
| } | ||
|
|
||
| for (int i = 0; i < 100; i++) { | ||
| long ledgerId = Long.MAX_VALUE - i; | ||
| int idx = BookieImpl.computeJournalIndex(ledgerId, numJournals, useHash); | ||
| assertTrue("Large ID produced invalid index (" + mode + ")", | ||
| idx >= 0 && idx < numJournals); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void assertNearEvenDistribution(int[] counts) { | ||
| int total = Arrays.stream(counts).sum(); | ||
| int expected = total / counts.length; | ||
| int maxDeviation = (int) (expected * TOLERANCE); | ||
|
|
||
| for (int i = 0; i < counts.length; i++) { | ||
| int deviation = Math.abs(counts[i] - expected); | ||
| assertTrue(String.format("journal %d: %d (expected ~%d, max deviation %d)", | ||
| i, counts[i], expected, maxDeviation), | ||
| deviation <= maxDeviation); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about putting this block into a static method so that we can add unit tests ?
The function gets the index and the number of journals and computes the journal index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review. I added the tests and factored out the static method.