43
43
import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .IndexRule ;
44
44
import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .IndexRuleBinding ;
45
45
import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .Subject ;
46
+ import org .apache .skywalking .banyandb .database .v1 .BanyandbDatabase .Trace ;
46
47
import org .apache .skywalking .banyandb .model .v1 .BanyandbModel ;
47
48
import org .apache .skywalking .banyandb .property .v1 .BanyandbProperty ;
48
49
import org .apache .skywalking .banyandb .property .v1 .BanyandbProperty .Property ;
53
54
import org .apache .skywalking .banyandb .measure .v1 .MeasureServiceGrpc ;
54
55
import org .apache .skywalking .banyandb .stream .v1 .BanyandbStream ;
55
56
import org .apache .skywalking .banyandb .stream .v1 .StreamServiceGrpc ;
57
+ import org .apache .skywalking .banyandb .trace .v1 .BanyandbTrace ;
58
+ import org .apache .skywalking .banyandb .trace .v1 .TraceServiceGrpc ;
56
59
import org .apache .skywalking .banyandb .v1 .client .auth .AuthInterceptor ;
57
60
import org .apache .skywalking .banyandb .v1 .client .grpc .HandleExceptionsWith ;
58
61
import org .apache .skywalking .banyandb .v1 .client .grpc .channel .ChannelManager ;
69
72
import org .apache .skywalking .banyandb .v1 .client .metadata .ResourceExist ;
70
73
import org .apache .skywalking .banyandb .v1 .client .metadata .StreamMetadataRegistry ;
71
74
import org .apache .skywalking .banyandb .v1 .client .metadata .TopNAggregationMetadataRegistry ;
75
+ import org .apache .skywalking .banyandb .v1 .client .metadata .TraceMetadataRegistry ;
72
76
73
77
import java .io .Closeable ;
74
78
import java .io .IOException ;
@@ -124,6 +128,11 @@ public class BanyanDBClient implements Closeable {
124
128
*/
125
129
@ Getter (value = AccessLevel .PACKAGE )
126
130
private MeasureServiceGrpc .MeasureServiceStub measureServiceStub ;
131
+ /**
132
+ * gRPC client stub
133
+ */
134
+ @ Getter (value = AccessLevel .PACKAGE )
135
+ private TraceServiceGrpc .TraceServiceStub traceServiceStub ;
127
136
/**
128
137
* gRPC future stub.
129
138
*/
@@ -134,6 +143,11 @@ public class BanyanDBClient implements Closeable {
134
143
*/
135
144
@ Getter (value = AccessLevel .PACKAGE )
136
145
private MeasureServiceGrpc .MeasureServiceBlockingStub measureServiceBlockingStub ;
146
+ /**
147
+ * gRPC future stub.
148
+ */
149
+ @ Getter (value = AccessLevel .PACKAGE )
150
+ private TraceServiceGrpc .TraceServiceBlockingStub traceServiceBlockingStub ;
137
151
/**
138
152
* The connection status.
139
153
*/
@@ -211,8 +225,10 @@ public void connect() throws IOException {
211
225
this .channel = interceptedChannel ;
212
226
streamServiceBlockingStub = StreamServiceGrpc .newBlockingStub (this .channel );
213
227
measureServiceBlockingStub = MeasureServiceGrpc .newBlockingStub (this .channel );
228
+ traceServiceBlockingStub = TraceServiceGrpc .newBlockingStub (this .channel );
214
229
streamServiceStub = StreamServiceGrpc .newStub (this .channel );
215
230
measureServiceStub = MeasureServiceGrpc .newStub (this .channel );
231
+ traceServiceStub = TraceServiceGrpc .newStub (this .channel );
216
232
isConnected = true ;
217
233
}
218
234
} finally {
@@ -228,8 +244,10 @@ void connect(Channel channel) {
228
244
this .channel = channel ;
229
245
streamServiceBlockingStub = StreamServiceGrpc .newBlockingStub (this .channel );
230
246
measureServiceBlockingStub = MeasureServiceGrpc .newBlockingStub (this .channel );
247
+ traceServiceBlockingStub = TraceServiceGrpc .newBlockingStub (this .channel );
231
248
streamServiceStub = StreamServiceGrpc .newStub (this .channel );
232
249
measureServiceStub = MeasureServiceGrpc .newStub (this .channel );
250
+ traceServiceStub = TraceServiceGrpc .newStub (this .channel );
233
251
isConnected = true ;
234
252
}
235
253
} finally {
@@ -396,6 +414,49 @@ public StreamWrite createStreamWrite(String group, String name, final String ele
396
414
return new StreamWrite (this .metadataCache .findStreamMetadata (group , name ), elementId , timestamp );
397
415
}
398
416
417
+ /**
418
+ * Build a trace bulk write processor.
419
+ *
420
+ * @param maxBulkSize the max size of each flush. The actual size is determined by the length of byte array.
421
+ * @param flushInterval if given maxBulkSize is not reached in this period, the flush would be trigger
422
+ * automatically. Unit is second.
423
+ * @param concurrency the number of concurrency would run for the flush max.
424
+ * @param timeout network timeout threshold in seconds.
425
+ * @return trace bulk write processor
426
+ */
427
+ public TraceBulkWriteProcessor buildTraceWriteProcessor (int maxBulkSize , int flushInterval , int concurrency , int timeout ) {
428
+ checkState (this .traceServiceStub != null , "trace service is null" );
429
+
430
+ return new TraceBulkWriteProcessor (this , maxBulkSize , flushInterval , concurrency , timeout , WRITE_HISTOGRAM , options );
431
+ }
432
+
433
+ /**
434
+ * Build a TraceWrite request.
435
+ *
436
+ * @param group the group of the trace
437
+ * @param name the name of the trace
438
+ * @param timestamp the timestamp of the trace
439
+ * @return the request to be built
440
+ */
441
+ public TraceWrite createTraceWrite (String group , String name , long timestamp ) throws BanyanDBException {
442
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (group ));
443
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (name ));
444
+ return new TraceWrite (this .metadataCache .findTraceMetadata (group , name ), timestamp );
445
+ }
446
+
447
+ /**
448
+ * Build a TraceWrite request without initial timestamp.
449
+ *
450
+ * @param group the group of the trace
451
+ * @param name the name of the trace
452
+ * @return the request to be built
453
+ */
454
+ public TraceWrite createTraceWrite (String group , String name ) throws BanyanDBException {
455
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (group ));
456
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (name ));
457
+ return new TraceWrite (this .metadataCache .findTraceMetadata (group , name ));
458
+ }
459
+
399
460
/**
400
461
* Query streams according to given conditions
401
462
*
@@ -456,6 +517,29 @@ public MeasureQueryResponse query(MeasureQuery measureQuery) throws BanyanDBExce
456
517
throw new RuntimeException ("No metadata found for the query" );
457
518
}
458
519
520
+ /**
521
+ * Query traces according to given conditions
522
+ *
523
+ * @param traceQuery condition for query
524
+ * @return trace query response.
525
+ */
526
+ public TraceQueryResponse query (TraceQuery traceQuery ) throws BanyanDBException {
527
+ checkState (this .traceServiceStub != null , "trace service is null" );
528
+
529
+ for (String group : traceQuery .groups ) {
530
+ MetadataCache .EntityMetadata em = this .metadataCache .findTraceMetadata (group , traceQuery .name );
531
+ if (em != null ) {
532
+ final BanyandbTrace .QueryResponse response = HandleExceptionsWith .callAndTranslateApiException (() ->
533
+ this .traceServiceBlockingStub
534
+ .withDeadlineAfter (this .getOptions ().getDeadline (), TimeUnit .SECONDS )
535
+ .query (traceQuery .build (em )));
536
+ return new TraceQueryResponse (response );
537
+ }
538
+
539
+ }
540
+ throw new RuntimeException ("No metadata found for the query" );
541
+ }
542
+
459
543
/**
460
544
* Define a new group and attach to the current client.
461
545
*
@@ -968,6 +1052,69 @@ public DeleteResponse deleteProperty(String group, String name, String id) throw
968
1052
}
969
1053
}
970
1054
1055
+ /**
1056
+ * Define a new trace
1057
+ *
1058
+ * @param trace the trace to be stored in the BanyanDB
1059
+ * @throws BanyanDBException if the trace is invalid
1060
+ */
1061
+ public void define (Trace trace ) throws BanyanDBException {
1062
+ TraceMetadataRegistry registry = new TraceMetadataRegistry (checkNotNull (this .channel ));
1063
+ registry .create (trace );
1064
+ }
1065
+
1066
+ /**
1067
+ * Update the trace.
1068
+ *
1069
+ * @param trace the trace to be stored in the BanyanDB
1070
+ * @throws BanyanDBException if the trace is invalid
1071
+ */
1072
+ public void update (Trace trace ) throws BanyanDBException {
1073
+ TraceMetadataRegistry registry = new TraceMetadataRegistry (checkNotNull (this .channel ));
1074
+ registry .update (trace );
1075
+ }
1076
+
1077
+ /**
1078
+ * Find the trace with given group and name
1079
+ *
1080
+ * @param group group of the metadata
1081
+ * @param name name of the metadata
1082
+ * @return the trace found in BanyanDB. Otherwise, null is returned.
1083
+ */
1084
+ public Trace findTrace (String group , String name ) throws BanyanDBException {
1085
+ try {
1086
+ return new TraceMetadataRegistry (checkNotNull (this .channel )).get (group , name );
1087
+ } catch (BanyanDBException ex ) {
1088
+ if (ex .getStatus ().equals (Status .Code .NOT_FOUND )) {
1089
+ return null ;
1090
+ }
1091
+ throw ex ;
1092
+ }
1093
+ }
1094
+
1095
+ /**
1096
+ * Find the traces with given group
1097
+ *
1098
+ * @param group group of the metadata
1099
+ * @return the traces found in BanyanDB
1100
+ */
1101
+ public List <Trace > findTraces (String group ) throws BanyanDBException {
1102
+ TraceMetadataRegistry registry = new TraceMetadataRegistry (checkNotNull (this .channel ));
1103
+ return registry .list (group );
1104
+ }
1105
+
1106
+ /**
1107
+ * Delete the trace
1108
+ *
1109
+ * @param group group of the metadata
1110
+ * @param name name of the metadata
1111
+ * @return if this trace has been deleted
1112
+ */
1113
+ public boolean deleteTrace (String group , String name ) throws BanyanDBException {
1114
+ TraceMetadataRegistry registry = new TraceMetadataRegistry (checkNotNull (this .channel ));
1115
+ return registry .delete (group , name );
1116
+ }
1117
+
971
1118
/**
972
1119
* Try to find the group defined
973
1120
*
@@ -1169,6 +1316,20 @@ public ResourceExist existProperty(String group, String name) throws BanyanDBExc
1169
1316
return new PropertyMetadataRegistry (checkNotNull (this .channel )).exist (group , name );
1170
1317
}
1171
1318
1319
+ /**
1320
+ * Check whether the trace definition is existed in the server
1321
+ *
1322
+ * @param group group of the metadata
1323
+ * @param name name of the metadata
1324
+ * @return ResourceExist which indicates whether group and trace exist
1325
+ */
1326
+ public ResourceExist existTrace (String group , String name ) throws BanyanDBException {
1327
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (group ));
1328
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (name ));
1329
+
1330
+ return new TraceMetadataRegistry (checkNotNull (this .channel )).exist (group , name );
1331
+ }
1332
+
1172
1333
/**
1173
1334
* Update the stream metadata cache from the server
1174
1335
* @param group the group of the stream
@@ -1193,6 +1354,18 @@ public MetadataCache.EntityMetadata updateMeasureMetadataCacheFromSever(String g
1193
1354
return this .metadataCache .updateMeasureFromSever (group , name );
1194
1355
}
1195
1356
1357
+ /**
1358
+ * Update the trace metadata cache from the server
1359
+ * @param group the group of the trace
1360
+ * @param name the name of the trace
1361
+ * @return the updated trace metadata, or null if the trace does not exist
1362
+ */
1363
+ public MetadataCache .EntityMetadata updateTraceMetadataCacheFromServer (String group , String name ) throws BanyanDBException {
1364
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (group ));
1365
+ Preconditions .checkArgument (!Strings .isNullOrEmpty (name ));
1366
+ return this .metadataCache .updateTraceFromServer (group , name );
1367
+ }
1368
+
1196
1369
/**
1197
1370
* Get the API version of the server
1198
1371
*
0 commit comments