@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } HostAddress that = (HostAddress)o; if (!partitionServerAddress.equals(that.partitionServerAddress)) { return false; } if (!ring.equals(that.ring)) { return false; } return true; }
@Override public Host getHostByAddress(PartitionServerAddress address) { for (Host host : hosts) { if (host.getAddress().equals(address)) { return host; } } return null; }
@Override public Host getHostByAddress(PartitionServerAddress address) { for (ZkHost host : hosts.values()) { if (host.getAddress().equals(address)) { return host; } } return null; }
@Override public boolean removeHost(PartitionServerAddress address) throws IOException { for (Map.Entry<String, ZkHost> entry : hosts.entrySet()) { if (entry.getValue().getAddress().equals(address)) { ZkHost host = hosts.remove(entry.getKey()); host.delete(); fireDataLocationChangeListener(); return true; } } return false; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ZkHost other = (ZkHost) obj; if (getAddress() == null) { if (other.getAddress() != null) { return false; } } else if (!getAddress().equals(other.getAddress())) { return false; } if (path == null) { if (other.path != null) { return false; } } else if (!path.equals(other.path)) { return false; } return true; } }
private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException { RingGroup rg = coordinator.getRingGroup(req.getParameter("g")); Ring r = rg.getRing(Integer.parseInt(req.getParameter("n"))); Host h = r.getHostByAddress(PartitionServerAddress.parse(URLEnc.decode(req.getParameter("h")))); h.setFlags(Hosts.splitHostFlags(req.getParameter("hostFlags"))); PartitionServerAddress address = PartitionServerAddress.parse(req.getParameter("hostAddress")); if (!h.getAddress().equals(address)) { h.setAddress(address); // Redirect to Ring (host address has changed) resp.sendRedirect("/ring.jsp?g=" + rg.getName() + "&n=" + r.getRingNumber()); } else { redirectBack(resp, rg, r, h); } }
@Test public void testCreateAndLoad() throws Exception { final ZkHost host = ZkHost.create(getZk(), coordinator, getRoot(), ADDRESS, null, Collections.emptyList()); assertEquals(ADDRESS, host.getAddress()); assertEquals(0, host.getCommandQueue().size()); assertNull(host.getCurrentCommand()); assertEquals(HostState.OFFLINE, host.getState()); assertFalse(Hosts.isOnline(host)); host.setEphemeralStatistic("a", "A"); host.setEphemeralStatistic("b", "B"); WaitUntil.orDie(() -> { try { return "A".equals(host.getStatistic("a")) && "B".equals(host.getStatistic("b")); } catch (IOException e) { throw new RuntimeException(e); } }); assertEquals("A", host.getStatistic("a")); assertEquals("B", host.getStatistic("b")); assertNull(host.getStatistic("c")); host.setAddress(OTHER_ADDRESS); WaitUntil.orDie(() -> host.getAddress().equals(OTHER_ADDRESS)); assertEquals(OTHER_ADDRESS, host.getAddress()); }
@Test public void testHosts() throws Exception { final ZkRing ring = ZkRing.create(getZk(), coordinator, ring_group_root, 1, null, null); assertEquals(0, ring.getHosts().size()); Host host = ring.addHost(LOCALHOST, Collections.emptyList()); assertEquals(LOCALHOST, host.getAddress()); WaitUntil.orDie(() -> !ring.getHosts().isEmpty()); assertEquals(Collections.singleton(host), ring.getHosts()); assertEquals(LOCALHOST, ring.getHostByAddress(LOCALHOST).getAddress()); ring.close(); // assure that hosts reload well, too final ZkRing sameRing = new ZkRing(getZk(), ring_root, null, coordinator, null); assertEquals(1, sameRing.getHosts().size()); assertEquals(Collections.singleton(host), sameRing.getHosts()); assertEquals(LOCALHOST, sameRing.getHostByAddress(LOCALHOST).getAddress()); // Rename that host sameRing.getHostByAddress(LOCALHOST).setAddress(LOCALHOST2); WaitUntil.orDie(() -> sameRing.getHostByAddress(LOCALHOST2) != null && LOCALHOST2.equals(sameRing.getHostByAddress(LOCALHOST2).getAddress())); assertEquals(LOCALHOST2, sameRing.getHostByAddress(LOCALHOST2).getAddress()); assertNull(sameRing.getHostByAddress(LOCALHOST)); assertTrue(sameRing.removeHost(LOCALHOST2)); assertNull(sameRing.getHostByAddress(LOCALHOST2)); assertFalse(sameRing.removeHost(LOCALHOST2)); ring.close(); }