// load parameters
meta = AdminToolUtils.getValueList((List<String>) options.valuesOf(OPT_HEAD_META_SET),
"=");
if(meta.size() != 2 && meta.size() != 4) {
throw new VoldemortException("Invalid metakey-metafile pairs.");
}
url = (String) options.valueOf(AdminParserUtils.OPT_URL);
if(options.has(AdminParserUtils.OPT_NODE)) {
nodeIds = (List<Integer>) options.valuesOf(AdminParserUtils.OPT_NODE);
allNodes = false;
}
if(options.has(AdminParserUtils.OPT_CONFIRM)) {
confirm = true;
}
// print summary
System.out.println("Set metadata");
System.out.println("Metadata:");
for(Integer i = 0; i < meta.size(); i += 2) {
System.out.println(" set \'" + meta.get(i) + "\' from file \'" + meta.get(i + 1)
+ "\'");
}
System.out.println("Location:");
System.out.println(" bootstrap url = " + url);
if(allNodes) {
System.out.println(" node = all nodes");
} else {
System.out.println(" node = " + Joiner.on(", ").join(nodeIds));
}
// execute command
if(!AdminToolUtils.askConfirm(confirm, "set metadata")) {
return;
}
AdminClient adminClient = AdminToolUtils.getAdminClient(url);
if(allNodes) {
nodeIds = AdminToolUtils.getAllNodeIds(adminClient);
}
AdminToolUtils.assertServerNotInRebalancingState(adminClient, nodeIds);
if(meta.size() == 2) {
String metaKey = meta.get(0), metaValue = meta.get(1);
String metaFile = metaValue.replace("~", System.getProperty("user.home"));
if(metaKey.equals(MetadataStore.CLUSTER_KEY)
|| metaKey.equals(MetadataStore.REBALANCING_SOURCE_CLUSTER_XML)) {
if(!Utils.isReadableFile(metaFile)) {
throw new VoldemortException("Cluster xml file path incorrect");
}
ClusterMapper mapper = new ClusterMapper();
Cluster newCluster = mapper.readCluster(new File(metaFile));
doMetaSet(adminClient, nodeIds, metaKey, mapper.writeCluster(newCluster));
} else if(metaKey.equals(MetadataStore.STORES_KEY)) {
if(!Utils.isReadableFile(metaFile)) {
throw new VoldemortException("Stores definition xml file path incorrect");
}
StoreDefinitionsMapper mapper = new StoreDefinitionsMapper();
List<StoreDefinition> newStoreDefs = mapper.readStoreList(new File(metaFile));
StoreDefinitionUtils.validateSchemasAsNeeded(newStoreDefs);
// original metadata
Integer nodeIdToGetStoreXMLFrom = nodeIds.iterator().next();
Versioned<String> storesXML = adminClient.metadataMgmtOps.getRemoteMetadata(nodeIdToGetStoreXMLFrom,
MetadataStore.STORES_KEY);
List<StoreDefinition> oldStoreDefs = mapper.readStoreList(new StringReader(storesXML.getValue()));
doMetaSet(adminClient, nodeIds, metaKey, mapper.writeStoreList(newStoreDefs));
if(!allNodes) {
System.err.println("WARNING: Metadata version update of stores goes to all servers, "
+ "although this set-metadata oprations only goes to node: ");
for(Integer nodeId: nodeIds) {
System.err.println(nodeId);
}
}
doMetaUpdateVersionsOnStores(adminClient, oldStoreDefs, newStoreDefs);
} else if(metaKey.equals(MetadataStore.SLOP_STREAMING_ENABLED_KEY)
|| metaKey.equals(MetadataStore.PARTITION_STREAMING_ENABLED_KEY)
|| metaKey.equals(MetadataStore.READONLY_FETCH_ENABLED_KEY)) {
doMetaSet(adminClient, nodeIds, metaKey, metaValue);
} else if(metaKey.equals(KEY_OFFLINE)) {
for(Integer nodeId: nodeIds) {
adminClient.metadataMgmtOps.setRemoteOfflineState(nodeId,
Boolean.parseBoolean(metaValue));
}
} else if(metaKey.equals(MetadataStore.REBALANCING_STEAL_INFO)) {
if(!Utils.isReadableFile(metaFile)) {
throw new VoldemortException("Rebalancing steal info file path incorrect");
}
String rebalancingStealInfoJsonString = FileUtils.readFileToString(new File(metaFile));
RebalancerState state = RebalancerState.create(rebalancingStealInfoJsonString);
doMetaSet(adminClient, nodeIds, metaKey, state.toJsonString());
} else {
throw new VoldemortException("Incorrect metadata key");
}
} else if(meta.size() == 4) {
// set metadata pair cluster.xml, stores.xml
String clusterFile, storesFile;
if(meta.get(0).equals(MetadataStore.CLUSTER_KEY)
&& meta.get(2).equals(MetadataStore.STORES_KEY)) {
clusterFile = meta.get(1);
storesFile = meta.get(3);
} else if(meta.get(0).equals(MetadataStore.STORES_KEY)
&& meta.get(2).equals(MetadataStore.CLUSTER_KEY)) {
storesFile = meta.get(1);
clusterFile = meta.get(3);
} else {
throw new VoldemortException("meta set-pair keys must be <cluster.xml, stores.xml>");
}
clusterFile = clusterFile.replace("~", System.getProperty("user.home"));
storesFile = storesFile.replace("~", System.getProperty("user.home"));
ClusterMapper clusterMapper = new ClusterMapper();
StoreDefinitionsMapper storeDefsMapper = new StoreDefinitionsMapper();
// original metadata
Integer nodeIdToGetStoreXMLFrom = nodeIds.iterator().next();
Versioned<String> storesXML = adminClient.metadataMgmtOps.getRemoteMetadata(nodeIdToGetStoreXMLFrom,
MetadataStore.STORES_KEY);
List<StoreDefinition> oldStoreDefs = storeDefsMapper.readStoreList(new StringReader(storesXML.getValue()));
if(!Utils.isReadableFile(clusterFile)) {
throw new VoldemortException("Cluster xml file path incorrect");
}
Cluster cluster = clusterMapper.readCluster(new File(clusterFile));
if(!Utils.isReadableFile(storesFile)) {
throw new VoldemortException("Stores definition xml file path incorrect");
}
List<StoreDefinition> newStoreDefs = storeDefsMapper.readStoreList(new File(storesFile));
StoreDefinitionUtils.validateSchemasAsNeeded(newStoreDefs);