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;
}
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);
}
});
}
}
Aggregations