Skip to content

Commit

Permalink
add providerProcessRegister event and record context to bolt (#1417)
Browse files Browse the repository at this point in the history
Co-authored-by: liujianjun.ljj <[email protected]>
  • Loading branch information
EvenLjj and liujianjun.ljj authored May 22, 2024
1 parent c7a45ea commit 6b5f639
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 4 deletions.
4 changes: 2 additions & 2 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<slf4j.version>1.7.21</slf4j.version>
<sofa.common.tools.version>1.3.15</sofa.common.tools.version>
<sofa.common.tools.version>1.4.0</sofa.common.tools.version>
<javassist.version>3.29.2-GA</javassist.version>
<netty.version>4.1.44.Final</netty.version>
<hessian.version>3.5.3</hessian.version>
<resteasy.version>3.6.3.Final</resteasy.version>
<bolt.version>1.6.6</bolt.version>
<bolt.version>1.6.10</bolt.version>
<tracer.version>3.0.8</tracer.version>
<lookout.version>1.4.1</lookout.version>
<bytebuddy.version>1.9.8</bytebuddy.version>
Expand Down
4 changes: 2 additions & 2 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
<!-- Test libs -->
<junit.version>4.13.1</junit.version>
<!-- alipay libs -->
<bolt.version>1.6.6</bolt.version>
<sofa.common.tools.version>1.3.15</sofa.common.tools.version>
<bolt.version>1.6.10</bolt.version>
<sofa.common.tools.version>1.4.0</sofa.common.tools.version>
<tracer.version>3.0.8</tracer.version>
<lookout.version>1.4.1</lookout.version>
<!-- Build args -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.event.EventBus;
import com.alipay.sofa.rpc.event.ProviderProcessorRegisterEvent;
import com.alipay.sofa.rpc.event.ProviderProcessorUnRegistryEvent;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.listener.ConfigListener;
Expand Down Expand Up @@ -175,6 +178,9 @@ private void doExport() {
Server server = serverConfig.buildIfAbsent();
// 注册请求调用器
server.registerProcessor(providerConfig, providerProxyInvoker);
if (EventBus.isEnable(ProviderProcessorRegisterEvent.class)) {
EventBus.post(new ProviderProcessorRegisterEvent(providerConfig, serverConfig));
}
if (serverConfig.isAutoStart()) {
server.start();
}
Expand Down Expand Up @@ -306,6 +312,9 @@ public void unExport() {
if (server != null) {
try {
server.unRegisterProcessor(providerConfig, serverConfig.isAutoStart());
if (EventBus.isEnable(ProviderProcessorUnRegistryEvent.class)) {
EventBus.post(new ProviderProcessorUnRegistryEvent(providerConfig, serverConfig));
}
} catch (Exception e) {
if (LOGGER.isWarnEnabled(appName)) {
// TODO WARN
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.context;

import com.alipay.sofa.common.insight.RecordContext;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.core.request.SofaRequest;

import java.util.Map;

/**
* @author Even
* @date 2024/4/29 21:03
*/
public class RecordContextResolver {

public static void carryWithRequest(RecordContext recordContext, SofaRequest sofaRequest) {
recordContext.setTargetServiceUniqueName(sofaRequest.getTargetServiceUniqueName());
recordContext.setMethodName(sofaRequest.getMethodName());
Object traceContext = sofaRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME);
if (traceContext instanceof Map) {
Map<String, String> ctxMap = (Map<String, String>) traceContext;
String traceId = ctxMap.get(RemotingConstants.TRACE_ID_KEY);
String rpcId = ctxMap.get(RemotingConstants.RPC_ID_KEY);
recordContext.setTraceId(traceId);
recordContext.setRpcId(rpcId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.event;

import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;

/**
* @author Even
* @date 2024/4/28 17:18
*/
public class ProviderProcessorRegisterEvent implements Event {

private final ProviderConfig providerConfig;

private final ServerConfig serverConfig;

public ProviderProcessorRegisterEvent(ProviderConfig providerConfig, ServerConfig serverConfig) {
this.providerConfig = providerConfig;
this.serverConfig = serverConfig;
}

public ProviderConfig getProviderConfig() {
return providerConfig;
}

public ServerConfig getServerConfig() {
return serverConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.event;

import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;

/**
* @author Even
* @date 2024/4/28 17:19
*/
public class ProviderProcessorUnRegistryEvent implements Event {

private final ProviderConfig providerConfig;

private final ServerConfig serverConfig;

public ProviderProcessorUnRegistryEvent(ProviderConfig providerConfig, ServerConfig serverConfig) {
this.providerConfig = providerConfig;
this.serverConfig = serverConfig;
}

public ProviderConfig getProviderConfig() {
return providerConfig;
}

public ServerConfig getServerConfig() {
return serverConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.UserThreadPoolManager;
import com.alipay.sofa.rpc.context.RecordContextResolver;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
Expand Down Expand Up @@ -211,6 +212,7 @@ public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SofaRequest
LOGGER.errorWithApp(appName, e.getMessage(), e);
}
} finally {
RecordContextResolver.carryWithRequest(bizCtx.getInvokeContext().getRecordContext(), request);
processingCount.decrementAndGet();
if (!isAsyncChain) {
if (EventBus.isEnable(ServerEndHandleEvent.class)) {
Expand Down

0 comments on commit 6b5f639

Please sign in to comment.