FrankChen021 commented on code in PR #20320:
URL: https://github.com/apache/druid/pull/20320#discussion_r4056924130


##########
services/src/test/java/org/apache/druid/testing/embedded/EmbeddedClusterApis.java:
##########
@@ -281,21 +282,41 @@ public List<Interval> getSortedSegmentIntervals(String 
dataSource, EmbeddedOverl
    */
   public void verifyNumVisibleSegmentsIs(int numExpectedSegments, String 
dataSource, EmbeddedOverlord overlord)
   {
-    int segmentCount = getVisibleUsedSegments(dataSource, overlord).size();
+    final Set<DataSegment> visibleSegments = 
getVisibleUsedSegments(dataSource, overlord);
+    final int segmentCount = visibleSegments.size();
     Assertions.assertEquals(
         numExpectedSegments,
         segmentCount,
         "Segment count mismatch"
     );
-    Assertions.assertEquals(
-        String.valueOf(segmentCount),
-        runSql(
-            "SELECT COUNT(*) FROM sys.segments WHERE datasource='%s'"
-            + " AND is_overshadowed = 0 AND is_available = 1",
-            dataSource
-        ),
-        "Segment count mismatch in sys.segments table"
-    );
+
+    // The Broker learns about segment changes asynchronously from the 
Coordinator, so the
+    // sys.segments table may briefly lag behind the metadata store. Match the 
segment IDs as well
+    // as the count because compaction can replace segments without changing 
their number.
+    final String expectedCount = String.valueOf(segmentCount);
+    final String expectedSegmentIds = visibleSegments
+        .stream()
+        .map(segment -> StringUtils.format("'%s'", 
StringUtils.escapeSql(segment.getId().toString())))

Review Comment:
   [P2] Percent signs in segment IDs break SQL formatting
   
   **Finding:** `expectedSegmentIds` is inserted into `sql` before `runSql` 
calls `StringUtils.format(sql, ...)`. Because segment IDs include the 
datasource name and Druid permits `%` in IDs, a datasource or version 
containing a format sequence such as `%s` or `%n` is interpreted by 
`String.format` on every poll rather than treated as SQL data. The helper then 
throws or generates a malformed or incorrect query, so compaction verification 
fails for otherwise valid datasource names.
   
   **Suggestion:** Encode percent signs for the outer format string, for 
example with `StringUtils.encodeForFormat` after SQL escaping, or format the 
datasource before appending the ID literals so segment IDs never pass through 
`StringUtils.format`.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to