@Override public Host getHostForSegment(String scope, String stream, long segmentId) { return new Host("localhost", 1000, ""); } }
public CompletableFuture<List<NodeUri>> getControllerServerList() { if (cluster == null) { return Futures.failedFuture(new IllegalStateException("Controller cluster not initialized")); } return CompletableFuture.supplyAsync(() -> { try { return cluster.getClusterMembers().stream() .map(host -> NodeUri.newBuilder().setEndpoint(host.getIpAddr()).setPort(host.getPort()).build()) .collect(Collectors.toList()); } catch (ClusterException e) { // cluster implementation throws checked exceptions which cannot be thrown inside completable futures. throw Exceptions.sneakyThrow(e); } }, executor); }
private CompletableFuture<Void> handleHostRemoved(Host host) { return Futures.allOf(sweepers.stream().map(sweeper -> { if (sweeper.isReady()) { // Note: if we find sweeper to be ready, it is possible that this processes can be swept by both // sweepFailedProcesses and handleFailedProcess. A sweep is safe and idempotent operation. return RetryHelper.withIndefiniteRetriesAsync(() -> sweeper.handleFailedProcess(host.getHostId()), e -> log.warn(e.getMessage()), executor); } else { return CompletableFuture.completedFuture((Void) null); } }).collect(Collectors.toList())); }
@Override public String toString() { return this.getIpAddr() + ":" + this.getPort() + ((this.getEndpointId() == null) ? "" : ":" + this.getEndpointId()); }
public String getHostId() { return this.getIpAddr() + "-" + this.getEndpointId(); } }
switch (eventType) { case HOST_ADDED: nodeAddedQueue.offer(host.getIpAddr()); break; case HOST_REMOVED: nodeRemovedQueue.offer(host.getIpAddr()); break; case ERROR: clusterZKInstance1.registerHost(new Host(HOST_1, PORT, null)); assertEquals(HOST_1, nodeAddedQueue.poll(5, TimeUnit.SECONDS)); clusterZKInstance1.deregisterHost(new Host(HOST_1, PORT, null)); assertEquals(HOST_1, nodeRemovedQueue.poll(5, TimeUnit.SECONDS));
@Test(timeout = 60000L) public void clusterListenerTest() throws InterruptedException { String hostName = "localhost"; Host host = new Host(hostName, 10, "host1"); TaskSweeper taskSweeper = new TaskSweeper(taskStore, host.getHostId(), executor, new TestTasks(taskStore, executor, host.getHostId())); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); StreamTransactionMetadataTasks txnTasks = new StreamTransactionMetadataTasks(streamStore, hostStore, segmentHelper, executor, host.getHostId(), connectionFactory, AuthHelper.getDisabledAuthHelper()); txnTasks.initializeStreamWriters("commitStream", new EventStreamWriterMock<>(), "abortStream", new EventStreamWriterMock<>()); validateAddedNode(host.getHostId()); Host host1 = new Host(hostName, 20, "host2"); clusterZK.registerHost(host1); validateAddedNode(host1.getHostId()); validateRemovedNode(host1.getHostId()); validateRemovedNode(host.getHostId());
private void reportContainerCountPerHost(Host host, Set<Integer> containerIds) { DYNAMIC_LOGGER.reportGaugeValue(nameFromHost(SEGMENT_STORE_HOST_CONTAINER_COUNT, host.toString()), containerIds.size()); }
private Set<Integer> getDesiredContainerList() { log.debug("Fetching the latest container assignment from ZooKeeper."); if (hostContainerMapNode.getCurrentData() != null) { //Check if path exists. //read data from zk. byte[] containerToHostMapSer = hostContainerMapNode.getCurrentData().getData(); if (containerToHostMapSer != null) { @SuppressWarnings("unchecked") val controlMapping = (Map<Host, Set<Integer>>) SerializationUtils.deserialize(containerToHostMapSer); return controlMapping.entrySet().stream() .filter(ep -> ep.getKey().equals(this.host)) .map(Map.Entry::getValue) .findFirst().orElse(Collections.emptySet()); } } return null; } }
switch (eventType) { case HOST_ADDED: nodeAddedQueue.offer(host.getIpAddr()); break; case HOST_REMOVED: nodeRemovedQueue.offer(host.getIpAddr()); break; case ERROR: clusterZKInstance1.registerHost(new Host(HOST_1, PORT, null)); assertEquals(HOST_1, nodeAddedQueue.poll(5, TimeUnit.SECONDS)); clusterZKInstance1.registerHost(new Host(HOST_2, PORT, null)); assertEquals(HOST_2, nodeAddedQueue.poll(5, TimeUnit.SECONDS)); assertEquals(2, clusterListener.getClusterMembers().size());
Host host = new Host(hostName, getPort(), UUID.randomUUID().toString()); serviceConfig.getGRPCServerConfig().get().getTokenSigningKey()); streamMetadataTasks = new StreamMetadataTasks(streamStore, hostStore, taskMetadataStore, segmentHelper, controllerExecutor, host.getHostId(), connectionFactory, authHelper, requestTracker); streamTransactionMetadataTasks = new StreamTransactionMetadataTasks(streamStore, hostStore, segmentHelper, controllerExecutor, host.getHostId(), serviceConfig.getTimeoutServiceConfig(), connectionFactory, authHelper); streamCutService = new StreamCutService(Config.BUCKET_COUNT, host.getHostId(), streamStore, streamMetadataTasks, retentionExecutor, requestTracker); log.info("starting auto retention service asynchronously"); TaskSweeper taskSweeper = new TaskSweeper(taskMetadataStore, host.getHostId(), controllerExecutor, streamMetadataTasks); controllerEventProcessors = new ControllerEventProcessors(host.getHostId(), serviceConfig.getEventProcessorConfig().get(), localController, checkpointStore, streamStore, connectionFactory, streamMetadataTasks, streamTransactionMetadataTasks,
private void reportHostFailures(Host failedHost) { DYNAMIC_LOGGER.incCounterValue(globalMetricName(SEGMENT_STORE_HOST_FAILURES), 1); DYNAMIC_LOGGER.incCounterValue(nameFromHost(SEGMENT_STORE_HOST_FAILURES, failedHost.toString()), 1); // Set to 0 the number of containers for the failed host. DYNAMIC_LOGGER.reportGaugeValue(nameFromHost(SEGMENT_STORE_HOST_CONTAINER_COUNT, failedHost.toString()), 0); }
public Controller.NodeUri getSegmentUri(final String scope, final String stream, final long segmentId, final HostControllerStore hostStore) { final Host host = hostStore.getHostForSegment(scope, stream, segmentId); return Controller.NodeUri.newBuilder().setEndpoint(host.getIpAddr()).setPort(host.getPort()).build(); }
public static Map<Host, Set<Integer>> getHostContainerMap(String host, int port, int containerCount) { Exceptions.checkNotNullOrEmpty(host, "host"); Preconditions.checkArgument(port > 0, "port"); Preconditions.checkArgument(containerCount > 0, "containerCount"); Map<Host, Set<Integer>> hostContainerMap = new HashMap<>(); hostContainerMap.put(new Host(host, port, null), IntStream.range(0, containerCount).boxed().collect(Collectors.toSet())); return hostContainerMap; } }
public void clusterListenerStarterTest() throws InterruptedException, ExecutionException { String hostName = "localhost"; Host host = new Host(hostName, 10, "originalhost"); TaskSweeper taskSweeper = spy(new TaskSweeper(taskStore, host.getHostId(), executor, new TestTasks(taskStore, executor, host.getHostId()))); segmentHelper, executor, host.getHostId(), connectionFactory, AuthHelper.getDisabledAuthHelper()); verify(taskSweeper, times(0)).handleFailedProcess(anyString()); verify(txnSweeper, times(0)).handleFailedProcess(anyString()); validateAddedNode(host.getHostId()); Host newHost = new Host(hostName, 20, "newHost1"); clusterZK.registerHost(newHost); validateAddedNode(newHost.getHostId()); clusterZK.deregisterHost(newHost); validateRemovedNode(newHost.getHostId()); log.info("deregistering new host"); newHost = new Host(hostName, 20, "newHost2"); clusterZK.registerHost(newHost); validateAddedNode(newHost.getHostId()); clusterZK.deregisterHost(newHost); log.info("removing newhost2"); validateRemovedNode(newHost.getHostId()); assertTrue(Futures.await(taskHostSweep2, 3000)); assertTrue(Futures.await(txnHostSweep2, 3000));
/** * Register Host to cluster. * * @param host Host to be part of cluster. */ @Override @Synchronized public void registerHost(Host host) { Preconditions.checkNotNull(host, "host"); Exceptions.checkArgument(!entryMap.containsKey(host), "host", "host is already registered to cluster."); String hostPath = ZKPaths.makePath(getPathPrefix(), host.toString()); PersistentNode node = new PersistentNode(client, CreateMode.EPHEMERAL, false, hostPath, SerializationUtils.serialize(host)); node.start(); //start creation of ephemeral node in background. entryMap.put(host, node); }
@Before public void setup() throws Exception { // 1. Start ZK server. zkServer = new TestingServerStarter().start(); // 2. Start ZK client. curatorClient = CuratorFrameworkFactory.newClient(zkServer.getConnectString(), new ExponentialBackoffRetry(200, 10, 5000)); curatorClient.start(); // 3. Start executor service. executor = Executors.newScheduledThreadPool(5); // 4. start cluster event listener clusterZK = new ClusterZKImpl(curatorClient, ClusterType.CONTROLLER); clusterZK.addListener((eventType, host) -> { switch (eventType) { case HOST_ADDED: nodeAddedQueue.offer(host.getHostId()); break; case HOST_REMOVED: nodeRemovedQueue.offer(host.getHostId()); break; case ERROR: default: break; } }); }
private void validateStore(HostControllerStore hostStore) { // Validate store values. Assert.assertEquals(containerCount, hostStore.getContainerCount()); Host hostObj = hostStore.getHostForSegment("dummyScope", "dummyStream", (int) Math.floor(containerCount * Math.random())); Assert.assertEquals(controllerPort, hostObj.getPort()); Assert.assertEquals(host, hostObj.getIpAddr()); } }
private void attachZKSegmentManager(ServiceBuilder builder) { builder.withContainerManager(setup -> new ZKSegmentContainerManager(setup.getContainerRegistry(), this.zkClient, new Host(this.serviceConfig.getPublishedIPAddress(), this.serviceConfig.getPublishedPort(), null), setup.getCoreExecutor())); }
hosts.add(new Host("host1", 123, null)); rebalance = balancer.rebalance(new HashMap<>(), hosts); assertEquals(1, rebalance.size()); hosts.add(new Host("host2", 123, null)); rebalance = balancer.rebalance(rebalance, hosts); assertEquals(2, rebalance.size()); hosts.add(new Host("host3", 123, null)); hosts.add(new Host("host4", 123, null)); rebalance = balancer.rebalance(rebalance, hosts); assertEquals(4, rebalance.size()); hosts.remove(new Host("host2", 123, null)); rebalance = balancer.rebalance(rebalance, hosts); assertEquals(3, rebalance.size()); hosts.add(new Host("host2", 123, null)); hosts.add(new Host("host5", 123, null)); hosts.add(new Host("host6", 123, null)); hosts.add(new Host("host7", 123, null)); hosts.add(new Host("host8", 123, null)); hosts.add(new Host("host9", 123, null)); hosts.remove(new Host("host1", 123, null)); hosts.remove(new Host("host3", 123, null)); hosts.remove(new Host("host4", 123, null)); rebalance = balancer.rebalance(rebalance, hosts); assertEquals(6, rebalance.size());