You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2021/08/12 11:09:06 UTC

[GitHub] [skywalking] sonatype-lift[bot] commented on a change in pull request #7448: Banyandb integration

sonatype-lift[bot] commented on a change in pull request #7448:
URL: https://github.com/apache/skywalking/pull/7448#discussion_r687612947



##########
File path: oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/dao/BanyanDBTraceQueryDAO.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.banyandb.dao;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.apache.skywalking.banyandb.client.request.TraceFetchRequest;
+import org.apache.skywalking.banyandb.client.request.TraceSearchQuery;
+import org.apache.skywalking.banyandb.client.request.TraceSearchRequest;
+import org.apache.skywalking.banyandb.client.response.BanyanDBQueryResponse;
+import org.apache.skywalking.oap.server.core.analysis.IDManager;
+import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.Tag;
+import org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord;
+import org.apache.skywalking.oap.server.core.query.type.BasicTrace;
+import org.apache.skywalking.oap.server.core.query.type.QueryOrder;
+import org.apache.skywalking.oap.server.core.query.type.Span;
+import org.apache.skywalking.oap.server.core.query.type.TraceBrief;
+import org.apache.skywalking.oap.server.core.query.type.TraceState;
+import org.apache.skywalking.oap.server.core.storage.AbstractDAO;
+import org.apache.skywalking.oap.server.core.storage.query.ITraceQueryDAO;
+import org.apache.skywalking.oap.server.storage.plugin.banyandb.BanyanDBClient;
+import org.apache.skywalking.oap.server.storage.plugin.banyandb.BanyanDBSchema;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class BanyanDBTraceQueryDAO extends AbstractDAO<BanyanDBClient> implements ITraceQueryDAO {
+    private static final Set<String> DEFAULT_PROJECTION = ImmutableSet.of("duration", "state", "start_time", "trace_id");
+
+    public BanyanDBTraceQueryDAO(BanyanDBClient client) {
+        super(client);
+    }
+
+    @Override
+    public TraceBrief queryBasicTraces(long startSecondTB, long endSecondTB, long minDuration, long maxDuration, String serviceId, String serviceInstanceId, String endpointId, String traceId, int limit, int from, TraceState traceState, QueryOrder queryOrder, List<Tag> tags) throws IOException {
+        TraceSearchRequest.TraceSearchRequestBuilder<?, ?> queryBuilder = TraceSearchRequest.builder().name(BanyanDBSchema.NAME).group(BanyanDBSchema.GROUP);
+        if (startSecondTB != 0 && endSecondTB != 0) {
+            queryBuilder.timeRange(TraceSearchRequest.TimeRange.builder()
+                    .startTime(startSecondTB)
+                    .endTime(endSecondTB).build());
+        }
+        if (minDuration != 0) {
+            // duration >= minDuration
+            queryBuilder.query(TraceSearchQuery.Ge("duration", minDuration));
+        }
+        if (maxDuration != 0) {
+            // duration <= maxDuration
+            queryBuilder.query(TraceSearchQuery.Le("duration", maxDuration));
+        }
+
+        if (!Strings.isNullOrEmpty(serviceId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("service_id", serviceId));
+        }
+
+        if (!Strings.isNullOrEmpty(serviceInstanceId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("service_instance_id", serviceInstanceId));
+        }
+
+        if (!Strings.isNullOrEmpty(endpointId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("endpoint_id", endpointId));
+        }
+
+        switch (traceState) {
+            case ERROR:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.ERROR.getState()));
+                break;
+            case SUCCESS:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.SUCCESS.getState()));
+                break;
+            default:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.ALL.getState()));
+                break;
+        }
+
+        switch (queryOrder) {
+            case BY_START_TIME:
+                queryBuilder.queryOrderField("start_time").queryOrderSort(TraceSearchRequest.SortOrder.DESC);
+                break;
+            case BY_DURATION:
+                queryBuilder.queryOrderField("duration").queryOrderSort(TraceSearchRequest.SortOrder.DESC);
+                break;
+        }
+
+        queryBuilder.projections(DEFAULT_PROJECTION);
+        queryBuilder.limit(limit);
+        queryBuilder.offset(from);
+
+        // build request
+        BanyanDBQueryResponse response = this.getClient().queryBasicTraces(queryBuilder.build());
+        TraceBrief brief = new TraceBrief();
+        brief.setTotal(response.getTotal());
+        brief.getTraces().addAll(response.getEntities().stream().map(entity -> {
+            BasicTrace trace = new BasicTrace();
+            trace.setDuration(((Long) entity.getFields().get("duration")).intValue());
+            trace.setStart(String.valueOf(entity.getFields().get(SegmentRecord.START_TIME)));
+            trace.setSegmentId(entity.getEntityId());
+            trace.setError(((Long) entity.getFields().get("state")).intValue() == 1);
+            trace.getTraceIds().add((String) entity.getFields().get(SegmentRecord.TRACE_ID));

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getTraceIds(trace)` could be null and is dereferenced at line 117.
   (at-me [in a reply](https://help.sonatype.com/lift) with `help` or `ignore`)

##########
File path: oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/dao/BanyanDBTraceQueryDAO.java
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.banyandb.dao;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.apache.skywalking.banyandb.client.request.TraceFetchRequest;
+import org.apache.skywalking.banyandb.client.request.TraceSearchQuery;
+import org.apache.skywalking.banyandb.client.request.TraceSearchRequest;
+import org.apache.skywalking.banyandb.client.response.BanyanDBQueryResponse;
+import org.apache.skywalking.oap.server.core.analysis.IDManager;
+import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.Tag;
+import org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord;
+import org.apache.skywalking.oap.server.core.query.type.BasicTrace;
+import org.apache.skywalking.oap.server.core.query.type.QueryOrder;
+import org.apache.skywalking.oap.server.core.query.type.Span;
+import org.apache.skywalking.oap.server.core.query.type.TraceBrief;
+import org.apache.skywalking.oap.server.core.query.type.TraceState;
+import org.apache.skywalking.oap.server.core.storage.AbstractDAO;
+import org.apache.skywalking.oap.server.core.storage.query.ITraceQueryDAO;
+import org.apache.skywalking.oap.server.storage.plugin.banyandb.BanyanDBClient;
+import org.apache.skywalking.oap.server.storage.plugin.banyandb.BanyanDBSchema;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class BanyanDBTraceQueryDAO extends AbstractDAO<BanyanDBClient> implements ITraceQueryDAO {
+    private static final Set<String> DEFAULT_PROJECTION = ImmutableSet.of("duration", "state", "start_time", "trace_id");
+
+    public BanyanDBTraceQueryDAO(BanyanDBClient client) {
+        super(client);
+    }
+
+    @Override
+    public TraceBrief queryBasicTraces(long startSecondTB, long endSecondTB, long minDuration, long maxDuration, String serviceId, String serviceInstanceId, String endpointId, String traceId, int limit, int from, TraceState traceState, QueryOrder queryOrder, List<Tag> tags) throws IOException {
+        TraceSearchRequest.TraceSearchRequestBuilder<?, ?> queryBuilder = TraceSearchRequest.builder().name(BanyanDBSchema.NAME).group(BanyanDBSchema.GROUP);
+        if (startSecondTB != 0 && endSecondTB != 0) {
+            queryBuilder.timeRange(TraceSearchRequest.TimeRange.builder()
+                    .startTime(startSecondTB)
+                    .endTime(endSecondTB).build());
+        }
+        if (minDuration != 0) {
+            // duration >= minDuration
+            queryBuilder.query(TraceSearchQuery.Ge("duration", minDuration));
+        }
+        if (maxDuration != 0) {
+            // duration <= maxDuration
+            queryBuilder.query(TraceSearchQuery.Le("duration", maxDuration));
+        }
+
+        if (!Strings.isNullOrEmpty(serviceId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("service_id", serviceId));
+        }
+
+        if (!Strings.isNullOrEmpty(serviceInstanceId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("service_instance_id", serviceInstanceId));
+        }
+
+        if (!Strings.isNullOrEmpty(endpointId)) {
+            queryBuilder.query(TraceSearchQuery.Eq("endpoint_id", endpointId));
+        }
+
+        switch (traceState) {
+            case ERROR:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.ERROR.getState()));
+                break;
+            case SUCCESS:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.SUCCESS.getState()));
+                break;
+            default:
+                queryBuilder.query(TraceSearchQuery.Eq("state", BanyanDBSchema.TraceState.ALL.getState()));
+                break;
+        }
+
+        switch (queryOrder) {
+            case BY_START_TIME:
+                queryBuilder.queryOrderField("start_time").queryOrderSort(TraceSearchRequest.SortOrder.DESC);
+                break;
+            case BY_DURATION:
+                queryBuilder.queryOrderField("duration").queryOrderSort(TraceSearchRequest.SortOrder.DESC);
+                break;
+        }
+
+        queryBuilder.projections(DEFAULT_PROJECTION);
+        queryBuilder.limit(limit);
+        queryBuilder.offset(from);
+
+        // build request
+        BanyanDBQueryResponse response = this.getClient().queryBasicTraces(queryBuilder.build());
+        TraceBrief brief = new TraceBrief();
+        brief.setTotal(response.getTotal());
+        brief.getTraces().addAll(response.getEntities().stream().map(entity -> {

Review comment:
       *NULL_DEREFERENCE:*  object returned by `getTraces(brief)` could be null and is dereferenced at line 111.
   (at-me [in a reply](https://help.sonatype.com/lift) with `help` or `ignore`)




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org