@Override public Host getHostByAddress(PartitionServerAddress address) { for (Host host : hosts) { if (host.getAddress().equals(address)) { return host; } } return null; }
private static boolean isAssignedDeletablePartition(Host host) throws IOException { for (HostDomain hostDomain : host.getAssignedDomains()) { for (HostDomainPartition partition : hostDomain.getPartitions()) { if (partition.isDeletable()) { return true; } } } return false; }
protected HostData getHostData(Host host) throws IOException { HostData data = new HostData(); data.address = host.getAddress(); data.isOnline = Hosts.isOnline(host); data.state = host.getState(); data.statisticsString = host.getStatistic(Hosts.RUNTIME_STATISTICS_KEY); data.statisticsJson = getRuntimeStatsString(host); return data; }
public static void enqueueCommandIfNotPresent(Host host, HostCommand command) throws IOException { if (host.getCurrentCommand() != command && !host.getCommandQueue().contains(command)) { host.enqueueCommand(command); } }
public static long computeUpdateETA(Host host) { try { if (host.getState() != HostState.UPDATING) { return -1; } String etaString = host.getStatistic(UPDATE_ETA_STATISTIC_KEY); if (etaString != null) { return Long.parseLong(etaString); } else { return -1; } } catch (IOException e) { return -1; } }
private static boolean isServingAndAboutToServe(Host host) throws IOException { return host.getState().equals(HostState.SERVING) && host.getCurrentCommand() == null && host.getCommandQueue().size() == 0; }
public static boolean isOnline(Host host) throws IOException { return host.getState() != HostState.OFFLINE; }
private static String formatHostListTooltip(String title, Set<Host> hosts) throws IOException { if (hosts.size() == 0) { return "-"; } else { TreeSet<Host> sortedHosts = new TreeSet<Host>(hosts); StringBuilder content = new StringBuilder(); for (Host host : sortedHosts) { content.append("<div class='" + hostStateToClass(host.getState()) + "'>"); content.append(host.getAddress().toString()); content.append("</div>"); } return htmlTooltip(Integer.toString(hosts.size()), title, content.toString()); } }
@Test public void testListenersPreservedWhenHostAdded() throws Exception { ZkRing ring = ZkRing.create(getZk(), coordinator, ZkPath.append(getRoot(), "ring-group-one"), 1, null, null); Host h1 = ring.addHost(new PartitionServerAddress("localhost", 1), Collections.emptyList()); MockHostCommandQueueChangeListener l1 = new MockHostCommandQueueChangeListener(); h1.setCommandQueueChangeListener(l1); final MockHostStateChangeListener l2 = new MockHostStateChangeListener(); h1.setStateChangeListener(l2); ring.addHost(new PartitionServerAddress("localhost", 2), Collections.emptyList()); h1.setState(HostState.UPDATING); WaitUntil.orDie(() -> HostState.UPDATING == l2.calledWith); assertEquals(HostState.UPDATING, l2.calledWith); h1.enqueueCommand(HostCommand.EXECUTE_UPDATE); l1.waitForNotification(); assertEquals(h1, l1.calledWith); }
LOG.info(getLogPrefix() + "Loading partition metadata for Host: " + host.getAddress()); HostAddress hostAddress = new HostAddress(ring, host.getAddress()); for (HostDomain hostDomain : host.getAssignedDomains()) { Domain domain = hostDomain.getDomain(); if (domain == null) { LOG.debug(getLogPrefix() + "Loading partition metadata for Host: " + host.getAddress() + ", Domain: " + domain.getName());
HostDomain hostDomain = host.getHostDomain(domain); hostDomain = host.addDomain(domain); for (HostDomain hostDomain : host.getAssignedDomains()) { if (!domains.contains(hostDomain.getDomain())) { for (HostDomainPartition partition : hostDomain.getPartitions()) {
public HostConnection(Host host, int tryLockTimeoutMs, int establishConnectionTimeoutMs, int queryTimeoutMs, int bulkQueryTimeoutMs) throws IOException { this.host = host; this.tryLockTimeoutMs = tryLockTimeoutMs; this.establishConnectionTimeoutMs = establishConnectionTimeoutMs; this.queryTimeoutMs = queryTimeoutMs; this.bulkQueryTimeoutMs = bulkQueryTimeoutMs; host.setStateChangeListener(this); onWatchedNodeChange(host.getState()); }
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); } }
private static boolean allPartitionsUpToDate(Host host, Collection<DomainAndVersion> domainVersions, boolean allowMoreRecentDomainVersions) throws IOException { // Check that each domain of the given domain group version is up to date on this host for (DomainAndVersion domainAndVersion : domainVersions) { Domain domain = domainAndVersion.getDomain(); HostDomain hostDomain = host.getHostDomain(domain); if (hostDomain != null) { for (HostDomainPartition partition : hostDomain.getPartitions()) { // Ignore deletable partitions if (!partition.isDeletable()) { // If the partition is not currently at the given domain group version, the host is not up-to-date if (partition.getCurrentDomainVersion() == null || (!allowMoreRecentDomainVersions && partition.getCurrentDomainVersion() != domainAndVersion.getVersionNumber()) || (allowMoreRecentDomainVersions && (partition.getCurrentDomainVersion() < domainAndVersion.getVersionNumber()))) { LOG.info("Host " + host.getAddress().getHostName() + " is not up to date for domain " + domain.getName() + " and version " + domainAndVersion.getVersionNumber()); LOG.info("Host " + host.getAddress().getHostName() + " has a partition on domain " + domain.getName() + " at version " + partition.getCurrentDomainVersion()); return false; } } } } } return true; }
public static ServingStatusAggregator computeServingStatusAggregator(Host host, DomainGroup domainGroup) throws IOException { ServingStatusAggregator result = new ServingStatusAggregator(); for (HostDomain hostDomain : host.getAssignedDomains()) { DomainAndVersion domainVersion = domainGroup.getDomainVersion(hostDomain.getDomain()); // Ignore domains that are not relevant if (domainVersion != null) { for (HostDomainPartition partition : hostDomain.getPartitions()) { // Ignore deletable partitions if (!partition.isDeletable()) { // Check if partition is served and up to date boolean servedAndUpToDate = host.getState() == HostState.SERVING && partition.getCurrentDomainVersion() != null && partition.getCurrentDomainVersion().equals(domainVersion.getVersionNumber()); // Aggregate counts result.add(hostDomain.getDomain(), partition.getPartitionNumber(), servedAndUpToDate); } } } } return result; }
private void removeHost(Ring ring, Host host) throws IOException { // put to idle host.clearCommandQueue(); Hosts.enqueueCommandIfNotPresent(host, HostCommand.GO_TO_IDLE); // sleep for a few seconds try { Thread.sleep(5000); } catch (InterruptedException e) { LOG.error("Interrupted waiting for host to idle", e); } PartitionServerAddress address = host.getAddress(); ring.removeHost(address); LOG.info("Removed host " + address + " from ring " + ring.getRingNumber()); }
HostDomain hostDomain = host.getHostDomain(domain); for (HostDomain hostDomain : host.getAssignedDomains()) { if (!domains.contains(hostDomain.getDomain())) { for (HostDomainPartition partition : hostDomain.getPartitions()) {
@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(); }
public void processCommandOnStartup() { try { HostCommand command = host.getCurrentCommand(); LOG.info("Current command is: " + command); if (command != null) { commandQueue.put(command); } else { host.nextCommand(); } } catch (Exception e) { LOG.error("Failed to process current command on startup.", e); stop(); } hasProcessedCommandOnStartup = true; }
@Test public void testCommandAll() throws IOException { final Host hc = new MockHost(LOCALHOST); SlightlyLessAbstractRing rc = new SlightlyLessAbstractRing(1, null) { @Override public Set<Host> getHosts() { return Collections.singleton(hc); } }; assertNull(hc.getCurrentCommand()); assertTrue(hc.getCommandQueue().isEmpty()); Rings.commandAll(rc, HostCommand.SERVE_DATA); assertNull(hc.getCurrentCommand()); assertEquals(Arrays.asList(HostCommand.SERVE_DATA), hc.getCommandQueue()); }