Skip to content

Commit

Permalink
Add --upgrade command to Schema Loader (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torch3333 committed Nov 15, 2023
1 parent 8f1495f commit d290f20
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ protected void waitForCreationIfNecessary() {
// one session to the other, so we need to wait
Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
}

@Override
protected List<String> getCommandArgsForUpgrade(Path configFilePath) {
return ImmutableList.<String>builder()
.addAll(super.getCommandArgsForUpgrade(configFilePath))
.add("--replication-factor=1")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ protected List<String> getCommandArgsForReparation(Path configFilePath, Path sch
.add("--no-backup")
.build();
}

@Override
protected List<String> getCommandArgsForUpgrade(Path configFilePath) {
return ImmutableList.<String>builder()
.addAll(super.getCommandArgsForUpgrade(configFilePath))
.add("--no-backup")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ protected List<String> getCommandArgsForReparation(Path configFilePath, Path sch
.build();
}

@Override
protected List<String> getCommandArgsForUpgrade(Path configFilePath) {
return ImmutableList.<String>builder()
.addAll(super.getCommandArgsForUpgrade(configFilePath))
.add("--replication-factor=1")
.build();
}

@Override
protected void waitForCreationIfNecessary() {
// In some of the tests, we modify metadata in one Cassandra cluster session (via the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ protected List<String> getCommandArgsForAlteration(Path configFilePath, Path sch
.build();
}

protected List<String> getCommandArgsForUpgrade(Path configFilePath) {
return ImmutableList.of("--config", configFilePath.toString(), "--upgrade");
}

@AfterAll
public void afterAll() throws Exception {
dropTablesIfExist();
Expand Down Expand Up @@ -407,6 +411,25 @@ public void createTableThenAlterTables_ShouldExecuteProperly() throws Exception
.isEqualTo(expectedTable2Metadata);
}

@Test
public void createThenDropNamespacesTableThenUpgrade_ShouldCreateNamespacesTableCorrectly()
throws Exception {
// Arrange
int exitCodeCreation =
executeWithArgs(
getCommandArgsForCreationWithCoordinator(CONFIG_FILE_PATH, SCHEMA_FILE_PATH));
assertThat(exitCodeCreation).isZero();
adminTestUtils.dropNamespacesTable();

// Act
int exitCodeUpgrade = executeWithArgs(getCommandArgsForUpgrade(CONFIG_FILE_PATH));

// Assert
assertThat(exitCodeUpgrade).isZero();
waitForCreationIfNecessary();
assertThat(transactionAdmin.getNamespaceNames()).containsOnly(namespace1, namespace2);
}

private void createTables_ShouldCreateTablesWithCoordinator() throws Exception {
// Act
int exitCode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,41 @@ private static void importTables(Either<Path, Properties> config, Either<Path, S
}
}

/**
* Upgrades the ScalarDB environment to support the latest version of the ScalarDB API. Typically,
* you will be requested, as indicated on the release notes, to run this method after updating the
* ScalarDB version of your application environment.
*
* @param configPath path to the ScalarDB config properties.
* @param options specific options for upgrading.
* @throws SchemaLoaderException thrown when upgrading failed.
*/
public static void upgrade(Path configPath, Map<String, String> options)
throws SchemaLoaderException {
Either<Path, Properties> config = new Left<>(configPath);
upgrade(config, options);
}

/**
* Upgrades the ScalarDB environment to support the latest version of the ScalarDB API. Typically,
* you will be requested, as indicated on the release notes, to run this method after updating the
* ScalarDB version of your application environment.
*
* @param configProperties ScalarDB config properties.
* @param options specific options for upgrading.
* @throws SchemaLoaderException thrown when upgrading failed.
*/
public static void upgrade(Properties configProperties, Map<String, String> options)
throws SchemaLoaderException {
Either<Path, Properties> config = new Right<>(configProperties);
upgrade(config, options);
}

private static void upgrade(Either<Path, Properties> config, Map<String, String> options)
throws SchemaLoaderException {
getSchemaOperator(config).upgrade(options);
}

@VisibleForTesting
static SchemaOperator getSchemaOperator(Either<Path, Properties> config)
throws SchemaLoaderException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,18 @@ public void importTables(List<ImportTableSchema> tableSchemaList) throws SchemaL
}
}

public void upgrade(Map<String, String> options) throws SchemaLoaderException {
try {
// As of 4.0.0, upgrade() implementation is identical for the storage or transaction admin.
// This could change in the future but for now using either admin regardless of the schema is
// fine
transactionAdmin.get().upgrade(options);
logger.info("Upgrading the environment succeeded.");
} catch (ExecutionException e) {
throw new RuntimeException("Upgrading the environment failed", e);
}
}

@Override
public void close() {
if (storageAdminLoaded.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ private static class Mode {
description = "Import tables : it will import existing non-ScalarDB tables to ScalarDB.",
defaultValue = "false")
boolean importTables;

@Option(
names = {"--upgrade"},
description =
"Upgrades the ScalarDB environment to support the latest version of the ScalarDB API. Typically, you will be requested, as indicated on the release notes, to run this command after"
+ " updating the ScalarDB version of your application environment.",
defaultValue = "false")
boolean upgrade;
}

@Override
Expand All @@ -113,6 +121,8 @@ public Integer call() throws Exception {
alterTables();
} else if (mode.importTables) {
importTables();
} else if (mode.upgrade) {
upgrade();
}
return 0;
}
Expand Down Expand Up @@ -155,6 +165,11 @@ private void importTables() throws SchemaLoaderException {
SchemaLoader.importTables(configPath, schemaFile);
}

private void upgrade() throws SchemaLoaderException {
Map<String, String> options = prepareAllOptions();
SchemaLoader.upgrade(configPath, options);
}

private Map<String, String> prepareAllOptions() {
return prepareOptions(
CassandraAdmin.REPLICATION_STRATEGY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,26 @@ public void importTable_WithConfigFilePathAndSchemaFilePath_ShouldCallParserAndO
verify(importSchemaParser).parse();
verify(operator).importTables(anyList());
}

@Test
public void upgrade_WithConfigProperties_ShouldCallOperatorProperly() throws Exception {
// Arrange

// Act
SchemaLoader.upgrade(configProperties, options);

// Assert
verify(operator).upgrade(options);
}

@Test
public void upgrade_WithConfigFilePath_ShouldCallOperatorProperly() throws Exception {
// Arrange

// Act
SchemaLoader.upgrade(configFilePath, options);

// Assert
verify(operator).upgrade(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,14 @@ private TableSchema prepareTableSchemaMock(
}
return schema;
}

@Test
public void upgrade_ShouldCallTransactionAdminProperly() throws Exception {
// Act
operator.upgrade(options);

// Assert
verify(transactionAdmin).upgrade(options);
verifyNoInteractions(storageAdmin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,41 @@ public void call_ImportOptionGivenWithCoordinatorArgument_ShouldThrowIllegalArgu
Assertions.assertThat(exitCode).isEqualTo(1);
schemaLoaderMockedStatic.verifyNoInteractions();
}

@Test
public void call_WithProperArgumentsForUpgrading_ShouldCallUpgradeProperly() {
// Arrange
String configFile = "path_to_config_file";
Map<String, String> options =
ImmutableMap.<String, String>builder()
.put(CassandraAdmin.REPLICATION_STRATEGY, replicationStrategy)
.put(CassandraAdmin.COMPACTION_STRATEGY, compactionStrategy)
.put(CassandraAdmin.REPLICATION_FACTOR, replicationFactor)
.put(DynamoAdmin.REQUEST_UNIT, ru)
.put(DynamoAdmin.NO_SCALING, noScaling.toString())
.put(DynamoAdmin.NO_BACKUP, noBackup.toString())
.build();

// Act
commandLine.execute(
"--config",
configFile,
"--upgrade",
"--replication-strategy",
replicationStrategy,
"--compaction-strategy",
compactionStrategy,
"--replication-factor",
replicationFactor,
"--ru",
ru,
"--no-scaling",
"--no-backup",
"-f",
schemaFile,
"--coordinator");

// Assert
schemaLoaderMockedStatic.verify(() -> SchemaLoader.upgrade(Paths.get(configFile), options));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ public void createTablesThenDropTablesThenRepairAllWithoutCoordinator_ShouldExec
@Disabled("repairAll is not supported with ScalarDB Server")
@Override
public void createTablesThenDropTablesThenRepairAllWithCoordinator_ShouldExecuteProperly() {}

@Test
@Disabled("upgrade is not supported with ScalarDB Server")
@Override
public void createThenDropNamespacesTableThenUpgrade_ShouldCreateNamespacesTableCorrectly()
throws Exception {
super.createThenDropNamespacesTableThenUpgrade_ShouldCreateNamespacesTableCorrectly();
}
}

0 comments on commit d290f20

Please sign in to comment.