public static FunctionCheckedException<CassandraClient, List<TokenRange>, Exception> getDescribeRing( CassandraKeyValueServiceConfig config) { return client -> client.describe_ring(config.getKeyspaceOrThrow()); } }
private UUID getUuidForTable(TableReference tableRef) { String internalTableName = CassandraKeyValueServiceImpl.internalTableName(tableRef); String keyspace = config.getKeyspaceOrThrow(); String fullTableNameForUuid = keyspace + "." + internalTableName; return UUID.nameUUIDFromBytes(fullTableNameForUuid.getBytes()); } }
@VisibleForTesting CfDef getCfForTable(TableReference tableRef, byte[] rawMetadata, int gcGraceSeconds) { return ColumnFamilyDefinitions .getCfDef(config.getKeyspaceOrThrow(), tableRef, gcGraceSeconds, rawMetadata); }
Set<String> getExistingLowerCased() throws TException { return getExistingLowerCased(config.getKeyspaceOrThrow()); }
@Override public String toString() { return MoreObjects.toStringHelper(getClass()) .add("host", this.host) .add("keyspace", config.getKeyspaceOrThrow()) .add("usingSsl", config.usingSsl()) .add("sslConfiguration", config.sslConfiguration().isPresent() ? config.sslConfiguration().get() : "unspecified") .add("socketTimeoutMillis", config.socketTimeoutMillis()) .add("socketQueryTimeoutMillis", config.socketQueryTimeoutMillis()) .toString(); }
Set<String> getExisting() { String keyspace = config.getKeyspaceOrThrow(); try { return clientPool.runWithRetry(new FunctionCheckedException<CassandraClient, Set<String>, Exception>() { @Override public Set<String> apply(CassandraClient client) throws Exception { return getExisting(client, keyspace); } @Override public String toString() { return "describe_keyspace(" + keyspace + ")"; } }); } catch (Exception e) { throw Throwables.throwUncheckedException(e); } }
@Test public void resolvesConfigWithOriginalKeyspaceIfNoNamespaceProvided() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITH_KEYSPACE, Optional::empty, Optional.empty()); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(CONFIG_WITH_KEYSPACE.getKeyspaceOrThrow()); }
@Override public CassandraClient create() throws Exception { try { return instrumentClient(getRawClientWithKeyspace(addr, config)); } catch (Exception e) { String message = String.format("Failed to construct client for %s/%s", addr, config.getKeyspaceOrThrow()); if (config.usingSsl()) { message += " over SSL"; } throw new ClientCreationFailedException(message, e); } }
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); }
private static boolean keyspaceAlreadyExists(InetSocketAddress host, CassandraKeyValueServiceConfig config) throws TException { try { CassandraClient client = CassandraClientFactory.getClientInternal(host, config); client.describe_keyspace(config.getKeyspaceOrThrow()); CassandraKeyValueServices.waitForSchemaVersions(config, client, "while checking if schemas diverged on startup"); return true; } catch (NotFoundException e) { return false; } }
static KsDef createKsDefForFresh(CassandraClient client, CassandraKeyValueServiceConfig config) throws TException { KsDef ksDef = new KsDef(config.getKeyspaceOrThrow(), CassandraConstants.NETWORK_STRATEGY, ImmutableList.of()); Set<String> dcs = sanityCheckDatacenters(client, config); ksDef.setStrategy_options(Maps.asMap(dcs, ignore -> String.valueOf(config.replicationFactor()))); ksDef.setDurable_writes(true); return ksDef; }
@Test public void resolvesConfigIfKeyspaceAndNamespaceProvidedAndMatch() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITH_KEYSPACE, Optional::empty, Optional.of(KEYSPACE)); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE); }
@Test public void resolvesConfigWithNamespaceIfNoKeyspaceProvided() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITHOUT_KEYSPACE, Optional::empty, Optional.of(KEYSPACE)); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE); }
private void assertReplicationFactorMismatchError(Exception ex) { assertThat(ex.getMessage(), is("Your current Cassandra keyspace (" + CASSANDRA.getConfig().getKeyspaceOrThrow() + ") has a replication factor not matching your Atlas Cassandra configuration. Change them to match, " + "but be mindful of what steps you'll need to take to correctly repair or cleanup existing data in " + "your cluster.")); }
@Test public void canReplaceKeyspace() { CassandraKeyValueServiceConfig newConfig = CassandraKeyValueServiceConfigs.copyWithKeyspace( CONFIG_WITH_KEYSPACE, KEYSPACE_2); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE_2); }
@Test public void canAddKeyspace() { CassandraKeyValueServiceConfig newConfig = CassandraKeyValueServiceConfigs.copyWithKeyspace( CONFIG_WITHOUT_KEYSPACE, KEYSPACE); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE); }
private static boolean attemptToCreateKeyspaceOnHost(InetSocketAddress host, CassandraKeyValueServiceConfig config) throws TException { try { CassandraClient client = CassandraClientFactory.getClientInternal(host, config); KsDef ksDef = createKsDefForFresh(client, config); client.system_add_keyspace(ksDef); log.info("Created keyspace: {}", SafeArg.of("keyspace", config.getKeyspaceOrThrow())); CassandraKeyValueServices.waitForSchemaVersions(config, client, "after adding the initial empty keyspace"); return true; } catch (InvalidRequestException e) { return keyspaceAlreadyExists(host, config); } }
private static Cassandra.Client getRawClientWithKeyspace(InetSocketAddress addr, CassandraKeyValueServiceConfig config) throws Exception { Client ret = getRawClient(addr, config); try { ret.set_keyspace(config.getKeyspaceOrThrow()); log.debug("Created new client for {}/{}{}{}", SafeArg.of("address", CassandraLogHelper.host(addr)), UnsafeArg.of("keyspace", config.getKeyspaceOrThrow()), SafeArg.of("usingSsl", config.usingSsl() ? " over SSL" : ""), UnsafeArg.of("usernameConfig", config.credentials().isPresent() ? " as user " + config.credentials().get().username() : "")); return ret; } catch (Exception e) { ret.getOutputProtocol().getTransport().close(); throw e; } }
private void assertThatGcGraceSecondsIs(CassandraKeyValueService kvs, int gcGraceSeconds) throws TException { List<CfDef> knownCfs = kvs.getClientPool().runWithRetry(client -> client.describe_keyspace(CASSANDRA.getConfig().getKeyspaceOrThrow()).getCf_defs()); CfDef clusterSideCf = Iterables.getOnlyElement(knownCfs.stream() .filter(cf -> cf.getName().equals(getInternalTestTableName())) .collect(Collectors.toList())); assertThat(clusterSideCf.gc_grace_seconds, equalTo(gcGraceSeconds)); }
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; }); }