tkalkirill commented on code in PR #1800:
URL: https://github.com/apache/ignite-3/pull/1800#discussion_r1144808772
##########
modules/index/src/main/java/org/apache/ignite/internal/index/IndexManager.java:
##########
@@ -534,32 +584,159 @@ public BinaryTuple convert(BinaryRow binaryRow) {
}
private class ConfigurationListener implements
ConfigurationNamedListListener<TableIndexView> {
- /** {@inheritDoc} */
@Override
- public @NotNull CompletableFuture<?> onCreate(@NotNull
ConfigurationNotificationEvent<TableIndexView> ctx) {
+ public CompletableFuture<?>
onCreate(ConfigurationNotificationEvent<TableIndexView> ctx) {
return onIndexCreate(ctx);
}
- /** {@inheritDoc} */
@Override
- public @NotNull CompletableFuture<?> onRename(
+ public CompletableFuture<?> onRename(
String oldName,
String newName,
ConfigurationNotificationEvent<TableIndexView> ctx
) {
return failedFuture(new
UnsupportedOperationException("https://issues.apache.org/jira/browse/IGNITE-16196"));
}
- /** {@inheritDoc} */
@Override
- public @NotNull CompletableFuture<?> onDelete(@NotNull
ConfigurationNotificationEvent<TableIndexView> ctx) {
+ public CompletableFuture<?>
onDelete(ConfigurationNotificationEvent<TableIndexView> ctx) {
return onIndexDrop(ctx);
}
- /** {@inheritDoc} */
@Override
- public @NotNull CompletableFuture<?> onUpdate(@NotNull
ConfigurationNotificationEvent<TableIndexView> ctx) {
+ public CompletableFuture<?>
onUpdate(ConfigurationNotificationEvent<TableIndexView> ctx) {
return failedFuture(new IllegalStateException("Should not be
called"));
}
}
+
+ /**
+ * Initializes the build of the index.
+ */
+ private void initIndexBuildIfNeeded(TableIndexView tableIndexView,
TableImpl table) {
+ for (int partitionId = 0; partitionId <
table.internalTable().partitions(); partitionId++) {
+ buildIndexExecutor.submit(new BuildIndexTask(table,
tableIndexView, partitionId, true));
+ }
+ }
+
+ /**
+ * Task of building a table index for a partition.
+ *
+ * <p>Only the leader of the raft group will manage the building of the
index. Leader sends batches of row IDs via
+ * {@link BuildIndexCommand}, the next batch will only be send after the
previous batch has been processed.
+ *
+ * <p>Index building itself occurs locally on each node of the raft group
(majority) when processing {@link BuildIndexCommand}. This
+ * ensures that the index build in the raft group is consistent and that
the index build is restored after restarting the raft group
+ * (not from the beginning).
+ */
+ private class BuildIndexTask implements Runnable {
+ private final TableImpl table;
+
+ private final TableIndexView tableIndexView;
+
+ private final int partitionId;
+
+ private final boolean firstBatch;
+
+ private BuildIndexTask(TableImpl table, TableIndexView tableIndexView,
int partitionId, boolean firstBatch) {
+ this.table = table;
+ this.tableIndexView = tableIndexView;
+ this.partitionId = partitionId;
+ this.firstBatch = firstBatch;
+ }
+
+ @Override
+ public void run() {
+ if (!busyLock.enterBusy()) {
+ return;
+ }
+
+ try {
+ InternalTable internalTable = table.internalTable();
+
+ RaftGroupService raftGroupService =
internalTable.partitionRaftGroupService(partitionId);
+
+ if (!isLocalNodeLeader(raftGroupService)) {
+ // TODO: IGNITE-19053 Must handle the change of leader
+ return;
+ }
+
+ RowId lastBuildRowId =
internalTable.storage().getOrCreateIndex(partitionId,
tableIndexView.id()).getLastBuildRowId();
+
+ if (lastBuildRowId == null) {
+ // Index has already been built.
+ return;
+ }
+
+ if (firstBatch) {
+ LOG.info("Start building the index: [{}]",
createCommonTableIndexInfo());
+ }
+
+ List<RowId> batchRowIds = createBatchRowIds(lastBuildRowId,
BUILD_INDEX_ROW_ID_BATCH_SIZE);
+
+ boolean finish = batchRowIds.size() <
BUILD_INDEX_ROW_ID_BATCH_SIZE;
+
+ raftGroupService.run(createBuildIndexCommand(batchRowIds,
finish))
+ .thenAccept(unused -> {
+ if (!finish) {
+ buildIndexExecutor.submit(new
BuildIndexTask(table, tableIndexView, partitionId, false));
+ }
+ });
+ } catch (Throwable t) {
+ LOG.error("Index build error: [{}]", t,
createCommonTableIndexInfo());
+ } finally {
+ busyLock.leaveBusy();
+ }
+ }
+
+ private boolean isLocalNodeLeader(RaftGroupService raftGroupService) {
+ Peer leader = raftGroupService.leader();
+
+ assert leader != null : "tableId=" + table.tableId() + ",
partitionId=" + partitionId;
+
+ return localNodeConsistentId().equals(leader.consistentId());
+ }
+
+ private List<RowId> createBatchRowIds(RowId lastBuildRowId, int
batchSize) {
+ MvPartitionStorage mvPartition =
table.internalTable().storage().getMvPartition(partitionId);
+
+ assert mvPartition != null : createCommonTableIndexInfo();
+
+ List<RowId> batch = new ArrayList<>(batchSize);
+
+ for (int i = 0; i < batchSize; i++) {
+ lastBuildRowId =
mvPartition.closestRowId(lastBuildRowId.increment());
+
+ if (lastBuildRowId == null) {
+ break;
+ }
+
+ batch.add(lastBuildRowId);
+ }
+
+ return batch;
+ }
+
+ private BuildIndexCommand createBuildIndexCommand(List<RowId> rowIds,
boolean finish) {
+ return TABLE_MESSAGES_FACTORY.buildIndexCommand()
+
.tablePartitionId(TABLE_MESSAGES_FACTORY.tablePartitionIdMessage()
+ .tableId(table.tableId())
+ .partitionId(partitionId)
+ .build()
+ )
+ .indexId(tableIndexView.id())
+ .rowIds(rowIds.stream().map(RowId::uuid).collect(toList()))
+ .finish(finish)
+ .build();
+ }
+
+ private String createCommonTableIndexInfo() {
+ return "table=" + table.name() + ", tableId=" + table.tableId()
+ + ", partitionId=" + partitionId
+ + ", index=" + tableIndexView.name() + ", indexId=" +
tableIndexView.id();
+ }
+ }
+
+ private String localNodeConsistentId() {
Review Comment:
Ok.
--
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]