Search in sources :

Example 1 with StandardHttpResponseMapper

use of org.apache.nifi.cluster.coordination.http.StandardHttpResponseMapper 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)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Supplier (java.util.function.Supplier)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 CircularFifoQueue (org.apache.commons.collections4.queue.CircularFifoQueue)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)1 FlowElection (org.apache.nifi.cluster.coordination.flow.FlowElection)1 HttpResponseMapper (org.apache.nifi.cluster.coordination.http.HttpResponseMapper)1