Search in sources :

Example 16 with NodeIdentifier

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

the class NodeClusterCoordinator method shutdown.

@Override
public void shutdown() {
    if (closed) {
        return;
    }
    closed = true;
    final NodeIdentifier localId = getLocalNodeIdentifier();
    if (localId != null) {
        final NodeConnectionStatus shutdownStatus = new NodeConnectionStatus(localId, DisconnectionCode.NODE_SHUTDOWN);
        updateNodeStatus(shutdownStatus, false);
        logger.info("Successfully notified other nodes that I am shutting down");
    }
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 17 with NodeIdentifier

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

the class NodeClusterCoordinator method handleNodeConnectionStatusRequest.

private NodeConnectionStatusResponseMessage handleNodeConnectionStatusRequest() {
    final NodeConnectionStatusResponseMessage msg = new NodeConnectionStatusResponseMessage();
    final NodeIdentifier self = getLocalNodeIdentifier();
    if (self != null) {
        final NodeConnectionStatus connectionStatus = nodeStatuses.get(self);
        msg.setNodeConnectionStatus(connectionStatus);
    }
    return msg;
}
Also used : NodeConnectionStatusResponseMessage(org.apache.nifi.cluster.protocol.message.NodeConnectionStatusResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 18 with NodeIdentifier

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

the class NodeClusterCoordinator method notifyOthersOfNodeStatusChange.

/**
 * Notifies other nodes that the status of a node changed
 *
 * @param updatedStatus the updated status for a node in the cluster
 * @param notifyAllNodes if <code>true</code> will notify all nodes. If
 * <code>false</code>, will notify only the cluster coordinator
 */
void notifyOthersOfNodeStatusChange(final NodeConnectionStatus updatedStatus, final boolean notifyAllNodes, final boolean waitForCoordinator) {
    // If this node is the active cluster coordinator, then we are going to replicate to all nodes.
    // Otherwise, get the active coordinator (or wait for one to become active) and then notify the coordinator.
    final Set<NodeIdentifier> nodesToNotify;
    if (notifyAllNodes) {
        nodesToNotify = getNodeIdentifiers(NodeConnectionState.CONNECTED, NodeConnectionState.CONNECTING);
        // Do not notify ourselves because we already know about the status update.
        nodesToNotify.remove(getLocalNodeIdentifier());
    } else if (waitForCoordinator) {
        nodesToNotify = Collections.singleton(waitForElectedClusterCoordinator());
    } else {
        final NodeIdentifier nodeId = getElectedActiveCoordinatorNode();
        if (nodeId == null) {
            return;
        }
        nodesToNotify = Collections.singleton(nodeId);
    }
    final NodeStatusChangeMessage message = new NodeStatusChangeMessage();
    message.setNodeId(updatedStatus.getNodeIdentifier());
    message.setNodeConnectionStatus(updatedStatus);
    senderListener.notifyNodeStatusChange(nodesToNotify, message);
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeStatusChangeMessage(org.apache.nifi.cluster.protocol.message.NodeStatusChangeMessage)

Example 19 with NodeIdentifier

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

the class NodeClusterCoordinator method requestNodeDisconnect.

@Override
public void requestNodeDisconnect(final NodeIdentifier nodeId, final DisconnectionCode disconnectionCode, final String explanation) {
    final Set<NodeIdentifier> connectedNodeIds = getNodeIdentifiers(NodeConnectionState.CONNECTED);
    if (connectedNodeIds.size() == 1 && connectedNodeIds.contains(nodeId)) {
        throw new IllegalNodeDisconnectionException("Cannot disconnect node " + nodeId + " because it is the only node currently connected");
    }
    logger.info("Requesting that {} disconnect due to {}", nodeId, explanation == null ? disconnectionCode : explanation);
    updateNodeStatus(new NodeConnectionStatus(nodeId, disconnectionCode, explanation));
    // shutdown, as we will not be able to connect to the node anyway.
    if (disconnectionCode == DisconnectionCode.NODE_SHUTDOWN) {
        return;
    }
    final DisconnectMessage request = new DisconnectMessage();
    request.setNodeId(nodeId);
    request.setExplanation(explanation);
    addNodeEvent(nodeId, "Disconnection requested due to " + explanation);
    disconnectAsynchronously(request, 10, 5);
}
Also used : DisconnectMessage(org.apache.nifi.cluster.protocol.message.DisconnectMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) IllegalNodeDisconnectionException(org.apache.nifi.cluster.manager.exception.IllegalNodeDisconnectionException)

Example 20 with NodeIdentifier

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

the class AffectedComponentEntityMerger method mergeAffectedComponents.

public void mergeAffectedComponents(final Set<AffectedComponentEntity> affectedComponents, final Map<NodeIdentifier, Set<AffectedComponentEntity>> affectedComponentMap) {
    final Map<String, Integer> activeThreadCounts = new HashMap<>();
    final Map<String, String> states = new HashMap<>();
    final Map<String, PermissionsDTO> canReads = new HashMap<>();
    for (final Map.Entry<NodeIdentifier, Set<AffectedComponentEntity>> nodeEntry : affectedComponentMap.entrySet()) {
        final Set<AffectedComponentEntity> nodeAffectedComponents = nodeEntry.getValue();
        // go through all the nodes referencing components
        if (nodeAffectedComponents != null) {
            for (final AffectedComponentEntity nodeAffectedComponentEntity : nodeAffectedComponents) {
                final AffectedComponentDTO nodeAffectedComponent = nodeAffectedComponentEntity.getComponent();
                if (nodeAffectedComponentEntity.getPermissions().getCanRead()) {
                    // handle active thread counts
                    if (nodeAffectedComponent.getActiveThreadCount() != null && nodeAffectedComponent.getActiveThreadCount() > 0) {
                        final Integer current = activeThreadCounts.get(nodeAffectedComponent.getId());
                        if (current == null) {
                            activeThreadCounts.put(nodeAffectedComponent.getId(), nodeAffectedComponent.getActiveThreadCount());
                        } else {
                            activeThreadCounts.put(nodeAffectedComponent.getId(), nodeAffectedComponent.getActiveThreadCount() + current);
                        }
                    }
                    // handle controller service state
                    final String state = states.get(nodeAffectedComponent.getId());
                    if (state == null) {
                        if (ControllerServiceState.DISABLING.name().equals(nodeAffectedComponent.getState())) {
                            states.put(nodeAffectedComponent.getId(), ControllerServiceState.DISABLING.name());
                        } else if (ControllerServiceState.ENABLING.name().equals(nodeAffectedComponent.getState())) {
                            states.put(nodeAffectedComponent.getId(), ControllerServiceState.ENABLING.name());
                        }
                    }
                }
                // handle read permissions
                final PermissionsDTO mergedPermissions = canReads.get(nodeAffectedComponentEntity.getId());
                final PermissionsDTO permissions = nodeAffectedComponentEntity.getPermissions();
                if (permissions != null) {
                    if (mergedPermissions == null) {
                        canReads.put(nodeAffectedComponentEntity.getId(), permissions);
                    } else {
                        PermissionsDtoMerger.mergePermissions(mergedPermissions, permissions);
                    }
                }
            }
        }
    }
    // go through each affected components
    if (affectedComponents != null) {
        for (final AffectedComponentEntity affectedComponent : affectedComponents) {
            final PermissionsDTO permissions = canReads.get(affectedComponent.getId());
            if (permissions != null && permissions.getCanRead() != null && permissions.getCanRead()) {
                final Integer activeThreadCount = activeThreadCounts.get(affectedComponent.getId());
                if (activeThreadCount != null) {
                    affectedComponent.getComponent().setActiveThreadCount(activeThreadCount);
                }
                final String state = states.get(affectedComponent.getId());
                if (state != null) {
                    affectedComponent.getComponent().setState(state);
                }
            } else {
                affectedComponent.setPermissions(permissions);
                affectedComponent.setComponent(null);
            }
        }
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) PermissionsDTO(org.apache.nifi.web.api.dto.PermissionsDTO) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) Map(java.util.Map) HashMap(java.util.HashMap) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity)

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