use of org.apache.ignite.table.Table in project ignite-3 by apache.
the class ItOrToUnionRuleTest method initTestData.
/**
* Before all.
*/
@BeforeAll
static void initTestData() {
TableDefinition schTbl1 = SchemaBuilders.tableBuilder("PUBLIC", "PRODUCTS").columns(SchemaBuilders.column("ID", ColumnType.INT32).build(), SchemaBuilders.column("CATEGORY", ColumnType.string()).asNullable(true).build(), SchemaBuilders.column("CAT_ID", ColumnType.INT32).build(), SchemaBuilders.column("SUBCATEGORY", ColumnType.string()).asNullable(true).build(), SchemaBuilders.column("SUBCAT_ID", ColumnType.INT32).build(), SchemaBuilders.column("NAME", ColumnType.string()).asNullable(true).build()).withPrimaryKey("ID").withIndex(SchemaBuilders.sortedIndex(IDX_CATEGORY).addIndexColumn("CATEGORY").done().build()).withIndex(SchemaBuilders.sortedIndex(IDX_CAT_ID).addIndexColumn("CAT_ID").done().build()).withIndex(SchemaBuilders.sortedIndex(IDX_SUBCATEGORY).addIndexColumn("SUBCATEGORY").done().build()).withIndex(SchemaBuilders.sortedIndex(IDX_SUBCAT_ID).addIndexColumn("SUBCAT_ID").done().build()).build();
Table tbl = CLUSTER_NODES.get(0).tables().createTable(schTbl1.canonicalName(), tblCh -> SchemaConfigurationConverter.convert(schTbl1, tblCh).changeReplicas(1).changePartitions(10));
insertData(tbl, new String[] { "ID", "CATEGORY", "CAT_ID", "SUBCATEGORY", "SUBCAT_ID", "NAME" }, new Object[][] { { 1, "Photo", 1, "Camera Media", 11, "Media 1" }, { 2, "Photo", 1, "Camera Media", 11, "Media 2" }, { 3, "Photo", 1, "Camera Lens", 12, "Lens 1" }, { 4, "Photo", 1, "Other", 12, "Charger 1" }, { 5, "Video", 2, "Camera Media", 21, "Media 3" }, { 6, "Video", 2, "Camera Lens", 22, "Lens 3" }, { 7, "Video", 1, null, 0, "Canon" }, { 8, null, 0, "Camera Lens", 11, "Zeiss" }, { 9, null, 0, null, 0, null }, { 10, null, 0, null, 30, null }, { 11, null, 0, null, 30, null }, { 12, null, 0, null, 31, null }, { 13, null, 0, null, 31, null }, { 14, null, 0, null, 32, null }, { 15, null, 0, null, 33, null }, { 16, null, 0, null, 34, null }, { 17, null, 0, null, 35, null }, { 18, null, 0, null, 36, null }, { 19, null, 0, null, 37, null }, { 20, null, 0, null, 38, null }, { 21, null, 0, null, 39, null }, { 22, null, 0, null, 40, null }, { 23, null, 0, null, 41, null } });
}
use of org.apache.ignite.table.Table in project ignite-3 by apache.
the class ItProjectScanMergeRuleTest method initTestData.
/**
* Before all.
*/
@BeforeAll
static void initTestData() {
TableDefinition schTbl1 = SchemaBuilders.tableBuilder("PUBLIC", "PRODUCTS").columns(SchemaBuilders.column("ID", ColumnType.INT32).build(), SchemaBuilders.column("CATEGORY", ColumnType.string()).asNullable(true).build(), SchemaBuilders.column("CAT_ID", ColumnType.INT32).build(), SchemaBuilders.column("SUBCATEGORY", ColumnType.string()).asNullable(true).build(), SchemaBuilders.column("SUBCAT_ID", ColumnType.INT32).build(), SchemaBuilders.column("NAME", ColumnType.string()).asNullable(true).build()).withPrimaryKey("ID").withIndex(SchemaBuilders.sortedIndex(IDX_CAT_ID).addIndexColumn("CAT_ID").done().build()).build();
Table tbl = CLUSTER_NODES.get(0).tables().createTable(schTbl1.canonicalName(), tblCh -> SchemaConfigurationConverter.convert(schTbl1, tblCh).changeReplicas(1).changePartitions(10));
insertData(tbl, new String[] { "ID", "CATEGORY", "CAT_ID", "SUBCATEGORY", "SUBCAT_ID", "NAME" }, new Object[][] { { 1, "prod1", 1, "cat1", 11, "noname1" }, { 2, "prod2", 2, "cat1", 11, "noname2" }, { 3, "prod3", 3, "cat1", 12, "noname3" }, { 4, "prod4", 4, "cat1", 13, "noname4" } });
}
use of org.apache.ignite.table.Table in project ignite-3 by apache.
the class SchemaValidationTest method schemaMismatch.
@Test
public void schemaMismatch() {
SchemaDescriptor schema = new SchemaDescriptor(1, new Column[] { new Column("id", NativeTypes.INT64, false), new Column("affId", NativeTypes.INT64, false) }, new Column[] { new Column("val", NativeTypes.INT64, true) });
Table tbl = createTableImpl(schema);
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.recordView().get(null, Tuple.create().set("id", 0L).set("affId", 1L).set("val", 0L)));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.recordView().get(null, Tuple.create().set("id", 0L)));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.keyValueView().get(null, Tuple.create().set("id", 0L)));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.keyValueView().get(null, Tuple.create().set("id", 0L).set("affId", 1L).set("val", 0L)));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.keyValueView().put(null, Tuple.create().set("id", 0L), Tuple.create()));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.keyValueView().put(null, Tuple.create().set("id", 0L).set("affId", 1L).set("val", 0L), Tuple.create()));
assertThrowsWithCause(SchemaMismatchException.class, () -> tbl.keyValueView().put(null, Tuple.create().set("id", 0L).set("affId", 1L), Tuple.create().set("id", 0L).set("val", 0L)));
}
use of org.apache.ignite.table.Table 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.table.Table in project ignite-3 by apache.
the class TableManagerTest method mockManagersAndCreateTableWithDelay.
/**
* Instantiates a table and prepares Table manager. When the latch would open, the method completes.
*
* @param tableDefinition Configuration schema for a table.
* @param tblManagerFut Future for table manager.
* @param phaser Phaser for the wait.
* @return Table manager.
* @throws NodeStoppingException If something went wrong.
*/
@NotNull
private TableImpl mockManagersAndCreateTableWithDelay(TableDefinition tableDefinition, CompletableFuture<TableManager> tblManagerFut, Phaser phaser) throws NodeStoppingException {
when(rm.prepareRaftGroup(any(), any(), any())).thenAnswer(mock -> {
RaftGroupService raftGrpSrvcMock = mock(RaftGroupService.class);
when(raftGrpSrvcMock.leader()).thenReturn(new Peer(new NetworkAddress("localhost", 47500)));
return CompletableFuture.completedFuture(raftGrpSrvcMock);
});
when(ts.getByAddress(any(NetworkAddress.class))).thenReturn(new ClusterNode(UUID.randomUUID().toString(), "node0", new NetworkAddress("localhost", 47500)));
try (MockedStatic<SchemaUtils> schemaServiceMock = mockStatic(SchemaUtils.class)) {
schemaServiceMock.when(() -> SchemaUtils.prepareSchemaDescriptor(anyInt(), any())).thenReturn(mock(SchemaDescriptor.class));
}
try (MockedStatic<AffinityUtils> affinityServiceMock = mockStatic(AffinityUtils.class)) {
ArrayList<List<ClusterNode>> assignment = new ArrayList<>(PARTITIONS);
for (int part = 0; part < PARTITIONS; part++) {
assignment.add(new ArrayList<>(Collections.singleton(node)));
}
affinityServiceMock.when(() -> AffinityUtils.calculateAssignments(any(), anyInt(), anyInt())).thenReturn(assignment);
}
TableManager tableManager = createTableManager(tblManagerFut);
final int tablesBeforeCreation = tableManager.tables().size();
tblsCfg.tables().listen(ctx -> {
boolean createTbl = ctx.newValue().get(tableDefinition.canonicalName()) != null && ctx.oldValue().get(tableDefinition.canonicalName()) == null;
boolean dropTbl = ctx.oldValue().get(tableDefinition.canonicalName()) != null && ctx.newValue().get(tableDefinition.canonicalName()) == null;
if (!createTbl && !dropTbl) {
return CompletableFuture.completedFuture(null);
}
if (phaser != null) {
phaser.arriveAndAwaitAdvance();
}
return CompletableFuture.completedFuture(null);
});
TableImpl tbl2 = (TableImpl) tableManager.createTable(tableDefinition.canonicalName(), tblCh -> SchemaConfigurationConverter.convert(tableDefinition, tblCh).changeReplicas(REPLICAS).changePartitions(PARTITIONS));
assertNotNull(tbl2);
assertEquals(tablesBeforeCreation + 1, tableManager.tables().size());
return tbl2;
}
Aggregations