Copilot commented on code in PR #3404: URL: https://github.com/apache/fluss/pull/3404#discussion_r3331434638
########## fluss-server/src/test/java/org/apache/fluss/server/coordinator/CoordinatorServiceOrphanRpcsITCase.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.fluss.server.coordinator; + +import org.apache.fluss.fs.FsPath; +import org.apache.fluss.metadata.Schema; +import org.apache.fluss.metadata.TableBucket; +import org.apache.fluss.metadata.TableDescriptor; +import org.apache.fluss.metadata.TablePath; +import org.apache.fluss.rpc.gateway.CoordinatorGateway; +import org.apache.fluss.rpc.messages.ListKvSnapshotsRequest; +import org.apache.fluss.rpc.messages.ListKvSnapshotsResponse; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsRequest; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsResponse; +import org.apache.fluss.rpc.messages.PbKvSnapshot; +import org.apache.fluss.rpc.messages.PbRemoteLogManifestEntry; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshot; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandleStore; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotStore; +import org.apache.fluss.server.kv.snapshot.KvSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.SharedKvFileRegistry; +import org.apache.fluss.server.testutils.FlussClusterExtension; +import org.apache.fluss.server.testutils.RpcMessageTestUtils; +import org.apache.fluss.server.zk.ZooKeeperClient; +import org.apache.fluss.server.zk.data.RemoteLogManifestHandle; +import org.apache.fluss.types.DataTypes; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; Review Comment: This import becomes unnecessary once the helper uses a direct executor (and keeping it will fail compilation due to an unused import). ########## fluss-server/src/main/java/org/apache/fluss/server/coordinator/CoordinatorService.java: ########## @@ -776,6 +788,139 @@ public CompletableFuture<MetadataResponse> metadata(MetadataRequest request) { return metadataResponseAccessContextEvent.getResultFuture(); } + @Override + public CompletableFuture<ListRemoteLogManifestsResponse> listRemoteLogManifests( + ListRemoteLogManifestsRequest request) { + long tableId = request.getTableId(); + // Capture session before entering async block since currentSession() is thread-local + Session session = authorizer != null ? currentSession() : null; + return CompletableFuture.supplyAsync( + () -> { + if (authorizer != null) { + authorizeTableWithSession(session, OperationType.DESCRIBE, tableId); + } + Long partitionId = request.hasPartitionId() ? request.getPartitionId() : null; + ListRemoteLogManifestsResponse response = new ListRemoteLogManifestsResponse(); + try { + List<ZooKeeperClient.TableBucketAndManifest> entries = + zkClient.listRemoteLogManifestHandles(tableId, partitionId); + for (ZooKeeperClient.TableBucketAndManifest entry : entries) { + PbRemoteLogManifestEntry pb = response.addManifest(); + PbTableBucket pbTb = pb.setTableBucket(); + pbTb.setTableId(entry.getTableBucket().getTableId()); + if (entry.getTableBucket().getPartitionId() != null) { + pbTb.setPartitionId(entry.getTableBucket().getPartitionId()); + } + pbTb.setBucketId(entry.getTableBucket().getBucket()); + pb.setRemoteLogManifestPath( + entry.getManifestHandle() + .getRemoteLogManifestPath() + .toString()); + pb.setRemoteLogEndOffset( + entry.getManifestHandle().getRemoteLogEndOffset()); + } + } catch (ApiException e) { + throw e; + } catch (Exception e) { + throw new UnknownServerException( + "Failed to list remote log manifests for tableId=" + tableId, e); + } + return response; + }, + ioExecutor); + } + + @Override + public CompletableFuture<ListKvSnapshotsResponse> listKvSnapshots( + ListKvSnapshotsRequest request) { + long tableId = request.getTableId(); + Session session = authorizer != null ? currentSession() : null; + return CompletableFuture.supplyAsync( + () -> { + if (authorizer != null) { + authorizeTableWithSession(session, OperationType.DESCRIBE, tableId); + } + Long partitionId = request.hasPartitionId() ? request.getPartitionId() : null; + + // Resolve table metadata: prefer in-memory context, fallback to ZK assignment + // (single getData) to handle the race between createTable and context sync. + CoordinatorContext context = coordinatorContextSupplier.get(); + TablePath tablePath = context.getTablePathById(tableId); + int numBuckets; + if (tablePath != null) { + TableInfo tableInfo = context.getTableInfoById(tableId); + if (tableInfo == null) { + throw new ApiException( + "Table metadata not yet available for table " + tableId); + } + numBuckets = tableInfo.getNumBuckets(); + } else { + try { + if (partitionId != null) { + Optional<PartitionAssignment> pAssignment = + zkClient.getPartitionAssignment(partitionId); + if (!pAssignment.isPresent()) { + throw new ApiException( + "Partition " + partitionId + " does not exist"); + } + numBuckets = pAssignment.get().getBuckets().size(); + } else { + Optional<TableAssignment> assignment = + zkClient.getTableAssignment(tableId); + if (!assignment.isPresent()) { + throw new ApiException("Table " + tableId + " does not exist"); + } + numBuckets = assignment.get().getBuckets().size(); + } + } catch (ApiException e) { + throw e; + } catch (Exception e) { + throw new ApiException( + "Failed to resolve table " + tableId + ": " + e.getMessage()); + } Review Comment: The generic Exception catch drops the root cause by creating a new ApiException without passing the original exception. This makes debugging production failures harder because the stack trace is lost. ########## fluss-server/src/test/java/org/apache/fluss/server/coordinator/CoordinatorServiceOrphanRpcsITCase.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.fluss.server.coordinator; + +import org.apache.fluss.fs.FsPath; +import org.apache.fluss.metadata.Schema; +import org.apache.fluss.metadata.TableBucket; +import org.apache.fluss.metadata.TableDescriptor; +import org.apache.fluss.metadata.TablePath; +import org.apache.fluss.rpc.gateway.CoordinatorGateway; +import org.apache.fluss.rpc.messages.ListKvSnapshotsRequest; +import org.apache.fluss.rpc.messages.ListKvSnapshotsResponse; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsRequest; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsResponse; +import org.apache.fluss.rpc.messages.PbKvSnapshot; +import org.apache.fluss.rpc.messages.PbRemoteLogManifestEntry; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshot; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandleStore; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotStore; +import org.apache.fluss.server.kv.snapshot.KvSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.SharedKvFileRegistry; +import org.apache.fluss.server.testutils.FlussClusterExtension; +import org.apache.fluss.server.testutils.RpcMessageTestUtils; +import org.apache.fluss.server.zk.ZooKeeperClient; +import org.apache.fluss.server.zk.data.RemoteLogManifestHandle; +import org.apache.fluss.types.DataTypes; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; Review Comment: This import becomes unnecessary once the helper uses a direct executor (and keeping it will fail compilation due to an unused import). ########## fluss-server/src/test/java/org/apache/fluss/server/coordinator/CoordinatorServiceOrphanRpcsITCase.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.fluss.server.coordinator; + +import org.apache.fluss.fs.FsPath; +import org.apache.fluss.metadata.Schema; +import org.apache.fluss.metadata.TableBucket; +import org.apache.fluss.metadata.TableDescriptor; +import org.apache.fluss.metadata.TablePath; +import org.apache.fluss.rpc.gateway.CoordinatorGateway; +import org.apache.fluss.rpc.messages.ListKvSnapshotsRequest; +import org.apache.fluss.rpc.messages.ListKvSnapshotsResponse; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsRequest; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsResponse; +import org.apache.fluss.rpc.messages.PbKvSnapshot; +import org.apache.fluss.rpc.messages.PbRemoteLogManifestEntry; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshot; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandleStore; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotStore; +import org.apache.fluss.server.kv.snapshot.KvSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.SharedKvFileRegistry; +import org.apache.fluss.server.testutils.FlussClusterExtension; +import org.apache.fluss.server.testutils.RpcMessageTestUtils; +import org.apache.fluss.server.zk.ZooKeeperClient; +import org.apache.fluss.server.zk.data.RemoteLogManifestHandle; +import org.apache.fluss.types.DataTypes; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** ITCase for orphan-cleanup-related coordinator RPCs: ListRemoteLogManifests, ListKvSnapshots. */ +class CoordinatorServiceOrphanRpcsITCase { + + @RegisterExtension + public static final FlussClusterExtension FLUSS_CLUSTER_EXTENSION = + FlussClusterExtension.builder().setNumOfTabletServers(1).build(); + + private static final TablePath PK_TABLE_PATH = TablePath.of("orphan_rpc_test_db", "pk_table"); + private static final TableDescriptor PK_TABLE_DESCRIPTOR = + TableDescriptor.builder() + .schema( + Schema.newBuilder() + .column("id", DataTypes.INT()) + .withComment("pk col") + .column("val", DataTypes.STRING()) + .withComment("value col") + .primaryKey("id") + .build()) + .distributedBy(3, "id") + .build(); + + private static final int MAX_SNAPSHOTS_TO_RETAIN = 2; + + private static long pkTableId; + @TempDir static Path tempDir; + + @BeforeAll + static void setupTables() throws Exception { + pkTableId = + RpcMessageTestUtils.createTable( + FLUSS_CLUSTER_EXTENSION, PK_TABLE_PATH, PK_TABLE_DESCRIPTOR); + FLUSS_CLUSTER_EXTENSION.waitUntilTableReady(pkTableId); + } + + // ------------------------------------------------------------------------- + // ListRemoteLogManifests + // ------------------------------------------------------------------------- + + @Test + void listManifestsForUnpartitionedTable() throws Exception { + long tableId = 999_111L; + ZooKeeperClient zk = FLUSS_CLUSTER_EXTENSION.getZooKeeperClient(); + zk.upsertRemoteLogManifestHandle( + new TableBucket(tableId, 0), + new RemoteLogManifestHandle(new FsPath("oss://bucket-0/m.manifest"), 100L)); + zk.upsertRemoteLogManifestHandle( + new TableBucket(tableId, 1), + new RemoteLogManifestHandle(new FsPath("oss://bucket-1/m.manifest"), 200L)); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListRemoteLogManifestsRequest request = new ListRemoteLogManifestsRequest(); + request.setTableId(tableId); + ListRemoteLogManifestsResponse response = gateway.listRemoteLogManifests(request).get(); + + assertThat(response.getManifestsList()).hasSize(2); + assertThat(response.getManifestsList()) + .extracting(PbRemoteLogManifestEntry::getRemoteLogEndOffset) + .containsExactlyInAnyOrder(100L, 200L); + } + + @Test + void listManifestsForUnknownTableReturnsEmpty() throws Exception { + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListRemoteLogManifestsRequest request = new ListRemoteLogManifestsRequest(); + request.setTableId(999_999L); + ListRemoteLogManifestsResponse response = gateway.listRemoteLogManifests(request).get(); + + assertThat(response.getManifestsList()).isEmpty(); + } + + // ------------------------------------------------------------------------- + // ListKvSnapshots + // ------------------------------------------------------------------------- + + @Test + void listSnapshotsReturnsRetainedSubset() throws Exception { + TableBucket tb = new TableBucket(pkTableId, 0); + CompletedSnapshotStore store = createTestStore(tb, snapshot -> false); + for (long snapId = 1L; snapId <= 4L; snapId++) { + store.add(createMinimalSnapshot(tb, snapId)); + } + injectStore(tb, store); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(pkTableId); + ListKvSnapshotsResponse response = gateway.listKvSnapshots(request).get(); + + assertThat(response.getTableId()).isEqualTo(pkTableId); + assertThat(response.hasPartitionId()).isFalse(); + List<PbKvSnapshot> bucket0 = + response.getActiveSnapshotsList().stream() + .filter(s -> s.getBucketId() == 0) + .collect(Collectors.toList()); + assertThat(bucket0) + .extracting(PbKvSnapshot::getSnapshotId) + .containsExactlyInAnyOrder(3L, 4L); + } + + @Test + void listSnapshotsReturnsStillInUseBeyondRetention() throws Exception { + TableBucket tb = new TableBucket(pkTableId, 1); + CompletedSnapshotStore store = + createTestStore(tb, snapshot -> snapshot.getSnapshotID() == 1L); + for (long snapId = 1L; snapId <= 4L; snapId++) { + store.add(createMinimalSnapshot(tb, snapId)); + } + injectStore(tb, store); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(pkTableId); + ListKvSnapshotsResponse response = gateway.listKvSnapshots(request).get(); + + List<PbKvSnapshot> bucket1Snapshots = + response.getActiveSnapshotsList().stream() + .filter(s -> s.getBucketId() == 1) + .collect(Collectors.toList()); + // RETAINED = {3, 4}, STILL_IN_USE = {1} + assertThat(bucket1Snapshots) + .extracting(PbKvSnapshot::getSnapshotId) + .containsExactlyInAnyOrder(1L, 3L, 4L); + } + + @Test + void listSnapshotsForUnknownTableFails() { + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(999_888L); + + assertThatThrownBy(() -> gateway.listKvSnapshots(request).get()) + .isInstanceOf(ExecutionException.class); + } + + // ------------------------------------------------------------------------- + // Helpers + // ------------------------------------------------------------------------- + + private CompletedSnapshotStore createTestStore( + TableBucket tb, CompletedSnapshotStore.SnapshotInUseChecker inUseChecker) { + return new CompletedSnapshotStore( + MAX_SNAPSHOTS_TO_RETAIN, + new SharedKvFileRegistry(Executors.newSingleThreadExecutor()), + Collections.emptyList(), + new NoOpSnapshotHandleStore(), + Executors.newSingleThreadExecutor(), + inUseChecker); Review Comment: This helper creates new single-thread executors for every test store but never shuts them down. These non-daemon threads can leak across tests and potentially prevent the JVM from exiting. Since this ITCase doesn’t need async cleanup behavior, prefer direct executors / the default SharedKvFileRegistry constructor to avoid thread creation entirely. ########## fluss-server/src/test/java/org/apache/fluss/server/coordinator/CoordinatorServiceOrphanRpcsITCase.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.fluss.server.coordinator; + +import org.apache.fluss.fs.FsPath; +import org.apache.fluss.metadata.Schema; +import org.apache.fluss.metadata.TableBucket; +import org.apache.fluss.metadata.TableDescriptor; +import org.apache.fluss.metadata.TablePath; +import org.apache.fluss.rpc.gateway.CoordinatorGateway; +import org.apache.fluss.rpc.messages.ListKvSnapshotsRequest; +import org.apache.fluss.rpc.messages.ListKvSnapshotsResponse; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsRequest; +import org.apache.fluss.rpc.messages.ListRemoteLogManifestsResponse; +import org.apache.fluss.rpc.messages.PbKvSnapshot; +import org.apache.fluss.rpc.messages.PbRemoteLogManifestEntry; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshot; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotHandleStore; +import org.apache.fluss.server.kv.snapshot.CompletedSnapshotStore; +import org.apache.fluss.server.kv.snapshot.KvSnapshotHandle; +import org.apache.fluss.server.kv.snapshot.SharedKvFileRegistry; +import org.apache.fluss.server.testutils.FlussClusterExtension; +import org.apache.fluss.server.testutils.RpcMessageTestUtils; +import org.apache.fluss.server.zk.ZooKeeperClient; +import org.apache.fluss.server.zk.data.RemoteLogManifestHandle; +import org.apache.fluss.types.DataTypes; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** ITCase for orphan-cleanup-related coordinator RPCs: ListRemoteLogManifests, ListKvSnapshots. */ +class CoordinatorServiceOrphanRpcsITCase { + + @RegisterExtension + public static final FlussClusterExtension FLUSS_CLUSTER_EXTENSION = + FlussClusterExtension.builder().setNumOfTabletServers(1).build(); + + private static final TablePath PK_TABLE_PATH = TablePath.of("orphan_rpc_test_db", "pk_table"); + private static final TableDescriptor PK_TABLE_DESCRIPTOR = + TableDescriptor.builder() + .schema( + Schema.newBuilder() + .column("id", DataTypes.INT()) + .withComment("pk col") + .column("val", DataTypes.STRING()) + .withComment("value col") + .primaryKey("id") + .build()) + .distributedBy(3, "id") + .build(); + + private static final int MAX_SNAPSHOTS_TO_RETAIN = 2; + + private static long pkTableId; + @TempDir static Path tempDir; + + @BeforeAll + static void setupTables() throws Exception { + pkTableId = + RpcMessageTestUtils.createTable( + FLUSS_CLUSTER_EXTENSION, PK_TABLE_PATH, PK_TABLE_DESCRIPTOR); + FLUSS_CLUSTER_EXTENSION.waitUntilTableReady(pkTableId); + } + + // ------------------------------------------------------------------------- + // ListRemoteLogManifests + // ------------------------------------------------------------------------- + + @Test + void listManifestsForUnpartitionedTable() throws Exception { + long tableId = 999_111L; + ZooKeeperClient zk = FLUSS_CLUSTER_EXTENSION.getZooKeeperClient(); + zk.upsertRemoteLogManifestHandle( + new TableBucket(tableId, 0), + new RemoteLogManifestHandle(new FsPath("oss://bucket-0/m.manifest"), 100L)); + zk.upsertRemoteLogManifestHandle( + new TableBucket(tableId, 1), + new RemoteLogManifestHandle(new FsPath("oss://bucket-1/m.manifest"), 200L)); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListRemoteLogManifestsRequest request = new ListRemoteLogManifestsRequest(); + request.setTableId(tableId); + ListRemoteLogManifestsResponse response = gateway.listRemoteLogManifests(request).get(); + + assertThat(response.getManifestsList()).hasSize(2); + assertThat(response.getManifestsList()) + .extracting(PbRemoteLogManifestEntry::getRemoteLogEndOffset) + .containsExactlyInAnyOrder(100L, 200L); + } + + @Test + void listManifestsForUnknownTableReturnsEmpty() throws Exception { + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListRemoteLogManifestsRequest request = new ListRemoteLogManifestsRequest(); + request.setTableId(999_999L); + ListRemoteLogManifestsResponse response = gateway.listRemoteLogManifests(request).get(); + + assertThat(response.getManifestsList()).isEmpty(); + } + + // ------------------------------------------------------------------------- + // ListKvSnapshots + // ------------------------------------------------------------------------- + + @Test + void listSnapshotsReturnsRetainedSubset() throws Exception { + TableBucket tb = new TableBucket(pkTableId, 0); + CompletedSnapshotStore store = createTestStore(tb, snapshot -> false); + for (long snapId = 1L; snapId <= 4L; snapId++) { + store.add(createMinimalSnapshot(tb, snapId)); + } + injectStore(tb, store); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(pkTableId); + ListKvSnapshotsResponse response = gateway.listKvSnapshots(request).get(); + + assertThat(response.getTableId()).isEqualTo(pkTableId); + assertThat(response.hasPartitionId()).isFalse(); + List<PbKvSnapshot> bucket0 = + response.getActiveSnapshotsList().stream() + .filter(s -> s.getBucketId() == 0) + .collect(Collectors.toList()); + assertThat(bucket0) + .extracting(PbKvSnapshot::getSnapshotId) + .containsExactlyInAnyOrder(3L, 4L); + } + + @Test + void listSnapshotsReturnsStillInUseBeyondRetention() throws Exception { + TableBucket tb = new TableBucket(pkTableId, 1); + CompletedSnapshotStore store = + createTestStore(tb, snapshot -> snapshot.getSnapshotID() == 1L); + for (long snapId = 1L; snapId <= 4L; snapId++) { + store.add(createMinimalSnapshot(tb, snapId)); + } + injectStore(tb, store); + + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(pkTableId); + ListKvSnapshotsResponse response = gateway.listKvSnapshots(request).get(); + + List<PbKvSnapshot> bucket1Snapshots = + response.getActiveSnapshotsList().stream() + .filter(s -> s.getBucketId() == 1) + .collect(Collectors.toList()); + // RETAINED = {3, 4}, STILL_IN_USE = {1} + assertThat(bucket1Snapshots) + .extracting(PbKvSnapshot::getSnapshotId) + .containsExactlyInAnyOrder(1L, 3L, 4L); + } + + @Test + void listSnapshotsForUnknownTableFails() { + CoordinatorGateway gateway = FLUSS_CLUSTER_EXTENSION.newCoordinatorClient(); + ListKvSnapshotsRequest request = new ListKvSnapshotsRequest(); + request.setTableId(999_888L); + + assertThatThrownBy(() -> gateway.listKvSnapshots(request).get()) + .isInstanceOf(ExecutionException.class); + } + + // ------------------------------------------------------------------------- + // Helpers + // ------------------------------------------------------------------------- + + private CompletedSnapshotStore createTestStore( + TableBucket tb, CompletedSnapshotStore.SnapshotInUseChecker inUseChecker) { + return new CompletedSnapshotStore( + MAX_SNAPSHOTS_TO_RETAIN, + new SharedKvFileRegistry(Executors.newSingleThreadExecutor()), + Collections.emptyList(), + new NoOpSnapshotHandleStore(), + Executors.newSingleThreadExecutor(), + inUseChecker); Review Comment: This helper creates new single-thread executors for every test store but never shuts them down. These non-daemon threads can leak across tests and potentially prevent the JVM from exiting. Since this ITCase doesn’t need async cleanup behavior, prefer direct executors / the default SharedKvFileRegistry constructor to avoid thread creation entirely. ########## fluss-server/src/main/java/org/apache/fluss/server/coordinator/CoordinatorService.java: ########## @@ -776,6 +788,139 @@ public CompletableFuture<MetadataResponse> metadata(MetadataRequest request) { return metadataResponseAccessContextEvent.getResultFuture(); } + @Override + public CompletableFuture<ListRemoteLogManifestsResponse> listRemoteLogManifests( + ListRemoteLogManifestsRequest request) { + long tableId = request.getTableId(); + // Capture session before entering async block since currentSession() is thread-local + Session session = authorizer != null ? currentSession() : null; + return CompletableFuture.supplyAsync( + () -> { + if (authorizer != null) { + authorizeTableWithSession(session, OperationType.DESCRIBE, tableId); + } + Long partitionId = request.hasPartitionId() ? request.getPartitionId() : null; + ListRemoteLogManifestsResponse response = new ListRemoteLogManifestsResponse(); + try { + List<ZooKeeperClient.TableBucketAndManifest> entries = + zkClient.listRemoteLogManifestHandles(tableId, partitionId); + for (ZooKeeperClient.TableBucketAndManifest entry : entries) { + PbRemoteLogManifestEntry pb = response.addManifest(); + PbTableBucket pbTb = pb.setTableBucket(); + pbTb.setTableId(entry.getTableBucket().getTableId()); + if (entry.getTableBucket().getPartitionId() != null) { + pbTb.setPartitionId(entry.getTableBucket().getPartitionId()); + } + pbTb.setBucketId(entry.getTableBucket().getBucket()); + pb.setRemoteLogManifestPath( + entry.getManifestHandle() + .getRemoteLogManifestPath() + .toString()); + pb.setRemoteLogEndOffset( + entry.getManifestHandle().getRemoteLogEndOffset()); + } + } catch (ApiException e) { + throw e; + } catch (Exception e) { + throw new UnknownServerException( + "Failed to list remote log manifests for tableId=" + tableId, e); + } + return response; + }, + ioExecutor); + } + + @Override + public CompletableFuture<ListKvSnapshotsResponse> listKvSnapshots( + ListKvSnapshotsRequest request) { + long tableId = request.getTableId(); + Session session = authorizer != null ? currentSession() : null; + return CompletableFuture.supplyAsync( + () -> { + if (authorizer != null) { + authorizeTableWithSession(session, OperationType.DESCRIBE, tableId); + } + Long partitionId = request.hasPartitionId() ? request.getPartitionId() : null; + + // Resolve table metadata: prefer in-memory context, fallback to ZK assignment + // (single getData) to handle the race between createTable and context sync. + CoordinatorContext context = coordinatorContextSupplier.get(); + TablePath tablePath = context.getTablePathById(tableId); + int numBuckets; + if (tablePath != null) { + TableInfo tableInfo = context.getTableInfoById(tableId); + if (tableInfo == null) { + throw new ApiException( + "Table metadata not yet available for table " + tableId); + } + numBuckets = tableInfo.getNumBuckets(); + } else { + try { + if (partitionId != null) { + Optional<PartitionAssignment> pAssignment = + zkClient.getPartitionAssignment(partitionId); + if (!pAssignment.isPresent()) { + throw new ApiException( + "Partition " + partitionId + " does not exist"); + } + numBuckets = pAssignment.get().getBuckets().size(); + } else { + Optional<TableAssignment> assignment = + zkClient.getTableAssignment(tableId); + if (!assignment.isPresent()) { + throw new ApiException("Table " + tableId + " does not exist"); + } + numBuckets = assignment.get().getBuckets().size(); + } + } catch (ApiException e) { + throw e; + } catch (Exception e) { + throw new ApiException( + "Failed to resolve table " + tableId + ": " + e.getMessage()); + } Review Comment: The generic Exception catch drops the root cause by creating a new ApiException without passing the original exception. This makes debugging production failures harder because the stack trace is lost. -- 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]
