@Override public boolean canExecuteCommands(NodeId nodeId, Set<Qualifier<?>> qualifiers) { ZNode typeNode = rootNode.child(CoordinationUtil.nodeNameOf(nodeId.getType())); String identifier = nodeId.getIdentifier(); if (!typeNode.hasChild(identifier)) { throw new CoordinatorException("Node with id " + nodeId + " is not found"); } ZNode node = typeNode.child(identifier); if (!node.hasChild(CoordinationUtil.AVAILABLE_NODE_NAME)) { return false; } for (Qualifier<?> qualifier : qualifiers) { if (!node.hasChild(nodeNameOf(qualifier))) { return false; } } return true; }
@Override public Set<NodeId> getAvailableNodes(NodeType type) { Set<NodeId> result = Sets.newHashSet(); ZNode typeNode = rootNode.child(CoordinationUtil.nodeNameOf(type)); for (ZNode node : typeNode.children()) { if (node.hasChild(CoordinationUtil.AVAILABLE_NODE_NAME)) { result.add(NodeId.of(type, node.getShortPath())); } } return result; }
public void launchKernelAgent() { log.info("Starting Kernel Agent"); String name = "KERNEL-AGENT"; KernelAgent agent = (KernelAgent) applicationContext.getBean("kernelAgent"); KernelAgentWorker kernelAgentWorker = (KernelAgentWorker) applicationContext.getBean("kernelAgentWorker"); agent.setNodeContext(Coordination.emptyContext(NodeId.kernelAgentNode(name))); agent.init(); log.info("Agent {} initialized", agent.getNodeContext().getId()); try { do { agent.stop(); agentLatch = new CountDownLatch(1); alive.set(false); do { try { RegistrationPack registrationPack = RegistrationPack.create( agent.getNodeContext().getId(), kernelAgentWorker.getQualifiers()); Ack ack = agent.getExchangeClient().registerNode(registrationPack); if (Ack.SUCCESS.equals(ack)) break; } catch (IOException e) { log.info("Agent {} can't do registration {}", agent.getNodeContext().getId(), e); log.info("Wait 10 seconds and try again"); Thread.sleep(AGENT_REGISTRATION_PERIOD); } } while (true); log.info("Registration agent {} has been done", agent.getNodeContext().getId()); agent.start(); agentLatch.await(); } while (alive.get()); } catch (InterruptedException e) { // do nothing; } agent.stop(); log.info("Kernel Agent finish work"); }