yifan-c commented on code in PR #249: URL: https://github.com/apache/cassandra-sidecar/pull/249#discussion_r2289464242
########## client-common/src/main/java/org/apache/cassandra/sidecar/common/response/CompactionStatsResponse.java: ########## @@ -0,0 +1,194 @@ +/* + * 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.cassandra.sidecar.common.response; + +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.cassandra.sidecar.common.response.data.ActiveCompactionEntry; + +/** + * Response class for the CompactionStats API + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class CompactionStatsResponse +{ + private final long concurrentCompactors; + private final Map<String, Map<String, Integer>> pendingTasks; + private final long totalPendingTasks; + private final long completedCompactions; + private final long dataCompacted; + private final long abortedCompactions; + private final long reducedCompactions; + private final long sstablesDroppedFromCompaction; + private final CompletedCompactionsRate completedCompactionsRate; + private final List<ActiveCompactionEntry> activeCompactions; + private final long activeCompactionsCount; + private final String activeCompactionsRemainingTime; + + /** + * Represents the completed compactions rate + */ + @JsonInclude(JsonInclude.Include.NON_NULL) + public static class CompletedCompactionsRate + { + private final String meanRate; + private final String fifteenMinuteRate; + + @JsonCreator + public CompletedCompactionsRate(@JsonProperty("meanRate") final String meanRate, + @JsonProperty("fifteenMinuteRate") final String fifteenMinuteRate) + { + this.meanRate = meanRate; + this.fifteenMinuteRate = fifteenMinuteRate; + } + + @JsonProperty("meanRate") + public String meanRate() + { + return meanRate; + } + + @JsonProperty("fifteenMinuteRate") + public String fifteenMinuteRate() + { + return fifteenMinuteRate; + } + } + + /** + * Constructs a new {@link CompactionStatsResponse}. + * + * @param concurrentCompactors number of concurrent compactors + * @param pendingTasks pending compaction tasks by keyspace and table + * @param totalPendingTasks total number of pending tasks + * @param completedCompactions total compactions completed + * @param dataCompacted total data compacted in bytes + * @param abortedCompactions total compactions aborted + * @param reducedCompactions total compactions reduced + * @param sstablesDroppedFromCompaction total SSTables dropped from compaction + * @param completedCompactionsRate completed compactions rate statistics + * @param activeCompactions list of active compactions + * @param activeCompactionsCount number of active compactions + * @param activeCompactionsRemainingTime estimated remaining time for active compactions formatted as "XhYYmZZs" + */ + @JsonCreator + public CompactionStatsResponse(@JsonProperty("concurrentCompactors") long concurrentCompactors, + @JsonProperty("pendingTasks") Map<String, Map<String, Integer>> pendingTasks, + @JsonProperty("totalPendingTasks") long totalPendingTasks, + @JsonProperty("completedCompactions") long completedCompactions, + @JsonProperty("dataCompacted") long dataCompacted, + @JsonProperty("abortedCompactions") long abortedCompactions, + @JsonProperty("reducedCompactions") long reducedCompactions, + @JsonProperty("sstablesDroppedFromCompaction") long sstablesDroppedFromCompaction, + @JsonProperty("completedCompactionsRate") CompletedCompactionsRate completedCompactionsRate, + @JsonProperty("activeCompactions") List<ActiveCompactionEntry> activeCompactions, + @JsonProperty("activeCompactionsCount") long activeCompactionsCount, + @JsonProperty("activeCompactionsRemainingTime") String activeCompactionsRemainingTime) Review Comment: nit: create constants for the strings, since each of them is referenced in multiple places. ########## integration-tests/src/integrationTest/org/apache/cassandra/sidecar/routes/CompactionStatsIntegrationTest.java: ########## @@ -0,0 +1,372 @@ +/* + * 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.cassandra.sidecar.routes; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.HttpResponseExpectation; +import io.vertx.ext.web.client.HttpResponse; +import org.apache.cassandra.sidecar.common.response.CompactionStatsResponse; +import org.apache.cassandra.sidecar.common.response.data.ActiveCompactionEntry; +import org.apache.cassandra.sidecar.testing.QualifiedName; +import org.apache.cassandra.sidecar.testing.SharedClusterSidecarIntegrationTestBase; + +import static org.apache.cassandra.testing.TestUtils.DC1_RF1; +import static org.apache.cassandra.testing.TestUtils.TEST_KEYSPACE; +import static org.apache.cassandra.testing.TestUtils.TEST_TABLE_PREFIX; +import static org.apache.cassandra.testing.utils.AssertionUtils.getBlocking; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for the Compaction Statistics API endpoint. + * + * <p>Tests the compaction statistics functionality by generating large-scale SSTable data across + * multiple tables, triggering concurrent compactions, and validating API responses in both active + * and completed compaction scenarios.</p> + * + * <h2>Testing Mechanism</h2> + * <ul> + * <li><strong>Data Setup:</strong> Creates 5 tables with 100 SSTables each (500 total, 1000 records per SSTable)</li> + * <li><strong>Concurrency:</strong> Triggers compactions simultaneously using separate threads</li> + * <li><strong>Active Detection:</strong> Polls API immediately with retry logic to catch in-progress compactions</li> + * <li><strong>Comprehensive Validation:</strong> Validates all response fields including statistics, rates, + * pending tasks, and detailed active compaction data when available</li> + * <li><strong>Dual Success:</strong> Test passes whether active compactions are captured or completed</li> + * </ul> + */ +class CompactionStatsIntegrationTest extends SharedClusterSidecarIntegrationTestBase +{ + private static final String COMPACTION_STATS_ROUTE = "/api/v1/cassandra/stats/compaction"; + private static final int MAX_POLL_ATTEMPTS = 10; + private static final List<QualifiedName> TEST_TABLES = new ArrayList<>(); + private static final int TABLE_COUNT = 5; + + @Override + protected void initializeSchemaForTest() + { + createTestKeyspace(TEST_KEYSPACE, DC1_RF1); + + for (int i = 1; i <= TABLE_COUNT; i++) + { + TEST_TABLES.add(new QualifiedName(TEST_KEYSPACE, TEST_TABLE_PREFIX + "_compaction_" + i)); + } + + // Create test tables for compaction activity + for (QualifiedName tableName : TEST_TABLES) + { + createTestTable(tableName, + "CREATE TABLE %s ( \n" + + " id int PRIMARY KEY, \n" + + " data text \n" + + ");"); + } + } + + @Test + void testCompactionStatsRetrieval() + { + logger.info("Starting compaction stats test with {} tables", TEST_TABLES.size()); + + // Generate SSTables for all test tables + for (QualifiedName tableName : TEST_TABLES) + { + generateSSTables(tableName, 100); + } + + // Create threads to trigger compaction on all tables + List<Thread> compactionThreads = new ArrayList<>(); + for (QualifiedName tableName : TEST_TABLES) + { + Thread thread = new Thread(() -> triggerCompactionForTable(tableName)); + compactionThreads.add(thread); + } + + // Start all compaction threads + for (Thread thread : compactionThreads) + { + thread.start(); + } + + // Poll immediately and repeatedly to catch active compactions + CompactionStatsResponse stats = null; + HttpResponse<Buffer> response; + boolean foundActiveCompactions; + + for (int attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) Review Comment: Please check out `org.apache.cassandra.testing.utils.AssertionUtils#loopAssert(int, int, java.lang.Runnable)` -- 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: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org