Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -41,9 +41,6 @@
import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
import org.apache.servicecomb.foundation.common.utils.EventUtils;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.metrics.MetricsServoRegistry;
import org.apache.servicecomb.foundation.metrics.performance.QueueMetrics;
import org.apache.servicecomb.foundation.metrics.performance.QueueMetricsData;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
import org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream;
Expand Down Expand Up @@ -113,8 +110,6 @@ protected void scheduleInvocation() {
InvocationType.PRODUCER, System.nanoTime());
EventUtils.triggerEvent(startedEvent);

QueueMetrics metricsData = initMetrics(operationMeta);

operationMeta.getExecutor().execute(() -> {
synchronized (this.requestEx) {
try {
Expand All @@ -127,7 +122,7 @@ protected void scheduleInvocation() {
return;
}

runOnExecutor(metricsData, startedEvent);
runOnExecutor(startedEvent);
} catch (Throwable e) {
LOGGER.error("rest server onRequest error", e);
sendFailResponse(e);
Expand All @@ -136,13 +131,10 @@ protected void scheduleInvocation() {
});
}

protected void runOnExecutor(QueueMetrics metricsData, InvocationStartedEvent startedEvent) {
protected void runOnExecutor(InvocationStartedEvent startedEvent) {
Object[] args = RestCodec.restToArgs(requestEx, restOperationMeta);
createInvocation(args);

this.invocation.setMetricsData(metricsData);
updateMetrics();

//立刻设置开始时间,否则Finished时无法计算TotalTime
invocation.setStartTime(startedEvent.getStartedTime());
invocation.triggerStartProcessingEvent();
Expand Down Expand Up @@ -190,7 +182,6 @@ protected void doInvoke() throws Throwable {
sendResponseQuietly(resp);

invocation.triggerFinishedEvent(resp.getStatusCode(), resp.isSuccessed());
endMetrics();
});
}

Expand Down Expand Up @@ -245,52 +236,4 @@ protected void sendResponse(Response response) throws Exception {
responseEx.flushBuffer();
}
}

/**
* Init the metrics. Note down the queue count and start time.
* @param operationMeta Operation data
* @return QueueMetrics
*/
private QueueMetrics initMetrics(OperationMeta operationMeta) {
QueueMetrics metricsData = new QueueMetrics();
metricsData.setQueueStartTime(System.currentTimeMillis());
metricsData.setOperQualifiedName(operationMeta.getMicroserviceQualifiedName());
QueueMetricsData reqQueue = MetricsServoRegistry.getOrCreateLocalMetrics()
.getOrCreateQueueMetrics(operationMeta.getMicroserviceQualifiedName());
reqQueue.incrementCountInQueue();
return metricsData;
}

/**
* Update the queue metrics.
*/
private void updateMetrics() {
QueueMetrics metricsData = (QueueMetrics) this.invocation.getMetricsData();
if (null != metricsData) {
metricsData.setQueueEndTime(System.currentTimeMillis());
QueueMetricsData reqQueue = MetricsServoRegistry.getOrCreateLocalMetrics()
.getOrCreateQueueMetrics(restOperationMeta.getOperationMeta().getMicroserviceQualifiedName());
reqQueue.incrementTotalCount();
Long timeInQueue = metricsData.getQueueEndTime() - metricsData.getQueueStartTime();
reqQueue.setTotalTime(reqQueue.getTotalTime() + timeInQueue);
reqQueue.setMinLifeTimeInQueue(timeInQueue);
reqQueue.setMaxLifeTimeInQueue(timeInQueue);
reqQueue.decrementCountInQueue();
}
}

/**
* Prepare the end time of queue metrics.
*/
private void endMetrics() {
QueueMetrics metricsData = (QueueMetrics) this.invocation.getMetricsData();
if (null != metricsData) {
metricsData.setEndOperTime(System.currentTimeMillis());
QueueMetricsData reqQueue = MetricsServoRegistry.getOrCreateLocalMetrics()
.getOrCreateQueueMetrics(restOperationMeta.getOperationMeta().getMicroserviceQualifiedName());
reqQueue.incrementTotalServExecutionCount();
reqQueue.setTotalServExecutionTime(
reqQueue.getTotalServExecutionTime() + (metricsData.getEndOperTime() - metricsData.getQueueEndTime()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.metrics.performance.QueueMetrics;
import org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
Expand Down Expand Up @@ -229,7 +228,7 @@ public void invokeFilterHaveResponse(@Mocked HttpServerFilter filter) {
Holder<Response> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
result.value = Response.ok("not run to here");
}

Expand Down Expand Up @@ -258,7 +257,7 @@ public void invokeFilterNoResponse(@Mocked HttpServerFilter filter) {
Holder<Boolean> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
result.value = true;
}
};
Expand Down Expand Up @@ -288,7 +287,7 @@ public void sendFailResponse(Throwable throwable) {
}

@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {

}
};
Expand All @@ -311,7 +310,7 @@ public void invokeNormal(@Mocked HttpServerFilter filter) {

restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
}

@Override
Expand All @@ -338,7 +337,7 @@ public void sendFailResponseHaveProduceProcessor() {
Holder<Response> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
}

@Override
Expand All @@ -360,11 +359,11 @@ public void sendResponseQuietlyNormal(@Mocked Response response) {
Holder<Response> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
}

@Override
protected void sendResponse(Response response) throws Exception {
protected void sendResponse(Response response) {
result.value = response;
}
};
Expand All @@ -379,11 +378,11 @@ protected void sendResponse(Response response) throws Exception {
public void sendResponseQuietlyException(@Mocked Response response) {
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void doInvoke() throws Throwable {
protected void doInvoke() {
}

@Override
protected void sendResponse(Response response) throws Exception {
protected void sendResponse(Response response) {
throw new Error("");
}
};
Expand Down Expand Up @@ -623,7 +622,7 @@ public void scheduleInvocationException(@Mocked OperationMeta operationMeta) {
Error error = new Error("run on executor");
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void runOnExecutor(QueueMetrics metricsData, InvocationStartedEvent startedEvent) {
protected void runOnExecutor(InvocationStartedEvent startedEvent) {
throw error;
}

Expand Down Expand Up @@ -660,7 +659,7 @@ public void scheduleInvocationTimeout(@Mocked OperationMeta operationMeta) {

restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void runOnExecutor(QueueMetrics metricsData, InvocationStartedEvent startedEvent) {
protected void runOnExecutor(InvocationStartedEvent startedEvent) {
throw new Error("run on executor");
}

Expand Down Expand Up @@ -696,7 +695,7 @@ public void scheduleInvocationNormal(@Mocked OperationMeta operationMeta) {
Holder<Boolean> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void runOnExecutor(QueueMetrics metricsData, InvocationStartedEvent startedEvent) {
protected void runOnExecutor(InvocationStartedEvent startedEvent) {
result.value = true;
}
};
Expand Down Expand Up @@ -727,7 +726,7 @@ public void invoke() {
restInvocation.requestEx = requestEx;
restInvocation.restOperationMeta = restOperation;

restInvocation.runOnExecutor(null, new InvocationStartedEvent("", InvocationType.PRODUCER, System.nanoTime()));
restInvocation.runOnExecutor(new InvocationStartedEvent("", InvocationType.PRODUCER, System.nanoTime()));
Assert.assertTrue(result.value);
Assert.assertSame(invocation, restInvocation.invocation);
}
Expand All @@ -738,7 +737,7 @@ public void doInvoke(@Mocked Endpoint endpoint, @Mocked OperationMeta operationM
Response response = Response.ok("ok");
Handler handler = new Handler() {
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
public void handle(Invocation invocation, AsyncResponse asyncResp) {
asyncResp.complete(response);
}
};
Expand Down
11 changes: 0 additions & 11 deletions core/src/main/java/org/apache/servicecomb/core/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ public class Invocation extends SwaggerInvocation {

private int handlerIndex;

//start,end of queue and operation time after queue for operation level metrics.
private Object metricsData;

public Object getMetricsData() {
return metricsData;
}

public void setMetricsData(Object metricsData) {
this.metricsData = metricsData;
}


// 应答的处理器
// 同步模式:避免应答在网络线程中处理解码等等业务级逻辑
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,6 @@ public static void run() {
testController();
}

//0.5.0 version metrics integration test
try {
// this test class is intended for retry hanging issue JAV-127
String content = restTemplate.getForObject("cse://springmvc/codeFirstSpringmvc/metricsForTest", String.class);
@SuppressWarnings("unchecked")
Map<String, String> resultMap = JsonUtils.OBJ_MAPPER.readValue(content, HashMap.class);

TestMgr.check(true, resultMap.get("CPU and Memory").contains("heapUsed="));

String[] requestProviders = resultMap.get("totalRequestProvider OPERATIONAL_LEVEL")
.replace("{", "")
.replace("}", "")
.split(",");
Map<String, Integer> requests = new HashMap<>();
for (String requestProvider : requestProviders) {
String[] requestKeyAndValues = requestProvider.split("=");
requests.put(requestKeyAndValues[0], Integer.parseInt(requestKeyAndValues[1]));
}

for (Entry<String, Integer> request : requests.entrySet()) {
TestMgr.check(true, request.getValue() > 0);
}

TestMgr.check(true, resultMap.get("RequestQueueRelated").contains("springmvc.codeFirst.saySomething"));
TestMgr.check(true, resultMap.get("RequestQueueRelated").contains("springmvc.controller.sayHi"));
} catch (Exception e) {
TestMgr.check("true", "false");
}

//0.5.0 later version metrics integration test
try {
RegistryMetric metric = metricsPublisher.metrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -39,8 +38,6 @@
import org.apache.servicecomb.demo.ignore.InputModelForTestIgnore;
import org.apache.servicecomb.demo.ignore.OutputModelForTestIgnore;
import org.apache.servicecomb.demo.server.User;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.metrics.MetricsServoRegistry;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.extend.annotations.RawJsonRequestBody;
import org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders;
Expand All @@ -49,7 +46,6 @@
import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.apache.servicecomb.swagger.invocation.response.Headers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -72,9 +68,6 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.netflix.servo.monitor.Monitor;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
Expand All @@ -87,20 +80,12 @@
@RequestMapping(path = "/codeFirstSpringmvc", produces = MediaType.APPLICATION_JSON_VALUE)
public class CodeFirstSpringmvc {

private MetricsServoRegistry registry;

@Autowired
public CodeFirstSpringmvc(MetricsServoRegistry registry) {
this.registry = registry;
}


private String _fileUpload(MultipartFile file1, Part file2) {
try (InputStream is1 = file1.getInputStream(); InputStream is2 = file2.getInputStream()) {
String content1 = IOUtils.toString(is1);
String content2 = IOUtils.toString(is2);
return String.format("%s:%s:%s\n"
+ "%s:%s:%s",
+ "%s:%s:%s",
file1.getOriginalFilename(),
file1.getContentType(),
content1,
Expand Down Expand Up @@ -135,7 +120,7 @@ public ResponseEntity<Date> responseEntity(InvocationContext c1, @RequestAttribu
InvocationContext c2 = ContextUtils.getInvocationContext();
headers.add("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());

return new ResponseEntity<Date>(date, headers, HttpStatus.ACCEPTED);
return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
}

@ResponseHeaders({@ResponseHeader(name = "h1", response = String.class),
Expand All @@ -148,7 +133,7 @@ public ResponseEntity<Date> responseEntityPATCH(InvocationContext c1, @RequestAt
InvocationContext c2 = ContextUtils.getInvocationContext();
headers.add("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());

return new ResponseEntity<Date>(date, headers, HttpStatus.ACCEPTED);
return new ResponseEntity<>(date, headers, HttpStatus.ACCEPTED);
}

@ApiResponse(code = 200, response = User.class, message = "")
Expand Down Expand Up @@ -313,7 +298,7 @@ public OutputModelForTestIgnore testModelWithIgnoreField(@RequestBody InputModel
return new OutputModelForTestIgnore("output_id", input.getInputId(), input.getContent(), input.getInputObject(),
input.getInputJsonObject(), input.getInputIgnoreInterface(),
new Person("outputSomeone"), new JsonObject("{\"OutputJsonKey\" : \"OutputJsonValue\"}"), () -> {
});
});
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -343,21 +328,6 @@ public String testform(HttpServletRequest request) {
return form1 + form2;
}

//Only for 0.5.0 Integration Test
@RequestMapping(path = "/metricsForTest", method = RequestMethod.GET)
public String metricsForTest() {
List<Monitor<?>> monitors = registry.getMetricsMonitors();
Map<String, String> values = new HashMap<>();
for (Monitor<?> monitor : monitors) {
values.put(monitor.getConfig().getName(), monitor.getValue().toString());
}
try {
return JsonUtils.writeValueAsString(values);
} catch (JsonProcessingException e) {
throw new InvocationException(500, "500", "JsonProcessingException", e);
}
}

//Only for Prometheus integration test
@RequestMapping(path = "/prometheusForTest", method = RequestMethod.GET)
public String prometheusForTest() {
Expand Down
Loading