You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by am...@apache.org on 2023/08/04 15:47:00 UTC
[ignite-3] branch ignite-20167 created (now 7abf3304f7)
This is an automated email from the ASF dual-hosted git repository.
amashenkov pushed a change to branch ignite-20167
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
at 7abf3304f7 SqlSchemaManager API and query flow minor refactoring.
This branch includes the following new commits:
new 7abf3304f7 SqlSchemaManager API and query flow minor refactoring.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[ignite-3] 01/01: SqlSchemaManager API and query flow minor refactoring.
Posted by am...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
amashenkov pushed a commit to branch ignite-20167
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
commit 7abf3304f7f335197106e75ce435a04b4dbe11a9
Author: amashenkov <an...@gmail.com>
AuthorDate: Fri Aug 4 18:46:51 2023 +0300
SqlSchemaManager API and query flow minor refactoring.
---
.../internal/catalog/CatalogManagerImpl.java | 5 ++
.../ignite/internal/catalog/CatalogService.java | 9 +++
.../ignite/internal/sql/api/ItCommonApiTest.java | 13 +---
.../internal/sql/engine/SqlQueryProcessor.java | 11 ++-
.../sql/engine/exec/ExecutionServiceImpl.java | 11 ++-
.../internal/sql/engine/prepare/CacheKey.java | 26 +++----
.../sql/engine/prepare/PrepareServiceImpl.java | 3 +-
.../sql/engine/schema/CatalogSqlSchemaManager.java | 87 +++++++++-------------
.../sql/engine/schema/IgniteSchemaTable.java | 4 +-
.../sql/engine/schema/SqlSchemaManager.java | 18 ++---
.../sql/engine/schema/SqlSchemaManagerImpl.java | 21 ++----
.../sql/engine/exec/ExecutionServiceImplTest.java | 4 +-
.../sql/engine/exec/MockedStructuresTest.java | 7 ++
.../engine/exec/schema/SqlSchemaManagerTest.java | 21 +++---
.../engine/framework/PredefinedSchemaManager.java | 28 ++-----
.../internal/sql/engine/framework/TestNode.java | 2 +-
.../engine/schema/CatalogSqlSchemaManagerTest.java | 24 +++---
17 files changed, 134 insertions(+), 160 deletions(-)
diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java
index 67cff40275..cb7cd43bae 100644
--- a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java
+++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java
@@ -281,6 +281,11 @@ public class CatalogManagerImpl extends Producer<CatalogEvent, CatalogEventParam
return catalogByVer.lastEntry().getKey();
}
+ @Override
+ public CompletableFuture<Void> catalogReadyFuture(int version) {
+ return versionTracker.waitFor(version);
+ }
+
private Catalog catalog(int version) {
return catalogByVer.get(version);
}
diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogService.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogService.java
index c1d30f1b4c..091e5a3a43 100644
--- a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogService.java
+++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogService.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.catalog;
import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogSchemaDescriptor;
import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor;
@@ -78,5 +79,13 @@ public interface CatalogService {
*/
int latestCatalogVersion();
+ /**
+ * Returns a future, which completes, when catalog of given version will be available.
+ *
+ * @param version Catalog version to wait for.
+ * @return Operation future.
+ */
+ CompletableFuture<Void> catalogReadyFuture(int version);
+
void listen(CatalogEvent evt, EventListener<CatalogEventParameters> closure);
}
diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItCommonApiTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItCommonApiTest.java
index 5474073978..4042ea32c0 100644
--- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItCommonApiTest.java
+++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItCommonApiTest.java
@@ -222,11 +222,6 @@ public class ItCommonApiTest extends ClusterPerClassIntegrationTest {
}
private static class ErroneousSchemaManager implements SqlSchemaManager {
- /** {@inheritDoc} */
- @Override
- public SchemaPlus schema(@Nullable String schema) {
- return null;
- }
/** {@inheritDoc} */
@Override
@@ -242,13 +237,7 @@ public class ItCommonApiTest extends ClusterPerClassIntegrationTest {
/** {@inheritDoc} */
@Override
- public CompletableFuture<SchemaPlus> actualSchemaAsync(long ver) {
- throw new UnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override
- public SchemaPlus activeSchema(@Nullable String name, long timestamp) {
+ public CompletableFuture<SchemaPlus> schemaReadyFuture(long version) {
throw new UnsupportedOperationException();
}
}
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/SqlQueryProcessor.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/SqlQueryProcessor.java
index 24db718b90..75e525bc80 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/SqlQueryProcessor.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/SqlQueryProcessor.java
@@ -43,6 +43,7 @@ import org.apache.calcite.util.Pair;
import org.apache.ignite.internal.catalog.CatalogManager;
import org.apache.ignite.internal.distributionzones.DistributionZoneManager;
import org.apache.ignite.internal.hlc.HybridClock;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
import org.apache.ignite.internal.index.IndexManager;
import org.apache.ignite.internal.index.event.IndexEvent;
import org.apache.ignite.internal.index.event.IndexEventParameters;
@@ -443,9 +444,15 @@ public class SqlQueryProcessor implements QueryProcessor {
boolean implicitTxRequired = outerTx == null;
- tx.set(implicitTxRequired ? txManager.begin(!rwOp, null) : outerTx);
+ InternalTransaction currentTx = implicitTxRequired ? txManager.begin(!rwOp, null) : outerTx;
- SchemaPlus schema = sqlSchemaManager.schema(schemaName);
+ tx.set(currentTx);
+
+ // TODO IGNITE-18733: wait for actual metadata for TX.
+ HybridTimestamp txTimestamp = currentTx.startTimestamp();
+ int observableCatalogVersion = catalogManager.activeCatalogVersion(txTimestamp.longValue());
+
+ SchemaPlus schema = sqlSchemaManager.schema(schemaName, observableCatalogVersion);
if (schema == null) {
return CompletableFuture.failedFuture(new SchemaNotFoundException(schemaName));
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImpl.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImpl.java
index 6b483f8b0e..5c547623a1 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImpl.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImpl.java
@@ -239,13 +239,13 @@ public class ExecutionServiceImpl<RowT> implements ExecutionService, TopologyEve
return queryManager.execute(tx, plan);
}
- private BaseQueryContext createQueryContext(UUID queryId, @Nullable String schema, Object[] params) {
+ private BaseQueryContext createQueryContext(UUID queryId, long version, @Nullable String schema, Object[] params) {
return BaseQueryContext.builder()
.queryId(queryId)
.parameters(params)
.frameworkConfig(
Frameworks.newConfigBuilder(FRAMEWORK_CONFIG)
- .defaultSchema(sqlSchemaManager.schema(schema))
+ .defaultSchema(sqlSchemaManager.schema(schema, (int) version))
.build()
)
.logger(LOG)
@@ -330,7 +330,10 @@ public class ExecutionServiceImpl<RowT> implements ExecutionService, TopologyEve
private void onMessage(String nodeName, QueryStartRequest msg) {
assert nodeName != null && msg != null;
- CompletableFuture<?> fut = sqlSchemaManager.actualSchemaAsync(msg.schemaVersion());
+ //TODO IGNITE-18733: We should use txTimestamp from message tx attributes to wait for metadata ready,
+ // then get actual version for txTimestamp and wait for catalog version ready.
+ // As optimization, we can lookup for latest available schema and go sync way if it's version >= desirable.
+ CompletableFuture<?> fut = sqlSchemaManager.schemaReadyFuture(msg.schemaVersion());
if (fut.isDone()) {
submitFragment(nodeName, msg);
@@ -427,7 +430,7 @@ public class ExecutionServiceImpl<RowT> implements ExecutionService, TopologyEve
private DistributedQueryManager getOrCreateQueryManager(QueryStartRequest msg) {
return queryManagerMap.computeIfAbsent(msg.queryId(), key -> {
- BaseQueryContext ctx = createQueryContext(key, msg.schema(), msg.parameters());
+ BaseQueryContext ctx = createQueryContext(key, msg.schemaVersion(), msg.schema(), msg.parameters());
return new DistributedQueryManager(ctx);
});
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/CacheKey.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/CacheKey.java
index 89cd57d3a2..ae84daaf49 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/CacheKey.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/CacheKey.java
@@ -26,7 +26,9 @@ import java.util.Objects;
* context could be schema name, dynamic parameters, and so on...
*/
public class CacheKey {
- public static final Class[] EMPTY_CLASS_ARRAY = {};
+ static final Class<?>[] EMPTY_CLASS_ARRAY = {};
+
+ private final long catalogVersion;
private final String schemaName;
@@ -34,34 +36,26 @@ public class CacheKey {
private final Object contextKey;
- private final Class[] paramTypes;
+ private final Class<?>[] paramTypes;
/**
* Constructor.
*
+ * @param catalogVersion Catalog version.
* @param schemaName Schema name.
* @param query Query string.
* @param contextKey Optional context key to differ queries with and without/different flags, having an impact on result plan (like
* LOCAL flag)
* @param paramTypes Types of all dynamic parameters, no any type can be {@code null}.
*/
- public CacheKey(String schemaName, String query, Object contextKey, Class[] paramTypes) {
+ public CacheKey(long catalogVersion, String schemaName, String query, Object contextKey, Class<?>[] paramTypes) {
+ this.catalogVersion = catalogVersion;
this.schemaName = schemaName;
this.query = query;
this.contextKey = contextKey;
this.paramTypes = paramTypes;
}
- /**
- * Constructor.
- *
- * @param schemaName Schema name.
- * @param query Query string.
- */
- public CacheKey(String schemaName, String query) {
- this(schemaName, query, null, EMPTY_CLASS_ARRAY);
- }
-
/** {@inheritDoc} */
@Override
public boolean equals(Object o) {
@@ -74,6 +68,9 @@ public class CacheKey {
CacheKey cacheKey = (CacheKey) o;
+ if (catalogVersion != cacheKey.catalogVersion) {
+ return false;
+ }
if (!schemaName.equals(cacheKey.schemaName)) {
return false;
}
@@ -89,7 +86,8 @@ public class CacheKey {
@Override
public int hashCode() {
- int result = schemaName.hashCode();
+ int result = Long.hashCode(catalogVersion);
+ result = 31 * result + schemaName.hashCode();
result = 31 * result + query.hashCode();
result = 31 * result + (contextKey != null ? contextKey.hashCode() : 0);
result = 31 * result + Arrays.deepHashCode(paramTypes);
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java
index ae7f38842e..81b58c958f 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java
@@ -310,12 +310,13 @@ public class PrepareServiceImpl implements PrepareService, SchemaUpdateListener
private static CacheKey createCacheKey(ParsedResult parsedResult, PlanningContext ctx) {
boolean distributed = distributionPresent(ctx.config().getTraitDefs());
+ long catalogVersion = ctx.unwrap(BaseQueryContext.class).schemaVersion();
Class[] paramTypes = ctx.parameters().length == 0
? EMPTY_CLASS_ARRAY :
Arrays.stream(ctx.parameters()).map(p -> (p != null) ? p.getClass() : Void.class).toArray(Class[]::new);
- return new CacheKey(ctx.schemaName(), parsedResult.normalizedQuery(), distributed, paramTypes);
+ return new CacheKey(catalogVersion, ctx.schemaName(), parsedResult.normalizedQuery(), distributed, paramTypes);
}
private ResultSetMetadata resultSetMetadata(
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManager.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManager.java
index ed27b83e91..7f9eac1f86 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManager.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManager.java
@@ -20,6 +20,8 @@ package org.apache.ignite.internal.sql.engine.schema;
import static org.apache.ignite.internal.catalog.CatalogService.DEFAULT_SCHEMA_NAME;
import com.github.benmanes.caffeine.cache.Caffeine;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -67,13 +69,6 @@ public class CatalogSqlSchemaManager implements SqlSchemaManager {
this.cache = Caffeine.newBuilder().maximumSize(cacheSize).<Map.Entry<String, Integer>, SchemaPlus>build().asMap();
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus schema(@Nullable String schema) {
- // Should be removed -schema(name, version) must be used instead
- throw new UnsupportedOperationException();
- }
-
/** {@inheritDoc} */
@Override
public SchemaPlus schema(String name, int version) {
@@ -92,74 +87,52 @@ public class CatalogSqlSchemaManager implements SqlSchemaManager {
/** {@inheritDoc} */
@Override
- public CompletableFuture<?> actualSchemaAsync(long ver) {
- return CompletableFuture.completedFuture(catalogManager.schema((int) ver));
+ public CompletableFuture<?> schemaReadyFuture(long version) {
+ // SqlSchemaManager creates SQL schema lazily on-demand, thus waiting for Catalog version is enough.
+ return catalogManager.catalogReadyFuture((int) version);
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus activeSchema(@Nullable String name, long timestamp) {
- String schemaName = name == null ? DEFAULT_SCHEMA_NAME : name;
-
- int version = catalogManager.activeCatalogVersion(timestamp);
+ private static SchemaPlus createSqlSchema(int version, CatalogSchemaDescriptor schemaDescriptor) {
+ String schemaName = schemaDescriptor.name();
- CatalogSchemaDescriptor descriptor = catalogManager.schema(schemaName, version);
-
- Entry<String, Integer> entry = Map.entry(schemaName, version);
- return cache.computeIfAbsent(entry, (v) -> createSqlSchema(v.getValue(), descriptor));
- }
-
- private SchemaPlus createSqlSchema(int version, CatalogSchemaDescriptor descriptor) {
- String schemaName = descriptor.name();
-
- int numTables = descriptor.tables().length;
+ int numTables = schemaDescriptor.tables().length;
Map<String, Table> schemaTables = new HashMap<>(numTables);
- Map<Integer, TableDescriptorImpl> tableDescriptorMap = new LinkedHashMap<>(numTables);
+ Int2ObjectMap<TableDescriptor> tableDescriptorMap = new Int2ObjectOpenHashMap<>(numTables);
// Assemble sql-engine.TableDescriptors as they are required by indexes.
- for (CatalogTableDescriptor tableDescriptor : descriptor.tables()) {
- TableDescriptorImpl descriptorImpl = createTableDescriptor(tableDescriptor);
- tableDescriptorMap.put(tableDescriptor.id(), descriptorImpl);
+ for (CatalogTableDescriptor tableDescriptor : schemaDescriptor.tables()) {
+ TableDescriptor descriptor = createTableDescriptor(tableDescriptor);
+ tableDescriptorMap.put(tableDescriptor.id(), descriptor);
}
- Map<Integer, Map<String, IgniteSchemaIndex>> schemaTableIndexes = new HashMap<>(descriptor.indexes().length);
+ Int2ObjectMap<Map<String, IgniteSchemaIndex>> schemaTableIndexes = new Int2ObjectOpenHashMap<>(schemaDescriptor.indexes().length);
// Assemble indexes as they are required by tables.
- for (CatalogIndexDescriptor indexDescriptor : descriptor.indexes()) {
+ for (CatalogIndexDescriptor indexDescriptor : schemaDescriptor.indexes()) {
int tableId = indexDescriptor.tableId();
- TableDescriptorImpl tableDescriptorImpl = tableDescriptorMap.get(tableId);
- assert tableDescriptorImpl != null : "Table is not found in schema: " + tableId;
+ TableDescriptor tableDescriptor = tableDescriptorMap.get(tableId);
+ assert tableDescriptor != null : "Table is not found in schema: " + tableId;
String indexName = indexDescriptor.name();
Map<String, IgniteSchemaIndex> tableIndexes = schemaTableIndexes.computeIfAbsent(tableId, id -> new LinkedHashMap<>());
- Type type;
- if (indexDescriptor instanceof CatalogSortedIndexDescriptor) {
- type = Type.SORTED;
- } else if (indexDescriptor instanceof CatalogHashIndexDescriptor) {
- type = Type.HASH;
- } else {
- throw new IllegalArgumentException("Unexpected index type: " + indexDescriptor);
- }
-
- RelCollation indexCollation = IgniteSchemaIndex.createIndexCollation(indexDescriptor, tableDescriptorImpl);
- IgniteSchemaIndex schemaIndex = new IgniteSchemaIndex(indexName, type, tableDescriptorImpl.distribution(), indexCollation);
+ IgniteSchemaIndex schemaIndex = createSchemaIndex(indexDescriptor, tableDescriptor);
tableIndexes.put(indexName, schemaIndex);
schemaTableIndexes.put(tableId, tableIndexes);
}
// Assemble tables.
- for (CatalogTableDescriptor tableDescriptor : descriptor.tables()) {
+ for (CatalogTableDescriptor tableDescriptor : schemaDescriptor.tables()) {
int tableId = tableDescriptor.id();
String tableName = tableDescriptor.name();
- TableDescriptorImpl descriptorImpl = tableDescriptorMap.get(tableId);
- assert descriptorImpl != null;
+ TableDescriptor descriptor = tableDescriptorMap.get(tableId);
+ assert descriptor != null;
- IgniteStatistic statistic = new IgniteStatistic(() -> 0.0d, descriptorImpl.distribution());
+ IgniteStatistic statistic = new IgniteStatistic(() -> 0.0d, descriptor.distribution());
Map<String, IgniteSchemaIndex> tableIndexMap = schemaTableIndexes.getOrDefault(tableId, Collections.emptyMap());
- IgniteSchemaTable schemaTable = new IgniteSchemaTable(tableName, tableId, version, descriptorImpl, statistic, tableIndexMap);
+ IgniteSchemaTable schemaTable = new IgniteSchemaTable(tableName, tableId, version, descriptor, statistic, tableIndexMap);
schemaTables.put(tableName, schemaTable);
}
@@ -170,7 +143,21 @@ public class CatalogSqlSchemaManager implements SqlSchemaManager {
return rootSchema.add(schemaName, igniteSchema);
}
- private static TableDescriptorImpl createTableDescriptor(CatalogTableDescriptor descriptor) {
+ private static IgniteSchemaIndex createSchemaIndex(CatalogIndexDescriptor indexDescriptor, TableDescriptor tableDescriptor) {
+ Type type;
+ if (indexDescriptor instanceof CatalogSortedIndexDescriptor) {
+ type = Type.SORTED;
+ } else if (indexDescriptor instanceof CatalogHashIndexDescriptor) {
+ type = Type.HASH;
+ } else {
+ throw new IllegalArgumentException("Unexpected index type: " + indexDescriptor);
+ }
+
+ RelCollation indexCollation = IgniteSchemaIndex.createIndexCollation(indexDescriptor, tableDescriptor);
+ return new IgniteSchemaIndex(indexDescriptor.name(), type, tableDescriptor.distribution(), indexCollation);
+ }
+
+ private static TableDescriptor createTableDescriptor(CatalogTableDescriptor descriptor) {
List<ColumnDescriptor> colDescriptors = new ArrayList<>();
List<Integer> colocationColumns = new ArrayList<>();
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchemaTable.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchemaTable.java
index b27768ddca..74cee2f891 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchemaTable.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/IgniteSchemaTable.java
@@ -187,8 +187,8 @@ public final class IgniteSchemaTable extends AbstractTable implements IgniteTabl
/** {@inheritDoc} */
@Override
public <C> @Nullable C unwrap(Class<C> cls) {
- if (cls.isInstance(this)) {
- return cls.cast(this);
+ if (cls.isInstance(desc)) {
+ return cls.cast(desc);
}
return super.unwrap(cls);
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManager.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManager.java
index ef45bc8598..10e65de3d2 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManager.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManager.java
@@ -25,11 +25,6 @@ import org.jetbrains.annotations.Nullable;
* Sql schemas operations interface.
*/
public interface SqlSchemaManager {
- /**
- * Returns a required schema if specified, or default schema otherwise.
- */
- SchemaPlus schema(@Nullable String schema);
-
/**
* Returns schema with given name and by the given version, if name is not specified, returns default schema of the given version.
*/
@@ -39,18 +34,15 @@ public interface SqlSchemaManager {
* Returns a table by given id.
*
* @param id An id of required table.
- *
* @return The table.
*/
IgniteTable tableById(int id);
/**
- * Wait for {@code ver} schema version, just a stub, need to be removed after IGNITE-18733.
- */
- CompletableFuture<?> actualSchemaAsync(long ver);
-
- /**
- * Returns a required schema if specified, or default schema otherwise.
+ * Returns a future to wait for given SQL schema version readiness.
+ *
+ * @param version SQL schema version to wait.
+ * @return Operation future.
*/
- SchemaPlus activeSchema(@Nullable String name, long timestamp);
+ CompletableFuture<?> schemaReadyFuture(long version);
}
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
index 1722d86074..540e4aeac1 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/SqlSchemaManagerImpl.java
@@ -64,6 +64,7 @@ import org.apache.ignite.internal.util.IgniteSpinBusyLock;
import org.apache.ignite.lang.IgniteInternalException;
import org.apache.ignite.lang.NodeStoppingException;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.TestOnly;
/**
* Holds actual schema and mutates it on schema change, requested by Ignite.
@@ -139,9 +140,9 @@ public class SqlSchemaManagerImpl implements SqlSchemaManager {
});
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus schema(@Nullable String schema) {
+ /** Returns latest schema. */
+ @TestOnly
+ public SchemaPlus latestSchema(@Nullable String schema) {
// stub for waiting pk indexes, more clear place is IgniteSchema
CompletableFuture.allOf(pkIdxReady.values().toArray(CompletableFuture[]::new)).join();
@@ -153,24 +154,24 @@ public class SqlSchemaManagerImpl implements SqlSchemaManager {
/** {@inheritDoc} */
@Override
public SchemaPlus schema(String name, int version) {
- throw new UnsupportedOperationException();
+ return latestSchema(name);
}
/** {@inheritDoc} */
@Override
- public CompletableFuture<?> actualSchemaAsync(long ver) {
+ public CompletableFuture<?> schemaReadyFuture(long version) {
if (!busyLock.enterBusy()) {
throw new IgniteInternalException(NODE_STOPPING_ERR, new NodeStoppingException());
}
try {
- if (ver == IgniteSchema.INITIAL_VERSION) {
+ if (version == IgniteSchema.INITIAL_VERSION) {
return completedFuture(calciteSchemaVv.latest());
}
CompletableFuture<SchemaPlus> lastSchemaFut;
try {
- lastSchemaFut = calciteSchemaVv.get(ver);
+ lastSchemaFut = calciteSchemaVv.get(version);
} catch (OutdatedTokenException e) {
return completedFuture(null);
}
@@ -181,12 +182,6 @@ public class SqlSchemaManagerImpl implements SqlSchemaManager {
}
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus activeSchema(@Nullable String name, long timestamp) {
- throw new UnsupportedOperationException();
- }
-
/** {@inheritDoc} */
@Override
public IgniteTable tableById(int id) {
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImplTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImplTest.java
index 45ff216e23..820ca0fe9d 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImplTest.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/ExecutionServiceImplTest.java
@@ -593,7 +593,7 @@ public class ExecutionServiceImplTest {
when(schemaManagerMock.tableById(anyInt())).thenReturn(table);
- when(schemaManagerMock.actualSchemaAsync(isA(long.class))).thenReturn(CompletableFuture.completedFuture(null));
+ when(schemaManagerMock.schemaReadyFuture(isA(long.class))).thenReturn(CompletableFuture.completedFuture(null));
TestExecutableTableRegistry executableTableRegistry = new TestExecutableTableRegistry();
executableTableRegistry.setColocatioGroupProvider((tableId) -> {
@@ -611,7 +611,7 @@ public class ExecutionServiceImplTest {
rootSch.add(schema.getName(), schema);
SchemaPlus plus = rootSch.plus().getSubSchema(schema.getName());
- when(schemaManagerMock.schema(any())).thenReturn(plus);
+ when(schemaManagerMock.schema(any(), anyInt())).thenReturn(plus);
var executionService = new ExecutionServiceImpl<>(
messageService,
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/MockedStructuresTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/MockedStructuresTest.java
index af6f852021..bfb46bc2c5 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/MockedStructuresTest.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/MockedStructuresTest.java
@@ -30,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
@@ -57,6 +58,7 @@ import org.apache.ignite.internal.configuration.testframework.InjectRevisionList
import org.apache.ignite.internal.distributionzones.DistributionZoneManager;
import org.apache.ignite.internal.distributionzones.configuration.DistributionZonesConfiguration;
import org.apache.ignite.internal.hlc.HybridClock;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
import org.apache.ignite.internal.index.IndexManager;
import org.apache.ignite.internal.metastorage.MetaStorageManager;
import org.apache.ignite.internal.metastorage.dsl.Operation;
@@ -94,6 +96,7 @@ import org.apache.ignite.internal.table.distributed.TableManager;
import org.apache.ignite.internal.table.distributed.raft.snapshot.outgoing.OutgoingSnapshotsManager;
import org.apache.ignite.internal.testframework.IgniteAbstractTest;
import org.apache.ignite.internal.testframework.IgniteTestUtils;
+import org.apache.ignite.internal.tx.InternalTransaction;
import org.apache.ignite.internal.tx.TxManager;
import org.apache.ignite.internal.vault.VaultManager;
import org.apache.ignite.lang.ByteArray;
@@ -578,6 +581,10 @@ public class MockedStructuresTest extends IgniteAbstractTest {
return ret;
});
+ InternalTransaction tx = mock(InternalTransaction.class);
+ when(tx.startTimestamp()).thenReturn(HybridTimestamp.MAX_VALUE);
+ when(tm.begin(anyBoolean(), any())).thenReturn(tx);
+
when(replicaManager.stopReplica(any())).thenReturn(completedFuture(true));
return createTableManager();
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/schema/SqlSchemaManagerTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/schema/SqlSchemaManagerTest.java
index eb63c18ebe..2bd1db8432 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/schema/SqlSchemaManagerTest.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/schema/SqlSchemaManagerTest.java
@@ -89,9 +89,6 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
@Mock
private TableImpl table;
- @Mock
- private Index<IndexDescriptor> index;
-
@Mock
private SchemaRegistryImpl schemaRegistry;
@@ -147,7 +144,7 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
testRevisionRegister.moveForward();
- Table schemaTable = sqlSchemaManager.schema("PUBLIC").getTable("T");
+ Table schemaTable = sqlSchemaManager.latestSchema("PUBLIC").getTable("T");
assertNotNull(schemaTable);
IgniteTableImpl igniteTable = assertInstanceOf(IgniteTableImpl.class, schemaTable);
@@ -156,7 +153,7 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
sqlSchemaManager.onTableDropped("PUBLIC", tableId, testRevisionRegister.actualToken() + 1);
testRevisionRegister.moveForward();
- assertNull(sqlSchemaManager.schema("PUBLIC").getTable("T"));
+ assertNull(sqlSchemaManager.latestSchema("PUBLIC").getTable("T"));
}
@Test
@@ -185,7 +182,7 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
testRevisionRegister.moveForward();
- assertEquals(1, ((IgniteTableImpl) sqlSchemaManager.schema("PUBLIC").getTable("T")).indexes().size());
+ assertEquals(1, ((IgniteTableImpl) sqlSchemaManager.latestSchema("PUBLIC").getTable("T")).indexes().size());
IndexDescriptor descMock = mock(IndexDescriptor.class);
when(descMock.columns()).thenReturn(List.of());
@@ -195,7 +192,7 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
testRevisionRegister.moveForward();
- IgniteSchema schema = sqlSchemaManager.schema("PUBLIC").unwrap(IgniteSchema.class);
+ IgniteSchema schema = sqlSchemaManager.latestSchema("PUBLIC").unwrap(IgniteSchema.class);
Table schemaTable = schema.getTable("T");
IgniteIndex igniteIndex = schema.index(indexId);
@@ -209,7 +206,7 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
sqlSchemaManager.onIndexDropped("PUBLIC", igniteTable.id(), indexId, testRevisionRegister.actualToken() + 1);
testRevisionRegister.moveForward();
- assertNull(sqlSchemaManager.schema("PUBLIC").unwrap(IgniteSchema.class).index(indexId));
+ assertNull(sqlSchemaManager.latestSchema("PUBLIC").unwrap(IgniteSchema.class).index(indexId));
verifyNoMoreInteractions(tableManager);
}
@@ -248,12 +245,12 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
when(descMock.name()).thenReturn(idxName);
{
- SchemaPlus schema1 = sqlSchemaManager.schema("PUBLIC");
+ SchemaPlus schema1 = sqlSchemaManager.latestSchema("PUBLIC");
sqlSchemaManager.onIndexCreated(tableId, indexId, descMock, testRevisionRegister.actualToken() + 1);
testRevisionRegister.moveForward();
- SchemaPlus schema2 = sqlSchemaManager.schema("PUBLIC");
+ SchemaPlus schema2 = sqlSchemaManager.latestSchema("PUBLIC");
// Validate schema snapshot.
assertNotSame(schema1, schema2);
@@ -267,10 +264,10 @@ public class SqlSchemaManagerTest extends BaseIgniteAbstractTest {
}
{
sqlSchemaManager.onIndexDropped("PUBLIC", table.tableId(), indexId, testRevisionRegister.actualToken() + 1);
- SchemaPlus schema1 = sqlSchemaManager.schema("PUBLIC");
+ SchemaPlus schema1 = sqlSchemaManager.latestSchema("PUBLIC");
testRevisionRegister.moveForward();
- SchemaPlus schema2 = sqlSchemaManager.schema("PUBLIC");
+ SchemaPlus schema2 = sqlSchemaManager.latestSchema("PUBLIC");
// Validate schema snapshot.
assertNotSame(schema1, schema2);
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/PredefinedSchemaManager.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/PredefinedSchemaManager.java
index c98b745510..17c46d163b 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/PredefinedSchemaManager.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/PredefinedSchemaManager.java
@@ -17,18 +17,18 @@
package org.apache.ignite.internal.sql.engine.framework;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Collection;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
-import java.util.stream.Collectors;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.tools.Frameworks;
import org.apache.ignite.internal.sql.engine.schema.IgniteSchema;
import org.apache.ignite.internal.sql.engine.schema.IgniteTable;
import org.apache.ignite.internal.sql.engine.schema.SqlSchemaManager;
+import org.apache.ignite.internal.util.CollectionUtils;
import org.jetbrains.annotations.Nullable;
/**
@@ -42,7 +42,7 @@ import org.jetbrains.annotations.Nullable;
*/
public class PredefinedSchemaManager implements SqlSchemaManager {
private final SchemaPlus root;
- private final Map<Integer, IgniteTable> tableById;
+ private final Int2ObjectMap<IgniteTable> tableById;
/** Constructs schema manager from a single schema. */
PredefinedSchemaManager(IgniteSchema schema) {
@@ -52,7 +52,7 @@ public class PredefinedSchemaManager implements SqlSchemaManager {
/** Constructs schema manager from a collection of schemas. */
PredefinedSchemaManager(Collection<IgniteSchema> schemas) {
this.root = Frameworks.createRootSchema(false);
- this.tableById = new HashMap<>();
+ this.tableById = new Int2ObjectOpenHashMap<>();
for (IgniteSchema schema : schemas) {
root.add(schema.getName(), schema);
@@ -61,35 +61,23 @@ public class PredefinedSchemaManager implements SqlSchemaManager {
schema.getTableNames().stream()
.map(schema::getTable)
.map(IgniteTable.class::cast)
- .collect(Collectors.toMap(IgniteTable::id, Function.identity()))
+ .collect(CollectionUtils.toIntMapCollector(IgniteTable::id, Function.identity()))
);
}
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus schema(@Nullable String schema) {
- return schema == null ? root : root.getSubSchema(schema);
- }
-
/** {@inheritDoc} */
@Override
public SchemaPlus schema(String name, int version) {
- return schema(name);
+ return name == null ? root : root.getSubSchema(name);
}
/** {@inheritDoc} */
@Override
- public CompletableFuture<?> actualSchemaAsync(long ver) {
+ public CompletableFuture<?> schemaReadyFuture(long version) {
return CompletableFuture.completedFuture(root);
}
- /** {@inheritDoc} */
- @Override
- public SchemaPlus activeSchema(@Nullable String name, long timestamp) {
- return schema(name);
- }
-
/** {@inheritDoc} */
@Override
public IgniteTable tableById(int id) {
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/TestNode.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/TestNode.java
index 5cf322fc81..d644f5d010 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/TestNode.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/framework/TestNode.java
@@ -103,7 +103,7 @@ public class TestNode implements LifecycleAware {
this.nodeName = nodeName;
var ps = new PrepareServiceImpl(nodeName, 0, mock(DdlSqlToCommandConverter.class), PLANNING_TIMEOUT, mock(MetricManager.class));
this.prepareService = registerService(ps);
- this.schema = schemaManager.schema("PUBLIC");
+ this.schema = schemaManager.schema("PUBLIC", -1);
TopologyService topologyService = clusterService.topologyService();
MessagingService messagingService = clusterService.messagingService();
diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManagerTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManagerTest.java
index 18b64e3644..79b1f2a67b 100644
--- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManagerTest.java
+++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/schema/CatalogSqlSchemaManagerTest.java
@@ -85,7 +85,6 @@ public class CatalogSqlSchemaManagerTest {
public void testBasicSchema() {
TestSchema testSchema = new TestSchema("TEST");
testSchema.version = 2000;
- testSchema.timestamp = 111111L;
TestTable t1 = new TestTable("T1");
t1.addColumn("c1", ColumnType.INT32);
@@ -99,7 +98,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 2000);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
assertEquals(testSchema.name, schema.getName());
@@ -156,7 +155,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteSchemaTable table = getTable(schema, testTable);
@@ -197,7 +196,7 @@ public class CatalogSqlSchemaManagerTest {
SqlSchemaManager sqlSchemaManager = newSchemaManager();
{
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
assertEquals(DEFAULT_SCHEMA_NAME, schema.getName());
@@ -205,7 +204,7 @@ public class CatalogSqlSchemaManagerTest {
}
{
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(null, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(null, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
assertEquals(DEFAULT_SCHEMA_NAME, schema.getName());
@@ -234,7 +233,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteTable table = getTable(schema, testTable);
@@ -274,7 +273,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteTable table = (IgniteTable) schema.getTable(testTable.name);
@@ -302,7 +301,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteTable table = (IgniteTable) schema.getTable(testTable.name);
@@ -336,7 +335,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteTable table = (IgniteTable) schema.getTable(testTable.name);
@@ -369,7 +368,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteSchemaTable table = (IgniteSchemaTable) schema.getTable(testTable.name);
@@ -413,7 +412,7 @@ public class CatalogSqlSchemaManagerTest {
testSchema.init(catalogManager);
SqlSchemaManager sqlSchemaManager = newSchemaManager();
- SchemaPlus schemaPlus = sqlSchemaManager.activeSchema(testSchema.name, testSchema.timestamp);
+ SchemaPlus schemaPlus = sqlSchemaManager.schema(testSchema.name, 1111);
IgniteCatalogSchema schema = unwrapSchema(schemaPlus);
IgniteSchemaTable table = (IgniteSchemaTable) schema.getTable(testTable.name);
@@ -463,13 +462,10 @@ public class CatalogSqlSchemaManagerTest {
this.name = name;
}
- long timestamp = System.nanoTime();
-
int version = 1111;
void init(CatalogManager catalogManager) {
CatalogSchemaDescriptor schemaDescriptor = newSchemaDescriptor(version);
- when(catalogManager.activeCatalogVersion(timestamp)).thenReturn(version);
when(catalogManager.schema(name != null ? name : DEFAULT_SCHEMA_NAME, version)).thenReturn(schemaDescriptor);
}