Skip to content

Commit

Permalink
Fix rocketmq4 transmission error.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jun 3, 2024
1 parent 8957154 commit 7664592
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.jd.live.agent.core.plugin.definition.PluginDefinition;
import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter;
import com.jd.live.agent.governance.config.GovernanceConfig;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor.MQProducerInterceptor;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor.MQProducerSendInterceptor;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor.MQProducerStartInterceptor;

@Extension(value = "MQProducerDefinition_v4", order = PluginDefinition.ORDER_TRANSMISSION)
@ConditionalOnProperties(value = {
Expand All @@ -36,11 +37,20 @@ public class MQProducerDefinition extends PluginDefinitionAdapter {

private static final String METHOD_START = "start";

private static final String METHOD_SEND = "send";

private static final String METHOD_REQUEST = "request";

private static final String METHOD_SEND_MESSAGE_IN_TRANSACTION = "sendMessageInTransaction";

private static final String METHOD_SEND_ONEWAY = "sendOneway";

public MQProducerDefinition() {
super(MatcherBuilder.isImplement(TYPE_MQ_PRODUCER),
new InterceptorDefinitionAdapter(
MatcherBuilder.named(METHOD_START).
and(MatcherBuilder.arguments(0)),
new MQProducerInterceptor()));
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_START), new MQProducerStartInterceptor()),
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_SEND), new MQProducerSendInterceptor()),
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_REQUEST), new MQProducerSendInterceptor()),
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_SEND_ONEWAY), new MQProducerSendInterceptor()),
new InterceptorDefinitionAdapter(MatcherBuilder.named(METHOD_SEND_MESSAGE_IN_TRANSACTION), new MQProducerSendInterceptor()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,45 @@
*/
package com.jd.live.agent.plugin.transmission.rocketmq.v4.definition;

import com.jd.live.agent.bootstrap.classloader.ResourcerType;
import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder;
import com.jd.live.agent.core.extension.annotation.*;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.InjectLoader;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinition;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter;
import com.jd.live.agent.core.plugin.definition.PluginDefinition;
import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter;
import com.jd.live.agent.governance.config.GovernanceConfig;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor.MQClientAPIImplInterceptor;
import com.jd.live.agent.governance.context.bag.CargoRequire;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor.MessageUtilInterceptor;

@Extension(value = "MQClientAPIImplDefinition_v4", order = PluginDefinition.ORDER_TRANSMISSION)
import java.util.List;

@Injectable
@Extension(value = "MessageUtilDefinition_v5", order = PluginDefinition.ORDER_TRANSMISSION)
@ConditionalOnProperties(value = {
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LIVE_ENABLED, matchIfMissing = true),
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LANE_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.OR)
@ConditionalOnClass(MQClientAPIImplDefinition.TYPE_MQ_CLIENT_API_IMPL)
@ConditionalOnClass(MessageUtilDefinition.TYPE_MESSAGE_UTIL)
@ConditionalOnClass(MessageDefinition.TYPE_CLIENT_LOGGER)
public class MQClientAPIImplDefinition extends PluginDefinitionAdapter {
public class MessageUtilDefinition extends PluginDefinitionAdapter {

protected static final String TYPE_MQ_CLIENT_API_IMPL = "org.apache.rocketmq.client.impl.MQClientAPIImpl";
protected static final String TYPE_MESSAGE_UTIL = "org.apache.rocketmq.client.utils.MessageUtil";

private static final String METHOD_SEND_MESSAGE = "sendMessage";
private static final String METHOD_CREATE_REPLY_MESSAGE = "createReplyMessage";

private static final String[] ARGUMENT_SEND_MESSAGE = {
"java.lang.String",
"java.lang.String",
"org.apache.rocketmq.common.message.Message",
"org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader",
"long",
"org.apache.rocketmq.client.impl.CommunicationMode",
"org.apache.rocketmq.client.producer.SendCallback",
"org.apache.rocketmq.client.impl.producer.TopicPublishInfo",
"org.apache.rocketmq.client.impl.factory.MQClientInstance",
"int",
"org.apache.rocketmq.client.hook.SendMessageContext",
"org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl"
};
@Inject
@InjectLoader(ResourcerType.CORE_IMPL)
private List<CargoRequire> requires;

public MQClientAPIImplDefinition() {
super(TYPE_MQ_CLIENT_API_IMPL,
public MessageUtilDefinition() {
this.matcher = () -> MatcherBuilder.named(TYPE_MESSAGE_UTIL);
this.interceptors = new InterceptorDefinition[]{
new InterceptorDefinitionAdapter(
MatcherBuilder.named(METHOD_SEND_MESSAGE).
and(MatcherBuilder.arguments(ARGUMENT_SEND_MESSAGE)),
new MQClientAPIImplInterceptor()));
MatcherBuilder.named(METHOD_CREATE_REPLY_MESSAGE), () -> new MessageUtilInterceptor(requires))
};
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor;

import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
import com.jd.live.agent.governance.context.RequestContext;
import org.apache.rocketmq.common.message.Message;

import java.util.Collection;

public class MQProducerSendInterceptor extends InterceptorAdaptor {

@SuppressWarnings("unchecked")
@Override
public void onEnter(ExecutableContext ctx) {
Object argument = ctx.getArguments()[0];
if (argument instanceof Message) {
attachTag((Message) argument);
} else if (argument instanceof Collection) {
attachTag((Collection<Message>) argument);
}
}

private void attachTag(Collection<Message> messages) {
RequestContext.cargos(cargo ->
messages.forEach(
message -> message.putUserProperty(cargo.getKey(), cargo.getValue())));
}

private void attachTag(Message message) {
RequestContext.cargos(cargo -> message.putUserProperty(cargo.getKey(), cargo.getValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
import com.jd.live.agent.plugin.transmission.rocketmq.v4.context.RocketmqContext;

public class MQProducerInterceptor extends InterceptorAdaptor {
public class MQProducerStartInterceptor extends InterceptorAdaptor {

@Override
public void onExit(ExecutableContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public void onEnter(ExecutableContext ctx) {

private void restoreTag(Message message) {
String restored = message.getProperty(Cargo.KEY_TAG_RESTORED_BY);
if (!application.getInstance().equals(restored)) {
message.putUserProperty(Cargo.KEY_TAG_RESTORED_BY, application.getInstance());
String uniqueThreadName = application.getUniqueThreadName();
if (!uniqueThreadName.equals(restored)) {
message.putUserProperty(Cargo.KEY_TAG_RESTORED_BY, uniqueThreadName);
RequestContext.create().addCargo(require, message.getProperties(), Label::parseValue);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jd.live.agent.plugin.transmission.rocketmq.v4.interceptor;

import com.jd.live.agent.bootstrap.bytekit.context.ExecutableContext;
import com.jd.live.agent.bootstrap.bytekit.context.MethodContext;
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
import com.jd.live.agent.governance.context.bag.CargoRequire;
import com.jd.live.agent.governance.context.bag.CargoRequires;
import org.apache.rocketmq.common.message.Message;

import java.util.List;
import java.util.Map;

public class MessageUtilInterceptor extends InterceptorAdaptor {

private final CargoRequire require;

public MessageUtilInterceptor(List<CargoRequire> requires) {
this.require = new CargoRequires(requires);
}

@Override
public void onSuccess(ExecutableContext ctx) {
MethodContext mc = (MethodContext) ctx;
attachTag((Message) mc.getArgument(0), (Message) mc.getResult());
}

private void attachTag(Message request, Message response) {
Map<String, String> properties = request.getProperties();
if (properties != null) {
properties.forEach((k, v) -> {
if (require.match(k)) {
response.putUserProperty(k, v);
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
com.jd.live.agent.plugin.transmission.rocketmq.v4.definition.MessageDefinition
com.jd.live.agent.plugin.transmission.rocketmq.v4.definition.MQClientAPIImplDefinition
com.jd.live.agent.plugin.transmission.rocketmq.v4.definition.MessageUtilDefinition
com.jd.live.agent.plugin.transmission.rocketmq.v4.definition.MQProducerDefinition
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@

public class MQProducerSendInterceptor extends InterceptorAdaptor {

private static final char SPLITTER_KEY_VALUE = 1;

private static final char SPLITTER_HEADER = 2;

@SuppressWarnings("unchecked")
@Override
public void onEnter(ExecutableContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

public class MessageUtilInterceptor extends InterceptorAdaptor {

private static final char SPLITTER_KEY_VALUE = 1;

private static final char SPLITTER_HEADER = 2;

private final CargoRequire require;

public MessageUtilInterceptor(List<CargoRequire> requires) {
Expand Down

0 comments on commit 7664592

Please sign in to comment.