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);
});
}
}
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);
});
}
}
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);
});
}
}
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);
}
}
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);
});
}
}
Aggregations