/** * Add field use_raw_configuration_schema to endpointProfile that used to support devices using * SDK version 0.9.0 */ public void transform() { //mongo MongoClient client = new MongoClient(host); MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> endpointProfile = database.getCollection("endpoint_profile"); endpointProfile.updateMany(new Document(), eq("$set", eq("use_raw_schema", false))); //cassandra Cluster cluster = Cluster.builder().addContactPoint(host).build(); Session session = cluster.connect(dbName); session.execute("ALTER TABLE ep_profile ADD use_raw_schema boolean"); session.close(); cluster.close(); } }
/** * Returns Cassandra session and its generation number. * * @return Wrapper object providing Cassandra session and its generation number. */ private synchronized WrappedSession session() { if (wrapperSes != null) return wrapperSes; Session ses = SessionPool.get(this); if (ses != null) { this.wrapperSes = new WrappedSession(ses, generation); return this.wrapperSes; } synchronized (sesStatements) { sesStatements.clear(); } try { ses = builder.build().connect(); generation++; this.wrapperSes = new WrappedSession(ses, generation); } catch (Throwable e) { throw new IgniteException("Failed to establish session with Cassandra database", e); } return this.wrapperSes; }
@Test(groups = "short") public void testMissingRpcAddressAtStartup() throws Exception { deleteNode2RpcAddressFromNode1(); // Use only one contact point to make sure that the control connection is on node1 Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints().get(0)) .withPort(ccm().getBinaryPort()) .build()); cluster.connect(); // Since node2's RPC address is unknown on our control host, it should have been ignored assertEquals(cluster.getMetrics().getConnectedToHosts().getValue().intValue(), 1); assertNull(cluster.getMetadata().getHost(getContactPointsWithPorts().get(1))); }
/** * Tests the NoHostAvailableException. by attempting to build a cluster using the IP address * "255.255.255.255" and test all available exception methods. */ @Test(groups = "short") public void noHostAvailableException() throws Exception { try { Cluster.builder().addContactPoints("255.255.255.255").build(); } catch (NoHostAvailableException e) { assertEquals(e.getErrors().size(), 1); assertTrue( e.getErrors() .values() .iterator() .next() .toString() .contains("[/255.255.255.255] Cannot connect")); NoHostAvailableException copy = (NoHostAvailableException) e.copy(); assertEquals(copy.getMessage(), e.getMessage()); assertEquals(copy.getErrors(), e.getErrors()); } }
@Test(groups = "short") public void should_countdown_inflight_requests_metrics() { sCluster .node(1) .primingClient() .prime(PrimingRequest.queryBuilder().withQuery("mock query").withThen(then()).build()); Cluster cluster = null; try { cluster = builder().build(); Session session = cluster.connect(); assertThat(cluster.getMetrics().getInFlightRequests().getValue()).isEqualTo(0); session.executeAsync("mock query").getUninterruptibly(); session.executeAsync("mock query").getUninterruptibly(); assertThat(cluster.getMetrics().getInFlightRequests().getValue()).isEqualTo(0); } finally { if (cluster != null) { cluster.close(); } } } }
protected void initTestCluster(Object testInstance) throws Exception { if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster()) { Cluster.Builder builder = ccmTestConfig.clusterProvider(testInstance); // add contact points only if the provided builder didn't do so if (builder.getContactPoints().isEmpty()) builder.addContactPoints(getContactPoints()); builder.withPort(ccm.getBinaryPort()); cluster = register(builder.build()); cluster.init(); } }
@Test(groups = "short") public void should_handle_tuples_with_custom_codecs() { CodecRegistry codecRegistry = new CodecRegistry(); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpTupleTypes(cluster); codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType)));
/** * Validates that when a Cluster is initialized that {@link * SpeculativeExecutionPolicy#init(Cluster)} is called and that when a Cluster is closed {@link * SpeculativeExecutionPolicy#close()} is called. * * @test_category queries:speculative_execution * @expected_result init and close are called on cluster init and close. * @jira_ticket JAVA-796 * @since 2.0.11, 2.1.7, 2.2.1 */ @Test(groups = "short") public void should_init_and_close_policy_on_cluster() { SpeculativeExecutionPolicy mockPolicy = mock(SpeculativeExecutionPolicy.class); Cluster cluster = Cluster.builder() .addContactPoints(scassandras.address(2).getAddress()) .withPort(scassandras.getBinaryPort()) .withSpeculativeExecutionPolicy(mockPolicy) .build(); verify(mockPolicy, times(0)).init(cluster); verify(mockPolicy, times(0)).close(); try { cluster.init(); verify(mockPolicy, times(1)).init(cluster); } finally { cluster.close(); verify(mockPolicy, times(1)).close(); } }
@Test(groups = "short") public void should_count_inflight_requests_metrics() { sCluster .node(1) .primingClient() .prime( PrimingRequest.queryBuilder() .withQuery("mock query") .withThen(then().withFixedDelay(100000L)) .build()); Cluster cluster = null; try { cluster = builder().build(); Session session = cluster.connect(); assertThat(cluster.getMetrics().getInFlightRequests().getValue()).isEqualTo(0); session.executeAsync("mock query"); session.executeAsync("mock query"); assertThat(cluster.getMetrics().getInFlightRequests().getValue()).isEqualTo(2); } finally { if (cluster != null) { cluster.close(); } } }
cluster = Cluster.builder().withCredentials(username, password) .withPort(Integer.valueOf(port)).addContactPoints(hosts).build(); } else { cluster = Cluster.builder().withPort(Integer.valueOf(port)) .addContactPoints(hosts).build(); session = cluster.connect(keyspace);
@Override public Cluster get() { String host = configuration.getHostAddress(); int port = configuration.getPort(); LOGGER.info("Connecting to cassandra cluster with host:{}, port:{}", host, port); Cluster cluster = Cluster.builder() .addContactPoint(host) .withPort(port) .build(); Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: {}", metadata.getClusterName()); metadata.getAllHosts().forEach(h -> { LOGGER.info("Datacenter:{}, host:{}, rack: {}", h.getDatacenter(), h.getAddress(), h.getRack()); }); return cluster; } }
.addContactPoints(cstr.contactPoints()) .withPort(cstr.port()); Cluster cluster = builder.build(); Session session = cluster.connect(cstr.keyspace()); hierarchy(session.getClass(), type -> bind.apply(type, cstr.keyspace(), session));
@Test(groups = "short") public void should_handle_partial_tuples_with_custom_codecs() { CodecRegistry codecRegistry = new CodecRegistry(); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpTupleTypes(cluster); codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType)));
/** * Verifies that the cluster builder fails when beta flag is set and user attempts to pass a * version explicitly. * * @jira_ticket JAVA-1248 */ @Test(groups = "short") public void should_not_initialize_when_beta_flag_is_set_and_version_explicitly_required() throws Exception { try { Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .allowBetaProtocolVersion() .withProtocolVersion(V4) .build(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertThat(e.getMessage()) .isEqualTo("Can not set the version explicitly if `allowBetaProtocolVersion` was used."); } }
@BeforeMethod(groups = "short") public void setUp() { sCluster = ScassandraCluster.builder().withNodes(4).build(); sCluster.init(); cluster = Cluster.builder() .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) .withLoadBalancingPolicy(lbSpy) .withNettyOptions(nonQuietClusterCloseOptions) .build(); session = cluster.connect(); // Reset invocations before entering test. Mockito.reset(lbSpy); }
/** * Create a new instance of UpdateUuidsMigration. * * @param connection the connection to relational database * @param options the options for configuring NoSQL databases */ public UpdateUuidsMigration(Connection connection, Options options) { this.connection = connection; client = new MongoClient(options.getHost()); cluster = Cluster.builder() .addContactPoint(options.getHost()) .build(); dbName = options.getDbName(); this.nosql = options.getNoSql(); }
private void startUp() { int port = 9042; String[] seeds; if (configuration.containsKey(CONTACT_POINTS)) { seeds = configuration.get(CONTACT_POINTS).split(","); } else { seeds = new String[] {LOCALHOST}; } Cluster cluster = new Cluster.Builder() .addContactPoints(seeds) .withPort(port) .build(); String keySpace = configuration.get(KEY_SPACE); if (keySpace == null || keySpace.isEmpty()) { keySpace=DEFAULT_KEYSPACE; } session = Optional.of(cluster.connect(keySpace)); dataAccess = new DataAccess(session.get()); }