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");
}
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);
}
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);
}
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);
}
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);
}
Aggregations