Search in sources :

Example 1 with AclException

use of org.apache.eventmesh.api.exception.AclException 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 2 with AclException

use of org.apache.eventmesh.api.exception.AclException in project incubator-eventmesh by apache.

the class SubscribeProcessor method process.

public void process(Subscription subscription, EventEmitter<Response> emitter) throws Exception {
    RequestHeader header = subscription.getHeader();
    if (!ServiceUtils.validateHeader(header)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateSubscription(grpcType, subscription)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    try {
        doAclCheck(subscription);
    } catch (AclException e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION to Subscribe. failed", e);
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
        return;
    }
    ConsumerManager consumerManager = eventMeshGrpcServer.getConsumerManager();
    String consumerGroup = subscription.getConsumerGroup();
    String url = subscription.getUrl();
    List<Subscription.SubscriptionItem> subscriptionItems = subscription.getSubscriptionItemsList();
    // Collect new clients in the subscription
    List<ConsumerGroupClient> newClients = new LinkedList<>();
    for (Subscription.SubscriptionItem item : subscriptionItems) {
        ConsumerGroupClient newClient = ConsumerGroupClient.builder().env(header.getEnv()).idc(header.getIdc()).sys(header.getSys()).ip(header.getIp()).pid(header.getPid()).consumerGroup(consumerGroup).topic(item.getTopic()).grpcType(grpcType).subscriptionMode(item.getMode()).url(url).lastUpTime(new Date()).build();
        newClients.add(newClient);
    }
    // register new clients into ConsumerManager
    for (ConsumerGroupClient newClient : newClients) {
        consumerManager.registerClient(newClient);
    }
    // register new clients into EventMeshConsumer
    EventMeshConsumer eventMeshConsumer = consumerManager.getEventMeshConsumer(consumerGroup);
    boolean requireRestart = false;
    for (ConsumerGroupClient newClient : newClients) {
        if (eventMeshConsumer.registerClient(newClient)) {
            requireRestart = true;
        }
    }
    // restart consumer group if required
    if (requireRestart) {
        logger.info("ConsumerGroup {} topic info changed, restart EventMesh Consumer", consumerGroup);
        consumerManager.restartEventMeshConsumer(consumerGroup);
    } else {
        logger.warn("EventMesh consumer [{}] didn't restart.", consumerGroup);
    }
    ServiceUtils.sendRespAndDone(StatusCode.SUCCESS, "subscribe success", emitter);
}
Also used : EventMeshConsumer(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer) AclException(org.apache.eventmesh.api.exception.AclException) LinkedList(java.util.LinkedList) Date(java.util.Date) ConsumerManager(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription) ConsumerGroupClient(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)

Example 3 with AclException

use of org.apache.eventmesh.api.exception.AclException in project incubator-eventmesh by apache.

the class SubscribeStreamProcessor method process.

public void process(Subscription subscription, EventEmitter<SimpleMessage> emitter) throws Exception {
    RequestHeader header = subscription.getHeader();
    if (!ServiceUtils.validateHeader(header)) {
        ServiceUtils.sendStreamRespAndDone(header, StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
        return;
    }
    if (!ServiceUtils.validateSubscription(grpcType, subscription)) {
        ServiceUtils.sendStreamRespAndDone(header, StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    try {
        doAclCheck(subscription);
    } catch (AclException e) {
        aclLogger.warn("CLIENT HAS NO PERMISSION to Subscribe. failed", e);
        ServiceUtils.sendStreamRespAndDone(header, StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
        return;
    }
    ConsumerManager consumerManager = eventMeshGrpcServer.getConsumerManager();
    String consumerGroup = subscription.getConsumerGroup();
    List<Subscription.SubscriptionItem> subscriptionItems = subscription.getSubscriptionItemsList();
    // Collect new clients in subscription
    List<ConsumerGroupClient> newClients = new LinkedList<>();
    for (Subscription.SubscriptionItem item : subscriptionItems) {
        ConsumerGroupClient newClient = ConsumerGroupClient.builder().env(header.getEnv()).idc(header.getIdc()).sys(header.getSys()).ip(header.getIp()).pid(header.getPid()).consumerGroup(consumerGroup).topic(item.getTopic()).subscriptionMode(item.getMode()).grpcType(grpcType).eventEmitter(emitter).lastUpTime(new Date()).build();
        newClients.add(newClient);
    }
    // register new clients into ConsumerManager
    for (ConsumerGroupClient newClient : newClients) {
        consumerManager.registerClient(newClient);
    }
    // register new clients into EventMeshConsumer
    EventMeshConsumer eventMeshConsumer = consumerManager.getEventMeshConsumer(consumerGroup);
    boolean requireRestart = false;
    for (ConsumerGroupClient newClient : newClients) {
        if (eventMeshConsumer.registerClient(newClient)) {
            requireRestart = true;
        }
    }
    // restart consumer group if required
    if (requireRestart) {
        logger.info("ConsumerGroup {} topic info changed, restart EventMesh Consumer", consumerGroup);
        consumerManager.restartEventMeshConsumer(consumerGroup);
    } else {
        logger.warn("EventMesh consumer [{}] didn't restart.", consumerGroup);
    }
    ServiceUtils.sendStreamResp(header, StatusCode.SUCCESS, "subscribe success", emitter);
}
Also used : EventMeshConsumer(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer) AclException(org.apache.eventmesh.api.exception.AclException) LinkedList(java.util.LinkedList) Date(java.util.Date) ConsumerManager(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) Subscription(org.apache.eventmesh.common.protocol.grpc.protos.Subscription) ConsumerGroupClient(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)

Aggregations

Date (java.util.Date)3 AclException (org.apache.eventmesh.api.exception.AclException)3 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)3 ConsumerManager (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager)3 ConsumerGroupClient (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)3 LinkedList (java.util.LinkedList)2 Subscription (org.apache.eventmesh.common.protocol.grpc.protos.Subscription)2 EventMeshConsumer (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer)2 Heartbeat (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat)1 ClientType (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat.ClientType)1