Skip to content

Commit

Permalink
Base64 the metadata export result (#25691)
Browse files Browse the repository at this point in the history
* Base64 the metadata export result

* Add license check skip suffix
  • Loading branch information
Pace2Car authored May 16, 2023
1 parent dc2ec8c commit 1d62d3f
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ header:
- 'docs/**'
- '**/*.md'
- '**/*.json'
- '**/*.data'
- '**/*.iml'
- '**/*.ini'
- 'LICENSE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import org.apache.commons.codec.binary.Base64;
import org.apache.shardingsphere.distsql.handler.ral.query.MetaDataRequiredQueryableRALExecutor;
import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ExportMetaDataStatement;
import org.apache.shardingsphere.globalclock.core.provider.GlobalClockProvider;
Expand Down Expand Up @@ -77,7 +78,7 @@ private String generateExportData(final ShardingSphereMetaData metaData) {
ExportedClusterInfo exportedClusterInfo = new ExportedClusterInfo();
exportedClusterInfo.setMetaData(exportedMetaData);
generateSnapshotInfo(metaData, exportedClusterInfo);
return JsonUtils.toJsonString(exportedClusterInfo);
return Base64.encodeBase64String(JsonUtils.toJsonString(exportedClusterInfo).getBytes());
}

private Map<String, String> getDatabases(final ProxyContext proxyContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater;
import org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportMetaDataStatement;
Expand Down Expand Up @@ -54,18 +55,17 @@ public void executeUpdate(final String databaseName, final ImportMetaDataStateme
if (sqlStatement.getFilePath().isPresent()) {
File file = new File(sqlStatement.getFilePath().get());
try {
jsonMetaDataConfig = FileUtils.readFileToString(file, Charset.defaultCharset());
jsonMetaDataConfig = new String(Base64.decodeBase64(FileUtils.readFileToString(file, Charset.defaultCharset())));
} catch (final IOException ex) {
throw new FileIOException(ex);
}
} else {
jsonMetaDataConfig = sqlStatement.getMetaDataValue();
jsonMetaDataConfig = new String(Base64.decodeBase64(sqlStatement.getMetaDataValue()));
}
ExportedClusterInfo exportedClusterInfo = JsonUtils.readValue(jsonMetaDataConfig, ExportedClusterInfo.class);
ExportedMetaData exportedMetaData = exportedClusterInfo.getMetaData();
importServerConfig(exportedMetaData);
importDatabase(exportedMetaData);
// TODO restore snapshot info
}

private void importServerConfig(final ExportedMetaData exportedMetaData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
@StaticMockSettings(ProxyContext.class)
class ExportMetaDataExecutorTest {

private static final String METADATA_VALUE_EXPECTED = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7ImVtcHR5X21ldGFkYXRhIjoiZGF0YWJhc2VOYW1lOiBudWxsXG5kYXRhU291cmNlczpcbn"
+ "J1bGVzOlxuIn0sInByb3BzIjoiIiwicnVsZXMiOiJydWxlczpcbi0gIUdMT0JBTF9DTE9DS1xuICBlbmFibGVkOiBmYWxzZVxuICBwcm92aWRlcjogbG9jYWxcbiAgdHlwZTogVFNPXG4ifX0=";

private final ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);

@Test
Expand All @@ -98,9 +101,7 @@ void assertExecuteWithEmptyMetaData() {
Collection<LocalDataQueryResultRow> actual = new ExportMetaDataExecutor().getRows(contextManager.getMetaDataContexts().getMetaData(), sqlStatement);
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(3),
is("{\"meta_data\":{\"databases\":{\"empty_metadata\":\"databaseName: null\\ndataSources:\\nrules:\\n\"},"
+ "\"props\":\"\",\"rules\":\"rules:\\n- !GLOBAL_CLOCK\\n enabled: false\\n provider: local\\n type: TSO\\n\"}}"));
assertThat(row.getCell(3), is(METADATA_VALUE_EXPECTED));
}

private ContextManager mockEmptyContextManager() {
Expand Down Expand Up @@ -164,7 +165,7 @@ private DataSource createDataSource(final String name) {
@SneakyThrows(IOException.class)
private String loadExpectedRow() {
StringBuilder result = new StringBuilder();
String fileName = Objects.requireNonNull(ExportMetaDataExecutorTest.class.getResource("/expected/export-metadata-configuration.json")).getFile();
String fileName = Objects.requireNonNull(ExportMetaDataExecutorTest.class.getResource("/expected/export-metadata-configuration.data")).getFile();
try (
FileReader fileReader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fileReader)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
@MockitoSettings(strictness = Strictness.LENIENT)
class ImportMetaDataUpdaterTest {

private static final String METADATA_VALUE = "{\"meta_data\":{\"databases\":{\"sharding_db\":\"databaseName: sharding_db\\ndataSources:\\nrules:\\n\"},"
+ "\"props\":\"props:\\n system-log-level: INFO\\n sql-show: false\\n\","
+ "\"rules\":\"rules:\\n- !AUTHORITY\\n privilege:\\n type: ALL_PERMITTED\\n users:\\n - authenticationMethodName: ''\\n password: root\\n user: root@%\\n\"}}";
private static final String METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7InNoYXJkaW5nX2RiIjoiZGF0YWJhc2VOYW1lOiBzaGFyZGluZ19kYlxuZGF0YVNvdXJjZXM6XG5ydWxlczpcbiJ9LCJwcm9w"
+ "cyI6InByb3BzOlxuICBzeXN0ZW0tbG9nLWxldmVsOiBJTkZPXG4gIHNxbC1zaG93OiBmYWxzZVxuIiwicnVsZXMiOiJydWxlczpcbi0gIUFVVEhPUklUWVxuICBwcml2aWxlZ2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFR"
+ "FxuICB1c2VyczpcbiAgLSBhdXRoZW50aWNhdGlvbk1ldGhvZE5hbWU6ICcnXG4gICAgcGFzc3dvcmQ6IHJvb3RcbiAgICB1c2VyOiByb290QCVcbiJ9fQ==";

private static final String EMPTY = "empty_metadata";

Expand All @@ -71,7 +71,7 @@ class ImportMetaDataUpdaterTest {

@BeforeEach
void setup() {
featureMap.put(EMPTY, "/conf/import/empty-metadata.json");
featureMap.put(EMPTY, "/conf/import/empty-metadata.data");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7ImVtcHR5X21ldGFkYXRhIjoiZGF0YWJhc2VOYW1lOiBlbXB0eV9tZXRhZGF0YVxuIn0sInByb3BzIjoiIiwicnVsZXMiOiIifX0=

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7Im5vcm1hbF9kYiI6ImRhdGFiYXNlTmFtZTogbm9ybWFsX2RiXG5kYXRhU291cmNlczpcbiAgZHNfMDpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOm9wZW5nYXVzczovLzEyNy4wLjAuMTo1NDMyL2RlbW9fZHNfMFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiAgZHNfMTpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOm9wZW5nYXVzczovLzEyNy4wLjAuMTo1NDMyL2RlbW9fZHNfMVxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiJ9LCJwcm9wcyI6InByb3BzOlxuICBzcWwtc2hvdzogdHJ1ZVxuIiwicnVsZXMiOiJydWxlczpcbi0gIUFVVEhPUklUWVxuICBwcml2aWxlZ2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFRFxuICB1c2VyczpcbiAgLSBhdXRoZW50aWNhdGlvbk1ldGhvZE5hbWU6ICcnXG4gICAgcGFzc3dvcmQ6IHJvb3RcbiAgICB1c2VyOiByb290QCVcbi0gIUdMT0JBTF9DTE9DS1xuICBlbmFibGVkOiBmYWxzZVxuICBwcm92aWRlcjogbG9jYWxcbiAgdHlwZTogVFNPXG4ifX0=

This file was deleted.

0 comments on commit 1d62d3f

Please sign in to comment.