8000 HDFS-17049. EC: Fix duplicate block group IDs generated by SequentialBlockGroupIdGenerator. by zhangshuyan0 · Pull Request #5743 · apache/hadoop · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

HDFS-17049. EC: Fix duplicate block group IDs generated by SequentialBlockGroupIdGenerator. #5743

New issue

Have a question about 8000 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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SequentialBlockGroupIdGenerator extends SequentialNumber {
}

@Override // NumberGenerator
public long nextValue() {
public synchronized long nextValue() {
skipTo((getCurrentValue() & ~BLOCK_GROUP_INDEX_MASK) + MAX_BLOCKS_IN_GROUP);
// Make sure there's no conflict with existing random block IDs
final Block b = new Block(getCurrentValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -172,6 +176,43 @@ public void testTriggerBlockGroupIdCollision() throws IOException {
}
}

/**
* Test that the values generated by blockGroup ID generator are unique,
* even if they are generated concurrently.
* @throws Exception
*/
@Test
public void testBlockGroupIdThreadSafety() throws Exception {
// Each thread use a list to store its own block group IDs.
List<List<Long>> blockIds = new ArrayList<>();
List<Thread> threads = new ArrayList<>();

for (int i = 0; i < 20; i++) {
blockIds.add(new ArrayList<>());
threads.add(new Thread(() -> {
for (int j = 0; j < 1000; j++) {
long next = blockGrpIdGenerator.nextValue();
blockIds.get(j).add(next);
}
}));
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
// Check if there are duplicate IDs.
Set<Long> allBlockIds = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here will be more readable?

    Set<Long> allBlockIds = new HashSet<>();
    for (List<Long> set : blockIds) {
      for (long id : set) {
        if (!allBlockIds.add(id)) {
          fail("Same block group id is generated!");
        }
      }
    }

Any more simple check way? Such as using HashMap directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm not understand how to use HashMap to get a more simple implement. I just modified the code as you did above.

for (List<Long> set : blockIds) {
for (long id : set) {
if (!allBlockIds.add(id)) {
fail("Same block group id is generated!");
}
}
}
}

/**
* Test that collisions in the blockGroup ID when the id is occupied by legacy
* block.
Expand Down
0