Search in sources :

Example 1 with QueueSizeDTO

use of org.apache.nifi.web.api.dto.QueueSizeDTO in project nifi by apache.

the class ListFlowFilesEndpointMerger method mergeResponses.

@Override
protected void mergeResponses(ListingRequestDTO clientDto, Map<NodeIdentifier, ListingRequestDTO> dtoMap, Set<NodeResponse> successfulResponses, Set<NodeResponse> problematicResponses) {
    final Comparator<FlowFileSummaryDTO> comparator = new Comparator<FlowFileSummaryDTO>() {

        @Override
        public int compare(final FlowFileSummaryDTO dto1, final FlowFileSummaryDTO dto2) {
            int positionCompare = dto1.getPosition().compareTo(dto2.getPosition());
            if (positionCompare != 0) {
                return positionCompare;
            }
            final String address1 = dto1.getClusterNodeAddress();
            final String address2 = dto2.getClusterNodeAddress();
            if (address1 == null && address2 == null) {
                return 0;
            }
            if (address1 == null) {
                return 1;
            }
            if (address2 == null) {
                return -1;
            }
            return address1.compareTo(address2);
        }
    };
    final NavigableSet<FlowFileSummaryDTO> flowFileSummaries = new TreeSet<>(comparator);
    ListFlowFileState state = null;
    int numStepsCompleted = 0;
    int numStepsTotal = 0;
    int objectCount = 0;
    long byteCount = 0;
    boolean finished = true;
    for (final Map.Entry<NodeIdentifier, ListingRequestDTO> entry : dtoMap.entrySet()) {
        final NodeIdentifier nodeIdentifier = entry.getKey();
        final String nodeAddress = nodeIdentifier.getApiAddress() + ":" + nodeIdentifier.getApiPort();
        final ListingRequestDTO nodeRequest = entry.getValue();
        numStepsTotal++;
        if (Boolean.TRUE.equals(nodeRequest.getFinished())) {
            numStepsCompleted++;
        }
        final QueueSizeDTO nodeQueueSize = nodeRequest.getQueueSize();
        objectCount += nodeQueueSize.getObjectCount();
        byteCount += nodeQueueSize.getByteCount();
        if (!nodeRequest.getFinished()) {
            finished = false;
        }
        if (nodeRequest.getLastUpdated().after(clientDto.getLastUpdated())) {
            clientDto.setLastUpdated(nodeRequest.getLastUpdated());
        }
        // Keep the state with the lowest ordinal value (the "least completed").
        final ListFlowFileState nodeState = ListFlowFileState.valueOfDescription(nodeRequest.getState());
        if (state == null || state.compareTo(nodeState) > 0) {
            state = nodeState;
        }
        if (nodeRequest.getFlowFileSummaries() != null) {
            for (final FlowFileSummaryDTO summaryDTO : nodeRequest.getFlowFileSummaries()) {
                if (summaryDTO.getClusterNodeId() == null || summaryDTO.getClusterNodeAddress() == null) {
                    summaryDTO.setClusterNodeId(nodeIdentifier.getId());
                    summaryDTO.setClusterNodeAddress(nodeAddress);
                }
                flowFileSummaries.add(summaryDTO);
                // Keep the set from growing beyond our max
                if (flowFileSummaries.size() > clientDto.getMaxResults()) {
                    flowFileSummaries.pollLast();
                }
            }
        }
        if (nodeRequest.getFailureReason() != null) {
            clientDto.setFailureReason(nodeRequest.getFailureReason());
        }
    }
    final List<FlowFileSummaryDTO> summaryDTOs = new ArrayList<>(flowFileSummaries);
    clientDto.setFlowFileSummaries(summaryDTOs);
    // depends on invariant if numStepsTotal is 0, so is numStepsCompleted, all steps being completed
    // would be 1
    final int percentCompleted = (numStepsTotal == 0) ? 1 : numStepsCompleted / numStepsTotal;
    clientDto.setPercentCompleted(percentCompleted);
    clientDto.setFinished(finished);
    clientDto.getQueueSize().setByteCount(byteCount);
    clientDto.getQueueSize().setObjectCount(objectCount);
}
Also used : ListingRequestDTO(org.apache.nifi.web.api.dto.ListingRequestDTO) QueueSizeDTO(org.apache.nifi.web.api.dto.QueueSizeDTO) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) ListFlowFileState(org.apache.nifi.controller.queue.ListFlowFileState) TreeSet(java.util.TreeSet) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Map(java.util.Map) FlowFileSummaryDTO(org.apache.nifi.web.api.dto.FlowFileSummaryDTO)

Aggregations

ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)1 ListFlowFileState (org.apache.nifi.controller.queue.ListFlowFileState)1 FlowFileSummaryDTO (org.apache.nifi.web.api.dto.FlowFileSummaryDTO)1 ListingRequestDTO (org.apache.nifi.web.api.dto.ListingRequestDTO)1 QueueSizeDTO (org.apache.nifi.web.api.dto.QueueSizeDTO)1