/** * On no master. * * @throws IOException Signals that an I/O exception has occurred. */ protected void onNoMaster() throws IOException { for (ControlNodeListener listener : listeners) { listener.onControlNodeDown(); } }
/** * Updates current node data. In case this is master node - will also update * ZK node; Although it is possible that we will have race condition here, i * expect possibility is quite low and can be ignored in first releases. * * @param currentNodeInfo the current node info * @throws IOException Signals that an I/O exception has occurred. */ public void updateNodeData(final ControlNodeInfo currentNodeInfo) throws IOException { this.currentNodeInfo = currentNodeInfo; if (isMaster()) { doZkClientAction(new ZkClientAction() { @Override public void doWithZkClient(CuratorFramework client) throws Exception { client.setData().forPath(ControlNodeTracker.CONTROL_SERVER_NODE_PATH, controlNodeAvroConverter.get().toByteArray(currentNodeInfo)); } }, true); } }
/** * On master change. * * @param currentData the current data */ protected void onMasterChange(ChildData currentData) { ControlNodeInfo controlServerInfo = extractControlServerInfo(currentData); for (ControlNodeListener listener : listeners) { listener.onControlNodeChange(controlServerInfo); } }
/** * Stop Zookeeper Service. */ public void stop() { if (getNodeConfig().isZkEnabled()) { try { controlZkNode.close(); } catch (IOException ex) { LOG.warn("Error closing ZK node", ex); } } }
/** * Checks if is master. * * @return true, if is master */ public boolean isMaster() { ControlNodeInfo serverInfo = getControlServerInfo(); if (currentNodeInfo != null && serverInfo != null) { String curHost = currentNodeInfo.getConnectionInfo().getThriftHost().toString(); int curPort = currentNodeInfo.getConnectionInfo().getThriftPort(); String masterHost = serverInfo.getConnectionInfo().getThriftHost().toString(); int masterPort = serverInfo.getConnectionInfo().getThriftPort(); return curHost.equals(masterHost) && curPort == masterPort; } else { return false; } }
if (resolver == null) { ControlNode zkNode = controlZkService.getControlZkNode(); resolver = new ConsistentHashResolver(zkNode.getCurrentOperationServerNodes(), userHashPartitions); zkNode.addListener(new OperationsNodeListener() { @Override public void onNodeUpdated(OperationsNodeInfo node) {
/** * Starts LoadDistributionService. */ @Override public void start() { LOG.info("Load distribution service starting..."); LOG.info(this.toString()); loadManager = new DynamicLoadManager(this); if (zkService != null) { loadManager.registerListeners(); isMaster = zkService.getControlZkNode().isMaster(); LOG.trace("Load Distribution Service isMaster " + isMaster); operate = true; super.start(); } else { LOG.error("Load distribution service start failed, ZK Service not set."); } }
/** * Start Zookeeper service. */ public void start() { if (getNodeConfig().isZkEnabled()) { LOG.info("Control service starting ZooKepper connection to {}", getNodeConfig().getZkHostPortList()); ControlNodeInfo nodeInfo = new ControlNodeInfo(); ConnectionInfo connectionInfo = new ConnectionInfo( getNodeConfig().getThriftHost(), getNodeConfig().getThriftPort(), null); nodeInfo.setConnectionInfo(connectionInfo); controlZkNode = new ControlNode(nodeInfo, zkClient); try { controlZkNode.start(); } catch (Exception ex) { if (getNodeConfig().isZkIgnoreErrors()) { LOG.info("Failed to register control in ZooKeeper", ex); } else { LOG.error("Failed to register control in ZooKeeper", ex); throw new RuntimeException(ex); // NOSONAR } } } }
@Override protected void onNoMaster() throws IOException { super.onNoMaster(); createZkNode(); } }
/** * Gets the current bootstrap nodes. * * @return the current bootstrap nodes */ public List<BootstrapNodeInfo> getCurrentBootstrapNodes() { if (getNodeConfig().isZkEnabled()) { return controlZkNode.getCurrentBootstrapNodes(); } else { return null; // NOSONAR } }
/** * Deregister listeners for Operations server nodes updates and Bootstrap * nodes updates. */ public void deregisterListeners() { LOG.trace("DynamicLoadManager deregister listeners..."); ControlNode pm = getLoadDistributionService().getZkService().getControlZkNode(); pm.removeListener((OperationsNodeListener) this); pm.removeListener((BootstrapNodeListener) this); }
/** * Register listeners for Operations server nodes updates and Bootstrap * nodes updates. */ public void registerListeners() { LOG.trace("DynamicLoadManager register listeners..."); ControlNode pm = getLoadDistributionService().getZkService().getControlZkNode(); pm.addListener((OperationsNodeListener) this); pm.addListener((BootstrapNodeListener) this); }
List<OperationsNodeInfo> endpoints = controlZkNode.getCurrentOperationServerNodes(); for (OperationsNodeInfo endpoint : endpoints) { String host = endpoint.getConnectionInfo().getThriftHost().toString();
@Override public boolean createZkNode() throws IOException { try { nodePath = zkClient.create().withMode(CreateMode.EPHEMERAL) .forPath(ControlNodeTracker.CONTROL_SERVER_NODE_PATH, controlNodeAvroConverter.get().toByteArray(currentNodeInfo)); LOG.info("Created node with path: " + nodePath); } catch (NodeExistsException ex) { LOG.info("master already exists ", ex); } catch (Exception ex) { LOG.error("Unknown Error", ex); close(); throw new IOException(ex); } return true; }
/** * Check neighbors. */ private void checkNeighbors() { if (neighbors == null) { synchronized (zkLock) { if (neighbors == null) { neighbors = new Neighbors<>( KaaThriftService.OPERATIONS_SERVICE, new NeighborTemplate<OperationsServiceMsg>() { @Override public void process(Iface client, List<OperationsServiceMsg> messages) throws TException { OperationsServiceMsg.dispatch(client, messages); } @Override public void onServerError(String serverId, Exception ex) { LOG.error("Can't send configuration update to {}", serverId, ex); } }, neighborConnectionsSize); ControlNode zkNode = controlZkService.getControlZkNode(); neighbors.setZkNode( KaaThriftService.KAA_NODE_SERVICE, zkNode.getControlServerInfo().getConnectionInfo(), zkNode); } } } }