wu-sheng commented on code in PR #10004: URL: https://github.com/apache/skywalking/pull/10004#discussion_r1029244547
########## oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/BanyanDBZipkinQueryDAO.java: ########## @@ -0,0 +1,297 @@ +/* + * 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; + +import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.skywalking.banyandb.v1.client.AbstractCriteria; +import org.apache.skywalking.banyandb.v1.client.AbstractQuery; +import org.apache.skywalking.banyandb.v1.client.DataPoint; +import org.apache.skywalking.banyandb.v1.client.MeasureQuery; +import org.apache.skywalking.banyandb.v1.client.MeasureQueryResponse; +import org.apache.skywalking.banyandb.v1.client.RowEntity; +import org.apache.skywalking.banyandb.v1.client.StreamQuery; +import org.apache.skywalking.banyandb.v1.client.StreamQueryResponse; +import org.apache.skywalking.banyandb.v1.client.TimestampRange; +import org.apache.skywalking.oap.server.core.query.input.Duration; +import org.apache.skywalking.oap.server.core.storage.query.IZipkinQueryDAO; +import org.apache.skywalking.oap.server.core.zipkin.ZipkinServiceRelationTraffic; +import org.apache.skywalking.oap.server.core.zipkin.ZipkinServiceSpanTraffic; +import org.apache.skywalking.oap.server.core.zipkin.ZipkinServiceTraffic; +import org.apache.skywalking.oap.server.core.zipkin.ZipkinSpanRecord; +import org.apache.skywalking.oap.server.library.util.CollectionUtils; +import org.apache.skywalking.oap.server.library.util.StringUtil; +import org.apache.skywalking.oap.server.storage.plugin.banyandb.stream.AbstractBanyanDBDAO; +import zipkin2.Span; +import zipkin2.storage.QueryRequest; + +public class BanyanDBZipkinQueryDAO extends AbstractBanyanDBDAO implements IZipkinQueryDAO { + private final static int QUERY_MAX_SIZE = Integer.MAX_VALUE; + private static final Set<String> SERVICE_TRAFFIC_TAGS = ImmutableSet.of(ZipkinServiceTraffic.SERVICE_NAME); + private static final Set<String> REMOTE_SERVICE_TRAFFIC_TAGS = ImmutableSet.of( + ZipkinServiceRelationTraffic.REMOTE_SERVICE_NAME); + private static final Set<String> SPAN_TRAFFIC_TAGS = ImmutableSet.of(ZipkinServiceSpanTraffic.SPAN_NAME); + private static final Set<String> TRACE_ID = ImmutableSet.of(ZipkinSpanRecord.TRACE_ID); + private static final Set<String> TRACE_TAGS = ImmutableSet.of( + ZipkinSpanRecord.TRACE_ID, + ZipkinSpanRecord.SPAN_ID, + ZipkinSpanRecord.PARENT_ID, + ZipkinSpanRecord.KIND, + ZipkinSpanRecord.TIMESTAMP, + ZipkinSpanRecord.TIMESTAMP_MILLIS, + ZipkinSpanRecord.DURATION, + ZipkinSpanRecord.NAME, + ZipkinSpanRecord.DEBUG, + ZipkinSpanRecord.SHARED, + ZipkinSpanRecord.LOCAL_ENDPOINT_SERVICE_NAME, + ZipkinSpanRecord.LOCAL_ENDPOINT_IPV4, + ZipkinSpanRecord.LOCAL_ENDPOINT_IPV6, + ZipkinSpanRecord.LOCAL_ENDPOINT_PORT, + ZipkinSpanRecord.REMOTE_ENDPOINT_SERVICE_NAME, + ZipkinSpanRecord.REMOTE_ENDPOINT_IPV4, + ZipkinSpanRecord.REMOTE_ENDPOINT_IPV6, + ZipkinSpanRecord.REMOTE_ENDPOINT_PORT, + ZipkinSpanRecord.TAGS, + ZipkinSpanRecord.ANNOTATIONS + ); + + public BanyanDBZipkinQueryDAO(BanyanDBStorageClient client) { + super(client); + } + + @Override + public List<String> getServiceNames() throws IOException { + MeasureQueryResponse resp = + query(ZipkinServiceTraffic.INDEX_NAME, + SERVICE_TRAFFIC_TAGS, + Collections.emptySet(), new QueryBuilder<MeasureQuery>() { + + @Override + protected void apply(MeasureQuery query) { + query.setLimit(QUERY_MAX_SIZE); + } + } + ); + final List<String> services = new ArrayList<>(); + for (final DataPoint dataPoint : resp.getDataPoints()) { + services.add(dataPoint.getTagValue(ZipkinServiceTraffic.SERVICE_NAME)); + } + return services; + } + + @Override + public List<String> getRemoteServiceNames(final String serviceName) throws IOException { + MeasureQueryResponse resp = + query(ZipkinServiceRelationTraffic.INDEX_NAME, + REMOTE_SERVICE_TRAFFIC_TAGS, + Collections.emptySet(), new QueryBuilder<MeasureQuery>() { + + @Override + protected void apply(MeasureQuery query) { + if (StringUtil.isNotEmpty(serviceName)) { + query.and(eq(ZipkinServiceRelationTraffic.SERVICE_NAME, serviceName)); + } + query.setLimit(QUERY_MAX_SIZE); + } + } + ); + final List<String> remoteServices = new ArrayList<>(); + for (final DataPoint dataPoint : resp.getDataPoints()) { + remoteServices.add(dataPoint.getTagValue(ZipkinServiceRelationTraffic.REMOTE_SERVICE_NAME)); + } + return remoteServices; + } + + @Override + public List<String> getSpanNames(final String serviceName) throws IOException { + MeasureQueryResponse resp = + query(ZipkinServiceSpanTraffic.INDEX_NAME, + SPAN_TRAFFIC_TAGS, + Collections.emptySet(), new QueryBuilder<MeasureQuery>() { + + @Override + protected void apply(MeasureQuery query) { + if (StringUtil.isNotEmpty(serviceName)) { + query.and(eq(ZipkinServiceSpanTraffic.SERVICE_NAME, serviceName)); + } + query.setLimit(QUERY_MAX_SIZE); + } + } + ); + final List<String> spanNames = new ArrayList<>(); + for (final DataPoint dataPoint : resp.getDataPoints()) { + spanNames.add(dataPoint.getTagValue(ZipkinServiceSpanTraffic.SPAN_NAME)); + } + return spanNames; + } + + @Override + public List<Span> getTrace(final String traceId) throws IOException { + StreamQueryResponse resp = + query(ZipkinSpanRecord.INDEX_NAME, TRACE_TAGS, + new QueryBuilder<StreamQuery>() { + + @Override + protected void apply(StreamQuery query) { + query.and(eq(ZipkinSpanRecord.TRACE_ID, traceId)); + query.setLimit(QUERY_MAX_SIZE); + } + } + ); + + List<Span> trace = new ArrayList<>(resp.getElements().size()); + + for (final RowEntity rowEntity : resp.getElements()) { + ZipkinSpanRecord spanRecord = new ZipkinSpanRecord.Builder().storage2Entity( + new BanyanDBConverter.StorageToStream(ZipkinSpanRecord.INDEX_NAME, rowEntity)); + trace.add(ZipkinSpanRecord.buildSpanFromRecord(spanRecord)); + } + + return trace; + } + + @Override + public List<List<Span>> getTraces(final QueryRequest request, Duration duration) throws IOException { + final int tracesLimit = request.limit(); + int scrollLimit = 1000; + int scrollFrom = 0; + Set<String> traceIds = new HashSet<>(); + while (traceIds.size() < tracesLimit) { + Set<String> resp = getTraceIds(request, duration, scrollFrom, scrollLimit); Review Comment: @hanahmily @lujiajing1126 Please confirm this is good enough. Zipkin provides trace IDs query to the Span table, and also requires distinct trace IDs according to the `limit` condition. It is using `aggregation` or `distinct .. group by` in other storage options, but BanyanDB doesn't provide this. So, we have to do a rolling query, but as BanyanDB can't provide a rolling query(no rolling ID concept), we have to use `from... to...` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
