private static Set<String> getDcForSimpleStrategy(CassandraClient client, KsDef ksDef, CassandraKeyValueServiceConfig config) throws TException { checkKsDefRfEqualsOne(ksDef, config); Set<String> datacenters = sanityCheckDatacenters(client, config); checkOneDatacenter(config, datacenters); return datacenters; }
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); }
static KsDef checkAndSetReplicationFactor(CassandraClient client, KsDef ksDef, CassandraKeyValueServiceConfig config) throws TException { KsDef result = ksDef; Set<String> datacenters; if (Objects.equals(result.getStrategy_class(), CassandraConstants.SIMPLE_STRATEGY)) { datacenters = getDcForSimpleStrategy(client, result, config); result = setNetworkStrategyIfCheckedTopology(result, config, datacenters); } else { datacenters = sanityCheckDatacenters(client, config); } sanityCheckReplicationFactor(result, config, datacenters); return result; }
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; }
private void lowerConsistencyWhenSafe() { Set<String> dcs; Map<String, String> strategyOptions; try { dcs = clientPool.runWithRetry(client -> CassandraVerifier.sanityCheckDatacenters( client, config)); KsDef ksDef = clientPool.runWithRetry(client -> client.describe_keyspace(config.getKeyspaceOrThrow())); strategyOptions = Maps.newHashMap(ksDef.getStrategy_options()); if (dcs.size() == 1) { String dc = dcs.iterator().next(); if (strategyOptions.get(dc) != null) { int currentRf = Integer.parseInt(strategyOptions.get(dc)); if (currentRf == config.replicationFactor()) { if (currentRf == 2 && config.clusterMeetsNormalConsistencyGuarantees()) { log.info("Setting Read Consistency to ONE, as cluster has only one datacenter at RF2."); readConsistency = ConsistencyLevel.ONE; rangeLoader.setConsistencyLevel(readConsistency); } } } } } catch (TException e) { return; } }
@Test public void unsetTopologyAndHighRfThrows() throws TException { setTopology(defaultTopology(HOST_1), defaultTopology(HOST_2)); when(config.replicationFactor()).thenReturn(3); assertThatThrownBy(() -> CassandraVerifier.sanityCheckDatacenters(client, config)) .isInstanceOf(IllegalStateException.class); }
@Test public void nonDefaultDcAndHighRfSucceeds() throws TException { setTopology(createDetails(DC_1, RACK_1, HOST_1)); when(config.replicationFactor()).thenReturn(3); assertThat(CassandraVerifier.sanityCheckDatacenters(client, config)) .containsExactly(DC_1); }
@Test public void unsetTopologyAndRfOneSucceeds() throws TException { setTopology(defaultTopology(HOST_1), defaultTopology(HOST_2)); when(config.replicationFactor()).thenReturn(1); assertThat(CassandraVerifier.sanityCheckDatacenters(client, config)) .containsExactly(CassandraConstants.DEFAULT_DC); }
@Test public void oneDcOneRackAndMoreHostsThanRfThrows() throws TException { setTopology(createDetails(DC_1, RACK_1, HOST_1), createDetails(DC_1, RACK_1, HOST_2), createDetails(DC_1, RACK_1, HOST_3), createDetails(DC_1, RACK_1, HOST_4)); when(config.replicationFactor()).thenReturn(3); assertThatThrownBy(() -> CassandraVerifier.sanityCheckDatacenters(client, config)) .isInstanceOf(IllegalStateException.class); }
@Test public void oneDcFewerRacksThanRfAndMoreHostsThanRfThrows() throws TException { setTopology(defaultDcDetails(RACK_1, HOST_1), defaultDcDetails(RACK_2, HOST_2), defaultDcDetails(RACK_1, HOST_3), defaultDcDetails(RACK_2, HOST_4)); when(config.replicationFactor()).thenReturn(3); assertThatThrownBy(() -> CassandraVerifier.sanityCheckDatacenters(client, config)) .isInstanceOf(IllegalStateException.class); }
@Test public void oneDcMoreRacksThanRfAndMoreHostsThanRfSucceeds() throws TException { setTopology(defaultDcDetails(RACK_1, HOST_1), defaultDcDetails(RACK_2, HOST_2), defaultDcDetails(RACK_1, HOST_3), defaultDcDetails(RACK_3, HOST_4)); when(config.replicationFactor()).thenReturn(2); assertThat(CassandraVerifier.sanityCheckDatacenters(client, config)) .containsExactly(CassandraConstants.DEFAULT_DC); }
@Test public void oneDcFewerRacksThanRfAndFewerHostsThanRfSucceeds() throws TException { setTopology(defaultDcDetails(RACK_1, HOST_1), defaultDcDetails(RACK_2, HOST_2), defaultDcDetails(RACK_1, HOST_2), defaultDcDetails(RACK_2, HOST_1)); when(config.replicationFactor()).thenReturn(3); assertThat(CassandraVerifier.sanityCheckDatacenters(client, config)) .containsExactly(CassandraConstants.DEFAULT_DC); }
@Test public void multipleDcSuceeds() throws TException { setTopology(createDetails(DC_1, RACK_1, HOST_1), createDetails(DC_1, RACK_1, HOST_2), createDetails(DC_1, RACK_1, HOST_3), createDetails(DC_2, RACK_1, HOST_4)); when(config.replicationFactor()).thenReturn(3); assertThat(CassandraVerifier.sanityCheckDatacenters(client, config)) .containsExactlyInAnyOrder(DC_1, DC_2); }
private static Set<String> getDcForSimpleStrategy(CassandraClient client, KsDef ksDef, CassandraKeyValueServiceConfig config) throws TException { checkKsDefRfEqualsOne(ksDef, config); Set<String> datacenters = sanityCheckDatacenters(client, config); checkOneDatacenter(config, datacenters); return datacenters; }
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); }
static KsDef checkAndSetReplicationFactor(CassandraClient client, KsDef ksDef, CassandraKeyValueServiceConfig config) throws TException { KsDef result = ksDef; Set<String> datacenters; if (Objects.equals(result.getStrategy_class(), CassandraConstants.SIMPLE_STRATEGY)) { datacenters = getDcForSimpleStrategy(client, result, config); result = setNetworkStrategyIfCheckedTopology(result, config, datacenters); } else { datacenters = sanityCheckDatacenters(client, config); } sanityCheckReplicationFactor(result, config, datacenters); return result; }
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; }
private void lowerConsistencyWhenSafe() { Set<String> dcs; Map<String, String> strategyOptions; try { dcs = clientPool.runWithRetry(client -> CassandraVerifier.sanityCheckDatacenters( client, config)); KsDef ksDef = clientPool.runWithRetry(client -> client.describe_keyspace(config.getKeyspaceOrThrow())); strategyOptions = Maps.newHashMap(ksDef.getStrategy_options()); if (dcs.size() == 1) { String dc = dcs.iterator().next(); if (strategyOptions.get(dc) != null) { int currentRf = Integer.parseInt(strategyOptions.get(dc)); if (currentRf == config.replicationFactor()) { if (currentRf == 2 && config.clusterMeetsNormalConsistencyGuarantees()) { log.info("Setting Read Consistency to ONE, as cluster has only one datacenter at RF2."); readConsistency = ConsistencyLevel.ONE; rangeLoader.setConsistencyLevel(readConsistency); } } } } } catch (TException e) { return; } }