rpuch commented on code in PR #2682:
URL: https://github.com/apache/ignite-3/pull/2682#discussion_r1356605380


##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuildCompletionListener.java:
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.ignite.internal.table.distributed.index;
+
+/** Index build completion listener. */

Review Comment:
   This javadoc just repeats the interface name word by word. Can this be 
replaced by something that is not obvious from the type name? :) Like why it's 
needed and how it's used, and whether it's about local build completion or 
global (distributed) build completion.



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuildCompletionListener.java:
##########
@@ -0,0 +1,24 @@
+/*
+ * 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.ignite.internal.table.distributed.index;
+
+/** Index build completion listener. */
+public interface IndexBuildCompletionListener {
+    /** Handles the index build completion event. */
+    void apply(int indexId, int tableId, int partitionId);

Review Comment:
   `apply()` is a natural name for something that takes some arguments and 
produces a result, but here nothing is returned. How about something like 
`onBuildCompletion(...)`?



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuilder.java:
##########
@@ -43,12 +46,23 @@
 import org.apache.ignite.network.ClusterNode;
 
 /**
- * Class for managing the building of table indexes.
+ * Component that is responsible for building an index for a specific 
partition.
+ *
+ * <p>Approximate index building algorithm:</p>
+ * <ul>
+ *     <li>If the index has not yet been built ({@link 
IndexStorage#getNextRowIdToBuild()} {@code != null}) or is not in the process of
+ *     being built, then an asynchronous task is added to build it.</li>
+ *     <li>Index building task generates batches of {@link RowId} (by using 
{@link IndexStorage#getNextRowIdToBuild()}) and sends this batch
+ *     to the primary replica (only the primary replica is expected to start 
building the index) so that the corresponding replication group

Review Comment:
   So on which node does `IndexBuilder` itself work (if it sends batches to a 
primary replica)? Is it colocated with the primary replica? How is it 
guaranteed that only one `IndexBuilder` drives the index build process?



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuilder.java:
##########
@@ -43,12 +46,23 @@
 import org.apache.ignite.network.ClusterNode;
 
 /**
- * Class for managing the building of table indexes.
+ * Component that is responsible for building an index for a specific 
partition.
+ *
+ * <p>Approximate index building algorithm:</p>
+ * <ul>
+ *     <li>If the index has not yet been built ({@link 
IndexStorage#getNextRowIdToBuild()} {@code != null}) or is not in the process of
+ *     being built, then an asynchronous task is added to build it.</li>
+ *     <li>Index building task generates batches of {@link RowId} (by using 
{@link IndexStorage#getNextRowIdToBuild()}) and sends this batch

Review Comment:
   ```suggestion
    *     <li>Index building task generates batches of {@link RowId}s (by using 
{@link IndexStorage#getNextRowIdToBuild()}) and sends these batches
   ```



##########
modules/table/src/test/java/org/apache/ignite/internal/table/distributed/index/IndexBuilderTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.ignite.internal.table.distributed.index;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutFast;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.IntStream;
+import org.apache.ignite.internal.replicator.ReplicaService;
+import org.apache.ignite.internal.replicator.message.ReplicaRequest;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.index.IndexStorage;
+import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest;
+import org.apache.ignite.network.ClusterNode;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** For {@link IndexBuilder} testing. */
+public class IndexBuilderTest extends BaseIgniteAbstractTest {
+    private static final int TABLE_ID = 1;
+
+    private static final int INDEX_ID = 2;
+
+    private static final int PARTITION_ID = 3;
+
+    private final ReplicaService replicaService = mock(ReplicaService.class, 
invocation -> completedFuture(null));
+
+    private IndexBuilder indexBuilder;
+
+    @BeforeEach
+    void setUp() {
+        indexBuilder = new IndexBuilder("test", 1, replicaService);

Review Comment:
   This can be moved to the field initializer



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/index/IndexBuildTask.java:
##########
@@ -87,6 +90,7 @@ class IndexBuildTask {
         this.busyLock = busyLock;
         this.batchSize = batchSize;
         this.node = node;
+        this.listeners = listeners;

Review Comment:
   Is it intended that `listeners` is live (that is, the list sees changes made 
in `IndexBuilder`)?



##########
modules/table/src/test/java/org/apache/ignite/internal/table/distributed/index/IndexBuilderTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.ignite.internal.table.distributed.index;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutFast;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.IntStream;
+import org.apache.ignite.internal.replicator.ReplicaService;
+import org.apache.ignite.internal.replicator.message.ReplicaRequest;
+import org.apache.ignite.internal.storage.MvPartitionStorage;
+import org.apache.ignite.internal.storage.RowId;
+import org.apache.ignite.internal.storage.index.IndexStorage;
+import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest;
+import org.apache.ignite.network.ClusterNode;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** For {@link IndexBuilder} testing. */
+public class IndexBuilderTest extends BaseIgniteAbstractTest {
+    private static final int TABLE_ID = 1;
+
+    private static final int INDEX_ID = 2;
+
+    private static final int PARTITION_ID = 3;
+
+    private final ReplicaService replicaService = mock(ReplicaService.class, 
invocation -> completedFuture(null));
+
+    private IndexBuilder indexBuilder;
+
+    @BeforeEach
+    void setUp() {
+        indexBuilder = new IndexBuilder("test", 1, replicaService);
+    }
+
+    @AfterEach
+    void tearDown() {
+        indexBuilder.close();
+    }
+
+    @Test
+    void testIndexBuildCompletionListener() {
+        CompletableFuture<Void> listenCompletionIndexBuildingFuture = 
listenCompletionIndexBuilding(INDEX_ID, TABLE_ID, PARTITION_ID);
+
+        scheduleBuildIndex(INDEX_ID, TABLE_ID, PARTITION_ID, 
List.of(rowId(PARTITION_ID)));
+
+        assertThat(listenCompletionIndexBuildingFuture, 
willCompleteSuccessfully());
+    }
+
+    @Test
+    void testStopListenIndexBuildCompletion() {
+        CompletableFuture<Void> invokeListenerFuture = new 
CompletableFuture<>();
+
+        IndexBuildCompletionListener listener = (indexId, tableId, 
partitionId) -> invokeListenerFuture.complete(null);
+
+        indexBuilder.listen(listener);
+        indexBuilder.stopListen(listener);
+
+        scheduleBuildIndex(INDEX_ID, TABLE_ID, PARTITION_ID, 
List.of(rowId(PARTITION_ID)));
+
+        assertThat(invokeListenerFuture, willTimeoutFast());
+    }
+
+    @Test
+    void testIndexBuildCompletionListenerTwoBatches() {
+        CompletableFuture<Void> listenCompletionIndexBuildingFuture = 
listenCompletionIndexBuilding(INDEX_ID, TABLE_ID, PARTITION_ID);
+
+        List<RowId> nextRowIdsToBuild = IntStream.range(0, 2 * 
IndexBuilder.BATCH_SIZE)
+                .mapToObj(i -> rowId(PARTITION_ID))
+                .collect(toList());
+
+        CompletableFuture<Void> secondInvokeReplicaServiceFuture = new 
CompletableFuture<>();
+
+        CompletableFuture<Void> awaitSecondInvokeForReplicaService = 
awaitSecondInvokeForReplicaService(secondInvokeReplicaServiceFuture);
+
+        scheduleBuildIndex(INDEX_ID, TABLE_ID, PARTITION_ID, 
nextRowIdsToBuild);
+
+        assertThat(awaitSecondInvokeForReplicaService, 
willCompleteSuccessfully());
+
+        
assertFalse(listenCompletionIndexBuildingFuture.newIncompleteFuture().isDone());

Review Comment:
   This looks weird as `newIncompleteFuture().isDone()` will always be `false`



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

Reply via email to