Search in sources :

Example 1 with NodeIdentifier

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

the class StandardNiFiServiceFacade method deleteNode.

@Override
public void deleteNode(final String nodeId) {
    final NiFiUser user = NiFiUserUtils.getNiFiUser();
    if (user == null) {
        throw new WebApplicationException(new Throwable("Unable to access details for current user."));
    }
    final String userDn = user.getIdentity();
    final NodeIdentifier nodeIdentifier = clusterCoordinator.getNodeIdentifier(nodeId);
    if (nodeIdentifier == null) {
        throw new UnknownNodeException("Cannot remove Node with ID " + nodeId + " because it is not part of the cluster");
    }
    final NodeConnectionStatus nodeConnectionStatus = clusterCoordinator.getConnectionStatus(nodeIdentifier);
    if (!nodeConnectionStatus.getState().equals(NodeConnectionState.DISCONNECTED)) {
        throw new IllegalNodeDeletionException("Cannot remove Node with ID " + nodeId + " because it is not disconnected, current state = " + nodeConnectionStatus.getState());
    }
    clusterCoordinator.removeNode(nodeIdentifier, userDn);
    heartbeatMonitor.removeHeartbeat(nodeIdentifier);
}
Also used : NiFiUser(org.apache.nifi.authorization.user.NiFiUser) WebApplicationException(javax.ws.rs.WebApplicationException) UnknownNodeException(org.apache.nifi.cluster.manager.exception.UnknownNodeException) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) IllegalNodeDeletionException(org.apache.nifi.cluster.manager.exception.IllegalNodeDeletionException)

Example 2 with NodeIdentifier

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

the class StandardNiFiServiceFacade method updateNode.

@Override
public NodeDTO updateNode(final NodeDTO nodeDTO) {
    final NiFiUser user = NiFiUserUtils.getNiFiUser();
    if (user == null) {
        throw new WebApplicationException(new Throwable("Unable to access details for current user."));
    }
    final String userDn = user.getIdentity();
    final NodeIdentifier nodeId = clusterCoordinator.getNodeIdentifier(nodeDTO.getNodeId());
    if (nodeId == null) {
        throw new UnknownNodeException("No node exists with ID " + nodeDTO.getNodeId());
    }
    if (NodeConnectionState.CONNECTING.name().equalsIgnoreCase(nodeDTO.getStatus())) {
        clusterCoordinator.requestNodeConnect(nodeId, userDn);
    } else if (NodeConnectionState.DISCONNECTING.name().equalsIgnoreCase(nodeDTO.getStatus())) {
        clusterCoordinator.requestNodeDisconnect(nodeId, DisconnectionCode.USER_DISCONNECTED, "User " + userDn + " requested that node be disconnected from cluster");
    }
    return getNode(nodeId);
}
Also used : NiFiUser(org.apache.nifi.authorization.user.NiFiUser) WebApplicationException(javax.ws.rs.WebApplicationException) UnknownNodeException(org.apache.nifi.cluster.manager.exception.UnknownNodeException) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 3 with NodeIdentifier

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

the class SiteToSiteResource method getPeers.

/**
 * Returns the available Peers and its status of this NiFi.
 *
 * @return A peersEntity.
 */
@GET
@Path("/peers")
@Consumes(MediaType.WILDCARD)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ApiOperation(value = "Returns the available Peers and its status of this NiFi", response = PeersEntity.class, authorizations = { @Authorization(value = "Read - /site-to-site") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getPeers(@Context HttpServletRequest req) {
    authorizeSiteToSite();
    if (!properties.isSiteToSiteHttpEnabled()) {
        return responseCreator.httpSiteToSiteIsNotEnabledResponse();
    }
    final Integer transportProtocolVersion;
    try {
        transportProtocolVersion = negotiateTransportProtocolVersion(req, transportProtocolVersionNegotiator);
    } catch (BadRequestException e) {
        return responseCreator.badRequestResponse(e);
    }
    final List<PeerDTO> peers = new ArrayList<>();
    if (properties.isNode()) {
        try {
            final Map<NodeIdentifier, NodeWorkload> clusterWorkload = clusterCoordinator.getClusterWorkload();
            clusterWorkload.entrySet().stream().forEach(entry -> {
                final PeerDTO peer = new PeerDTO();
                final NodeIdentifier nodeId = entry.getKey();
                final String siteToSiteAddress = nodeId.getSiteToSiteAddress();
                peer.setHostname(siteToSiteAddress == null ? nodeId.getApiAddress() : siteToSiteAddress);
                peer.setPort(nodeId.getSiteToSiteHttpApiPort() == null ? nodeId.getApiPort() : nodeId.getSiteToSiteHttpApiPort());
                peer.setSecure(nodeId.isSiteToSiteSecure());
                peer.setFlowFileCount(entry.getValue().getFlowFileCount());
                peers.add(peer);
            });
        } catch (IOException e) {
            throw new RuntimeException("Failed to retrieve cluster workload due to " + e, e);
        }
    } else {
        // Standalone mode.
        final PeerDTO peer = new PeerDTO();
        // Private IP address or hostname may not be accessible from client in some environments.
        // So, use the value defined in nifi.properties instead when it is defined.
        final String remoteInputHost = properties.getRemoteInputHost();
        String localName;
        try {
            // Get local host name using InetAddress if available, same as RAW socket does.
            localName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to get local host name using InetAddress.", e);
            }
            localName = req.getLocalName();
        }
        peer.setHostname(isEmpty(remoteInputHost) ? localName : remoteInputHost);
        peer.setPort(properties.getRemoteInputHttpPort());
        peer.setSecure(properties.isSiteToSiteSecure());
        // doesn't matter how many FlowFiles we have, because we're the only host.
        peer.setFlowFileCount(0);
        peers.add(peer);
    }
    final PeersEntity entity = new PeersEntity();
    entity.setPeers(peers);
    return noCache(setCommonHeaders(Response.ok(entity), transportProtocolVersion, transactionManager)).build();
}
Also used : UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) IOException(java.io.IOException) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) PeersEntity(org.apache.nifi.web.api.entity.PeersEntity) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) BadRequestException(org.apache.nifi.remote.exception.BadRequestException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with NodeIdentifier

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

the class ApplicationResource method replicateToCoordinator.

protected Response replicateToCoordinator(final String method, final Object entity) {
    ensureFlowInitialized();
    try {
        final NodeIdentifier coordinatorNode = getClusterCoordinatorNode();
        final Set<NodeIdentifier> coordinatorNodes = Collections.singleton(coordinatorNode);
        return getRequestReplicator().replicate(coordinatorNodes, method, getAbsolutePath(), entity, getHeaders(), true, false).awaitMergedResponse().getResponse();
    } catch (final InterruptedException ie) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Request to " + method + " " + getAbsolutePath() + " was interrupted").type("text/plain").build();
    }
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

Example 5 with NodeIdentifier

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

the class ApplicationResource method replicate.

/**
 * Replicates the request to the given node
 *
 * @param method   the HTTP method
 * @param entity   the Entity to replicate
 * @param nodeUuid the UUID of the node to replicate the request to
 * @return the response from the node
 * @throws UnknownNodeException if the nodeUuid given does not map to any node in the cluster
 */
protected Response replicate(final URI path, final String method, final Object entity, final String nodeUuid, final Map<String, String> headersToOverride) {
    // since we're cluster we must specify the cluster node identifier
    if (nodeUuid == null) {
        throw new IllegalArgumentException("The cluster node identifier must be specified.");
    }
    final NodeIdentifier nodeId = clusterCoordinator.getNodeIdentifier(nodeUuid);
    if (nodeId == null) {
        throw new UnknownNodeException("Cannot replicate request " + method + " " + getAbsolutePath() + " to node with ID " + nodeUuid + " because the specified node does not exist.");
    }
    ensureFlowInitialized();
    try {
        final Map<String, String> headers = headersToOverride == null ? getHeaders() : getHeaders(headersToOverride);
        // and have it replicate the request on our behalf.
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            // If we are to replicate directly to the nodes, we need to indicate that the replication source is
            // the cluster coordinator so that the node knows to service the request.
            final Set<NodeIdentifier> targetNodes = Collections.singleton(nodeId);
            return requestReplicator.replicate(targetNodes, method, path, entity, headers, true, true).awaitMergedResponse().getResponse();
        } else {
            headers.put(RequestReplicator.REPLICATION_TARGET_NODE_UUID_HEADER, nodeId.getId());
            return requestReplicator.forwardToCoordinator(getClusterCoordinatorNode(), method, path, entity, headers).awaitMergedResponse().getResponse();
        }
    } catch (final InterruptedException ie) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Request to " + method + " " + path + " was interrupted").type("text/plain").build();
    }
}
Also used : UnknownNodeException(org.apache.nifi.cluster.manager.exception.UnknownNodeException) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier)

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