private MappingManager getMappingManager(Session session) { synchronized (mappingManagers) { MappingManager mappingManager = mappingManagers.get(session); if (mappingManager == null) { mappingManager = new MappingManager(session); mappingManagers.put(session, mappingManager); CodecRegistry codecRegistry = session.getCluster().getConfiguration().getCodecRegistry(); for (TypeCodec<?> codec : codecs) { codecRegistry.register(codec); } for (Class<?> udtClass : udtClasses) { mappingManager.udtCodec(udtClass); } } return mappingManager; } } }
private <T> T executeWithSession(SessionCallable<T> sessionCallable) { ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy(); ReconnectionSchedule schedule = reconnectionPolicy.newSchedule(); long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis(); while (true) { try { return sessionCallable.executeWithSession(session.get()); } catch (NoHostAvailableException e) { long timeLeft = deadline - System.currentTimeMillis(); if (timeLeft <= 0) { throw e; } else { long delay = Math.min(schedule.nextDelayMs(), timeLeft); log.warn(e.getCustomMessage(10, true, true)); log.warn("Reconnecting in %dms", delay); try { Thread.sleep(delay); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new RuntimeException("interrupted", interrupted); } } } } }
public void prepare() { LOG.info("Preparing state for {}", options.toString()); Preconditions.checkNotNull(options.getMapper, "CassandraBackingMap.Options should have getMapper"); Preconditions.checkNotNull(options.putMapper, "CassandraBackingMap.Options should have putMapper"); client = options.clientProvider.getClient(conf); session = client.connect(); if (options.maxParallelism == null || options.maxParallelism <= 0) { PoolingOptions po = session.getCluster().getConfiguration().getPoolingOptions(); Integer maxRequestsPerHost = Math.min( po.getMaxConnectionsPerHost(HostDistance.LOCAL) * po.getMaxRequestsPerConnection(HostDistance.LOCAL), po.getMaxConnectionsPerHost(HostDistance.REMOTE) * po.getMaxRequestsPerConnection(HostDistance.REMOTE) ); options.maxParallelism = maxRequestsPerHost / 2; LOG.info("Parallelism default set to {}", options.maxParallelism); } throttle = new Semaphore(options.maxParallelism, false); this.getResultMapper = new TridentAyncCQLResultSetValuesMapper(options.stateMapper.getStateFields(), throttle); this.putResultMapper = new TridentAyncCQLResultSetValuesMapper(null, throttle); }
@OnScheduled public void onScheduled(final ProcessContext context) { super.onScheduled(context); final int fetchSize = context.getProperty(FETCH_SIZE).evaluateAttributeExpressions().asInteger(); if (fetchSize > 0) { synchronized (cluster.get()) { cluster.get().getConfiguration().getQueryOptions().setFetchSize(fetchSize); } } }
@Inject public DataStaxClusterImpl(final CassandraConfig cassandraFig ) throws Exception { this.cassandraConfig = cassandraFig; this.cluster = getCluster(); logger.info("Initialized datastax cluster client. Hosts={}, Idle Timeout={}s, Pool Timeout={}s", getCluster().getMetadata().getAllHosts().toString(), getCluster().getConfiguration().getPoolingOptions().getIdleTimeoutSeconds(), getCluster().getConfiguration().getPoolingOptions().getPoolTimeoutMillis() / 1000); // always initialize the keyspaces this.createApplicationKeyspace(false); this.createApplicationLocalKeyspace(false); }
.getConfiguration() .getCodecRegistry() .register(instance);
MAX_CONNECTIONS_PROPERTY); if (maxConnections != null) { cluster.getConfiguration().getPoolingOptions() .setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(maxConnections)); CORE_CONNECTIONS_PROPERTY); if (coreConnections != null) { cluster.getConfiguration().getPoolingOptions() .setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(coreConnections)); CONNECT_TIMEOUT_MILLIS_PROPERTY); if (connectTimoutMillis != null) { cluster.getConfiguration().getSocketOptions() .setConnectTimeoutMillis(Integer.valueOf(connectTimoutMillis)); READ_TIMEOUT_MILLIS_PROPERTY); if (readTimoutMillis != null) { cluster.getConfiguration().getSocketOptions() .setReadTimeoutMillis(Integer.valueOf(readTimoutMillis));
Configuration configuration = cluster.getConfiguration(); CodecRegistry codecRegistry = configuration.getCodecRegistry();
newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel)); Metadata metadata = newCluster.getMetadata();
newSession = newCluster.connect(); newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel)); Metadata metadata = newCluster.getMetadata(); log.info("Connected to Cassandra cluster: {}", new Object[]{metadata.getClusterName()});
private ProtocolVersion protocolVersion() { // Since the QueryLogger can be registered before the Cluster was initialized, we can't retrieve // it at construction time. Cache it field at first use (a volatile field is good enough since // we // don't need mutual exclusion). if (protocolVersion == null) { protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); // At least one connection was established when QueryLogger is invoked assert protocolVersion != null : "protocol version should be defined"; } return protocolVersion; }
@Override public void init(Cluster cluster) { childPolicy.init(cluster); queryOptions = cluster.getConfiguration().getQueryOptions(); }
@BeforeMethod(groups = "unit") public void setUpQueryBuilder() throws Exception { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); }
@Test(groups = "short") public void should_set_flag_on_successful_agreement() { ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 10; ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); }
@Test(groups = "short", priority = 1) public void should_unset_flag_on_failed_agreement() { // Setting to 0 results in no query being set, so agreement fails ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 0; ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isFalse(); }
@Test(groups = "short") public void should_set_flag_on_non_schema_altering_statement() { ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 10; ResultSet rs = session().execute("select release_version from system.local"); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); }
public void checkGetValuesReturnsSerializedValue( ProtocolVersion protocolVersion, SimpleStatement statement, TestTable table) { CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); ByteBuffer[] values = statement.getValues(protocolVersion, codecRegistry); assertThat(values.length).isEqualTo(1); assertThat(values[0]) .as("Value not serialized as expected for " + table.sampleValue) .isEqualTo( codecRegistry .codecFor(table.testColumnType) .serialize(table.sampleValue, protocolVersion)); }
/** @jira_ticket JAVA-1209 */ @Test(groups = "short") public void getProtocolVersion_should_return_version() throws InterruptedException { ProtocolVersion version = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); assertThat(version).isNotNull(); } }
/** @jira_ticket JAVA-1209 */ @Test(groups = "unit") public void getProtocolVersion_should_return_null_if_not_connected() { Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); assertThat(cluster.getConfiguration().getProtocolOptions().getProtocolVersion()).isNull(); }
@BeforeMethod(groups = "short") public void setup() { primingClient.prime( queryBuilder().withQuery(query).withThen(then().withFixedDelay(100L)).build()); // Set default timeout too low cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(10); }