Skip to content

Commit

Permalink
Add --tsv option
Browse files Browse the repository at this point in the history
  • Loading branch information
willemt committed Jan 8, 2016
1 parent 0c5b7b3 commit 8407278
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 112 deletions.
8 changes: 7 additions & 1 deletion USAGE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
virtraft - test raft

Usage:
virtraft --nodes NODES [-d RATE | -D RATE | -c RATE | -s SEED | -i ITERS | --debug]
virtraft --nodes NODES [-d RATE | -D RATE | -c RATE | -s SEED | -i ITERS | --tsv | --debug]
virtraft --version
virtraft --help

Expand All @@ -12,6 +12,12 @@ Options:
-c --client_rate RATE Rate entries are received from the client 0-100 [default: 100]
-s --seed SEED The simulation's seed [default: 0]
-i --iterations ITERS Number of iterations before the simulation ends [default: -1]
--tsv Output node status tab separated values at exit
-g --debug Show debug logs
-v --version Display version.
-h --help Prints a short usage summary.

Examples:

Output a node status table:
build/virtraft --nodes 3 --iterations 1000 --tsv | column -t
5 changes: 5 additions & 0 deletions deps/raft/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,11 @@ void raft_vote(raft_server_t* me_, raft_node_t* node);
* @param[in] term The new current term */
void raft_set_current_term(raft_server_t* me, const int term);

/** Set the commit idx.
* This should be used to reload persistent state, ie. the commit_idx field.
* @param[in] commit_idx The new commit index. */
void raft_set_commit_idx(raft_server_t* me, int commit_idx);

/** Add an entry to the server's log.
* This should be used to reload persistent state, ie. the commit log.
* @param[in] ety The entry to be appended */
Expand Down
2 changes: 0 additions & 2 deletions deps/raft/raft_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ int raft_apply_entry(raft_server_t* me_);
* @return 0 if unsuccessful */
int raft_append_entry(raft_server_t* me_, raft_entry_t* c);

void raft_set_commit_idx(raft_server_t* me, int commit_idx);

void raft_set_last_applied_idx(raft_server_t* me, int idx);

void raft_set_state(raft_server_t* me_, int state);
Expand Down
62 changes: 50 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ system_t sys;

options_t opts;

static void __print_tsv()
{
int i;

printf("node\t");
printf("state\t");
printf("current_idx\t");
printf("last_log_term\t");
printf("current_term\t");
printf("commit_idx\t");
printf("last_applied_idx\t");
printf("log_count\t");
printf("\n");

for (i = 0; i < sys.n_nodes; i++)
{
raft_server_t* r = sys.nodes[i].raft;

printf("%d:%lx\t", i, (unsigned long)r);
printf("%s\t",
raft_get_state(r) == RAFT_STATE_LEADER ? "leader" :
raft_get_state(r) == RAFT_STATE_CANDIDATE ? "candidate" :
"follower");
printf("%d\t", raft_get_current_idx(r));
printf("%d\t", raft_get_last_log_term(r));
printf("%d\t", raft_get_current_term(r));
printf("%d\t", raft_get_commit_idx(r));
printf("%d\t", raft_get_last_applied_idx(r));
printf("%d\t", raft_get_log_count(r));
printf("\n");
}
}

static void __print_stats()
{
int i;
Expand All @@ -78,17 +111,17 @@ static void __print_stats()
{
raft_server_t* r = sys.nodes[i].raft;

printf("node: %d %lx\n", i, (unsigned long)r);
printf("state: %s\n",
printf("node %d:%lx\n", i, (unsigned long)r);
printf("state %s\n",
raft_get_state(r) == RAFT_STATE_LEADER ? "leader" :
raft_get_state(r) == RAFT_STATE_CANDIDATE ? "candidate" :
"follower");
printf("current_idx: %d\n", raft_get_current_idx(r));
printf("last_log_term: %d\n", raft_get_last_log_term(r));
printf("current_term: %d\n", raft_get_current_term(r));
printf("commit_idx: %d\n", raft_get_commit_idx(r));
printf("last_applied_idx: %d\n", raft_get_last_applied_idx(r));
printf("log_count: %d\n", raft_get_log_count(r));
printf("current_idx %d\n", raft_get_current_idx(r));
printf("last_log_term %d\n", raft_get_last_log_term(r));
printf("current_term %d\n", raft_get_current_term(r));
printf("commit_idx %d\n", raft_get_commit_idx(r));
printf("last_applied_idx %d\n", raft_get_last_applied_idx(r));
printf("log_count %d\n", raft_get_log_count(r));
printf("\n");
}
}
Expand Down Expand Up @@ -135,7 +168,7 @@ static int __raft_applylog(
(unsigned long)other,
ety->id,
other_ety ? other_ety->id : -999);
__int_handler(0);
__print_stats(0);
abort();
}
}
Expand Down Expand Up @@ -365,6 +398,7 @@ static void __server_poll_messages(node_t* me, system_t* sys)
}
}

// FIXME: this is O(n^2)
/** Election Safety
* At most one leader can be elected in a given term. */
static void __ensure_election_safety(system_t* sys)
Expand Down Expand Up @@ -432,6 +466,7 @@ static void __push_entry(system_t* sys)
}
}

// FIXME: this is O(n^2)
/** Leader Completeness
* If a log entry is committed in a given term, then that entry will be present
* in the logs of the leaders for all higher-numbered terms. §3.6 */
Expand Down Expand Up @@ -518,8 +553,6 @@ int main(int argc, char **argv)
sys.n_nodes = atoi(opts.NODES);
sys.nodes = calloc(sys.n_nodes, sizeof(*sys.nodes));

printf("Starting with %d nodes\n", sys.n_nodes);

/* create server for every node */
for (i = 0; i < sys.n_nodes; i++)
__create_node(&sys.nodes[i], i, &sys);
Expand All @@ -546,12 +579,17 @@ int main(int argc, char **argv)
__ensure_election_safety(&sys);
__ensure_log_matching(&sys);
__ensure_leader_completeness(&sys);

/* sleep(1); */

iters++;

if (iters == max_iters)
break;
}

__print_stats();
if (opts.tsv)
__print_tsv();
else
__print_stats();
}
Loading

0 comments on commit 8407278

Please sign in to comment.