private static URI getHttpUri(ServiceDescriptor descriptor, boolean httpsRequired) { String url = descriptor.getProperties().get(httpsRequired ? "https" : "http"); if (url != null) { try { return new URI(url); } catch (URISyntaxException ignored) { } } return null; }
.filter(descriptor -> !nodeInfo.getNodeId().equals(descriptor.getNodeId())) .collect(toImmutableSet()); .filter(task -> !onlineIds.contains(task.getService().getId())) .forEach(MonitoringTask::disable); .filter(service -> !tasks.keySet().contains(service.getId())) .collect(toImmutableSet()); tasks.put(service.getId(), new MonitoringTask(service, uri)); .filter(task -> onlineIds.contains(task.getService().getId())) .forEach(MonitoringTask::enable);
@BeforeMethod public void setup() { testHttpClient = new TestingHttpClient(input -> new TestingResponse(OK, ArrayListMultimap.create(), ACTIVE.name().getBytes())); expectedVersion = new NodeVersion("1"); coordinator = new PrestoNode(UUID.randomUUID().toString(), URI.create("https://192.0.2.8"), expectedVersion, false); activeNodes = ImmutableList.of( new PrestoNode(nodeInfo.getNodeId(), URI.create("http://192.0.1.1"), expectedVersion, false), new PrestoNode(UUID.randomUUID().toString(), URI.create("http://192.0.2.1:8080"), expectedVersion, false), new PrestoNode(UUID.randomUUID().toString(), URI.create("http://192.0.2.3"), expectedVersion, false), coordinator); inactiveNodes = ImmutableList.of( new PrestoNode(UUID.randomUUID().toString(), URI.create("https://192.0.3.9"), NodeVersion.UNKNOWN, false), new PrestoNode(UUID.randomUUID().toString(), URI.create("https://192.0.4.9"), new NodeVersion("2"), false)); List<ServiceDescriptor> descriptors = new ArrayList<>(); for (PrestoNode node : Iterables.concat(activeNodes, inactiveNodes)) { descriptors.add(serviceDescriptor("presto") .setNodeId(node.getNodeIdentifier()) .addProperty("http", node.getHttpUri().toString()) .addProperty("node_version", node.getNodeVersion().toString()) .addProperty("coordinator", String.valueOf(node.equals(coordinator))) .build()); } selector = new StaticServiceSelector(descriptors); }
boolean coordinator = isCoordinator(service); if (uri != null && nodeVersion != null) { PrestoNode node = new PrestoNode(service.getNodeId(), uri, nodeVersion, coordinator); NodeState nodeState = getNodeState(node); String connectorIds = service.getProperties().get("connectorIds"); if (connectorIds != null) { connectorIds = connectorIds.toLowerCase(ENGLISH);
private static void assertDescriptorEquals(ServiceDescriptor expected, ServiceDescriptor actual) { assertEquals(actual, expected); assertEquals(actual.getId(), expected.getId()); assertEquals(actual.getNodeId(), expected.getNodeId()); assertEquals(actual.getType(), expected.getType()); assertEquals(actual.getPool(), expected.getPool()); assertEquals(actual.getLocation(), expected.getLocation()); assertEquals(actual.getProperties(), expected.getProperties()); } }
@Test public void testBuilderNodeId() { ServiceDescriptor expected = new ServiceDescriptor( UUID.fromString("12345678-1234-1234-1234-123456789012"), "node", "type", "pool", "location", ServiceState.RUNNING, ImmutableMap.of("a", "apple", "b", "banana")); ServiceDescriptorBuilder builder = serviceDescriptor(expected.getType()) .setId(expected.getId()) .setLocation(expected.getLocation()) .setNodeId(expected.getNodeId()) .setPool(expected.getPool()) .setState(expected.getState()); for (Map.Entry<String, String> entry : expected.getProperties().entrySet()) { builder.addProperty(entry.getKey(), entry.getValue()); } assertDescriptorEquals(expected, builder.build()); }
@Test public void testBuilderNodeInfo() { NodeInfo nodeInfo = new NodeInfo(new NodeConfig().setEnvironment("test").setPool("pool")); ServiceDescriptor expected = new ServiceDescriptor( UUID.fromString("12345678-1234-1234-1234-123456789012"), nodeInfo.getNodeId(), "type", nodeInfo.getPool(), "location", ServiceState.STOPPED, ImmutableMap.of("a", "apple", "b", "banana")); ServiceDescriptor actual = serviceDescriptor(expected.getType()) .setId(expected.getId()) .setLocation(expected.getLocation()) .setNodeInfo(nodeInfo) .setState(expected.getState()) .addProperties(expected.getProperties()) .build(); assertDescriptorEquals(expected, actual); }
if (serviceDescriptor.getId().equals(currentServer)) { continue; String jdbcUrl = serviceDescriptor.getProperties().get("jdbc"); if (jdbcUrl == null) { continue; this.currentServer = serviceDescriptor.getId(); return connection;
@Override public ImmutableList<ServiceDescriptor> getServiceInventory(Iterable<SlotStatus> allSlotStatus) { ImmutableList.Builder<ServiceDescriptor> newDescriptors = ImmutableList.builder(); for (SlotStatus slotStatus : allSlotStatus) { // if the self reference is null, the slot is totally offline so skip for now if (slotStatus.getSelf() == null) { continue; } List<ServiceDescriptor> serviceDescriptors = getServiceInventory(slotStatus); if (serviceDescriptors == null) { continue; } for (ServiceDescriptor serviceDescriptor : serviceDescriptors) { newDescriptors.add(new ServiceDescriptor(null, slotStatus.getId().toString(), serviceDescriptor.getType(), serviceDescriptor.getPool(), slotStatus.getLocation(), slotStatus.getState() == SlotLifecycleState.RUNNING ? ServiceState.RUNNING : ServiceState.STOPPED, interpolateProperties(serviceDescriptor.getProperties(), slotStatus))); } } return newDescriptors.build(); }
@Provides @ForDiscoveryClient public URI getDiscoveryUri(ServiceInventory serviceInventory, DiscoveryClientConfig config) { Iterable<ServiceDescriptor> discovery = serviceInventory.getServiceDescriptors("discovery"); for (ServiceDescriptor descriptor : discovery) { if (descriptor.getState() != ServiceState.RUNNING) { continue; } try { return new URI(descriptor.getProperties().get("https")); } catch (Exception ignored) { } try { return new URI(descriptor.getProperties().get("http")); } catch (Exception ignored) { } } if (config != null) { return config.getDiscoveryServiceURI(); } return null; } }
@Override public void run() { try { ping(); updateState(); } catch (Throwable e) { // ignore to avoid getting unscheduled log.warn(e, "Error pinging service %s (%s)", service.getId(), uri); } } }, heartbeat.toMillis(), heartbeat.toMillis(), TimeUnit.MILLISECONDS);
public ServiceDescriptor build() { return new ServiceDescriptor(id, nodeId, type, pool, location, state, properties.build()); } }
private static PrestoNode findCurrentNode(List<ServiceDescriptor> allServices, String currentNodeId, NodeVersion expectedNodeVersion, boolean httpsRequired) { for (ServiceDescriptor service : allServices) { URI uri = getHttpUri(service, httpsRequired); NodeVersion nodeVersion = getNodeVersion(service); if (uri != null && nodeVersion != null) { PrestoNode node = new PrestoNode(service.getNodeId(), uri, nodeVersion, isCoordinator(service)); if (node.getNodeIdentifier().equals(currentNodeId)) { checkState( node.getNodeVersion().equals(expectedNodeVersion), "INVARIANT: current node version (%s) should be equal to %s", node.getNodeVersion(), expectedNodeVersion); return node; } } } throw new IllegalStateException("INVARIANT: current node not returned from service selector"); }
@Override public boolean apply(ServiceDescriptor serviceDescriptor) { return serviceDescriptor.getType().equals(type); } });
private static void assertDescriptorEquals(ServiceDescriptor expected, ServiceDescriptor actual) { assertEquals(actual, expected); assertEquals(actual.getId(), expected.getId()); assertEquals(actual.getNodeId(), expected.getNodeId()); assertEquals(actual.getType(), expected.getType()); assertEquals(actual.getPool(), expected.getPool()); assertEquals(actual.getLocation(), expected.getLocation()); assertEquals(actual.getProperties(), expected.getProperties()); } }
@Test public void testBuilderNodeId() { ServiceDescriptor expected = new ServiceDescriptor( UUID.fromString("12345678-1234-1234-1234-123456789012"), "node", "type", "pool", "location", ServiceState.RUNNING, ImmutableMap.of("a", "apple", "b", "banana")); ServiceDescriptorBuilder builder = serviceDescriptor(expected.getType()) .setId(expected.getId()) .setLocation(expected.getLocation()) .setNodeId(expected.getNodeId()) .setPool(expected.getPool()) .setState(expected.getState()); for (Map.Entry<String, String> entry : expected.getProperties().entrySet()) { builder.addProperty(entry.getKey(), entry.getValue()); } assertDescriptorEquals(expected, builder.build()); }
@Test public void testBuilderNodeInfo() { NodeInfo nodeInfo = new NodeInfo(new NodeConfig().setEnvironment("test").setPool("pool")); ServiceDescriptor expected = new ServiceDescriptor( UUID.fromString("12345678-1234-1234-1234-123456789012"), nodeInfo.getNodeId(), "type", nodeInfo.getPool(), "location", ServiceState.STOPPED, ImmutableMap.of("a", "apple", "b", "banana")); ServiceDescriptor actual = serviceDescriptor(expected.getType()) .setId(expected.getId()) .setLocation(expected.getLocation()) .setNodeInfo(nodeInfo) .setState(expected.getState()) .addProperties(expected.getProperties()) .build(); assertDescriptorEquals(expected, actual); }
if (serviceDescriptor.getId().equals(currentServer)) { continue; String jdbcUrl = serviceDescriptor.getProperties().get("jdbc"); if (jdbcUrl == null) { continue; this.currentServer = serviceDescriptor.getId(); return connection;
private void synchronize() if (descriptor.getNodeId().equals(node.getNodeId())) { String uri = descriptor.getProperties().get("http"); if (uri == null) { log.error("service descriptor for node %s is missing http uri", descriptor.getNodeId()); continue;