dmsolr commented on a change in pull request #4239: Provide influxdb as a new 
storage plugin
URL: https://github.com/apache/skywalking/pull/4239#discussion_r379863712
 
 

 ##########
 File path: 
oap-server/server-storage-plugin/storage-influxdb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/influxdb/query/TraceQuery.java
 ##########
 @@ -0,0 +1,217 @@
+/*
+ * 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.oap.server.storage.plugin.influxdb.query;
+
+import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.List;
+import lombok.extern.slf4j.Slf4j;
+import 
org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord;
+import org.apache.skywalking.oap.server.core.query.entity.BasicTrace;
+import org.apache.skywalking.oap.server.core.query.entity.QueryOrder;
+import org.apache.skywalking.oap.server.core.query.entity.Span;
+import org.apache.skywalking.oap.server.core.query.entity.TraceBrief;
+import org.apache.skywalking.oap.server.core.query.entity.TraceState;
+import org.apache.skywalking.oap.server.core.storage.query.ITraceQueryDAO;
+import org.apache.skywalking.oap.server.library.util.BooleanUtils;
+import org.apache.skywalking.oap.server.storage.plugin.influxdb.InfluxClient;
+import org.apache.skywalking.oap.server.storage.plugin.influxdb.base.RecordDAO;
+import org.elasticsearch.common.Strings;
+import org.influxdb.dto.Query;
+import org.influxdb.dto.QueryResult;
+import org.influxdb.querybuilder.SelectQueryImpl;
+import org.influxdb.querybuilder.WhereQueryImpl;
+import org.influxdb.querybuilder.clauses.Clause;
+
+import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.eq;
+import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.gte;
+import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.lte;
+import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.regex;
+import static org.influxdb.querybuilder.BuiltQuery.QueryBuilder.select;
+
+@Slf4j
+public class TraceQuery implements ITraceQueryDAO {
+    private final InfluxClient client;
+
+    public TraceQuery(InfluxClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public TraceBrief queryBasicTraces(long startSecondTB,
+                                       long endSecondTB,
+                                       long minDuration,
+                                       long maxDuration,
+                                       String endpointName,
+                                       int serviceId,
+                                       int serviceInstanceId,
+                                       int endpointId,
+                                       String traceId,
+                                       int limit,
+                                       int from,
+                                       TraceState traceState,
+                                       QueryOrder queryOrder)
+        throws IOException {
+
+        String orderBy = SegmentRecord.START_TIME;
+        if (queryOrder == QueryOrder.BY_DURATION) {
+            orderBy = SegmentRecord.LATENCY;
+        }
+
+        WhereQueryImpl<SelectQueryImpl> recallQuery = select()
+            .function("top", orderBy, limit + from)
+            .column(SegmentRecord.SEGMENT_ID)
+            .column(SegmentRecord.START_TIME)
+            .column(SegmentRecord.ENDPOINT_NAME)
+            .column(SegmentRecord.LATENCY)
+            .column(SegmentRecord.IS_ERROR)
+            .column(SegmentRecord.TRACE_ID)
+            .from(client.getDatabase(), SegmentRecord.INDEX_NAME)
+            .where();
+
+        if (startSecondTB != 0 && endSecondTB != 0) {
+            recallQuery.and(gte(SegmentRecord.TIME_BUCKET, startSecondTB))
+                       .and(lte(SegmentRecord.TIME_BUCKET, endSecondTB));
+        }
+        if (minDuration != 0) {
+            recallQuery.and(gte(SegmentRecord.LATENCY, minDuration));
+        }
+        if (maxDuration != 0) {
+            recallQuery.and(lte(SegmentRecord.LATENCY, maxDuration));
+        }
+        if (!Strings.isNullOrEmpty(endpointName)) {
+            recallQuery.and(regex(SegmentRecord.ENDPOINT_NAME, "/" + 
endpointName.replaceAll("/", "\\\\/") + "/"));
+        }
+        if (serviceId != 0) {
+            recallQuery.and(eq(RecordDAO.TAG_SERVICE_ID, 
String.valueOf(serviceId)));
+        }
+        if (serviceInstanceId != 0) {
+            recallQuery.and(eq(SegmentRecord.SERVICE_INSTANCE_ID, 
serviceInstanceId));
+        }
+        if (endpointId != 0) {
+            recallQuery.and(eq(SegmentRecord.ENDPOINT_ID, endpointId));
+        }
+        if (!Strings.isNullOrEmpty(traceId)) {
+            recallQuery.and(eq(SegmentRecord.TRACE_ID, traceId));
+        }
+        switch (traceState) {
+            case ERROR:
+                recallQuery.and(eq(SegmentRecord.IS_ERROR, BooleanUtils.TRUE));
+                break;
+            case SUCCESS:
+                recallQuery.and(eq(SegmentRecord.IS_ERROR, 
BooleanUtils.FALSE));
+                break;
+        }
+
+        WhereQueryImpl<SelectQueryImpl> countQuery = select()
+            .count(SegmentRecord.ENDPOINT_ID)
+            .from(client.getDatabase(), SegmentRecord.INDEX_NAME)
+            .where();
+        for (Clause clause : recallQuery.getClauses()) {
+            countQuery.where(clause);
+        }
+        Query query = new Query(countQuery.getCommand() + 
recallQuery.getCommand());
+
+        List<QueryResult.Result> results = client.query(query);
+        if (log.isDebugEnabled()) {
+            log.debug("SQL: {} result set: {}", query.getCommand(), results);
+        }
+        if (results.size() != 2) {
+            throw new IOException("Expecting to get 2 Results, but it is " + 
results.size());
+        }
+        List<QueryResult.Series> counter = results.get(0).getSeries();
+        List<QueryResult.Series> result = results.get(1).getSeries();
+        if (result == null || result.isEmpty()) {
+            return new TraceBrief();
+        }
+
+        TraceBrief traceBrief = new TraceBrief();
+        traceBrief.setTotal(((Number) 
counter.get(0).getValues().get(0).get(1)).intValue());
+
+        result.get(0).getValues().stream().sorted((a, b) -> {
+            return Long.compare(((Number) b.get(1)).longValue(), ((Number) 
a.get(1)).longValue());
+        }).skip(from).forEach(values -> {
+            BasicTrace basicTrace = new BasicTrace();
+
+            basicTrace.setSegmentId((String) values.get(2));
+            basicTrace.setStart(String.valueOf((long) values.get(3)));
+            basicTrace.getEndpointNames().add((String) values.get(4));
+            basicTrace.setDuration((int) values.get(5));
+            basicTrace.setError(BooleanUtils.valueToBoolean((int) 
values.get(6)));
+            basicTrace.getTraceIds().add((String) values.get(7));
+
+            traceBrief.getTraces().add(basicTrace);
+        });
 
 Review comment:
   It took me a lot of time to get it clearly. It is a bad story.
   
   1. top/bottom cannot work with limit.
   2. I think top/bottom collects data by PriorityQueue. And it does not 
re-sort them in the result set. So we get the dataset unorder. Anyway, we 
always get the un-ordered result set in reality.
   
   I mean, the function `top` works after `limit`, if limit enabled.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to