Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4631]Optimize the message body of the Java SDK's returned reply message #4632

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ProtocolKey {
public static final String GRPC_RESPONSE_TIME = "time";

public static final String SUB_MESSAGE_TYPE = "submessagetype";
public static final String SUB_REPLY_MESSAGE = "subscription_reply";

/**
* CloudEvents spec
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.eventmesh.common.protocol.grpc.common.EventMeshCloudEventUtils;
import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.grpc.common.StatusCode;
import org.apache.eventmesh.common.protocol.grpc.common.SubscriptionReply;
import org.apache.eventmesh.runtime.boot.EventMeshGrpcServer;
import org.apache.eventmesh.runtime.constants.EventMeshConstants;
import org.apache.eventmesh.runtime.core.protocol.grpc.processor.ReplyMessageProcessor;
Expand Down Expand Up @@ -87,7 +86,7 @@ public StreamObserver<CloudEvent> subscribeStream(StreamObserver<CloudEvent> res
public void onNext(CloudEvent subscription) {
final String subMessageType = Optional.ofNullable(subscription.getAttributesMap().get(ProtocolKey.SUB_MESSAGE_TYPE))
.orElse(CloudEventAttributeValue.newBuilder().build()).getCeString();
if (StringUtils.equals(subMessageType, SubscriptionReply.TYPE)) {
if (StringUtils.equals(subMessageType, ProtocolKey.SUB_REPLY_MESSAGE)) {
log.info("cmd={}|{}|client2eventMesh|from={}|to={}", "reply-to-server", EventMeshConstants.PROTOCOL_GRPC,
EventMeshCloudEventUtils.getIp(subscription), eventMeshGrpcServer.getEventMeshGrpcConfiguration().getEventMeshIp());
handleSubscribeReply(subscription, emitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.eventmesh.common.protocol.grpc.cloudevents.ConsumerServiceGrpc.ConsumerServiceStub;
import org.apache.eventmesh.common.protocol.grpc.common.EventMeshCloudEventUtils;
import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
import org.apache.eventmesh.common.protocol.grpc.common.SubscriptionReply;
import org.apache.eventmesh.common.utils.JsonUtils;
import org.apache.eventmesh.common.utils.LogUtils;

import java.io.Serializable;
Expand Down Expand Up @@ -118,26 +116,19 @@ public void onCompleted() {
private CloudEvent buildReplyMessage(final CloudEvent reqMessage, final T replyMessage) {
final CloudEvent cloudEvent = EventMeshCloudEventBuilder.buildEventMeshCloudEvent(replyMessage,
clientConfig, listener.getProtocolType());
SubscriptionReply subscriptionReply = SubscriptionReply.builder().producerGroup(clientConfig.getConsumerGroup())
.topic(EventMeshCloudEventUtils.getSubject(cloudEvent))
.content(EventMeshCloudEventUtils.getDataContent(cloudEvent))
.seqNum(EventMeshCloudEventUtils.getSeqNum(cloudEvent))
.uniqueId(EventMeshCloudEventUtils.getUniqueId(cloudEvent))
.ttl(EventMeshCloudEventUtils.getTtl(cloudEvent)).build();

Map<String, String> prop = new HashMap<>();
Map<String, CloudEventAttributeValue> reqMessageMap = reqMessage.getAttributesMap();
reqMessageMap.entrySet().forEach(entry -> prop.put(entry.getKey(), entry.getValue().getCeString()));
Map<String, CloudEventAttributeValue> cloudEventMap = cloudEvent.getAttributesMap();
cloudEventMap.entrySet().forEach(entry -> prop.put(entry.getKey(), entry.getValue().getCeString()));
subscriptionReply.putAllProperties(prop);

return CloudEvent.newBuilder(cloudEvent).putAllAttributes(reqMessageMap)
return CloudEvent.newBuilder(cloudEvent).putAllAttributes(reqMessageMap).putAllAttributes(cloudEventMap)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't newBuilder(cloudEvent) automatically merge the attributes (i.e., cloudEventMap) of cloudEvent here? Additionally, is prop object redundant now?

newBuilder(cloudEvent)这一步不是会自动合并cloudEvent的attributes(即cloudEventMap)吗?另外prop对象现在是不是多余的?

Copy link
Member Author

@mxsm mxsm Dec 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't newBuilder(cloudEvent) automatically merge the attributes (i.e., cloudEventMap) of cloudEvent here? Additionally, is prop object redundant now?

newBuilder(cloudEvent)这一步不是会自动合并cloudEvent的attributes(即cloudEventMap)吗?另外prop对象现在是不是多余的?

@pandaapo Here, it is guaranteed that if there are identical properties between the returned message and the consumed message, the properties of the consumed message will be used. At the same time, I compared the version before optimization. It is also a similar operation. If the properties do not need to be overwritten, this step is not required. I keep it consistent with the version before GRPC optimization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are identical properties between the returned message and the consumed message, the properties of the consumed message will be used.

OK. Then what is role of prop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are identical properties between the returned message and the consumed message, the properties of the consumed message will be used.

OK. Then what is role of prop?

@pandaapo I have removed it. my fault .

.putAttributes(ProtocolKey.DATA_CONTENT_TYPE,
CloudEventAttributeValue.newBuilder().setCeString(EventMeshDataContentType.JSON.getCode()).build())
//Indicate that it is a subscription response
.putAttributes(ProtocolKey.SUB_MESSAGE_TYPE, CloudEventAttributeValue.newBuilder().setCeString(SubscriptionReply.TYPE).build())
.setTextData(JsonUtils.toJSONString(subscriptionReply)).build();
// Indicate that it is a subscription response
.putAttributes(ProtocolKey.SUB_MESSAGE_TYPE, CloudEventAttributeValue.newBuilder().setCeString(ProtocolKey.SUB_REPLY_MESSAGE).build())
.build();
}

@Override
Expand Down