rev = mk.getHeadRevision();
}
int sum = 0;
for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
String json = mk.getNodes("/node-" + entry.getKey(), rev, 0, 0, 1000, null);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(json);
entry.setValue(obj);
sum += (Long) obj.get("value");
}
if (sum < 60) {
// retry with other nodes
return false;
}
StringBuilder jsop = new StringBuilder();
boolean withdrawn = false;
for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
long value = (Long) entry.getValue().get("value");
jsop.append("^\"/node-").append(entry.getKey());
jsop.append("/value\":");
if (value >= 20 && !withdrawn) {
jsop.append(value - 20);
withdrawn = true;
} else {
jsop.append(value + 10);
}
}
String oldRev = rev;
rev = mk.commit("", jsop.toString(), rev, null);
if (useBranch) {
rev = mk.merge(rev, null);
}
log("Successful transfer @" + oldRev + ": " + jsop.toString() + " (new rev: " + rev + ")");
return true;
}
}));
}
for (Thread t : writers) {
t.start();
}
for (Thread t : writers) {
t.join();
}
// dispose will flush all pending revisions
for (MongoMK mk : kernels) {
mk.dispose();
}
MongoMK mk = openMongoMK();
String rev = mk.getHeadRevision();
long sum = 0;
for (int i = 0; i < NUM_NODES; i++) {
String json = mk.getNodes("/node-" + i, rev, 0, 0, 1000, null);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(json);
sum += (Long) obj.get("value");
}
log("Conflict rate: " + conflicts.get() +
"/" + (NUM_WRITERS * NUM_TRANSFERS_PER_THREAD));
System.out.print(logBuffer);