Search in sources :

Example 81 with NodeIdentifier

use of org.apache.nifi.cluster.protocol.NodeIdentifier in project nifi by apache.

the class ThreadPoolRequestReplicator method submitAsyncRequest.

private void submitAsyncRequest(final Set<NodeIdentifier> nodeIds, final String scheme, final String path, final Function<NodeIdentifier, NodeHttpRequest> callableFactory, final Map<String, String> headers) {
    if (nodeIds.isEmpty()) {
        // return quickly for trivial case
        return;
    }
    // submit the requests to the nodes
    for (final NodeIdentifier nodeId : nodeIds) {
        final NodeHttpRequest callable = callableFactory.apply(nodeId);
        executorService.submit(callable);
    }
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 82 with NodeIdentifier

use of org.apache.nifi.cluster.protocol.NodeIdentifier in project nifi by apache.

the class ThreadPoolRequestReplicator method onCompletedResponse.

/**
 * When all nodes have completed a request and provided a response (or have timed out), this method will be invoked
 * to handle calling the Callback that was provided for the request, if any, and handle any cleanup or post-processing
 * related to the request
 *
 * @param requestId the ID of the request that has completed
 */
private void onCompletedResponse(final String requestId) {
    final AsyncClusterResponse response = responseMap.get(requestId);
    if (response != null && callback != null) {
        try {
            callback.afterRequest(response.getURIPath(), response.getMethod(), response.getCompletedNodeResponses());
        } catch (final Exception e) {
            logger.warn("Completed request {} {} but failed to properly handle the Request Completion Callback due to {}", response.getMethod(), response.getURIPath(), e.toString());
            logger.warn("", e);
        }
    }
    if (response != null && logger.isDebugEnabled()) {
        logTimingInfo(response);
    }
    // If we have any nodes that are slow to respond, keep track of this. If the same node is slow 3 times in
    // a row, log a warning to indicate that the node is responding slowly.
    final Set<NodeIdentifier> slowResponseNodes = ResponseUtils.findLongResponseTimes(response, 1.5D);
    for (final NodeIdentifier nodeId : response.getNodesInvolved()) {
        final AtomicInteger counter = sequentialLongRequestCounts.computeIfAbsent(nodeId, id -> new AtomicInteger(0));
        if (slowResponseNodes.contains(nodeId)) {
            final int sequentialLongRequests = counter.incrementAndGet();
            if (sequentialLongRequests >= 3) {
                final String message = "Response time from " + nodeId + " was slow for each of the last 3 requests made. " + "To see more information about timing, enable DEBUG logging for " + logger.getName();
                logger.warn(message);
                if (eventReporter != null) {
                    eventReporter.reportEvent(Severity.WARNING, "Node Response Time", message);
                }
                counter.set(0);
            }
        } else {
            counter.set(0);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) DisconnectedNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException) URISyntaxException(java.net.URISyntaxException) UriConstructionException(org.apache.nifi.cluster.manager.exception.UriConstructionException) IllegalClusterStateException(org.apache.nifi.cluster.manager.exception.IllegalClusterStateException) NoConnectedNodesException(org.apache.nifi.cluster.manager.exception.NoConnectedNodesException) AccessDeniedException(org.apache.nifi.authorization.AccessDeniedException) ConnectingNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException) UnknownNodeException(org.apache.nifi.cluster.manager.exception.UnknownNodeException)

Example 83 with NodeIdentifier

use of org.apache.nifi.cluster.protocol.NodeIdentifier in project nifi by apache.

the class NodeClusterCoordinator method resetNodeStatus.

@Override
public boolean resetNodeStatus(final NodeConnectionStatus connectionStatus, final long qualifyingUpdateId) {
    final NodeIdentifier nodeId = connectionStatus.getNodeIdentifier();
    final NodeConnectionStatus currentStatus = getConnectionStatus(nodeId);
    if (currentStatus == null) {
        return replaceNodeStatus(nodeId, null, connectionStatus);
    } else if (currentStatus.getUpdateIdentifier() == qualifyingUpdateId) {
        return replaceNodeStatus(nodeId, currentStatus, connectionStatus);
    }
    // The update identifier is not the same. We will not replace the value
    return false;
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 84 with NodeIdentifier

use of org.apache.nifi.cluster.protocol.NodeIdentifier in project nifi by apache.

the class NodeClusterCoordinator method afterRequest.

/**
 * Callback that is called after an HTTP Request has been replicated to
 * nodes in the cluster. This allows us to disconnect nodes that did not
 * complete the request, if applicable.
 */
@Override
public void afterRequest(final String uriPath, final String method, final Set<NodeResponse> nodeResponses) {
    // as the cluster coordinator is responsible for performing the actual request replication.
    if (!isActiveClusterCoordinator()) {
        return;
    }
    final boolean mutableRequest = isMutableRequest(method);
    /*
         * Nodes that encountered issues handling the request are marked as
         * disconnected for mutable requests (e.g., post, put, delete). For
         * other requests (e.g., get, head), the nodes remain in their current
         * state even if they had problems handling the request.
         */
    if (mutableRequest) {
        final HttpResponseMapper responseMerger = new StandardHttpResponseMapper(nifiProperties);
        final Set<NodeResponse> problematicNodeResponses = responseMerger.getProblematicNodeResponses(nodeResponses);
        // all nodes failed
        final boolean allNodesFailed = problematicNodeResponses.size() == nodeResponses.size();
        // some nodes had a problematic response because of a missing counter, ensure the are not disconnected
        final boolean someNodesFailedMissingCounter = !problematicNodeResponses.isEmpty() && problematicNodeResponses.size() < nodeResponses.size() && isMissingCounter(problematicNodeResponses, uriPath);
        // ensure nodes stay connected in certain scenarios
        if (allNodesFailed) {
            logger.warn("All nodes failed to process URI {} {}. As a result, no node will be disconnected from cluster", method, uriPath);
            return;
        }
        if (someNodesFailedMissingCounter) {
            return;
        }
        // disconnect problematic nodes
        if (!problematicNodeResponses.isEmpty() && problematicNodeResponses.size() < nodeResponses.size()) {
            final Set<NodeIdentifier> failedNodeIds = problematicNodeResponses.stream().map(response -> response.getNodeId()).collect(Collectors.toSet());
            logger.warn(String.format("The following nodes failed to process URI %s '%s'.  Requesting each node disconnect from cluster.", uriPath, failedNodeIds));
            for (final NodeIdentifier nodeId : failedNodeIds) {
                requestNodeDisconnect(nodeId, DisconnectionCode.FAILED_TO_SERVICE_REQUEST, "Failed to process request " + method + " " + uriPath);
            }
        }
    }
}
Also used : NodeProtocolSender(org.apache.nifi.cluster.protocol.NodeProtocolSender) ConnectionResponseMessage(org.apache.nifi.cluster.protocol.message.ConnectionResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) RequestCompletionCallback(org.apache.nifi.cluster.coordination.http.replication.RequestCompletionCallback) LoggerFactory(org.slf4j.LoggerFactory) FlowService(org.apache.nifi.services.FlowService) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) ReconnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ReconnectionRequestMessage) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ConnectionRequestMessage(org.apache.nifi.cluster.protocol.message.ConnectionRequestMessage) ProtocolHandler(org.apache.nifi.cluster.protocol.ProtocolHandler) NodeEvent(org.apache.nifi.cluster.event.NodeEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpResponseMapper(org.apache.nifi.cluster.coordination.http.HttpResponseMapper) Set(java.util.Set) RevisionManager(org.apache.nifi.web.revision.RevisionManager) UUID(java.util.UUID) StandardHttpResponseMapper(org.apache.nifi.cluster.coordination.http.StandardHttpResponseMapper) Collectors(java.util.stream.Collectors) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) ClusterCoordinationProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.ClusterCoordinationProtocolSenderListener) List(java.util.List) ConnectionResponse(org.apache.nifi.cluster.protocol.ConnectionResponse) Pattern(java.util.regex.Pattern) ComponentRevision(org.apache.nifi.cluster.protocol.ComponentRevision) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) LeaderElectionManager(org.apache.nifi.controller.leader.election.LeaderElectionManager) Event(org.apache.nifi.cluster.event.Event) HashMap(java.util.HashMap) NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) CircularFifoQueue(org.apache.commons.collections4.queue.CircularFifoQueue) IllegalNodeDisconnectionException(org.apache.nifi.cluster.manager.exception.IllegalNodeDisconnectionException) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) MessageType(org.apache.nifi.cluster.protocol.message.ProtocolMessage.MessageType) ClusterNodeFirewall(org.apache.nifi.cluster.firewall.ClusterNodeFirewall) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) IOException(java.io.IOException) NodeStatusChangeMessage(org.apache.nifi.cluster.protocol.message.NodeStatusChangeMessage) DisconnectMessage(org.apache.nifi.cluster.protocol.message.DisconnectMessage) AtomicLong(java.util.concurrent.atomic.AtomicLong) FlowElection(org.apache.nifi.cluster.coordination.flow.FlowElection) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) EventReporter(org.apache.nifi.events.EventReporter) NiFiProperties(org.apache.nifi.util.NiFiProperties) ClusterWorkloadRequestMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadRequestMessage) Severity(org.apache.nifi.reporting.Severity) Collections(java.util.Collections) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) HttpResponseMapper(org.apache.nifi.cluster.coordination.http.HttpResponseMapper) StandardHttpResponseMapper(org.apache.nifi.cluster.coordination.http.StandardHttpResponseMapper) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) StandardHttpResponseMapper(org.apache.nifi.cluster.coordination.http.StandardHttpResponseMapper)

Example 85 with NodeIdentifier

use of org.apache.nifi.cluster.protocol.NodeIdentifier in project nifi by apache.

the class NodeClusterCoordinator method handleNodeStatusChange.

private void handleNodeStatusChange(final NodeStatusChangeMessage statusChangeMessage) {
    final NodeConnectionStatus updatedStatus = statusChangeMessage.getNodeConnectionStatus();
    final NodeIdentifier nodeId = statusChangeMessage.getNodeId();
    logger.debug("Handling request {}", statusChangeMessage);
    final NodeConnectionStatus oldStatus = nodeStatuses.get(statusChangeMessage.getNodeId());
    // Either remove the value from the map or update the map depending on the connection state
    if (statusChangeMessage.getNodeConnectionStatus().getState() == NodeConnectionState.REMOVED) {
        nodeStatuses.remove(nodeId, oldStatus);
    } else {
        nodeStatuses.put(nodeId, updatedStatus);
    }
    logger.info("Status of {} changed from {} to {}", statusChangeMessage.getNodeId(), oldStatus, updatedStatus);
    logger.debug("State of cluster nodes is now {}", nodeStatuses);
    final NodeConnectionStatus status = statusChangeMessage.getNodeConnectionStatus();
    final String summary = summarizeStatusChange(oldStatus, status);
    if (!StringUtils.isEmpty(summary)) {
        addNodeEvent(nodeId, summary);
    }
    // Update our counter so that we are in-sync with the cluster on the
    // most up-to-date version of the NodeConnectionStatus' Update Identifier.
    // We do this so that we can accurately compare status updates that are generated
    // locally against those generated from other nodes in the cluster.
    NodeConnectionStatus.updateIdGenerator(updatedStatus.getUpdateIdentifier());
    if (isActiveClusterCoordinator()) {
        notifyOthersOfNodeStatusChange(statusChangeMessage.getNodeConnectionStatus());
    }
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Aggregations

NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)141 HashMap (java.util.HashMap)72 Map (java.util.Map)71 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)42 Test (org.junit.Test)34 Set (java.util.Set)30 URI (java.net.URI)26 HashSet (java.util.HashSet)26 ArrayList (java.util.ArrayList)24 List (java.util.List)18 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)15 ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)15 NodeConnectionStatus (org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)14 NiFiProperties (org.apache.nifi.util.NiFiProperties)11 Collections (java.util.Collections)10 Pattern (java.util.regex.Pattern)10 NiFiUserDetails (org.apache.nifi.authorization.user.NiFiUserDetails)10 NiFiAuthenticationToken (org.apache.nifi.web.security.token.NiFiAuthenticationToken)10 Authentication (org.springframework.security.core.Authentication)10 Response (javax.ws.rs.core.Response)9