-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckpointer.java
executable file
·77 lines (67 loc) · 2.51 KB
/
Checkpointer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
// Updates held client content backups
public class Checkpointer extends Worker implements Runnable{
public Checkpointer(Job job, AggregationServer server)
{ super(job, server); }
// Writes the client's meta information to <uuid>.meta
private void writeMetaFile(String dir) throws IOException{
String root = dir + mJob.getUUID();
Path metaPath = Paths.get(root + ".meta");
writeFile(getWrittableMeta(), metaPath, root);
}
// Writes the client's XML content to <uuid>.xml
private void writeContentFile(String dir) throws IOException{
String root = dir + mJob.getUUID();
Path contentPath = Paths.get(root + ".xml");
writeFile(mJob.getContent(), contentPath, root);
}
// Writes the content to disk as a .tmp file before overwriting
// the destination file
private void writeFile(String toWrite, Path writePath, String root)
throws IOException {
Path tmpPath = Paths.get(root + ".tmp");
// Write file to tmp
Files.write(tmpPath, toWrite.getBytes());
// Overwrite previous content file
Files.move(tmpPath, writePath, REPLACE_EXISTING);
}
// Writes client content to file if present
private void maybeUpdateContentFile(String root)
throws InterruptedException, IOException{
if (mJob.getContent() != null)
{ writeContentFile(root); }
}
// Creates a folder for storing backups if required
private void maybeCreateFolder(String dir) throws IOException{
if (!Files.exists(Paths.get(dir)))
{ Files.createDirectories(Paths.get(dir)); }
}
// Assembles clients time information into a string
private String getWrittableMeta(){
String meta = "TimeStamp," + mJob.getTimeStamp().toString();
meta += "\r\n" + "LogicalTime," + mClock.getTime() +"\r\n";
return meta;
}
public void run(){
// Root directory for storing backups
String dir = "./Server/Checkpoint/";
try{
mFileLock.acquire();
// Critical Section
maybeCreateFolder(dir);
writeMetaFile(dir);
maybeUpdateContentFile(dir);
// Critical Section
mFileLock.release();
}catch (Exception e){ e.printStackTrace(); }
if (TEST && mLogger != null){
try{ writeToLog("Checkpoint Update", mJob.getUUID()); }
catch (Exception e){ e.printStackTrace(); }
}
}
}