Search in sources :

Example 1 with NodeState

use of com.facebook.presto.spi.NodeState in project presto by prestodb.

the class DiscoveryNodeManager method refreshNodesInternal.

private synchronized PrestoNode refreshNodesInternal() {
    lastUpdateTimestamp = System.nanoTime();
    // This is currently a blacklist.
    // TODO: make it a whitelist (a failure-detecting service selector) and maybe build in support for injecting this in airlift
    Set<ServiceDescriptor> services = serviceSelector.selectAllServices().stream().filter(service -> !failureDetector.getFailed().contains(service)).collect(toImmutableSet());
    PrestoNode currentNode = null;
    ImmutableSet.Builder<Node> activeNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> inactiveNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> shuttingDownNodesBuilder = ImmutableSet.builder();
    ImmutableSet.Builder<Node> coordinatorsBuilder = ImmutableSet.builder();
    ImmutableSetMultimap.Builder<ConnectorId, Node> byConnectorIdBuilder = ImmutableSetMultimap.builder();
    for (ServiceDescriptor service : services) {
        URI uri = getHttpUri(service);
        NodeVersion nodeVersion = getNodeVersion(service);
        boolean coordinator = isCoordinator(service);
        if (uri != null && nodeVersion != null) {
            PrestoNode node = new PrestoNode(service.getNodeId(), uri, nodeVersion, coordinator);
            NodeState nodeState = getNodeState(node);
            // record current node
            if (node.getNodeIdentifier().equals(nodeInfo.getNodeId())) {
                currentNode = node;
                checkState(currentNode.getNodeVersion().equals(expectedNodeVersion), "INVARIANT: current node version (%s) should be equal to %s", currentNode.getNodeVersion(), expectedNodeVersion);
            }
            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 ConnectorId(connectorId), node);
                        }
                    }
                    // always add system connector
                    byConnectorIdBuilder.put(new ConnectorId(GlobalSystemConnector.NAME), node);
                    break;
                case INACTIVE:
                    inactiveNodesBuilder.add(node);
                    break;
                case SHUTTING_DOWN:
                    shuttingDownNodesBuilder.add(node);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown node state " + nodeState);
            }
        }
    }
    if (allNodes != null) {
        // log node that are no longer active (but not shutting down)
        SetView<Node> missingNodes = difference(allNodes.getActiveNodes(), Sets.union(activeNodesBuilder.build(), shuttingDownNodesBuilder.build()));
        for (Node missingNode : missingNodes) {
            log.info("Previously active node is missing: %s (last seen at %s)", missingNode.getNodeIdentifier(), missingNode.getHostAndPort());
        }
    }
    allNodes = new AllNodes(activeNodesBuilder.build(), inactiveNodesBuilder.build(), shuttingDownNodesBuilder.build());
    activeNodesByConnectorId = byConnectorIdBuilder.build();
    coordinators = coordinatorsBuilder.build();
    checkState(currentNode != null, "INVARIANT: current node not returned from service selector");
    return currentNode;
}
Also used : HttpClient(io.airlift.http.client.HttpClient) ServiceType(io.airlift.discovery.client.ServiceType) Logger(io.airlift.log.Logger) URISyntaxException(java.net.URISyntaxException) NodeInfo(io.airlift.node.NodeInfo) NodeVersion(com.facebook.presto.client.NodeVersion) Duration(io.airlift.units.Duration) ACTIVE(com.facebook.presto.spi.NodeState.ACTIVE) ServiceSelector(io.airlift.discovery.client.ServiceSelector) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) Sets.difference(com.google.common.collect.Sets.difference) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) Node(com.facebook.presto.spi.Node) Managed(org.weakref.jmx.Managed) GlobalSystemConnector(com.facebook.presto.connector.system.GlobalSystemConnector) Arrays.asList(java.util.Arrays.asList) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImmutableCollectors.toImmutableSet(com.facebook.presto.util.ImmutableCollectors.toImmutableSet) URI(java.net.URI) Splitter(com.google.common.base.Splitter) ENGLISH(java.util.Locale.ENGLISH) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) SHUTTING_DOWN(com.facebook.presto.spi.NodeState.SHUTTING_DOWN) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SetView(com.google.common.collect.Sets.SetView) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) GuardedBy(javax.annotation.concurrent.GuardedBy) Threads.threadsNamed(io.airlift.concurrent.Threads.threadsNamed) NodeState(com.facebook.presto.spi.NodeState) 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) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) FailureDetector(com.facebook.presto.failureDetector.FailureDetector) PostConstruct(javax.annotation.PostConstruct) HttpUriBuilder.uriBuilderFrom(io.airlift.http.client.HttpUriBuilder.uriBuilderFrom) Optional(java.util.Optional) ConnectorId(com.facebook.presto.connector.ConnectorId) INACTIVE(com.facebook.presto.spi.NodeState.INACTIVE) NodeState(com.facebook.presto.spi.NodeState) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Node(com.facebook.presto.spi.Node) URI(java.net.URI) NodeVersion(com.facebook.presto.client.NodeVersion) ImmutableCollectors.toImmutableSet(com.facebook.presto.util.ImmutableCollectors.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 2 with NodeState

use of com.facebook.presto.spi.NodeState in project presto by prestodb.

the class RemoteNodeState method asyncRefresh.

public synchronized void asyncRefresh() {
    Duration sinceUpdate = nanosSince(lastUpdateNanos.get());
    if (nanosSince(lastWarningLogged.get()).toMillis() > 1_000 && sinceUpdate.toMillis() > 10_000 && future.get() != null) {
        log.warn("Node state update request to %s has not returned in %s", stateInfoUri, sinceUpdate.toString(SECONDS));
        lastWarningLogged.set(System.nanoTime());
    }
    if (sinceUpdate.toMillis() > 1_000 && future.get() == null) {
        Request request = prepareGet().setUri(stateInfoUri).setHeader(CONTENT_TYPE, JSON_UTF_8.toString()).build();
        HttpResponseFuture<JsonResponse<NodeState>> responseFuture = httpClient.executeAsync(request, createFullJsonResponseHandler(jsonCodec(NodeState.class)));
        future.compareAndSet(null, responseFuture);
        Futures.addCallback(responseFuture, new FutureCallback<JsonResponse<NodeState>>() {

            @Override
            public void onSuccess(@Nullable JsonResponse<NodeState> result) {
                lastUpdateNanos.set(System.nanoTime());
                future.compareAndSet(responseFuture, null);
                if (result != null) {
                    if (result.hasValue()) {
                        nodeState.set(ofNullable(result.getValue()));
                    }
                    if (result.getStatusCode() != OK.code()) {
                        log.warn("Error fetching node state from %s returned status %d: %s", stateInfoUri, result.getStatusCode(), result.getStatusMessage());
                        return;
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                log.warn("Error fetching node state from %s: %s", stateInfoUri, t.getMessage());
                lastUpdateNanos.set(System.nanoTime());
                future.compareAndSet(responseFuture, null);
            }
        });
    }
}
Also used : NodeState(com.facebook.presto.spi.NodeState) Request(io.airlift.http.client.Request) Duration(io.airlift.units.Duration) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse)

Aggregations

NodeState (com.facebook.presto.spi.NodeState)2 Duration (io.airlift.units.Duration)2 NodeVersion (com.facebook.presto.client.NodeVersion)1 ConnectorId (com.facebook.presto.connector.ConnectorId)1 GlobalSystemConnector (com.facebook.presto.connector.system.GlobalSystemConnector)1 FailureDetector (com.facebook.presto.failureDetector.FailureDetector)1 Node (com.facebook.presto.spi.Node)1 ACTIVE (com.facebook.presto.spi.NodeState.ACTIVE)1 INACTIVE (com.facebook.presto.spi.NodeState.INACTIVE)1 SHUTTING_DOWN (com.facebook.presto.spi.NodeState.SHUTTING_DOWN)1 ImmutableCollectors.toImmutableSet (com.facebook.presto.util.ImmutableCollectors.toImmutableSet)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Splitter (com.google.common.base.Splitter)1 ImmutableSet (com.google.common.collect.ImmutableSet)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.threadsNamed (io.airlift.concurrent.Threads.threadsNamed)1