提交 58ede1d0 编写于 作者: A Ajanthan 提交者: wu-sheng

Fixing #1231. Adding order and status to trace query. (#1255)

* Fixing #1231. Adding order and status to trace query.

* Fixing 1231. Using enum for traceState and queryOrder

* Incorporating code review suggestions
上级 a835fe4e
......@@ -19,7 +19,9 @@
package org.apache.skywalking.apm.collector.storage.dao.ui;
import org.apache.skywalking.apm.collector.storage.base.dao.DAO;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
/**
* Interface to be implemented for execute database query operation
......@@ -55,5 +57,5 @@ public interface ISegmentDurationUIDAO extends DAO {
* @return not nullable result list
*/
TraceBrief loadTop(long startSecondTimeBucket, long endSecondTimeBucket, long minDuration, long maxDuration,
String operationName, int applicationId, int limit, int from, String... segmentIds);
String operationName, int applicationId, int limit, int from, TraceState traceState, QueryOrder queryOrder, String... segmentIds);
}
/*
* 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 org.apache.skywalking.apm.collector.storage.ui.trace;
public enum QueryOrder {
BY_START_TIME,
BY_DURATION
}
......@@ -31,6 +31,25 @@ public class TraceQueryCondition {
private Duration queryDuration;
private int minTraceDuration;
private int maxTraceDuration;
private TraceState traceState;
private QueryOrder queryOrder;
public TraceState getTraceState() {
return traceState;
}
public void setTraceState(TraceState traceState) {
this.traceState = traceState;
}
public QueryOrder getQueryOrder() {
return queryOrder;
}
public void setQueryOrder(QueryOrder queryOrder) {
this.queryOrder = queryOrder;
}
private Pagination paging;
public int getApplicationId() {
......
/*
* 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 org.apache.skywalking.apm.collector.storage.ui.trace;
public enum TraceState {
ALL,
SUCCESS,
ERROR
}
......@@ -27,7 +27,9 @@ import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentDurationUIDAO;
import org.apache.skywalking.apm.collector.storage.es.base.dao.EsDAO;
import org.apache.skywalking.apm.collector.storage.table.segment.SegmentDurationTable;
import org.apache.skywalking.apm.collector.storage.ui.trace.BasicTrace;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
......@@ -49,7 +51,7 @@ public class SegmentDurationEsUIDAO extends EsDAO implements ISegmentDurationUID
@Override
public TraceBrief loadTop(long startSecondTimeBucket, long endSecondTimeBucket, long minDuration, long maxDuration,
String operationName, int applicationId, int limit, int from, String... segmentIds) {
String operationName, int applicationId, int limit, int from, TraceState traceState, QueryOrder queryOrder, String... segmentIds) {
SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(SegmentDurationTable.TABLE);
searchRequestBuilder.setTypes(SegmentDurationTable.TABLE_TYPE);
searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
......@@ -81,8 +83,22 @@ public class SegmentDurationEsUIDAO extends EsDAO implements ISegmentDurationUID
if (applicationId != 0) {
boolQueryBuilder.must().add(QueryBuilders.termQuery(SegmentDurationTable.APPLICATION_ID.getName(), applicationId));
}
searchRequestBuilder.addSort(SegmentDurationTable.START_TIME.getName(), SortOrder.DESC);
switch (traceState) {
case ERROR:
mustQueryList.add(QueryBuilders.matchQuery(SegmentDurationTable.IS_ERROR.getName(), BooleanUtils.TRUE));
break;
case SUCCESS:
mustQueryList.add(QueryBuilders.matchQuery(SegmentDurationTable.IS_ERROR.getName(), BooleanUtils.FALSE));
break;
}
switch (queryOrder) {
case BY_START_TIME:
searchRequestBuilder.addSort(SegmentDurationTable.START_TIME.getName(), SortOrder.DESC);
break;
case
BY_DURATION:searchRequestBuilder.addSort(SegmentDurationTable.DURATION.getName(), SortOrder.DESC);
break;
}
searchRequestBuilder.setSize(limit);
searchRequestBuilder.setFrom(from);
......
......@@ -31,7 +31,9 @@ import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentDurationUIDAO;
import org.apache.skywalking.apm.collector.storage.h2.base.dao.H2DAO;
import org.apache.skywalking.apm.collector.storage.table.segment.SegmentDurationTable;
import org.apache.skywalking.apm.collector.storage.ui.trace.BasicTrace;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -48,7 +50,7 @@ public class SegmentDurationH2UIDAO extends H2DAO implements ISegmentDurationUID
@Override
public TraceBrief loadTop(long startSecondTimeBucket, long endSecondTimeBucket, long minDuration, long maxDuration,
String operationName, int applicationId, int limit, int from, String... segmentIds) {
String operationName, int applicationId, int limit, int from, TraceState traceState, QueryOrder queryOrder, String... segmentIds) {
H2Client client = getClient();
String sql = "select * from {0} where {1} >= ? and {1} <= ?";
List<Object> params = new ArrayList<>();
......@@ -90,8 +92,30 @@ public class SegmentDurationH2UIDAO extends H2DAO implements ISegmentDurationUID
params.add(applicationId);
columns.add(SegmentDurationTable.APPLICATION_ID.getName());
}
if (traceState != null) {
paramIndex++;
sql = sql + " and {" + paramIndex + "} = ?";
switch (traceState) {
case ERROR:
params.add(BooleanUtils.TRUE);
break;
case SUCCESS:
params.add(BooleanUtils.FALSE);
break;
}
columns.add(SegmentDurationTable.IS_ERROR);
}
sql = sql + " limit " + from + "," + limit;
switch (queryOrder) {
case BY_START_TIME:
sql = sql + " queryOrder by " + SegmentDurationTable.START_TIME.getName() + " dsc";
break;
case BY_DURATION:
sql = sql + " queryOrder by " + SegmentDurationTable.DURATION.getName() + " dsc";
break;
}
sql = SqlBuilder.buildSql(sql, columns);
Object[] p = params.toArray(new Object[0]);
......
......@@ -22,9 +22,7 @@ import org.apache.skywalking.apm.collector.core.UnexpectedException;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.core.util.Const;
import org.apache.skywalking.apm.collector.core.util.StringUtils;
import org.apache.skywalking.apm.collector.storage.ui.trace.Trace;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceQueryCondition;
import org.apache.skywalking.apm.collector.storage.ui.trace.*;
import org.apache.skywalking.apm.collector.ui.graphql.Query;
import org.apache.skywalking.apm.collector.ui.service.SegmentTopService;
import org.apache.skywalking.apm.collector.ui.service.TraceStackService;
......@@ -81,9 +79,11 @@ public class TraceQuery implements Query {
long maxDuration = condition.getMaxTraceDuration();
String operationName = condition.getOperationName();
int applicationId = condition.getApplicationId();
TraceState traceState = condition.getTraceState();
QueryOrder queryOrder = condition.getQueryOrder();
PaginationUtils.Page page = PaginationUtils.INSTANCE.exchange(condition.getPaging());
return getSegmentTopService().loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, traceId, applicationId, page.getLimit(), page.getFrom());
return getSegmentTopService().loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, traceId, applicationId, page.getLimit(), page.getFrom(),traceState,queryOrder);
}
public Trace queryTrace(String traceId) {
......
......@@ -25,7 +25,9 @@ import org.apache.skywalking.apm.collector.core.util.StringUtils;
import org.apache.skywalking.apm.collector.storage.StorageModule;
import org.apache.skywalking.apm.collector.storage.dao.ui.IGlobalTraceUIDAO;
import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentDurationUIDAO;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,12 +47,12 @@ public class SegmentTopService {
}
public TraceBrief loadTop(long startSecondTimeBucket, long endSecondTimeBucket, long minDuration, long maxDuration,
String operationName,
String traceId, int applicationId, int limit, int from) {
String operationName,
String traceId, int applicationId, int limit, int from, TraceState traceState, QueryOrder queryOrder) {
logger.debug("startSecondTimeBucket: {}, endSecondTimeBucket: {}, minDuration: {}, " +
"maxDuration: {}, operationName: {}, traceId: {}, applicationId: {}, limit: {}, from: {}",
"maxDuration: {}, operationName: {}, traceId: {}, applicationId: {}, limit: {}, from: {}, traceState: {}, queryOrder: {}",
startSecondTimeBucket, endSecondTimeBucket, minDuration,
maxDuration, operationName, traceId, applicationId, limit, from);
maxDuration, operationName, traceId, applicationId, limit, from,traceState,queryOrder);
TraceBrief traceBrief;
if (StringUtils.isNotEmpty(traceId)) {
......@@ -58,9 +60,9 @@ public class SegmentTopService {
if (CollectionUtils.isEmpty(segmentIds)) {
return new TraceBrief();
}
traceBrief = segmentDurationUIDAO.loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, applicationId, limit, from, segmentIds.toArray(new String[0]));
traceBrief = segmentDurationUIDAO.loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, applicationId, limit, from, traceState, queryOrder, segmentIds.toArray(new String[0]));
} else {
traceBrief = segmentDurationUIDAO.loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, applicationId, limit, from);
traceBrief = segmentDurationUIDAO.loadTop(startSecondTimeBucket, endSecondTimeBucket, minDuration, maxDuration, operationName, applicationId, limit, from, traceState, queryOrder);
}
traceBrief.getTraces().forEach(trace -> {
......
......@@ -21,7 +21,9 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.skywalking.apm.collector.storage.ui.common.Duration;
import org.apache.skywalking.apm.collector.storage.ui.common.Pagination;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceQueryCondition;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
import org.apache.skywalking.apm.collector.ui.service.SegmentTopService;
import org.apache.skywalking.apm.collector.ui.service.TraceStackService;
import org.junit.Assert;
......@@ -61,12 +63,14 @@ public class TraceQueryTest {
duration.setEnd("2017-02");
duration.setStep(Step.MONTH);
traceQueryCondition.setQueryDuration(duration);
traceQueryCondition.setQueryOrder(QueryOrder.BY_START_TIME);
traceQueryCondition.setTraceState(TraceState.ALL);
traceQueryCondition.setPaging(new Pagination());
Mockito.when(segmentTopService.loadTop(
Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyString(), Mockito.anyString(),
Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt()
Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyObject(), Mockito.anyObject()
)
).then(invocation -> {
Object[] arguments = invocation.getArguments();
......
......@@ -24,7 +24,9 @@ import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentDurationUIDAO;
import org.apache.skywalking.apm.collector.storage.ui.common.Duration;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.ui.trace.BasicTrace;
import org.apache.skywalking.apm.collector.storage.ui.trace.QueryOrder;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceState;
import org.apache.skywalking.apm.collector.ui.utils.DurationUtils;
import org.junit.Assert;
import org.junit.Before;
......@@ -68,11 +70,11 @@ public class SegmentTopServiceTest {
long startSecondTimeBucket = DurationUtils.INSTANCE.startTimeDurationToSecondTimeBucket(duration.getStep(), duration.getStart());
long endSecondTimeBucket = DurationUtils.INSTANCE.endTimeDurationToSecondTimeBucket(duration.getStep(), duration.getEnd());
when(globalTraceUIDAO.getSegmentIds(anyString())).then(invocation -> Collections.singletonList("segmentIds"));
when(segmentDurationUIDAO.loadTop(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyInt(), anyInt(), anyInt())).then(invocation -> getTrace());
when(segmentDurationUIDAO.loadTop(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyInt(), anyInt(), anyInt(), anyObject())).then(invocation -> getTrace());
TraceBrief traceBrief = segmentTopService.loadTop(startSecondTimeBucket, endSecondTimeBucket, 0, 1, "test", null, 1, 10, 0);
when(segmentDurationUIDAO.loadTop(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyInt(), anyInt(), anyInt(),anyObject(),anyObject())).then(invocation -> getTrace());
when(segmentDurationUIDAO.loadTop(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyInt(), anyInt(), anyInt(), anyObject(),anyObject(),anyObject())).then(invocation -> getTrace());
TraceBrief traceBrief = segmentTopService.loadTop(startSecondTimeBucket, endSecondTimeBucket, 0, 1, "test", null, 1, 10, 0,TraceState.ALL,QueryOrder.BY_START_TIME);
Assert.assertTrue(traceBrief.getTraces().size() == 1);
traceBrief = segmentTopService.loadTop(startSecondTimeBucket, endSecondTimeBucket, 0, 1, "test", "traceId", 1, 10, 0);
traceBrief = segmentTopService.loadTop(startSecondTimeBucket, endSecondTimeBucket, 0, 1, "test", "traceId", 1, 10, 0,TraceState.ALL,QueryOrder.BY_START_TIME);
Assert.assertTrue(traceBrief.getTraces().size() == 1);
}
......
......@@ -42,9 +42,17 @@ input TraceQueryCondition {
minTraceDuration: Int
# The max time of trace
maxTraceDuration: Int
traceState: TraceState!
queryOrder: QueryOrder!
paging: Pagination!
}
enum TraceState {
ALL
SUCCESS
ERROR
}
enum QueryOrder {
BY_START_TIME
BY_DURATION
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册