Search in sources :

Example 6 with ClusterCoordinator

use of org.apache.nifi.cluster.coordination.ClusterCoordinator in project nifi by apache.

the class ThreadPoolRequestReplicatorFactoryBean method getObject.

@Override
public ThreadPoolRequestReplicator getObject() throws Exception {
    if (replicator == null && nifiProperties.isNode()) {
        final EventReporter eventReporter = applicationContext.getBean("eventReporter", EventReporter.class);
        final ClusterCoordinator clusterCoordinator = applicationContext.getBean("clusterCoordinator", ClusterCoordinator.class);
        final RequestCompletionCallback requestCompletionCallback = applicationContext.getBean("clusterCoordinator", RequestCompletionCallback.class);
        final int corePoolSize = nifiProperties.getClusterNodeProtocolCorePoolSize();
        final int maxPoolSize = nifiProperties.getClusterNodeProtocolMaxPoolSize();
        final int maxConcurrentRequests = nifiProperties.getClusterNodeMaxConcurrentRequests();
        final Client jerseyClient = WebUtils.createClient(null, SslContextFactory.createSslContext(nifiProperties));
        final String connectionTimeout = nifiProperties.getClusterNodeConnectionTimeout();
        final String readTimeout = nifiProperties.getClusterNodeReadTimeout();
        replicator = new ThreadPoolRequestReplicator(corePoolSize, maxPoolSize, maxConcurrentRequests, jerseyClient, clusterCoordinator, connectionTimeout, readTimeout, requestCompletionCallback, eventReporter, nifiProperties);
    }
    return replicator;
}
Also used : ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) Client(javax.ws.rs.client.Client) ThreadPoolRequestReplicator(org.apache.nifi.cluster.coordination.http.replication.ThreadPoolRequestReplicator) RequestCompletionCallback(org.apache.nifi.cluster.coordination.http.replication.RequestCompletionCallback) EventReporter(org.apache.nifi.events.EventReporter)

Example 7 with ClusterCoordinator

use of org.apache.nifi.cluster.coordination.ClusterCoordinator in project nifi by apache.

the class TestThreadPoolRequestReplicator method testMutableRequestRequiresAllNodesConnected.

@Test
public void testMutableRequestRequiresAllNodesConnected() throws URISyntaxException {
    final ClusterCoordinator coordinator = createClusterCoordinator();
    // build a map of connection state to node ids
    final Map<NodeConnectionState, List<NodeIdentifier>> nodeMap = new HashMap<>();
    final List<NodeIdentifier> connectedNodes = new ArrayList<>();
    connectedNodes.add(new NodeIdentifier("1", "localhost", 8100, "localhost", 8101, "localhost", 8102, 8103, false));
    connectedNodes.add(new NodeIdentifier("2", "localhost", 8200, "localhost", 8201, "localhost", 8202, 8203, false));
    nodeMap.put(NodeConnectionState.CONNECTED, connectedNodes);
    final List<NodeIdentifier> otherState = new ArrayList<>();
    otherState.add(new NodeIdentifier("3", "localhost", 8300, "localhost", 8301, "localhost", 8302, 8303, false));
    nodeMap.put(NodeConnectionState.CONNECTING, otherState);
    when(coordinator.getConnectionStates()).thenReturn(nodeMap);
    final NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    final ThreadPoolRequestReplicator replicator = new ThreadPoolRequestReplicator(2, 5, 100, ClientBuilder.newClient(), coordinator, "1 sec", "1 sec", null, null, props) {

        @Override
        public AsyncClusterResponse replicate(Set<NodeIdentifier> nodeIds, String method, URI uri, Object entity, Map<String, String> headers, boolean indicateReplicated, boolean verify) {
            return null;
        }
    };
    try {
        // set the user
        final Authentication authentication = new NiFiAuthenticationToken(new NiFiUserDetails(StandardNiFiUser.ANONYMOUS));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        try {
            replicator.replicate(HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>());
            Assert.fail("Expected ConnectingNodeMutableRequestException");
        } catch (final ConnectingNodeMutableRequestException e) {
        // expected behavior
        }
        nodeMap.remove(NodeConnectionState.CONNECTING);
        nodeMap.put(NodeConnectionState.DISCONNECTED, otherState);
        try {
            replicator.replicate(HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>());
            Assert.fail("Expected DisconnectedNodeMutableRequestException");
        } catch (final DisconnectedNodeMutableRequestException e) {
        // expected behavior
        }
        nodeMap.remove(NodeConnectionState.DISCONNECTED);
        nodeMap.put(NodeConnectionState.DISCONNECTING, otherState);
        try {
            replicator.replicate(HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>());
            Assert.fail("Expected DisconnectedNodeMutableRequestException");
        } catch (final DisconnectedNodeMutableRequestException e) {
        // expected behavior
        }
        // should not throw an Exception because it's a GET
        replicator.replicate(HttpMethod.GET, new URI("http://localhost:80/processors/1"), new MultiValueMap<>(), new HashMap<>());
        // should not throw an Exception because all nodes are now connected
        nodeMap.remove(NodeConnectionState.DISCONNECTING);
        replicator.replicate(HttpMethod.POST, new URI("http://localhost:80/processors/1"), new ProcessorEntity(), new HashMap<>());
    } finally {
        replicator.shutdown();
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ConnectingNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException) ArrayList(java.util.ArrayList) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) NiFiAuthenticationToken(org.apache.nifi.web.security.token.NiFiAuthenticationToken) Authentication(org.springframework.security.core.Authentication) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) List(java.util.List) ArrayList(java.util.ArrayList) DisconnectedNodeMutableRequestException(org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.apache.commons.collections4.map.MultiValueMap) NiFiUserDetails(org.apache.nifi.authorization.user.NiFiUserDetails) Test(org.junit.Test)

Example 8 with ClusterCoordinator

use of org.apache.nifi.cluster.coordination.ClusterCoordinator in project nifi by apache.

the class TestThreadPoolRequestReplicator method createClusterCoordinator.

private ClusterCoordinator createClusterCoordinator() {
    final ClusterCoordinator coordinator = mock(ClusterCoordinator.class);
    when(coordinator.getConnectionStatus(Mockito.any(NodeIdentifier.class))).thenAnswer(new Answer<NodeConnectionStatus>() {

        @Override
        public NodeConnectionStatus answer(InvocationOnMock invocation) throws Throwable {
            return new NodeConnectionStatus(invocation.getArgumentAt(0, NodeIdentifier.class), NodeConnectionState.CONNECTED);
        }
    });
    return coordinator;
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus)

Example 9 with ClusterCoordinator

use of org.apache.nifi.cluster.coordination.ClusterCoordinator in project nifi by apache.

the class StandardFlowServiceFactoryBean method getObject.

@Override
public Object getObject() throws Exception {
    if (flowService == null) {
        final FlowController flowController = applicationContext.getBean("flowController", FlowController.class);
        final RevisionManager revisionManager = applicationContext.getBean("revisionManager", RevisionManager.class);
        if (properties.isNode()) {
            final NodeProtocolSenderListener nodeProtocolSenderListener = applicationContext.getBean("nodeProtocolSenderListener", NodeProtocolSenderListener.class);
            final ClusterCoordinator clusterCoordinator = applicationContext.getBean("clusterCoordinator", ClusterCoordinator.class);
            flowService = StandardFlowService.createClusteredInstance(flowController, properties, nodeProtocolSenderListener, clusterCoordinator, encryptor, revisionManager, authorizer);
        } else {
            flowService = StandardFlowService.createStandaloneInstance(flowController, properties, encryptor, revisionManager, authorizer);
        }
    }
    return flowService;
}
Also used : RevisionManager(org.apache.nifi.web.revision.RevisionManager) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) FlowController(org.apache.nifi.controller.FlowController) NodeProtocolSenderListener(org.apache.nifi.cluster.protocol.impl.NodeProtocolSenderListener)

Example 10 with ClusterCoordinator

use of org.apache.nifi.cluster.coordination.ClusterCoordinator in project nifi by apache.

the class FlowResource method getClusterSummary.

/**
 * Retrieves the cluster summary for this NiFi.
 *
 * @return A clusterSummaryEntity.
 * @throws InterruptedException if interrupted
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("cluster/summary")
@ApiOperation(value = "The cluster summary for this NiFi", response = ClusteSummaryEntity.class, authorizations = { @Authorization(value = "Read - /flow") })
@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 getClusterSummary() throws InterruptedException {
    authorizeFlow();
    final ClusterSummaryDTO clusterConfiguration = new ClusterSummaryDTO();
    final ClusterCoordinator clusterCoordinator = getClusterCoordinator();
    if (clusterCoordinator != null && clusterCoordinator.isConnected()) {
        final Map<NodeConnectionState, List<NodeIdentifier>> stateMap = clusterCoordinator.getConnectionStates();
        int totalNodeCount = 0;
        for (final List<NodeIdentifier> nodeList : stateMap.values()) {
            totalNodeCount += nodeList.size();
        }
        final List<NodeIdentifier> connectedNodeIds = stateMap.get(NodeConnectionState.CONNECTED);
        final int connectedNodeCount = (connectedNodeIds == null) ? 0 : connectedNodeIds.size();
        clusterConfiguration.setConnectedNodeCount(connectedNodeCount);
        clusterConfiguration.setTotalNodeCount(totalNodeCount);
        clusterConfiguration.setConnectedNodes(connectedNodeCount + " / " + totalNodeCount);
    }
    clusterConfiguration.setClustered(isClustered());
    clusterConfiguration.setConnectedToCluster(isConnectedToCluster());
    // create the response entity
    final ClusteSummaryEntity entity = new ClusteSummaryEntity();
    entity.setClusterSummary(clusterConfiguration);
    // generate the response
    return generateOkResponse(entity).build();
}
Also used : ClusteSummaryEntity(org.apache.nifi.web.api.entity.ClusteSummaryEntity) ClusterSummaryDTO(org.apache.nifi.web.api.dto.ClusterSummaryDTO) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) ArrayList(java.util.ArrayList) List(java.util.List) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) 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)

Aggregations

ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)12 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)9 URI (java.net.URI)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 Response (javax.ws.rs.core.Response)4 MultiValueMap (org.apache.commons.collections4.map.MultiValueMap)4 ConnectingNodeMutableRequestException (org.apache.nifi.cluster.manager.exception.ConnectingNodeMutableRequestException)4 DisconnectedNodeMutableRequestException (org.apache.nifi.cluster.manager.exception.DisconnectedNodeMutableRequestException)4 NiFiProperties (org.apache.nifi.util.NiFiProperties)4 Test (org.junit.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 SocketTimeoutException (java.net.SocketTimeoutException)3 URISyntaxException (java.net.URISyntaxException)3 HashSet (java.util.HashSet)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 ProcessingException (javax.ws.rs.ProcessingException)3 Produces (javax.ws.rs.Produces)3