@Test public void AddAndDeleteTopicAndMetaServerTest() { LeastAdjustmentMetaServerAssigningStrategy strategy = new LeastAdjustmentMetaServerAssigningStrategy(); AssignBalancer assignBalancer = new LeastAdjustmentAssianBalancer(); Reflects.forField().setDeclaredFieldValue(LeastAdjustmentMetaServerAssigningStrategy.class, "m_assignBalancer", strategy, assignBalancer); List<Server> metaServers = new ArrayList<>(); metaServers.add(new Server("a").setPort(1)); metaServers.add(new Server("c").setPort(1)); metaServers.add(new Server("d").setPort(1)); metaServers.add(new Server("e").setPort(1)); List<Topic> topics = new ArrayList<>(); for (int i = 0; i < 4; i++) { topics.add(new Topic(String.valueOf(i))); } topics.add(new Topic("10")); topics.add(new Topic("11")); topics.add(new Topic("12")); Assignment<String> originAssignments = new Assignment<>(); final ClientContext a = new ClientContext("a", "a", 1, "a", "a", 1L); final ClientContext b = new ClientContext("b", "a", 1, "a", "a", 1L); final ClientContext c = new ClientContext("c", "a", 1, "a", "a", 1L); originAssignments.addAssignment("0", new HashMap<String, ClientContext>() { { put("a", a); } }); originAssignments.addAssignment("1", new HashMap<String, ClientContext>() { { put("b", b); } }); originAssignments.addAssignment("2", new HashMap<String, ClientContext>() { { put("c", c); } }); originAssignments.addAssignment("3", new HashMap<String, ClientContext>() { { put("a", a); } }); originAssignments.addAssignment("4", new HashMap<String, ClientContext>() { { put("a", a); } }); originAssignments.addAssignment("5", new HashMap<String, ClientContext>() { { put("b", b); } }); Assignment<String> newAssigns = strategy.assign(metaServers, topics, originAssignments); System.out.println("AddAndDeleteTopicAndMetaServerTest:"); System.out.println("---------------------------------------------------------------"); System.out.println("Current MetaServers:"); for (Server server : metaServers) { System.out.print(server.getId() + " "); } System.out.println(); System.out.println("---------------------------------------------------------------"); System.out.println("Current Topics:"); for (Topic topic : topics) { System.out.print(topic.getName() + " "); } System.out.println(); System.out.println("---------------------------------------------------------------"); System.out.println("Origin Assignments:"); for (Entry<String, Map<String, ClientContext>> assign : originAssignments.getAssignments().entrySet()) { System.out.println(assign); } System.out.println("---------------------------------------------------------------"); System.out.println("New Assignments:"); for (Entry<String, Map<String, ClientContext>> assign : newAssigns.getAssignments().entrySet()) { System.out.println(assign); } normalAssert(metaServers, topics, newAssigns); Set<String> inuseMetaServers = new HashSet<>(); for (Entry<String, Map<String, ClientContext>> topicAssign : newAssigns.getAssignments().entrySet()) { String metaServer = topicAssign.getValue().entrySet().iterator().next().getKey(); inuseMetaServers.add(metaServer); } assertFalse(inuseMetaServers.contains("b")); assertTrue(inuseMetaServers.contains("d")); assertTrue(inuseMetaServers.contains("e")); assertTrue(newAssigns.getAssignment("5") == null); assertTrue(newAssigns.getAssignment("10") != null); assertTrue(newAssigns.getAssignment("11") != null); assertTrue(newAssigns.getAssignment("12") != null); }
@Override public Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments) { Assignment<String> newAssignments = new Assignment<>(); if (metaServers == null || metaServers.isEmpty() || topics == null || topics.isEmpty()) { return newAssignments; } if (originAssignments == null) { originAssignments = new Assignment<>(); } Set<String> topicNames = new HashSet<>(); for (Topic topic : topics) { topicNames.add(topic.getName()); } Map<String, Server> currentMetaServers = new HashMap<>(); for (Server server : metaServers) { currentMetaServers.put(server.getId(), server); } Map<String, List<String>> originMetaServerToTopic = mapMetaServerToTopics(topicNames, currentMetaServers, originAssignments); List<String> freeTopics = findFreeTopics(topics, originMetaServerToTopic); Map<String, List<String>> newAssins = m_assignBalancer.assign(originMetaServerToTopic, freeTopics); for (Entry<String, List<String>> entry : newAssins.entrySet()) { putAssignToResult(newAssignments, currentMetaServers, entry.getKey(), entry.getValue()); } return newAssignments; }
LeastAdjustmentMetaServerAssigningStrategy implements MetaServerAssigningStrategy { @Override public Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments) { Assignment<String> newAssignments = new Assignment<>(); if (metaServers == null || metaServers.isEmpty() || topics == null || topics.isEmpty()) { return newAssignments; } if (originAssignments == null) { originAssignments = new Assignment<>(); } Set<String> topicNames = new HashSet<>(); for (Topic topic : topics) { topicNames.add(topic.getName()); } Map<String, Server> currentMetaServers = new HashMap<>(); for (Server server : metaServers) { currentMetaServers.put(server.getId(), server); } Map<String, List<String>> originMetaServerToTopic = mapMetaServerToTopics(topicNames, currentMetaServers, originAssignments); List<String> freeTopics = findFreeTopics(topics, originMetaServerToTopic); Map<String, List<String>> newAssins = m_assignBalancer.assign(originMetaServerToTopic, freeTopics); for (Entry<String, List<String>> entry : newAssins.entrySet()) { putAssignToResult(newAssignments, currentMetaServers, entry.getKey(), entry.getValue()); } return newAssignments; } }
LeastAdjustmentMetaServerAssigningStrategy implements MetaServerAssigningStrategy { @Override public Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments) { Assignment<String> newAssignments = new Assignment<>(); if (metaServers == null || metaServers.isEmpty() || topics == null || topics.isEmpty()) { return newAssignments; } if (originAssignments == null) { originAssignments = new Assignment<>(); } Set<String> topicNames = new HashSet<>(); for (Topic topic : topics) { topicNames.add(topic.getName()); } Map<String, Server> currentMetaServers = new HashMap<>(); for (Server server : metaServers) { currentMetaServers.put(server.getId(), server); } Map<String, List<String>> originMetaServerToTopic = mapMetaServerToTopics(topicNames, currentMetaServers, originAssignments); List<String> freeTopics = findFreeTopics(topics, originMetaServerToTopic); Map<String, List<String>> newAssins = m_assignBalancer.assign(originMetaServerToTopic, freeTopics); for (Entry<String, List<String>> entry : newAssins.entrySet()) { putAssignToResult(newAssignments, currentMetaServers, entry.getKey(), entry.getValue()); } return newAssignments; }  }
LeastAdjustmentMetaServerAssigningStrategy implements MetaServerAssigningStrategy { @Override public Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments) { Assignment<String> newAssignments = new Assignment<>(); if (metaServers == null || metaServers.isEmpty() || topics == null || topics.isEmpty()) { return newAssignments; } if (originAssignments == null) { originAssignments = new Assignment<>(); } Set<String> topicNames = new HashSet<>(); for (Topic topic : topics) { topicNames.add(topic.getName()); } Map<String, Server> currentMetaServers = new HashMap<>(); for (Server server : metaServers) { currentMetaServers.put(server.getId(), server); } Map<String, List<String>> originMetaServerToTopic = mapMetaServerToTopics(topicNames, currentMetaServers, originAssignments); List<String> freeTopics = findFreeTopics(topics, originMetaServerToTopic); Map<String, List<String>> newAssins = m_assignBalancer.assign(originMetaServerToTopic, freeTopics); for (Entry<String, List<String>> entry : newAssins.entrySet()) { putAssignToResult(newAssignments, currentMetaServers, entry.getKey(), entry.getValue()); } return newAssignments; }  @Override Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments); }
LeastAdjustmentMetaServerAssigningStrategy implements MetaServerAssigningStrategy { @Override public Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments) { Assignment<String> newAssignments = new Assignment<>(); if (metaServers == null || metaServers.isEmpty() || topics == null || topics.isEmpty()) { return newAssignments; } if (originAssignments == null) { originAssignments = new Assignment<>(); } Set<String> topicNames = new HashSet<>(); for (Topic topic : topics) { topicNames.add(topic.getName()); } Map<String, Server> currentMetaServers = new HashMap<>(); for (Server server : metaServers) { currentMetaServers.put(server.getId(), server); } Map<String, List<String>> originMetaServerToTopic = mapMetaServerToTopics(topicNames, currentMetaServers, originAssignments); List<String> freeTopics = findFreeTopics(topics, originMetaServerToTopic); Map<String, List<String>> newAssins = m_assignBalancer.assign(originMetaServerToTopic, freeTopics); for (Entry<String, List<String>> entry : newAssins.entrySet()) { putAssignToResult(newAssignments, currentMetaServers, entry.getKey(), entry.getValue()); } return newAssignments; }  @Override Assignment<String> assign(List<Server> metaServers, List<Topic> topics, Assignment<String> originAssignments);  }