-#include "log.h"
-#include "paxos.h"
+#include "include/log.h"
+#include "include/paxos.h"
-// Paxos must maintain some durable state (i.e., that survives power
-// failures) to run Paxos correct. This module implements a log with
-// all durable state to run Paxos. Since the values chosen correspond
-// to views, the log contains all views since the beginning of time.
+// Maintains durable state (i.e. surviving power failures) needed for correct
+// operation of Paxos as a log.
-log::log(proposer_acceptor *_acc, string _me) : pxs (_acc) {
- name = "paxos-" + _me + ".log";
- logread();
-}
+log::log(string _me) : name("paxos-" + _me + ".log") {}
-void log::logread(void) {
- std::ifstream from(name);
+void log::replay() {
+ auto from = unmarshall(read());
string type;
- unsigned instance;
- LOG << "logread";
+ DEBUG_LOG << "Replaying paxos log from disk";
while (from >> type) {
- if (type == "done") {
- string v;
- from >> instance;
- from.get();
- getline(from, v);
- pxs->values[instance] = v;
- pxs->instance_h = instance;
- LOG << "logread: instance: " << instance << " w. v = "
- << pxs->values[instance];
- pxs->accepted_value.clear();
- pxs->promise.n = 0;
- pxs->accepted.n = 0;
- } else if (type == "propseen") {
- from >> pxs->promise.n >> pxs->promise.m;
- LOG << "logread: high update: " << pxs->promise.n << "(" << pxs->promise.m << ")";
- } else if (type == "accepted") {
- string v;
- from >> pxs->accepted.n >> pxs->accepted.m;
- from.get();
- getline(from, v);
- pxs->accepted_value = v;
- LOG << "logread: prop update " << pxs->accepted.n << "(" << pxs->accepted.m << ") with v = " << pxs->accepted_value;
+ if (handlers.count(type)) {
+ handlers[type](from);
} else {
- LOG << "logread: unknown log record";
+ DEBUG_LOG << "unknown log record";
VERIFY(0);
}
- }
- from.close();
-}
-
-string log::dump() {
- std::ifstream from(name);
- string res;
- string v;
- while (std::getline(from, v))
- res += v + "\n";
- from.close();
- return res;
-}
-
-void log::restore(string s) {
- LOG << "restore: " << s;
- std::ofstream f(name, std::ios::trunc);
- f << s;
- f.close();
-}
-
-// XXX should be an atomic operation
-void log::loginstance(unsigned instance, string v) {
- std::ofstream f(name, std::ios::app);
- f << "done " << instance << " " << v << "\n";
- f.close();
+ }
}
-// an acceptor should call logprop(promise) when it
-// receives a prepare to which it responds prepare_ok().
-void log::logprop(prop_t promise) {
- std::ofstream f(name, std::ios::app);
- f << "propseen " << promise.n << " " << promise.m << "\n";
- f.close();
+string log::read() {
+ return (std::stringstream() << std::ifstream(name).rdbuf()).str();
}
-// an acceptor should call logaccept(accepted, accepted_value) when it
-// receives an accept RPC to which it replies accept_ok().
-void log::logaccept(prop_t n, string v) {
- std::ofstream f(name, std::ios::app);
- f << "accepted " << n.n << " " << n.m << " " << v << "\n";
- f.close();
+void log::write(string s) {
+ DEBUG_LOG << s;
+ std::ofstream(name, std::ios::trunc) << s;
}