in spanner-data-validator-java/src/main/java/com/google/migration/common/ShardFileReader.java [95:171]
public List<Shard> readShardingConfig(String sourceShardsFilePath) {
String jsonString = null;
try {
InputStream stream =
Channels.newInputStream(
FileSystems.open(FileSystems.matchNewResource(sourceShardsFilePath, false)));
jsonString = IOUtils.toString(stream, StandardCharsets.UTF_8);
} catch (IOException e) {
LOG.error(
"Failed to read shard input file. Make sure it is ASCII or UTF-8 encoded and contains a"
+ " well-formed JSON string.",
e);
throw new RuntimeException(
"Failed to read shard input file. Make sure it is ASCII or UTF-8 encoded and contains a"
+ " well-formed JSON string.",
e);
}
// TODO - create a structure for the shard config and map directly to the object
Type shardConfiguration = new TypeToken<Map>() {}.getType();
Map shardConfigMap =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create()
.fromJson(jsonString, shardConfiguration);
List<Shard> shardList = new ArrayList<>();
List<Map> dataShards =
(List)
(((Map) shardConfigMap.getOrDefault("shardConfigurationBulk", new HashMap<>()))
.getOrDefault("dataShards", new ArrayList<>()));
for (Map dataShard : dataShards) {
List<Map> databases = (List) (dataShard.getOrDefault("databases", new ArrayList<>()));
String host = (String) (dataShard.get("host"));
if (databases.isEmpty()) {
LOG.warn("no databases found for host: {}", host);
throw new RuntimeException("no databases found for host: " + String.valueOf(host));
}
String password =
resolvePassword(
sourceShardsFilePath,
(String) dataShard.get("secretManagerUri"),
host,
(String) dataShard.get("password"));
if (password == null || password.isEmpty()) {
LOG.warn("could not fetch password for host: {}", host);
throw new RuntimeException(
"Neither password nor secretManagerUri was found in the shard file "
+ sourceShardsFilePath
+ " for host "
+ host);
}
String namespace =
Optional.ofNullable(dataShard.get("namespace")).map(Object::toString).orElse(null);
for (Map database : databases) {
Shard shard =
new Shard(
database.getOrDefault("databaseId", database.get("dbName")).toString(),
host,
dataShard.getOrDefault("port", 0).toString(),
(String) (dataShard.get("user")),
password,
database.get("dbName").toString(),
namespace,
(String) (dataShard.get("secretManagerUri")),
dataShard.getOrDefault("connectionProperties", "").toString());
shardList.add(shard);
}
}
return shardList;
}