Search in sources :

Example 1 with ConsumerGroupClient

use of org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient 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 ConsumerGroupClient

use of org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient in project incubator-eventmesh by apache.

the class ConsumerManager method registerClient.

public synchronized void registerClient(ConsumerGroupClient newClient) {
    String consumerGroup = newClient.getConsumerGroup();
    String topic = newClient.getTopic();
    GrpcType grpcType = newClient.getGrpcType();
    String url = newClient.getUrl();
    String ip = newClient.getIp();
    String pid = newClient.getPid();
    SubscriptionMode subscriptionMode = newClient.getSubscriptionMode();
    List<ConsumerGroupClient> localClients = clientTable.get(consumerGroup);
    if (localClients == null) {
        localClients = new ArrayList<>();
        localClients.add(newClient);
        clientTable.put(consumerGroup, localClients);
    } else {
        boolean isContains = false;
        for (ConsumerGroupClient localClient : localClients) {
            if (GrpcType.WEBHOOK.equals(grpcType) && StringUtils.equals(localClient.getTopic(), topic) && StringUtils.equals(localClient.getUrl(), url) && localClient.getSubscriptionMode().equals(subscriptionMode)) {
                isContains = true;
                localClient.setUrl(newClient.getUrl());
                localClient.setLastUpTime(newClient.getLastUpTime());
                break;
            } else if (GrpcType.STREAM.equals(grpcType) && StringUtils.equals(localClient.getTopic(), topic) && StringUtils.equals(localClient.getIp(), ip) && StringUtils.equals(localClient.getPid(), pid) && localClient.getSubscriptionMode().equals(subscriptionMode)) {
                isContains = true;
                localClient.setEventEmitter(newClient.getEventEmitter());
                localClient.setLastUpTime(newClient.getLastUpTime());
                break;
            }
        }
        if (!isContains) {
            localClients.add(newClient);
        }
    }
}
Also used : SubscriptionMode(org.apache.eventmesh.common.protocol.grpc.protos.Subscription.SubscriptionItem.SubscriptionMode) GrpcType(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.GrpcType) ConsumerGroupClient(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)

Example 3 with ConsumerGroupClient

use of org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient in project incubator-eventmesh by apache.

the class UnsubscribeProcessor 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(null, subscription)) {
        ServiceUtils.sendRespAndDone(StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
        return;
    }
    ConsumerManager consumerManager = eventMeshGrpcServer.getConsumerManager();
    String consumerGroup = subscription.getConsumerGroup();
    String url = subscription.getUrl();
    List<Subscription.SubscriptionItem> subscriptionItems = subscription.getSubscriptionItemsList();
    // Collect clients to remove in the unsubscribe
    List<ConsumerGroupClient> removeClients = 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()).url(url).lastUpTime(new Date()).build();
        removeClients.add(newClient);
    }
    // deregister clients from ConsumerManager
    for (ConsumerGroupClient client : removeClients) {
        consumerManager.deregisterClient(client);
    }
    // deregister clients from EventMeshConsumer
    EventMeshConsumer eventMeshConsumer = consumerManager.getEventMeshConsumer(consumerGroup);
    boolean requireRestart = false;
    for (ConsumerGroupClient client : removeClients) {
        if (eventMeshConsumer.deregisterClient(client)) {
            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, "unsubscribe success", emitter);
}
Also used : EventMeshConsumer(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer) 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 4 with ConsumerGroupClient

use of org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient 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 5 with ConsumerGroupClient

use of org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient 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

ConsumerGroupClient (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)8 Date (java.util.Date)5 LinkedList (java.util.LinkedList)4 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)4 ConsumerManager (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager)4 AclException (org.apache.eventmesh.api.exception.AclException)3 Subscription (org.apache.eventmesh.common.protocol.grpc.protos.Subscription)3 EventMeshConsumer (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.EventMeshConsumer)3 HashSet (java.util.HashSet)1 Heartbeat (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat)1 ClientType (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat.ClientType)1 SubscriptionMode (org.apache.eventmesh.common.protocol.grpc.protos.Subscription.SubscriptionItem.SubscriptionMode)1 GrpcType (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.GrpcType)1