Search in sources :

Example 1 with TableAlreadyExistsException

use of org.apache.ignite.lang.TableAlreadyExistsException in project ignite-3 by apache.

the class DdlCommandHandler method handleCreateTable.

/**
 * Handles create table command.
 */
private void handleCreateTable(CreateTableCommand cmd) {
    final PrimaryKeyDefinitionBuilder pkeyDef = SchemaBuilders.primaryKey();
    pkeyDef.withColumns(IgniteObjectName.quoteNames(cmd.primaryKeyColumns()));
    pkeyDef.withColocationColumns(IgniteObjectName.quoteNames(cmd.affColumns()));
    final IgniteTypeFactory typeFactory = Commons.typeFactory();
    final List<org.apache.ignite.schema.definition.ColumnDefinition> colsInner = new ArrayList<>();
    for (ColumnDefinition col : cmd.columns()) {
        ColumnDefinitionBuilder col0 = SchemaBuilders.column(IgniteObjectName.quote(col.name()), typeFactory.columnType(col.type())).asNullable(col.nullable()).withDefaultValueExpression(col.defaultValue());
        colsInner.add(col0.build());
    }
    Consumer<TableChange> tblChanger = tblCh -> {
        TableChange conv = convert(SchemaBuilders.tableBuilder(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName())).columns(colsInner).withPrimaryKey(pkeyDef.build()).build(), tblCh);
        if (cmd.partitions() != null) {
            conv.changePartitions(cmd.partitions());
        }
        if (cmd.replicas() != null) {
            conv.changeReplicas(cmd.replicas());
        }
    };
    String fullName = TableDefinitionImpl.canonicalName(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName()));
    try {
        tableManager.createTable(fullName, tblChanger);
    } catch (TableAlreadyExistsException ex) {
        if (!cmd.ifTableExists()) {
            throw ex;
        }
    }
}
Also used : SortedIndexDefinitionBuilder(org.apache.ignite.schema.definition.builder.SortedIndexDefinitionBuilder) Pair(org.apache.ignite.internal.util.Pair) CollectionUtils.nullOrEmpty(org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) IgniteException(org.apache.ignite.lang.IgniteException) ColumnDefinition(org.apache.ignite.internal.sql.engine.prepare.ddl.ColumnDefinition) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) DropTableCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DropTableCommand) Map(java.util.Map) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) PrimaryKeyView(org.apache.ignite.configuration.schemas.table.PrimaryKeyView) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) TableDefinitionImpl(org.apache.ignite.internal.schema.definition.TableDefinitionImpl) TableManager(org.apache.ignite.internal.table.distributed.TableManager) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) SortedIndexColumnBuilder(org.apache.ignite.schema.definition.builder.SortedIndexDefinitionBuilder.SortedIndexColumnBuilder) NamedListView(org.apache.ignite.configuration.NamedListView) AbstractTableDdlCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AbstractTableDdlCommand) CreateIndexCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.CreateIndexCommand) Set(java.util.Set) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) Collectors(java.util.stream.Collectors) Commons(org.apache.ignite.internal.sql.engine.util.Commons) AlterTableDropCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableDropCommand) Consumer(java.util.function.Consumer) DdlCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DdlCommand) CreateTableCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.CreateTableCommand) List(java.util.List) IgniteInternalCheckedException(org.apache.ignite.lang.IgniteInternalCheckedException) AlterTableAddCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.AlterTableAddCommand) DropIndexCommand(org.apache.ignite.internal.sql.engine.prepare.ddl.DropIndexCommand) ColumnAlreadyExistsException(org.apache.ignite.lang.ColumnAlreadyExistsException) ColumnNotFoundException(org.apache.ignite.lang.ColumnNotFoundException) IndexAlreadyExistsException(org.apache.ignite.lang.IndexAlreadyExistsException) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) IgniteTypeFactory(org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory) ArrayList(java.util.ArrayList) ColumnDefinition(org.apache.ignite.internal.sql.engine.prepare.ddl.ColumnDefinition) PrimaryKeyDefinitionBuilder(org.apache.ignite.schema.definition.builder.PrimaryKeyDefinitionBuilder) TableChange(org.apache.ignite.configuration.schemas.table.TableChange)

Example 2 with TableAlreadyExistsException

use of org.apache.ignite.lang.TableAlreadyExistsException in project ignite-3 by apache.

the class TableManager method createTableAsyncInternal.

/**
 * Internal method that creates a new table with the given {@code name} asynchronously. If a table with the same name already exists,
 * a future will be completed with {@link TableAlreadyExistsException}.
 *
 * @param name            Table name.
 * @param tableInitChange Table changer.
 * @return Future representing pending completion of the operation.
 * @throws IgniteException If an unspecified platform exception has happened internally. Is thrown when:
 *                         <ul>
 *                             <li>the node is stopping.</li>
 *                         </ul>
 * @see TableAlreadyExistsException
 */
private CompletableFuture<Table> createTableAsyncInternal(String name, Consumer<TableChange> tableInitChange) {
    CompletableFuture<Table> tblFut = new CompletableFuture<>();
    tableAsyncInternal(name).thenAccept(tbl -> {
        if (tbl != null) {
            tblFut.completeExceptionally(new TableAlreadyExistsException(name));
        } else {
            tablesCfg.tables().change(change -> {
                if (change.get(name) != null) {
                    throw new TableAlreadyExistsException(name);
                }
                change.create(name, (ch) -> {
                    tableInitChange.accept(ch);
                    var extConfCh = ((ExtendedTableChange) ch);
                    tableCreateFuts.put(extConfCh.id(), tblFut);
                    // Affinity assignments calculation.
                    extConfCh.changeAssignments(ByteUtils.toBytes(AffinityUtils.calculateAssignments(baselineMgr.nodes(), ch.partitions(), ch.replicas()))).changeSchemas(schemasCh -> schemasCh.create(String.valueOf(INITIAL_SCHEMA_VERSION), schemaCh -> {
                        SchemaDescriptor schemaDesc;
                        // prepareSchemaDescriptor() method.
                        try {
                            schemaDesc = SchemaUtils.prepareSchemaDescriptor(((ExtendedTableView) ch).schemas().size(), ch);
                        } catch (IllegalArgumentException ex) {
                            throw new ConfigurationValidationException(ex.getMessage());
                        }
                        schemaCh.changeSchema(SchemaSerializerImpl.INSTANCE.serialize(schemaDesc));
                    }));
                });
            }).exceptionally(t -> {
                Throwable ex = getRootCause(t);
                if (ex instanceof TableAlreadyExistsException) {
                    tblFut.completeExceptionally(ex);
                } else {
                    LOG.error(IgniteStringFormatter.format("Table wasn't created [name={}]", name), ex);
                    tblFut.completeExceptionally(ex);
                    tableCreateFuts.values().removeIf(fut -> fut == tblFut);
                }
                return null;
            });
        }
    });
    return tblFut;
}
Also used : IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) ConfigurationUtil.directProxy(org.apache.ignite.internal.configuration.util.ConfigurationUtil.directProxy) ConfigurationUtil.getByInternalId(org.apache.ignite.internal.configuration.util.ConfigurationUtil.getByInternalId) ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) Path(java.nio.file.Path) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) IgniteComponent(org.apache.ignite.internal.manager.IgniteComponent) SchemaConfiguration(org.apache.ignite.internal.configuration.schema.SchemaConfiguration) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DEFAULT_DATA_REGION_NAME(org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema.DEFAULT_DATA_REGION_NAME) CompletionException(java.util.concurrent.CompletionException) Producer(org.apache.ignite.internal.manager.Producer) StorageEngine(org.apache.ignite.internal.storage.engine.StorageEngine) UUID(java.util.UUID) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) Collectors(java.util.stream.Collectors) TxManager(org.apache.ignite.internal.tx.TxManager) TableView(org.apache.ignite.configuration.schemas.table.TableView) ClusterNode(org.apache.ignite.network.ClusterNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) InternalTable(org.apache.ignite.internal.table.InternalTable) IgniteException(org.apache.ignite.lang.IgniteException) Loza(org.apache.ignite.internal.raft.Loza) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) ByteUtils(org.apache.ignite.internal.util.ByteUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaSerializerImpl(org.apache.ignite.internal.schema.marshaller.schema.SchemaSerializerImpl) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SchemaView(org.apache.ignite.internal.configuration.schema.SchemaView) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteSpinBusyLock(org.apache.ignite.internal.util.IgniteSpinBusyLock) IgniteTables(org.apache.ignite.table.manager.IgniteTables) NoSuchElementException(java.util.NoSuchElementException) VersionedValue(org.apache.ignite.internal.causality.VersionedValue) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) Files(java.nio.file.Files) NamedListView(org.apache.ignite.configuration.NamedListView) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) BaselineManager(org.apache.ignite.internal.baseline.BaselineManager) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) ConfigurationNamedListListener(org.apache.ignite.configuration.notifications.ConfigurationNamedListListener) NetworkAddress(org.apache.ignite.network.NetworkAddress) Consumer(java.util.function.Consumer) ConfigurationNotificationEvent(org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent) TableEvent(org.apache.ignite.internal.table.event.TableEvent) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) EventListener(org.apache.ignite.internal.manager.EventListener) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) Table(org.apache.ignite.table.Table) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) RocksDbStorageEngine(org.apache.ignite.internal.storage.rocksdb.RocksDbStorageEngine) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) InternalTable(org.apache.ignite.internal.table.InternalTable) Table(org.apache.ignite.table.Table)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 NamedListView (org.apache.ignite.configuration.NamedListView)2 TableChange (org.apache.ignite.configuration.schemas.table.TableChange)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 NoSuchElementException (java.util.NoSuchElementException)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1