Skip to content
Merged
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 @@ -1059,22 +1059,19 @@ public void fillBeToTablets(long be, long tableId, long partId, long indexId, lo
ConcurrentHashMap<Long, ConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>>>
partToTablets) {
// global
globalBeToTablets.putIfAbsent(be, ConcurrentHashMap.newKeySet());
globalBeToTablets.get(be).add(tabletId);
globalBeToTablets.computeIfAbsent(be, ignored -> ConcurrentHashMap.newKeySet()).add(tabletId);

// table
beToTabletsInTable.putIfAbsent(tableId, new ConcurrentHashMap<Long, Set<Long>>());
ConcurrentHashMap<Long, Set<Long>> beToTabletsOfTable = beToTabletsInTable.get(tableId);
beToTabletsOfTable.putIfAbsent(be, ConcurrentHashMap.newKeySet());
beToTabletsOfTable.get(be).add(tabletId);
ConcurrentHashMap<Long, Set<Long>> beToTabletsOfTable =
beToTabletsInTable.computeIfAbsent(tableId, ignored -> new ConcurrentHashMap<>());
beToTabletsOfTable.computeIfAbsent(be, ignored -> ConcurrentHashMap.newKeySet()).add(tabletId);

// partition
partToTablets.putIfAbsent(partId, new ConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>>());
ConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>> indexToTablets = partToTablets.get(partId);
indexToTablets.putIfAbsent(indexId, new ConcurrentHashMap<Long, Set<Long>>());
ConcurrentHashMap<Long, Set<Long>> beToTabletsOfIndex = indexToTablets.get(indexId);
beToTabletsOfIndex.putIfAbsent(be, ConcurrentHashMap.newKeySet());
beToTabletsOfIndex.get(be).add(tabletId);
ConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>> indexToTablets =
partToTablets.computeIfAbsent(partId, ignored -> new ConcurrentHashMap<>());
ConcurrentHashMap<Long, Set<Long>> beToTabletsOfIndex =
indexToTablets.computeIfAbsent(indexId, ignored -> new ConcurrentHashMap<>());
beToTabletsOfIndex.computeIfAbsent(be, ignored -> ConcurrentHashMap.newKeySet()).add(tabletId);
}

private void enqueueWarmupTask(WarmupTabletTask task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public class CloudTabletRebalancerTest {

Expand Down Expand Up @@ -81,6 +82,30 @@ protected boolean isInternalDbId(Long dbId) {
}
}

private static class CountingConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
private int computeIfAbsentCalls;
private int getCalls;
private int putIfAbsentCalls;

@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
computeIfAbsentCalls++;
return super.computeIfAbsent(key, mappingFunction);
}

@Override
public V get(Object key) {
getCalls++;
return super.get(key);
}

@Override
public V putIfAbsent(K key, V value) {
putIfAbsentCalls++;
return super.putIfAbsent(key, value);
}
}

private static void setField(Object obj, String name, Object value) throws Exception {
Field f = CloudTabletRebalancer.class.getDeclaredField(name);
f.setAccessible(true);
Expand All @@ -101,6 +126,50 @@ private static <T> T invokePrivate(Object obj, String method, Class<?>[] types,
return (T) m.invoke(obj, args);
}

@Test
public void testFillBeToTabletsUsesComputedContainers() {
TestRebalancer rebalancer = new TestRebalancer();
long beId = 1L;
long tableId = 2L;
long partitionId = 3L;
long indexId = 4L;

CountingConcurrentHashMap<Long, Set<Long>> globalBeToTablets = new CountingConcurrentHashMap<>();
CountingConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>> beToTabletsInTable =
new CountingConcurrentHashMap<>();
CountingConcurrentHashMap<Long, Set<Long>> beToTabletsOfTable = new CountingConcurrentHashMap<>();
beToTabletsInTable.put(tableId, beToTabletsOfTable);

CountingConcurrentHashMap<Long, ConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>>>
partToTablets = new CountingConcurrentHashMap<>();
CountingConcurrentHashMap<Long, ConcurrentHashMap<Long, Set<Long>>> indexToTablets =
new CountingConcurrentHashMap<>();
CountingConcurrentHashMap<Long, Set<Long>> beToTabletsOfIndex = new CountingConcurrentHashMap<>();
partToTablets.put(partitionId, indexToTablets);
indexToTablets.put(indexId, beToTabletsOfIndex);

rebalancer.fillBeToTablets(beId, tableId, partitionId, indexId, 5L,
globalBeToTablets, beToTabletsInTable, partToTablets);
rebalancer.fillBeToTablets(beId, tableId, partitionId, indexId, 6L,
globalBeToTablets, beToTabletsInTable, partToTablets);

assertComputedContainerUsed(globalBeToTablets);
assertComputedContainerUsed(beToTabletsInTable);
assertComputedContainerUsed(beToTabletsOfTable);
assertComputedContainerUsed(partToTablets);
assertComputedContainerUsed(indexToTablets);
assertComputedContainerUsed(beToTabletsOfIndex);
Assertions.assertEquals(Set.of(5L, 6L), globalBeToTablets.get(beId));
Assertions.assertEquals(Set.of(5L, 6L), beToTabletsOfTable.get(beId));
Assertions.assertEquals(Set.of(5L, 6L), beToTabletsOfIndex.get(beId));
}

private static void assertComputedContainerUsed(CountingConcurrentHashMap<?, ?> map) {
Assertions.assertEquals(2, map.computeIfAbsentCalls);
Assertions.assertEquals(0, map.putIfAbsentCalls);
Assertions.assertEquals(0, map.getCalls);
}

@Test
public void testPickTabletPreferCold_picksColdWhenAvailable() throws Exception {
TestRebalancer r = new TestRebalancer();
Expand Down
Loading