@Override public CqlResult apply(CassandraClient client) throws TException { return client.execute_cql3_query(cqlQuery, Compression.NONE, consistency); }
@Override public void batch_mutate(String kvsMethodName, Map<ByteBuffer, Map<String, List<Mutation>>> mutation_map, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { int numberOfRowsMutated = mutation_map.size(); try (CloseableTrace trace = startLocalTrace("client.batch_mutate(number of mutations {}, consistency {})" + " on kvs.{}", numberOfRowsMutated, consistency_level, kvsMethodName)) { client.batch_mutate(kvsMethodName, mutation_map, consistency_level); } }
@Override public CASResult cas(TableReference tableReference, ByteBuffer key, List<Column> expected, List<Column> updates, ConsistencyLevel serial_consistency_level, ConsistencyLevel commit_consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { try (CloseableTrace trace = startLocalTrace("client.cas(table {})", LoggingArgs.safeTableOrPlaceholder(tableReference))) { return client.cas(tableReference, key, expected, updates, serial_consistency_level, commit_consistency_level); } }
private static void createSimpleRfTestKeyspaceIfNotExists(CassandraClient client) throws TException { if (client.describe_keyspaces().stream() .map(KsDef::getName) .noneMatch(keyspace -> Objects.equals(keyspace, CassandraConstants.SIMPLE_RF_TEST_KEYSPACE))) { client.system_add_keyspace(SIMPLE_RF_TEST_KS_DEF); } }
client = CassandraClientFactory.getClientInternal(host, config); try { client.describe_keyspace(config.getKeyspaceOrThrow()); } catch (NotFoundException e) { return; // don't care to check for ring consistency when we're not even fully initialized tokenRangesToHost.put(ImmutableSet.copyOf(client.describe_ring(config.getKeyspaceOrThrow())), host); } catch (Exception e) { log.warn("Failed to get ring info from host: {}", } finally { if (client != null) { client.getOutputProtocol().getTransport().close();
private static void updateExistingKeyspace(CassandraClientPool clientPool, CassandraKeyValueServiceConfig config) throws TException { clientPool.runWithRetry((FunctionCheckedException<CassandraClient, Void, TException>) client -> { KsDef originalKsDef = client.describe_keyspace(config.getKeyspaceOrThrow()); // there was an existing keyspace // check and make sure it's definition is up to date with our config KsDef modifiedKsDef = originalKsDef.deepCopy(); checkAndSetReplicationFactor( client, modifiedKsDef, config); if (!modifiedKsDef.equals(originalKsDef)) { // Can't call system_update_keyspace to update replication factor if CfDefs are set modifiedKsDef.setCf_defs(ImmutableList.of()); client.system_update_keyspace(modifiedKsDef); CassandraKeyValueServices.waitForSchemaVersions(config, client, "after updating the existing keyspace"); } return null; }); }
private Set<String> getTableNames(CassandraClient client, String keyspace, Function<CfDef, String> nameGetter) throws TException { try { CassandraKeyValueServices .waitForSchemaVersions(config, client, "before making a call to get all table names."); } catch (IllegalStateException e) { throw new InsufficientConsistencyException("Could not reach a quorum of nodes agreeing on schema versions " + "before making a call to get all table names.", e); } KsDef ks = client.describe_keyspace(keyspace); return ks.getCf_defs().stream() .map(nameGetter) .collect(Collectors.toSet()); } }
@Test public void waitWaitsForSchemaVersions() throws TException { CassandraClient waitingClient = mock(CassandraClient.class); when(waitingClient.describe_schema_versions()).thenReturn( ImmutableMap.of(), ImmutableMap.of(VERSION_UNREACHABLE, QUORUM_OF_NODES, VERSION_1, REST_OF_NODES), ImmutableMap.of(VERSION_1, QUORUM_OF_NODES, VERSION_UNREACHABLE, REST_OF_NODES), ImmutableMap.of(VERSION_1, ALL_NODES)); CassandraKeyValueServices.waitForSchemaVersions(waitingConfig, waitingClient, TABLE); verify(waitingClient, times(3)).describe_schema_versions(); }
@Override public Map<ByteBuffer, List<ColumnOrSuperColumn>> multiget_slice(String kvsMethodName, TableReference tableRef, List<ByteBuffer> keys, SlicePredicate predicate, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { int numberOfKeys = keys.size(); int numberOfColumns = predicate.slice_range.count; try (CloseableTrace trace = startLocalTrace( "client.multiget_slice(table {}, number of keys {}, number of columns {}, consistency {}) on kvs.{}", LoggingArgs.safeTableOrPlaceholder(tableRef), numberOfKeys, numberOfColumns, consistency_level, kvsMethodName)) { return client.multiget_slice(kvsMethodName, tableRef, keys, predicate, consistency_level); } }
@Override public List<KeySlice> get_range_slices(String kvsMethodName, TableReference tableRef, SlicePredicate predicate, KeyRange range, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { return executeRead( () -> client.get_range_slices(kvsMethodName, tableRef, predicate, range, consistency_level), ThriftQueryWeighers.getRangeSlices(range)); }
public static FunctionCheckedException<CassandraClient, List<TokenRange>, Exception> getDescribeRing( CassandraKeyValueServiceConfig config) { return client -> client.describe_ring(config.getKeyspaceOrThrow()); } }
client = CassandraClientFactory.getClientInternal(host, config); try { client.describe_keyspace(config.getKeyspaceOrThrow()); } catch (NotFoundException e) { return; // don't care to check for ring consistency when we're not even fully initialized tokenRangesToHost.put(ImmutableSet.copyOf(client.describe_ring(config.getKeyspaceOrThrow())), host); } catch (Exception e) { log.warn("Failed to get ring info from host: {}", } finally { if (client != null) { client.getOutputProtocol().getTransport().close();
static void currentRfOnKeyspaceMatchesDesiredRf(CassandraClient client, CassandraKeyValueServiceConfig config) throws TException { KsDef ks = client.describe_keyspace(config.getKeyspaceOrThrow()); Set<String> dcs = sanityCheckDatacenters(client, config); sanityCheckReplicationFactor(ks, config, dcs); }
versions = client.describe_schema_versions(); if (uniqueSchemaWithQuorumAgreementAndOtherNodesUnreachable(config, versions)) { return;
private void changeReplicationFactor(int replicationFactor) throws TException { clientPool.run((FunctionCheckedException<CassandraClient, Void, TException>) client -> { KsDef originalKsDef = client.describe_keyspace(CASSANDRA.getConfig().getKeyspaceOrThrow()); KsDef modifiedKsDef = originalKsDef.deepCopy(); modifiedKsDef.setStrategy_class(CassandraConstants.NETWORK_STRATEGY); modifiedKsDef.setStrategy_options(ImmutableMap.of("dc1", Integer.toString(replicationFactor))); modifiedKsDef.setCf_defs(ImmutableList.of()); client.system_update_keyspace(modifiedKsDef); return null; }); }
@Override public Map<ByteBuffer, List<ColumnOrSuperColumn>> multiget_slice(String kvsMethodName, TableReference tableRef, List<ByteBuffer> keys, SlicePredicate predicate, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { return executeRead( () -> client.multiget_slice(kvsMethodName, tableRef, keys, predicate, consistency_level), ThriftQueryWeighers.multigetSlice(keys)); }
@Override public List<KeySlice> get_range_slices(String kvsMethodName, TableReference tableRef, SlicePredicate predicate, KeyRange range, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { int numberOfKeys = predicate.slice_range.count; int numberOfColumns = range.count; try (CloseableTrace trace = startLocalTrace( "client.get_range_slices(table {}, number of keys {}, number of columns {}, consistency {}) on kvs.{}", LoggingArgs.safeTableOrPlaceholder(tableRef), numberOfKeys, numberOfColumns, consistency_level, kvsMethodName)) { return client.get_range_slices(kvsMethodName, tableRef, predicate, range, consistency_level); } }
static Set<String> sanityCheckDatacenters(CassandraClient client, CassandraKeyValueServiceConfig config) throws TException { createSimpleRfTestKeyspaceIfNotExists(client); Multimap<String, String> datacenterToRack = HashMultimap.create(); Set<String> hosts = Sets.newHashSet(); for (TokenRange tokenRange : client.describe_ring(CassandraConstants.SIMPLE_RF_TEST_KEYSPACE)) { for (EndpointDetails details : tokenRange.getEndpoint_details()) { datacenterToRack.put(details.datacenter, details.rack); hosts.add(details.host); } } if (clusterHasExactlyOneDatacenter(datacenterToRack) && config.replicationFactor() > 1) { checkNodeTopologyIsSet(config, datacenterToRack); checkMoreRacksThanRfOrFewerHostsThanRf(config, hosts, datacenterToRack); } return datacenterToRack.keySet(); }
private static void createSimpleRfTestKeyspaceIfNotExists(CassandraClient client) throws TException { if (client.describe_keyspaces().stream() .map(KsDef::getName) .noneMatch(keyspace -> Objects.equals(keyspace, CassandraConstants.SIMPLE_RF_TEST_KEYSPACE))) { client.system_add_keyspace(SIMPLE_RF_TEST_KS_DEF); } }
@Override public CqlResult execute_cql3_query(CqlQuery cqlQuery, Compression compression, ConsistencyLevel consistency) throws InvalidRequestException, UnavailableException, TimedOutException, SchemaDisagreementException, TException { return executeRead( () -> client.execute_cql3_query(cqlQuery, compression, consistency), ThriftQueryWeighers.EXECUTE_CQL3_QUERY); }