Search in sources :

Example 1 with InternalTable

use of org.apache.ignite.internal.table.InternalTable in project ignite-3 by apache.

the class SqlSchemaManagerTest method testTableEventIsProcessedRequiredVersionIsLess.

@Test
public void testTableEventIsProcessedRequiredVersionIsLess() {
    when(table.schemaView()).thenReturn(schemaRegistry);
    when(table.name()).thenReturn("TEST_SCHEMA.T");
    InternalTable mock = mock(InternalTable.class);
    when(mock.tableId()).thenReturn(tableId);
    when(table.internalTable()).thenReturn(mock);
    when(schemaRegistry.schema()).thenReturn(schemaDescriptor);
    when(schemaRegistry.lastSchemaVersion()).thenReturn(schemaDescriptor.version());
    schemaManager.onTableCreated("TEST_SCHEMA", table, testRevisionRegister.actualToken() + 1);
    testRevisionRegister.moveForward();
    IgniteTable actTable = schemaManager.tableById(tableId, tableVer - 1);
    assertEquals(tableId, actTable.id());
    Mockito.verifyNoMoreInteractions(tableManager);
}
Also used : IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) InternalTable(org.apache.ignite.internal.table.InternalTable) Test(org.junit.jupiter.api.Test)

Example 2 with InternalTable

use of org.apache.ignite.internal.table.InternalTable in project ignite-3 by apache.

the class SqlSchemaManagerTest method testOnTableDroppedHandler.

@Test
public void testOnTableDroppedHandler() {
    when(table.schemaView()).thenReturn(schemaRegistry);
    when(table.name()).thenReturn("TEST_SCHEMA.T");
    InternalTable mock = mock(InternalTable.class);
    when(mock.tableId()).thenReturn(tableId);
    when(table.internalTable()).thenReturn(mock);
    when(schemaRegistry.schema()).thenReturn(schemaDescriptor);
    when(schemaRegistry.lastSchemaVersion()).thenReturn(schemaDescriptor.version());
    schemaManager.onTableCreated("TEST_SCHEMA", table, testRevisionRegister.actualToken() + 1);
    testRevisionRegister.moveForward();
    Table schemaTable = schemaManager.schema("TEST_SCHEMA").getTable("T");
    assertNotNull(schemaTable);
    IgniteTableImpl igniteTable = assertInstanceOf(IgniteTableImpl.class, schemaTable);
    assertEquals(tableId, igniteTable.table().tableId());
    schemaManager.onTableDropped("TEST_SCHEMA", table.name(), testRevisionRegister.actualToken() + 1);
    testRevisionRegister.moveForward();
    assertNull(schemaManager.schema("TEST_SCHEMA").getTable("T"));
}
Also used : InternalTable(org.apache.ignite.internal.table.InternalTable) Table(org.apache.calcite.schema.Table) IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) IgniteTableImpl(org.apache.ignite.internal.sql.engine.schema.IgniteTableImpl) InternalTable(org.apache.ignite.internal.table.InternalTable) Test(org.junit.jupiter.api.Test)

Example 3 with InternalTable

use of org.apache.ignite.internal.table.InternalTable in project ignite-3 by apache.

the class TableManager method createTableLocally.

/**
 * Creates local structures for a table.
 *
 * @param causalityToken Causality token.
 * @param name  Table name.
 * @param tblId Table id.
 * @param assignment Affinity assignment.
 */
private void createTableLocally(long causalityToken, String name, UUID tblId, List<List<ClusterNode>> assignment, SchemaDescriptor schemaDesc) {
    int partitions = assignment.size();
    var partitionsGroupsFutures = new ArrayList<CompletableFuture<RaftGroupService>>();
    Path storageDir = partitionsStoreDir.resolve(name);
    try {
        Files.createDirectories(storageDir);
    } catch (IOException e) {
        throw new IgniteInternalException("Failed to create partitions store directory for " + name + ": " + e.getMessage(), e);
    }
    TableConfiguration tableCfg = tablesCfg.tables().get(name);
    DataRegion dataRegion = dataRegions.computeIfAbsent(tableCfg.dataRegion().value(), dataRegionName -> {
        DataRegion newDataRegion = engine.createDataRegion(dataStorageCfg.regions().get(dataRegionName));
        try {
            newDataRegion.start();
        } catch (Exception e) {
            try {
                newDataRegion.stop();
            } catch (Exception stopException) {
                e.addSuppressed(stopException);
            }
            throw e;
        }
        return newDataRegion;
    });
    TableStorage tableStorage = engine.createTable(storageDir, tableCfg, dataRegion);
    tableStorage.start();
    for (int p = 0; p < partitions; p++) {
        int partId = p;
        try {
            partitionsGroupsFutures.add(raftMgr.prepareRaftGroup(raftGroupName(tblId, p), assignment.get(p), () -> new PartitionListener(tblId, new VersionedRowStore(tableStorage.getOrCreatePartition(partId), txManager))));
        } catch (NodeStoppingException e) {
            throw new AssertionError("Loza was stopped before Table manager", e);
        }
    }
    CompletableFuture.allOf(partitionsGroupsFutures.toArray(CompletableFuture[]::new)).thenRun(() -> {
        try {
            Int2ObjectOpenHashMap<RaftGroupService> partitionMap = new Int2ObjectOpenHashMap<>(partitions);
            for (int p = 0; p < partitions; p++) {
                CompletableFuture<RaftGroupService> future = partitionsGroupsFutures.get(p);
                assert future.isDone();
                RaftGroupService service = future.join();
                partitionMap.put(p, service);
            }
            InternalTableImpl internalTable = new InternalTableImpl(name, tblId, partitionMap, partitions, netAddrResolver, txManager, tableStorage);
            var schemaRegistry = new SchemaRegistryImpl(v -> {
                if (!busyLock.enterBusy()) {
                    throw new IgniteException(new NodeStoppingException());
                }
                try {
                    return tableSchema(tblId, v);
                } finally {
                    busyLock.leaveBusy();
                }
            }, () -> {
                if (!busyLock.enterBusy()) {
                    throw new IgniteException(new NodeStoppingException());
                }
                try {
                    return latestSchemaVersion(tblId);
                } finally {
                    busyLock.leaveBusy();
                }
            });
            schemaRegistry.onSchemaRegistered(schemaDesc);
            var table = new TableImpl(internalTable, schemaRegistry);
            tablesVv.update(causalityToken, previous -> {
                var val = previous == null ? new HashMap() : new HashMap<>(previous);
                val.put(name, table);
                return val;
            }, th -> {
                throw new IgniteInternalException(IgniteStringFormatter.format("Cannot create a table [name={}, id={}]", name, tblId), th);
            });
            tablesByIdVv.update(causalityToken, previous -> {
                var val = previous == null ? new HashMap() : new HashMap<>(previous);
                val.put(tblId, table);
                return val;
            }, th -> {
                throw new IgniteInternalException(IgniteStringFormatter.format("Cannot create a table [name={}, id={}]", name, tblId), th);
            });
            completeApiCreateFuture(table);
            fireEvent(TableEvent.CREATE, new TableEventParameters(causalityToken, table), null);
        } catch (Exception e) {
            fireEvent(TableEvent.CREATE, new TableEventParameters(causalityToken, tblId, name), e);
        }
    }).join();
}
Also used : TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) 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) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteException(org.apache.ignite.lang.IgniteException) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) Path(java.nio.file.Path) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) IOException(java.io.IOException) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) CompletionException(java.util.concurrent.CompletionException) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteException(org.apache.ignite.lang.IgniteException) NoSuchElementException(java.util.NoSuchElementException) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IOException(java.io.IOException) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion)

Example 4 with InternalTable

use of org.apache.ignite.internal.table.InternalTable in project ignite-3 by apache.

the class SqlSchemaManagerTest method testTableEventIsNotProcessed.

@Test
public void testTableEventIsNotProcessed() throws NodeStoppingException {
    when(tableManager.table(eq(tableId))).thenReturn(table);
    when(table.schemaView()).thenReturn(schemaRegistry);
    InternalTable mock = mock(InternalTable.class);
    when(mock.tableId()).thenReturn(tableId);
    when(table.internalTable()).thenReturn(mock);
    when(schemaRegistry.schema()).thenReturn(schemaDescriptor);
    when(schemaRegistry.lastSchemaVersion()).thenReturn(schemaDescriptor.version());
    IgniteTable actTable = schemaManager.tableById(tableId, tableVer);
    assertEquals(tableId, actTable.id());
    Mockito.verify(tableManager).table(eq(tableId));
    Mockito.verifyNoMoreInteractions(tableManager);
}
Also used : IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) InternalTable(org.apache.ignite.internal.table.InternalTable) Test(org.junit.jupiter.api.Test)

Example 5 with InternalTable

use of org.apache.ignite.internal.table.InternalTable in project ignite-3 by apache.

the class SqlSchemaManagerTest method testTableEventIsProcessedRequiredVersionIsGreater.

@Test
public void testTableEventIsProcessedRequiredVersionIsGreater() throws NodeStoppingException {
    when(table.schemaView()).thenReturn(schemaRegistry);
    when(table.name()).thenReturn("TEST_SCHEMA.T");
    InternalTable mock = mock(InternalTable.class);
    when(mock.tableId()).thenReturn(tableId);
    when(table.internalTable()).thenReturn(mock);
    when(schemaRegistry.schema()).thenReturn(schemaDescriptor);
    when(schemaRegistry.lastSchemaVersion()).thenReturn(tableVer - 1);
    schemaManager.onTableCreated("TEST_SCHEMA", table, testRevisionRegister.actualToken() + 1);
    testRevisionRegister.moveForward();
    when(tableManager.table(eq(tableId))).thenReturn(table);
    when(schemaRegistry.lastSchemaVersion()).thenReturn(tableVer);
    IgniteTable actTable = schemaManager.tableById(tableId, tableVer);
    assertEquals(tableId, actTable.id());
    IgniteInternalException ex = assertThrows(IgniteInternalException.class, () -> schemaManager.tableById(tableId, tableVer + 1));
    assertThat(ex.getMessage(), containsString("Table version not found"));
    Mockito.verify(tableManager, times(2)).table(eq(tableId));
    Mockito.verifyNoMoreInteractions(tableManager);
}
Also used : IgniteTable(org.apache.ignite.internal.sql.engine.schema.IgniteTable) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) InternalTable(org.apache.ignite.internal.table.InternalTable) Test(org.junit.jupiter.api.Test)

Aggregations

InternalTable (org.apache.ignite.internal.table.InternalTable)7 IgniteTable (org.apache.ignite.internal.sql.engine.schema.IgniteTable)5 Test (org.junit.jupiter.api.Test)5 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 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 NoSuchElementException (java.util.NoSuchElementException)2 Set (java.util.Set)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionException (java.util.concurrent.CompletionException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Consumer (java.util.function.Consumer)2