JAXBContext jc = JAXBContext.newInstance("edu.yale.cs.hadoopdb.catalog.xml");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,new Boolean(true));
ObjectFactory factory = new ObjectFactory();
Configuration configuration = factory.createConfiguration();
int node_counter = 0;
int rand_offset = gen.nextInt(nodes.size() - 1);
for(String node : nodes){
File replication_script_file = new File(properties.getProperty(REPLICATION_SCRIPT_PREFIX) + node + ".sh");
BufferedWriter bw = new BufferedWriter(new FileWriter(replication_script_file));
Node n = factory.createNode();
n.setDriver(properties.getProperty(DRIVER));
n.setPassword(properties.getProperty(PASSWORD));
n.setUsername(properties.getProperty(USERNAME));
n.setLocation(node);
//Unchunked relation replication
int replica_node_id = (node_counter + rand_offset)%nodes.size();
for(String relation : relations_unchunked){
Relation r = factory.createRelation();
r.setId(relation);
//Original
Partition p = factory.createPartition();
p.setId(new Integer(node_counter).toString());
p.setUrl(properties.getProperty(URL_PREFIX) + node + ":" + properties.getProperty(PORT)
+ "/" + properties.getProperty(UNCHUNKED_DB_PREFIX) + node_counter);
r.getPartitions().add(p);
//Replica
Partition p_r = factory.createPartition();
p_r.setId(new Integer(replica_node_id).toString());
p_r.setUrl(properties.getProperty(URL_PREFIX) + node + ":" + properties.getProperty(PORT)
+ "/r_" + properties.getProperty(UNCHUNKED_DB_PREFIX) + replica_node_id);
r.getPartitions().add(p_r);
n.getRelations().add(r);
}
//Update script file
bw.append("scp -i " + properties.getProperty(SSH_KEY) + " " + nodes.get(replica_node_id) + ":"
+ properties.getProperty(DUMP_FILE_U_PREFIX) + replica_node_id + ".sql "
+ properties.getProperty(DUMP_FILE_U_PREFIX) + replica_node_id + ".sql\n");
bw.append("psql -U postgres -c 'CREATE DATABASE r_" + properties.getProperty(UNCHUNKED_DB_PREFIX) + replica_node_id
+ " WITH OWNER " + properties.getProperty(USERNAME) + ";'\n");
bw.append("psql -U " + properties.getProperty(USERNAME) + " -d r_"
+ properties.getProperty(UNCHUNKED_DB_PREFIX) + replica_node_id
+ " -f " + properties.getProperty(DUMP_FILE_U_PREFIX) + replica_node_id + ".sql\n");
//Chunked Relation Replication
int[] replica_chunk_id = new int[chunks_per_node];
for(int i = 0; i < chunks_per_node; i++){
replica_chunk_id[i] = getNumber(node_counter, (node_counter == (nodes.size() - 1)));
}
for(String relation : relations_chunked){
Relation r = factory.createRelation();
r.setId(relation);
int start_index = node_counter*chunks_per_node;
for(int index = start_index; index < start_index + chunks_per_node; index ++) {
//Original
Partition p = factory.createPartition();
p.setId(new Integer(index).toString());
p.setUrl(properties.getProperty(URL_PREFIX) + node + ":" + properties.getProperty(PORT)
+ "/" + properties.getProperty(CHUNKED_DB_PREFIX) + index);
r.getPartitions().add(p);
//Replica
int chunk_id = replica_chunk_id[index - start_index];
replica_node_id = chunk_id/20;
Partition p_r = factory.createPartition();
p_r.setId(new Integer(chunk_id).toString());
p_r.setUrl(properties.getProperty(URL_PREFIX) + node + ":" + properties.getProperty(PORT)
+ "/r_" + properties.getProperty(CHUNKED_DB_PREFIX)+ chunk_id);
r.getPartitions().add(p_r);
//Update script file
bw.append("scp -i " + properties.getProperty(SSH_KEY) + " " + nodes.get(replica_node_id) + ":"
+ properties.getProperty(DUMP_FILE_C_PREFIX) + chunk_id + ".sql "
+ properties.getProperty(DUMP_FILE_C_PREFIX) + chunk_id + ".sql\n");
bw.append("psql -U postgres -c 'CREATE DATABASE r_" + properties.getProperty(CHUNKED_DB_PREFIX) + chunk_id
+ " WITH OWNER " + properties.getProperty(USERNAME) + ";'\n");
bw.append("psql -U " + properties.getProperty(USERNAME) + " -d r_" + properties.getProperty(CHUNKED_DB_PREFIX) + chunk_id
+ " -f " + properties.getProperty(DUMP_FILE_C_PREFIX) + chunk_id + ".sql\n");
}
n.getRelations().add(r);
}
node_counter++;
configuration.getNodes().add(n);
bw.flush();
bw.close();
}
JAXBElement<Configuration> jaxb = factory.createDBClusterConfiguration(configuration);
marshaller.marshal(jaxb, new FileOutputStream(new File(properties.getProperty(CATALOG_FILE))));
}