Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/338#discussion_r60086063
  
    --- Diff: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/http/StandardHttpResponseMerger.java
 ---
    @@ -0,0 +1,164 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.nifi.cluster.coordination.http;
    +
    +import java.io.IOException;
    +import java.net.URI;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Set;
    +import java.util.stream.Collectors;
    +
    +import javax.ws.rs.core.StreamingOutput;
    +
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.BulletinBoardEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ComponentStateEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ConnectionStatusEndpiontMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ControllerServiceEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ControllerServiceReferenceEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ControllerServicesEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ControllerStatusEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.CountersEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.DropRequestEndpiontMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.FlowSnippetEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.GroupStatusEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ListFlowFilesEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.PortStatusEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProcessGroupEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProcessorEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProcessorStatusEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProcessorsEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProvenanceEventEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ProvenanceQueryEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.RemoteProcessGroupEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.RemoteProcessGroupStatusEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.RemoteProcessGroupsEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ReportingTaskEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.ReportingTasksEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.StatusHistoryEndpointMerger;
    +import 
org.apache.nifi.cluster.coordination.http.endpoints.SystemDiagnosticsEndpointMerger;
    +import org.apache.nifi.cluster.manager.NodeResponse;
    +import org.apache.nifi.stream.io.NullOutputStream;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +public class StandardHttpResponseMerger implements HttpResponseMerger {
    +    private Logger logger = 
LoggerFactory.getLogger(StandardHttpResponseMerger.class);
    +    private static final List<EndpointResponseMerger> endpointMergers = 
new ArrayList<>();
    +
    +    static {
    +        endpointMergers.add(new ControllerStatusEndpointMerger());
    +        endpointMergers.add(new GroupStatusEndpointMerger());
    +        endpointMergers.add(new ProcessorStatusEndpointMerger());
    +        endpointMergers.add(new ConnectionStatusEndpiontMerger());
    +        endpointMergers.add(new PortStatusEndpointMerger());
    +        endpointMergers.add(new RemoteProcessGroupStatusEndpointMerger());
    +        endpointMergers.add(new ProcessorEndpointMerger());
    +        endpointMergers.add(new ProcessorsEndpointMerger());
    +        endpointMergers.add(new RemoteProcessGroupEndpointMerger());
    +        endpointMergers.add(new RemoteProcessGroupsEndpointMerger());
    +        endpointMergers.add(new ProcessGroupEndpointMerger());
    +        endpointMergers.add(new FlowSnippetEndpointMerger());
    +        endpointMergers.add(new ProvenanceQueryEndpointMerger());
    +        endpointMergers.add(new ProvenanceEventEndpointMerger());
    +        endpointMergers.add(new ControllerServiceEndpointMerger());
    +        endpointMergers.add(new ControllerServicesEndpointMerger());
    +        endpointMergers.add(new 
ControllerServiceReferenceEndpointMerger());
    +        endpointMergers.add(new ReportingTaskEndpointMerger());
    +        endpointMergers.add(new ReportingTasksEndpointMerger());
    +        endpointMergers.add(new DropRequestEndpiontMerger());
    +        endpointMergers.add(new ListFlowFilesEndpointMerger());
    +        endpointMergers.add(new ComponentStateEndpointMerger());
    +        endpointMergers.add(new BulletinBoardEndpointMerger());
    +        endpointMergers.add(new StatusHistoryEndpointMerger());
    +        endpointMergers.add(new SystemDiagnosticsEndpointMerger());
    +        endpointMergers.add(new CountersEndpointMerger());
    +    }
    +
    +    @Override
    +    public NodeResponse mergeResponses(final URI uri, final String 
httpMethod, final Set<NodeResponse> nodeResponses) {
    +        final boolean hasSuccess = hasSuccessfulResponse(nodeResponses);
    +        if (!hasSuccess) {
    +            // It doesn't really matter which response we choose, as all 
are problematic.
    +            final NodeResponse clientResponse = 
nodeResponses.iterator().next();
    +
    +            // Drain the response from all nodes except for the 'chosen 
one'. This ensures that we don't
    +            // leave data lingering on the socket and ensures that we 
don't consume the content of the response
    +            // that we intend to respond with
    +            drainResponses(nodeResponses, clientResponse);
    +            return clientResponse;
    +        }
    +
    +        // Determine which responses are successful
    +        final Set<NodeResponse> successResponses = 
nodeResponses.stream().filter(p -> p.is2xx()).collect(Collectors.toSet());
    +        final Set<NodeResponse> problematicResponses = 
nodeResponses.stream().filter(p -> !p.is2xx()).collect(Collectors.toSet());
    +
    +        // Choose any of the successful responses to be the 'chosen one'.
    +        final NodeResponse clientResponse = 
successResponses.iterator().next();
    +
    +        final EndpointResponseMerger merger = 
getEndpointResponseMerger(uri, httpMethod);
    +        if (merger == null) {
    +            return clientResponse;
    +        }
    +
    +        return merger.merge(uri, httpMethod, successResponses, 
problematicResponses, clientResponse);
    +    }
    +
    +    @Override
    +    public Set<NodeResponse> getProblematicNodeResponses(final 
Set<NodeResponse> allResponses) {
    +        // Check if there are any 2xx responses
    +        final boolean containsSuccessfulResponse = 
hasSuccessfulResponse(allResponses);
    +
    +        if (containsSuccessfulResponse) {
    +            // If there is a 2xx response, we consider a response to be 
problematic if it is not 2xx
    +            return allResponses.stream().filter(p -> 
!p.is2xx()).collect(Collectors.toSet());
    +        } else {
    +            // If no node is successful, we consider a problematic 
response to be only those that are 5xx
    +            return allResponses.stream().filter(p -> 
p.is5xx()).collect(Collectors.toSet());
    +        }
    --- End diff --
    
    One of the benefits of functional programming is the ability to create a 
reference to a function and then reuse it. So the above could be further 
simplified by doing something like this:
    ```
    Predicate<?> predicate = containsSuccessfulResponse ? p -> !p.is2xx() : p 
-> p.is5xx()
    return allResponses.stream().filter(predicate).collect(Collectors.toSet());
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to