korlov42 commented on code in PR #3424:
URL: https://github.com/apache/ignite-3/pull/3424#discussion_r1530352182
##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ddl/DdlCommandHandler.java:
##########
@@ -169,12 +200,86 @@ private static BiFunction<Object, Throwable, Boolean>
handleModificationResult(b
/** Handles create index command. */
private CompletableFuture<Boolean> handleCreateIndex(CreateIndexCommand
cmd) {
return
catalogManager.execute(DdlToCatalogCommandConverter.convert(cmd))
+ .thenCompose(catalogVersion -> inBusyLock(busyLock, () ->
waitTillIndexBecomesAvailableOrRemoved(cmd, catalogVersion)))
.handle(handleModificationResult(cmd.ifNotExists(),
IndexExistsValidationException.class));
}
+ private CompletionStage<Void>
waitTillIndexBecomesAvailableOrRemoved(CreateIndexCommand cmd, Integer
creationCatalogVersion) {
+ CompletableFuture<Void> future = inFlightFutures.registerFuture(new
CompletableFuture<>());
+
+ Catalog catalog = catalogManager.catalog(creationCatalogVersion);
+ assert catalog != null : creationCatalogVersion;
+
+ CatalogSchemaDescriptor schema = catalog.schema(cmd.schemaName());
+ assert schema != null : "Did not find schema " + cmd.schemaName() + "
in version " + creationCatalogVersion;
+
+ CatalogIndexDescriptor index = schema.aliveIndex(cmd.indexName());
+ assert index != null
+ : "Did not find index " + cmd.indexName() + " in schema " +
cmd.schemaName() + " in version " + creationCatalogVersion;
+
+ EventListener<CatalogEventParameters> availabilityListener =
EventListener.fromConsumer(event -> {
+ if (((MakeIndexAvailableEventParameters) event).indexId() ==
index.id()) {
+ completeFutureWhenEventVersionActivates(future, event);
+ }
+ });
+ catalogManager.listen(CatalogEvent.INDEX_AVAILABLE,
availabilityListener);
+
+ EventListener<CatalogEventParameters> removalListener =
EventListener.fromConsumer(event -> {
+ if (((RemoveIndexEventParameters) event).indexId() == index.id()) {
+ future.complete(null);
+ }
+ });
+ catalogManager.listen(CatalogEvent.INDEX_REMOVED, removalListener);
+
+ // We added listeners, but the index could switch to a state of
interest before we added them, so check
+ // explicitly.
+ int latestVersion = catalogManager.latestCatalogVersion();
+ for (int version = creationCatalogVersion + 1; version <=
latestVersion; version++) {
Review Comment:
why do we iterate over all future versions? Is it not enough to check the
latest one?
##########
modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItIndexDdlTest.java:
##########
@@ -98,4 +114,52 @@ private static void tryToCreateIndex(String tableName,
String indexName, boolean
private static void tryToDropIndex(String indexName, boolean
failIfNotExist) {
sql(String.format("DROP INDEX %s", failIfNotExist ? indexName : "IF
EXISTS " + indexName));
}
+
+ private static <T> T preventingIndexBuild(Supplier<T> supplier) {
+ return
CLUSTER.aliveNode().transactions().runInTransaction((Function<Transaction, T>)
tx -> supplier.get());
+ }
+
+ @Test
+ public void createIndexFutureCompletesWhenIndexBecomesAvailable() {
Review Comment:
it would be nice to have e2e tests ensuring index is available immediately
after control is returned from sql api. For such a test it does make sense to
return delay duration to its default (1s)
--
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]