Search in sources :

Example 1 with NodeWorkload

use of org.apache.nifi.cluster.coordination.node.NodeWorkload 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 2 with NodeWorkload

use of org.apache.nifi.cluster.coordination.node.NodeWorkload in project nifi by apache.

the class TestJaxbProtocolUtils method testRoundTripClusterWorkloadResponse.

@Test
public void testRoundTripClusterWorkloadResponse() throws JAXBException {
    final ClusterWorkloadResponseMessage msg = new ClusterWorkloadResponseMessage();
    final Map<NodeIdentifier, NodeWorkload> expectedNodeWorkloads = new HashMap<>();
    IntStream.range(1, 4).forEach(i -> {
        final String hostname = "node" + i;
        final NodeIdentifier nodeId = new NodeIdentifier(hostname, hostname, 8080, hostname, 8081, hostname, 8082, 8083, false);
        final NodeWorkload workload = new NodeWorkload();
        workload.setReportedTimestamp(System.currentTimeMillis() - 1000);
        workload.setSystemStartTime(System.currentTimeMillis());
        workload.setActiveThreadCount(i);
        workload.setFlowFileCount(i * 10);
        workload.setFlowFileBytes(i * 10 * 1024);
        expectedNodeWorkloads.put(nodeId, workload);
    });
    msg.setNodeWorkloads(expectedNodeWorkloads);
    // Marshall.
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JaxbProtocolUtils.JAXB_CONTEXT.createMarshaller().marshal(msg, baos);
    // Un-marshall.
    final Object unmarshalled = JaxbProtocolUtils.JAXB_CONTEXT.createUnmarshaller().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
    assertTrue(unmarshalled instanceof ClusterWorkloadResponseMessage);
    // Assert result.
    final ClusterWorkloadResponseMessage response = (ClusterWorkloadResponseMessage) unmarshalled;
    assertEquals(expectedNodeWorkloads.size(), response.getNodeWorkloads().size());
    response.getNodeWorkloads().entrySet().stream().forEach(entry -> {
        assertTrue(expectedNodeWorkloads.containsKey(entry.getKey()));
        final NodeWorkload w = entry.getValue();
        NodeWorkload expectedW = expectedNodeWorkloads.get(entry.getKey());
        assertEquals(expectedW.getActiveThreadCount(), w.getActiveThreadCount());
        assertEquals(expectedW.getReportedTimestamp(), w.getReportedTimestamp());
        assertEquals(expectedW.getSystemStartTime(), w.getSystemStartTime());
        assertEquals(expectedW.getFlowFileBytes(), w.getFlowFileBytes());
        assertEquals(expectedW.getFlowFileCount(), w.getFlowFileCount());
    });
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with NodeWorkload

use of org.apache.nifi.cluster.coordination.node.NodeWorkload in project nifi by apache.

the class ClusterProtocolHeartbeatMonitor method handleClusterWorkload.

private ProtocolMessage handleClusterWorkload(final ClusterWorkloadRequestMessage msg) {
    final ClusterWorkloadResponseMessage response = new ClusterWorkloadResponseMessage();
    final Map<NodeIdentifier, NodeWorkload> workloads = new HashMap<>();
    getLatestHeartbeats().values().stream().filter(hb -> NodeConnectionState.CONNECTED.equals(hb.getConnectionStatus().getState())).forEach(hb -> {
        NodeWorkload wl = new NodeWorkload();
        wl.setReportedTimestamp(hb.getTimestamp());
        wl.setSystemStartTime(hb.getSystemStartTime());
        wl.setActiveThreadCount(hb.getActiveThreadCount());
        wl.setFlowFileCount(hb.getFlowFileCount());
        wl.setFlowFileBytes(hb.getFlowFileBytes());
        workloads.put(hb.getNodeIdentifier(), wl);
    });
    response.setNodeWorkloads(workloads);
    return response;
}
Also used : ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) HeartbeatPayload(org.apache.nifi.cluster.protocol.HeartbeatPayload) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) HeartbeatMessage(org.apache.nifi.cluster.protocol.message.HeartbeatMessage) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) MessageType(org.apache.nifi.cluster.protocol.message.ProtocolMessage.MessageType) NodeConnectionState(org.apache.nifi.cluster.coordination.node.NodeConnectionState) NodeConnectionStatus(org.apache.nifi.cluster.coordination.node.NodeConnectionStatus) ProtocolHandler(org.apache.nifi.cluster.protocol.ProtocolHandler) HeartbeatResponseMessage(org.apache.nifi.cluster.protocol.message.HeartbeatResponseMessage) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Heartbeat(org.apache.nifi.cluster.protocol.Heartbeat) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NiFiProperties(org.apache.nifi.util.NiFiProperties) ClusterWorkloadRequestMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadRequestMessage) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) ProtocolListener(org.apache.nifi.cluster.protocol.ProtocolListener) Collections(java.util.Collections) ProtocolMessage(org.apache.nifi.cluster.protocol.message.ProtocolMessage) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterWorkloadResponseMessage(org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload)

Example 4 with NodeWorkload

use of org.apache.nifi.cluster.coordination.node.NodeWorkload in project nifi by apache.

the class TestSiteToSiteResource method testPeersClustered.

@Test
public void testPeersClustered() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final Map<String, String> clusterSettings = new HashMap<>();
    clusterSettings.put(NiFiProperties.CLUSTER_IS_NODE, "true");
    final SiteToSiteResource resource = getSiteToSiteResource(serviceFacade, clusterSettings);
    final ClusterCoordinator clusterCoordinator = mock(ClusterCoordinator.class);
    final Map<String, NodeWorkload> hostportWorkloads = new HashMap<>();
    final Map<NodeIdentifier, NodeWorkload> workloads = new HashMap<>();
    IntStream.range(1, 4).forEach(i -> {
        final String hostname = "node" + i;
        final int siteToSiteHttpApiPort = 8110 + i;
        final NodeIdentifier nodeId = new NodeIdentifier(hostname, hostname, 8080 + i, hostname, 8090 + i, hostname, 8100 + i, siteToSiteHttpApiPort, false);
        final NodeWorkload workload = new NodeWorkload();
        workload.setReportedTimestamp(System.currentTimeMillis() - i);
        workload.setFlowFileBytes(1024 * i);
        workload.setFlowFileCount(10 * i);
        workload.setActiveThreadCount(i);
        workload.setSystemStartTime(System.currentTimeMillis() - (1000 * i));
        workloads.put(nodeId, workload);
        hostportWorkloads.put(hostname + ":" + siteToSiteHttpApiPort, workload);
    });
    when(clusterCoordinator.getClusterWorkload()).thenReturn(workloads);
    resource.setClusterCoordinator(clusterCoordinator);
    final Response response = resource.getPeers(req);
    PeersEntity resultEntity = (PeersEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertEquals(3, resultEntity.getPeers().size());
    resultEntity.getPeers().stream().forEach(peerDTO -> {
        final NodeWorkload workload = hostportWorkloads.get(peerDTO.getHostname() + ":" + peerDTO.getPort());
        assertNotNull(workload);
        assertEquals(workload.getFlowFileCount(), peerDTO.getFlowFileCount());
    });
}
Also used : HashMap(java.util.HashMap) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) PeersEntity(org.apache.nifi.web.api.entity.PeersEntity) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Test(org.junit.Test)

Aggregations

NodeWorkload (org.apache.nifi.cluster.coordination.node.NodeWorkload)4 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)2 ClusterWorkloadResponseMessage (org.apache.nifi.cluster.protocol.message.ClusterWorkloadResponseMessage)2 PeersEntity (org.apache.nifi.web.api.entity.PeersEntity)2 Test (org.junit.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1