use of org.apache.ignite.internal.table.TableImpl in project ignite-3 by apache.
the class ItDataSchemaSyncTest method test.
/**
* The test executes various operation over the lagging node.
* The operations can be executed only the node overtakes a distributed cluster state.
*/
@Test
public void test() throws Exception {
Ignite ignite0 = clusterNodes.get(0);
final IgniteImpl ignite1 = (IgniteImpl) clusterNodes.get(1);
createTable(ignite0, SCHEMA, SHORT_TABLE_NAME);
TableImpl table = (TableImpl) ignite0.tables().table(TABLE_NAME);
assertEquals(1, table.schemaView().schema().version());
for (int i = 0; i < 10; i++) {
table.recordView().insert(null, Tuple.create().set("key", (long) i).set("valInt", i).set("valStr", "str_" + i));
}
WatchListenerInhibitor listenerInhibitor = WatchListenerInhibitor.metastorageEventsInhibitor(ignite1);
listenerInhibitor.startInhibit();
ColumnDefinition columnDefinition = SchemaBuilders.column("valStr2", ColumnType.string()).withDefaultValueExpression("default").build();
ignite0.tables().alterTable(TABLE_NAME, tblChanger -> tblChanger.changeColumns(cols -> cols.create(columnDefinition.name(), colChg -> convert(columnDefinition, colChg))));
for (Ignite node : clusterNodes) {
if (node == ignite1) {
continue;
}
TableImpl tableOnNode = (TableImpl) node.tables().table(TABLE_NAME);
IgniteTestUtils.waitForCondition(() -> tableOnNode.schemaView().lastSchemaVersion() == 2, 10_000);
}
TableImpl table1 = (TableImpl) ignite1.tables().table(TABLE_NAME);
for (int i = 10; i < 20; i++) {
table.recordView().insert(null, Tuple.create().set("key", (long) i).set("valInt", i).set("valStr", "str_" + i).set("valStr2", "str2_" + i));
}
final CompletableFuture insertFut = IgniteTestUtils.runAsync(() -> table1.recordView().insert(null, Tuple.create().set("key", 0L).set("valInt", 0).set("valStr", "str_" + 0).set("valStr2", "str2_" + 0)));
final CompletableFuture getFut = IgniteTestUtils.runAsync(() -> {
table1.recordView().get(null, Tuple.create().set("key", 10L));
});
final CompletableFuture checkDefaultFut = IgniteTestUtils.runAsync(() -> {
assertEquals("default", table1.recordView().get(null, Tuple.create().set("key", 0L)).value("valStr2"));
});
assertEquals(1, table1.schemaView().lastSchemaVersion());
assertFalse(getFut.isDone());
assertFalse(insertFut.isDone());
assertFalse(checkDefaultFut.isDone());
listenerInhibitor.stopInhibit();
getFut.get(10, TimeUnit.SECONDS);
insertFut.get(10, TimeUnit.SECONDS);
checkDefaultFut.get(10, TimeUnit.SECONDS);
for (Ignite node : clusterNodes) {
Table tableOnNode = node.tables().table(TABLE_NAME);
for (int i = 0; i < 20; i++) {
Tuple row = tableOnNode.recordView().get(null, Tuple.create().set("key", (long) i));
assertNotNull(row);
assertEquals(i, row.intValue("valInt"));
assertEquals("str_" + i, row.value("valStr"));
assertEquals(i < 10 ? "default" : ("str2_" + i), row.value("valStr2"));
}
}
}
use of org.apache.ignite.internal.table.TableImpl in project ignite-3 by apache.
the class SqlSchemaManagerImpl method convert.
private IgniteTableImpl convert(TableImpl table) {
SchemaDescriptor descriptor = table.schemaView().schema();
List<ColumnDescriptor> colDescriptors = descriptor.columnNames().stream().map(descriptor::column).sorted(Comparator.comparingInt(Column::columnOrder)).map(col -> new ColumnDescriptorImpl(col.name(), descriptor.isKeyColumn(col.schemaIndex()), col.columnOrder(), col.schemaIndex(), col.type(), col::defaultValue)).collect(Collectors.toList());
return new IgniteTableImpl(new TableDescriptorImpl(colDescriptors), table.internalTable(), table.schemaView());
}
use of org.apache.ignite.internal.table.TableImpl 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();
}
use of org.apache.ignite.internal.table.TableImpl in project ignite-3 by apache.
the class TableManager method stop.
/**
* {@inheritDoc}
*/
@Override
public void stop() {
if (!stopGuard.compareAndSet(false, true)) {
return;
}
busyLock.block();
Map<String, TableImpl> tables = tablesVv.latest();
if (tables != null) {
for (TableImpl table : tables.values()) {
try {
table.internalTable().storage().stop();
table.internalTable().close();
for (int p = 0; p < table.internalTable().partitions(); p++) {
raftMgr.stopRaftGroup(raftGroupName(table.tableId(), p));
}
} catch (Exception e) {
LOG.error("Failed to stop a table {}", e, table.name());
}
}
}
// Stop all data regions when all table storages are stopped.
for (Map.Entry<String, DataRegion> entry : dataRegions.entrySet()) {
try {
entry.getValue().stop();
} catch (Exception e) {
LOG.error("Failed to stop data region " + entry.getKey(), e);
}
}
engine.stop();
}
use of org.apache.ignite.internal.table.TableImpl in project ignite-3 by apache.
the class JdbcMetadataCatalog method getColumnsMeta.
/**
* See {@link DatabaseMetaData#getColumns(String, String, String, String)} for details.
*
* <p>Ignite has only one possible CATALOG_NAME, it is handled on the client (driver) side.
*
* @param schemaNamePtrn Schema name java regex pattern.
* @param tblNamePtrn Table name java regex pattern.
* @param colNamePtrn Column name java regex pattern.
* @return Future of the list of metadatas about columns that match specified schema/tablename/columnname criterias.
*/
public CompletableFuture<Collection<JdbcColumnMeta>> getColumnsMeta(String schemaNamePtrn, String tblNamePtrn, String colNamePtrn) {
String schemaNameRegex = translateSqlWildcardsToRegex(schemaNamePtrn);
String tlbNameRegex = translateSqlWildcardsToRegex(tblNamePtrn);
String colNameRegex = translateSqlWildcardsToRegex(colNamePtrn);
return tables.tablesAsync().thenApply(tablesList -> {
return tablesList.stream().filter(t -> matches(getTblSchema(t.name()), schemaNameRegex)).filter(t -> matches(getTblName(t.name()), tlbNameRegex)).flatMap(tbl -> {
SchemaDescriptor schema = ((TableImpl) tbl).schemaView().schema();
List<Pair<String, Column>> tblColPairs = new ArrayList<>();
for (Column column : schema.keyColumns().columns()) {
tblColPairs.add(new Pair<>(tbl.name(), column));
}
for (Column column : schema.valueColumns().columns()) {
tblColPairs.add(new Pair<>(tbl.name(), column));
}
return tblColPairs.stream();
}).filter(e -> matches(e.getSecond().name(), colNameRegex)).sorted(bySchemaThenTabNameThenColOrder).map(pair -> createColumnMeta(pair.getFirst(), pair.getSecond())).collect(Collectors.toCollection(LinkedHashSet::new));
});
}
Aggregations