Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add support for sync revoke of ticket

When revoke a ticket, we always need wait a long time (may be
DEFAULT_TICKET_EXPIRY sometimes) now. Revoke time can be greatly
shortened with this patch.

Signed-off-by: Guangliang Zhao <gzhao@suse.com>
  • Loading branch information...
commit ed5d41bc7a45c58209793cc835b78b70fa9aeebb 1 parent 783c30b
Guangliang Zhao glzhao authored
87 src/main.c
@@ -571,64 +571,12 @@ static int do_list(void)
571 571 return rv;
572 572 }
573 573
574   -static inline void load_bar(int x, int n, int r, int w)
575   -{
576   - int i;
577   - float ratio;
578   - int c;
579   -
580   - /* Only update r times.*/
581   - if ( x % (n / r) != 0 ) return;
582   -
583   - /* Calculuate the ratio of complete-to-incomplete.*/
584   - ratio = x / (float)n;
585   - c = ratio * w;
586   -
587   - /* Show the percentage complete.*/
588   - printf("%3d%% [", (int)(ratio * 100));
589   -
590   - /* Show the load bar.*/
591   - for (i = 0; i < c; i++)
592   - printf("=");
593   - for (i = c; i < w; i++)
594   - printf(" ");
595   -
596   - printf("]");
597   - printf("\r");
598   - fflush(stdout);
599   -}
600   -
601   -static void counting_down(int total_time)
602   -{
603   - struct winsize size;
604   - int screen_width;
605   - int i;
606   -
607   - ioctl(STDIN_FILENO, TIOCGWINSZ, (char*)&size);
608   - screen_width = size.ws_col / (float)2;
609   -
610   - /* ignore signals */
611   - signal(SIGTERM, SIG_IGN);
612   - signal(SIGINT, SIG_IGN);
613   - signal(SIGHUP, SIG_IGN);
614   -
615   - i = 0;
616   - while (i <= total_time) {
617   - load_bar(i, total_time, total_time, screen_width);
618   - sleep(1);
619   - i++;
620   - }
621   -
622   - log_info("\nCounting Down Over...\n");
623   -}
624 574 static int do_command(cmd_request_t cmd)
625 575 {
626 576 char *buf;
627 577 struct boothc_header *h, reply;
628 578 int buflen;
629 579 int fd, rv;
630   - int expire_time;
631   - int i;
632 580
633 581 buflen = sizeof(struct boothc_header) +
634 582 sizeof(cl.site) + sizeof(cl.ticket);
@@ -701,41 +649,12 @@ static int do_command(cmd_request_t cmd)
701 649 log_info("grant command sent, result will be returned "
702 650 "asynchronously, you can get the result from "
703 651 "the log files");
704   - else if (cmd == BOOTHC_CMD_REVOKE) {
  652 + else if (cmd == BOOTHC_CMD_REVOKE)
705 653 log_info("revoke command sent, result will be returned "
706 654 "asynchronously, you can get the result from "
707   - "the log files after the ticket expiry time.");
708   - i = 0;
709   - /* FIXME: if we access the server then get the actual
710   - * remaining time the waiting will be shorter, for now,
711   - * client is just waiting the expiry time.
712   - */
713   - read_config(cl.configfile);
714   - while (i < booth_conf->ticket_count) {
715   - if (!strncmp(booth_conf->ticket[i].name, cl.ticket,
716   - BOOTH_NAME_LEN)) {
717   - expire_time = booth_conf->ticket[i].expiry;
718   - log_info("You have to wait %d seconds to "
719   - "ensure all timer has expired!",
720   - expire_time);
721   - counting_down(expire_time);
722   - rv = 0;
723   - break;
724   - }
725   - i++;
726   - /* no ticket found in conf file */
727   - if( i == booth_conf->ticket_count ) {
728   - log_error("check your config file, "
729   - "ticket %s not found", cl.ticket);
730   - log_error("your booth's config file may "
731   - "not be the same!");
732   - break;
733   - rv = -1;
734   - }
735   - }
736   - }
  655 + "the log files.");
737 656 else
738   - log_error("internal error when reading reply result!");
  657 + log_error("internal error reading reply result!");
739 658 rv = 0;
740 659 } else if (reply.result == BOOTHC_RLT_SYNC_SUCC) {
741 660 if (cmd == BOOTHC_CMD_GRANT)
16 src/paxos.c
@@ -170,7 +170,7 @@ static int next_ballot_number(struct paxos_instance *pi)
170 170 static void proposer_prepare(struct paxos_instance *pi, int *round)
171 171 {
172 172 struct paxos_msghdr *hdr;
173   - void *msg;
  173 + void *msg, *extra;
174 174 int msglen = sizeof(struct paxos_msghdr) + pi->ps->extralen;
175 175 int ballot;
176 176
@@ -183,6 +183,7 @@ static void proposer_prepare(struct paxos_instance *pi, int *round)
183 183 }
184 184 memset(msg, 0, msglen);
185 185 hdr = msg;
  186 + extra = (char *)msg + sizeof(struct paxos_msghdr);
186 187
187 188 if (*round > pi->round)
188 189 pi->round = *round;
@@ -197,6 +198,10 @@ static void proposer_prepare(struct paxos_instance *pi, int *round)
197 198 hdr->ballot_number = htonl(ballot);
198 199 hdr->extralen = htonl(pi->ps->extralen);
199 200
  201 + if (pi->ps->p_op->prepare &&
  202 + pi->ps->p_op->prepare((pi_handle_t)pi, extra) < 0)
  203 + return;
  204 +
200 205 if (pi->ps->p_op->broadcast)
201 206 pi->ps->p_op->broadcast(msg, msglen);
202 207 else {
@@ -246,8 +251,8 @@ static void proposer_propose(struct paxos_space *ps,
246 251 }
247 252
248 253 extra = (char *)msg + sizeof(struct paxos_msghdr);
249   - if (ps->p_op->prepare) {
250   - if (ps->p_op->prepare(pih, extra))
  254 + if (ps->p_op->is_prepared) {
  255 + if (ps->p_op->is_prepared(pih, extra))
251 256 pi->proposer->open_number++;
252 257 } else
253 258 pi->proposer->open_number++;
@@ -427,8 +432,9 @@ static void acceptor_accepted(struct paxos_space *ps,
427 432 memcpy(value, (char *)msg + sizeof(struct paxos_msghdr) + ps->extralen,
428 433 ps->valuelen);
429 434
430   - if (ps->p_op->accepted)
431   - ps->p_op->accepted(pih, extra, ballot, value);
  435 + if (ps->p_op->accepted
  436 + && ps->p_op->accepted(pih, extra, ballot, value) < 0)
  437 + return;
432 438
433 439 pi->acceptor->state = ACCEPTING;
434 440 to = ntohl(hdr->from);
1  src/paxos.h
@@ -35,6 +35,7 @@ struct paxos_operations {
35 35 int (*catchup) (pi_handle_t handle);
36 36 int (*prepare) (pi_handle_t handle, void *extra);
37 37 int (*promise) (pi_handle_t handle, void *extra);
  38 + int (*is_prepared) (pi_handle_t handle, void *extra);
38 39 int (*propose) (pi_handle_t handle, void *extra,
39 40 int round, void *value);
40 41 int (*accepted) (pi_handle_t handle, void *extra,
518 src/paxos_lease.c
@@ -19,6 +19,8 @@
19 19 #include <stdlib.h>
20 20 #include <string.h>
21 21 #include <errno.h>
  22 +#include <arpa/inet.h>
  23 +#include <assert.h>
22 24 #include "paxos.h"
23 25 #include "paxos_lease.h"
24 26 #include "transport.h"
@@ -30,7 +32,15 @@
30 32 #define PAXOS_LEASE_SPACE "paxoslease"
31 33 #define PLEASE_VALUE_LEN 1024
32 34
  35 +#define OP_START_LEASE 0
  36 +#define OP_STOP_LEASE 1
  37 +
  38 +#define LEASE_STARTED 0
  39 +#define LEASE_STOPPED 1
  40 +
33 41 struct paxos_lease_msghdr {
  42 + int op;
  43 + int clear;
34 44 int leased;
35 45 };
36 46
@@ -38,7 +48,11 @@ struct paxos_lease_value {
38 48 char name[PAXOS_NAME_LEN+1];
39 49 int owner;
40 50 int expiry;
41   - int release;
  51 +};
  52 +
  53 +struct lease_action {
  54 + int op;
  55 + int clear;
42 56 };
43 57
44 58 struct lease_state {
@@ -52,6 +66,7 @@ struct lease_state {
52 66 struct paxos_lease {
53 67 char name[PAXOS_NAME_LEN+1];
54 68 pi_handle_t pih;
  69 + struct lease_action action;
55 70 struct lease_state proposer;
56 71 struct lease_state acceptor;
57 72 int owner;
@@ -72,22 +87,33 @@ static struct paxos_operations *px_op = NULL;
72 87 const struct paxos_lease_operations *p_l_op;
73 88 ps_handle_t ps_handle = 0;
74 89
75   -static void end_paxos_request(pi_handle_t handle, int round, int result)
  90 +static int find_paxos_lease(pi_handle_t handle, struct paxos_lease **pl)
76 91 {
77   - struct paxos_lease *pl;
  92 + struct paxos_lease *lpl;
78 93 int found = 0;
79 94
80   - list_for_each_entry(pl, &lease_head, list) {
81   - if (pl->pih == handle) {
  95 + list_for_each_entry(lpl, &lease_head, list) {
  96 + if (lpl->pih == handle) {
82 97 found = 1;
83 98 break;
84 99 }
85 100 }
86   - if (!found) {
  101 +
  102 + if (!found)
87 103 log_error("cound not found the handle for paxos lease: %ld",
88 104 handle);
  105 + else
  106 + *pl = lpl;
  107 +
  108 + return found;
  109 +}
  110 +
  111 +static void end_paxos_request(pi_handle_t handle, int round, int result)
  112 +{
  113 + struct paxos_lease *pl;
  114 +
  115 + if (!find_paxos_lease(handle, &pl))
89 116 return;
90   - }
91 117
92 118 if (round != pl->proposer.round) {
93 119 log_error("current paxos round is not the proposer round, "
@@ -113,8 +139,6 @@ static void renew_expires(unsigned long data)
113 139 strncpy(value.name, pl->name, PAXOS_NAME_LEN + 1);
114 140 value.owner = myid;
115 141 value.expiry = pl->expiry;
116   - if (pl->release)
117   - value.release = 1;
118 142 paxos_propose(pl->pih, &value, pl->proposer.round);
119 143 }
120 144
@@ -142,7 +166,7 @@ static void lease_expires(unsigned long data)
142 166 del_timer(&pl->acceptor.timer2);
143 167
144 168 if (pl->failover)
145   - paxos_lease_acquire(plh, 1, NULL);
  169 + paxos_lease_acquire(plh, NOT_CLEAR_RELEASE, 1, NULL);
146 170 }
147 171
148 172 static void lease_retry(unsigned long data)
@@ -164,6 +188,13 @@ static void lease_retry(unsigned long data)
164 188 value.owner = myid;
165 189 value.expiry = pl->expiry;
166 190
  191 + pl->action.op = OP_START_LEASE;
  192 + /**
  193 + * We don't know whether the lease_retry after ticket grant
  194 + * is manual or not, so set clear as NOT_CLEAR_RELEASE is
  195 + * the only safe choice.
  196 + **/
  197 + pl->action.clear = NOT_CLEAR_RELEASE;
167 198 round = paxos_round_request(pl->pih, &value, &pl->acceptor.round,
168 199 end_paxos_request);
169 200
@@ -172,6 +203,7 @@ static void lease_retry(unsigned long data)
172 203 }
173 204
174 205 int paxos_lease_acquire(pl_handle_t handle,
  206 + int clear,
175 207 int renew,
176 208 void (*end_acquire) (pl_handle_t handle, int result))
177 209 {
@@ -185,8 +217,9 @@ int paxos_lease_acquire(pl_handle_t handle,
185 217 value.expiry = pl->expiry;
186 218 pl->renew = renew;
187 219 pl->end_lease = end_acquire;
188   - pl->release = 0;
189 220
  221 + pl->action.op = OP_START_LEASE;
  222 + pl->action.clear = clear;
190 223 round = paxos_round_request(pl->pih, &value, &pl->acceptor.round,
191 224 end_paxos_request);
192 225 pl->proposer.timer2 = add_timer(1 * pl->expiry / 10, (unsigned long)pl,
@@ -199,38 +232,54 @@ int paxos_lease_acquire(pl_handle_t handle,
199 232 }
200 233 }
201 234
202   -int paxos_lease_release(pl_handle_t handle)
  235 +int paxos_lease_release(pl_handle_t handle,
  236 + void (*end_release) (pl_handle_t handle, int result))
203 237 {
204 238 struct paxos_lease *pl = (struct paxos_lease *)handle;
  239 + struct paxos_lease_value value;
  240 + int round;
  241 +
  242 + log_debug("enter paxos_lease_release");
  243 + memset(&value, 0, sizeof(struct paxos_lease_value));
  244 + strncpy(value.name, pl->name, PAXOS_NAME_LEN + 1);
  245 + pl->end_lease = end_release;
205 246
206   - pl->release = 1;
  247 + pl->action.op = OP_STOP_LEASE;
  248 + round = paxos_round_request(pl->pih, &value,
  249 + &pl->acceptor.round,
  250 + end_paxos_request);
  251 + if (round > 0)
  252 + pl->proposer.round = round;
207 253
208   - return 0;
  254 + log_debug("exit paxos_lease_release");
  255 + return (round > 0)? 0: -1;
209 256 }
210 257
211 258 static int lease_catchup(pi_handle_t handle)
212 259 {
213 260 struct paxos_lease *pl;
214 261 struct paxos_lease_result plr;
215   - int found = 0;
216 262
217   - list_for_each_entry(pl, &lease_head, list) {
218   - if (pl->pih == handle) {
219   - found = 1;
220   - break;
221   - }
222   - }
223   - if (!found) {
224   - log_error("could not find the lease handle: %ld", handle);
  263 + if (!find_paxos_lease(handle, &pl))
225 264 return -1;
226   - }
227 265
228 266 p_l_op->catchup(pl->name, &pl->owner, &pl->proposer.round, &pl->expires);
229 267 log_debug("catchup result: name: %s, owner: %d, ballot: %d, expires: %llu",
230 268 (char *)pl->name, pl->owner, pl->proposer.round, pl->expires);
231 269
232   - if (pl->owner == -1)
  270 + /**
  271 + * 1. If no site hold the ticket, the relet will be set LEASE_STOPPED.
  272 + * Grant commond will set the relet to LEASE_STARTED first, so we don't
  273 + * need worry about it.
  274 + * 2. If someone hold the ticket, the relet will be set LEASE_STARTED.
  275 + * Because we must make sure that the site can renew, and relet also
  276 + * must be set to LEASE_STARTED.
  277 + **/
  278 + if (-1 == pl->owner) {
  279 + pl->release = LEASE_STOPPED;
233 280 return 0;
  281 + } else
  282 + pl->release = LEASE_STARTED;
234 283
235 284 if (current_time() > pl->expires) {
236 285 plr.owner = pl->owner = -1;
@@ -264,11 +313,33 @@ static int lease_catchup(pi_handle_t handle)
264 313 return 0;
265 314 }
266 315
267   -static int lease_prepared(pi_handle_t handle __attribute__((unused)),
268   - void *header)
  316 +static int lease_prepare(pi_handle_t handle, void *header)
  317 +{
  318 + struct paxos_lease_msghdr *msghdr = header;
  319 + struct paxos_lease *pl;
  320 +
  321 + log_debug("enter lease_prepare");
  322 + if (!find_paxos_lease(handle, &pl))
  323 + return -1;
  324 +
  325 + msghdr->op = htonl(pl->action.op);
  326 + msghdr->clear = htonl(pl->action.clear);
  327 +
  328 + /**
  329 + * Action of paxos_lease is only used to pass args,
  330 + * so clear it now
  331 + **/
  332 + memset(&pl->action, 0, sizeof(struct lease_action));
  333 + log_debug("exit lease_prepare");
  334 + return 0;
  335 +}
  336 +
  337 +static inline int start_lease_is_prepared(pi_handle_t handle __attribute__((unused)),
  338 + void *header)
269 339 {
270 340 struct paxos_lease_msghdr *hdr = header;
271 341
  342 + log_debug("enter start_lease_is_prepared");
272 343 if (hdr->leased) {
273 344 log_debug("already leased");
274 345 return 0;
@@ -278,26 +349,48 @@ static int lease_prepared(pi_handle_t handle __attribute__((unused)),
278 349 }
279 350 }
280 351
281   -static int handle_lease_request(pi_handle_t handle, void *header)
  352 +static inline int stop_lease_is_prepared(pi_handle_t handle __attribute__((unused)),
  353 + void *header __attribute__((unused)))
282 354 {
283   - struct paxos_lease_msghdr *hdr;
284   - struct paxos_lease *pl;
285   - int found = 0;
  355 + log_debug("enter stop_lease_is_prepared");
  356 + return 1;
  357 +}
286 358
287   - hdr = header;
  359 +static int lease_is_prepared(pi_handle_t handle, void *header)
  360 +{
  361 + struct paxos_lease_msghdr *hdr = header;
  362 + int ret = 0;
  363 + int op = ntohl(hdr->op);
  364 +
  365 + log_debug("enter lease_is_prepared");
  366 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  367 + switch (op) {
  368 + case OP_START_LEASE:
  369 + ret = start_lease_is_prepared(handle, header);
  370 + break;
  371 + case OP_STOP_LEASE:
  372 + ret = stop_lease_is_prepared(handle, header);
  373 + break;
  374 + }
  375 +
  376 + log_debug("exit lease_is_prepared");
  377 + return ret;
  378 +}
288 379
289   - list_for_each_entry(pl, &lease_head, list) {
290   - if (pl->pih == handle) {
291   - found = 1;
292   - break;
293   - }
294   - }
295   - if (!found) {
296   - log_error("could not find the lease handle: %ld", handle);
  380 +static int start_lease_promise(pi_handle_t handle, void *header)
  381 +{
  382 + struct paxos_lease_msghdr *hdr = header;
  383 + struct paxos_lease *pl;
  384 + int clear = ntohl(hdr->clear);
  385 +
  386 + log_debug("enter start_lease_promise");
  387 + if (!find_paxos_lease(handle, &pl))
297 388 return -1;
298   - }
299 389
300   - if (pl->owner == -1) {
  390 + if (NOT_CLEAR_RELEASE == clear && LEASE_STOPPED == pl->release) {
  391 + log_debug("could not be leased");
  392 + hdr->leased = 1;
  393 + } else if (-1 == pl->owner) {
301 394 log_debug("has not been leased");
302 395 hdr->leased = 0;
303 396 } else {
@@ -305,26 +398,52 @@ static int handle_lease_request(pi_handle_t handle, void *header)
305 398 hdr->leased = 1;
306 399 }
307 400
  401 + log_debug("exit start_lease_promise");
308 402 return 0;
309 403 }
310 404
311   -static int lease_propose(pi_handle_t handle,
312   - void *extra __attribute__((unused)),
313   - int round, void *value)
  405 +static int stop_lease_promise(pi_handle_t handle,
  406 + void *header __attribute__((unused)))
314 407 {
315 408 struct paxos_lease *pl;
316   - int found = 0;
317 409
318   - list_for_each_entry(pl, &lease_head, list) {
319   - if (pl->pih == handle) {
320   - found = 1;
321   - break;
322   - }
323   - }
324   - if (!found) {
325   - log_error("could not find the lease handle: %ld", handle);
  410 + log_debug("enter stop_lease_promise");
  411 + if (!find_paxos_lease(handle, &pl))
  412 + return -1;
  413 +
  414 + log_debug("exit stop_lease_promise");
  415 + return 0;
  416 +}
  417 +
  418 +static int lease_promise(pi_handle_t handle, void *header)
  419 +{
  420 + struct paxos_lease_msghdr *hdr = header;
  421 + int ret = 0;
  422 + int op = ntohl(hdr->op);
  423 +
  424 + log_debug("enter lease_promise");
  425 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  426 + switch (op) {
  427 + case OP_START_LEASE:
  428 + ret = start_lease_promise(handle, header);
  429 + break;
  430 + case OP_STOP_LEASE:
  431 + ret = stop_lease_promise(handle, header);
  432 + break;
  433 + }
  434 +
  435 + log_debug("exit lease_promise");
  436 + return ret;
  437 +}
  438 +
  439 +static int start_lease_propose(pi_handle_t handle, void *extra,
  440 + int round, void *value)
  441 +{
  442 + struct paxos_lease *pl;
  443 +
  444 + log_debug("enter start_lease_propose");
  445 + if (!find_paxos_lease(handle, &pl))
326 446 return -1;
327   - }
328 447
329 448 if (round != pl->proposer.round) {
330 449 log_error("current round is not the proposer round, "
@@ -356,28 +475,80 @@ static int lease_propose(pi_handle_t handle,
356 475 pl->proposer.expires = current_time() + pl->expiry;
357 476 }
358 477
  478 + log_debug("exit start_lease_propose");
359 479 return 0;
360 480 }
361 481
362   -static int lease_accepted(pi_handle_t handle,
363   - void *extra __attribute__((unused)),
364   - int round, void *value)
  482 +static int stop_lease_propose(pi_handle_t handle,
  483 + void *extra __attribute__((unused)),
  484 + int round,
  485 + void *value)
365 486 {
366 487 struct paxos_lease *pl;
367   - int found = 0;
368 488
369   - list_for_each_entry(pl, &lease_head, list) {
370   - if (pl->pih == handle) {
371   - found = 1;
372   - break;
  489 + log_debug("enter stop_lease_propose");
  490 + if (!find_paxos_lease(handle, &pl))
  491 + return -1;
  492 +
  493 + if (round != pl->proposer.round) {
  494 + log_error("current round is not the proposer round, "
  495 + "current round: %d, proposer round: %d",
  496 + round, pl->proposer.round);
  497 + return -1;
  498 + }
  499 +
  500 + if (!pl->proposer.plv) {
  501 + pl->proposer.plv = malloc(sizeof(struct paxos_lease_value));
  502 + if (!pl->proposer.plv) {
  503 + log_error("could not alloc mem for propsoer plv");
  504 + return -ENOMEM;
373 505 }
374 506 }
375   - if (!found) {
376   - log_error("could not find the lease handle: %ld", handle);
  507 + memcpy(pl->proposer.plv, value, sizeof(struct paxos_lease_value));
  508 +
  509 + log_debug("exit stop_lease_propose");
  510 + return 0;
  511 +}
  512 +
  513 +static int lease_propose(pi_handle_t handle, void *extra,
  514 + int round, void *value)
  515 +{
  516 + struct paxos_lease_msghdr *hdr = extra;
  517 + int ret = 0;
  518 + int op = ntohl(hdr->op);
  519 +
  520 + log_debug("enter lease_propose");
  521 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  522 + switch (op) {
  523 + case OP_START_LEASE:
  524 + ret = start_lease_propose(handle, extra, round, value);
  525 + break;
  526 + case OP_STOP_LEASE:
  527 + ret = stop_lease_propose(handle, extra, round, value);
  528 + break;
  529 + }
  530 +
  531 + log_debug("exit lease_propose");
  532 + return ret;
  533 +}
  534 +
  535 +static int start_lease_accepted(pi_handle_t handle, void *extra,
  536 + int round, void *value)
  537 +{
  538 + struct paxos_lease_msghdr *hdr = extra;
  539 + struct paxos_lease *pl;
  540 +
  541 + log_debug("enter start_lease_accepted");
  542 + if (!find_paxos_lease(handle, &pl))
377 543 return -1;
378   - }
379 544
380 545 pl->acceptor.round = round;
  546 +
  547 + if (NOT_CLEAR_RELEASE == hdr->clear && LEASE_STOPPED == pl->release) {
  548 + log_debug("could not be leased");
  549 + return -1;
  550 + }
  551 +
381 552 if (!pl->acceptor.plv) {
382 553 pl->acceptor.plv = malloc(sizeof(struct paxos_lease_value));
383 554 if (!pl->acceptor.plv) {
@@ -393,27 +564,63 @@ static int lease_accepted(pi_handle_t handle,
393 564 lease_expires);
394 565 pl->acceptor.expires = current_time() + pl->expiry;
395 566
  567 + log_debug("exit start_lease_accepted");
396 568 return 0;
397 569 }
398 570
399   -static int lease_commit(pi_handle_t handle,
400   - void *extra __attribute__((unused)),
401   - int round)
  571 +static int stop_lease_accepted(pi_handle_t handle,
  572 + void *extra __attribute__((unused)),
  573 + int round, void *value)
402 574 {
403 575 struct paxos_lease *pl;
404   - struct paxos_lease_result plr;
405   - int found = 0;
406 576
407   - list_for_each_entry(pl, &lease_head, list) {
408   - if (pl->pih == handle) {
409   - found = 1;
410   - break;
  577 + log_debug("enter stop_lease_accepted");
  578 + if (!find_paxos_lease(handle, &pl))
  579 + return -1;
  580 +
  581 + pl->acceptor.round = round;
  582 + if (!pl->acceptor.plv) {
  583 + pl->acceptor.plv = malloc(sizeof(struct paxos_lease_value));
  584 + if (!pl->acceptor.plv) {
  585 + log_error("could not alloc mem for acceptor plv");
  586 + return -ENOMEM;
411 587 }
412 588 }
413   - if (!found) {
414   - log_error("could not find the lease handle: %ld", handle);
  589 + memcpy(pl->acceptor.plv, value, sizeof(struct paxos_lease_value));
  590 + log_debug("exit stop_lease_accepted");
  591 + return 0;
  592 +}
  593 +
  594 +static int lease_accepted(pi_handle_t handle, void *extra,
  595 + int round, void *value)
  596 +{
  597 + struct paxos_lease_msghdr *hdr = extra;
  598 + int ret = 0;
  599 + int op = ntohl(hdr->op);
  600 +
  601 + log_debug("enter lease_accepted");
  602 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  603 + switch (op) {
  604 + case OP_START_LEASE:
  605 + ret = start_lease_accepted(handle, extra, round, value);
  606 + break;
  607 + case OP_STOP_LEASE:
  608 + ret = stop_lease_accepted(handle, extra, round, value);
  609 + break;
  610 + }
  611 +
  612 + log_debug("exit lease_accepted");
  613 + return ret;
  614 +}
  615 +
  616 +static int start_lease_commit(pi_handle_t handle, void *extra, int round)
  617 +{
  618 + struct paxos_lease *pl;
  619 + struct paxos_lease_result plr;
  620 +
  621 + log_debug("enter start_lease_commit");
  622 + if (!find_paxos_lease(handle, &pl))
415 623 return -1;
416   - }
417 624
418 625 if (round != pl->proposer.round) {
419 626 log_error("current round is not the proposer round, "
@@ -422,9 +629,9 @@ static int lease_commit(pi_handle_t handle,
422 629 return -1;
423 630 }
424 631
  632 + pl->release = LEASE_STARTED;
425 633 pl->owner = pl->proposer.plv->owner;
426 634 pl->expiry = pl->proposer.plv->expiry;
427   - pl->release = pl->proposer.plv->release;
428 635 if (pl->acceptor.timer2 != pl->acceptor.timer1) {
429 636 if (pl->acceptor.timer2)
430 637 del_timer(&pl->acceptor.timer2);
@@ -435,43 +642,79 @@ static int lease_commit(pi_handle_t handle,
435 642 plr.owner = pl->proposer.plv->owner;
436 643 plr.expires = current_time() + pl->proposer.plv->expiry;
437 644 plr.ballot = round;
  645 + p_l_op->notify((pl_handle_t)pl, &plr);
438 646
439   - if (pl->release) {
440   - if (pl->acceptor.timer2)
441   - del_timer(&pl->acceptor.timer2);
442   - if (pl->acceptor.timer1)
443   - del_timer(&pl->acceptor.timer1);
444   - if (pl->proposer.timer2)
445   - del_timer(&pl->proposer.timer2);
446   - if (pl->proposer.timer1)
447   - del_timer(&pl->proposer.timer1);
448   - plr.owner = pl->owner = -1;
449   - plr.expires = 0;
  647 + log_debug("exit start_lease_commit");
  648 + return 0;
  649 +}
  650 +
  651 +static int stop_lease_commit(pi_handle_t handle,
  652 + void *extra __attribute__((unused)),
  653 + int round)
  654 +{
  655 + struct paxos_lease *pl;
  656 + struct paxos_lease_result plr;
  657 +
  658 + log_debug("enter stop_lease_commit");
  659 + if (!find_paxos_lease(handle, &pl))
  660 + return -1;
  661 +
  662 + if (round != pl->proposer.round) {
  663 + log_error("current round is not the proposer round, "
  664 + "current round: %d, proposer round: %d",
  665 + round, pl->proposer.round);
  666 + return -1;
450 667 }
451 668
452   - p_l_op->notify((pl_handle_t)pl, &plr);
  669 + if (pl->acceptor.timer2)
  670 + del_timer(&pl->acceptor.timer2);
  671 + if (pl->acceptor.timer1)
  672 + del_timer(&pl->acceptor.timer1);
  673 + if (pl->proposer.timer2)
  674 + del_timer(&pl->proposer.timer2);
  675 + if (pl->proposer.timer1)
  676 + del_timer(&pl->proposer.timer1);
453 677
  678 + pl->release = LEASE_STOPPED;
  679 +
  680 + strcpy(plr.name, pl->proposer.plv->name);
  681 + plr.owner = pl->owner = -1;
  682 + plr.ballot = round;
  683 + plr.expires = 0;
  684 + p_l_op->notify((pl_handle_t)pl, &plr);
  685 + log_debug("exit stop_lease_commit");
454 686 return 0;
455 687 }
456 688
457   -static int lease_learned(pi_handle_t handle,
458   - void *extra __attribute__((unused)),
459   - int round)
  689 +static int lease_commit(pi_handle_t handle, void *extra, int round)
  690 +{
  691 + struct paxos_lease_msghdr *hdr = extra;
  692 + int ret = 0;
  693 + int op = ntohl(hdr->op);
  694 +
  695 + log_debug("enter lease_commit");
  696 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  697 + switch (op) {
  698 + case OP_START_LEASE:
  699 + ret = start_lease_commit(handle, extra, round);
  700 + break;
  701 + case OP_STOP_LEASE:
  702 + ret = stop_lease_commit(handle, extra, round);
  703 + break;
  704 + }
  705 +
  706 + log_debug("exit lease_commit");
  707 + return ret;
  708 +}
  709 +
  710 +static int start_lease_learned(pi_handle_t handle, void *extra, int round)
460 711 {
461 712 struct paxos_lease *pl;
462 713 struct paxos_lease_result plr;
463   - int found = 0;
464 714
465   - list_for_each_entry(pl, &lease_head, list) {
466   - if (pl->pih == handle) {
467   - found = 1;
468   - break;
469   - }
470   - }
471   - if (!found) {
472   - log_error("could not find the lease handle: %ld", handle);
  715 + log_debug("enter start_lease_learned");
  716 + if (!find_paxos_lease(handle, &pl))
473 717 return -1;
474   - }
475 718
476 719 if (round != pl->acceptor.round) {
477 720 log_error("current round is not the proposer round, "
@@ -480,9 +723,9 @@ static int lease_learned(pi_handle_t handle,
480 723 return -1;
481 724 }
482 725
  726 + pl->release = LEASE_STARTED;
483 727 pl->owner = pl->acceptor.plv->owner;
484 728 pl->expiry = pl->acceptor.plv->expiry;
485   - pl->release = pl->acceptor.plv->release;
486 729 if (pl->acceptor.timer2 != pl->acceptor.timer1) {
487 730 if (pl->acceptor.timer2)
488 731 del_timer(&pl->acceptor.timer2);
@@ -493,21 +736,65 @@ static int lease_learned(pi_handle_t handle,
493 736 plr.owner = pl->acceptor.plv->owner;
494 737 plr.expires = current_time() + pl->acceptor.plv->expiry;
495 738 plr.ballot = round;
  739 + p_l_op->notify((pl_handle_t)pl, &plr);
  740 + log_debug("exit start_lease_learned");
  741 + return 0;
  742 +}
496 743
497   - if (pl->release) {
498   - if (pl->acceptor.timer2)
499   - del_timer(&pl->acceptor.timer2);
500   - if (pl->acceptor.timer1)
501   - del_timer(&pl->acceptor.timer1);
502   - plr.owner = pl->owner = -1;
503   - plr.expires = 0;
  744 +static int stop_lease_learned(pi_handle_t handle,
  745 + void *extra __attribute__((unused)),
  746 + int round)
  747 +{
  748 + struct paxos_lease *pl;
  749 + struct paxos_lease_result plr;
  750 +
  751 + log_debug("enter stop_lease_learned");
  752 + if (!find_paxos_lease(handle, &pl))
  753 + return -1;
  754 +
  755 + if (round != pl->acceptor.round) {
  756 + log_error("current round is not the proposer round, "
  757 + "current round: %d, proposer round: %d",
  758 + round, pl->proposer.round);
  759 + return -1;
504 760 }
505 761
506   - p_l_op->notify((pl_handle_t)pl, &plr);
  762 + if (pl->acceptor.timer2)
  763 + del_timer(&pl->acceptor.timer2);
  764 + if (pl->acceptor.timer1)
  765 + del_timer(&pl->acceptor.timer1);
507 766
  767 + pl->release = LEASE_STOPPED;
  768 + strcpy(plr.name, pl->acceptor.plv->name);
  769 + plr.owner = pl->owner = -1;
  770 + plr.ballot = round;
  771 + plr.expires = 0;
  772 + p_l_op->notify((pl_handle_t)pl, &plr);
  773 + log_debug("exit stop_lease_learned");
508 774 return 0;
509 775 }
510 776
  777 +static int lease_learned(pi_handle_t handle, void *extra, int round)
  778 +{
  779 + struct paxos_lease_msghdr *hdr = extra;
  780 + int ret = 0;
  781 + int op = ntohl(hdr->op);
  782 +
  783 + log_debug("enter lease_learned");
  784 + assert(OP_START_LEASE == op || OP_STOP_LEASE == op);
  785 + switch (op) {
  786 + case OP_START_LEASE:
  787 + ret = start_lease_learned(handle, extra, round);
  788 + break;
  789 + case OP_STOP_LEASE:
  790 + ret = stop_lease_learned(handle, extra, round);
  791 + break;
  792 + }
  793 +
  794 + log_debug("exit lease_learned");
  795 + return ret;
  796 +}
  797 +
511 798 pl_handle_t paxos_lease_init(const void *name,
512 799 unsigned int namelen,
513 800 int expiry,
@@ -540,8 +827,9 @@ pl_handle_t paxos_lease_init(const void *name,
540 827 px_op->send = pl_op->send;
541 828 px_op->broadcast = pl_op->broadcast;
542 829 px_op->catchup = lease_catchup;
543   - px_op->prepare = lease_prepared;
544   - px_op->promise = handle_lease_request;
  830 + px_op->prepare = lease_prepare;
  831 + px_op->is_prepared = lease_is_prepared;
  832 + px_op->promise = lease_promise;
545 833 px_op->propose = lease_propose;
546 834 px_op->accepted = lease_accepted;
547 835 px_op->commit = lease_commit;
7 src/paxos_lease.h
@@ -21,6 +21,9 @@
21 21
22 22 #define PLEASE_NAME_LEN 63
23 23
  24 +#define NOT_CLEAR_RELEASE 0
  25 +#define CLEAR_RELEASE 1
  26 +
24 27 typedef long pl_handle_t;
25 28
26 29 struct paxos_lease_result {
@@ -51,6 +54,7 @@ pl_handle_t paxos_lease_init(const void *name,
51 54 int paxos_lease_on_receive(void *msg, int msglen);
52 55
53 56 int paxos_lease_acquire(pl_handle_t handle,
  57 + int clear,
54 58 int renew,
55 59 void (*end_acquire) (pl_handle_t handle, int result));
56 60 /*
@@ -62,7 +66,8 @@ int paxos_lease_timeout(const void *name);
62 66 */
63 67 int paxos_lease_status_recovery(pl_handle_t handle);
64 68
65   -int paxos_lease_release(pl_handle_t handle);
  69 +int paxos_lease_release(pl_handle_t handle,
  70 + void (*end_release) (pl_handle_t handle, int result));
66 71
67 72 int paxos_lease_exit(pl_handle_t handle);
68 73
65 src/ticket.c
@@ -114,24 +114,59 @@ static int ticket_get_myid(void)
114 114 return booth_transport[booth_conf->proto].get_myid();
115 115 }
116 116
117   -static void end_acquire(pl_handle_t handle, int result)
  117 +static void end_acquire(pl_handle_t handle, int error)
118 118 {
119 119 struct ticket *tk;
120 120 int found = 0;
121 121
122   - if (result == 0) {
123   - list_for_each_entry(tk, &ticket_list, list) {
124   - if (tk->handle == handle) {
125   - found = 1;
126   - break;
127   - }
  122 + log_debug("enter end_acquire");
  123 + list_for_each_entry(tk, &ticket_list, list) {
  124 + if (tk->handle == handle) {
  125 + found = 1;
  126 + break;
128 127 }
129   - if (!found)
130   - log_error("BUG: ticket handle %ld does not exist",
131   - handle);
132   - log_info("ticket %s was granted/reovked successfully (site %d)",
133   - tk->id, ticket_get_myid());
134 128 }
  129 +
  130 + if (!found) {
  131 + log_error("BUG: ticket handle %ld does not exist", handle);
  132 + return;
  133 + }
  134 +
  135 + if (error)
  136 + log_info("ticket %s was granted failed (site %d), error:%s",
  137 + tk->id, ticket_get_myid(), strerror(error));
  138 + else
  139 + log_info("ticket %s was granted successfully (site %d)",
  140 + tk->id, ticket_get_myid());
  141 + log_debug("exit end_acquire");
  142 +}
  143 +
  144 +static void end_release(pl_handle_t handle, int error)
  145 +{
  146 + struct ticket *tk;
  147 + int found = 0;
  148 +
  149 + log_debug("enter end_release");
  150 + list_for_each_entry(tk, &ticket_list, list) {
  151 + if (tk->handle == handle) {
  152 + found = 1;
  153 + break;
  154 + }
  155 + }
  156 +
  157 + if (!found) {
  158 + log_error("BUG: ticket handle %ld does not exist", handle);
  159 + return;
  160 + }
  161 +
  162 + if (error)
  163 + log_info("ticket %s was reovked failed (site %d), error:%s",
  164 + tk->id, ticket_get_myid(), strerror(error));
  165 + else
  166 + log_info("ticket %s was reovked successfully (site %d)",
  167 + tk->id, ticket_get_myid());
  168 +
  169 + log_debug("exit end_release");
135 170 }
136 171
137 172 static int ticket_send(unsigned long id, void *value, int len)
@@ -393,6 +428,7 @@ int grant_ticket(char *ticket)
393 428 break;
394 429 }
395 430 }
  431 +
396 432 if (!found) {
397 433 log_error("ticket %s does not exist", ticket);
398 434 return BOOTHC_RLT_SYNC_FAIL;
@@ -401,7 +437,7 @@ int grant_ticket(char *ticket)
401 437 if (tk->owner == ticket_get_myid())
402 438 return BOOTHC_RLT_SYNC_SUCC;
403 439 else {
404   - paxos_lease_acquire(tk->handle, 1, end_acquire);
  440 + paxos_lease_acquire(tk->handle, CLEAR_RELEASE, 1, end_acquire);
405 441 return BOOTHC_RLT_ASYNC;
406 442 }
407 443 }
@@ -417,6 +453,7 @@ int revoke_ticket(char *ticket)
417 453 break;
418 454 }
419 455 }
  456 +
420 457 if (!found) {
421 458 log_error("ticket %s does not exist", ticket);
422 459 return BOOTHC_RLT_SYNC_FAIL;
@@ -425,7 +462,7 @@ int revoke_ticket(char *ticket)
425 462 if (tk->owner == -1)
426 463 return BOOTHC_RLT_SYNC_SUCC;
427 464 else {
428   - paxos_lease_release(tk->handle);
  465 + paxos_lease_release(tk->handle, end_release);
429 466 return BOOTHC_RLT_ASYNC;
430 467 }
431 468 }

0 comments on commit ed5d41b

Please sign in to comment.
Something went wrong with that request. Please try again.