Search in sources :

Example 1 with SchemaChangeListener

use of com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener in project java-driver by datastax.

the class SchemaChangesIT method should_handle_creation.

private <T> void should_handle_creation(String beforeStatement, String createStatement, Function<Metadata, Optional<T>> extract, Consumer<T> verifyMetadata, BiConsumer<SchemaChangeListener, T> verifyListener, CqlIdentifier... keyspaces) {
    if (beforeStatement != null) {
        ADMIN_SESSION_RULE.session().execute(beforeStatement);
    }
    SchemaChangeListener listener1 = mock(SchemaChangeListener.class);
    SchemaChangeListener listener2 = mock(SchemaChangeListener.class);
    // cluster1 executes the DDL query and gets a SCHEMA_CHANGE response.
    // cluster2 gets a SCHEMA_CHANGE push event on its control connection.
    List<String> keyspaceList = Lists.newArrayList();
    for (CqlIdentifier keyspace : keyspaces) {
        keyspaceList.add(keyspace.asInternal());
    }
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30)).withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, keyspaceList).build();
    try (CqlSession session1 = SessionUtils.newSession(CCM_RULE, ADMIN_SESSION_RULE.keyspace(), null, listener1, null, loader);
        CqlSession session2 = SessionUtils.newSession(CCM_RULE, null, null, listener2, null, loader)) {
        session1.execute(createStatement);
        // Refreshes on a response are synchronous:
        T newElement1 = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
        verifyMetadata.accept(newElement1);
        verifyListener.accept(listener1, newElement1);
        // Refreshes on a server event are asynchronous:
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            T newElement2 = extract.apply(session2.getMetadata()).orElseThrow(AssertionError::new);
            verifyMetadata.accept(newElement2);
            verifyListener.accept(listener2, newElement2);
        });
    }
}
Also used : SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 2 with SchemaChangeListener

use of com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener in project java-driver by datastax.

the class SchemaChangesIT method should_handle_update.

private <T> void should_handle_update(Iterable<String> beforeStatements, String updateStatement, Function<Metadata, Optional<T>> extract, Consumer<T> verifyNewMetadata, TriConsumer<SchemaChangeListener, T, T> verifyListener, CqlIdentifier... keyspaces) {
    for (String statement : beforeStatements) {
        ADMIN_SESSION_RULE.session().execute(statement);
    }
    SchemaChangeListener listener1 = mock(SchemaChangeListener.class);
    SchemaChangeListener listener2 = mock(SchemaChangeListener.class);
    List<String> keyspaceList = Lists.newArrayList();
    for (CqlIdentifier keyspace : keyspaces) {
        keyspaceList.add(keyspace.asInternal());
    }
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30)).withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, keyspaceList).build();
    try (CqlSession session1 = SessionUtils.newSession(CCM_RULE, ADMIN_SESSION_RULE.keyspace(), null, listener1, null, loader);
        CqlSession session2 = SessionUtils.newSession(CCM_RULE, null, null, listener2, null, loader)) {
        T oldElement = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
        assertThat(oldElement).isNotNull();
        session1.execute(updateStatement);
        T newElement = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
        verifyNewMetadata.accept(newElement);
        verifyListener.accept(listener1, oldElement, newElement);
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            verifyNewMetadata.accept(extract.apply(session2.getMetadata()).orElseThrow(AssertionError::new));
            verifyListener.accept(listener2, oldElement, newElement);
        });
    }
}
Also used : SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 3 with SchemaChangeListener

use of com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener in project java-driver by datastax.

the class SchemaChangesIT method should_handle_drop.

private <T> void should_handle_drop(Iterable<String> beforeStatements, String dropStatement, Function<Metadata, Optional<T>> extract, BiConsumer<SchemaChangeListener, T> verifyListener, CqlIdentifier... keyspaces) {
    for (String statement : beforeStatements) {
        ADMIN_SESSION_RULE.session().execute(statement);
    }
    SchemaChangeListener listener1 = mock(SchemaChangeListener.class);
    SchemaChangeListener listener2 = mock(SchemaChangeListener.class);
    List<String> keyspaceList = Lists.newArrayList();
    for (CqlIdentifier keyspace : keyspaces) {
        keyspaceList.add(keyspace.asInternal());
    }
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30)).withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, keyspaceList).build();
    try (CqlSession session1 = SessionUtils.newSession(CCM_RULE, ADMIN_SESSION_RULE.keyspace(), null, listener1, null, loader);
        CqlSession session2 = SessionUtils.newSession(CCM_RULE, null, null, listener2, null, loader)) {
        T oldElement = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
        assertThat(oldElement).isNotNull();
        session1.execute(dropStatement);
        assertThat(extract.apply(session1.getMetadata())).isEmpty();
        verifyListener.accept(listener1, oldElement);
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(extract.apply(session2.getMetadata())).isEmpty();
            verifyListener.accept(listener2, oldElement);
        });
    }
}
Also used : SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 4 with SchemaChangeListener

use of com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener in project java-driver by datastax.

the class DefaultDriverContext method buildSchemaChangeListener.

protected SchemaChangeListener buildSchemaChangeListener(SchemaChangeListener schemaChangeListenerFromBuilder) {
    List<SchemaChangeListener> listeners = new ArrayList<>();
    if (schemaChangeListenerFromBuilder != null) {
        listeners.add(schemaChangeListenerFromBuilder);
    }
    DefaultDriverOption newOption = DefaultDriverOption.METADATA_SCHEMA_CHANGE_LISTENER_CLASSES;
    @SuppressWarnings("deprecation") DefaultDriverOption legacyOption = DefaultDriverOption.METADATA_SCHEMA_CHANGE_LISTENER_CLASS;
    DriverExecutionProfile profile = config.getDefaultProfile();
    if (profile.isDefined(newOption)) {
        listeners.addAll(Reflection.buildFromConfigList(this, newOption, SchemaChangeListener.class, "com.datastax.oss.driver.internal.core.metadata.schema"));
    }
    if (profile.isDefined(legacyOption)) {
        LOG.warn("Option {} has been deprecated and will be removed in a future release; please use option {} instead.", legacyOption, newOption);
        Reflection.buildFromConfig(this, legacyOption, SchemaChangeListener.class, "com.datastax.oss.driver.internal.core.metadata.schema").ifPresent(listeners::add);
    }
    if (listeners.isEmpty()) {
        return new NoopSchemaChangeListener(this);
    } else if (listeners.size() == 1) {
        return listeners.get(0);
    } else {
        return new MultiplexingSchemaChangeListener(listeners);
    }
}
Also used : NoopSchemaChangeListener(com.datastax.oss.driver.internal.core.metadata.schema.NoopSchemaChangeListener) NoopSchemaChangeListener(com.datastax.oss.driver.internal.core.metadata.schema.NoopSchemaChangeListener) MultiplexingSchemaChangeListener(com.datastax.oss.driver.internal.core.metadata.schema.MultiplexingSchemaChangeListener) SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) MultiplexingSchemaChangeListener(com.datastax.oss.driver.internal.core.metadata.schema.MultiplexingSchemaChangeListener) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) ArrayList(java.util.ArrayList)

Example 5 with SchemaChangeListener

use of com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener in project java-driver by datastax.

the class SchemaChangesIT method should_handle_update_via_drop_and_recreate.

// Some element types don't have an ALTER command, but we can still observe an update if they get
// dropped and recreated while schema metadata is disabled
private <T> void should_handle_update_via_drop_and_recreate(Iterable<String> beforeStatements, String dropStatement, String recreateStatement, Function<Metadata, Optional<T>> extract, Consumer<T> verifyNewMetadata, TriConsumer<SchemaChangeListener, T, T> verifyListener, CqlIdentifier... keyspaces) {
    for (String statement : beforeStatements) {
        ADMIN_SESSION_RULE.session().execute(statement);
    }
    SchemaChangeListener listener1 = mock(SchemaChangeListener.class);
    SchemaChangeListener listener2 = mock(SchemaChangeListener.class);
    List<String> keyspaceList = Lists.newArrayList();
    for (CqlIdentifier keyspace : keyspaces) {
        keyspaceList.add(keyspace.asInternal());
    }
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30)).withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, keyspaceList).build();
    try (CqlSession session1 = SessionUtils.newSession(CCM_RULE, ADMIN_SESSION_RULE.keyspace(), null, listener1, null, loader);
        CqlSession session2 = SessionUtils.newSession(CCM_RULE, null, null, listener2, null, loader)) {
        T oldElement = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
        assertThat(oldElement).isNotNull();
        session1.setSchemaMetadataEnabled(false);
        session2.setSchemaMetadataEnabled(false);
        session1.execute(dropStatement);
        session1.execute(recreateStatement);
        session1.setSchemaMetadataEnabled(true);
        session2.setSchemaMetadataEnabled(true);
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            T newElement = extract.apply(session1.getMetadata()).orElseThrow(AssertionError::new);
            verifyNewMetadata.accept(newElement);
            verifyListener.accept(listener1, oldElement, newElement);
        });
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
            T newElement = extract.apply(session2.getMetadata()).orElseThrow(AssertionError::new);
            verifyNewMetadata.accept(newElement);
            verifyListener.accept(listener2, oldElement, newElement);
        });
    }
}
Also used : SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Aggregations

SchemaChangeListener (com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)5 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)5 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 DseDriverOption (com.datastax.dse.driver.api.core.config.DseDriverOption)1 AUTH_PROVIDER_SASL_PROPERTIES (com.datastax.dse.driver.api.core.config.DseDriverOption.AUTH_PROVIDER_SASL_PROPERTIES)1 AUTH_PROVIDER_SERVICE (com.datastax.dse.driver.api.core.config.DseDriverOption.AUTH_PROVIDER_SERVICE)1 DseGssApiAuthProvider (com.datastax.dse.driver.internal.core.auth.DseGssApiAuthProvider)1 AuthenticatorConfig (com.datastax.oss.common.sink.config.AuthenticatorConfig)1 ContactPointsValidator (com.datastax.oss.common.sink.config.ContactPointsValidator)1 SslConfig (com.datastax.oss.common.sink.config.SslConfig)1 SessionBuilder (com.datastax.oss.common.sink.ssl.SessionBuilder)1 UUIDUtil.generateClientId (com.datastax.oss.common.sink.util.UUIDUtil.generateClientId)1 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 CqlSessionBuilder (com.datastax.oss.driver.api.core.CqlSessionBuilder)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 OptionsMap (com.datastax.oss.driver.api.core.config.OptionsMap)1 ProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)1 TypedDriverOption (com.datastax.oss.driver.api.core.config.TypedDriverOption)1