Search in sources :

Example 96 with NodeIdentifier

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

the class UserEntityMerger method mergeComponents.

/**
 * Merges the UserEntity responses.
 *
 * @param clientEntity the entity being returned to the client
 * @param entityMap all node responses
 */
public void mergeComponents(final UserEntity clientEntity, final Map<NodeIdentifier, UserEntity> entityMap) {
    final UserDTO clientDto = clientEntity.getComponent();
    final Map<NodeIdentifier, UserDTO> dtoMap = new HashMap<>();
    for (final Map.Entry<NodeIdentifier, UserEntity> entry : entityMap.entrySet()) {
        final UserEntity nodeUserEntity = entry.getValue();
        final UserDTO nodeUserDto = nodeUserEntity.getComponent();
        dtoMap.put(entry.getKey(), nodeUserDto);
    }
    mergeDtos(clientDto, dtoMap);
}
Also used : HashMap(java.util.HashMap) UserDTO(org.apache.nifi.web.api.dto.UserDTO) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Map(java.util.Map) HashMap(java.util.HashMap) UserEntity(org.apache.nifi.web.api.entity.UserEntity)

Example 97 with NodeIdentifier

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

the class VersionControlInformationEntityMerger method merge.

public void merge(final VersionControlInformationEntity clientEntity, final Map<NodeIdentifier, VersionControlInformationEntity> entityMap) {
    final VersionControlInformationDTO clientDto = clientEntity.getVersionControlInformation();
    // We need to merge the 'current' and 'modified' flags because these are updated by nodes in the background. Since
    // the nodes can synchronize with the Flow Registry at different intervals, we have to determine how to handle these
    // flags if different nodes report different values for them.
    entityMap.values().stream().filter(entity -> entity != clientEntity).forEach(entity -> {
        final VersionControlInformationDTO dto = entity.getVersionControlInformation();
        updateFlowState(clientDto, dto);
    });
}
Also used : VersionedFlowState(org.apache.nifi.registry.flow.VersionedFlowState) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) Map(java.util.Map) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO)

Example 98 with NodeIdentifier

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

the class TestProcessorEndpointMerger method testMergeValidationErrors.

@Test
public void testMergeValidationErrors() {
    final ProcessorEndpointMerger merger = new ProcessorEndpointMerger();
    final Map<String, Set<NodeIdentifier>> validationErrorMap = new HashMap<>();
    final NodeIdentifier nodeId1234 = new NodeIdentifier("1234", "localhost", 9000, "localhost", 9001, "localhost", 9002, 9003, false);
    final List<String> nodeValidationErrors1234 = new ArrayList<>();
    nodeValidationErrors1234.add("error 1");
    nodeValidationErrors1234.add("error 2");
    ErrorMerger.mergeErrors(validationErrorMap, nodeId1234, nodeValidationErrors1234);
    final NodeIdentifier nodeXyz = new NodeIdentifier("xyz", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, false);
    final List<String> nodeValidationErrorsXyz = new ArrayList<>();
    nodeValidationErrorsXyz.add("error 1");
    ErrorMerger.mergeErrors(validationErrorMap, nodeXyz, nodeValidationErrorsXyz);
    assertEquals(2, validationErrorMap.size());
    final Set<NodeIdentifier> idsError1 = validationErrorMap.get("error 1");
    assertEquals(2, idsError1.size());
    assertTrue(idsError1.contains(nodeId1234));
    assertTrue(idsError1.contains(nodeXyz));
    final Set<NodeIdentifier> idsError2 = validationErrorMap.get("error 2");
    assertEquals(1, idsError2.size());
    assertTrue(idsError2.contains(nodeId1234));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 99 with NodeIdentifier

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

the class TestResponseUtils method testFindLongResponseTimes.

@Test
public void testFindLongResponseTimes() throws URISyntaxException {
    final Map<NodeIdentifier, NodeResponse> responses = new HashMap<>();
    final NodeIdentifier id1 = new NodeIdentifier("1", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, false);
    final NodeIdentifier id2 = new NodeIdentifier("2", "localhost", 8200, "localhost", 8201, "localhost", 8202, 8203, false);
    final NodeIdentifier id3 = new NodeIdentifier("3", "localhost", 8300, "localhost", 8301, "localhost", 8302, 8303, false);
    final NodeIdentifier id4 = new NodeIdentifier("4", "localhost", 8400, "localhost", 8401, "localhost", 8402, 8403, false);
    final URI uri = new URI("localhost:8080");
    final Response clientResponse = mock(Response.class);
    responses.put(id1, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(80), "1"));
    responses.put(id2, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(92), "1"));
    responses.put(id3, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(3), "1"));
    responses.put(id4, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(120), "1"));
    final AsyncClusterResponse response = new AsyncClusterResponse() {

        @Override
        public String getRequestIdentifier() {
            return "1";
        }

        @Override
        public String getMethod() {
            return "GET";
        }

        @Override
        public String getURIPath() {
            return null;
        }

        @Override
        public Set<NodeIdentifier> getNodesInvolved() {
            return new HashSet<>(responses.keySet());
        }

        @Override
        public Set<NodeIdentifier> getCompletedNodeIdentifiers() {
            return getNodesInvolved();
        }

        @Override
        public boolean isComplete() {
            return true;
        }

        @Override
        public boolean isOlderThan(long time, TimeUnit timeUnit) {
            return true;
        }

        @Override
        public NodeResponse getMergedResponse() {
            return null;
        }

        @Override
        public NodeResponse awaitMergedResponse() throws InterruptedException {
            return null;
        }

        @Override
        public NodeResponse awaitMergedResponse(long timeout, TimeUnit timeUnit) throws InterruptedException {
            return null;
        }

        @Override
        public NodeResponse getNodeResponse(NodeIdentifier nodeId) {
            return responses.get(nodeId);
        }

        @Override
        public Set<NodeResponse> getCompletedNodeResponses() {
            return new HashSet<>(responses.values());
        }
    };
    Set<NodeIdentifier> slowResponses = ResponseUtils.findLongResponseTimes(response, 1.5D);
    assertTrue(slowResponses.isEmpty());
    responses.put(id4, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(2500), "1"));
    slowResponses = ResponseUtils.findLongResponseTimes(response, 1.5D);
    assertEquals(1, slowResponses.size());
    assertEquals(id4, slowResponses.iterator().next());
}
Also used : Response(javax.ws.rs.core.Response) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) TimeUnit(java.util.concurrent.TimeUnit) URI(java.net.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 100 with NodeIdentifier

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

the class TestThreadPoolRequestReplicator method testMonitorNotifiedOnSuccessfulCompletion.

@Test(timeout = 5000)
public void testMonitorNotifiedOnSuccessfulCompletion() {
    withReplicator(replicator -> {
        final Object monitor = new Object();
        final CountDownLatch preNotifyLatch = new CountDownLatch(1);
        final CountDownLatch postNotifyLatch = new CountDownLatch(1);
        new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized (monitor) {
                    while (true) {
                        // If monitor is not notified, this will block indefinitely, and the test will timeout
                        try {
                            preNotifyLatch.countDown();
                            monitor.wait();
                            break;
                        } catch (InterruptedException e) {
                            continue;
                        }
                    }
                    postNotifyLatch.countDown();
                }
            }
        }).start();
        // wait for the background thread to notify that it is synchronized on monitor.
        preNotifyLatch.await();
        final Set<NodeIdentifier> nodeIds = new HashSet<>();
        final NodeIdentifier nodeId = new NodeIdentifier("1", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, false);
        nodeIds.add(nodeId);
        final URI uri = new URI("http://localhost:8080/processors/1");
        final Entity entity = new ProcessorEntity();
        // set the user
        final Authentication authentication = new NiFiAuthenticationToken(new NiFiUserDetails(StandardNiFiUser.ANONYMOUS));
        SecurityContextHolder.getContext().setAuthentication(authentication);
        // ensure the proxied entities header is set
        final Map<String, String> updatedHeaders = new HashMap<>();
        replicator.updateRequestHeaders(updatedHeaders, NiFiUserUtils.getNiFiUser());
        replicator.replicate(nodeIds, HttpMethod.GET, uri, entity, updatedHeaders, true, null, true, true, monitor);
        // wait for monitor to be notified.
        postNotifyLatch.await();
    });
}
Also used : ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) Entity(org.apache.nifi.web.api.entity.Entity) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) 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) NiFiUserDetails(org.apache.nifi.authorization.user.NiFiUserDetails) HashSet(java.util.HashSet) Test(org.junit.Test)

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