4 #include "threaded_log.h"
6 // Paxos must maintain some durable state (i.e., that survives power
7 // failures) to run Paxos correct. This module implements a log with
8 // all durable state to run Paxos. Since the values chosen correspond
9 // to views, the log contains all views since the beginning of time.
11 log::log(acceptor *_acc, std::string _me) : pxs (_acc) {
12 name = "paxos-" + _me + ".log";
16 void log::logread(void) {
21 from.open(name.c_str());
23 while (from >> type) {
29 pxs->values[instance] = v;
30 pxs->instance_h = instance;
31 LOG("logread: instance: " << instance << " w. v = " <<
32 pxs->values[instance]);
36 } else if (type == "propseen") {
39 LOG("logread: high update: " << pxs->n_h.n << "(" << pxs->n_h.m << ")");
40 } else if (type == "accepted") {
47 LOG("logread: prop update " << pxs->n_a.n << "(" << pxs->n_a.m << ") with v = " << pxs->v_a);
49 LOG("logread: unknown log record");
56 std::string log::dump() {
60 from.open(name.c_str());
61 while (getline(from, v))
67 void log::restore(std::string s) {
69 LOG("restore: " << s);
70 f.open(name.c_str(), std::ios::trunc);
75 // XXX should be an atomic operation
76 void log::loginstance(unsigned instance, std::string v) {
77 std::ofstream f(name, std::ios::app);
78 f << "done " << instance << " " << v << "\n";
82 // an acceptor should call logprop(n_h) when it
83 // receives a prepare to which it responds prepare_ok().
84 void log::logprop(prop_t n_h) {
86 f.open(name.c_str(), std::ios::app);
96 // an acceptor should call logaccept(n_a, v_a) when it
97 // receives an accept RPC to which it replies accept_ok().
98 void log::logaccept(prop_t n, std::string v) {
99 std::ofstream f(name, std::ios::app);
100 f << "accepted " << n.n << " " << n.m << " " << v << "\n";