This is an automated email from the ASF dual-hosted git repository.

wankai pushed a commit to branch fix-query-range
in repository https://gitbox.apache.org/repos/asf/skywalking.git

commit da3a37f205739080b5495c20cb94cc973a043144
Author: wankai123 <wankai...@foxmail.com>
AuthorDate: Thu Jun 19 10:09:13 2025 +0800

    Fix the query time range in the metadata API.
---
 .../plugin/banyandb/measure/BanyanDBMetadataQueryDAO.java  | 14 +++++++++-----
 .../plugin/elasticsearch/query/MetadataQueryEsDAO.java     | 12 +++++++-----
 .../plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java       | 11 +++++++++--
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git 
a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBMetadataQueryDAO.java
 
b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBMetadataQueryDAO.java
index 78938c649e..e6624c659a 100644
--- 
a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBMetadataQueryDAO.java
+++ 
b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBMetadataQueryDAO.java
@@ -127,8 +127,10 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
                         if (StringUtil.isNotEmpty(serviceId)) {
                             query.and(eq(InstanceTraffic.SERVICE_ID, 
serviceId));
                         }
-                        final var minuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getStartTimestamp());
-                        query.and(gte(InstanceTraffic.LAST_PING_TIME_BUCKET, 
minuteTimeBucket));
+                        final var startTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getStartTimestamp());
+                        final var endTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
+                        query.and(gte(InstanceTraffic.LAST_PING_TIME_BUCKET, 
startTimeBucket));
+                        query.and(lte(InstanceTraffic.TIME_BUCKET, 
endTimeBucket));
                         query.limit(limit);
                     }
                 });
@@ -203,7 +205,7 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
                             final var startTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getStartTimestamp());
                             final var endTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
                             
query.and(gte(EndpointTraffic.LAST_PING_TIME_BUCKET, startTimeBucket));
-                            
query.and(lte(EndpointTraffic.LAST_PING_TIME_BUCKET, endTimeBucket));
+                            query.and(lte(EndpointTraffic.TIME_BUCKET, 
endTimeBucket));
                         }
                         query.setOrderBy(new 
AbstractQuery.OrderBy(AbstractQuery.Sort.DESC));
                         query.limit(limit);
@@ -231,7 +233,7 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
                             
query.and(gte(ProcessTraffic.LAST_PING_TIME_BUCKET, lastPingStartTimeBucket));
                         }
                         if (lastPingEndTimeBucket > 0) {
-                            
query.and(lte(ProcessTraffic.LAST_PING_TIME_BUCKET, lastPingEndTimeBucket));
+                            query.and(lte(ProcessTraffic.TIME_BUCKET, 
lastPingEndTimeBucket));
                         }
                         if (supportStatus != null) {
                             
query.and(eq(ProcessTraffic.PROFILING_SUPPORT_STATUS, supportStatus.value()));
@@ -253,6 +255,7 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
     public List<Process> listProcesses(String serviceInstanceId, Duration 
duration, boolean includeVirtual) throws IOException {
         MetadataRegistry.Schema schema = 
MetadataRegistry.INSTANCE.findMetadata(ProcessTraffic.INDEX_NAME, 
DownSampling.Minute);
         long lastPingStartTimeBucket = duration.getStartTimeBucket();
+        long lastPingEndTimeBucket = duration.getEndTimeBucket();
         MeasureQueryResponse resp = query(false, schema,
                 PROCESS_TRAFFIC_TAGS,
                 Collections.emptySet(),
@@ -261,6 +264,7 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
                     protected void apply(MeasureQuery query) {
                         query.and(eq(ProcessTraffic.INSTANCE_ID, 
serviceInstanceId));
                         query.and(gte(ProcessTraffic.LAST_PING_TIME_BUCKET, 
lastPingStartTimeBucket));
+                        query.and(lte(ProcessTraffic.TIME_BUCKET, 
lastPingEndTimeBucket));
                         if (!includeVirtual) {
                             query.and(ne(ProcessTraffic.DETECT_TYPE, 
ProcessDetectType.VIRTUAL.value()));
                         }
@@ -287,7 +291,7 @@ public class BanyanDBMetadataQueryDAO extends 
AbstractBanyanDBDAO implements IMe
                     protected void apply(MeasureQuery query) {
                         query.and(eq(ProcessTraffic.AGENT_ID, agentId));
                         query.and(gte(ProcessTraffic.LAST_PING_TIME_BUCKET, 
startPingTimeBucket));
-                        query.and(lte(ProcessTraffic.LAST_PING_TIME_BUCKET, 
endPingTimeBucket));
+                        query.and(lte(ProcessTraffic.TIME_BUCKET, 
endPingTimeBucket));
                         query.and(ne(ProcessTraffic.DETECT_TYPE, 
ProcessDetectType.VIRTUAL.value()));
                         query.limit(limit);
                     }
diff --git 
a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
 
b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
index a6a25f87c0..ac4d234f99 100644
--- 
a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
+++ 
b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetadataQueryEsDAO.java
@@ -161,7 +161,7 @@ public class MetadataQueryEsDAO extends EsDAO implements 
IMetadataQueryDAO {
             final long startMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getStartTimestamp());
             final long endMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
             
query.must(Query.range(InstanceTraffic.LAST_PING_TIME_BUCKET).gte(startMinuteTimeBucket))
-                 
.must(Query.range(InstanceTraffic.TIME_BUCKET).lt(endMinuteTimeBucket));
+                 
.must(Query.range(InstanceTraffic.TIME_BUCKET).lte(endMinuteTimeBucket));
         }
         if 
(IndexController.LogicIndicesRegister.isMergedTable(InstanceTraffic.INDEX_NAME))
 {
             
query.must(Query.term(IndexController.LogicIndicesRegister.METRIC_TABLE_NAME, 
InstanceTraffic.INDEX_NAME));
@@ -237,7 +237,8 @@ public class MetadataQueryEsDAO extends EsDAO implements 
IMetadataQueryDAO {
         if (duration != null) {
             final long startMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getStartTimestamp());
             final long endMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
-            
query.must(Query.range(EndpointTraffic.LAST_PING_TIME_BUCKET).gte(startMinuteTimeBucket).lte(endMinuteTimeBucket));
+            
query.must(Query.range(EndpointTraffic.LAST_PING_TIME_BUCKET).gte(startMinuteTimeBucket))
+                 
.must(Query.range(EndpointTraffic.TIME_BUCKET).lte(endMinuteTimeBucket));
         }
 
         final var search = Search.builder().query(query).size(limit).sort(
@@ -389,9 +390,10 @@ public class MetadataQueryEsDAO extends EsDAO implements 
IMetadataQueryDAO {
             query.must(Query.term(ProcessTraffic.PROFILING_SUPPORT_STATUS, 
profilingSupportStatus.value()));
         }
         if (lastPingStartTimeBucket > 0) {
-            final RangeQueryBuilder rangeQuery = 
Query.range(ProcessTraffic.LAST_PING_TIME_BUCKET);
-            rangeQuery.gte(lastPingStartTimeBucket);
-            query.must(rangeQuery);
+            
query.must(Query.range(ProcessTraffic.LAST_PING_TIME_BUCKET).gte(lastPingStartTimeBucket));
+        }
+        if (lastPingEndTimeBucket > 0) {
+            
query.must(Query.range(ProcessTraffic.TIME_BUCKET).lte(lastPingEndTimeBucket));
         }
         if (!includeVirtual) {
             query.mustNot(Query.term(ProcessTraffic.DETECT_TYPE, 
ProcessDetectType.VIRTUAL.value()));
diff --git 
a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java
 
b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java
index ec0f7f87de..8dbb702a32 100644
--- 
a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java
+++ 
b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java
@@ -139,7 +139,7 @@ public class JDBCMetadataQueryDAO implements 
IMetadataQueryDAO {
             final var endMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
             sql.append(" and 
").append(InstanceTraffic.LAST_PING_TIME_BUCKET).append(" >= ?");
             parameters.add(startMinuteTimeBucket);
-            sql.append(" and ").append(InstanceTraffic.TIME_BUCKET).append(" < 
?");
+            sql.append(" and ").append(InstanceTraffic.TIME_BUCKET).append(" 
<= ?");
             parameters.add(endMinuteTimeBucket);
         }
         sql.append(" and ").append(InstanceTraffic.SERVICE_ID).append("=?");
@@ -228,7 +228,7 @@ public class JDBCMetadataQueryDAO implements 
IMetadataQueryDAO {
                 final var endMinuteTimeBucket = 
TimeBucket.getMinuteTimeBucket(duration.getEndTimestamp());
                 sql.append(" and 
").append(EndpointTraffic.LAST_PING_TIME_BUCKET).append(" >= ?");
                 condition.add(startMinuteTimeBucket);
-                sql.append(" and 
").append(EndpointTraffic.LAST_PING_TIME_BUCKET).append(" <= ?");
+                sql.append(" and 
").append(EndpointTraffic.TIME_BUCKET).append(" <= ?");
                 condition.add(endMinuteTimeBucket);
             }
             sql.append(" order by 
").append(EndpointTraffic.TIME_BUCKET).append(" desc");
@@ -493,6 +493,13 @@ public class JDBCMetadataQueryDAO implements 
IMetadataQueryDAO {
             sql.append(ProcessTraffic.LAST_PING_TIME_BUCKET).append(">=?");
             condition.add(lastPingStartTimeBucket);
         }
+        if (lastPingEndTimeBucket > 0) {
+            if (!condition.isEmpty()) {
+                sql.append(" and ");
+            }
+            sql.append(ProcessTraffic.TIME_BUCKET).append("<=?");
+            condition.add(lastPingEndTimeBucket);
+        }
         if (!includeVirtual) {
             if (!condition.isEmpty()) {
                 sql.append(" and ");

Reply via email to