com.datastax.driver.core.VersionNumber Java Examples

The following examples show how to use com.datastax.driver.core.VersionNumber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Migration016.java    From cassandra-reaper with Apache License 2.0 6 votes vote down vote up
/**
 * if Cassandra is running version less than 4.0
 *  alter every table to set `dclocal_read_repair_chance` to zero
 */
public static void migrate(Session session, String keyspace) {

  VersionNumber highestNodeVersion = session.getCluster().getMetadata().getAllHosts()
      .stream()
      .map(host -> host.getCassandraVersion())
      .max(VersionNumber::compareTo)
      .get();

  if (0 < VersionNumber.parse("4.0").compareTo(highestNodeVersion)) {
    LOG.warn("altering every table to set `dclocal_read_repair_chance` to zero…");
    session.getCluster().getMetadata().getKeyspace(keyspace).getTables()
        .stream()
        .filter(table -> !table.getName().equals("repair_schedule") && !table.getName().equals("repair_unit"))
        .forEach(tbl -> session.executeAsync(
            "ALTER TABLE " + tbl.getName() + " WITH dclocal_read_repair_chance = 0"));

    LOG.warn("alter every table to set `dclocal_read_repair_chance` to zero completed.");
  }

}
 
Example #2
Source File: CassandraSession.java    From presto with Apache License 2.0 5 votes vote down vote up
public VersionNumber getCassandraVersion()
{
    ResultSet result = executeWithSession(session -> session.execute("select release_version from system.local"));
    Row versionRow = result.one();
    if (versionRow == null) {
        throw new PrestoException(CASSANDRA_VERSION_ERROR, "The cluster version is not available. " +
                "Please make sure that the Cassandra cluster is up and running, " +
                "and that the contact points are specified correctly.");
    }
    return VersionNumber.parse(versionRow.getString("release_version"));
}
 
Example #3
Source File: TestCassandraClusteringPredicatesExtractor.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeTest
void setUp()
{
    col1 = new CassandraColumnHandle("partitionKey1", 1, CassandraType.BIGINT, true, false, false, false);
    col2 = new CassandraColumnHandle("clusteringKey1", 2, CassandraType.BIGINT, false, true, false, false);
    col3 = new CassandraColumnHandle("clusteringKey2", 3, CassandraType.BIGINT, false, true, false, false);
    col4 = new CassandraColumnHandle("clusteringKe3", 4, CassandraType.BIGINT, false, true, false, false);

    cassandraTable = new CassandraTable(
            new CassandraTableHandle("test", "records"), ImmutableList.of(col1, col2, col3, col4));

    cassandraVersion = VersionNumber.parse("2.1.5");
}
 
Example #4
Source File: DatabaseTest.java    From cassandra-migration with MIT License 5 votes vote down vote up
@Test
public void testCassandraVersionCheck() {
    Database database = new Database(cassandra.getCluster(), CassandraJUnitRule.TEST_KEYSPACE);
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("1.1.14")), is(false));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("1.2.19")), is(false));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("2.0.10")), is(true));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("2.1.19")), is(true));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("2.2.14")), is(true));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("3.0.15")), is(true));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("3.11.4")), is(true));
    assertThat(database.isVersionAtLeastV2(VersionNumber.parse("4.0")), is(true));
    database.close();

}
 
Example #5
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
public CassandraStorage(
    UUID reaperInstanceId,
    ReaperApplicationConfiguration config,
    Environment environment) throws ReaperException {

  this.reaperInstanceId = reaperInstanceId;
  CassandraFactory cassandraFactory = config.getCassandraFactory();
  overrideQueryOptions(cassandraFactory);
  overrideRetryPolicy(cassandraFactory);
  overridePoolingOptions(cassandraFactory);

  // https://docs.datastax.com/en/developer/java-driver/3.5/manual/metrics/#metrics-4-compatibility
  cassandraFactory.setJmxEnabled(false);
  if (!CassandraStorage.UNINITIALISED.compareAndSet(true, false)) {
    // If there's been a past connection attempt, metrics are already registered
    cassandraFactory.setMetricsEnabled(false);
  }

  cassandra = cassandraFactory.build(environment);
  if (config.getActivateQueryLogger()) {
    cassandra.register(QueryLogger.builder().build());
  }
  CodecRegistry codecRegistry = cassandra.getConfiguration().getCodecRegistry();
  codecRegistry.register(new DateTimeCodec());
  session = cassandra.connect(config.getCassandraFactory().getKeyspace());

  version = cassandra.getMetadata().getAllHosts()
      .stream()
      .map(h -> h.getCassandraVersion())
      .min(VersionNumber::compareTo)
      .get();

  initializeAndUpgradeSchema(cassandra, session, config, version);
  prepareStatements();
}
 
Example #6
Source File: BasicSteps.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
@And("^the last added repair has twcs table \"([^\"]*)\" in the blacklist$")
public void the_last_added_repair_has_twcs_table_in_the_blacklist(String twcsTable) throws Throwable {
  synchronized (BasicSteps.class) {
    final VersionNumber lowestNodeVersion = getCassandraVersion();

    RUNNERS.parallelStream().forEach(runner -> {
      Response response = runner.callReaper("GET", "/repair_run/cluster/" + TestContext.TEST_CLUSTER, EMPTY_PARAMS);
      String responseData = response.readEntity(String.class);
      assertEquals(responseData, Response.Status.OK.getStatusCode(), response.getStatus());
      Assertions.assertThat(responseData).isNotBlank();
      List<RepairRunStatus> runs = SimpleReaperClient.parseRepairRunStatusListJSON(responseData);
      if ((reaperVersion.isPresent()
          && 0 < VersionNumber.parse("1.4.0").compareTo(VersionNumber.parse(reaperVersion.get())))
          // while DTCS is available in 2.0.11 it is not visible over jmx until 2.1
          //  see `Table.DEFAULT_COMPACTION_STRATEGY`
          || VersionNumber.parse("2.1").compareTo(lowestNodeVersion) > 0) {

        Assertions
            .assertThat(runs.get(0).getColumnFamilies().contains(twcsTable))
            .isTrue();
      } else {
        // auto TWCS blacklisting was only added in Reaper 1.4.0, and requires Cassandra >= 2.1
        Assertions
            .assertThat(runs.get(0).getColumnFamilies().contains(twcsTable))
            .isFalse();
      }
    });
  }
}
 
Example #7
Source File: BasicSteps.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private static void createKeyspace(String keyspaceName) {
  try (Cluster cluster = buildCluster(); Session tmpSession = cluster.connect()) {
    VersionNumber lowestNodeVersion = getCassandraVersion(tmpSession);

    try {
      if (null == tmpSession.getCluster().getMetadata().getKeyspace(keyspaceName)) {
        tmpSession.execute(
            "CREATE KEYSPACE "
                + (VersionNumber.parse("2.0").compareTo(lowestNodeVersion) <= 0 ? "IF NOT EXISTS " : "")
                + keyspaceName
              + " WITH replication = {" + buildNetworkTopologyStrategyString(cluster) + "}");
      }
    } catch (AlreadyExistsException ignore) { }
  }
}
 
Example #8
Source File: BasicSteps.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private static VersionNumber getCassandraVersion(Session tmpSession) {

    return tmpSession
            .getCluster()
            .getMetadata()
            .getAllHosts()
            .stream()
            .map(host -> host.getCassandraVersion())
            .min(VersionNumber::compareTo)
            .get();
  }
 
Example #9
Source File: BasicSteps.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private static void createTable(String keyspaceName, String tableName) {
  try (Cluster cluster = buildCluster(); Session tmpSession = cluster.connect()) {
    VersionNumber lowestNodeVersion = getCassandraVersion(tmpSession);

    String createTableStmt
        = "CREATE TABLE "
            + (VersionNumber.parse("2.0").compareTo(lowestNodeVersion) <= 0 ? "IF NOT EXISTS " : "")
            + keyspaceName
            + "."
            + tableName
            + "(id int PRIMARY KEY, value text)";

    if (tableName.endsWith("twcs")) {
      if (((VersionNumber.parse("3.0.8").compareTo(lowestNodeVersion) <= 0
          && VersionNumber.parse("3.0.99").compareTo(lowestNodeVersion) >= 0)
          || VersionNumber.parse("3.8").compareTo(lowestNodeVersion) <= 0)) {
        // TWCS is available by default
        createTableStmt
            += " WITH compaction = {'class':'TimeWindowCompactionStrategy',"
                + "'compaction_window_size': '1', "
                + "'compaction_window_unit': 'MINUTES'}";
      } else if (VersionNumber.parse("2.0.11").compareTo(lowestNodeVersion) <= 0) {
        createTableStmt += " WITH compaction = {'class':'DateTieredCompactionStrategy'}";
      }
    }

    try {
      if (null == tmpSession.getCluster().getMetadata().getKeyspace(keyspaceName).getTable(tableName)) {
        tmpSession.execute(createTableStmt);
      }
    } catch (AlreadyExistsException ignore) { }

    for (int i = 0; i < 100; i++) {
      tmpSession.execute(
          "INSERT INTO " + keyspaceName + "." + tableName + "(id, value) VALUES(" + i + ",'" + i + "')");
    }
  }
}
 
Example #10
Source File: CassandraClusteringPredicatesExtractor.java    From presto with Apache License 2.0 4 votes vote down vote up
public CassandraClusteringPredicatesExtractor(List<CassandraColumnHandle> clusteringColumns, TupleDomain<ColumnHandle> predicates, VersionNumber cassandraVersion)
{
    this.predicates = requireNonNull(predicates, "predicates is null");
    this.clusteringPushDownResult = getClusteringKeysSet(clusteringColumns, predicates, requireNonNull(cassandraVersion, "cassandraVersion is null"));
}
 
Example #11
Source File: RepairUnitService.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
private static Integer versionCompare(String str1, String str2) {
  VersionNumber version1 = VersionNumber.parse(str1);
  VersionNumber version2 = VersionNumber.parse(str2);
  return version1.compareTo(version2);
}
 
Example #12
Source File: Migration021.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
/**
 * Apply TWCS for metrics tables if the Cassandra version allows it.
 */
public static void migrate(Session session, String keyspace) {

  VersionNumber lowestNodeVersion = session.getCluster().getMetadata().getAllHosts()
      .stream()
      .map(host -> host.getCassandraVersion())
      .min(VersionNumber::compareTo)
      .get();

  if ((VersionNumber.parse("3.0.8").compareTo(lowestNodeVersion) <= 0
      && VersionNumber.parse("3.0.99").compareTo(lowestNodeVersion) >= 0)
      || VersionNumber.parse("3.8").compareTo(lowestNodeVersion) <= 0) {
    try {
      if (!isUsingTwcs(session, keyspace)) {
        LOG.info("Altering {} to use TWCS...", METRICS_V1_TABLE);
        session.execute(
                "ALTER TABLE " + METRICS_V1_TABLE + " WITH compaction = {'class': 'TimeWindowCompactionStrategy', "
                    + "'unchecked_tombstone_compaction': 'true', "
                    + "'compaction_window_size': '2', "
                    + "'compaction_window_unit': 'MINUTES'}");

        LOG.info("Altering {} to use TWCS...", METRICS_V2_TABLE);
        session.execute(
                "ALTER TABLE " + METRICS_V2_TABLE + " WITH compaction = {'class': 'TimeWindowCompactionStrategy', "
                    + "'unchecked_tombstone_compaction': 'true', "
                    + "'compaction_window_size': '1', "
                    + "'compaction_window_unit': 'HOURS'}");

        LOG.info("{} was successfully altered to use TWCS.", METRICS_V2_TABLE);

        LOG.info("Altering {} to use TWCS...", OPERATIONS_TABLE);
        session.execute(
                "ALTER TABLE " + OPERATIONS_TABLE + " WITH compaction = {'class': 'TimeWindowCompactionStrategy', "
                    + "'unchecked_tombstone_compaction': 'true', "
                    + "'compaction_window_size': '1', "
                    + "'compaction_window_unit': 'HOURS'}");

        LOG.info("{} was successfully altered to use TWCS.", OPERATIONS_TABLE);
      }
    } catch (RuntimeException e) {
      LOG.error("Failed altering metrics tables to TWCS", e);
    }
  }

}
 
Example #13
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
private static void initializeAndUpgradeSchema(
    com.datastax.driver.core.Cluster cassandra,
    Session session,
    ReaperApplicationConfiguration config,
    VersionNumber version) throws ReaperException {

  Preconditions.checkState(
          0 >= VersionNumber.parse("2.1").compareTo(version),
          "All Cassandra nodes in Reaper's backend storage must be running version 2.1+");

  try (Database database = new Database(cassandra, config.getCassandraFactory().getKeyspace())) {

    int currentVersion = database.getVersion();
    Preconditions.checkState(
        currentVersion == 0 || currentVersion >= 15,
        "You need to upgrade from Reaper 1.2.2 at least in order to run this version. "
        + "Please upgrade to 1.2.2, or greater, before performing this upgrade.");

    MigrationRepository migrationRepo = new MigrationRepository("db/cassandra");
    if (currentVersion < migrationRepo.getLatestVersion()) {
      LOG.warn("Starting db migration from {} to {}…", currentVersion, migrationRepo.getLatestVersion());

      if (15 <= currentVersion) {
        List<String> otherRunningReapers = session.execute("SELECT reaper_instance_host FROM running_reapers").all()
            .stream()
            .map((row) -> row.getString("reaper_instance_host"))
            .filter((reaperInstanceHost) -> !AppContext.REAPER_INSTANCE_ADDRESS.equals(reaperInstanceHost))
            .collect(Collectors.toList());

        Preconditions.checkState(
            otherRunningReapers.isEmpty(),
            "Database migration can not happen with other reaper instances running. Found ",
            StringUtils.join(otherRunningReapers));
      }

      // We now only support migrations starting at version 15 (Reaper 1.2.2)
      int startVersion = database.getVersion() == 0 ? 15 : database.getVersion();
      migrate(startVersion, migrationRepo, session);
      // some migration steps depend on the Cassandra version, so must be rerun every startup
      Migration016.migrate(session, config.getCassandraFactory().getKeyspace());
      // Switch metrics table to TWCS if possible, this is intentionally executed every startup
      Migration021.migrate(session, config.getCassandraFactory().getKeyspace());
    } else {
      LOG.info(
          String.format("Keyspace %s already at schema version %d", session.getLoggedKeyspace(), currentVersion));
    }
  }
}
 
Example #14
Source File: BasicSteps.java    From cassandra-reaper with Apache License 2.0 4 votes vote down vote up
private static VersionNumber getCassandraVersion() {
  try (Cluster cluster = buildCluster(); Session tmpSession = cluster.connect()) {
    return getCassandraVersion(tmpSession);
  }
}
 
Example #15
Source File: JmxProxyImpl.java    From cassandra-reaper with Apache License 2.0 3 votes vote down vote up
/**
 * Compares two Cassandra versions using classes provided by the Datastax Java Driver.
 *
 * @param str1 a string of ordinal numbers separated by decimal points.
 * @param str2 a string of ordinal numbers separated by decimal points.
 * @return The result is a negative integer if str1 is _numerically_ less than str2. The result is
 *     a positive integer if str1 is _numerically_ greater than str2. The result is zero if the
 *     strings are _numerically_ equal. It does not work if "1.10" is supposed to be equal to
 *     "1.10.0".
 */
static Integer versionCompare(String str1, String str2) {
  VersionNumber version1 = VersionNumber.parse(str1);
  VersionNumber version2 = VersionNumber.parse(str2);

  return version1.compareTo(version2);
}