Skip to content

Commit 0581ef6

Browse files
authored
Bump up the API of BanyanDB Server to support the query trace. (#59)
1 parent 595b4a8 commit 0581ef6

File tree

9 files changed

+109
-2
lines changed

9 files changed

+109
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ Changes by Version
22
==================
33
Release Notes.
44

5-
0.7.0
5+
0.7.0-rc0
66
------------------
77

88
### Features
99

10+
* Bump up the API of BanyanDB Server to support the query trace.
1011

1112
### Bugs
1213

src/main/java/org/apache/skywalking/banyandb/v1/client/AbstractQuery.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public abstract class AbstractQuery<T> {
6262
*/
6363
protected AbstractCriteria criteria;
6464

65+
/**
66+
* Enable or disable trace.
67+
*/
68+
protected boolean trace;
69+
6570
public AbstractQuery(List<String> groups, String name, TimestampRange timestampRange, Set<String> tagProjections) {
6671
this.groups = groups;
6772
this.name = name;
@@ -100,6 +105,14 @@ public AbstractQuery<T> criteria(AbstractCriteria criteria) {
100105
return this;
101106
}
102107

108+
/**
109+
* Enable trace for the query.
110+
*/
111+
public AbstractQuery<T> enableTrace() {
112+
this.trace = true;
113+
return this;
114+
}
115+
103116
/**
104117
* @return QueryRequest for gRPC level query.
105118
* @throws BanyanDBException thrown from entity build, e.g. invalid reference to non-exist fields or tags.

src/main/java/org/apache/skywalking/banyandb/v1/client/MeasureQuery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ BanyandbMeasure.QueryRequest build(MetadataCache.EntityMetadata entityMetadata)
200200
}
201201
// add all criteria
202202
buildCriteria().ifPresent(builder::setCriteria);
203+
builder.setTrace(this.trace);
203204
return builder.build();
204205
}
205206

src/main/java/org/apache/skywalking/banyandb/v1/client/StreamQuery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ BanyandbStream.QueryRequest build(MetadataCache.EntityMetadata entityMetadata) t
8282
if (orderBy != null) {
8383
builder.setOrderBy(orderBy.build());
8484
}
85+
builder.setTrace(this.trace);
8586
return builder.build();
8687
}
8788
}

src/main/proto/banyandb/v1/banyandb-common.proto

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,40 @@ message Group {
7777
// updated_at indicates when resources of the group are updated
7878
google.protobuf.Timestamp updated_at = 4;
7979
}
80+
81+
82+
// Trace is the top level message of a trace.
83+
message Trace {
84+
// trace_id is the unique identifier of the trace.
85+
string trace_id = 1;
86+
// spans is a list of spans in the trace.
87+
repeated Span spans = 2;
88+
// error indicates whether the trace is an error trace.
89+
bool error = 3;
90+
}
91+
92+
// Span is the basic unit of a trace.
93+
message Span {
94+
// start_time is the start time of the span.
95+
google.protobuf.Timestamp start_time = 1;
96+
// end_time is the end time of the span.
97+
google.protobuf.Timestamp end_time = 2;
98+
// error indicates whether the span is an error span.
99+
bool error = 3;
100+
// tags is a list of tags of the span.
101+
repeated Tag tags = 4;
102+
// message is the message generated by the span.
103+
string message = 5;
104+
// children is a list of child spans of the span.
105+
repeated Span children = 6;
106+
// duration is the duration of the span.
107+
int64 duration = 7;
108+
}
109+
110+
// Tag is the key-value pair of a span.
111+
message Tag {
112+
// key is the key of the tag.
113+
string key = 1;
114+
// value is the value of the tag.
115+
string value = 2;
116+
}

src/main/proto/banyandb/v1/banyandb-measure.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ message DataPoint {
4444
message QueryResponse {
4545
// data_points are the actual data returned
4646
repeated DataPoint data_points = 1;
47+
// trace contains the trace information of the query when trace is enabled
48+
common.v1.Trace trace = 2;
4749
}
4850

49-
// QueryRequest is the request contract for query.
5051
// QueryRequest is the request contract for query.
5152
message QueryRequest {
5253
// groups indicate where the data points are stored.
@@ -101,6 +102,8 @@ message QueryRequest {
101102
uint32 limit = 11;
102103
// order_by is given to specify the sort for a tag.
103104
model.v1.QueryOrder order_by = 12;
105+
// trace is used to enable trace for the query
106+
bool trace = 13;
104107
}
105108

106109
// TopNList contains a series of topN items

src/main/proto/banyandb/v1/banyandb-stream.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ message Element {
4949
message QueryResponse {
5050
// elements are the actual data returned
5151
repeated Element elements = 1;
52+
// trace contains the trace information of the query when trace is enabled
53+
common.v1.Trace trace = 2;
5254
}
5355

5456
// QueryRequest is the request contract for query.
@@ -72,6 +74,8 @@ message QueryRequest {
7274
model.v1.Criteria criteria = 7;
7375
// projection can be used to select the key names of the element in the response
7476
model.v1.TagProjection projection = 8 [(validate.rules).message.required = true];
77+
// trace is used to enable trace for the query
78+
bool trace = 9;
7579
}
7680

7781
message ElementValue {

src/test/java/org/apache/skywalking/banyandb/v1/client/BanyanDBClientMeasureQueryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public void testQuery_tableScan() throws BanyanDBException {
116116
parseProjectionList(request.getTagProjection()));
117117
assertCollectionEqual(Lists.newArrayList("total"),
118118
request.getFieldProjection().getNamesList());
119+
Assert.assertFalse(request.getTrace());
119120
}
120121

121122
@Test
@@ -152,6 +153,28 @@ public void testQuery_responseConversion() {
152153
(Number) resp.getDataPoints().get(0).getFieldValue("total"));
153154
}
154155

156+
@Test
157+
public void testQuery_enableTrace() throws BanyanDBException {
158+
ArgumentCaptor<BanyandbMeasure.QueryRequest> requestCaptor = ArgumentCaptor.forClass(BanyandbMeasure.QueryRequest.class);
159+
160+
Instant end = Instant.now();
161+
Instant begin = end.minus(15, ChronoUnit.MINUTES);
162+
MeasureQuery query = new MeasureQuery(Lists.newArrayList("sw_metric"), "service_cpm_minute",
163+
new TimestampRange(begin.toEpochMilli(), end.toEpochMilli()),
164+
ImmutableSet.of("entity_id"),
165+
ImmutableSet.of("total"));
166+
query.maxBy("total", ImmutableSet.of("entity_id"));
167+
// search with conditions
168+
query.and(PairQueryCondition.StringQueryCondition.eq("entity_id", "abc"));
169+
query.enableTrace();
170+
client.query(query);
171+
172+
verify(measureQueryService).query(requestCaptor.capture(), ArgumentMatchers.any());
173+
174+
final BanyandbMeasure.QueryRequest request = requestCaptor.getValue();
175+
Assert.assertTrue(request.getTrace());
176+
}
177+
155178
static <T> void assertCollectionEqual(Collection<T> c1, Collection<T> c2) {
156179
Assert.assertTrue(c1.size() == c2.size() && c1.containsAll(c2) && c2.containsAll(c1));
157180
}

src/test/java/org/apache/skywalking/banyandb/v1/client/BanyanDBClientStreamQueryTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public void testQuery_tableScan() throws BanyanDBException {
126126
// assert projections
127127
assertCollectionEqual(Lists.newArrayList("searchable:duration", "searchable:state", "searchable:start_time", "searchable:trace_id"),
128128
parseProjectionList(request.getProjection()));
129+
Assert.assertFalse(request.getTrace());
129130
}
130131

131132
@Test
@@ -279,6 +280,7 @@ public void testQuery_indexScan() throws BanyanDBException {
279280
Assert.assertEquals("start_time", request.getOrderBy().getIndexRuleName());
280281
// assert projections
281282
assertCollectionEqual(Lists.newArrayList("searchable:duration", "searchable:state", "searchable:start_time", "searchable:trace_id"), parseProjectionList(request.getProjection()));
283+
Assert.assertFalse(request.getTrace());
282284
}
283285

284286
@Test
@@ -305,6 +307,7 @@ public void testQuery_TraceIDFetch() throws BanyanDBException {
305307
" }\n" +
306308
" }\n" +
307309
"}\n", request.getCriteria().toString());
310+
Assert.assertFalse(request.getTrace());
308311
}
309312

310313
@Test
@@ -361,6 +364,27 @@ public void testQuery_responseConversion() throws BanyanDBException {
361364
resp.getElements().get(0).getTagValue("data_binary"));
362365
}
363366

367+
@Test
368+
public void testQuery_enableTrace() throws BanyanDBException {
369+
ArgumentCaptor<BanyandbStream.QueryRequest> requestCaptor = ArgumentCaptor.forClass(BanyandbStream.QueryRequest.class);
370+
371+
Instant end = Instant.now();
372+
Instant begin = end.minus(15, ChronoUnit.MINUTES);
373+
StreamQuery query = new StreamQuery(Lists.newArrayList("default"), "sw",
374+
new TimestampRange(begin.toEpochMilli(), end.toEpochMilli()),
375+
ImmutableSet.of("state", "start_time", "duration", "trace_id"));
376+
// search for all states
377+
query.and(PairQueryCondition.LongQueryCondition.eq("state", 0L));
378+
query.setOrderBy(new StreamQuery.OrderBy("duration", AbstractQuery.Sort.DESC));
379+
query.enableTrace();
380+
client.query(query);
381+
382+
verify(streamQueryServiceImpl).query(requestCaptor.capture(), ArgumentMatchers.any());
383+
384+
final BanyandbStream.QueryRequest request = requestCaptor.getValue();
385+
Assert.assertTrue(request.getTrace());
386+
}
387+
364388
static <T> void assertCollectionEqual(Collection<T> c1, Collection<T> c2) {
365389
Assert.assertTrue(c1.size() == c2.size() && c1.containsAll(c2) && c2.containsAll(c1));
366390
}

0 commit comments

Comments
 (0)