Search in sources :

Example 1 with Heartbeat

use of org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat in project incubator-eventmesh by apache.

the class HeartbeatProcessor method doAclCheck.

private void doAclCheck(Heartbeat heartbeat) throws AclException {
    RequestHeader header = heartbeat.getHeader();
    if (eventMeshGrpcServer.getEventMeshGrpcConfiguration().eventMeshServerSecurityEnable) {
        String remoteAdd = header.getIp();
        String user = header.getUsername();
        String pass = header.getPassword();
        String sys = header.getSys();
        int requestCode = Integer.valueOf(RequestCode.HEARTBEAT.getRequestCode());
        for (Heartbeat.HeartbeatItem item : heartbeat.getHeartbeatItemsList()) {
            Acl.doAclCheckInHttpHeartbeat(remoteAdd, user, pass, sys, item.getTopic(), requestCode);
        }
    }
}
Also used : Heartbeat(org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)

Example 2 with Heartbeat

use of org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat in project incubator-eventmesh by apache.

the class HeartbeatProcessor method process.

public void process(Heartbeat heartbeat, EventEmitter<Response> emitter) throws Exception {
    RequestHeader header = heartbeat.getHeader();
    if (!ServiceUtils.validateHeader(header)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateHeartBeat(heartbeat)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    try {
        doAclCheck(heartbeat);
    } catch (AclException e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION, HeartbeatProcessor failed", e);
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
        return;
    }
    // only handle heartbeat for consumers
    ClientType clientType = heartbeat.getClientType();
    if (!ClientType.SUB.equals(clientType)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    ConsumerManager consumerManager = eventMeshGrpcServer.getConsumerManager();
    String consumerGroup = heartbeat.getConsumerGroup();
    // update clients' timestamp in the heartbeat items
    for (Heartbeat.HeartbeatItem item : heartbeat.getHeartbeatItemsList()) {
        ConsumerGroupClient hbClient = ConsumerGroupClient.builder().env(header.getEnv()).idc(header.getIdc()).sys(header.getSys()).ip(header.getIp()).pid(header.getPid()).consumerGroup(consumerGroup).topic(item.getTopic()).lastUpTime(new Date()).build();
        consumerManager.updateClientTime(hbClient);
    }
    ServiceUtils.sendRespAndDone(StatusCode.SUCCESS, "heartbeat success", emitter);
}
Also used : ClientType(org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat.ClientType) ConsumerManager(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager) Heartbeat(org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) AclException(org.apache.eventmesh.api.exception.AclException) ConsumerGroupClient(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient) Date(java.util.Date)

Example 3 with Heartbeat

use of org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat in project incubator-eventmesh by apache.

the class EventMeshGrpcConsumer method heartBeat.

private void heartBeat() {
    RequestHeader header = EventMeshClientUtil.buildHeader(clientConfig, EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME);
    scheduler.scheduleAtFixedRate(() -> {
        if (subscriptionMap.isEmpty()) {
            return;
        }
        Heartbeat.Builder heartbeatBuilder = Heartbeat.newBuilder().setHeader(header).setConsumerGroup(clientConfig.getConsumerGroup()).setClientType(Heartbeat.ClientType.SUB);
        for (Map.Entry<String, String> entry : subscriptionMap.entrySet()) {
            Heartbeat.HeartbeatItem heartbeatItem = Heartbeat.HeartbeatItem.newBuilder().setTopic(entry.getKey()).setUrl(entry.getValue()).build();
            heartbeatBuilder.addHeartbeatItems(heartbeatItem);
        }
        Heartbeat heartbeat = heartbeatBuilder.build();
        try {
            Response response = heartbeatClient.heartbeat(heartbeat);
            if (logger.isDebugEnabled()) {
                logger.debug("Grpc Consumer Heartbeat response: {}", response);
            }
        } catch (Exception e) {
            logger.error("Error in sending out heartbeat. error {}", e.getMessage());
        }
    }, 10000, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS);
    logger.info("Grpc Consumer Heartbeat started.");
}
Also used : Response(org.apache.eventmesh.common.protocol.grpc.protos.Response) Heartbeat(org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Heartbeat (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat)3 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)3 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AclException (org.apache.eventmesh.api.exception.AclException)1 ClientType (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat.ClientType)1 Response (org.apache.eventmesh.common.protocol.grpc.protos.Response)1 ConsumerManager (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager)1 ConsumerGroupClient (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)1