|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | +#include<string.h> |
| 4 | +#include<unistd.h> |
| 5 | +#include<assert.h> |
| 6 | +#include<time.h> |
| 7 | +#include<signal.h> |
| 8 | +#include<errno.h> |
| 9 | +#include<sys/wait.h> |
| 10 | +#include<sys/time.h> |
| 11 | +#include<stdbool.h> |
| 12 | + |
| 13 | +#include<jansson.h> |
| 14 | + |
| 15 | +#include"raft.h" |
| 16 | +#include"util.h" |
| 17 | + |
| 18 | +staticvoidapplier(void*state,raft_update_tupdate,raft_bool_tsnapshot) { |
| 19 | +json_error_terror; |
| 20 | + |
| 21 | +json_t*patch=json_loadb(update.data,update.len,0,&error); |
| 22 | +if (!patch) { |
| 23 | +shout( |
| 24 | +"error parsing json at position %d: %s\n", |
| 25 | +error.column, |
| 26 | +error.text |
| 27 | +); |
| 28 | +} |
| 29 | + |
| 30 | +if (snapshot) { |
| 31 | +json_object_clear(state); |
| 32 | +} |
| 33 | + |
| 34 | +if (json_object_update(state,patch)) { |
| 35 | +shout("error updating state\n"); |
| 36 | +} |
| 37 | + |
| 38 | +json_decref(patch); |
| 39 | + |
| 40 | +char*encoded=json_dumps(state,JSON_INDENT(4) |JSON_SORT_KEYS); |
| 41 | +if (encoded) { |
| 42 | +debug( |
| 43 | +"applied %s: new state is %s\n", |
| 44 | +snapshot ?"a snapshot" :"an update", |
| 45 | +encoded |
| 46 | +); |
| 47 | +}else { |
| 48 | +shout( |
| 49 | +"applied %s, but new state could not be encoded\n", |
| 50 | +snapshot ?"a snapshot" :"an update" |
| 51 | +); |
| 52 | +} |
| 53 | +free(encoded); |
| 54 | +} |
| 55 | + |
| 56 | +staticraft_update_tsnapshooter(void*state) { |
| 57 | +json_error_terror; |
| 58 | + |
| 59 | +raft_update_tshot; |
| 60 | +shot.data=json_dumps(state,JSON_SORT_KEYS); |
| 61 | +shot.len=strlen(shot.data); |
| 62 | +if (shot.data) { |
| 63 | +debug("snapshot taken: %.*s\n",shot.len,shot.data); |
| 64 | +}else { |
| 65 | +shout("failed to take a snapshot\n"); |
| 66 | +} |
| 67 | + |
| 68 | +returnshot; |
| 69 | +} |
| 70 | + |
| 71 | +staticvoiddie(intsignum) { |
| 72 | +debug("dying\n"); |
| 73 | +exit(signum); |
| 74 | +} |
| 75 | + |
| 76 | +staticvoidusage(char*prog) { |
| 77 | +printf( |
| 78 | +"Usage: %s -i ID -r ID:HOST:PORT [-r ID:HOST:PORT ...] [-l LOGFILE]\n" |
| 79 | +" -l : Run as a daemon and write output to LOGFILE.\n", |
| 80 | +prog |
| 81 | +); |
| 82 | +} |
| 83 | + |
| 84 | +raft_traft; |
| 85 | + |
| 86 | +staticvoidmain_loop(char*host,intport) { |
| 87 | +mstimer_tt; |
| 88 | +mstimer_reset(&t); |
| 89 | + |
| 90 | +// create a UDP socket for raft |
| 91 | +intr=raft_create_udp_socket(raft); |
| 92 | +if (r==NOBODY) { |
| 93 | +die(EXIT_FAILURE); |
| 94 | +} |
| 95 | + |
| 96 | +#defineEMIT_EVERY_MS 1000 |
| 97 | +intemit_ms=0; |
| 98 | +while (true) { |
| 99 | +intms; |
| 100 | +raft_msg_tm; |
| 101 | +intapplied; |
| 102 | + |
| 103 | +ms=mstimer_reset(&t); |
| 104 | + |
| 105 | +raft_tick(raft,ms); |
| 106 | +m=raft_recv_message(raft); |
| 107 | +if (m) { |
| 108 | +raft_handle_message(raft,m); |
| 109 | +} |
| 110 | + |
| 111 | +if (raft_is_leader(raft)) { |
| 112 | +emit_ms+=ms; |
| 113 | +while (emit_ms>EMIT_EVERY_MS) { |
| 114 | +emit_ms-=EMIT_EVERY_MS; |
| 115 | +charbuf[1000]; |
| 116 | +charkey='a'+rand() %5; |
| 117 | +intvalue=rand() %10000; |
| 118 | +sprintf(buf,"{\"key-%c\":%d}",key,value); |
| 119 | +shout("emit update: = %s\n",buf); |
| 120 | +raft_update_tupdate= {strlen(buf),buf,NULL}; |
| 121 | +raft_emit(raft,update); |
| 122 | +} |
| 123 | +} |
| 124 | +} |
| 125 | + |
| 126 | +close(r); |
| 127 | +} |
| 128 | + |
| 129 | +intmain(intargc,char**argv) { |
| 130 | +char*logfilename=NULL; |
| 131 | +booldaemonize= false; |
| 132 | + |
| 133 | +intmyid=NOBODY; |
| 134 | +intid; |
| 135 | +char*host; |
| 136 | +char*str; |
| 137 | +intport; |
| 138 | +intopt; |
| 139 | + |
| 140 | +char*myhost=NULL; |
| 141 | +intmyport; |
| 142 | + |
| 143 | +json_t*state=json_object(); |
| 144 | + |
| 145 | +raft_config_trc; |
| 146 | +rc.peernum_max=64; |
| 147 | +rc.heartbeat_ms=20; |
| 148 | +rc.election_ms_min=150; |
| 149 | +rc.election_ms_max=300; |
| 150 | +rc.log_len=10; |
| 151 | +rc.chunk_len=4; |
| 152 | +rc.msg_len_max=500; |
| 153 | +rc.userdata=state; |
| 154 | +rc.applier=applier; |
| 155 | +rc.snapshooter=snapshooter; |
| 156 | +raft=raft_init(&rc); |
| 157 | + |
| 158 | +intpeernum=0; |
| 159 | +while ((opt=getopt(argc,argv,"hi:r:l:"))!=-1) { |
| 160 | +switch (opt) { |
| 161 | +case'i': |
| 162 | +myid=atoi(optarg); |
| 163 | +break; |
| 164 | +case'r': |
| 165 | +if (myid==NOBODY) { |
| 166 | +usage(argv[0]); |
| 167 | +returnEXIT_FAILURE; |
| 168 | +} |
| 169 | + |
| 170 | +str=strtok(optarg,":"); |
| 171 | +if (str) { |
| 172 | +id=atoi(str); |
| 173 | +}else { |
| 174 | +usage(argv[0]); |
| 175 | +returnEXIT_FAILURE; |
| 176 | +} |
| 177 | + |
| 178 | +host=strtok(NULL,":"); |
| 179 | + |
| 180 | +str=strtok(NULL,":"); |
| 181 | +if (str) { |
| 182 | +port=atoi(str); |
| 183 | +}else { |
| 184 | +usage(argv[0]); |
| 185 | +returnEXIT_FAILURE; |
| 186 | +} |
| 187 | + |
| 188 | +if (!raft_peer_up(raft,id,host,port,id==myid)) { |
| 189 | +usage(argv[0]); |
| 190 | +returnEXIT_FAILURE; |
| 191 | +} |
| 192 | +if (id==myid) { |
| 193 | +myhost=host; |
| 194 | +myport=port; |
| 195 | +} |
| 196 | +peernum++; |
| 197 | +break; |
| 198 | +case'l': |
| 199 | +logfilename=optarg; |
| 200 | +daemonize= true; |
| 201 | +break; |
| 202 | +case'h': |
| 203 | +usage(argv[0]); |
| 204 | +returnEXIT_SUCCESS; |
| 205 | +default: |
| 206 | +usage(argv[0]); |
| 207 | +returnEXIT_FAILURE; |
| 208 | +} |
| 209 | +} |
| 210 | +if (!myhost) { |
| 211 | +usage(argv[0]); |
| 212 | +returnEXIT_FAILURE; |
| 213 | +} |
| 214 | + |
| 215 | +if (logfilename) { |
| 216 | +if (!freopen(logfilename,"a",stdout)) { |
| 217 | +// nowhere to report this failure |
| 218 | +returnEXIT_FAILURE; |
| 219 | +} |
| 220 | +if (!freopen(logfilename,"a",stderr)) { |
| 221 | +// nowhere to report this failure |
| 222 | +returnEXIT_FAILURE; |
| 223 | +} |
| 224 | +} |
| 225 | + |
| 226 | +if (daemonize) { |
| 227 | +if (daemon(true, true)==-1) { |
| 228 | +shout("could not daemonize: %s\n",strerror(errno)); |
| 229 | +returnEXIT_FAILURE; |
| 230 | +} |
| 231 | +} |
| 232 | + |
| 233 | +signal(SIGTERM,die); |
| 234 | +signal(SIGINT,die); |
| 235 | + |
| 236 | +main_loop(myhost,myport); |
| 237 | + |
| 238 | +returnEXIT_SUCCESS; |
| 239 | +} |