Skip to content

Commit 14a4aca

Browse files
committed
https://github.com/cloudera-labs/hms-mirror/issues/194
Added Commandline Option: -cdcs --consolidate-db-create-statements that allows user to consolidate the db create statements.
1 parent 0f6fe79 commit 14a4aca

File tree

9 files changed

+199
-7
lines changed

9 files changed

+199
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<groupId>com.cloudera.utils.hadoop</groupId>
3030
<artifactId>hms-mirror</artifactId>
31-
<version>3.1.0.1</version>
31+
<version>3.2.0.0</version>
3232
<packaging>jar</packaging>
3333

3434
<name>hms-mirror</name>

src/main/java/com/cloudera/utils/hms/mirror/cli/HmsMirrorCommandLineOptions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,19 @@ CommandLineRunner configSkipLinkCheckTrue(HmsMirrorConfig hmsMirrorConfig) {
10761076
};
10771077
}
10781078

1079+
@Bean
1080+
@Order(1)
1081+
@ConditionalOnProperty(
1082+
name = "hms-mirror.config.database.consolidate-db-create-statements",
1083+
havingValue = "true")
1084+
CommandLineRunner configconsolidateDBCreate(HmsMirrorConfig hmsMirrorConfig) {
1085+
return args -> {
1086+
log.info("consolidate-db-create-statements: {}", Boolean.TRUE);
1087+
hmsMirrorConfig.setConsolidateDBCreateStatements(Boolean.TRUE);
1088+
};
1089+
}
1090+
1091+
10791092
@Bean
10801093
@Order(1)
10811094
@ConditionalOnProperty(
@@ -1498,6 +1511,12 @@ private static Options getOptions() {
14981511
flipOption.setRequired(Boolean.FALSE);
14991512
options.addOption(flipOption);
15001513

1514+
Option cdcs = new Option("cdcs", "consolidate-db-create-statements", false,
1515+
"Database Create with Locations will be consolidated into a single CREATE DATABASE statement.");
1516+
cdcs.setOptionalArg(Boolean.FALSE);
1517+
cdcs.setRequired(Boolean.FALSE);
1518+
options.addOption(cdcs);
1519+
15011520
Option smDistCpOption = new Option("dc", "distcp", false,
15021521
"Build the 'distcp' workplans. Optional argument (PULL, PUSH) to define which cluster is running " +
15031522
"the distcp commands. Default is PULL.");

src/main/java/com/cloudera/utils/hms/mirror/cli/HmsMirrorCommandLineOptionsEnum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public enum HmsMirrorCommandLineOptionsEnum {
2929
COMMENT("com", "comment", null, "Comments to add to report output."),
3030
CONCURRENCY("c", "concurrency", "threads", ""),
3131
CONSOLIDATE_TABLES_FOR_DISTCP("ctfd", "consolidate-tables-for-distcp", null, ""),
32+
CONSOLIDATE_DB_CREATE_STATEMENTS("cdcs", "consolidate-db-create-statements", null, ""),
3233
CREATE_IF_NOT_EXIST("cine", "create-if-not-exist", null, ""),
3334
TARGET_NAMESPACE("tns", "target-namespace", "target", ""),
3435
COMPRESS_TEXT_OUTPUT("cto", "compress-text-output", null, ""),

src/main/java/com/cloudera/utils/hms/mirror/domain/HmsMirrorConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public class HmsMirrorConfig implements Cloneable {
101101
*/
102102
private String dbPrefix = null;
103103
private String dbRename = null;
104+
105+
private boolean consolidateDBCreateStatements = Boolean.FALSE;
106+
104107
private Environment dumpSource = Environment.LEFT;
105108
private boolean execute = Boolean.FALSE;
106109
/*

src/main/java/com/cloudera/utils/hms/mirror/service/DatabaseService.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,6 @@ public boolean buildDBStatements(DBMirror dbMirror) {
761761
}
762762
dbMirror.getSql(Environment.LEFT).add(new Pair(CREATE_DB_DESC, sb.toString()));
763763

764-
765764
// TODO: DB Properties.
766765

767766
break;
@@ -827,7 +826,7 @@ public boolean buildDBStatements(DBMirror dbMirror) {
827826
altRightDB = Boolean.TRUE;
828827
}
829828
}
830-
829+
StringBuilder sbDef = new StringBuilder();
831830
// Create the DB on the RIGHT is it doesn't exist.
832831
if (createRight) {
833832
log.debug("Building RIGHT DB SQL for {} -> CREATE", dbMirror.getName());
@@ -837,7 +836,10 @@ public boolean buildDBStatements(DBMirror dbMirror) {
837836
if (dbPropsLeft.get(COMMENT) != null && !dbPropsLeft.get(COMMENT).trim().isEmpty()) {
838837
sbL.append(COMMENT).append(" \"").append(dbPropsLeft.get(COMMENT)).append("\"\n");
839838
}
840-
dbMirror.getSql(Environment.RIGHT).add(new Pair(CREATE_DB_DESC, sbL.toString()));
839+
if (!config.isConsolidateDBCreateStatements()) {
840+
dbMirror.getSql(Environment.RIGHT).add(new Pair(CREATE_DB_DESC, sbL.toString()));
841+
}
842+
sbDef.append(sbL.toString());
841843
log.trace("RIGHT DB Create SQL: {}", sbL);
842844
}
843845

@@ -846,7 +848,11 @@ public boolean buildDBStatements(DBMirror dbMirror) {
846848
// If the original location is null or doesn't equal the target location, set it.
847849
if (isNull(origRightLocation) || !origRightLocation.equals(targetLocation)) {
848850
String alterDbLoc = MessageFormat.format(ALTER_DB_LOCATION, targetDatabase, targetLocation);
849-
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_LOCATION_DESC, alterDbLoc));
851+
if (!config.isConsolidateDBCreateStatements()) {
852+
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_LOCATION_DESC, alterDbLoc));
853+
} else {
854+
sbDef.append(DB_LOCATION).append(" \"").append(targetLocation).append("\"\n");
855+
}
850856
dbPropsRight.put(DB_LOCATION, targetLocation);
851857
log.trace("RIGHT DB Location SQL: {}", alterDbLoc);
852858
}
@@ -856,18 +862,29 @@ public boolean buildDBStatements(DBMirror dbMirror) {
856862
if (isNull(origRightManagedLocation) || !origRightManagedLocation.equals(targetManagedLocation)) {
857863
if (!config.getCluster(Environment.RIGHT).isHdpHive3()) {
858864
String alterDbMngdLoc = MessageFormat.format(ALTER_DB_MNGD_LOCATION, targetDatabase, targetManagedLocation);
859-
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_MNGD_LOCATION_DESC, alterDbMngdLoc));
865+
if (!config.isConsolidateDBCreateStatements()) {
866+
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_MNGD_LOCATION_DESC, alterDbMngdLoc));
867+
} else {
868+
sbDef.append(DB_MANAGED_LOCATION).append(" \"").append(targetManagedLocation).append("\"\n");
869+
}
860870
dbPropsRight.put(DB_MANAGED_LOCATION, targetManagedLocation);
861871
log.trace("RIGHT DB Managed Location SQL: {}", alterDbMngdLoc);
862872
} else {
863873
String alterDbMngdLoc = MessageFormat.format(ALTER_DB_LOCATION, targetDatabase, targetManagedLocation);
864-
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_LOCATION_DESC, alterDbMngdLoc));
874+
if (!config.isConsolidateDBCreateStatements()) {
875+
dbMirror.getSql(Environment.RIGHT).add(new Pair(ALTER_DB_LOCATION_DESC, alterDbMngdLoc));
876+
} else {
877+
sbDef.append(DB_LOCATION).append(" \"").append(targetManagedLocation).append("\"\n");
878+
}
865879
dbMirror.addIssue(Environment.RIGHT, HDPHIVE3_DB_LOCATION.getDesc());
866880
dbPropsRight.put(DB_LOCATION, targetManagedLocation);
867881
log.trace("RIGHT DB Managed Location SQL: {}", alterDbMngdLoc);
868882
}
869883
}
870884
}
885+
if (config.isConsolidateDBCreateStatements()) {
886+
dbMirror.getSql(Environment.RIGHT).add(new Pair(CREATE_DB_DESC, sbDef.toString()));
887+
}
871888

872889
// Build the DBPROPERITES
873890
// Check if the user has specified any DB Properties to skip.

src/main/resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ config.misc=Miscellaneous
325325
config.save=Save Configuration
326326
config.copyAvroSchemaUrls=Copy Avro Schema URLs
327327
config.databaseOnly=Database Only
328+
config.consolidateDBCreateStatements=Consolidate DB Create Statements
328329
config.skipLinkCheck=Skip Link Check
329330
config.skipLinkCheck.tooltip=Skip the link check from the Right cluster to the Left Cluster
330331
config.dbPrefix=Database Prefix

src/main/resources/templates/fragments/config/misc.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
th:checked="${CONFIG.databaseOnly}"/>
6161
</div>
6262

63+
<div class="pure-control-group">
64+
<label for="consolidateDBCreateStatements" th:text="#{config.consolidateDBCreateStatements}"></label>
65+
<input type="checkbox" name="consolidateDBCreateStatements"
66+
th:field="*{consolidateDBCreateStatements}"
67+
th:checked="${CONFIG.consolidateDBCreateStatements}"/>
68+
</div>
69+
6370
<div class="pure-control-group">
6471
<label for="readOnly" th:text="#{config.readOnly}"></label>
6572
<input type="checkbox" name="readOnly"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2024. Cloudera, Inc. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.cloudera.utils.hms.mirror.integration.end_to_end.cdp;
19+
20+
import com.cloudera.utils.hms.mirror.cli.Mirror;
21+
import com.cloudera.utils.hms.mirror.integration.end_to_end.E2EBaseTest;
22+
import lombok.extern.slf4j.Slf4j;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.context.junit.jupiter.SpringExtension;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@ExtendWith(SpringExtension.class)
31+
@SpringBootTest(classes = Mirror.class,
32+
args = {
33+
"--hms-mirror.config.data-strategy=STORAGE_MIGRATION",
34+
"--hms-mirror.config.storage-migration-namespace=s3a://my_cs_bucket",
35+
"--hms-mirror.config.warehouse-directory=/warehouse/managed",
36+
"--hms-mirror.config.external-warehouse-directory=/warehouse/external",
37+
"--hms-mirror.conversion.test-filename=/test_data/assorted_tbls_02.yaml",
38+
"--hms-mirror.config.database.consolidate-db-create-statements=true",
39+
"--hms-mirror.config.filename=/config/default.yaml.cdp",
40+
"--hms-mirror.config.output-dir=${user.home}/.hms-mirror/test-output/e2e/cdp/sm_smn_wd_cds"
41+
})
42+
@Slf4j
43+
public class Test_sm_smn_wd_cds extends E2EBaseTest {
44+
// String[] args = new String[]{
45+
// "-d", "STORAGE_MIGRATION",
46+
// "-smn", TARGET_NAMESPACE,
47+
// "-wd", "/warehouse/managed_tables", "-ewd", "/warehouse/external_tables",
48+
// "-ltd", ASSORTED_TBLS_04,
49+
// "-cfg", CDP_CDP,
50+
// "-o", outputDir
51+
// };
52+
//
53+
// long rtn = 0;
54+
// MirrorLegacy mirror = new MirrorLegacy();
55+
// rtn = mirror.go(args);
56+
// int check = 0;
57+
// assertEquals("Return Code Failure: " + rtn + " doesn't match: " + check * -1, check * -1, rtn);
58+
59+
60+
@Test
61+
public void returnCodeTest() {
62+
// Get Runtime Return Code.
63+
long rtn = getReturnCode();
64+
// Verify the return code.
65+
long check = 0L;
66+
assertEquals(check, rtn, "Return Code Failure: " + rtn);
67+
}
68+
69+
// @Test
70+
// public void phaseTest() {
71+
// validatePhase("ext_purge_odd_parts", "web_sales", PhaseState.CALCULATED_SQL);
72+
// }
73+
//
74+
// @Test
75+
// public void issueTest() {
76+
// validateTableIssueCount("ext_purge_odd_parts", "web_sales",
77+
// Environment.LEFT, 17);
78+
// }
79+
80+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2024. Cloudera, Inc. All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.cloudera.utils.hms.mirror.integration.end_to_end.cdp_to_cdp;
19+
20+
import com.cloudera.utils.hms.mirror.cli.Mirror;
21+
import com.cloudera.utils.hms.mirror.integration.end_to_end.E2EBaseTest;
22+
import lombok.extern.slf4j.Slf4j;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.context.junit.jupiter.SpringExtension;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@ExtendWith(SpringExtension.class)
31+
@SpringBootTest(classes = Mirror.class,
32+
args = {
33+
"--hms-mirror.config.data-strategy=SQL",
34+
"--hms-mirror.config.migrate-acid-only=true",
35+
"--hms-mirror.conversion.test-filename=/test_data/assorted_tbls_01.yaml",
36+
"--hms-mirror.config.filename=/config/default.yaml.cdp-cdp",
37+
"--hms-mirror.config.database.consolidate-db-create-statements=true",
38+
"--hms-mirror.config.output-dir=${user.home}/.hms-mirror/test-output/e2e/cdp_cdp/sql_mao_cds"
39+
})
40+
@Slf4j
41+
public class Test_sql_mao_cds extends E2EBaseTest {
42+
// String[] args = new String[]{"-d", "SQL",
43+
// "-mao",
44+
// "-ltd", ASSORTED_TBLS_04,
45+
// "-cfg", CDP_CDP,
46+
// "-o", outputDir
47+
// };
48+
//
49+
//
50+
// long rtn = 0;
51+
// MirrorLegacy mirror = new MirrorLegacy();
52+
// rtn = mirror.go(args);
53+
// int check = 0;
54+
// assertEquals("Return Code Failure: " + rtn + " doesn't match: " + check, rtn, check);
55+
56+
@Test
57+
public void returnCodeTest() {
58+
// Get Runtime Return Code.
59+
long rtn = getReturnCode();
60+
// Verify the return code.
61+
long check = 0L;
62+
assertEquals(check * -1, rtn, "Return Code Failure: " + rtn);
63+
}
64+
}

0 commit comments

Comments
 (0)