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