Search in sources :

Example 1 with EventListener

use of org.apache.ignite.internal.manager.EventListener in project ignite-3 by apache.

the class TableManager method tableSchema.

/**
 * Return table schema of certain version from history.
 *
 * @param tblId     Table id.
 * @param schemaVer Schema version.
 * @return Schema descriptor.
 */
private SchemaDescriptor tableSchema(UUID tblId, int schemaVer) {
    Map<UUID, TableImpl> tablesById = tablesByIdVv.latest();
    TableImpl table = tablesById == null ? null : tablesById.get(tblId);
    assert table != null : "Table is undefined [tblId=" + tblId + ']';
    ExtendedTableConfiguration tblCfg = ((ExtendedTableConfiguration) tablesCfg.tables().get(table.name()));
    if (schemaVer <= table.schemaView().lastSchemaVersion()) {
        return getSchemaDescriptorLocally(schemaVer, tblCfg);
    }
    CompletableFuture<SchemaDescriptor> fut = new CompletableFuture<>();
    var clo = new EventListener<TableEventParameters>() {

        @Override
        public boolean notify(@NotNull TableEventParameters parameters, @Nullable Throwable exception) {
            if (tblId.equals(parameters.tableId()) && schemaVer <= parameters.table().schemaView().lastSchemaVersion()) {
                fut.complete(getSchemaDescriptorLocally(schemaVer, tblCfg));
                return true;
            }
            return false;
        }

        @Override
        public void remove(@NotNull Throwable exception) {
            fut.completeExceptionally(exception);
        }
    };
    listen(TableEvent.ALTER, clo);
    if (schemaVer <= table.schemaView().lastSchemaVersion()) {
        fut.complete(getSchemaDescriptorLocally(schemaVer, tblCfg));
    }
    if (!isSchemaExists(tblId, schemaVer) && fut.complete(null)) {
        removeListener(TableEvent.ALTER, clo);
    }
    return fut.join();
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) NotNull(org.jetbrains.annotations.NotNull) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) CompletableFuture(java.util.concurrent.CompletableFuture) EventListener(org.apache.ignite.internal.manager.EventListener) UUID(java.util.UUID) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with EventListener

use of org.apache.ignite.internal.manager.EventListener in project ignite-3 by apache.

the class StopCalciteModuleTest method before.

/**
 * Before.
 * TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
 */
@BeforeEach
public void before(TestInfo testInfo) {
    when(clusterSrvc.messagingService()).thenReturn(msgSrvc);
    when(clusterSrvc.topologyService()).thenReturn(topologySrvc);
    when(clusterSrvc.localConfiguration()).thenReturn(localCfg);
    ClusterNode node = new ClusterNode("mock-node-id", NODE_NAME, null);
    when(topologySrvc.localMember()).thenReturn(node);
    when(topologySrvc.allMembers()).thenReturn(Collections.singleton(node));
    SchemaDescriptor schemaDesc = new SchemaDescriptor(1, new Column[] { new Column(0, "ID", NativeTypes.INT32, false) }, new Column[] { new Column(1, "VAL", NativeTypes.INT32, false) });
    schemaReg = new SchemaRegistryImpl(1, (v) -> schemaDesc, () -> INITIAL_SCHEMA_VERSION);
    when(tbl.name()).thenReturn("PUBLIC.TEST");
    // Mock create table (notify on register listener).
    doAnswer(invocation -> {
        EventListener<TableEventParameters> clo = (EventListener<TableEventParameters>) invocation.getArguments()[1];
        clo.notify(new TableEventParameters(0, UUID.randomUUID(), "TEST", new TableImpl(tbl, schemaReg)), null);
        return null;
    }).when(tableManager).listen(eq(TableEvent.CREATE), any());
    RowAssembler asm = new RowAssembler(schemaReg.schema(), 0, 0, 0, 0);
    asm.appendInt(0);
    asm.appendInt(0);
    BinaryRow binaryRow = asm.build();
    // Mock table scan
    doAnswer(invocation -> {
        int part = (int) invocation.getArguments()[0];
        return (Flow.Publisher<BinaryRow>) s -> {
            s.onSubscribe(new Flow.Subscription() {

                @Override
                public void request(long n) {
                }

                @Override
                public void cancel() {
                }
            });
            if (part == 0) {
                for (int i = 0; i < ROWS; ++i) {
                    s.onNext(binaryRow);
                }
            }
            s.onComplete();
        };
    }).when(tbl).scan(anyInt(), any());
    LOG.info(">>>> Starting test {}", testInfo.getTestMethod().orElseThrow().getName());
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) InternalTable(org.apache.ignite.internal.table.InternalTable) IgniteException(org.apache.ignite.lang.IgniteException) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ThreadInfo(java.lang.management.ThreadInfo) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) TableImpl(org.apache.ignite.internal.table.TableImpl) Flow(java.util.concurrent.Flow) INITIAL_SCHEMA_VERSION(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl.INITIAL_SCHEMA_VERSION) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ManagementFactory(java.lang.management.ManagementFactory) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MessagingService(org.apache.ignite.network.MessagingService) TableManager(org.apache.ignite.internal.table.distributed.TableManager) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) Mockito.when(org.mockito.Mockito.when) ThreadMXBean(java.lang.management.ThreadMXBean) UUID(java.util.UUID) TestInfo(org.junit.jupiter.api.TestInfo) ClusterNode(org.apache.ignite.network.ClusterNode) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) NativeTypes(org.apache.ignite.internal.schema.NativeTypes) Column(org.apache.ignite.internal.schema.Column) TableEvent(org.apache.ignite.internal.table.event.TableEvent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ClusterService(org.apache.ignite.network.ClusterService) EventListener(org.apache.ignite.internal.manager.EventListener) Collections(java.util.Collections) SchemaRegistry(org.apache.ignite.internal.schema.SchemaRegistry) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableImpl(org.apache.ignite.internal.table.TableImpl) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) Flow(java.util.concurrent.Flow) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) Column(org.apache.ignite.internal.schema.Column) EventListener(org.apache.ignite.internal.manager.EventListener) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

UUID (java.util.UUID)2 EventListener (org.apache.ignite.internal.manager.EventListener)2 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)2 TableImpl (org.apache.ignite.internal.table.TableImpl)2 TableEventParameters (org.apache.ignite.internal.table.event.TableEventParameters)2 ManagementFactory (java.lang.management.ManagementFactory)1 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Flow (java.util.concurrent.Flow)1 Consumer (java.util.function.Consumer)1 ExtendedTableConfiguration (org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration)1 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)1 Column (org.apache.ignite.internal.schema.Column)1 NativeTypes (org.apache.ignite.internal.schema.NativeTypes)1 SchemaRegistry (org.apache.ignite.internal.schema.SchemaRegistry)1 SchemaRegistryImpl (org.apache.ignite.internal.schema.registry.SchemaRegistryImpl)1