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 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920
|
/*
* ccm.c: Consensus Cluster Service Program
*
* Copyright (c) International Business Machines Corp., 2002
* Author: Ram Pai (linuxram@us.ibm.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <lha_internal.h>
#include <ccm.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <clplumbing/cl_signal.h>
#include <clplumbing/coredumps.h>
#include "ccmmsg.h"
#include "ccmmisc.h"
/* PROTOTYPE */
static void
ccm_reset_all_join_request(ccm_info_t* info);
static void report_reset(void);
static int ccm_already_joined(ccm_info_t *);
static void ccm_memcomp_reset(ccm_info_t *);
/* For enhanced membership service */
static void append_change_msg(ccm_info_t *info,const char *node);
static int received_all_change_msg(ccm_info_t *info);
static int is_expected_change_msg(ccm_info_t *info, const char *node,
enum change_event_type);
static void add_change_msg(ccm_info_t *info, const char *node,
const char *orig, enum change_event_type);
static void reset_change_info(ccm_info_t *info);
static void send_mem_list_to_all(ll_cluster_t *hb, ccm_info_t *info,
char *cookie);
static void ccm_fill_update_table(ccm_info_t *info,
ccm_update_t *update_table, const void *uptime_list);
static void dump_mbrs(ccm_info_t *info);
static longclock_t change_time;
static gboolean gl_membership_converged = FALSE;
const char state_strings[12][64]={
"CCM_STATE_NONE",
"CCM_STATE_VERSION_REQUEST",
"CCM_STATE_JOINING",
"CCM_STATE_SENT_MEMLISTREQ",
"CCM_STATE_MEMLIST_RES",
"CCM_STATE_JOINED",
"CCM_STATE_WAIT_FOR_MEM_LIST",
"CCM_STATE_WAIT_FOR_CHANGE",
"CCM_STATE_NEW_NODE_WAIT_FOR_MEM_LIST",
"CCM_STATE_END"
};
const char*
state2string(int state){
if (state > CCM_STATE_END){
return "INVALID STATE";
}
return state_strings[state];
}
static int
string2state(const char* state_str)
{
int i;
if (state_str == NULL){
ccm_log(LOG_ERR, "%s: state_str is NULL", __FUNCTION__);
return -1;
}
for (i = 0 ; i < DIMOF(state_strings); i++){
if (strncmp(state_strings[i], state_str, 64) == 0){
return i;
}
}
ccm_log(LOG_ERR, "%s: Cannot find a match for string %s",
__FUNCTION__, state_str);
return -1;
}
static void
ccm_set_state(ccm_info_t* info, int istate,const struct ha_msg* msg)
{
int oldstate = info->state;
info->state = (istate);
if((istate)==CCM_STATE_JOINING){
client_influx();
}
if (istate == CCM_STATE_JOINED){
gl_membership_converged =TRUE;
}
if (llm_get_myindex(CCM_GET_LLM(info)) == info->ccm_cluster_leader
&& CCM_STATE_JOINED == istate) {
info->has_quorum = ccm_calculate_quorum(info);
}
else {
ccm_stop_query_quorum ();
}
ccm_debug(LOG_DEBUG,"node state %s -> %s"
, state2string(oldstate),state2string(istate));
}
static void
change_time_init(void)
{
change_time = ccm_get_time();
}
static int
change_timeout(unsigned long timeout)
{
return(ccm_timeout(change_time, ccm_get_time(), timeout));
}
static longclock_t mem_list_time;
static void
mem_list_time_init(void)
{
mem_list_time = ccm_get_time();
}
static int
mem_list_timeout(unsigned long timeout)
{
return(ccm_timeout(mem_list_time, ccm_get_time(), timeout));
}
static longclock_t new_node_mem_list_time;
static void new_node_mem_list_time_init(void)
{
new_node_mem_list_time = ccm_get_time();
}
static int new_node_mem_list_timeout(unsigned long timeout)
{
return(ccm_timeout(new_node_mem_list_time, ccm_get_time(), timeout));
}
#define CCM_GET_MYNODE_ID(info) \
info->llm.nodes[info->llm.myindex].nodename
#define CCM_GET_CL_NODEID(info) \
info->llm.nodes[CCM_GET_CL(info)].nodename
#define CCM_GET_RECEIVED_CHANGE_MSG(info, node) \
llm_get_change(CCM_GET_LLM(info),llm_get_index(&info->llm, node))
#define CCM_SET_RECEIVED_CHANGE_MSG(info, node, value) \
llm_set_change(CCM_GET_LLM(info), llm_get_index(&info->llm, node), value)
/*
////////////////////////////////////////////////////////////////
// BEGIN OF Functions associated with CCM token types that are
// communicated accross nodes and their values.
////////////////////////////////////////////////////////////////
*/
static void ccm_state_wait_for_mem_list(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info);
static void ccm_state_new_node_wait_for_mem_list(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info);
/* END OF TYPE_STR datastructure and associated functions */
/* */
/* timeout configuration function */
/* */
static void
ccm_configure_timeout(ll_cluster_t *hb, ccm_info_t *info)
{
long keepalive = hb->llc_ops->get_keepalive(hb);
ccm_debug2(LOG_DEBUG, "%s: keepalive=%ld", __FUNCTION__, keepalive);
CCM_TMOUT_SET_U(info, 5*keepalive);
CCM_TMOUT_SET_LU(info, 30*keepalive);
CCM_TMOUT_SET_VRS(info, 9*keepalive);
CCM_TMOUT_SET_ITF(info, 18*keepalive);
CCM_TMOUT_SET_IFF(info, 12*keepalive);
CCM_TMOUT_SET_FL(info, CCM_TMOUT_GET_ITF(info)+5);
}
/* */
/* timeout_msg_create: */
/* fake up a timeout message, which is in the */
/* same format as the other messages that are */
/* communicated across the nodes. */
/* */
#ifdef TIMEOUT_MSG_FUNCTIONS_NEEDED
/* */
/* timeout_msg_done: */
/* done with the processing of this message. */
static void
timeout_msg_done(void)
{
/* nothing to do. */
return;
}
/* */
/* timeout_msg_del: */
/* delete the given timeout message. */
/* nobody calls this function. */
/* someday somebody will call it :) */
static void
timeout_msg_del(void)
{
ha_msg_del(timeout_msg);
timeout_msg = NULL;
}
#endif
/* */
/* These are the function that keep track of number of time a version */
/* response message has been dropped. These function are consulted by */
/* the CCM algorithm to determine if a version response message has */
/* to be dropped or not. */
/* */
static int respdrop=0;
#define MAXDROP 3
static int
resp_can_i_drop(void)
{
if (respdrop >= MAXDROP){
return FALSE;
}
return TRUE;
}
static void
resp_dropped(void)
{
respdrop++;
}
static void
resp_reset(void)
{
respdrop=0;
}
/* */
/* End of response processing messages. */
/* */
/* */
/* BEGIN OF functions that track the time since a connectivity reply has */
/* been sent to the leader. */
/* */
static longclock_t finallist_time;
static void
finallist_init(void)
{
finallist_time = ccm_get_time();
}
static void
finallist_reset(void)
{
finallist_time = 0;
}
static int
finallist_timeout(unsigned long timeout)
{
return(ccm_timeout(finallist_time, ccm_get_time(), timeout));
}
/* */
/* END OF functions that track the time since a connectivity reply has */
/* been sent to the leader. */
/* */
/* Reset all the datastructures. Go to a state which is equivalent */
/* to a state when the node is just about to join a cluster. */
void
ccm_reset(ccm_info_t *info)
{
if(ccm_already_joined(info)){
client_evicted();
}
ccm_mem_reset(info);
ccm_memcomp_reset(info);
CCM_SET_ACTIVEPROTO(info, CCM_VER_NONE);
CCM_SET_COOKIE(info,"");
CCM_SET_MAJORTRANS(info,0);
CCM_SET_MINORTRANS(info,0);
CCM_SET_CL(info,-1);
CCM_SET_JOINED_TRANSITION(info, 0);
ccm_set_state(info, CCM_STATE_NONE, NULL);
info->has_quorum = -1;
update_reset(CCM_GET_UPDATETABLE(info));
ccm_reset_all_join_request(info);
version_reset(CCM_GET_VERSION(info));
finallist_reset();
leave_reset();
report_reset();
}
static void
ccm_init(ccm_info_t *info)
{
update_init(CCM_GET_UPDATETABLE(info));
ccm_reset_all_join_request(info);
CCM_INIT_MAXTRANS(info);
leave_init();
(void)timeout_msg_init(info);
ccm_reset(info);
}
/*
* BEGIN OF ROUTINES THAT REPORT THE MEMBERSHIP TO CLIENTS.
*/
static void
report_reset(void)
{
return;
}
/* */
/* print and report the cluster membership to clients. */
/* */
static void
report_mbrs(ccm_info_t *info)
{
int i;
const char *nodename;
static struct born_s {
int index;
int bornon;
} bornon[MAXNODE];/*avoid making it a
stack variable*/
if(ccm_get_memcount(info)==1){
bornon[0].index = CCM_GET_MEMINDEX(info,0);
bornon[0].bornon = CCM_GET_MAJORTRANS(info);
} else for(i=0; i < ccm_get_memcount(info); i++){
bornon[i].index = CCM_GET_MEMINDEX(info,i);
bornon[i].bornon = update_get_uptime(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
CCM_GET_MEMINDEX(info,i));
if(bornon[i].bornon==0)
bornon[i].bornon=CCM_GET_MAJORTRANS(info);
assert(bornon[i].bornon!=-1);
}
ccm_debug2(LOG_DEBUG,"\t\t the following are the members "
"of the group of transition=%d",
CCM_GET_MAJORTRANS(info));
for (i=0 ; i < ccm_get_memcount(info); i++) {
nodename = llm_get_nodename(CCM_GET_LLM(info),
CCM_GET_MEMINDEX(info,i));
ccm_debug2(LOG_DEBUG,"\t\tnodename=%s bornon=%d", nodename,
bornon[i].bornon);
}
/*
* report to clients, the new membership
*/
dump_mbrs(info);
client_new_mbrship(info,
bornon);
return;
}
/* */
/* generate a random cookie. */
/* NOTE: cookie is a mechanism of seperating out the contexts */
/* of messages of partially partitioned clusters. */
/* For example, consider a case where node A is physically */
/* in the partition X and partition Y, and but has joined */
/* membership in partition X. It will end up getting ccm protocol */
/* messages sent by members in both the partitions. In order to */
/* seperate out messages belonging to individual partition, a */
/* random string is used as a identifier by each partition to */
/* identify its messages. In the above case A will get message */
/* from both the partitions but only listens to messages from */
/* partition X and drops messages from partition Y. */
/* */
static char *
ccm_generate_random_cookie(void)
{
char *cookie;
int i;
struct timeval tmp;
cookie = g_malloc(COOKIESIZE*sizeof(char));
/* g_malloc never returns NULL: assert(cookie); */
/* seed the random with a random value */
gettimeofday(&tmp, NULL);
srandom((unsigned int)tmp.tv_usec);
for ( i = 0 ; i < COOKIESIZE-1; i++ ) {
cookie[i] = random()%(127-'!')+'!';
}
cookie[i] = '\0';
return cookie;
}
static void
ccm_free_random_cookie(char *cookie)
{
assert(cookie && *cookie);
g_free(cookie);
}
/* BEGIN OF FUNCTIONS that keep track of connectivity information */
/* conveyed by individual members of the cluster. These functions */
/* are used by only the cluster leader. Ultimately these connectivity */
/* information is used by the cluster to extract out the members */
/* of the cluster that have total connectivity. */
static int
ccm_memcomp_cmpr(gconstpointer a, gconstpointer b)
{
return(*((const uint32_t *)a)-*((const uint32_t *)b));
}
static void
ccm_memcomp_free(gpointer data, gpointer userdata)
{
if(data) {
g_free(data);
}
return;
}
static void
ccm_memcomp_note(ccm_info_t *info, const char *orig,
uint32_t maxtrans, const char *memlist)
{
int index;
char *bitmap = NULL;
uint32_t *ptr;
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
bitmap_create(&bitmap, MAXNODE);
if (bitmap == NULL){
ccm_log(LOG_ERR, "bitmap creatation failed");
return;
}
/* find the index of the originator */
index = llm_get_index(CCM_GET_LLM(info), orig);
/* convert the memlist into a bit map and feed it to the graph */
ccm_str2bitmap(memlist, strlen(memlist), bitmap);
graph_update_membership(MEMCOMP_GET_GRAPH(mem_comp),
index, bitmap);
/*NOTE DO NOT DELETE bitlist, because it is
* being handled by graph*/
ptr = (uint32_t *)g_malloc(2*sizeof(uint32_t));
ptr[0] = maxtrans;
ptr[1] = index;
MEMCOMP_SET_MAXT(mem_comp,
(g_slist_insert_sorted(MEMCOMP_GET_MAXT(mem_comp),
ptr, ccm_memcomp_cmpr)));
return;
}
/* called by the cluster leader only */
static void
ccm_memcomp_note_my_membership(ccm_info_t *info)
{
char memlist[MAX_MEMLIST_STRING];
update_strcreate(CCM_GET_UPDATETABLE(info),
memlist, CCM_GET_LLM(info));
ccm_memcomp_note(info, llm_get_mynodename(&info->llm),
CCM_GET_MAXTRANS(info), memlist);
return;
}
/* add a new member to the membership list */
static void
ccm_memcomp_add(ccm_info_t *info, const char *orig)
{
int index, myindex;
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
index = llm_get_index(CCM_GET_LLM(info), orig);
myindex = llm_get_myindex(&info->llm);
graph_add_uuid(MEMCOMP_GET_GRAPH(mem_comp), index);
graph_add_to_membership(MEMCOMP_GET_GRAPH(mem_comp),
myindex, index);
/* ccm_memcomp_note(info, orig, maxtrans, memlist); */
return;
}
static void
ccm_memcomp_init(ccm_info_t *info)
{
int track=-1;
int index;
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
MEMCOMP_SET_GRAPH(mem_comp, graph_init());
/* go through the update list and note down all the members who
* had participated in the join messages. We should be expecting
* reply memlist bitmaps atleast from these nodes.
*/
while((index = update_get_next_index(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), &track)) != -1) {
graph_add_uuid(MEMCOMP_GET_GRAPH(mem_comp),index);
}
MEMCOMP_SET_MAXT(mem_comp, NULL);
MEMCOMP_SET_INITTIME(mem_comp, ccm_get_time());
}
static void
ccm_memcomp_reset(ccm_info_t *info)
{
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
graph_free(MEMCOMP_GET_GRAPH(mem_comp));
MEMCOMP_SET_GRAPH(mem_comp,NULL);
g_slist_foreach(MEMCOMP_GET_MAXT(mem_comp),
ccm_memcomp_free, NULL);
g_slist_free(MEMCOMP_GET_MAXT(mem_comp));
MEMCOMP_SET_MAXT(mem_comp, NULL);
return;
}
static int
ccm_memcomp_rcvd_all(ccm_info_t *info)
{
return graph_filled_all(MEMCOMP_GET_GRAPH(CCM_GET_MEMCOMP(info)));
}
static int
ccm_memcomp_timeout(ccm_info_t *info, long timeout)
{
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
return(ccm_timeout(MEMCOMP_GET_INITTIME(mem_comp),
ccm_get_time(), timeout));
}
static int
ccm_memcomp_get_maxmembership(ccm_info_t *info, char **bitmap)
{
GSList *head;
uint32_t *ptr;
int uuid;
memcomp_t *mem_comp = CCM_GET_MEMCOMP(info);
(void)graph_get_maxclique(MEMCOMP_GET_GRAPH(mem_comp),
bitmap);
head = MEMCOMP_GET_MAXT(mem_comp);
while (head) {
ptr = (uint32_t *)g_slist_nth_data(head, 0);
uuid = ptr[1];
if(bitmap_test(uuid, *bitmap, MAXNODE)) {
return ptr[0];
}
head = g_slist_next(head);
}
return 0;
}
/* */
/* END OF the membership tracking functions. */
/* */
static int
ccm_am_i_leader(ccm_info_t *info)
{
llm_info_t *llm = CCM_GET_LLM(info);
if ( llm_get_myindex(llm) == CCM_GET_CL(info)){
return TRUE;
}
return FALSE;
}
static gboolean
node_is_leader(ccm_info_t* info, const char* nodename)
{
return( llm_get_index(&info->llm, nodename) == CCM_GET_CL(info));
}
static int
ccm_already_joined(ccm_info_t *info)
{
if (CCM_GET_JOINED_TRANSITION(info)) {
return TRUE;
}
return FALSE;
}
/*
* END OF FUNCTIONS that keep track of stablized membership list
*/
/*
* BEGIN OF FUNCTIONS THAT KEEP TRACK of cluster nodes that have shown
* interest in joining the cluster.
*
* NOTE: when a new node wants to join the cluster, it multicasts a
* message asking for the necessary information to send out a join
* message. (it needs the current major transistion number, the context
* string i.e cookie, the protocol number that everybody is operating
* in).
*
* The functions below track these messages sent out by new potential
* members showing interest in acquiring the initial context.
*/
static void
ccm_add_new_joiner(ccm_info_t *info, const char *orig, struct ha_msg* msg)
{
llm_info_t* llm = &info->llm;
int idx = llm_get_index(&info->llm, orig);
const char* major_trans = 0;
int trans_val;
/* get the major transition version */
if ((major_trans = ha_msg_value(msg, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "ccm_state_version_request: "
"no protocol information");
return;
}
trans_val = atoi(major_trans);
llm_set_joinrequest(llm, idx, TRUE, trans_val);
return;
}
static gboolean
ccm_get_all_active_join_request(ccm_info_t* info)
{
llm_info_t* llm = &info->llm;
size_t i;
for (i = 0 ; i < llm->nodecount; i++){
if (STRNCMP_CONST(llm->nodes[i].status,"dead") != 0
&& llm_get_joinrequest(llm, i) == FALSE ){
return FALSE;
}
}
return TRUE;
}
static void
ccm_reset_all_join_request(ccm_info_t* info)
{
llm_info_t* llm = &info->llm;
size_t i;
for (i = 0 ; i < llm->nodecount; i++){
llm_set_joinrequest(llm, i, FALSE, 0);
}
}
static int
ccm_am_i_highest_joiner(ccm_info_t *info)
{
llm_info_t* llm = &info->llm;
int total_nodes =llm->nodecount;
int my_indx = llm->myindex;
int i;
for (i =0; i < total_nodes;i++){
if (i == my_indx) continue;
if ( llm_get_joinrequest(llm, i)){
int major_trans =llm_get_joinrequest_majortrans(llm, i);
int my_major_trans = CCM_GET_MAJORTRANS(info);
if (major_trans > my_major_trans ){
return FALSE;
}else if (major_trans == my_major_trans){
if (i > my_indx){
return FALSE;
}
}
}
}
return TRUE;
}
static void
ccm_remove_new_joiner(ccm_info_t *info, const char *orig)
{
llm_info_t* llm = &info->llm;
int index = llm_get_index(llm, orig);
llm_set_joinrequest(llm, index, FALSE, 0);
return;
}
/* send reply to a join quest and clear the request*/
static void
ccm_send_join_reply(ll_cluster_t *hb, ccm_info_t *info)
{
llm_info_t* llm = &info->llm;
size_t i;
for (i = 0 ; i < llm->nodecount; i++){
if ( i == (size_t)llm->myindex){
continue;
}
if (llm_get_joinrequest(llm, i)){
ccm_send_one_join_reply(hb,info, llm->nodes[i].nodename);
llm_set_joinrequest(llm, i, FALSE, 0);
}
}
}
/* */
/* END OF FUNCTIONS THAT KEEP TRACK of cluster nodes that have shown */
/* interest in joining the cluster. */
/* */
/*
/////////////////////////////////////////////////////////////////////
//
// BEGIN OF FUNCTIONS THAT SEND OUT messages to nodes of the cluster
//
/////////////////////////////////////////////////////////////////////
*/
/* compute the final membership list from the acquired connectivity */
/* information from other nodes. And send out the consolidated */
/* members of the cluster information to the all the members of */
/* that have participated in the CCM protocol. */
/* */
/* NOTE: Called by the cluster leader only. */
/* */
static void
ccm_compute_and_send_final_memlist(ll_cluster_t *hb, ccm_info_t *info)
{
char *bitmap;
uint maxtrans;
char string[MAX_MEMLIST_STRING];
char *cookie = NULL;
int repeat;
/* get the maxmimum membership list */
maxtrans = ccm_memcomp_get_maxmembership(info, &bitmap);
/* create a string with the membership information */
ccm_bitmap2str(bitmap, string, MAX_MEMLIST_STRING);
cookie = ccm_generate_random_cookie();
repeat = 0;
ccm_mem_bitmapfill(info, bitmap);
bitmap_delete(bitmap);
while (ccm_send_final_memlist(hb, info, cookie, string, maxtrans+1)
!= HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send finalmemlist", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
bitmap_delete(bitmap);
return;
}
}
/* fill my new memlist and update the new cookie if any */
/* increment the major transition number and reset the
* minor transition number
*/
CCM_SET_MAJORTRANS(info, maxtrans+1);
CCM_RESET_MINORTRANS(info);
/* if cookie has changed update it.
*/
if (cookie) {
ccm_debug2(LOG_DEBUG, "%s: cookie changed ", __FUNCTION__);
CCM_SET_COOKIE(info, cookie);
ccm_free_random_cookie(cookie);
}
/* check if any joiner is waiting for a response from us.
* If so respond and free all the joiners.
*/
ccm_send_join_reply(hb, info);
CCM_SET_CL(info, llm_get_myindex(CCM_GET_LLM(info)));
report_mbrs(info);/* call this before update_reset() */
/* update_reset(CCM_GET_UPDATETABLE(info));*/
ccm_memcomp_reset(info);
ccm_set_state(info, CCM_STATE_JOINED, NULL);
if(!ccm_already_joined(info)) {
CCM_SET_JOINED_TRANSITION(info, CCM_GET_MAJORTRANS(info));
}
return;
}
/* */
/* Browse through the list of all the connectivity request messages */
/* from cluster leaders. Send out the connectivity information only */
/* to the node which we believe is the cluster leader. To everybody */
/* else send out a null message. */
/* */
static int
ccm_send_cl_reply(ll_cluster_t *hb, ccm_info_t *info)
{
int ret=FALSE;
char memlist[MAX_MEMLIST_STRING];
const char* cl;
const char* cl_tmp;
void *cltrack;
uint trans;
int repeat;
/*
* Get the name of the cluster leader
*/
cl = update_get_cl_name(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info));
/* search through the update list and find if any Cluster
* leader has sent a memlist request. For each, check if
* that node is the one which we believe is the leader.
* if it is the leader, send it our membership list.
* if not send it an NULL membership reply.
*/
cltrack = update_initlink(CCM_GET_UPDATETABLE(info));
while((cl_tmp = update_next_link(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), cltrack, &trans)) != NULL) {
if(strncmp(cl, cl_tmp,
NODEIDSIZE) == 0) {
if(ccm_already_joined(info) &&
CCM_GET_MAJORTRANS(info) != trans){
ccm_log(LOG_INFO, "ccm evicted");
ccm_reset(info);
return FALSE;
}
ret = TRUE;
update_strcreate(CCM_GET_UPDATETABLE(info),
memlist, CCM_GET_LLM(info));
/* send Cluster Leader our memlist only if we are
* operating in the same transition as that of
* the leader, provided we have been a cluster member
* in the past
*/
repeat = 0;
while (ccm_send_memlist_res(hb, info, cl, memlist)
!=HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
} else {
/* I dont trust this Cluster Leader.
Send NULL memlist message */
repeat = 0;
while (ccm_send_memlist_res(hb, info, cl_tmp, NULL)
!= HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
}
}
update_freelink(CCM_GET_UPDATETABLE(info), cltrack);
update_free_memlist_request(CCM_GET_UPDATETABLE(info));
return ret;
}
/*
/////////////////////////////////////////////////////////////////////
//
// END OF FUNCTIONS THAT SEND OUT messages to nodes of the cluster
//
/////////////////////////////////////////////////////////////////////
*/
struct ha_msg * ccm_readmsg(ccm_info_t *info, ll_cluster_t *hb);
struct ha_msg *
ccm_readmsg(ccm_info_t *info, ll_cluster_t *hb)
{
int uuid;
assert(hb);
/* check if there are any leave events to be delivered */
if ((uuid=leave_get_next()) != -1) {
/* create a leave message and return it */
return ccm_create_leave_msg(info, uuid);
}
return hb->llc_ops->readmsg(hb, 0);
}
/* */
/* Move the state of this ccm node, from joining state directly to */
/* the joined state. */
/* */
/* NOTE: this is generally called when a joining nodes determines */
/* that it is the only node in the cluster, and everybody else are */
/* dead. */
/* */
static void
ccm_joining_to_joined(ll_cluster_t *hb, ccm_info_t *info)
{
char *bitmap;
char *cookie = NULL;
/* create a bitmap with the membership information */
(void) bitmap_create(&bitmap, MAXNODE);
bitmap_mark(llm_get_myindex(&info->llm), bitmap, MAXNODE);
/*
* I am the only around! Lets discard any cookie that we
* got from others, and create a new cookie.
* This bug was noticed: when testing with partitioned
* clusters.
*/
cookie = ccm_generate_random_cookie();
/* fill my new memlist and update the new cookie if any */
ccm_mem_bitmapfill(info, bitmap);
bitmap_delete(bitmap);
/* increment the major transition number and reset the
* minor transition number
*/
CCM_INCREMENT_MAJORTRANS(info);
CCM_RESET_MINORTRANS(info);
/* if cookie has changed update it.
*/
if (cookie) {
ccm_debug2(LOG_DEBUG, "%s: cookie changed ", __FUNCTION__);
CCM_SET_COOKIE(info, cookie);
ccm_free_random_cookie(cookie);
}
/* check if any joiner is waiting for a response from us.
* If so respond
*/
ccm_send_join_reply(hb, info);
CCM_SET_CL(info, llm_get_myindex(CCM_GET_LLM(info)));
update_reset(CCM_GET_UPDATETABLE(info));
ccm_set_state(info, CCM_STATE_JOINED, NULL);
report_mbrs(info);
if(!ccm_already_joined(info)) {
CCM_SET_JOINED_TRANSITION(info, 1);
}
return;
}
/*
* Move the state of this ccm node, from init state directly to
* the joined state.
*
* NOTE: this is generally called when a node when it determines
* that it is all alone in the cluster.
*/
static int
ccm_init_to_joined(ccm_info_t *info)
{
char* cookie;
int ret;
llm_info_t* llm = &info->llm;
ccm_mem_reset(info);
ret = ccm_mem_add(info, llm_get_myindex(llm));
if (ret != HA_OK){
ccm_log(LOG_ERR, "%s: adding myself to membership failed",
__FUNCTION__);
return HA_FAIL;
}
llm_set_uptime(llm, llm_get_myindex(llm), 1);
CCM_SET_MAJORTRANS(info, CCM_GET_MAJORTRANS(info)+1);
CCM_SET_MINORTRANS(info, 0);
cookie = ccm_generate_random_cookie();
CCM_SET_COOKIE(info, cookie);
ccm_free_random_cookie(cookie);
CCM_SET_CL(info, llm_get_myindex(CCM_GET_LLM(info)));
ccm_set_state(info, CCM_STATE_JOINED, NULL);
CCM_SET_JOINED_TRANSITION(info, 1);
report_mbrs(info);
return HA_OK;
}
static void
ccm_all_restart(ll_cluster_t* hb, ccm_info_t* info, struct ha_msg* msg)
{
const char * orig;
llm_info_t* llm = & info->llm;
if ( (orig = ha_msg_value(msg, F_ORIG)) ==NULL){
ccm_log(LOG_ERR, "orig not found in message");
return ;
}
if (strncmp(orig, llm_get_mynodename(llm), NODEIDSIZE) == 0){
/*don't react to our own message*/
return ;
}
if (info->state != CCM_STATE_VERSION_REQUEST
&& gl_membership_converged ){
gl_membership_converged = FALSE;
if (ccm_get_memcount(info) == 1) {
ccm_reset(info);
return;
}
ccm_set_state(info, CCM_STATE_NONE, msg);
ccm_reset_all_join_request(info);
CCM_SET_CL(info,-1);
if (ccm_send_restart_msg(hb, info) != HA_OK){
ccm_log(LOG_ERR, "sending out restart msg failed");
return;
}
if (ccm_send_protoversion(hb, info) != HA_OK){
ccm_log(LOG_ERR, "sending protoversion failed");
return;
}
ccm_set_state(info, CCM_STATE_VERSION_REQUEST, NULL);
}
}
static int
ccm_handle_state_info(ll_cluster_t* hb, ccm_info_t* info, struct ha_msg* msg)
{
const char* other_node_state;
int state;
if (!part_of_cluster(info->state)){
return HA_OK;
}
other_node_state = ha_msg_value(msg, F_STATE);
state = string2state(other_node_state);
if (state < 0){
ccm_log(LOG_ERR, "%s: wrong state", __FUNCTION__);
return HA_FAIL;
}
if (!part_of_cluster(state)){
return HA_OK;
}
/*both machines are already part of a cluster,
i.e. we are merging two partitions
*/
ccm_all_restart(hb, info, msg);
return HA_OK;
}
/* */
/* The state machine that processes message when it is */
/* the CCM_STATE_VERSION_REQUEST state */
/* */
static void
ccm_state_version_request(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *proto, *cookie, *trans, *clsize;
uint trans_val;
int proto_val;
uint clsize_val;
int try;
int repeat;
/* who sent this message */
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message from unknown host %s",
__FUNCTION__, orig);
return;
}
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION_RESP:
/* get the protocol version */
if ((proto = ha_msg_value(reply, CCM_PROTOCOL)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no protocol information", __FUNCTION__);
return;
}
proto_val = atoi(proto); /*TOBEDONE*/
if (proto_val >= CCM_VER_LAST) {
ccm_debug(LOG_WARNING, "%s: unknown protocol value", __FUNCTION__);
ccm_reset(info);
return;
}
/* if this reply has come from a node which is a member
* of a larger cluster, we will try to join that cluster
* else we will wait for some time, by dropping this
* response.
*/
if(resp_can_i_drop()) {
if ((clsize = ha_msg_value(reply, CCM_CLSIZE)) == NULL){
ccm_debug(LOG_WARNING, "%s: no cluster size information", __FUNCTION__);
return;
}
clsize_val = atoi(clsize);
if((clsize_val+1) <=
(llm_get_nodecount(CCM_GET_LLM(info))+1)/2) {
/* drop the response. We will wait for
* a response from a bigger group
*/
resp_dropped();
cl_shortsleep(); /* sleep for a while */
/* send a fresh version request message */
version_reset(CCM_GET_VERSION(info));
ccm_set_state(info, CCM_STATE_NONE, reply);
/* free all the joiners that we accumulated */
ccm_reset_all_join_request(info);
break;
}
}
resp_reset();
/* get the cookie string */
if ((cookie = ha_msg_value(reply, CCM_COOKIE)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no cookie information", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no protocol information", __FUNCTION__);
return;
}
trans_val = atoi(trans);
/* send the alive message to the cluster
The alive msg means: "I want to join this partition!"*/
CCM_SET_ACTIVEPROTO(info, proto_val);
CCM_SET_MAJORTRANS(info, trans_val);
CCM_SET_MINORTRANS(info, 0);
CCM_SET_COOKIE(info, cookie);
version_set_nresp(CCM_GET_VERSION(info),0);
repeat = 0;
while(ccm_send_alive_msg(hb, info) != HA_OK){
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING, "%s: failure to send alive", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
/* initialize the update table
and set our state to NEW_NODE_WAIT_FOR_MEM_LIST */
update_reset(CCM_GET_UPDATETABLE(info));
new_node_mem_list_time_init();
ccm_set_state(info, CCM_STATE_NEW_NODE_WAIT_FOR_MEM_LIST, reply);
/* free all the joiners that we accumulated */
ccm_reset_all_join_request(info);
break;
case CCM_TYPE_TIMEOUT:
try = version_retry(CCM_GET_VERSION(info),
CCM_TMOUT_GET_VRS(info));
switch (try) {
case VER_NO_CHANGE:
break;
case VER_TRY_AGAIN:
ccm_set_state(info, CCM_STATE_NONE, reply);
break;
case VER_TRY_END:
if(ccm_am_i_highest_joiner(info)) {
ccm_init_to_joined(info);
ccm_send_join_reply(hb, info);
} else {
ccm_debug2(LOG_DEBUG,"joined but not really");
version_reset(CCM_GET_VERSION(info));
ccm_set_state(info, CCM_STATE_NONE, reply);
ccm_reset_all_join_request(info);
}
break;
}
break;
case CCM_TYPE_PROTOVERSION:
/*
* cache this request. If we declare ourselves as
* a single member group, and if we find that
* somebody else also wanted to join the group.
* we will restart the join.
*/
ccm_add_new_joiner(info, orig, reply);
if (ccm_get_all_active_join_request(info)
&& ccm_am_i_highest_joiner(info)){
ccm_init_to_joined(info);
ccm_send_join_reply(hb, info);
}
break;
case CCM_TYPE_ABORT:
/* note down there is some activity going
* on and we are not yet alone in the cluster
*/
version_some_activity(CCM_GET_VERSION(info));
default:
/* nothing to do. Just forget the message */
break;
}
return;
}
static void
ccm_state_none(enum ccm_type msgtype,
struct ha_msg *msg,
ll_cluster_t *hb,
ccm_info_t *info)
{
if (ccm_send_protoversion(hb, info)!= HA_OK){
ccm_log(LOG_ERR, "sending version message failed");
return;
}
ccm_set_state(info, CCM_STATE_VERSION_REQUEST, NULL);
ccm_state_version_request(msgtype, msg, hb, info);
}
/* */
/* The state machine that processes message when it is */
/* CCM_STATE_JOINED state. */
/* */
static void
ccm_state_joined(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime;
uint trans_majorval=0, trans_minorval=0, uptime_val;
int repeat;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown"
, __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message "
"from unknown host %s", __FUNCTION__, orig);
return;
}
if(ccm_msg_type != CCM_TYPE_PROTOVERSION
&& ccm_msg_type != CCM_TYPE_STATE_INFO
&& ccm_msg_type != CCM_TYPE_RESTART) {
const char* tmpcookie = ha_msg_value(reply, CCM_COOKIE);
if (tmpcookie == NULL){
abort();
}
if(strncmp(CCM_GET_COOKIE(info),
ha_msg_value(reply, CCM_COOKIE), COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message "
"with unknown cookie, just dropping", __FUNCTION__);
dump_mbrs(info);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major "
"information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/*drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval,
CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received "
"%s message with "
"a earlier major transition number "
"recv_trans=%d, mytrans=%d", __FUNCTION__,
ccm_type2string(ccm_msg_type), trans_majorval,
CCM_GET_MAJORTRANS(info));
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor "
"information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
}
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION_RESP:
ccm_debug(LOG_WARNING, "%s: dropping message "
"of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
break;
case CCM_TYPE_PROTOVERSION:
/* If we were leader in the last successful iteration,
* then we shall respond with the neccessary information
*/
if (ccm_am_i_leader(info)){
repeat = 0;
while (ccm_send_one_join_reply(hb, info, orig)
!= HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join reply", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
}
break;
case CCM_TYPE_JOIN:
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update "
"information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/* update the minor transition number if it is of
* higher value and send a fresh JOIN message
*/
if (trans_minorval < CCM_GET_MINORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: got a join message from %s from lower "
"transition, restarting", __FUNCTION__, orig);
ccm_all_restart(hb, info, reply);
break;
}
update_reset(CCM_GET_UPDATETABLE(info));
update_add(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info),
orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
break;
case CCM_TYPE_LEAVE:
if (!node_is_member(info, orig)){
return;
}
/* If the dead node is the partition leader, go to
* JOINING state
*/
if (node_is_leader(info, orig)){
update_reset(CCM_GET_UPDATETABLE(info));
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING,reply);
return;
}
/* If I'm the leader, record this "I received the
* LEAVE message" and transit to WAIT_FOR_CHANGE
*/
if(ccm_am_i_leader(info)){
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
add_change_msg(info, orig, CCM_GET_MYNODE_ID(info),
NODE_LEAVE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), CCM_GET_MYNODE_ID(info),
CCM_GET_JOINED_TRANSITION(info), FALSE);
if(received_all_change_msg(info)){
char *newcookie = ccm_generate_random_cookie();
ccm_mem_update(info, orig, NODE_LEAVE);
send_mem_list_to_all(hb, info, newcookie);
CCM_SET_MAJORTRANS(info, trans_majorval+1);
CCM_RESET_MINORTRANS(info);
CCM_SET_COOKIE(info, newcookie);
ccm_free_random_cookie(newcookie);
report_mbrs(info);
return;
}
change_time_init();
ccm_bcast_node_leave_notice(hb,info, orig);
ccm_set_state(info, CCM_STATE_WAIT_FOR_CHANGE, reply);
}
break;
case CCM_TYPE_NODE_LEAVE_NOTICE:{
const char* node;
const char* leader = orig;
node = ha_msg_value(reply, F_NODE);
if(node == NULL){
ccm_log(LOG_ERR, "%s: node not found in the message"
, __FUNCTION__);
ccm_message_debug2(LOG_INFO, reply);
return;
}
if (!node_is_member(info, node)){
return;
}
if( !ccm_am_i_leader(info)){
send_node_leave_to_leader(hb, info, leader);
mem_list_time_init();
ccm_set_state(info,CCM_STATE_WAIT_FOR_MEM_LIST, reply);
}
break;
}
case CCM_TYPE_NODE_LEAVE:
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update "
"information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/* If I'm leader, record received LEAVE message by orig
* and transition to WAIT_FOR_CHANGE state
*/
if(ccm_am_i_leader(info)){
const char *node = ha_msg_value(reply, F_NODE);
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
add_change_msg(info,node,orig,NODE_LEAVE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val, FALSE);
change_time_init();
ccm_set_state(info, CCM_STATE_WAIT_FOR_CHANGE, reply);
}
break;
case CCM_TYPE_ALIVE:
/* If I'm leader, record I received the ALIVE message and
* transit to WAIT_FOR_CHANGE
*/
if (ccm_am_i_leader(info)){
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
add_change_msg(info,orig, CCM_GET_MYNODE_ID(info),
NEW_NODE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), CCM_GET_MYNODE_ID(info),
CCM_GET_JOINED_TRANSITION(info), FALSE);
if(received_all_change_msg(info)){
char *newcookie = ccm_generate_random_cookie();
ccm_mem_update(info, orig, NEW_NODE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
info->change_node_id, trans_majorval+1, FALSE);
send_mem_list_to_all(hb, info, newcookie);
CCM_SET_MAJORTRANS(info, trans_majorval+1);
CCM_RESET_MINORTRANS(info);
CCM_SET_COOKIE(info, newcookie);
ccm_free_random_cookie(newcookie);
report_mbrs(info);
return;
}
change_time_init();
ccm_set_state(info, CCM_STATE_WAIT_FOR_CHANGE, reply);
}else{
/* I'm not leader, send CCM_TYPE_NEW_NODE
* to leader and transit to WAIT_FOR_MEM_LIST
*/
ccm_send_newnode_to_leader(hb, info, orig);
mem_list_time_init();
ccm_set_state(info,CCM_STATE_WAIT_FOR_MEM_LIST, reply);
}
break;
case CCM_TYPE_NEW_NODE:
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update "
"information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/* If I'm leader, record received ALIVE message by orig
* and transition to WAIT_FOR_CHANGE state
*/
if(ccm_am_i_leader(info)){
const char *node = ha_msg_value(reply, F_NODE);
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
add_change_msg(info,node, orig, NEW_NODE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
orig, uptime_val, FALSE);
change_time_init();
ccm_set_state(info, CCM_STATE_WAIT_FOR_CHANGE, reply);
}
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
case CCM_TYPE_MEM_LIST:{
int quorum;
const char* memlist;
if (strncmp(orig, llm_get_mynodename((&info->llm) ), NODEIDSIZE) == 0){
/*this message is from myself, ignore it*/
break;
}
memlist = ha_msg_value(reply, CCM_MEMLIST);
if (memlist == NULL){
break;
}
if (ha_msg_value_int (reply, CCM_QUORUM, &quorum)==HA_OK){
info->has_quorum = quorum;
}
else {
info->has_quorum = -1;
}
if (node_is_leader(info, orig)
&& !am_i_member_in_memlist(info, memlist)){
ccm_set_state(info, CCM_STATE_NONE, reply);
break;
}
report_mbrs(info);
break;
}
case CCM_TYPE_REQ_MEMLIST:
case CCM_TYPE_RES_MEMLIST:
case CCM_TYPE_FINAL_MEMLIST:
case CCM_TYPE_ABORT:
ccm_log(LOG_ERR, "%s: dropping message "
"of type %s. Is this a Byzantine failure?"
, __FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
default:
break;
}
}
/* */
/* The state machine that processes message when it is in */
/* CCM_STATE_WAIT_FOR_CHANGE state. */
/* */
static void ccm_state_wait_for_change(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime, *node;
uint trans_majorval=0, trans_minorval=0, uptime_val=0;
gboolean uptime_set = FALSE;
int repeat;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown"
, __FUNCTION__ );
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message "
"from unknown host %s", __FUNCTION__, orig);
return;
}
node = ha_msg_value(reply, F_NODE);
if(ccm_msg_type != CCM_TYPE_PROTOVERSION
&& ccm_msg_type != CCM_TYPE_STATE_INFO
&& ccm_msg_type != CCM_TYPE_RESTART) {
if(strncmp(CCM_GET_COOKIE(info),
ha_msg_value(reply, CCM_COOKIE), COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message "
"with unknown cookie, just dropping", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major "
"information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/* drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval,
CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received "
"%s message with "
"a earlier major transition number "
"recv_trans=%d, mytrans=%d", __FUNCTION__,
ccm_type2string(ccm_msg_type), trans_majorval,
CCM_GET_MAJORTRANS(info));
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor "
"information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
}
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION:
/*
* cache this request. We will respond to it,
* after transition is complete.
*/
ccm_add_new_joiner(info, orig, reply);
break;
case CCM_TYPE_NODE_LEAVE_NOTICE:
/* It is my own message, then I can ignore it
* or from another lead, i.e. we are in split-brain
* and I can do nothing about it
*/
break;
case CCM_TYPE_LEAVE:
if (!node_is_member(info, orig)){
return;
}
if(strcmp(info->change_node_id, orig) == 0
&& info->change_type == NODE_LEAVE){
/*It is the same node leaving*/
return;
}
node = orig;
orig = CCM_GET_MYNODE_ID(info);
uptime_val = CCM_GET_JOINED_TRANSITION(info);
uptime_set = TRUE;
/*fall through*/
case CCM_TYPE_NODE_LEAVE:
/* only leader can stay in this state */
if(!ccm_am_i_leader(info))
break;
if (!uptime_set){
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information",
__FUNCTION__);
return;
}
uptime_val = atoi(uptime);
uptime_set = TRUE;
}
/* Record received LEAVE message by orig.
* If received all change msg, send mem_list to members.
*/
if(is_expected_change_msg(info,node,NODE_LEAVE)){
append_change_msg(info,orig);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val, FALSE);
if(received_all_change_msg(info)){
char *newcookie = ccm_generate_random_cookie();
ccm_mem_update(info, node, NODE_LEAVE);
send_mem_list_to_all(hb, info, newcookie);
CCM_SET_MAJORTRANS(info, trans_majorval+1);
CCM_RESET_MINORTRANS(info);
CCM_SET_COOKIE(info, newcookie);
report_mbrs(info);
reset_change_info(info);
/* update_reset(CCM_GET_UPDATETABLE(info));*/
ccm_free_random_cookie(newcookie);
ccm_send_join_reply(hb, info);
CCM_SET_CL(info, llm_get_myindex(CCM_GET_LLM(info)));
ccm_set_state(info, CCM_STATE_JOINED,reply);
return;
}
}else{
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
return;
}
break;
case CCM_TYPE_ALIVE:
node = orig;
orig = CCM_GET_MYNODE_ID(info);
uptime_val = CCM_GET_JOINED_TRANSITION(info);
uptime_set = TRUE;
/*fall through*/
case CCM_TYPE_NEW_NODE:
/* only leader can stay in this state */
if(!ccm_am_i_leader(info)){
assert(0);
}
if (!uptime_set){
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information",
__FUNCTION__);
return;
}
uptime_val = atoi(uptime);
uptime_set = TRUE;
}
if(is_expected_change_msg(info,node, NEW_NODE)){
append_change_msg(info,orig);
update_add(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info),
orig, uptime_val, FALSE);
if(received_all_change_msg(info)){
char *newcookie = ccm_generate_random_cookie();
ccm_mem_update(info, node, NEW_NODE);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
info->change_node_id, trans_majorval+1, FALSE);
send_mem_list_to_all(hb, info, newcookie);
CCM_SET_MAJORTRANS(info, trans_majorval+1);
CCM_RESET_MINORTRANS(info);
CCM_SET_COOKIE(info, newcookie);
report_mbrs(info);
reset_change_info(info);
/* update_reset(CCM_GET_UPDATETABLE(info));*/
ccm_free_random_cookie(newcookie);
ccm_send_join_reply(hb, info);
ccm_set_state(info, CCM_STATE_JOINED, reply);
return;
}
}else{
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
while (ccm_send_join(hb, info) != HA_OK) {
ccm_debug(LOG_WARNING, "%s: failure to send join",
__FUNCTION__);
cl_shortsleep();
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
return;
}
break;
case CCM_TYPE_TIMEOUT:
if(change_timeout(CCM_TMOUT_GET_U(info))){
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
break;
case CCM_TYPE_JOIN:
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
uptime_set = TRUE;
/* update the minor transition number if it is of
* higher value and send a fresh JOIN message
*/
if (trans_minorval < CCM_GET_MINORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: got a join message from %s from lower "
"transition, restarting", __FUNCTION__, orig);
ccm_all_restart(hb, info, reply);
break;
}
update_reset(CCM_GET_UPDATETABLE(info));
update_add(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info),
orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
default:
ccm_log(LOG_ERR, "%s: dropping message "
"of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
}
}
/* */
/* The state machine that processes message when it is */
/* in the CCM_STATE_SENT_MEMLISTREQ state */
/* */
static void
ccm_state_sent_memlistreq(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *memlist, *uptime;
uint trans_minorval=0, trans_majorval=0, trans_maxval=0;
uint uptime_val;
int repeat;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message from unknown host %s",
__FUNCTION__, orig);
return;
}
if(ccm_msg_type == CCM_TYPE_PROTOVERSION
|| ccm_msg_type == CCM_TYPE_STATE_INFO
|| ccm_msg_type == CCM_TYPE_RESTART) {
goto switchstatement;
}
if(strncmp(CCM_GET_COOKIE(info), ha_msg_value(reply, CCM_COOKIE),
COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message "
"with unknown cookie, just dropping", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s :no transition major information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/* drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval, CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received CCM_TYPE_JOIN message with"
"a earlier major transition number", __FUNCTION__);
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
switchstatement:
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION_RESP:
ccm_debug(LOG_WARNING, "%s: "
"dropping message of type %s. "
" Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
break;
case CCM_TYPE_PROTOVERSION:
/*
* cache this request. We will respond to it,
* if we become the leader.
*/
ccm_add_new_joiner(info, orig, reply);
break;
case CCM_TYPE_JOIN:
/* The join request has come too late.
* I am already the leader, and my
* leadership cannot be relinquished
* because that can confuse everybody.
* This join request shall be considered.
* But leadership shall not be relinquished.
*/
if(trans_majorval != CCM_GET_MAJORTRANS(info)
|| trans_minorval != CCM_GET_MINORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: got a join message from %s from a wrong "
"transition, restarting", __FUNCTION__, orig);
ccm_all_restart(hb, info, reply);
break;
}
ccm_debug2(LOG_DEBUG, "considering a late join message "
"from orig=%s", orig);
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME))
== NULL){
ccm_debug(LOG_WARNING,
"%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val, FALSE);
ccm_memcomp_add(info, orig);
break;
case CCM_TYPE_TIMEOUT:
if (ccm_memcomp_timeout(info,
CCM_TMOUT_GET_IFF(info))) {
/* we waited long for membership response
* from all nodes, stop waiting and send
* final membership list
*/
ccm_compute_and_send_final_memlist(hb, info);
}
break;
case CCM_TYPE_REQ_MEMLIST:
/* if this is my own message just forget it */
if(strncmp(orig, llm_get_mynodename(&info->llm),
NODEIDSIZE) == 0){
if(llm_get_live_nodecount(&info->llm) == 1){
ccm_log(LOG_INFO, "%s: directly call"
"ccm_compute_and_send_final_memlist()",
__FUNCTION__);
ccm_compute_and_send_final_memlist(hb, info);
}
break;
}
/* whoever is requesting memlist from me thinks it is
* the leader. Hmm....., we will send it a NULL memlist.
* In partitioned network case both of us can be
* leaders. Right?
*/
repeat = 0;
while (ccm_send_memlist_res(hb, info, orig, NULL) !=
HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
break;
case CCM_TYPE_RES_MEMLIST:
/* mark that this node has sent us a memlist reply.
* Calculate the membership list with this new message
*/
if(trans_minorval != CCM_GET_MINORTRANS(info)){
break;
}
if(trans_majorval != CCM_GET_MAJORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: dropping CCM_TYPE_RES_MEMLIST "
"from orig=%s mymajor=%d msg_major=%d",
__FUNCTION__, orig, trans_majorval,
CCM_GET_MAJORTRANS(info));
break;
}
if ((memlist = ha_msg_value(reply, CCM_MEMLIST))
== NULL) {
ccm_debug(LOG_WARNING, "%s: no memlist ", __FUNCTION__);
break;
}
/* get the max transition version */
if (!(trans = ha_msg_value(reply, CCM_MAXTRANS))) {
ccm_debug(LOG_WARNING,
"%s: no max transition "
"information %s, type=%d",
__FUNCTION__, orig, ccm_msg_type);
return;
}
trans_maxval = atoi(trans);
ccm_memcomp_note(info, orig, trans_maxval, memlist);
if (ccm_memcomp_rcvd_all(info)) {
ccm_compute_and_send_final_memlist(hb,info);
}
break;
case CCM_TYPE_LEAVE:
/* since we are waiting for a memlist from all the
* members who have sent me a join message, we
* should be waiting for their message or their
* leave message atleast.
*/
/* if this node had not participated in the update
* exchange than just neglect it
*/
if(!update_is_node_updated(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig)) {
break;
}
/* if this node had sent a memlist before dying,
* reset its memlist information */
ccm_memcomp_note(info, orig, 0, "");
if (ccm_memcomp_rcvd_all(info)) {
ccm_compute_and_send_final_memlist(hb, info);
}
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
case CCM_TYPE_FINAL_MEMLIST:
case CCM_TYPE_ABORT:
default:
ccm_log(LOG_ERR, "%s: dropping message of type %s. Is this "
"a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
}
}
/* */
/* the state machine that processes messages when it is in the */
/* CCM_STATE_MEMLIST_RES state. */
/* */
static void
ccm_state_memlist_res(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime, *memlist, *cookie, *cl;
uint trans_majorval=0, trans_minorval=0, trans_maxval=0;
uint uptime_val;
uint curr_major, curr_minor;
int indx;
int repeat;
int quorum;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message from unknown host %s",
__FUNCTION__, orig);
return;
}
if(ccm_msg_type == CCM_TYPE_PROTOVERSION
|| ccm_msg_type == CCM_TYPE_STATE_INFO
|| ccm_msg_type == CCM_TYPE_RESTART) {
goto switchstatement;
}
if(strncmp(CCM_GET_COOKIE(info), ha_msg_value(reply, CCM_COOKIE),
COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message with unknown cookie, just dropping",
__FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/* drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval, CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received CCM_TYPE_JOIN message with"
"a earlier major transition number", __FUNCTION__);
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
switchstatement:
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION_RESP:
ccm_debug(LOG_WARNING, "%s: dropping message"
" of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
break;
case CCM_TYPE_PROTOVERSION:
/*
* cache this request. We will respond to it, if we
* become the leader.
*/
ccm_add_new_joiner(info, orig, reply);
break;
case CCM_TYPE_JOIN:
/*
* This could have happened because the leader died
* and somebody noticed this and sent us this request.
* In such a case the minor transition number should
* have incremented. Or
* This could have happened because the leader's
* FINAL_MEMLIST
* has not reach us, whereas it has reached somebody
* else, and since that somebody saw a change in
* membership, initiated another join protocol.
* In such a case the major transition
* number should have incremented.
*/
/*
* if major number is incremented, send an abort message
* to the sender. The sender must resend the message.
*/
if (trans_majorval > CCM_GET_MAJORTRANS(info)) {
repeat = 0;
while (ccm_send_abort(hb, info, orig,
trans_majorval, trans_minorval)
!= HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send abort", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
break;
}
/* if minor transition number is incremented,
* reset uptable table and start a join protocol
*/
if (trans_minorval > CCM_GET_MINORTRANS(info)) {
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME))
== NULL){
ccm_debug(LOG_WARNING,
"%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
update_reset(CCM_GET_UPDATETABLE(info));
update_add(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
break;
case CCM_TYPE_REQ_MEMLIST:
/* there are two reasons that can bring us here
* 1. Because some other node still thinks he is
* the master,(though we dont think so). Send
* a NULL membership list to him immidiately.
* 2. Because of byzantine failures, though we have
* not received the membership list in the last
* round. We have waited to such an exent that some
* node already thinks he is the master of the
* the new group transition. Well, there is something
* seriously wrong with us. We will send a leave
* message to everybody and say good bye. And we
* will start all fresh!
*/
if (trans_minorval == CCM_GET_MINORTRANS(info)) {
repeat = 0;
while (ccm_send_memlist_res(hb, info, orig,
NULL) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
break;
}
break;
case CCM_TYPE_TIMEOUT:
/* If we have waited too long for the leader to respond
* just assume that the leader is dead and start over
* a new round of the protocol
*/
if(!finallist_timeout(CCM_TMOUT_GET_FL(info))) {
break;
}
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
finallist_reset();
ccm_set_state(info, CCM_STATE_JOINING, reply);
break;
case CCM_TYPE_LEAVE:
/*
* If this message is because of loss of connectivity
* with the node which we think is the master, then
* restart the join. Loss of anyother node should be
* confirmed by the finalmemlist of the master.
*/
cl = update_get_cl_name(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info));
if(strncmp(cl, orig, NODEIDSIZE) == 0) {
/* increment the current minor transition value
* and resend the join message
*/
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
finallist_reset();
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
break;
case CCM_TYPE_FINAL_MEMLIST:
/* WOW we received the membership list from the master.
* Check if I am part of the membership list. If not,
* voluntarily leave the cluster and start all over
* again
*/
cl = update_get_cl_name(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info));
if(strncmp(cl, orig, NODEIDSIZE) != 0) {
/* received memlist from a node we do not
* think is the leader. We just reject the
* message and wait for a message from the
* our percieved master
*/
ccm_debug(LOG_WARNING, "%s: received final memlist from "
"non-master,neglecting", __FUNCTION__);
break;
}
/*
* confirm that the major transition and minor
* transition version match
*/
curr_major = CCM_GET_MAJORTRANS(info);
curr_minor = CCM_GET_MINORTRANS(info);
if(curr_major != trans_majorval ||
curr_minor != trans_minorval){
ccm_debug(LOG_WARNING, "%s: "
"received final memlist from master, "
"but transition versions do not match: "
"rejecting the message", __FUNCTION__);
break;
}
if ((memlist = ha_msg_value(reply, CCM_MEMLIST))
== NULL) {
ccm_debug(LOG_WARNING, "%s: no membership list ", __FUNCTION__);
return;
}
if ((trans = ha_msg_value(reply, CCM_MAXTRANS))
== NULL) {
ccm_debug(LOG_WARNING, "%s: no membership list ", __FUNCTION__);
return;
}
trans_maxval = atoi(trans);
if (ha_msg_value_int (reply, CCM_QUORUM, &quorum)==HA_OK){
info->has_quorum = quorum;
}
else {
info->has_quorum = -1;
}
if (!am_i_member_in_memlist(info, memlist)) {
ccm_reset(info);
break;
}
ccm_mem_strfill(info, (const char *)memlist);
/* increment the major transition number and reset the
* minor transition number
*/
CCM_SET_MAJORTRANS(info, trans_maxval);
CCM_RESET_MINORTRANS(info);
/* check if leader has changed the COOKIE, this can
* happen if the leader sees a partitioned group
*/
if ((cookie = ha_msg_value(reply, CCM_NEWCOOKIE))
!= NULL) {
ccm_debug2(LOG_DEBUG, "%s: leader changed cookie ", __FUNCTION__);
CCM_SET_COOKIE(info, cookie);
}
indx = llm_get_index(&info->llm, cl);
assert(indx != -1);
CCM_SET_CL(info, indx);
report_mbrs(info); /* call before update_reset */
/* update_reset(CCM_GET_UPDATETABLE(info));*/
finallist_reset();
ccm_set_state(info, CCM_STATE_JOINED, reply);
ccm_reset_all_join_request(info);
if(!ccm_already_joined(info))
CCM_SET_JOINED_TRANSITION(info,
CCM_GET_MAJORTRANS(info));
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
case CCM_TYPE_ABORT:
case CCM_TYPE_RES_MEMLIST:
default:
ccm_log(LOG_ERR, "%s: dropping message of type %s. "
"Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
}
}
/* */
/* the state machine that processes messages when it is in the */
/* CCM_STATE_JOINING state. */
/* */
static void
ccm_state_joining(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime;
uint trans_majorval=0, trans_minorval=0;
uint uptime_val;
int repeat;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message "
"from unknown host %s", __FUNCTION__, orig);
return;
}
if(ccm_msg_type == CCM_TYPE_PROTOVERSION
|| ccm_msg_type == CCM_TYPE_STATE_INFO
|| ccm_msg_type == CCM_TYPE_RESTART) {
goto switchstatement;
}
if(strncmp(CCM_GET_COOKIE(info), ha_msg_value(reply, CCM_COOKIE),
COOKIESIZE) != 0){
if(ccm_msg_type == CCM_TYPE_PROTOVERSION_RESP) {
version_inc_nresp(CCM_GET_VERSION(info));
ccm_debug(LOG_WARNING, "%s: received message "
"incrementing versionresp counter %d",
__FUNCTION__, version_get_nresp(CCM_GET_VERSION(info)));
}
ccm_debug(LOG_WARNING, "%s: received message "
"with unknown cookie, just dropping", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/* drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval, CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received CCM_TYPE_JOIN message with"
"a earlier major transition number", __FUNCTION__);
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
if (trans_minorval < CCM_GET_MINORTRANS(info)) {
return;
}
switchstatement:
switch (ccm_msg_type) {
case CCM_TYPE_PROTOVERSION_RESP:
/* If we were joined in an earlier iteration, then this
* message should not have arrived. A bug in the logic!
*/
if(ccm_already_joined(info)) {
ccm_debug(LOG_WARNING, "%s: BUG:"
" received CCM_TYPE_PROTOVERSION_RESP "
"message when we have not asked for "
"it ", __FUNCTION__);
break;
}
ccm_debug(LOG_WARNING, "%s: dropping message "
" of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
break;
case CCM_TYPE_PROTOVERSION:
/*
* cache this request. We will respond to it,
* if we become the leader.
*/
ccm_add_new_joiner(info, orig, reply);
break;
case CCM_TYPE_JOIN:
/* get the update value */
if((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/*
* note down all the information contained in the
* message There is a possibility that I am the leader,
* if all the nodes died, and I am the only surviving
* node! If this message has originated from me,
* note down the current time. This information is
* needed, to later recognize that I am the only
* surviving node.
*/
/* update the minor transition number if it is of
* higher value
* and send a fresh JOIN message
*/
if (trans_minorval > CCM_GET_MINORTRANS(info)) {
update_reset(CCM_GET_UPDATETABLE(info));
update_add( CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
} else {
/* update the update table */
update_add( CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, uptime_val,
TRUE);
/* if all nodes have responded, its time
* to elect the leader
*/
if (UPDATE_GET_NODECOUNT(
CCM_GET_UPDATETABLE(info)) ==
llm_get_live_nodecount(&info->llm)) {
/* check if I am the leader */
if (update_am_i_leader(
CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info))) {
/* send out the
* membershiplist request */
repeat = 0;
while(ccm_send_memlist_request(
hb, info)!=HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send memlist request",
__FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_memcomp_init(info);
ccm_memcomp_note_my_membership(
info);
ccm_set_state(info,
CCM_STATE_SENT_MEMLISTREQ, reply);
} else {
/* check if we have already
* received memlist request
* from any node(which
* believes itself to be the
* leader)
* If so,we have to reply to
* them with our membership
* list. But there is a catch.
* If we do not think the
* requestor to be the leader,
* then we send it an null
* membership message!
*/
if (ccm_send_cl_reply(hb,info)
== TRUE) {
finallist_init();
ccm_set_state(info,
CCM_STATE_MEMLIST_RES, reply);
}
}
break; /* done all processing */
}
}
break;
case CCM_TYPE_REQ_MEMLIST:
/* well we have not yet timedout! And a memlist
* request has arrived from the cluster leader. Hmm...
* We should wait till timeout, to respond.
*
* NOTE: there is a chance
* that more than one cluster leader might request
* the membership list. Due to cluster partitioning :( )
*/
/* If we have received CCM_TYPE_JOIN from all nodes
* we don't need wait for timeout here.
*/
update_add_memlist_request(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info), orig, trans_majorval);
if (UPDATE_GET_NODECOUNT( CCM_GET_UPDATETABLE(info)) == llm_get_live_nodecount(&info->llm)
&& !update_am_i_leader(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info))) {
if (ccm_send_cl_reply(hb,info) == TRUE) {
finallist_init();
ccm_set_state(info, CCM_STATE_MEMLIST_RES, reply);
break;
}
}
/*
* FALL THROUGH
*/
case CCM_TYPE_TIMEOUT:
/*
* If timeout expired, elect the leader.
* If I am the leader, send out the membershiplist request
*/
if (!update_timeout_expired(CCM_GET_UPDATETABLE(info),
CCM_TMOUT_GET_U(info))) {
break;
}
if (update_am_i_leader(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info))) {
/* if I am the only one around go directly
* to joined state.
*/
if (UPDATE_GET_NODECOUNT(
CCM_GET_UPDATETABLE(info)) == 1) {
if(ccm_already_joined(info) ||
!version_get_nresp(
CCM_GET_VERSION(info))){
ccm_joining_to_joined(hb,
info);
} else {
ccm_reset(info);
}
break;
}
/* send out the membershiplist request */
repeat = 0;
while (ccm_send_memlist_request(hb, info)
!= HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send memlist request",
__FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_memcomp_init(info);
ccm_memcomp_note_my_membership(info);
ccm_set_state(info, CCM_STATE_SENT_MEMLISTREQ, reply);
} else {
/* check if we have already received memlist
* request from any node(which believes itself
* to be the leader)
* If so,we have to reply to them with our
* membership list. But there is a catch.
* If we do not think the
* requestor to be the leader, then we send
* it an abort message!
*/
if (ccm_send_cl_reply(hb, info) == TRUE) {
/* free the update data*/
finallist_init();
ccm_set_state(info,
CCM_STATE_MEMLIST_RES, reply);
}
}
break;
case CCM_TYPE_ABORT:
/*
* This is a case where my JOIN request is not honoured
* by the recieving host(probably because it is waiting
* on some message, before which it cannot initiate
* the join).
* We will resend the join message, incrementing the
* minor version number, provided this abort is
* requested
* for this minor version.
*/
if(trans_majorval != CCM_GET_MAJORTRANS(info) ||
trans_minorval != CCM_GET_MINORTRANS(info)) {
/* nothing to worry just forget this message */
break;
}
/* increment the current minor transition value
* and resend the
join message */
CCM_INCREMENT_MINORTRANS(info);
update_reset(CCM_GET_UPDATETABLE(info));
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_log(LOG_ERR,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
break;
case CCM_TYPE_LEAVE:
/*
* Has that node already sent a valid update message
* before death. If so, remove him from the update
* table.
*/
update_remove(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
orig);
/* if we have any cached version-request from this node
* we will get rid of that too
*/
ccm_remove_new_joiner(info, orig);
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
case CCM_TYPE_RES_MEMLIST:
case CCM_TYPE_FINAL_MEMLIST:
/* this message is from other partitions*/
ccm_debug(LOG_WARNING, "%s: received a %s message",
__FUNCTION__, ccm_type2string(ccm_msg_type));
ccm_debug(LOG_WARNING, "We probably have different partitions");
break;
default:
ccm_log(LOG_ERR, "%s: dropping message "
"of type %s from %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type), orig);
/* nothing to do. Just forget the message */
break;
}
return;
}
static void
ccm_control_init(ccm_info_t *info)
{
ccm_init(info);
/* if this is the only active node in the cluster, go to the
JOINED state */
if (llm_get_live_nodecount(CCM_GET_LLM(info)) == 1) {
ccm_init_to_joined(info);
} else {
ccm_set_state(info, CCM_STATE_NONE, NULL);
}
return;
}
/* */
/* The callback function which is called when the status of a link */
/* changes. */
/* */
static void
LinkStatus(const char * node, const char * lnk, const char * status ,
void * private)
{
ccm_debug2(LOG_DEBUG, "Link Status update: Link %s/%s "
"now has status %s", node, lnk, status);
}
/* */
/* The most important function which tracks the state machine. */
/* */
/* look at the current state machine and decide if */
/* the state machine needs immidiate control for further */
/* state machine processing. Called by the check function */
/* of heartbeat-source of the main event loop. */
int
ccm_need_control(void *data)
{
ccm_info_t *info = (ccm_info_t *)((ccm_t *)data)->info;
if(leave_any() ||
CCM_GET_STATE(info) != CCM_STATE_JOINED){
return TRUE;
}
return FALSE;
}
/* look at the current state machine and decide if */
/* the state machine needs immidiate control for further */
/* state machine processing. Called by the check function */
/* of heartbeat-source of the main event loop. */
int
ccm_take_control(void *data)
{
ccm_info_t *info = (ccm_info_t *)((ccm_t *)data)->info;
ll_cluster_t *hbfd = (ll_cluster_t *)((ccm_t *)data)->hbfd;
return ccm_control_process(info, hbfd);
}
IPC_Channel *
ccm_get_ipcchan(void *data)
{
ll_cluster_t *hbfd = (ll_cluster_t *)((ccm_t *)data)->hbfd;
return hbfd->llc_ops->ipcchan(hbfd);
}
#define PINGNODE "ping"
static int
set_llm_from_heartbeat(ll_cluster_t* llc, ccm_info_t* info){
llm_info_t* llm = &info->llm;
struct llc_ops* ops = llc->llc_ops;
const char* status;
const char* node;
const char* mynode = ops->get_mynodeid(llc);
const char* cluster;
const char* quorum_server;
const char* site;
int weight;
if (mynode == NULL){
ccm_log(LOG_ERR, "%s: mynode is NULL", __FUNCTION__);
return HA_FAIL;
}
ccm_debug2(LOG_DEBUG, "==== Starting Node Walk =========");
if (ops->init_nodewalk(llc) != HA_OK) {
ccm_log(LOG_ERR, "Cannot start node walk");
ccm_log(LOG_ERR, "REASON: %s", ops->errmsg(llc));
return HA_FAIL;
}
llm = CCM_GET_LLM(info);
llm_init(llm);
memset(info->cluster, 0, sizeof(info->cluster));
cluster = llc->llc_ops->get_parameter(llc, KEY_CLUSTER);
if (cluster != NULL) {
strncpy(info->cluster, cluster, PATH_MAX);
}
memset(info->quorum_server, 0, sizeof(info->quorum_server));
quorum_server = llc->llc_ops->get_parameter(llc, KEY_QSERVER);
if (quorum_server != NULL) {
strncpy(info->quorum_server, quorum_server, PATH_MAX);
}
while((node = ops->nextnode(llc)) != NULL) {
if (strcmp(ops->node_type(llc, node), PINGNODE)==0){
continue;
}
status = ops->node_status(llc, node);
site = ops->node_site(llc, node);
weight = ops->node_weight(llc, node);
ccm_debug2(LOG_DEBUG, "Cluster node: %s: status: %s", node,
status);
if (llm_add(llm, node, status, mynode, site, weight)!= HA_OK){
ccm_log(LOG_ERR, "%s: adding node %s to llm failed",
__FUNCTION__, node);
return HA_FAIL;
}
}
llm_display(llm);
if (ops->end_nodewalk(llc) != HA_OK) {
ccm_log(LOG_ERR, "Cannot end node walk");
ccm_log(LOG_ERR, "REASON: %s", ops->errmsg(llc));
return HA_FAIL;
}
ccm_debug2(LOG_DEBUG, "======= Ending Node Walk ==========");
ccm_debug2(LOG_DEBUG, "Total # of Nodes in the Cluster: %d",
llm_get_nodecount(llm));
return HA_OK;
}
ccm_info_t* ccm_info_saved = NULL;
ll_cluster_t* hb_fd_saved = NULL;
void *
ccm_initialize()
{
unsigned fmask;
const char * hname;
ccm_info_t *global_info = NULL;
ll_cluster_t* hb_fd;
ccm_t *ccmret = NULL;
const char * parameter;
ccm_debug2(LOG_DEBUG, "========================== Starting CCM ===="
"======================");
CL_SIGINTERRUPT(SIGTERM, 1);
cl_inherit_logging_environment(0);
hb_fd = ll_cluster_new("heartbeat");
ccm_debug(LOG_DEBUG, "Signing in with Heartbeat");
if (hb_fd->llc_ops->signon(hb_fd, "ccm")!= HA_OK) {
ccm_log(LOG_ERR, "Cannot sign on with heartbeat");
ccm_log(LOG_ERR, "REASON: %s", hb_fd->llc_ops->errmsg(hb_fd));
goto errout;
}
/* See if we should drop cores somewhere odd... */
parameter = hb_fd->llc_ops->get_parameter(hb_fd, KEY_COREROOTDIR);
if (parameter) {
cl_set_corerootdir(parameter);
}
cl_cdtocoredir();
if((global_info = (ccm_info_t *)g_malloc(sizeof(ccm_info_t))) == NULL){
ccm_log(LOG_ERR, "Cannot allocate memory ");
goto errout;
}
memset(global_info, 0, sizeof(ccm_info_t));
if((ccmret = (ccm_t *)g_malloc(sizeof(ccm_t))) == NULL){
ccm_log(LOG_ERR, "Cannot allocate memory");
goto errout;
}
if((hname = hb_fd->llc_ops->get_mynodeid(hb_fd)) == NULL) {
ccm_log(LOG_ERR, "get_mynodeid() failed");
goto errout;
}
ccm_log(LOG_INFO, "Hostname: %s", hname);
if (hb_fd->llc_ops->set_ifstatus_callback(hb_fd, LinkStatus, NULL)
!=HA_OK){
ccm_log(LOG_ERR, "Cannot set if status callback");
ccm_log(LOG_ERR, "REASON: %s", hb_fd->llc_ops->errmsg(hb_fd));
goto errout;
}
fmask = LLC_FILTER_DEFAULT;
if (hb_fd->llc_ops->setfmode(hb_fd, fmask) != HA_OK) {
ccm_log(LOG_ERR, "Cannot set filter mode");
ccm_log(LOG_ERR, "REASON: %s", hb_fd->llc_ops->errmsg(hb_fd));
goto errout;
}
/* we'll benefit from a bigger queue length on heartbeat side.
* Otherwise, if peers send messages faster than we can consume
* them right now, heartbeat messaging layer will kick us out once
* it's (small) default queue fills up :(
* If we fail to adjust the sendq length, that's not yet fatal, though.
*/
if (HA_OK != hb_fd->llc_ops->set_sendq_len(hb_fd, 1024)) {
ccm_log(LOG_WARNING, "Cannot set sendq length: %s",
hb_fd->llc_ops->errmsg(hb_fd));
}
if (set_llm_from_heartbeat(hb_fd, global_info) != HA_OK){
goto errout;
}
ccm_control_init(global_info);
ccm_configure_timeout(hb_fd, global_info);
ccmret->info = global_info;
ccmret->hbfd = hb_fd;
client_llm_init(&global_info->llm);
ccm_info_saved = global_info;
hb_fd_saved = hb_fd;
return (void*)ccmret;
errout:
if (ccmret){
g_free(ccmret);
ccmret = NULL;
}
if (global_info){
g_free(global_info);
global_info = NULL;
}
return NULL;
}
static void add_change_msg(ccm_info_t *info, const char *node, const char *orig, enum change_event_type type)
{
strlcpy(info->change_node_id, node, sizeof(info->change_node_id));
info->change_type = type;
if(type == NODE_LEAVE){
info->change_event_remaining_count = ccm_get_memcount(info)-1;
}else{
info->change_event_remaining_count = ccm_get_memcount(info);
}
append_change_msg(info, orig);
return;
}
static void append_change_msg(ccm_info_t *info, const char *node)
{
if (CCM_GET_RECEIVED_CHANGE_MSG(info, node) == 0){
CCM_SET_RECEIVED_CHANGE_MSG(info, node, 1);
info->change_event_remaining_count--;
}
return;
}
static int received_all_change_msg(ccm_info_t *info)
{
if(info->change_event_remaining_count == 0){
return 1;
}else{
return 0;
}
}
static int is_expected_change_msg(ccm_info_t *info, const char *node,enum change_event_type type)
{
if(strcmp(info->change_node_id, node) == 0){
if(info->change_type == type){
return 1;
}
}
return 0;
}
static void ccm_state_wait_for_mem_list(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime, *cookie, *memlist;
int uptime_list[MAXNODE];
size_t uptime_size = MAXNODE;
uint trans_majorval=0,trans_minorval=0, uptime_val;
uint curr_major, curr_minor;
int repeat;
int quorum;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message from unknown host %s",
__FUNCTION__, orig);
return;
}
if(ccm_msg_type != CCM_TYPE_PROTOVERSION
&& ccm_msg_type != CCM_TYPE_STATE_INFO
&& ccm_msg_type != CCM_TYPE_RESTART) {
if(strncmp(CCM_GET_COOKIE(info),
ha_msg_value(reply, CCM_COOKIE), COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message"
" with unknown cookie, just dropping", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/* drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval,
CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received %s message with "
"a earlier major transition number "
"recv_trans=%d, mytrans=%d",
__FUNCTION__, ccm_type2string(ccm_msg_type), trans_majorval,
CCM_GET_MAJORTRANS(info));
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
}
switch(ccm_msg_type){
case CCM_TYPE_MEM_LIST:
curr_major = CCM_GET_MAJORTRANS(info);
curr_minor = CCM_GET_MINORTRANS(info);
if(curr_major != trans_majorval ||
curr_minor != trans_minorval){
ccm_debug(LOG_WARNING, "%s: "
"received final memlist from master, "
"but transition versions do not match: "
"rejecting the message", __FUNCTION__);
break;
}
if ((memlist = ha_msg_value(reply, CCM_MEMLIST))
== NULL) {
ccm_debug(LOG_WARNING, "%s: no membership list ", __FUNCTION__);
return;
}
if (cl_msg_get_list_int(reply,CCM_UPTIMELIST,
uptime_list, &uptime_size) != HA_OK){
ccm_log(LOG_ERR,"%s: getting uptie_list failed",
__FUNCTION__);
return;
}
if (ha_msg_value_int (reply, CCM_QUORUM, &quorum)==HA_OK){
info->has_quorum = quorum;
}
else {
info->has_quorum = -1;
}
ccm_mem_strfill(info, (const char *)memlist);
CCM_SET_MAJORTRANS(info, curr_major+1);
CCM_RESET_MINORTRANS(info);
if ((cookie = ha_msg_value(reply, CCM_NEWCOOKIE))
!= NULL) {
ccm_debug2(LOG_DEBUG, "%s: leader changed cookie ", __FUNCTION__);
CCM_SET_COOKIE(info, cookie);
}
CCM_SET_CL(info, llm_get_index(&info->llm,orig));
ccm_fill_update_table(info, CCM_GET_UPDATETABLE(info),
uptime_list);
report_mbrs(info);
ccm_set_state(info, CCM_STATE_JOINED, reply);
break;
case CCM_TYPE_TIMEOUT:
if (mem_list_timeout(CCM_TMOUT_GET_U(info))){
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
break;
case CCM_TYPE_JOIN:
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/* update the minor transition number if it is of
* higher value and send a fresh JOIN message
*/
if (trans_minorval < CCM_GET_MINORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: got a join message from %s from earlier "
"transition, restarting", __FUNCTION__, orig);
ccm_all_restart(hb, info, reply);
break;
}
update_reset(CCM_GET_UPDATETABLE(info));
update_add(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info),
orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
break;
case CCM_TYPE_LEAVE:
/* if the dead node is leader, jump to CCM state machine */
if(node_is_leader(info, orig)){
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
return;
}
case CCM_TYPE_ALIVE:
/* We do nothing here because we believe leader
* will deal with this LEAVE message. SPOF?
*/
break;
case CCM_TYPE_PROTOVERSION:
/* leader will handle this message
* we can safely ignore it
*/
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
default:
ccm_log(LOG_ERR, "%s: dropping message "
"of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
}
}
static void
reset_change_info(ccm_info_t *info)
{
llm_info_t *llm = CCM_GET_LLM(info);
unsigned i;
for(i=0; i<llm_get_nodecount(llm); i++) {
llm_set_change(llm, i, FALSE);
}
return;
}
/* */
/* Construct and send mem_list, uptime_list to all members in the partition */
/* */
static void send_mem_list_to_all(ll_cluster_t *hb,
ccm_info_t *info, char *cookie)
{
int i, size, j, tmp, tmp_mem[100];
char *bitmap;
char memlist[MAX_MEMLIST_STRING];
int uptime[MAXNODE];
bitmap_create(&bitmap, MAXNODE);
size = info->memcount;
for (i=0; i<size; i++){
tmp_mem[i] = info->ccm_member[i];
}
for (i=0; i<size; i++){
for(j=0; j<(size-1-i); j++){
if(tmp_mem[j] > tmp_mem[j+1]){
tmp = tmp_mem[j];
tmp_mem[j] = tmp_mem[j+1];
tmp_mem[j+1] = tmp;
}
}
}
for ( i = 0 ; i < size ; i++ ) {
bitmap_mark(info->ccm_member[i],
bitmap, MAXNODE);
uptime[i] = htonl(update_get_uptime(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
tmp_mem[i]));
}
ccm_bitmap2str(bitmap, memlist, MAX_MEMLIST_STRING);
bitmap_delete(bitmap);
ccm_send_to_all(hb, info, memlist, cookie, uptime, size);
return;
}
static void ccm_state_new_node_wait_for_mem_list(enum ccm_type ccm_msg_type,
struct ha_msg *reply,
ll_cluster_t *hb,
ccm_info_t *info)
{
const char *orig, *trans, *uptime, *memlist, *cookie;
int uptime_list[MAXNODE];
size_t uptime_size = MAXNODE;
uint trans_majorval=0,trans_minorval=0, uptime_val;
uint curr_major, curr_minor;
int repeat;
int ret;
int quorum;
if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
ccm_debug(LOG_WARNING, "%s: received message from unknown", __FUNCTION__);
return;
}
if(!llm_is_valid_node(CCM_GET_LLM(info), orig)) {
ccm_debug(LOG_WARNING, "%s: received message from unknown host %s", __FUNCTION__, orig);
return;
}
if(ccm_msg_type != CCM_TYPE_PROTOVERSION
&& ccm_msg_type != CCM_TYPE_STATE_INFO
&& ccm_msg_type != CCM_TYPE_RESTART) {
if(strncmp(CCM_GET_COOKIE(info),
ha_msg_value(reply, CCM_COOKIE), COOKIESIZE) != 0){
ccm_debug(LOG_WARNING, "%s: received message with unknown cookie, just dropping", __FUNCTION__);
return;
}
/* get the major transition version */
if ((trans = ha_msg_value(reply, CCM_MAJORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition major information", __FUNCTION__);
return;
}
trans_majorval = atoi(trans);
/*drop the message if it has lower major transition number */
if (CCM_TRANS_EARLIER(trans_majorval,
CCM_GET_MAJORTRANS(info))) {
ccm_debug(LOG_WARNING, "%s: received"
" %s message with "
"a earlier major transition number "
"recv_trans=%d, mytrans=%d",
__FUNCTION__, ccm_type2string(ccm_msg_type), trans_majorval,
CCM_GET_MAJORTRANS(info));
return;
}
/* get the minor transition version */
if ((trans = ha_msg_value(reply, CCM_MINORTRANS)) == NULL) {
ccm_debug(LOG_WARNING, "%s: no transition minor information", __FUNCTION__);
return;
}
trans_minorval = atoi(trans);
}
switch(ccm_msg_type){
case CCM_TYPE_MEM_LIST:
curr_major = CCM_GET_MAJORTRANS(info);
curr_minor = CCM_GET_MINORTRANS(info);
if(curr_major != trans_majorval ||
curr_minor != trans_minorval){
ccm_debug(LOG_WARNING, "%s: received final memlist from master, "
"but transition versions do not match: "
"rejecting the message", __FUNCTION__);
break;
}
if ((memlist = ha_msg_value(reply, CCM_MEMLIST))
== NULL) {
ccm_debug(LOG_WARNING, "%s: no membership list ", __FUNCTION__);
return;
}
if (cl_msg_get_list_int(reply,CCM_UPTIMELIST,
uptime_list, &uptime_size) != HA_OK){
ccm_log(LOG_ERR,"%s: getting uptie_list failed", __FUNCTION__);
return;
}
ret = ccm_mem_strfill(info, (const char *)memlist);
if (ret != HA_OK){
ccm_log(LOG_ERR, "%s: filling membership from string failed",
__FUNCTION__);
return;
}
ret = ccm_mem_filluptime(info, uptime_list, uptime_size);
if (ret != HA_OK){
ccm_log(LOG_ERR, "%s: filling uptime failed",
__FUNCTION__);
return;
}
if (ha_msg_value_int (reply, CCM_QUORUM, &quorum)==HA_OK){
info->has_quorum = quorum;
}
else {
info->has_quorum = -1;
}
if (i_am_member(info) == FALSE){
version_reset(CCM_GET_VERSION(info));
ccm_set_state(info, CCM_STATE_NONE, reply);
ccm_reset_all_join_request(info);
break;
}
CCM_SET_MAJORTRANS(info, curr_major+1);
CCM_RESET_MINORTRANS(info);
if ((cookie = ha_msg_value(reply, CCM_NEWCOOKIE))
!= NULL) {
ccm_debug2(LOG_DEBUG, "%s: leader changed cookie ", __FUNCTION__);
CCM_SET_COOKIE(info, cookie);
}
CCM_SET_CL(info,llm_get_index(&info->llm, orig));
CCM_SET_JOINED_TRANSITION(info, CCM_GET_MAJORTRANS(info));
ccm_fill_update_table(info,
CCM_GET_UPDATETABLE(info), uptime_list);
ccm_set_state(info, CCM_STATE_JOINED, reply);
report_mbrs(info);
break;
case CCM_TYPE_TIMEOUT:
if (new_node_mem_list_timeout(CCM_TMOUT_GET_U(info))){
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
break;
case CCM_TYPE_JOIN:
/* get the update value */
if ((uptime = ha_msg_value(reply, CCM_UPTIME)) == NULL){
ccm_debug(LOG_WARNING, "%s: no update information", __FUNCTION__);
return;
}
uptime_val = atoi(uptime);
/* update the minor transition number if it is of
* higher value and send a fresh JOIN message
*/
if (trans_minorval < CCM_GET_MINORTRANS(info)) {
ccm_log(LOG_WARNING,
"%s: got a join message from %s from earlier "
"transition, restarting", __FUNCTION__, orig);
ccm_all_restart(hb, info, reply);
break;
}
update_reset(CCM_GET_UPDATETABLE(info));
update_add(CCM_GET_UPDATETABLE(info), CCM_GET_LLM(info),
orig, uptime_val, TRUE);
CCM_SET_MINORTRANS(info, trans_minorval);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
break;
case CCM_TYPE_LEAVE:
/* if the dead node is leader, jump to CCM state machine */
if(node_is_leader(info, orig)){
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
repeat = 0;
while (ccm_send_join(hb, info) != HA_OK) {
if(repeat < REPEAT_TIMES){
ccm_debug(LOG_WARNING,
"%s: failure to send join", __FUNCTION__);
cl_shortsleep();
repeat++;
}else{
break;
}
}
ccm_set_state(info, CCM_STATE_JOINING, reply);
}
case CCM_TYPE_ALIVE:
/* We do nothing here because we believe leader
* will deal with this LEAVE message. SPOF?
*/
break;
case CCM_TYPE_PROTOVERSION:
/* we are waiting for the leader for membership list
* it's ok if someone want to join -- just ignore
* the message and let the leader handl it
*/
break;
case CCM_TYPE_PROTOVERSION_RESP:
break;
case CCM_TYPE_STATE_INFO:
ccm_handle_state_info(hb, info, reply);
break;
case CCM_TYPE_RESTART:
ccm_all_restart(hb, info, reply);
break;
default:
ccm_log(LOG_ERR,"%s: dropping message"
" of type %s. Is this a Byzantine failure?",
__FUNCTION__, ccm_type2string(ccm_msg_type));
/* nothing to do. Just forget the message */
break;
}
}
static void ccm_fill_update_table(ccm_info_t *info,
ccm_update_t *update_table, const void *uptime_list)
{
const int *uptime;
int i;
uptime = (const int *)uptime_list;
UPDATE_SET_NODECOUNT(update_table, info->memcount);
for (i = 0; i< info->memcount; i++){
update_table->update[i].index = info->ccm_member[i];
update_table->update[i].uptime = ntohl(uptime[i]);
}
return;
}
int
jump_to_joining_state(ll_cluster_t *hb,
ccm_info_t *info,
struct ha_msg* msg){
reset_change_info(info);
update_reset(CCM_GET_UPDATETABLE(info));
CCM_INCREMENT_MINORTRANS(info);
if (ccm_send_join(hb, info) != HA_OK){
ccm_log(LOG_ERR, "sending joining message failed");
return HA_FAIL;
}
ccm_set_state(info, CCM_STATE_JOINING, msg);
return HA_OK;
}
state_msg_handler_t state_msg_handler[]={
ccm_state_none,
ccm_state_version_request,
ccm_state_joining,
ccm_state_sent_memlistreq,
ccm_state_memlist_res,
ccm_state_joined,
ccm_state_wait_for_mem_list,
ccm_state_wait_for_change,
ccm_state_new_node_wait_for_mem_list,
};
static void
dump_mbrs(ccm_info_t *info)
{
int i;
const char *nodename;
int leader;
static struct born_s {
int index;
int bornon;
} bornon[MAXNODE];/*avoid making it a
stack variable*/
if(ccm_get_memcount(info)==1){
bornon[0].index = CCM_GET_MEMINDEX(info,0);
bornon[0].bornon = CCM_GET_MAJORTRANS(info);
} else for(i=0; i < ccm_get_memcount(info); i++){
bornon[i].index = CCM_GET_MEMINDEX(info,i);
bornon[i].bornon = update_get_uptime(CCM_GET_UPDATETABLE(info),
CCM_GET_LLM(info),
CCM_GET_MEMINDEX(info,i));
if(bornon[i].bornon==0)
bornon[i].bornon=CCM_GET_MAJORTRANS(info);
}
ccm_debug(LOG_DEBUG,"dump current membership %p", info);
leader = info->ccm_cluster_leader;
ccm_debug(LOG_DEBUG,"\tleader=%s"
, leader < 0 ?"none": info->llm.nodes[leader].nodename);
ccm_debug(LOG_DEBUG,"\ttransition=%d", CCM_GET_MAJORTRANS(info));
ccm_debug(LOG_DEBUG,"\tstatus=%s",state2string(info->state));
ccm_debug(LOG_DEBUG,"\thas_quorum=%d",info->has_quorum);
for (i=0 ; i < ccm_get_memcount(info); i++) {
nodename = llm_get_nodename(CCM_GET_LLM(info),
CCM_GET_MEMINDEX(info,i));
ccm_debug(LOG_DEBUG,"\tnodename=%s bornon=%d", nodename,
bornon[i].bornon);
}
return;
}
void
ccm_on_quorum_changed(void)
{
ccm_debug(LOG_DEBUG,"quorum changed");
if (ccm_info_saved->state != CCM_STATE_JOINED) {
ccm_debug(LOG_DEBUG,"we are not in CCM_STATE_JOINED, ignore");
return;
}
send_mem_list_to_all(hb_fd_saved, ccm_info_saved, ccm_info_saved->ccm_cookie);
report_mbrs(ccm_info_saved);
}
|