@Test public void testAssignMigrations() { servers.clear(); servers.put(new TServerInstance(HostAndPort.fromParts("127.0.0.1", 1234), "a"), new FakeTServer()); servers.put(new TServerInstance(HostAndPort.fromParts("127.0.0.1", 1235), "b"), new FakeTServer()); servers.put(new TServerInstance(HostAndPort.fromParts("127.0.0.1", 1236), "c"), new FakeTServer()); Map<KeyExtent,TServerInstance> metadataTable = new TreeMap<>(); String table = "t1"; metadataTable.put(makeExtent(table, null, null), null); table = "t2"; metadataTable.put(makeExtent(table, "a", null), null); metadataTable.put(makeExtent(table, null, "a"), null); table = "t3"; metadataTable.put(makeExtent(table, "a", null), null); metadataTable.put(makeExtent(table, "b", "a"), null); metadataTable.put(makeExtent(table, "c", "b"), null); metadataTable.put(makeExtent(table, "d", "c"), null); metadataTable.put(makeExtent(table, "e", "d"), null); metadataTable.put(makeExtent(table, null, "e"), null); TestChaoticLoadBalancer balancer = new TestChaoticLoadBalancer(); SortedMap<TServerInstance,TabletServerStatus> current = new TreeMap<>(); for (Entry<TServerInstance,FakeTServer> entry : servers.entrySet()) { current.put(entry.getKey(), entry.getValue().getStatus()); } Map<KeyExtent,TServerInstance> assignments = new HashMap<>(); balancer.getAssignments(getAssignments(servers), metadataTable, assignments); assertEquals(assignments.size(), metadataTable.size()); }
@Override public void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current, Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments) { long total = assignments.size() + unassigned.size(); long avg = (long) Math.ceil(((double) total) / current.size()); Map<TServerInstance,Long> toAssign = new HashMap<>(); List<TServerInstance> tServerArray = new ArrayList<>(); for (Entry<TServerInstance,TabletServerStatus> e : current.entrySet()) { long numTablets = 0; for (TableInfo ti : e.getValue().getTableMap().values()) { numTablets += ti.tablets; } if (numTablets <= avg) { tServerArray.add(e.getKey()); toAssign.put(e.getKey(), avg - numTablets); } } if (tServerArray.isEmpty()) { return; } for (KeyExtent ke : unassigned.keySet()) { int index = r.nextInt(tServerArray.size()); TServerInstance dest = tServerArray.get(index); assignments.put(ke, dest); long remaining = toAssign.get(dest) - 1; if (remaining == 0) { tServerArray.remove(index); toAssign.remove(dest); } else { toAssign.put(dest, remaining); } } }
ChaoticLoadBalancer extends TabletBalancer { @Override public void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current, Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments) { long total = assignments.size() + unassigned.size(); long avg = (long) Math.ceil(((double) total) / current.size()); Map<TServerInstance,Long> toAssign = new HashMap<>(); List<TServerInstance> tServerArray = new ArrayList<>(); for (Entry<TServerInstance,TabletServerStatus> e : current.entrySet()) { long numTablets = 0; for (TableInfo ti : e.getValue().getTableMap().values()) { numTablets += ti.tablets; } if (numTablets <= avg) { tServerArray.add(e.getKey()); toAssign.put(e.getKey(), avg - numTablets); } } if (tServerArray.isEmpty()) { return; } for (KeyExtent ke : unassigned.keySet()) { int index = r.nextInt(tServerArray.size()); TServerInstance dest = tServerArray.get(index); assignments.put(ke, dest); long remaining = toAssign.get(dest) - 1; if (remaining == 0) { tServerArray.remove(index); toAssign.remove(dest); } else { toAssign.put(dest, remaining); } } } }
ChaoticLoadBalancer extends TabletBalancer { @Override public void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current, Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments) { long total = assignments.size() + unassigned.size(); long avg = (long) Math.ceil(((double) total) / current.size()); Map<TServerInstance,Long> toAssign = new HashMap<>(); List<TServerInstance> tServerArray = new ArrayList<>(); for (Entry<TServerInstance,TabletServerStatus> e : current.entrySet()) { long numTablets = 0; for (TableInfo ti : e.getValue().getTableMap().values()) { numTablets += ti.tablets; } if (numTablets <= avg) { tServerArray.add(e.getKey()); toAssign.put(e.getKey(), avg - numTablets); } } if (tServerArray.isEmpty()) { return; } for (KeyExtent ke : unassigned.keySet()) { int index = r.nextInt(tServerArray.size()); TServerInstance dest = tServerArray.get(index); assignments.put(ke, dest); long remaining = toAssign.get(dest) - 1; if (remaining == 0) { tServerArray.remove(index); toAssign.remove(dest); } else { toAssign.put(dest, remaining); } } } ChaoticLoadBalancer();  ChaoticLoadBalancer(String tableName); }
ChaoticLoadBalancer extends TabletBalancer { @Override public void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current, Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments) { long total = assignments.size() + unassigned.size(); long avg = (long) Math.ceil(((double) total) / current.size()); Map<TServerInstance,Long> toAssign = new HashMap<>(); List<TServerInstance> tServerArray = new ArrayList<>(); for (Entry<TServerInstance,TabletServerStatus> e : current.entrySet()) { long numTablets = 0; for (TableInfo ti : e.getValue().getTableMap().values()) { numTablets += ti.tablets; } if (numTablets <= avg) { tServerArray.add(e.getKey()); toAssign.put(e.getKey(), avg - numTablets); } } if (tServerArray.isEmpty()) { return; } for (KeyExtent ke : unassigned.keySet()) { int index = r.nextInt(tServerArray.size()); TServerInstance dest = tServerArray.get(index); assignments.put(ke, dest); long remaining = toAssign.get(dest) - 1; if (remaining == 0) { tServerArray.remove(index); toAssign.remove(dest); } else { toAssign.put(dest, remaining); } } } ChaoticLoadBalancer();  ChaoticLoadBalancer(String tableName); @Override void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current,
      Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments); @Override long balance(SortedMap<TServerInstance,TabletServerStatus> current,
      Set<KeyExtent> migrations, List<TabletMigration> migrationsOut); }
ChaoticLoadBalancer extends TabletBalancer { @Override public void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current, Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments) { long total = assignments.size() + unassigned.size(); long avg = (long) Math.ceil(((double) total) / current.size()); Map<TServerInstance,Long> toAssign = new HashMap<>(); List<TServerInstance> tServerArray = new ArrayList<>(); for (Entry<TServerInstance,TabletServerStatus> e : current.entrySet()) { long numTablets = 0; for (TableInfo ti : e.getValue().getTableMap().values()) { numTablets += ti.tablets; } if (numTablets <= avg) { tServerArray.add(e.getKey()); toAssign.put(e.getKey(), avg - numTablets); } } if (tServerArray.isEmpty()) { return; } for (KeyExtent ke : unassigned.keySet()) { int index = r.nextInt(tServerArray.size()); TServerInstance dest = tServerArray.get(index); assignments.put(ke, dest); long remaining = toAssign.get(dest) - 1; if (remaining == 0) { tServerArray.remove(index); toAssign.remove(dest); } else { toAssign.put(dest, remaining); } } } ChaoticLoadBalancer();  ChaoticLoadBalancer(String tableName); @Override void getAssignments(SortedMap<TServerInstance,TabletServerStatus> current,
      Map<KeyExtent,TServerInstance> unassigned, Map<KeyExtent,TServerInstance> assignments); @Override long balance(SortedMap<TServerInstance,TabletServerStatus> current,
      Set<KeyExtent> migrations, List<TabletMigration> migrationsOut);  }