private void createSetup(String file_path, String file_nonce,
long startTime, SystemProcedureExecutionContext context,
String hostname, final VoltTable result) {
{
Site site = context.getSite();
int numLocalPartitions = site.getPartitions().size();
LOG.trace("createSetup at : partition "+context.getPartitionExecutor().getPartitionId());
/*
* Used to close targets on failure
*/
final ArrayList<SnapshotDataTarget> targets = new ArrayList<SnapshotDataTarget>();
try {
final ArrayDeque<SnapshotTableTask> partitionedSnapshotTasks =
new ArrayDeque<SnapshotTableTask>();
final ArrayList<SnapshotTableTask> replicatedSnapshotTasks =
new ArrayList<SnapshotTableTask>();
LOG.trace("ExecutionSitesCurrentlySnapshotting initial check : " + SnapshotSiteProcessor.ExecutionSitesCurrentlySnapshotting.get());
assert(SnapshotSiteProcessor.ExecutionSitesCurrentlySnapshotting.get() == -1);
final List<Table> tables = SnapshotUtil.getTablesToSave(context.getDatabase());
synchronized (SnapshotSiteProcessor.m_digestWritten) {
if (SnapshotSiteProcessor.m_digestWritten.get() == false) {
SnapshotSiteProcessor.m_digestWritten.set(true);
SnapshotUtil.recordSnapshotTableList(startTime, file_path, file_nonce, tables);
LOG.trace("Digest written at partition " + context.getPartitionExecutor().getPartitionId());
}
}
final AtomicInteger numTables = new AtomicInteger(tables.size());
//LOG.info("NumTables Initial : "+numTables);
final SnapshotRegistry.Snapshot snapshotRecord =
SnapshotRegistry.startSnapshot(
startTime,
context.getHStoreSite().getHostId(),
context.getHStoreSite().getSiteId(),
context.getPartitionExecutor().getPartitionId(),
file_path,
file_nonce,
tables.toArray(new Table[0]));
for (final Table table : SnapshotUtil.getTablesToSave(context.getDatabase()))
{
String canSnapshot = "SUCCESS";
String err_msg = "";
final File saveFilePath =
SnapshotUtil.constructFileForTable(table, file_path, file_nonce,
String.valueOf(context.getHost().getId()),
String.valueOf(context.getHStoreSite().getSiteId()),
String.valueOf(context.getPartitionExecutor().getPartitionId())
);
SnapshotDataTarget sdt = null;
try {
sdt =
constructSnapshotDataTargetForTable(
context,
saveFilePath,
table,
context.getSite().getHost(),
numLocalPartitions,
startTime);
targets.add(sdt);
final SnapshotDataTarget sdtFinal = sdt;
final Runnable onClose = new Runnable() {
@Override
public void run() {
snapshotRecord.updateTable(table.getTypeName(),
new SnapshotRegistry.Snapshot.TableUpdater() {
@Override
public SnapshotRegistry.Snapshot.Table update(
SnapshotRegistry.Snapshot.Table registryTable) {
return snapshotRecord.new Table(
registryTable,
sdtFinal.getBytesWritten(),
sdtFinal.getLastWriteException());
}
});
int tablesLeft = numTables.decrementAndGet();
if (tablesLeft == 0) {
final SnapshotRegistry.Snapshot completed =
SnapshotRegistry.finishSnapshot(snapshotRecord);
final double duration =
(completed.timeFinished - completed.timeStarted) / 1000.0;
LOG.info(
"Snapshot " + snapshotRecord.nonce + " finished at " +
completed.timeFinished + " and took " + duration
+ " seconds ");
}
}
};
sdt.setOnCloseHandler(onClose);
final SnapshotTableTask task =
new SnapshotTableTask(
table.getRelativeIndex(),
sdt,
table.getIsreplicated(),
table.getTypeName());
if (table.getIsreplicated()) {
replicatedSnapshotTasks.add(task);
} else {
partitionedSnapshotTasks.offer(task);
}
} catch (IOException ex) {
/*
* Creation of this specific target failed. Close it if it was created.
* Continue attempting the snapshot anyways so that at least some of the data
* can be retrieved.
*/
try {
if (sdt != null) {
targets.remove(sdt);
sdt.close();
}
} catch (Exception e) {
LOG.error(e);
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
pw.flush();
canSnapshot = "FAILURE";
err_msg = "SNAPSHOT INITIATION OF " + saveFilePath +
"RESULTED IN IOException: \n" + sw.toString();
}
result.addRow(Integer.parseInt(context.getSite().getHost().getTypeName().replaceAll("[\\D]", "")),
hostname,
context.getHStoreSite().getSiteId(),
context.getPartitionExecutor().getPartitionId(),
table.getTypeName(),
canSnapshot,
err_msg);
}
synchronized (SnapshotSiteProcessor.m_taskListsForSites) {
if (!partitionedSnapshotTasks.isEmpty() || !replicatedSnapshotTasks.isEmpty()) {
// Used to sync across all partitions on all sites - set only once
if(SnapshotSiteProcessor.ExecutionSitesCurrentlySnapshotting.get() == -1){
SnapshotSiteProcessor.ExecutionSitesCurrentlySnapshotting.set(numLocalPartitions);
LOG.trace("ExecutionSitesCurrentlySnapshotting set :" + SnapshotSiteProcessor.ExecutionSitesCurrentlySnapshotting.get());
}
for (int ii = 0; ii < numLocalPartitions; ii++) {
SnapshotSiteProcessor.m_taskListsForSites.add(new ArrayDeque<SnapshotTableTask>());
}
} else {
SnapshotRegistry.discardSnapshot(snapshotRecord);
}
/**
* Distribute the writing of replicated tables to exactly one partition.
*/
CatalogMap<Partition> partition_map = site.getPartitions();
Integer lowest_partition_id = Integer.MAX_VALUE, p_id;
for (Partition pt : partition_map) {
p_id = pt.getId();
lowest_partition_id = Math.min(p_id, lowest_partition_id);
}