Skip to content

Commit

Permalink
Fix rest impl time. (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
leizhiyuan authored and ujjboy committed Nov 30, 2018
1 parent 5d0d8a8 commit 7fc2357
Show file tree
Hide file tree
Showing 25 changed files with 336 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.alipay.sofa.rpc.client.lb;

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -28,6 +30,8 @@
*/
public abstract class BaseLoadBalancerTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(BaseLoadBalancerTest.class);

protected List<ProviderInfo> buildSameWeightProviderList(int size) {
List<ProviderInfo> aliveConnections = new ArrayList<ProviderInfo>();
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -31,6 +33,7 @@
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class ConsistentHashLoadBalancerTest extends BaseLoadBalancerTest {

@Test
public void doSelect() throws Exception {

Expand All @@ -53,8 +56,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int count = 0;
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

for (int i = 0; i < size; i++) {
Assert.assertTrue(cnt.get(9000 + i) == 0);
Expand Down Expand Up @@ -98,8 +98,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

for (int i = 0; i < size; i++) {
Assert.assertTrue(cnt.get(9000 + i) == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -75,8 +75,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

Assert.assertTrue(cnt.get(9000) == 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -73,8 +73,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

// 忽略了权重
int avg = total / size;
Expand All @@ -94,7 +94,7 @@ public static void main(String[] args) {
y = a[i + 1];
temp = gcd(x, y);
}
System.out.println(temp);
LOGGER.info(String.valueOf(temp));
}

public static int gcd(int a, int b) {
Expand All @@ -105,7 +105,7 @@ public static int gcd(int a, int b) {
b = temp;
System.out.println("gcd(" + a + ", " + b + ")=");
}
System.out.println(a);
LOGGER.info(String.valueOf(a));
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -31,6 +33,9 @@
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class WeightRoundRobinLoadBalancerTest extends BaseLoadBalancerTest {

private static final Logger LOGGER = LoggerFactory.getLogger(WeightRoundRobinLoadBalancerTest.class);

@Test
public void doSelect() throws Exception {
WeightRoundRobinLoadBalancer loadBalancer = new WeightRoundRobinLoadBalancer(null);
Expand All @@ -51,8 +56,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -73,8 +78,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

Assert.assertTrue(cnt.get(9000) == 0);
int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class WeightRoundRobinLoadBalancerV2Test {
public class WeightRoundRobinLoadBalancerV2Test extends BaseLoadBalancerTest {

/**
* 上次选择的服务器
Expand Down Expand Up @@ -131,7 +131,7 @@ public static void main(String args[]) {

for (int i = 0; i < 15; i++) {
Server server = weightRoundRobin.getServer();
System.out.println("server " + server.getIp() + " weight=" + server.getWeight());
LOGGER.info("server " + server.getIp() + " weight=" + server.getWeight());
}
}

Expand Down
2 changes: 1 addition & 1 deletion core-impl/log/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
2 changes: 1 addition & 1 deletion core-impl/proxy/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ public class RpcConstants {
* @since 5.4.0
*/
public static final String INTERNAL_KEY_CLIENT_SEND_TIME = INTERNAL_KEY_PREFIX + "client_send_time";

/**
* 内部使用的key:_server_receive_time 服务端接收时间戳,long
*
* @since 5.4.8
*/
public static final String INTERNAL_KEY_SERVER_RECEIVE_TIME = INTERNAL_KEY_PREFIX + "server_receive_time";

/**
* 内部使用的key:_router_record 路由记录,string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.alipay.sofa.rpc.client.aft.bean.HelloServiceTimeOutImpl;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.After;
import org.junit.Before;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.module.FaultToleranceModule;
import com.alipay.sofa.rpc.module.Module;
import org.junit.After;
Expand All @@ -42,6 +44,9 @@
* @version $Id: FaultBaseTest.java, v 0.1 2017年11月16日 下午3:14 bystander Exp $
*/
public abstract class FaultBaseTest {

private static final Logger LOGGER = LoggerFactory.getLogger(FaultBaseTest.class);

public static final String APP_NAME1 = "testApp";
public static final String APP_NAME2 = "testAnotherApp";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
2 changes: 1 addition & 1 deletion extension-impl/remoting-bolt/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
2 changes: 1 addition & 1 deletion extension-impl/remoting-http/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
Expand All @@ -36,11 +37,16 @@ public class TraceResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {

// 补充服务端request和response大小
if (RpcInternalContext.isAttachmentEnable()) {
RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, requestContext.getLength());
context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, responseContext.getLength());
Long startTime = (Long) context.removeAttachment(RpcConstants.INTERNAL_KEY_SERVER_RECEIVE_TIME);
if (startTime != null) {
context.setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE, RpcRuntimeContext.now() - startTime);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@
import static com.alipay.sofa.rpc.common.RpcConstants.INTERNAL_KEY_APP_NAME;

/**
*
* 客户端:startRpc ——> filter --> beforeSend --> 存入tracer信息 --> clientReceived
* 服务端:serverReceived --> filter --> serverSend
*
*
* @author liangen
* @version $Id: RestTracerAdapter.java, v 0.1 2017年11月29日 上午9:56 liangen Exp $
*/
Expand All @@ -65,7 +63,7 @@ public class RestTracerAdapter {

/**
* 存入tracer信息
*
*
* @param requestContext ClientRequestContext
*/
public static void storeTracerInfo(ClientRequestContext requestContext) {
Expand Down Expand Up @@ -126,7 +124,7 @@ public static void serverReceived(NettyHttpRequest request) {

/**
* 适配服务端filter
*
*
* @param requestContext ContainerRequestContext
*/
public static void serverFilter(ContainerRequestContext requestContext) {
Expand All @@ -135,6 +133,9 @@ public static void serverFilter(ContainerRequestContext requestContext) {
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
RpcInternalContext context = RpcInternalContext.getContext();

context.setAttachment(RpcConstants.INTERNAL_KEY_SERVER_RECEIVE_TIME, RpcRuntimeContext.now());

SofaResourceMethodInvoker resourceMethodInvoker = (SofaResourceMethodInvoker)
((PostMatchContainerRequestContext) requestContext)
.getResourceMethod();
Expand Down Expand Up @@ -201,6 +202,16 @@ public static void serverSend(NettyHttpResponse response, Throwable throwable) {
}
}

SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();

RpcInternalContext context = RpcInternalContext.getContext();

if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME,
(Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}

Tracers.serverSend(sofaRequest, sofaResponse, null);
} catch (Throwable t) {
if (LOGGER.isWarnEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
*/
package com.alipay.sofa.rpc.transport.rest;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.tracer.sofatracer.RestTracerAdapter;

import javax.annotation.Priority;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

/**
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">zhanggeng</a>
* @author <a href="mailto:lw111072@alibaba-inc.com">liangen</a>
*/
Expand All @@ -40,6 +42,15 @@ public class TraceClientRequestFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
try {

if (RpcInternalContext.isAttachmentEnable()) {
// 补充客户端request长度
RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE,
requestContext.getHeaderString(HttpHeaders.CONTENT_LENGTH));

}

RestTracerAdapter.storeTracerInfo(requestContext);
} catch (Exception e) {
logger.error("the process of rest tracer client request occur error ", e);
Expand Down
Loading

0 comments on commit 7fc2357

Please sign in to comment.