Search in sources :

Example 1 with ConsumerManager

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

the class EventMeshGrpcServer method init.

public void init() throws Exception {
    logger.info("==================EventMeshGRPCServer Initializing==================");
    initThreadPool();
    initHttpClientPool();
    msgRateLimiter = RateLimiter.create(eventMeshGrpcConfiguration.eventMeshMsgReqNumPerSecond);
    producerManager = new ProducerManager(this);
    producerManager.init();
    consumerManager = new ConsumerManager(this);
    consumerManager.init();
    grpcRetryer = new GrpcRetryer(this);
    grpcRetryer.init();
    int serverPort = eventMeshGrpcConfiguration.grpcServerPort;
    server = ServerBuilder.forPort(serverPort).addService(new ProducerService(this, sendMsgExecutor)).addService(new ConsumerService(this, clientMgmtExecutor, replyMsgExecutor)).addService(new HeartbeatService(this, clientMgmtExecutor)).build();
    logger.info("GRPCServer[port={}] started", serverPort);
    logger.info("-----------------EventMeshGRPCServer initialized");
}
Also used : GrpcRetryer(org.apache.eventmesh.runtime.core.protocol.grpc.retry.GrpcRetryer) ConsumerService(org.apache.eventmesh.runtime.core.protocol.grpc.service.ConsumerService) ConsumerManager(org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager) ProducerService(org.apache.eventmesh.runtime.core.protocol.grpc.service.ProducerService) ProducerManager(org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager) HeartbeatService(org.apache.eventmesh.runtime.core.protocol.grpc.service.HeartbeatService)

Example 2 with ConsumerManager

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

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

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

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

ConsumerManager (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.ConsumerManager)5 Date (java.util.Date)4 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)4 ConsumerGroupClient (org.apache.eventmesh.runtime.core.protocol.grpc.consumer.consumergroup.ConsumerGroupClient)4 LinkedList (java.util.LinkedList)3 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 Heartbeat (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat)1 ClientType (org.apache.eventmesh.common.protocol.grpc.protos.Heartbeat.ClientType)1 ProducerManager (org.apache.eventmesh.runtime.core.protocol.grpc.producer.ProducerManager)1 GrpcRetryer (org.apache.eventmesh.runtime.core.protocol.grpc.retry.GrpcRetryer)1 ConsumerService (org.apache.eventmesh.runtime.core.protocol.grpc.service.ConsumerService)1 HeartbeatService (org.apache.eventmesh.runtime.core.protocol.grpc.service.HeartbeatService)1 ProducerService (org.apache.eventmesh.runtime.core.protocol.grpc.service.ProducerService)1