@Test public void bigTest2() { TabletServers tservers = new TabletServers(); Random rand = new SecureRandom(); for (int g = 1; g <= 60; g++) { for (int t = 1; t <= rand.nextInt(1000); t++) { tservers.addTablet(String.format("%02d:%d", g, t), "192.168.1." + (rand.nextInt(249) + 1) + ":9997"); } } for (int i = 1; i <= 250; i++) { tservers.addTserver("192.168.1." + i + ":9997"); } tservers.balance(1000); }
@Override public long balance(SortedMap<TServerInstance,TabletServerStatus> current, Set<KeyExtent> migrations, List<TabletMigration> migrationsOut) { if (!shouldBalance(current, migrations)) { return 5000; } if (System.currentTimeMillis() - lastRun < getWaitTime()) { return 5000; } MapCounter<String> groupCounts = new MapCounter<>(); Map<TServerInstance,TserverGroupInfo> tservers = new HashMap<>(); for (TServerInstance tsi : current.keySet()) { tservers.put(tsi, new TserverGroupInfo(tsi)); } Function<KeyExtent,String> partitioner = getPartitioner(); for (Pair<KeyExtent,Location> entry : getLocationProvider()) { String group = partitioner.apply(entry.getFirst()); Location loc = entry.getSecond(); if (loc.equals(Location.NONE) || !tservers.containsKey(loc.getTserverInstance())) { return 5000; } groupCounts.increment(group, 1); TserverGroupInfo tgi = tservers.get(loc.getTserverInstance()); tgi.addGroup(group); } Map<String,Integer> expectedCounts = new HashMap<>(); int totalExtra = 0; for (String group : groupCounts.keySet()) { int groupCount = groupCounts.getInt(group); totalExtra += groupCount % current.size(); expectedCounts.put(group, (groupCount / current.size())); } int expectedExtra = totalExtra / current.size(); int maxExtraGroups = expectedExtra + 1; expectedCounts = Collections.unmodifiableMap(expectedCounts); tservers = Collections.unmodifiableMap(tservers); for (TserverGroupInfo tgi : tservers.values()) { tgi.finishedAdding(expectedCounts); } Moves moves = new Moves(); balanceExpected(tservers, moves); if (moves.size() < getMaxMigrations()) { balanceExtraExpected(tservers, expectedExtra, moves); if (moves.size() < getMaxMigrations()) { boolean cont = balanceExtraMultiple(tservers, maxExtraGroups, moves); if (cont && moves.size() < getMaxMigrations()) { balanceExtraExtra(tservers, maxExtraGroups, moves); } } } populateMigrations(tservers.keySet(), migrationsOut, moves); lastRun = System.currentTimeMillis(); return 5000; }
GroupBalancer extends TabletBalancer { @Override public long balance(SortedMap<TServerInstance,TabletServerStatus> current, Set<KeyExtent> migrations, List<TabletMigration> migrationsOut) { if (!shouldBalance(current, migrations)) { return 5000; } if (System.currentTimeMillis() - lastRun < getWaitTime()) { return 5000; } MapCounter<String> groupCounts = new MapCounter<>(); Map<TServerInstance,TserverGroupInfo> tservers = new HashMap<>(); for (TServerInstance tsi : current.keySet()) { tservers.put(tsi, new TserverGroupInfo(tsi)); } Function<KeyExtent,String> partitioner = getPartitioner(); for (Pair<KeyExtent,Location> entry : getLocationProvider()) { String group = partitioner.apply(entry.getFirst()); Location loc = entry.getSecond(); if (loc.equals(Location.NONE) || !tservers.containsKey(loc.getTserverInstance())) { return 5000; } groupCounts.increment(group, 1); TserverGroupInfo tgi = tservers.get(loc.getTserverInstance()); tgi.addGroup(group); } Map<String,Integer> expectedCounts = new HashMap<>(); int totalExtra = 0; for (String group : groupCounts.keySet()) { int groupCount = groupCounts.getInt(group); totalExtra += groupCount % current.size(); expectedCounts.put(group, (groupCount / current.size())); } int expectedExtra = totalExtra / current.size(); int maxExtraGroups = expectedExtra + 1; expectedCounts = Collections.unmodifiableMap(expectedCounts); tservers = Collections.unmodifiableMap(tservers); for (TserverGroupInfo tgi : tservers.values()) { tgi.finishedAdding(expectedCounts); } Moves moves = new Moves(); balanceExpected(tservers, moves); if (moves.size() < getMaxMigrations()) { balanceExtraExpected(tservers, expectedExtra, moves); if (moves.size() < getMaxMigrations()) { boolean cont = balanceExtraMultiple(tservers, maxExtraGroups, moves); if (cont && moves.size() < getMaxMigrations()) { balanceExtraExtra(tservers, maxExtraGroups, moves); } } } populateMigrations(tservers.keySet(), migrationsOut, moves); lastRun = System.currentTimeMillis(); return 5000; } }
GroupBalancer extends TabletBalancer { @Override public long balance(SortedMap<TServerInstance,TabletServerStatus> current, Set<KeyExtent> migrations, List<TabletMigration> migrationsOut) { if (!shouldBalance(current, migrations)) { return 5000; } if (System.currentTimeMillis() - lastRun < getWaitTime()) { return 5000; } MapCounter<String> groupCounts = new MapCounter<>(); Map<TServerInstance,TserverGroupInfo> tservers = new HashMap<>(); for (TServerInstance tsi : current.keySet()) { tservers.put(tsi, new TserverGroupInfo(tsi)); } Function<KeyExtent,String> partitioner = getPartitioner(); for (Pair<KeyExtent,Location> entry : getLocationProvider()) { String group = partitioner.apply(entry.getFirst()); Location loc = entry.getSecond(); if (loc.equals(Location.NONE) || !tservers.containsKey(loc.getTserverInstance())) { return 5000; } groupCounts.increment(group, 1); TserverGroupInfo tgi = tservers.get(loc.getTserverInstance()); tgi.addGroup(group); } Map<String,Integer> expectedCounts = new HashMap<>(); int totalExtra = 0; for (String group : groupCounts.keySet()) { int groupCount = groupCounts.getInt(group); totalExtra += groupCount % current.size(); expectedCounts.put(group, (groupCount / current.size())); } int expectedExtra = totalExtra / current.size(); int maxExtraGroups = expectedExtra + 1; expectedCounts = Collections.unmodifiableMap(expectedCounts); tservers = Collections.unmodifiableMap(tservers); for (TserverGroupInfo tgi : tservers.values()) { tgi.finishedAdding(expectedCounts); } Moves moves = new Moves(); balanceExpected(tservers, moves); if (moves.size() < getMaxMigrations()) { balanceExtraExpected(tservers, expectedExtra, moves); if (moves.size() < getMaxMigrations()) { boolean cont = balanceExtraMultiple(tservers, maxExtraGroups, moves); if (cont && moves.size() < getMaxMigrations()) { balanceExtraExtra(tservers, maxExtraGroups, moves); } } } populateMigrations(tservers.keySet(), migrationsOut, moves); lastRun = System.currentTimeMillis(); return 5000; } GroupBalancer(TableId tableId); }
GroupBalancer extends TabletBalancer { @Override public long balance(SortedMap<TServerInstance,TabletServerStatus> current, Set<KeyExtent> migrations, List<TabletMigration> migrationsOut) { if (!shouldBalance(current, migrations)) { return 5000; } if (System.currentTimeMillis() - lastRun < getWaitTime()) { return 5000; } MapCounter<String> groupCounts = new MapCounter<>(); Map<TServerInstance,TserverGroupInfo> tservers = new HashMap<>(); for (TServerInstance tsi : current.keySet()) { tservers.put(tsi, new TserverGroupInfo(tsi)); } Function<KeyExtent,String> partitioner = getPartitioner(); for (Pair<KeyExtent,Location> entry : getLocationProvider()) { String group = partitioner.apply(entry.getFirst()); Location loc = entry.getSecond(); if (loc.equals(Location.NONE) || !tservers.containsKey(loc.getTserverInstance())) { return 5000; } groupCounts.increment(group, 1); TserverGroupInfo tgi = tservers.get(loc.getTserverInstance()); tgi.addGroup(group); } Map<String,Integer> expectedCounts = new HashMap<>(); int totalExtra = 0; for (String group : groupCounts.keySet()) { int groupCount = groupCounts.getInt(group); totalExtra += groupCount % current.size(); expectedCounts.put(group, (groupCount / current.size())); } int expectedExtra = totalExtra / current.size(); int maxExtraGroups = expectedExtra + 1; expectedCounts = Collections.unmodifiableMap(expectedCounts); tservers = Collections.unmodifiableMap(tservers); for (TserverGroupInfo tgi : tservers.values()) { tgi.finishedAdding(expectedCounts); } Moves moves = new Moves(); balanceExpected(tservers, moves); if (moves.size() < getMaxMigrations()) { balanceExtraExpected(tservers, expectedExtra, moves); if (moves.size() < getMaxMigrations()) { boolean cont = balanceExtraMultiple(tservers, maxExtraGroups, moves); if (cont && moves.size() < getMaxMigrations()) { balanceExtraExtra(tservers, maxExtraGroups, moves); } } } populateMigrations(tservers.keySet(), migrationsOut, moves); lastRun = System.currentTimeMillis(); return 5000; } GroupBalancer(TableId tableId); @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); }
GroupBalancer extends TabletBalancer { @Override public long balance(SortedMap<TServerInstance,TabletServerStatus> current, Set<KeyExtent> migrations, List<TabletMigration> migrationsOut) { if (!shouldBalance(current, migrations)) { return 5000; } if (System.currentTimeMillis() - lastRun < getWaitTime()) { return 5000; } MapCounter<String> groupCounts = new MapCounter<>(); Map<TServerInstance,TserverGroupInfo> tservers = new HashMap<>(); for (TServerInstance tsi : current.keySet()) { tservers.put(tsi, new TserverGroupInfo(tsi)); } Function<KeyExtent,String> partitioner = getPartitioner(); for (Pair<KeyExtent,Location> entry : getLocationProvider()) { String group = partitioner.apply(entry.getFirst()); Location loc = entry.getSecond(); if (loc.equals(Location.NONE) || !tservers.containsKey(loc.getTserverInstance())) { return 5000; } groupCounts.increment(group, 1); TserverGroupInfo tgi = tservers.get(loc.getTserverInstance()); tgi.addGroup(group); } Map<String,Integer> expectedCounts = new HashMap<>(); int totalExtra = 0; for (String group : groupCounts.keySet()) { int groupCount = groupCounts.getInt(group); totalExtra += groupCount % current.size(); expectedCounts.put(group, (groupCount / current.size())); } int expectedExtra = totalExtra / current.size(); int maxExtraGroups = expectedExtra + 1; expectedCounts = Collections.unmodifiableMap(expectedCounts); tservers = Collections.unmodifiableMap(tservers); for (TserverGroupInfo tgi : tservers.values()) { tgi.finishedAdding(expectedCounts); } Moves moves = new Moves(); balanceExpected(tservers, moves); if (moves.size() < getMaxMigrations()) { balanceExtraExpected(tservers, expectedExtra, moves); if (moves.size() < getMaxMigrations()) { boolean cont = balanceExtraMultiple(tservers, maxExtraGroups, moves); if (cont && moves.size() < getMaxMigrations()) { balanceExtraExtra(tservers, maxExtraGroups, moves); } } } populateMigrations(tservers.keySet(), migrationsOut, moves); lastRun = System.currentTimeMillis(); return 5000; } GroupBalancer(TableId tableId); @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);  }