Search in sources :

Example 1 with SHUTTING_DOWN

use of io.trino.metadata.NodeState.SHUTTING_DOWN in project trino by trinodb.

the class DiscoveryNodeManager method refreshNodesInternal.

private synchronized void refreshNodesInternal() {
    // This is a deny-list.
    Set<ServiceDescriptor> services = serviceSelector.selectAllServices().stream().filter(service -> !failureDetector.getFailed().contains(service)).collect(toImmutableSet());
    ImmutableSet.Builder<InternalNode> activeNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<InternalNode> inactiveNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<InternalNode> shuttingDownNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<InternalNode> coordinatorsBuilder = ImmutableSet.builder();
    ImmutableSetMultimap.Builder<CatalogName, InternalNode> byConnectorIdBuilder = ImmutableSetMultimap.builder();
    for (ServiceDescriptor service : services) {
        URI uri = getHttpUri(service, httpsRequired);
        NodeVersion nodeVersion = getNodeVersion(service);
        boolean coordinator = isCoordinator(service);
        if (uri != null && nodeVersion != null) {
            InternalNode node = new InternalNode(service.getNodeId(), uri, nodeVersion, coordinator);
            NodeState nodeState = getNodeState(node);
            switch(nodeState) {
                case ACTIVE:
                    activeNodesBuilder.add(node);
                    if (coordinator) {
                        coordinatorsBuilder.add(node);
                    }
                    // record available active nodes organized by connector id
                    String connectorIds = service.getProperties().get("connectorIds");
                    if (connectorIds != null) {
                        connectorIds = connectorIds.toLowerCase(ENGLISH);
                        for (String connectorId : CONNECTOR_ID_SPLITTER.split(connectorIds)) {
                            byConnectorIdBuilder.put(new CatalogName(connectorId), node);
                        }
                    }
                    // always add system connector
                    byConnectorIdBuilder.put(new CatalogName(GlobalSystemConnector.NAME), node);
                    break;
                case INACTIVE:
                    inactiveNodesBuilder.add(node);
                    break;
                case SHUTTING_DOWN:
                    shuttingDownNodesBuilder.add(node);
                    break;
                default:
                    log.error("Unknown state %s for node %s", nodeState, node);
            }
        }
    }
    if (allNodes != null) {
        // log node that are no longer active (but not shutting down)
        SetView<InternalNode> missingNodes = difference(allNodes.getActiveNodes(), Sets.union(activeNodesBuilder.build(), shuttingDownNodesBuilder.build()));
        for (InternalNode missingNode : missingNodes) {
            log.info("Previously active node is missing: %s (last seen at %s)", missingNode.getNodeIdentifier(), missingNode.getHost());
        }
    }
    // nodes by connector id changes anytime a node adds or removes a connector (note: this is not part of the listener system)
    activeNodesByCatalogName = byConnectorIdBuilder.build();
    AllNodes allNodes = new AllNodes(activeNodesBuilder.build(), inactiveNodesBuilder.build(), shuttingDownNodesBuilder.build(), coordinatorsBuilder.build());
    // only update if all nodes actually changed (note: this does not include the connectors registered with the nodes)
    if (!allNodes.equals(this.allNodes)) {
        // assign allNodes to a local variable for use in the callback below
        this.allNodes = allNodes;
        coordinators = coordinatorsBuilder.build();
        // notify listeners
        List<Consumer<AllNodes>> listeners = ImmutableList.copyOf(this.listeners);
        nodeStateEventExecutor.submit(() -> listeners.forEach(listener -> listener.accept(allNodes)));
    }
}
Also used : HttpClient(io.airlift.http.client.HttpClient) GlobalSystemConnector(io.trino.connector.system.GlobalSystemConnector) ServiceType(io.airlift.discovery.client.ServiceType) Logger(io.airlift.log.Logger) URISyntaxException(java.net.URISyntaxException) NodeInfo(io.airlift.node.NodeInfo) ArrayList(java.util.ArrayList) ServiceSelector(io.airlift.discovery.client.ServiceSelector) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) CatalogName(io.trino.connector.CatalogName) Sets.difference(com.google.common.collect.Sets.difference) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NodeVersion(io.trino.client.NodeVersion) URI(java.net.URI) Splitter(com.google.common.base.Splitter) ENGLISH(java.util.Locale.ENGLISH) ExecutorService(java.util.concurrent.ExecutorService) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) SHUTTING_DOWN(io.trino.metadata.NodeState.SHUTTING_DOWN) ImmutableSet(com.google.common.collect.ImmutableSet) ACTIVE(io.trino.metadata.NodeState.ACTIVE) FailureDetector(io.trino.failuredetector.FailureDetector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SetView(com.google.common.collect.Sets.SetView) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) InternalCommunicationConfig(io.trino.server.InternalCommunicationConfig) GuardedBy(javax.annotation.concurrent.GuardedBy) SetMultimap(com.google.common.collect.SetMultimap) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) PostConstruct(javax.annotation.PostConstruct) HttpUriBuilder.uriBuilderFrom(io.airlift.http.client.HttpUriBuilder.uriBuilderFrom) Optional(java.util.Optional) INACTIVE(io.trino.metadata.NodeState.INACTIVE) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) URI(java.net.URI) NodeVersion(io.trino.client.NodeVersion) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) Consumer(java.util.function.Consumer) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) CatalogName(io.trino.connector.CatalogName)

Aggregations

Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Splitter (com.google.common.base.Splitter)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 SetMultimap (com.google.common.collect.SetMultimap)1 Sets (com.google.common.collect.Sets)1 SetView (com.google.common.collect.Sets.SetView)1 Sets.difference (com.google.common.collect.Sets.difference)1 Threads.daemonThreadsNamed (io.airlift.concurrent.Threads.daemonThreadsNamed)1 ServiceDescriptor (io.airlift.discovery.client.ServiceDescriptor)1 ServiceSelector (io.airlift.discovery.client.ServiceSelector)1 ServiceType (io.airlift.discovery.client.ServiceType)1 HttpClient (io.airlift.http.client.HttpClient)1 HttpUriBuilder.uriBuilderFrom (io.airlift.http.client.HttpUriBuilder.uriBuilderFrom)1 Logger (io.airlift.log.Logger)1 NodeInfo (io.airlift.node.NodeInfo)1 NodeVersion (io.trino.client.NodeVersion)1 CatalogName (io.trino.connector.CatalogName)1