@Test public void newRegistration() throws Exception { when(client.exists(idPath)).thenReturn(null); when(client.stat(hostPath)).thenReturn(null); final boolean success = registrar.tryToRegister(client); assertTrue(success); verify(client).createAndSetData(idPath, hostId.getBytes()); }
@Override public boolean tryToRegister(ZooKeeperClient client) throws KeeperException, HostNotFoundException { final String idPath = Paths.configHostId(name); final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final long diff = clock.now().getMillis() - mtime; if (diff < zooKeeperRegistrationTtlMillis) { final String message = format( "Another agent already registered as '%s' (local=%s remote=%s). " + "That agent's registration expires in %d seconds", name, id.trim(), existingId.trim(), TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff)); log.warn(message); return false; } log.info("Another agent has already registered as '{}', but its ID node was last " + "updated more than {} milliseconds ago. I\'m deregistering the agent with the" + "old ID of {} and replacing it with this new agent with ID '{}'.", name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim()); } else { log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. " + "I'll assume it's dead and deregister it.", name, hostInfoPath); } ZooKeeperRegistrarUtil.reRegisterHost(client, name, id); } else { log.info("Matching agent id node already present, not registering agent {}: {}", id, name); } } if (upNode == null) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start(); } log.info("ZooKeeper registration complete"); return true; }
AgentZooKeeperRegistrar implements ZooKeeperRegistrar { @Override public boolean tryToRegister(ZooKeeperClient client) throws KeeperException, HostNotFoundException { final String idPath = Paths.configHostId(name); final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final long diff = clock.now().getMillis() - mtime; if (diff < zooKeeperRegistrationTtlMillis) { final String message = format( "Another agent already registered as '%s' (local=%s remote=%s). " + "That agent's registration expires in %d seconds", name, id.trim(), existingId.trim(), TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff)); log.warn(message); return false; } log.info("Another agent has already registered as '{}', but its ID node was last " + "updated more than {} milliseconds ago. I\'m deregistering the agent with the" + "old ID of {} and replacing it with this new agent with ID '{}'.", name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim()); } else { log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. " + "I'll assume it's dead and deregister it.", name, hostInfoPath); } ZooKeeperRegistrarUtil.reRegisterHost(client, name, id); } else { log.info("Matching agent id node already present, not registering agent {}: {}", id, name); } } if (upNode == null) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start(); } log.info("ZooKeeper registration complete"); return true; } }
AgentZooKeeperRegistrar implements ZooKeeperRegistrar { @Override public boolean tryToRegister(ZooKeeperClient client) throws KeeperException, HostNotFoundException { final String idPath = Paths.configHostId(name); final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final long diff = clock.now().getMillis() - mtime; if (diff < zooKeeperRegistrationTtlMillis) { final String message = format( "Another agent already registered as '%s' (local=%s remote=%s). " + "That agent's registration expires in %d seconds", name, id.trim(), existingId.trim(), TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff)); log.warn(message); return false; } log.info("Another agent has already registered as '{}', but its ID node was last " + "updated more than {} milliseconds ago. I\'m deregistering the agent with the" + "old ID of {} and replacing it with this new agent with ID '{}'.", name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim()); } else { log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. " + "I'll assume it's dead and deregister it.", name, hostInfoPath); } ZooKeeperRegistrarUtil.reRegisterHost(client, name, id); } else { log.info("Matching agent id node already present, not registering agent {}: {}", id, name); } } if (upNode == null) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start(); } log.info("ZooKeeper registration complete"); return true; } AgentZooKeeperRegistrar(final String name, final String id,
                                 final int zooKeeperRegistrationTtlMinutes, final Clock clock); }
AgentZooKeeperRegistrar implements ZooKeeperRegistrar { @Override public boolean tryToRegister(ZooKeeperClient client) throws KeeperException, HostNotFoundException { final String idPath = Paths.configHostId(name); final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final long diff = clock.now().getMillis() - mtime; if (diff < zooKeeperRegistrationTtlMillis) { final String message = format( "Another agent already registered as '%s' (local=%s remote=%s). " + "That agent's registration expires in %d seconds", name, id.trim(), existingId.trim(), TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff)); log.warn(message); return false; } log.info("Another agent has already registered as '{}', but its ID node was last " + "updated more than {} milliseconds ago. I\'m deregistering the agent with the" + "old ID of {} and replacing it with this new agent with ID '{}'.", name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim()); } else { log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. " + "I'll assume it's dead and deregister it.", name, hostInfoPath); } ZooKeeperRegistrarUtil.reRegisterHost(client, name, id); } else { log.info("Matching agent id node already present, not registering agent {}: {}", id, name); } } if (upNode == null) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start(); } log.info("ZooKeeper registration complete"); return true; } AgentZooKeeperRegistrar(final String name, final String id,
                                 final int zooKeeperRegistrationTtlMinutes, final Clock clock); @Override void startUp(); @Override void shutDown(); @Override boolean tryToRegister(ZooKeeperClient client); }
AgentZooKeeperRegistrar implements ZooKeeperRegistrar { @Override public boolean tryToRegister(ZooKeeperClient client) throws KeeperException, HostNotFoundException { final String idPath = Paths.configHostId(name); final String hostInfoPath = Paths.statusHostInfo(name); final Stat stat = client.exists(idPath); if (stat == null) { log.debug("Agent id node not present, registering agent {}: {}", id, name); ZooKeeperRegistrarUtil.registerHost(client, idPath, name, id); } else { final byte[] bytes = client.getData(idPath); final String existingId = bytes == null ? "" : new String(bytes, UTF_8); if (!id.equals(existingId)) { final Stat hostInfoStat = client.stat(hostInfoPath); if (hostInfoStat != null) { final long mtime = hostInfoStat.getMtime(); final long diff = clock.now().getMillis() - mtime; if (diff < zooKeeperRegistrationTtlMillis) { final String message = format( "Another agent already registered as '%s' (local=%s remote=%s). " + "That agent's registration expires in %d seconds", name, id.trim(), existingId.trim(), TimeUnit.MILLISECONDS.toSeconds(zooKeeperRegistrationTtlMillis - diff)); log.warn(message); return false; } log.info("Another agent has already registered as '{}', but its ID node was last " + "updated more than {} milliseconds ago. I\'m deregistering the agent with the" + "old ID of {} and replacing it with this new agent with ID '{}'.", name, zooKeeperRegistrationTtlMillis, existingId.trim(), id.trim()); } else { log.info("Another agent has registered as '{}', but it never updated '{}' in ZooKeeper. " + "I'll assume it's dead and deregister it.", name, hostInfoPath); } ZooKeeperRegistrarUtil.reRegisterHost(client, name, id); } else { log.info("Matching agent id node already present, not registering agent {}: {}", id, name); } } if (upNode == null) { final String upPath = Paths.statusHostUp(name); log.debug("Creating up node: {}", upPath); client.ensurePath(upPath, true); upNode = client.persistentEphemeralNode(upPath, EPHEMERAL, EMPTY_BYTES); upNode.start(); } log.info("ZooKeeper registration complete"); return true; } AgentZooKeeperRegistrar(final String name, final String id,
                                 final int zooKeeperRegistrationTtlMinutes, final Clock clock); @Override void startUp(); @Override void shutDown(); @Override boolean tryToRegister(ZooKeeperClient client);  }