nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegz <...@git.apache.org>
Subject [GitHub] nifi pull request: NIFI-1727: Refactored logic for merging HTTP Re...
Date Sun, 17 Apr 2016 18:06:22 GMT
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/338#discussion_r59989402
  
    --- Diff: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/http/endpoints/ControllerServiceEndpointMerger.java
---
    @@ -0,0 +1,145 @@
    +/*
    + * 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.endpoints;
    +
    +import java.net.URI;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.regex.Pattern;
    +
    +import org.apache.nifi.cluster.manager.NodeResponse;
    +import org.apache.nifi.cluster.protocol.NodeIdentifier;
    +import org.apache.nifi.controller.service.ControllerServiceState;
    +import org.apache.nifi.web.api.dto.ControllerServiceDTO;
    +import org.apache.nifi.web.api.dto.ControllerServiceReferencingComponentDTO;
    +import org.apache.nifi.web.api.entity.ControllerServiceEntity;
    +
    +public class ControllerServiceEndpointMerger extends AbstractSingleEntityEndpoint<ControllerServiceEntity,
ControllerServiceDTO> {
    +    public static final String CONTROLLER_SERVICES_URI = "/nifi-api/controller/controller-services/node";
    +    public static final Pattern CONTROLLER_SERVICE_URI_PATTERN = Pattern.compile("/nifi-api/controller/controller-services/node/[a-f0-9\\-]{36}");
    +
    +    @Override
    +    public boolean canHandle(URI uri, String method) {
    +        if (("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method)) &&
CONTROLLER_SERVICE_URI_PATTERN.matcher(uri.getPath()).matches()) {
    +            return true;
    +        } else if ("POST".equalsIgnoreCase(method) && CONTROLLER_SERVICES_URI.equals(uri.getPath()))
{
    +            return true;
    +        }
    +
    +        return false;
    +    }
    +
    +    @Override
    +    protected Class<ControllerServiceEntity> getEntityClass() {
    +        return ControllerServiceEntity.class;
    +    }
    +
    +    @Override
    +    protected ControllerServiceDTO getDto(ControllerServiceEntity entity) {
    +        return entity.getControllerService();
    +    }
    +
    +    @Override
    +    protected void mergeResponses(ControllerServiceDTO clientDto, Map<NodeIdentifier,
ControllerServiceDTO> dtoMap, Set<NodeResponse> successfulResponses, Set<NodeResponse>
problematicResponses) {
    +        final Map<String, Set<NodeIdentifier>> validationErrorMap = new HashMap<>();
    +        final Set<ControllerServiceReferencingComponentDTO> referencingComponents
= clientDto.getReferencingComponents();
    +        final Map<NodeIdentifier, Set<ControllerServiceReferencingComponentDTO>>
nodeReferencingComponentsMap = new HashMap<>();
    +
    +        String state = null;
    +        for (final Map.Entry<NodeIdentifier, ControllerServiceDTO> nodeEntry :
dtoMap.entrySet()) {
    +            final NodeIdentifier nodeId = nodeEntry.getKey();
    +            final ControllerServiceDTO nodeControllerService = nodeEntry.getValue();
    +
    +            if (state == null) {
    +                if (ControllerServiceState.DISABLING.name().equals(nodeControllerService.getState()))
{
    +                    state = ControllerServiceState.DISABLING.name();
    +                } else if (ControllerServiceState.ENABLING.name().equals(nodeControllerService.getState()))
{
    +                    state = ControllerServiceState.ENABLING.name();
    +                }
    +            }
    +
    +            for (final ControllerServiceReferencingComponentDTO nodeReferencingComponents
: nodeControllerService.getReferencingComponents()) {
    +                nodeReferencingComponentsMap.put(nodeId, nodeReferencingComponents.getReferencingComponents());
    +            }
    +
    +            // merge the validation errors
    +            mergeValidationErrors(validationErrorMap, nodeId, nodeControllerService.getValidationErrors());
    +        }
    +
    +        // merge the referencing components
    +        mergeControllerServiceReferences(referencingComponents, nodeReferencingComponentsMap);
    +
    +        // store the 'transition' state is applicable
    +        if (state != null) {
    +            clientDto.setState(state);
    +        }
    +
    +        // set the merged the validation errors
    +        clientDto.setValidationErrors(normalizedMergedValidationErrors(validationErrorMap,
dtoMap.size()));
    +    }
    +
    +    public static void mergeControllerServiceReferences(Set<ControllerServiceReferencingComponentDTO>
referencingComponents,
    +        Map<NodeIdentifier, Set<ControllerServiceReferencingComponentDTO>>
referencingComponentMap) {
    +
    +        final Map<String, Integer> activeThreadCounts = new HashMap<>();
    +        final Map<String, String> states = new HashMap<>();
    +        for (final Map.Entry<NodeIdentifier, Set<ControllerServiceReferencingComponentDTO>>
nodeEntry : referencingComponentMap.entrySet()) {
    +            final Set<ControllerServiceReferencingComponentDTO> nodeReferencingComponents
= nodeEntry.getValue();
    +
    +            // go through all the nodes referencing components
    +            if (nodeReferencingComponents != null) {
    +                for (final ControllerServiceReferencingComponentDTO nodeReferencingComponent
: nodeReferencingComponents) {
    +                    // handle active thread counts
    +                    if (nodeReferencingComponent.getActiveThreadCount() != null &&
nodeReferencingComponent.getActiveThreadCount() > 0) {
    +                        final Integer current = activeThreadCounts.get(nodeReferencingComponent.getId());
    +                        if (current == null) {
    +                            activeThreadCounts.put(nodeReferencingComponent.getId(),
nodeReferencingComponent.getActiveThreadCount());
    +                        } else {
    +                            activeThreadCounts.put(nodeReferencingComponent.getId(),
nodeReferencingComponent.getActiveThreadCount() + current);
    +                        }
    +                    }
    +
    +                    // handle controller service state
    +                    final String state = states.get(nodeReferencingComponent.getId());
    +                    if (state == null) {
    +                        if (ControllerServiceState.DISABLING.name().equals(nodeReferencingComponent.getState()))
{
    +                            states.put(nodeReferencingComponent.getId(), ControllerServiceState.DISABLING.name());
    +                        } else if (ControllerServiceState.ENABLING.name().equals(nodeReferencingComponent.getState()))
{
    +                            states.put(nodeReferencingComponent.getId(), ControllerServiceState.ENABLING.name());
    +                        }
    --- End diff --
    
    Same comment as before about comparing String and ControllerServiceState


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message