1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
|
/*
* Copyright (c) 2017, 2018 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "raft-rpc.h"
#include <stdlib.h>
#include <string.h>
#include "compiler.h"
#include "jsonrpc.h"
#include "ovsdb-error.h"
#include "ovsdb-parser.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/json.h"
#include "openvswitch/vlog.h"
#include "sset.h"
VLOG_DEFINE_THIS_MODULE(raft_rpc);
#define RAFT_RPC(ENUM, NAME) \
static void raft_##NAME##_uninit(struct raft_##NAME *); \
static void raft_##NAME##_clone(struct raft_##NAME *, \
const struct raft_##NAME *); \
static void raft_##NAME##_to_jsonrpc(const struct raft_##NAME *, \
struct json *); \
static void raft_##NAME##_from_jsonrpc(struct ovsdb_parser *, \
struct raft_##NAME *); \
static void raft_format_##NAME(const struct raft_##NAME *, struct ds *);
RAFT_RPC_TYPES
#undef RAFT_RPC
/* raft_rpc_type. */
const char *
raft_rpc_type_to_string(enum raft_rpc_type status)
{
switch (status) {
#define RAFT_RPC(ENUM, NAME) case ENUM: return #NAME;
RAFT_RPC_TYPES
#undef RAFT_RPC
}
return "<unknown>";
}
bool
raft_rpc_type_from_string(const char *s, enum raft_rpc_type *status)
{
#define RAFT_RPC(ENUM, NAME) \
if (!strcmp(s, #NAME)) { \
*status = ENUM; \
return true; \
}
RAFT_RPC_TYPES
#undef RAFT_RPC
return false;
}
/* raft_hello_request. */
static void
raft_hello_request_uninit(struct raft_hello_request *rq)
{
free(rq->address);
}
static void
raft_hello_request_clone(struct raft_hello_request *dst,
const struct raft_hello_request *src)
{
dst->address = nullable_xstrdup(src->address);
}
static void
raft_hello_request_to_jsonrpc(const struct raft_hello_request *rq,
struct json *args)
{
json_object_put_string(args, "address", rq->address);
}
static void
raft_hello_request_from_jsonrpc(struct ovsdb_parser *p,
struct raft_hello_request *rq)
{
rq->address = nullable_xstrdup(raft_parse_required_string(p, "address"));
}
static void
raft_format_hello_request(const struct raft_hello_request *rq,
struct ds *s)
{
ds_put_format(s, " address=\"%s\"", rq->address);
}
/* raft_append_request. */
static void
raft_append_request_uninit(struct raft_append_request *rq)
{
for (size_t i = 0; i < rq->n_entries; i++) {
raft_entry_uninit(&rq->entries[i]);
}
free(rq->entries);
}
static void
raft_append_request_clone(struct raft_append_request *dst,
const struct raft_append_request *src)
{
dst->entries = xmalloc(src->n_entries * sizeof *dst->entries);
for (size_t i = 0; i < src->n_entries; i++) {
raft_entry_clone(&dst->entries[i], &src->entries[i]);
}
}
static void
raft_append_request_to_jsonrpc(const struct raft_append_request *rq,
struct json *args)
{
raft_put_uint64(args, "term", rq->term);
raft_put_uint64(args, "prev_log_index", rq->prev_log_index);
raft_put_uint64(args, "prev_log_term", rq->prev_log_term);
raft_put_uint64(args, "leader_commit", rq->leader_commit);
struct json **entries = xmalloc(rq->n_entries * sizeof *entries);
for (size_t i = 0; i < rq->n_entries; i++) {
entries[i] = raft_entry_to_json(&rq->entries[i]);
}
json_object_put(args, "log", json_array_create(entries, rq->n_entries));
}
static void
raft_append_request_from_jsonrpc(struct ovsdb_parser *p,
struct raft_append_request *rq)
{
rq->term = raft_parse_required_uint64(p, "term");
rq->prev_log_index = raft_parse_required_uint64(p, "prev_log_index");
rq->prev_log_term = raft_parse_required_uint64(p, "prev_log_term");
rq->leader_commit = raft_parse_required_uint64(p, "leader_commit");
const struct json *log = ovsdb_parser_member(p, "log", OP_ARRAY);
if (!log) {
return;
}
const struct json_array *entries = json_array(log);
rq->entries = xmalloc(entries->n * sizeof *rq->entries);
rq->n_entries = 0;
for (size_t i = 0; i < entries->n; i++) {
struct ovsdb_error *error = raft_entry_from_json(entries->elems[i],
&rq->entries[i]);
if (error) {
ovsdb_parser_put_error(p, error);
break;
}
rq->n_entries++;
}
}
static void
raft_format_append_request(const struct raft_append_request *rq,
struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rq->term);
ds_put_format(s, " prev_log_index=%"PRIu64, rq->prev_log_index);
ds_put_format(s, " prev_log_term=%"PRIu64, rq->prev_log_term);
ds_put_format(s, " leader_commit=%"PRIu64, rq->leader_commit);
ds_put_format(s, " n_entries=%u", rq->n_entries);
}
/* raft_append_reply. */
const char *
raft_append_result_to_string(enum raft_append_result result)
{
switch (result) {
case RAFT_APPEND_OK:
return "OK";
case RAFT_APPEND_INCONSISTENCY:
return "inconsistency";
case RAFT_APPEND_IO_ERROR:
return "I/O error";
default:
return NULL;
}
}
bool
raft_append_result_from_string(const char *s, enum raft_append_result *resultp)
{
for (enum raft_append_result result = 0; ; result++) {
const char *s2 = raft_append_result_to_string(result);
if (!s2) {
*resultp = 0;
return false;
} else if (!strcmp(s, s2)) {
*resultp = result;
return true;
}
}
}
static void
raft_append_reply_uninit(struct raft_append_reply *rpy OVS_UNUSED)
{
}
static void
raft_append_reply_clone(struct raft_append_reply *dst OVS_UNUSED,
const struct raft_append_reply *src OVS_UNUSED)
{
}
static void
raft_append_reply_to_jsonrpc(const struct raft_append_reply *rpy,
struct json *args)
{
raft_put_uint64(args, "term", rpy->term);
raft_put_uint64(args, "log_end", rpy->log_end);
raft_put_uint64(args, "prev_log_index", rpy->prev_log_index);
raft_put_uint64(args, "prev_log_term", rpy->prev_log_term);
raft_put_uint64(args, "n_entries", rpy->n_entries);
json_object_put_string(args, "result",
raft_append_result_to_string(rpy->result));
}
static void
raft_append_reply_from_jsonrpc(struct ovsdb_parser *p,
struct raft_append_reply *rpy)
{
rpy->term = raft_parse_required_uint64(p, "term");
rpy->log_end = raft_parse_required_uint64(p, "log_end");
rpy->prev_log_index = raft_parse_required_uint64(p, "prev_log_index");
rpy->prev_log_term = raft_parse_required_uint64(p, "prev_log_term");
rpy->n_entries = raft_parse_required_uint64(p, "n_entries");
const char *result = raft_parse_required_string(p, "result");
if (result && !raft_append_result_from_string(result, &rpy->result)) {
ovsdb_parser_raise_error(p, "unknown result \"%s\"", result);
}
}
static void
raft_format_append_reply(const struct raft_append_reply *rpy, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rpy->term);
ds_put_format(s, " log_end=%"PRIu64, rpy->log_end);
ds_put_format(s, " result=\"%s\"",
raft_append_result_to_string(rpy->result));
}
/* raft_vote_request. */
static void
raft_vote_request_uninit(struct raft_vote_request *rq OVS_UNUSED)
{
}
static void
raft_vote_request_clone(struct raft_vote_request *dst OVS_UNUSED,
const struct raft_vote_request *src OVS_UNUSED)
{
}
static void
raft_vote_request_to_jsonrpc(const struct raft_vote_request *rq,
struct json *args)
{
raft_put_uint64(args, "term", rq->term);
raft_put_uint64(args, "last_log_index", rq->last_log_index);
raft_put_uint64(args, "last_log_term", rq->last_log_term);
if (rq->leadership_transfer) {
json_object_put(args, "leadership_transfer",
json_boolean_create(true));
}
if (rq->is_prevote) {
json_object_put(args, "is_prevote", json_boolean_create(true));
}
}
static void
raft_vote_request_from_jsonrpc(struct ovsdb_parser *p,
struct raft_vote_request *rq)
{
rq->term = raft_parse_required_uint64(p, "term");
rq->last_log_index = raft_parse_required_uint64(p, "last_log_index");
rq->last_log_term = raft_parse_required_uint64(p, "last_log_term");
rq->leadership_transfer
= raft_parse_optional_boolean(p, "leadership_transfer") == 1;
rq->is_prevote
= raft_parse_optional_boolean(p, "is_prevote") == 1;
}
static void
raft_format_vote_request(const struct raft_vote_request *rq, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rq->term);
ds_put_format(s, " last_log_index=%"PRIu64, rq->last_log_index);
ds_put_format(s, " last_log_term=%"PRIu64, rq->last_log_term);
if (rq->leadership_transfer) {
ds_put_cstr(s, " leadership_transfer=true");
}
if (rq->is_prevote) {
ds_put_cstr(s, " is_prevote=true");
}
}
/* raft_vote_reply. */
static void
raft_vote_reply_uninit(struct raft_vote_reply *rpy OVS_UNUSED)
{
}
static void
raft_vote_reply_clone(struct raft_vote_reply *dst OVS_UNUSED,
const struct raft_vote_reply *src OVS_UNUSED)
{
}
static void
raft_vote_reply_to_jsonrpc(const struct raft_vote_reply *rpy,
struct json *args)
{
raft_put_uint64(args, "term", rpy->term);
json_object_put_format(args, "vote", UUID_FMT, UUID_ARGS(&rpy->vote));
if (rpy->is_prevote) {
json_object_put(args, "is_prevote", json_boolean_create(true));
}
}
static void
raft_vote_reply_from_jsonrpc(struct ovsdb_parser *p,
struct raft_vote_reply *rpy)
{
rpy->term = raft_parse_required_uint64(p, "term");
rpy->vote = raft_parse_required_uuid(p, "vote");
rpy->is_prevote = raft_parse_optional_boolean(p, "is_prevote") == 1;
}
static void
raft_format_vote_reply(const struct raft_vote_reply *rpy, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rpy->term);
ds_put_format(s, " vote="SID_FMT, SID_ARGS(&rpy->vote));
if (rpy->is_prevote) {
ds_put_cstr(s, " is_prevote=true");
}
}
/* raft_add_server_request */
static void
raft_add_server_request_uninit(struct raft_add_server_request *rq)
{
free(rq->address);
}
static void
raft_add_server_request_clone(struct raft_add_server_request *dst,
const struct raft_add_server_request *src)
{
dst->address = nullable_xstrdup(src->address);
}
static void
raft_add_server_request_to_jsonrpc(const struct raft_add_server_request *rq,
struct json *args)
{
json_object_put_string(args, "address", rq->address);
}
static void
raft_add_server_request_from_jsonrpc(struct ovsdb_parser *p,
struct raft_add_server_request *rq)
{
rq->address = nullable_xstrdup(raft_parse_required_string(p, "address"));
}
static void
raft_format_add_server_request(const struct raft_add_server_request *rq,
struct ds *s)
{
ds_put_format(s, " address=\"%s\"", rq->address);
}
/* raft_add_server_reply. */
static void
raft_add_server_reply_uninit(struct raft_add_server_reply *rpy)
{
sset_destroy(&rpy->remote_addresses);
}
static void
raft_add_server_reply_clone(struct raft_add_server_reply *dst,
const struct raft_add_server_reply *src)
{
sset_clone(&dst->remote_addresses, &src->remote_addresses);
}
static void
raft_add_server_reply_to_jsonrpc(const struct raft_add_server_reply *rpy,
struct json *args)
{
json_object_put(args, "success", json_boolean_create(rpy->success));
if (!sset_is_empty(&rpy->remote_addresses)) {
json_object_put(args, "remote_addresses",
raft_addresses_to_json(&rpy->remote_addresses));
}
}
static void
raft_add_server_reply_from_jsonrpc(struct ovsdb_parser *p,
struct raft_add_server_reply *rpy)
{
rpy->success = raft_parse_required_boolean(p, "success");
const struct json *json = ovsdb_parser_member(p, "remote_addresses",
OP_ARRAY | OP_OPTIONAL);
if (json) {
ovsdb_parser_put_error(p, raft_addresses_from_json(
json, &rpy->remote_addresses));
} else {
sset_init(&rpy->remote_addresses);
}
}
static void
raft_format_add_server_reply(const struct raft_add_server_reply *rpy,
struct ds *s)
{
ds_put_format(s, " success=%s", rpy->success ? "true" : "false");
if (!sset_is_empty(&rpy->remote_addresses)) {
ds_put_cstr(s, " remote_addresses=[");
const char *address;
int i = 0;
SSET_FOR_EACH (address, &rpy->remote_addresses) {
if (i++ > 0) {
ds_put_cstr(s, ", ");
}
ds_put_cstr(s, address);
}
ds_put_char(s, ']');
}
}
/* raft_remove_server_reply. */
static void
raft_remove_server_reply_uninit(
struct raft_remove_server_reply *rpy OVS_UNUSED)
{
}
static void
raft_remove_server_reply_clone(
struct raft_remove_server_reply *dst OVS_UNUSED,
const struct raft_remove_server_reply *src OVS_UNUSED)
{
}
static void
raft_remove_server_reply_to_jsonrpc(const struct raft_remove_server_reply *rpy,
struct json *args)
{
if (!uuid_is_zero(&rpy->target_sid)) {
json_object_put_format(args, "target_server",
UUID_FMT, UUID_ARGS(&rpy->target_sid));
}
json_object_put(args, "success", json_boolean_create(rpy->success));
}
static void
raft_remove_server_reply_from_jsonrpc(struct ovsdb_parser *p,
struct raft_remove_server_reply *rpy)
{
rpy->success = raft_parse_required_boolean(p, "success");
raft_parse_optional_uuid(p, "target_server", &rpy->target_sid);
}
static void
raft_format_remove_server_reply(const struct raft_remove_server_reply *rpy,
struct ds *s)
{
ds_put_format(s, " success=%s", rpy->success ? "true" : "false");
}
/* raft_install_snapshot_request. */
static void
raft_install_snapshot_request_uninit(
struct raft_install_snapshot_request *rq)
{
json_destroy(rq->last_servers);
json_destroy(rq->data);
}
static void
raft_install_snapshot_request_clone(
struct raft_install_snapshot_request *dst,
const struct raft_install_snapshot_request *src)
{
dst->last_servers = json_clone(src->last_servers);
dst->data = json_clone(src->data);
}
static void
raft_install_snapshot_request_to_jsonrpc(
const struct raft_install_snapshot_request *rq, struct json *args)
{
raft_put_uint64(args, "term", rq->term);
raft_put_uint64(args, "last_index", rq->last_index);
raft_put_uint64(args, "last_term", rq->last_term);
json_object_put(args, "last_servers", json_clone(rq->last_servers));
json_object_put_format(args, "last_eid",
UUID_FMT, UUID_ARGS(&rq->last_eid));
raft_put_uint64(args, "election_timer", rq->election_timer);
json_object_put(args, "data", json_clone(rq->data));
}
static void
raft_install_snapshot_request_from_jsonrpc(
struct ovsdb_parser *p, struct raft_install_snapshot_request *rq)
{
rq->last_servers = json_nullable_clone(
ovsdb_parser_member(p, "last_servers", OP_OBJECT));
ovsdb_parser_put_error(p, raft_servers_validate_json(rq->last_servers));
rq->term = raft_parse_required_uint64(p, "term");
rq->last_index = raft_parse_required_uint64(p, "last_index");
rq->last_term = raft_parse_required_uint64(p, "last_term");
rq->last_eid = raft_parse_required_uuid(p, "last_eid");
/* election_timer is optional in file header, but is always populated in
* install_snapshot_request. */
rq->election_timer = raft_parse_required_uint64(p, "election_timer");
rq->data = json_nullable_clone(
ovsdb_parser_member(p, "data", OP_OBJECT | OP_ARRAY));
}
static void
raft_format_install_snapshot_request(
const struct raft_install_snapshot_request *rq, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rq->term);
ds_put_format(s, " last_index=%"PRIu64, rq->last_index);
ds_put_format(s, " last_term=%"PRIu64, rq->last_term);
ds_put_format(s, " last_eid="UUID_FMT, UUID_ARGS(&rq->last_eid));
ds_put_format(s, " election_timer=%"PRIu64, rq->election_timer);
ds_put_cstr(s, " last_servers=");
struct hmap servers;
struct ovsdb_error *error =
raft_servers_from_json(rq->last_servers, &servers);
if (!error) {
raft_servers_format(&servers, s);
raft_servers_destroy(&servers);
} else {
ds_put_cstr(s, "***error***");
ovsdb_error_destroy(error);
}
}
/* raft_install_snapshot_reply. */
static void
raft_install_snapshot_reply_uninit(
struct raft_install_snapshot_reply *rpy OVS_UNUSED)
{
}
static void
raft_install_snapshot_reply_clone(
struct raft_install_snapshot_reply *dst OVS_UNUSED,
const struct raft_install_snapshot_reply *src OVS_UNUSED)
{
}
static void
raft_install_snapshot_reply_to_jsonrpc(
const struct raft_install_snapshot_reply *rpy, struct json *args)
{
raft_put_uint64(args, "term", rpy->term);
raft_put_uint64(args, "last_index", rpy->last_index);
raft_put_uint64(args, "last_term", rpy->last_term);
}
static void
raft_install_snapshot_reply_from_jsonrpc(
struct ovsdb_parser *p,
struct raft_install_snapshot_reply *rpy)
{
rpy->term = raft_parse_required_uint64(p, "term");
rpy->last_index = raft_parse_required_uint64(p, "last_index");
rpy->last_term = raft_parse_required_uint64(p, "last_term");
}
static void
raft_format_install_snapshot_reply(
const struct raft_install_snapshot_reply *rpy, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rpy->term);
ds_put_format(s, " last_index=%"PRIu64, rpy->last_index);
ds_put_format(s, " last_term=%"PRIu64, rpy->last_term);
}
/* raft_remove_server_request. */
static void
raft_remove_server_request_uninit(
struct raft_remove_server_request *rq OVS_UNUSED)
{
}
static void
raft_remove_server_request_clone(
struct raft_remove_server_request *dst OVS_UNUSED,
const struct raft_remove_server_request *src OVS_UNUSED)
{
}
static void
raft_remove_server_request_to_jsonrpc(
const struct raft_remove_server_request *rq, struct json *args)
{
json_object_put_format(args, "server_id", UUID_FMT, UUID_ARGS(&rq->sid));
}
static void
raft_remove_server_request_from_jsonrpc(struct ovsdb_parser *p,
struct raft_remove_server_request *rq)
{
rq->sid = raft_parse_required_uuid(p, "server_id");
}
static void
raft_format_remove_server_request(const struct raft_remove_server_request *rq,
struct ds *s)
{
ds_put_format(s, " server="SID_FMT, SID_ARGS(&rq->sid));
}
/* raft_become_leader. */
static void
raft_become_leader_uninit(struct raft_become_leader *rpc OVS_UNUSED)
{
}
static void
raft_become_leader_clone(struct raft_become_leader *dst OVS_UNUSED,
const struct raft_become_leader *src OVS_UNUSED)
{
}
static void
raft_become_leader_to_jsonrpc(const struct raft_become_leader *rpc,
struct json *args)
{
raft_put_uint64(args, "term", rpc->term);
}
static void
raft_become_leader_from_jsonrpc(struct ovsdb_parser *p,
struct raft_become_leader *rpc)
{
rpc->term = raft_parse_required_uint64(p, "term");
}
static void
raft_format_become_leader(const struct raft_become_leader *rq, struct ds *s)
{
ds_put_format(s, " term=%"PRIu64, rq->term);
}
/* raft_execute_command_request. */
static void
raft_execute_command_request_uninit(
struct raft_execute_command_request *rq)
{
json_destroy(rq->data);
}
static void
raft_execute_command_request_clone(
struct raft_execute_command_request *dst,
const struct raft_execute_command_request *src)
{
dst->data = json_clone(src->data);
}
static void
raft_execute_command_request_to_jsonrpc(
const struct raft_execute_command_request *rq, struct json *args)
{
json_object_put(args, "data", json_clone(rq->data));
json_object_put_format(args, "prereq", UUID_FMT, UUID_ARGS(&rq->prereq));
json_object_put_format(args, "result", UUID_FMT, UUID_ARGS(&rq->result));
}
static void
raft_execute_command_request_from_jsonrpc(
struct ovsdb_parser *p, struct raft_execute_command_request *rq)
{
rq->data = json_nullable_clone(ovsdb_parser_member(p, "data",
OP_OBJECT | OP_ARRAY));
rq->prereq = raft_parse_required_uuid(p, "prereq");
rq->result = raft_parse_required_uuid(p, "result");
}
static void
raft_format_execute_command_request(
const struct raft_execute_command_request *rq, struct ds *s)
{
ds_put_format(s, " prereq="UUID_FMT, UUID_ARGS(&rq->prereq));
ds_put_format(s, " result="UUID_FMT, UUID_ARGS(&rq->result));
ds_put_format(s, " data=");
json_to_ds(rq->data, JSSF_SORT, s);
}
/* raft_execute_command_reply. */
static void
raft_execute_command_reply_uninit(
struct raft_execute_command_reply *rpy OVS_UNUSED)
{
}
static void
raft_execute_command_reply_clone(
struct raft_execute_command_reply *dst OVS_UNUSED,
const struct raft_execute_command_reply *src OVS_UNUSED)
{
}
static void
raft_execute_command_reply_to_jsonrpc(
const struct raft_execute_command_reply *rpy, struct json *args)
{
json_object_put_format(args, "result", UUID_FMT, UUID_ARGS(&rpy->result));
json_object_put_string(args, "status",
raft_command_status_to_string(rpy->status));
if (rpy->commit_index) {
raft_put_uint64(args, "commit_index", rpy->commit_index);
}
}
static void
raft_execute_command_reply_from_jsonrpc(
struct ovsdb_parser *p, struct raft_execute_command_reply *rpy)
{
rpy->result = raft_parse_required_uuid(p, "result");
const char *status = raft_parse_required_string(p, "status");
if (status && !raft_command_status_from_string(status, &rpy->status)) {
ovsdb_parser_raise_error(p, "unknown status \"%s\"", status);
}
rpy->commit_index = raft_parse_optional_uint64(p, "commit_index");
}
static void
raft_format_execute_command_reply(
const struct raft_execute_command_reply *rpy, struct ds *s)
{
ds_put_format(s, " result="UUID_FMT, UUID_ARGS(&rpy->result));
ds_put_format(s, " status=\"%s\"",
raft_command_status_to_string(rpy->status));
if (rpy->commit_index) {
ds_put_format(s, " commit_index=%"PRIu64, rpy->commit_index);
}
}
void
raft_rpc_uninit(union raft_rpc *rpc)
{
if (rpc) {
free(rpc->common.comment);
switch (rpc->type) {
#define RAFT_RPC(ENUM, NAME) \
case ENUM: \
raft_##NAME##_uninit(&rpc->NAME); \
break;
RAFT_RPC_TYPES
#undef RAFT_RPC
}
}
}
union raft_rpc *
raft_rpc_clone(const union raft_rpc *src)
{
union raft_rpc *dst = xmemdup(src, sizeof *src);
dst->common.comment = nullable_xstrdup(src->common.comment);
switch (src->type) {
#define RAFT_RPC(ENUM, NAME) \
case ENUM: \
raft_##NAME##_clone(&dst->NAME, &src->NAME); \
break;
RAFT_RPC_TYPES
#undef RAFT_RPC
}
return dst;
}
/* Returns 'rpc' converted to a jsonrpc_msg. The caller must eventually free
* the returned message.
*
* 'rpc->common.sid' should be the destination server ID; it is omitted if
* all-zeros. 'sid' is the source. 'cid' should be the cluster ID; it is
* omitted if all-zeros. */
struct jsonrpc_msg *
raft_rpc_to_jsonrpc(const struct uuid *cid,
const struct uuid *sid,
const union raft_rpc *rpc)
{
struct json *args = json_object_create();
if (!uuid_is_zero(cid)) {
json_object_put_format(args, "cluster", UUID_FMT, UUID_ARGS(cid));
}
if (!uuid_is_zero(&rpc->common.sid)) {
json_object_put_format(args, "to", UUID_FMT,
UUID_ARGS(&rpc->common.sid));
}
json_object_put_format(args, "from", UUID_FMT, UUID_ARGS(sid));
if (rpc->common.comment) {
json_object_put_string(args, "comment", rpc->common.comment);
}
switch (rpc->type) {
#define RAFT_RPC(ENUM, NAME) \
case ENUM: \
raft_##NAME##_to_jsonrpc(&rpc->NAME, args); \
break;
RAFT_RPC_TYPES
#undef RAFT_RPC
default:
OVS_NOT_REACHED();
}
return jsonrpc_create_notify(raft_rpc_type_to_string(rpc->type),
json_array_create_1(args));
}
/* Parses 'msg' as a Raft message directed to 'sid' and initializes 'rpc'
* appropriately. On success, returns NULL and the caller owns the contents of
* 'rpc' and must eventually uninitialize it with raft_rpc_uninit(). On
* failure, returns an error that the caller must eventually free.
*
* 'cidp' must point to the Raft cluster's ID. If the cluster ID isn't yet
* known, then '*cidp' must be UUID_ZERO and this function will attempt to
* initialize it based on 'msg'. */
struct ovsdb_error * OVS_WARN_UNUSED_RESULT
raft_rpc_from_jsonrpc(struct uuid *cidp, const struct uuid *sid,
const struct jsonrpc_msg *msg, union raft_rpc *rpc)
{
memset(rpc, 0, sizeof *rpc);
if (msg->type != JSONRPC_NOTIFY) {
return ovsdb_error(NULL, "expecting notify RPC but received %s",
jsonrpc_msg_type_to_string(msg->type));
}
if (!raft_rpc_type_from_string(msg->method, &rpc->type)) {
return ovsdb_error(NULL, "unknown method %s", msg->method);
}
if (json_array(msg->params)->n != 1) {
return ovsdb_error(NULL,
"%s RPC has %"PRIuSIZE" parameters (expected 1)",
msg->method, json_array(msg->params)->n);
}
struct ovsdb_parser p;
ovsdb_parser_init(&p, json_array(msg->params)->elems[0],
"raft %s RPC", msg->method);
bool is_hello = rpc->type == RAFT_RPC_HELLO_REQUEST;
bool is_add = rpc->type == RAFT_RPC_ADD_SERVER_REQUEST;
struct uuid cid;
if (raft_parse_uuid(&p, "cluster", is_add, &cid)
&& !uuid_equals(&cid, cidp)) {
if (uuid_is_zero(cidp)) {
*cidp = cid;
VLOG_INFO("learned cluster ID "CID_FMT, CID_ARGS(&cid));
} else {
ovsdb_parser_raise_error(&p, "wrong cluster "CID_FMT" "
"(expected "CID_FMT")",
CID_ARGS(&cid), CID_ARGS(cidp));
}
}
struct uuid to_sid;
if (raft_parse_uuid(&p, "to", is_add || is_hello, &to_sid)
&& !uuid_equals(&to_sid, sid)) {
ovsdb_parser_raise_error(&p, "misrouted message (addressed to "
SID_FMT" but we're "SID_FMT")",
SID_ARGS(&to_sid), SID_ARGS(sid));
}
rpc->common.sid = raft_parse_required_uuid(&p, "from");
rpc->common.comment = nullable_xstrdup(
raft_parse_optional_string(&p, "comment"));
switch (rpc->type) {
#define RAFT_RPC(ENUM, NAME) \
case ENUM: \
raft_##NAME##_from_jsonrpc(&p, &rpc->NAME); \
break;
RAFT_RPC_TYPES
#undef RAFT_RPC
default:
OVS_NOT_REACHED();
}
struct ovsdb_error *error = ovsdb_parser_finish(&p);
if (error) {
raft_rpc_uninit(rpc);
}
return error;
}
/* Appends a formatted representation of 'rpc' to 's'.
*
* Does not include the RPC's server ID in the formatted representation, since
* the caller usually has more context that allows for a more human friendly
* name. */
void
raft_rpc_format(const union raft_rpc *rpc, struct ds *s)
{
ds_put_cstr(s, raft_rpc_type_to_string(rpc->type));
if (rpc->common.comment) {
ds_put_format(s, " \"%s\"", rpc->common.comment);
}
ds_put_char(s, ':');
switch (rpc->type) {
#define RAFT_RPC(ENUM, NAME) \
case ENUM: \
raft_format_##NAME(&rpc->NAME, s); \
break;
RAFT_RPC_TYPES
#undef RAFT_RPC
default:
OVS_NOT_REACHED();
}
}
uint64_t
raft_rpc_get_term(const union raft_rpc *rpc)
{
switch (rpc->type) {
case RAFT_RPC_HELLO_REQUEST:
case RAFT_RPC_ADD_SERVER_REQUEST:
case RAFT_RPC_ADD_SERVER_REPLY:
case RAFT_RPC_REMOVE_SERVER_REQUEST:
case RAFT_RPC_REMOVE_SERVER_REPLY:
case RAFT_RPC_EXECUTE_COMMAND_REQUEST:
case RAFT_RPC_EXECUTE_COMMAND_REPLY:
return 0;
case RAFT_RPC_APPEND_REQUEST:
return rpc->append_request.term;
case RAFT_RPC_APPEND_REPLY:
return rpc->append_reply.term;
case RAFT_RPC_VOTE_REQUEST:
return rpc->vote_request.term;
case RAFT_RPC_VOTE_REPLY:
return rpc->vote_reply.term;
case RAFT_RPC_INSTALL_SNAPSHOT_REQUEST:
return rpc->install_snapshot_request.term;
case RAFT_RPC_INSTALL_SNAPSHOT_REPLY:
return rpc->install_snapshot_reply.term;
case RAFT_RPC_BECOME_LEADER:
return rpc->become_leader.term;
default:
OVS_NOT_REACHED();
}
}
const struct uuid *
raft_rpc_get_vote(const union raft_rpc *rpc)
{
switch (rpc->type) {
case RAFT_RPC_HELLO_REQUEST:
case RAFT_RPC_ADD_SERVER_REQUEST:
case RAFT_RPC_ADD_SERVER_REPLY:
case RAFT_RPC_REMOVE_SERVER_REQUEST:
case RAFT_RPC_REMOVE_SERVER_REPLY:
case RAFT_RPC_EXECUTE_COMMAND_REQUEST:
case RAFT_RPC_EXECUTE_COMMAND_REPLY:
case RAFT_RPC_APPEND_REQUEST:
case RAFT_RPC_APPEND_REPLY:
case RAFT_RPC_VOTE_REQUEST:
case RAFT_RPC_INSTALL_SNAPSHOT_REQUEST:
case RAFT_RPC_INSTALL_SNAPSHOT_REPLY:
case RAFT_RPC_BECOME_LEADER:
return NULL;
case RAFT_RPC_VOTE_REPLY: {
const struct raft_vote_reply *rpy = raft_vote_reply_cast(rpc);
return rpy->is_prevote ? NULL : &rpy->vote;
}
default:
OVS_NOT_REACHED();
}
}
/* Returns the minimum log index that must be synced to disk if 'rpc' is to be
* sent. (This is generally the biggest log index in the message but some
* messages, e.g. RAFT_RPC_APPEND_REQUEST, don't need their entries synced.) */
uint64_t
raft_rpc_get_min_sync_index(const union raft_rpc *rpc)
{
switch (rpc->type) {
case RAFT_RPC_HELLO_REQUEST:
case RAFT_RPC_ADD_SERVER_REQUEST:
case RAFT_RPC_ADD_SERVER_REPLY:
case RAFT_RPC_REMOVE_SERVER_REQUEST:
case RAFT_RPC_REMOVE_SERVER_REPLY:
case RAFT_RPC_EXECUTE_COMMAND_REQUEST:
case RAFT_RPC_EXECUTE_COMMAND_REPLY:
case RAFT_RPC_APPEND_REQUEST:
case RAFT_RPC_BECOME_LEADER:
case RAFT_RPC_VOTE_REPLY:
return 0;
case RAFT_RPC_APPEND_REPLY:
return raft_append_reply_cast(rpc)->log_end - 1;
case RAFT_RPC_VOTE_REQUEST:
return raft_vote_request_cast(rpc)->last_log_index;
case RAFT_RPC_INSTALL_SNAPSHOT_REQUEST:
return raft_install_snapshot_request_cast(rpc)->last_index;
case RAFT_RPC_INSTALL_SNAPSHOT_REPLY:
/* This will need to change if install_snapshot_reply becomes able to
* report an error */
return raft_install_snapshot_reply_cast(rpc)->last_index;
default:
OVS_NOT_REACHED();
}
}
|