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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationRun
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.replicationTests;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derby.drda.NetworkServerControl;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.sql.*;
import java.io.*;
import org.apache.derby.jdbc.ClientDataSourceInterface;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestCase;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.NetworkServerTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Framework to run replication tests.
* Subclass to create specific tests as
* in ReplicationRun_Local and ReplicationRun_Distributed.
*/
public class ReplicationRun extends BaseTestCase
{
/**
* Name of properties file defining the test environment
* and replication tests to be run.
* Located in <CODE>${user.dir}</CODE>
*/
final static String REPLICATIONTEST_PROPFILE = "replicationtest.properties";
final static String REPLICATION_MASTER_TIMED_OUT = "XRE06";
final static String REPLICATION_SLAVE_STARTED_OK = "XRE08";
final static String REPLICATION_DB_NOT_BOOTED = "XRE11";
final static String SLAVE_OPERATION_DENIED_WHILE_CONNECTED = "XRE41";
final static String REPLICATION_SLAVE_SHUTDOWN_OK = "XRE42";
static String testUser = null;
static String userDir = null;
static String userHome = null; // Used when running distributed.
static String dataEncryption = null;
// Set to a legal encryption string to
// create or connect to an encrypted db.
static String masterServerHost = "localhost";
static int masterServerPort = TestConfiguration.getCurrent().getPort(); // .. get current ports..
static String slaveServerHost = "localhost";
static int slaveServerPort = TestConfiguration.getCurrent().getNextAvailablePort();; // .. ..
static String testClientHost = "localhost";
static int slaveReplPort = TestConfiguration.getCurrent().getNextAvailablePort();;
static String masterDatabasePath = null;
static String slaveDatabasePath = null;
static String replicatedDb = "test";
static String bootLoad = ""; // The "test" to run when booting the master database.
static String freezeDB = ""; // Preliminary: need to "manually" freeze db as part of initialization.
static String unFreezeDB = ""; // Preliminary: need to "manually" unfreeze db as part of initialization.
static boolean junitTest = true; // Set to false in replicationtest.properties
// when running distributed using plain class w/main()
static boolean runUnReplicated = false;
static boolean simpleLoad = true;
static int simpleLoadTuples = 1000;
static int tuplesToInsertPerf = 10000;
static int commitFreq = 0; // autocommit
static String masterDbSubPath = "db_master";
static String slaveDbSubPath = "db_slave";
static String replicationTest = "";
static String replicationVerify = "";
static int THREADS = 0; // Number of threads and
static int MINUTES = 0; // minutes a StressMultiTest load should run.
// When ReplicationTestRunStress used as load...
static String sqlLoadInit = "";
final static String networkServerControl = "org.apache.derby.drda.NetworkServerControl";
static String specialTestingJar = null;
// None null if using e.g. your own modified tests.
static String jvmVersion = null;
static String masterJvmVersion = null;
static String slaveJvmVersion = null;
static String derbyVersion = null;
static String derbyMasterVersion = null; // Needed for PoC. Remove when committed.
static String derbySlaveVersion = null; // Needed for PoC. Remove when committed.
static String junit_jar = null; // Path for JUnit jar
static String test_jars = null; // Path for derbyTesting.jar:junit_jar
final static String FS = File.separator;
final static String PS = File.pathSeparator;
static boolean showSysinfo = false;
static long sleepTime = 5000L; // millisecs.
static final String DRIVER_CLASS_NAME = "org.apache.derby.jdbc.ClientDriver";
static final String DB_PROTOCOL="jdbc:derby";
static final String ALL_INTERFACES = "0.0.0.0";
static String LF = null;
static final String remoteShell = "/usr/bin/ssh -x"; // or /usr/bin/ssh ?
Utils util = new Utils();
State state = new State();
static boolean localEnv = false; // true if all hosts have access to
// same filesystem (NFS...)
static String derbyProperties = null;
String classPath = null; // Used in "localhost" testing.
/** A Connection to the master database*/
private Connection masterConn = null;
/** A Connection to the slave database*/
private Connection slaveConn = null;
/** The exception thrown as a result of a startSlave connection attempt */
private volatile Exception startSlaveException = null;
/**
* List of threads that have been started by the tests and not explicitly
* waited for. Wait for these to complete in {@link #tearDown()} so that
* they don't interfere with subsequent test cases.
*/
private ArrayList<Thread> helperThreads = new ArrayList<Thread>();
private String db_uid = null;
private String db_passwd = null;
/**
* Creates a new instance of ReplicationRun
* @param testcaseName Identifying the test.
*/
public ReplicationRun(String testcaseName)
{
super(testcaseName);
LF = System.getProperties().getProperty("line.separator");
}
/**
* Creates a new instance of ReplicationRun running with authentication.
*/
public ReplicationRun( String testcaseName, String user, String password )
{
this( testcaseName );
db_uid = user;
db_passwd = password;
}
/**
* Parent super()
* @throws java.lang.Exception .
*/
protected void setUp() throws Exception
{
super.setUp();
}
/**
* Parent super()
* @throws java.lang.Exception .
*/
protected void tearDown() throws Exception
{
stopServer(jvmVersion, derbyVersion,
slaveServerHost, slaveServerPort);
stopServer(jvmVersion, derbyVersion,
masterServerHost, masterServerPort);
for (Thread t : helperThreads) {
t.join();
}
helperThreads = null;
close(masterConn);
close(slaveConn);
masterConn = null;
slaveConn = null;
startSlaveException = null;
classPath = null;
util = null;
state = null;
super.tearDown();
}
/** Close a connection. */
private static void close(Connection conn) throws SQLException {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
/**
* Run the test. Extra logic in addition to BaseTestCase's similar logic,
* to save derby.log and database files for replication directories if a
* failure happens.
*/
public void runBare() throws Throwable {
try {
super.runBare();
} catch (Throwable running) {
// Copy the master and slave's derby.log file and databases
//
PrintWriter stackOut = null;
try {
String failPath = PrivilegedFileOpsForTests.
getAbsolutePath(getFailureFolder());
stackOut = new PrintWriter(
PrivilegedFileOpsForTests.getFileOutputStream(
new File(failPath, ERRORSTACKTRACEFILE), true));
String[] replPaths = new String[]{masterDbSubPath,
slaveDbSubPath};
for (int i=0; i < 2; i++) {
// Copy the derby.log file.
//
File origLog = new File(replPaths[i], DERBY_LOG);
File newLog = new File(failPath,
replPaths[i] + "-" + DERBY_LOG);
PrivilegedFileOpsForTests.copy(origLog, newLog);
// Copy the database.
//
String dbName = TestConfiguration.getCurrent().
getDefaultDatabaseName();
File dbDir = new File(replPaths[i], dbName );
File newDbDir = new File(failPath,
replPaths[i] + "-" + dbName);
PrivilegedFileOpsForTests.copy(dbDir,newDbDir);
}
} catch (IOException ioe) {
// We need to throw the original exception so if there
// is an exception saving the db or derby.log we will print it
// and additionally try to log it to file.
BaseTestCase.printStackTrace(ioe);
if (stackOut != null) {
stackOut.println("Copying db_slave/db_master's " +
DERBY_LOG + " or database failed:");
ioe.printStackTrace(stackOut);
stackOut.println();
}
} finally {
if (stackOut != null) {
stackOut.close();
}
// Let JUnit take over
throw running;
}
}
}
String useEncryption(boolean create)
{
String encryptionString = "";
if ( dataEncryption != null)
{
if ( create ) encryptionString = ";dataEncryption=true";
encryptionString = encryptionString+";"+dataEncryption;
}
return encryptionString;
}
//////////////////////////////////////////////////////////////
////
//// The replication test framework (testReplication()):
//// a) "clean" replication run starting master and slave servers,
//// preparing master and slave databases,
//// starting and stopping replication and doing
//// failover for a "normal"/"failure free" replication
//// test run.
//// b) Running (positive and negative) tests at the various states
//// of replication to test what is and is not accepted compared to
//// the functional specification.
//// c) Adding additional load on master and slave servers in
//// different states of replication.
////
//////////////////////////////////////////////////////////////
/* Template
public void testReplication()
throws Exception
{
util.DEBUG("WARNING: Define in subclass of ReplicationRun. "
+ "See ReplicationRun_Local for an example.");
}
*/
void connectPing(String fullDbPath,
String serverHost, int serverPort,
String testClientHost)
throws Exception
{
String dbURL = serverURL( fullDbPath, serverHost, serverPort );
Connection conn = null;
String lastmsg = null;
long sleeptime = 200L;
boolean done = false;
int count = 0;
while ( !done )
{
try
{
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
conn = DriverManager.getConnection(dbURL);
done = true;
util.DEBUG("Ping Got connection after "
+ count +" * "+ sleeptime + " ms.");
conn.close();
}
catch ( SQLException se )
{
int errCode = se.getErrorCode();
lastmsg = se.getMessage();
String sState = se.getSQLState();
String expectedState = "08004";
lastmsg = errCode + " " + sState + " " + lastmsg
+ ". Expected: "+ expectedState;
util.DEBUG("Got SQLException: " + lastmsg);
if ( (errCode == 40000)
&& (sState.equalsIgnoreCase(expectedState) ) )
{
if (count++ >= 600) {
// Have tried 600 * 200 ms == 2 minutes without
// success, so give up now.
fail("Failover did not succeed", se);
}
util.DEBUG("Failover not complete.");
Thread.sleep(sleeptime); // ms.
}
else
{
fail("Connect failed", se);
}
}
}
}
String showCurrentState(String ID, long waitTime,
String fullDbPath,
String serverHost, int serverPort)
throws Exception
{
int errCode = 0;
String sState = "CONNECTED";
String msg = null;
Thread.sleep(waitTime); // .... until stable...
try
{
ClientDataSourceInterface ds = configureDataSource(
fullDbPath,
serverHost,
serverPort,
useEncryption(false) );
Connection conn = ds.getConnection();
conn.close();
}
catch ( SQLException se )
{
errCode = se.getErrorCode();
msg = se.getMessage();
sState = se.getSQLState();
}
util.DEBUG(ID+": ["+serverHost+":"+serverPort+"/"+fullDbPath+"] "
+ errCode + " " + sState + " " + msg);
return sState;
}
void waitForConnect(long sleepTime, int tries,
String fullDbPath,
String serverHost, int serverPort)
throws Exception
{
int count = 0;
String msg = null;
while (true)
{
try
{
ClientDataSourceInterface ds = configureDataSource(
fullDbPath, serverHost, serverPort, useEncryption(false) );
Connection conn = ds.getConnection();
util.DEBUG("Wait Got connection after "
+ (count-1) +" * "+ sleepTime + " ms.");
conn.close();
return;
}
catch ( SQLException se )
{
if (count++ > tries) {
fail("Could not connect in " + (tries * sleepTime) + " ms",
se);
}
msg = se.getErrorCode() + "' '" + se.getSQLState()
+ "' '" + se.getMessage();
util.DEBUG(count + " got '" + msg +"'.");
Thread.sleep(sleepTime); // ms. Sleep and try again...
}
}
}
void waitForSQLState(String expectedState,
long sleepTime, int tries,
String fullDbPath,
String serverHost, int serverPort)
throws Exception
{
int count = 0;
String msg = null;
while (true)
{
try
{
ClientDataSourceInterface ds = configureDataSource(
fullDbPath,
serverHost,
serverPort,
useEncryption(false) );
Connection conn = ds.getConnection();
// Should never get here!
conn.close();
assertTrue("Expected SQLState'"+expectedState
+ "', but got connection!",
false);
}
catch ( SQLException se )
{
int errCode = se.getErrorCode();
msg = se.getMessage();
String sState = se.getSQLState();
msg = "'" + errCode + "' '" + sState + "' '" + msg +"'";
util.DEBUG(count
+ ": SQLState expected '"+expectedState+"'," +
" got " + msg);
if ( sState.equals(expectedState) )
{
util.DEBUG("Reached SQLState '" + expectedState +"' in "
+ (count-1)+"*"+sleepTime + "ms.");
return; // Got desired SQLState.
}
else if (count++ > tries)
{
fail("SQLState '" + expectedState + "' was not reached in "
+ (tries * sleepTime) + " ms", se);
}
else
{
Thread.sleep(sleepTime); // ms. Sleep and try again...
}
}
}
}
void shutdownDb(String jvmVersion, // Not yet used
String serverHost, int serverPort,
String dbPath, String replicatedDb,
String clientHost) // Not yet used
throws Exception
{
String dbURL = serverURL( dbPath+FS+replicatedDb, serverHost, serverPort );
util.DEBUG("**** DriverManager.getConnection(\"" + dbURL+";shutdown=true\");");
try{
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
DriverManager.getConnection(dbURL+";shutdown=true");
fail("Database shutdown should throw exception");
}
catch (SQLException se)
{
BaseJDBCTestCase.assertSQLState("08006", se);
}
}
////////////////////////////////////////////////////////////////
/* Utilities.... */
void startServerMonitor(String slaveHost)
{
util.DEBUG("startServerMonitor(" + slaveHost + ") NOT YET IMPLEMENTED.");
}
void runTest(String replicationTest,
String clientVM,
String testClientHost,
String serverHost, int serverPort,
String dbName)
throws Exception
{
util.DEBUG("runTest(" + replicationTest
+ ", " + clientVM
+ ", " + testClientHost
+ ", " + serverHost
+ ", " + serverPort
+ ", " + dbName
+ ") "
);
if ( replicationTest == null )
{
util.DEBUG("No replicationTest specified. Exitting.");
return;
}
if ( simpleLoad )
{
_testInsertUpdateDeleteOnMaster(serverHost, serverPort,
dbName, simpleLoadTuples);
return;
}
String URL = masterURL(dbName);
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbyTesting.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
String testingClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbynet.jar" // WHY IS THIS NEEDED?
// See TestConfiguration: startNetworkServer and stopNetworkServer
+ PS + test_jars;
String clientJvm = ReplicationRun.getClientJavaExecutableName();
final boolean isRemote = !testClientHost.equals("localhost");
final boolean isIjTest = (replicationTest.indexOf(".sql") >= 0);
ArrayList<String> cmd = new ArrayList<String>();
// For remote tests, we need to specify the Java VM to use and the
// classpath. For local tests, we'll just use the JVM and the classpath
// BaseTestCase.execJavaCmd() gives us. Note that this means we cannot
// vary versions when running locally.
if (isRemote) {
cmd.add(clientJvm);
cmd.add("-classpath");
cmd.add(isIjTest ? ijClassPath : testingClassPath);
}
util.DEBUG("replicationTest: " + replicationTest);
if ( isIjTest )
{
cmd.add("-Dij.driver=" + DRIVER_CLASS_NAME);
cmd.add("-Dij.connection.startTestClient=" + URL);
cmd.add("org.apache.derby.tools.ij");
cmd.add(replicationTest);
}
else
{ // JUnit or plain class w/main().
cmd.add("-Dderby.tests.trace=true");
cmd.add("-Dtest.serverHost=" + serverHost); // Tell the test what server
cmd.add("-Dtest.serverPort=" + serverPort); // and port to connect to.
cmd.add("-Dtest.inserts=" + tuplesToInsertPerf); // for SimplePerfTest
cmd.add("-Dtest.commitFreq=" + commitFreq); // for SimplePerfTest
if (THREADS != 0 && MINUTES != 0) {
// For StressMultiTestForReplLoad as load.
cmd.add("-Dderby.tests.ThreadsMinutes="+THREADS+"x"+MINUTES);
}
cmd.add("-Dtest.dbPath=" + masterDbPath(dbName)); // OK?
if (junitTest) {
cmd.add("junit.textui.TestRunner");
}
cmd.add(replicationTest);
}
String[] command = util.toStringArray(cmd);
long startTime = System.currentTimeMillis();
String results = null;
String workingDir = userHome; // Remember this is run on client against master..
if ( !isRemote )
{
runUserCommandLocally(command, "runTest ", null);
}
else
{
// This doesn't work if path names contain spaces or other
// characters with special meaning to the shell.
// NOT Correct: ...Must be positioned where the properties file
// is located.
results = runUserCommandRemotely(
"cd " + workingDir + ";" + util.splice(command, ' '),
testClientHost, testUser, "runTest ");
}
util.DEBUG("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
}
void runTestOnSlave(String replicationTest,
String clientVM,
String testClientHost,
String serverHost, int serverPort,
String dbName)
throws Exception
{
util.DEBUG("runTestOnSlave(" + replicationTest
+ ", " + clientVM
+ ", " + testClientHost
+ ", " + serverHost
+ ", " + serverPort
+ ", " + dbName
+ ") "
);
String URL = slaveURL(dbName);
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbyTesting.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
String testingClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbynet.jar" // WHY IS THIS NEEDED?
// See TestConfiguration: startNetworkServer and stopNetworkServer
+ PS + test_jars;
String clientJvm = ReplicationRun.getSlaveJavaExecutableName();
if ( replicationTest == null )
{
util.DEBUG("No replicationTest specified. Exitting.");
return;
}
final boolean isRemote = !serverHost.equals("localhost");
final boolean isIjTest = (replicationTest.indexOf(".sql") >= 0);
ArrayList<String> cmd = new ArrayList<String>();
// For remote tests, we need to specify the Java VM to use and the
// classpath. For local tests, we'll just use the JVM and the classpath
// BaseTestCase.execJavaCmd() gives us. Note that this means we cannot
// vary versions when running locally.
if (isRemote) {
cmd.add(clientJvm);
cmd.add("-classpath");
cmd.add(isIjTest ? ijClassPath : testingClassPath);
}
util.DEBUG("replicationTest: " + replicationTest);
if ( isIjTest )
{
cmd.add("-Dij.driver=" + DRIVER_CLASS_NAME);
cmd.add("-Dij.connection.startTestClient=" + URL);
cmd.add("org.apache.derby.tools.ij");
cmd.add(replicationTest);
}
else
{ // JUnit
cmd.add("-Dderby.tests.trace=true");
cmd.add("-Dtest.serverHost=" + serverHost); // Tell the test what server
cmd.add("-Dtest.serverPort=" + serverPort); // and port to connect to.
cmd.add("-Dtest.inserts=" + tuplesToInsertPerf); // for SimplePerfTest
cmd.add("-Dtest.commitFreq=" + commitFreq); // for SimplePerfTest
cmd.add("-Dtest.dbPath=" + slaveDbPath(dbName)); // OK?
cmd.add("junit.textui.TestRunner");
cmd.add(replicationTest);
}
String[] command = util.toStringArray(cmd);
long startTime = System.currentTimeMillis();
String results = null;
if ( !isRemote )
{
runUserCommandLocally(command, "runTestOnSlave ", null);
}
else
{
// This doesn't work if path names contain spaces or other
// characters with special meaning to the shell.
// Must be positioned where the properties file is located.
results = runUserCommandRemotely(
"cd " + userDir + ";" + util.splice(command, ' '),
testClientHost, testUser, "runTestOnSlave ");
}
util.DEBUG("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
}
/*
*
* Should allow:
* - Run load in separate thread.
*
*/
private void runLoad(String load,
String clientVM,
String testClientHost,
String masterHost, int masterPort,
String dbSubPath) // FIXME? Should we allow extra URL options?
throws Exception
{
util.DEBUG("runLoad(" + load
+ ", " + clientVM
+ ", " + testClientHost
+ ", " + masterHost
+ ", " + masterPort
+ ", " + dbSubPath
+ ") "
);
String URL = masterLoadURL(dbSubPath);
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbyTesting.jar"
// Needed for 'run resource 'createTestProcedures.subsql';' cases?
// Nope? what is 'resource'?
+ PS + derbyVersion +FS+ "derbytools.jar";
String testingClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbynet.jar" // WHY IS THIS NEEDED?
// See TestConfiguration: startNetworkServer and stopNetworkServer
+ PS + test_jars;
String clientJvm = ReplicationRun.getClientJavaExecutableName();
final boolean isRemote = !masterHost.equals("localhost");
final boolean isIjTest = (load.indexOf(".sql") >= 0);
util.DEBUG("load: " + load);
ArrayList<String> cmd = new ArrayList<String>();
// For remote tests, we need to specify the Java VM to use and the
// classpath. For local tests, we'll just use the JVM and the classpath
// BaseTestCase.execJavaCmd() gives us. Note that this means we cannot
// vary versions when running locally.
if (isRemote) {
cmd.add(clientJvm);
cmd.add("-classpath");
cmd.add(isIjTest ? ijClassPath : testingClassPath);
}
if ( isIjTest )
{
cmd.add("-Dij.driver=" + DRIVER_CLASS_NAME);
cmd.add("-Dij.connection.startTestClient=" + URL);
cmd.add("org.apache.derby.tools.ij");
cmd.add(load);
}
else
{
/* BEGIN For junit: */
cmd.add("-Dderby.tests.trace=true");
cmd.add("junit.textui.TestRunner");
cmd.add(load);
/* END */
}
String[] command = util.toStringArray(cmd);
if ( !isRemote )
{
runUserCommandInThreadLocally(command, dbSubPath,
"runLoad["+dbSubPath+"] ");
}
else
{
runUserCommandInThreadRemotely(util.splice(command, ' '),
testClientHost, testUser, "runLoad["+dbSubPath+"] ");
}
}
private void runStateTest(String stateTest,
String clientVM,
String testClientHost,
String masterHost, int masterPort, // serverHost?, serverPort?
String dbSubPath) // FIXME? Should we allow extra URL options?
throws Exception
{
util.DEBUG("runStateTest(" + stateTest
+ ", " + clientVM
+ ", " + testClientHost
+ ", " + masterHost
+ ", " + masterPort
+ ", " + dbSubPath
+ ") "
);
String URL = masterLoadURL(dbSubPath);
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbyTesting.jar"
// Needed for 'run resource 'createTestProcedures.subsql';' cases?
// Nope? what is 'resource'?
+ PS + derbyVersion +FS+ "derbytools.jar";
String testingClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbynet.jar" // WHY IS THIS NEEDED?
// See TestConfiguration: startNetworkServer and stopNetworkServer
+ PS + test_jars;
String clientJvm = ReplicationRun.getClientJavaExecutableName();
String command = null;
if ( masterHost.equals("localhost") )
{ // Use full classpath when running locally. Can not vary server versions!
ijClassPath = classPath;
testingClassPath = classPath;
}
util.DEBUG("stateTest: " + stateTest);
if ( stateTest.indexOf(".sql") >= 0 )
{
command = clientJvm
+ " -Dij.driver=" + DRIVER_CLASS_NAME
+ " -Dij.connection.startTestClient=" + URL
+ " -classpath " + ijClassPath + " org.apache.derby.tools.ij"
+ " " + stateTest
;
}
else
{
/* BEGIN For junit: */
command = "cd "+ userDir +";" // Must be positioned where the properties file is located.
+ clientJvm
+ " -Dderby.tests.trace=true"
// + " -Djava.security.policy=\"<NONE>\"" // Now using noSecurityManager decorator
+ " -classpath " + testingClassPath
+ " junit.textui.TestRunner"
+ " " + stateTest
;
/* END */
}
/* String results = */
runUserCommandRemotely(command,
testClientHost, // masterHost,
testUser,
// dbSubPath,
"runStateTest "); // ["+dbSubPath+"]
}
void bootMasterDatabase(String clientVM,
String dbSubPath,
String dbName,
String masterHost, // Where the command is to be executed.
int masterServerPort, // master server interface accepting client requests
String load)
throws Exception
{
// Should just do a "connect....;create=true" here, instead of copying in initMaster.
String URL = masterURL(dbName)
+";create=true"
+useEncryption(true);
{
util.DEBUG("bootMasterDatabase getConnection("+URL+")");
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
Connection conn = DriverManager.getConnection(URL);
conn.close();
}
// NB! should be done by startMaster. Preliminary needs to freeze db before copying to slave and setting replication mode.
util.DEBUG("************************** DERBY-???? Preliminary needs to freeze db before copying to slave and setting replication mode.");
{
URL = masterURL(dbName);
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
util.DEBUG("bootMasterDatabase getConnection("+URL+")");
Connection conn = DriverManager.getConnection(URL);
Statement s = conn.createStatement();
s.execute("call syscs_util.syscs_freeze_database()");
conn.close();
}
if ( load != null )
{
runLoad(load,
clientVM, // jvmVersion,
testClientHost,
masterServerHost, masterServerPort,
dbSubPath+FS+dbName);
}
util.DEBUG("bootMasterDatabase done.");
}
/**
* Set master db in replication master mode.
*/
void startMaster(String clientVM,
String dbName,
String masterHost, // Where the command is to be executed.
int masterServerPort, // master server interface accepting client requests
String slaveClientInterface, // Will be = slaveReplInterface = slaveHost if only one interface card used.
int slaveServerPort, // masterPort, // Not used since slave don't accept client requests
String slaveReplInterface, // slaveHost,
int slaveReplPort) // slavePort)
throws Exception
{
if ( masterHost.equalsIgnoreCase("localhost") )
{
startMaster_direct(dbName,
masterHost, masterServerPort,
slaveReplInterface, slaveReplPort);
}
else
{
startMaster_ij(dbName,
masterHost,
slaveReplInterface, slaveReplPort);
}
}
private void startMaster_ij(String dbName,
String masterHost, // Where the master db is run.
String slaveReplInterface, // master server interface accepting client requests
int slaveReplPort)
throws Exception
{
String URL = masterURL(dbName)
+";startMaster=true;slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort;
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
if ( masterHost.equals("localhost") )
{ // Use full classpath when running locally. Can not vary server versions!
ijClassPath = classPath;
}
String clientJvm = ReplicationRun.getMasterJavaExecutableName();
String command = clientJvm
+ " -Dij.driver=" + DRIVER_CLASS_NAME
+ " -Dij.connection.startMaster=\"" + URL + "\""
+ " -classpath " + ijClassPath + " org.apache.derby.tools.ij"
+ " " + userHome + FS + "ij_dummy_script.sql"
;
String results =
runUserCommandRemotely(command,
masterHost, // Must be run on the master!
testUser,
"startMaster_ij ");
util.DEBUG(results);
}
private void startMaster_direct(String dbName,
String masterHost, // Where the master db is run.
int masterServerPort, // master server interface accepting client requests
String slaveReplInterface, // slaveHost,
int slaveReplPort)
throws Exception
{
String URL = masterURL(dbName)
+";startMaster=true;slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort;
util.DEBUG("startMaster_direct getConnection("+URL+")");
Connection conn = null;
boolean done = false;
int count = 0;
while ( !done )
{
try
{
/* On 1.5 locking of Drivermanager.class prevents
* using DriverManager.getConnection() concurrently
* in startMaster and startSlave!
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
conn = DriverManager.getConnection(URL);
*/
String connectionAttributes = "startMaster=true"
+";slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort
+useEncryption(false);
ClientDataSourceInterface ds = configureDataSource
( masterDbPath( dbName ), masterHost, masterServerPort, connectionAttributes );
conn = ds.getConnection();
done = true;
conn.close();
util.DEBUG("startMaster_direct connected in " + count + " * 100ms.");
}
catch ( SQLException se )
{
int errCode = se.getErrorCode();
String msg = se.getMessage();
String sState = se.getSQLState();
String expectedState = "XRE04";
util.DEBUG("startMaster Got SQLException: "
+ errCode + " " + sState + " " + msg + ". Expected " + expectedState);
if ( (errCode == 40000)
&& (sState.equalsIgnoreCase(expectedState) ) )
{
if (count++ > 1200) {
// Have tried for 1200 * 100 ms == 2 minutes
// without success. Give up.
fail("startMaster did not succeed", se);
}
util.DEBUG("Not ready to startMaster. "
+"Beware: Will also report "
+ "'... got a fatal error for database '...../<dbname>'"
+ " in master derby.log.");
Thread.sleep(100L); // ms.
}
else
{
if (REPLICATION_MASTER_TIMED_OUT.equals(sState)) // FIXME! CANNOT_START_MASTER_ALREADY_BOOTED
{
util.DEBUG("Master already started?");
}
util.DEBUG("startMaster_direct Got: "
+state+" Expected "+expectedState);
throw se;
}
}
}
util.DEBUG("startMaster_direct exit.");
}
/**
* Get a connection to the master database.
* @return A connection to the master database
*/
protected Connection getMasterConnection() throws SQLException {
if (masterConn == null) {
String url = masterURL(replicatedDb);
masterConn = DriverManager.getConnection(url);
}
return masterConn;
}
/**
* Get a connection to the slave database.
* @return A connection to the slave database
*/
protected Connection getSlaveConnection() throws SQLException {
if (slaveConn == null) {
String url = slaveURL(replicatedDb);
slaveConn = DriverManager.getConnection(url);
}
return slaveConn;
}
/**
* Execute SQL on the master database through a Statement
* @param sql The sql that should be executed on the master database
* @throws java.sql.SQLException thrown if an error occured while
* executing the sql
*/
protected void executeOnMaster(String sql) throws SQLException {
Statement s = getMasterConnection().createStatement();
s.execute(sql);
s.close();
}
/**
* Execute SQL on the slave database through a Statement
* @param sql The sql that should be executed on the slave database
* @throws java.sql.SQLException thrown if an error occured while
* executing the sql
*/
protected void executeOnSlave(String sql) throws SQLException {
Statement s = getSlaveConnection().createStatement();
s.execute(sql);
s.close();
}
/**
* Set slave db in replication slave mode
*/
void startSlave(String clientVM,
String dbName,
String slaveClientInterface, // slaveHost, // Where the command is to be executed.
int slaveServerPort,
String slaveReplInterface,
int slaveReplPort,
String testClientHost)
throws Exception
{
if ( testClientHost.equalsIgnoreCase("localhost") )
{
startSlave_direct(dbName,
slaveClientInterface, slaveServerPort,
slaveReplInterface,slaveReplPort);
}
else
{
startSlave_ij(
dbName,
slaveClientInterface,
slaveReplInterface, slaveReplPort);
}
}
private void startSlave_ij(
String dbName,
String slaveHost, // Where the slave db is run.
String slaveReplInterface, // slaveHost,
int slaveReplPort)
throws Exception
{
String URL = slaveURL(dbName)
+";startSlave=true;slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort;
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
if ( slaveHost.equals("localhost") )
{ // Use full classpath when running locally. Can not vary server versions!
ijClassPath = classPath;
}
String clientJvm = ReplicationRun.getSlaveJavaExecutableName();
String command = clientJvm
+ " -Dij.driver=" + DRIVER_CLASS_NAME
+ " -Dij.connection.startSlave=\"" + URL + "\""
+ " -classpath " + ijClassPath + " org.apache.derby.tools.ij"
+ " " + userHome + FS + "ij_dummy_script.sql"
;
runUserCommandInThreadRemotely(command,
slaveHost, // Run on the slave.
testUser,
"startSlave_ij ");
}
private void startSlave_direct(String dbName,
String slaveHost, // Where the slave db is run.
int slaveServerPort, // slave server interface accepting client requests
String slaveReplInterface,
int slaveReplPort)
throws Exception
{
final String URL = slaveURL(dbName)
+";startSlave=true;slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort;
util.DEBUG("startSlave_direct getConnection("+URL+")");
final String fDbPath = slaveDbPath( dbName );
final String fSlaveHost = slaveHost;
final int fSlaveServerPort = slaveServerPort;
final String fConnAttrs = "startSlave=true"
+";slaveHost="+slaveReplInterface
+";slavePort="+slaveReplPort
+useEncryption(false);
Thread connThread = new Thread(
new Runnable()
{
public void run()
{
startSlaveException = null;
Connection conn = null;
try {
// NB! WIll hang here until startMaster is executed!
/*On 1.5 locking of Drivermanager.class prevents
* using DriverManager.getConnection() concurrently
* in startMaster and startSlave!
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
conn = DriverManager.getConnection(URL);
*/
ClientDataSourceInterface ds = configureDataSource(
fDbPath, fSlaveHost, fSlaveServerPort, fConnAttrs );
conn = ds.getConnection();
conn.close();
}
catch (SQLException se)
{
startSlaveException = se;
}
catch (Exception ex)
{
startSlaveException = ex;
}
}
}
);
connThread.start();
registerThread(connThread);
util.DEBUG("startSlave_direct exit.");
}
void failOver(String jvmVersion,
String dbPath, String dbSubPath, String dbName,
String host, // Where the db is run.
int serverPort,
String testClientHost)
throws Exception
{
if ( host.equalsIgnoreCase("localhost") )
{
failOver_direct(dbName);
}
else
{
failOver_ij(dbName, host, testClientHost);
}
}
private void failOver_ij(
String dbName,
String host, // Where the db is run.
String testClientHost)
throws Exception
{
String URL = masterURL(dbName)
+";failover=true";
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
if ( host.equals("localhost") )
{ // Use full classpath when running locally. Can not vary server versions!
ijClassPath = classPath;
}
String clientJvm = ReplicationRun.getClientJavaExecutableName();
String command = clientJvm
+ " -Dij.driver=" + DRIVER_CLASS_NAME
+ " -Dij.connection.failover=\"" + URL + "\""
+ " -classpath " + ijClassPath + " org.apache.derby.tools.ij"
+ " " + userHome + FS + "ij_dummy_script.sql"
;
// Execute the ij command on the testClientHost as testUser
String results =
runUserCommandRemotely(command,
testClientHost,
testUser,
"failOver_ij ");
util.DEBUG(results);
}
private void failOver_direct(String dbName)
throws Exception
{
String URL = masterURL(dbName)
+";failover=true";
util.DEBUG("failOver_direct getConnection("+URL+")");
try
{
Class.forName(DRIVER_CLASS_NAME); // Needed when running from classes!
DriverManager.getConnection(URL);
}
catch (SQLException se)
{
int errCode = se.getErrorCode();
String msg = se.getMessage();
String sState = se.getSQLState();
String expectedState = "XRE20";
msg = "failOver_direct Got SQLException: "
+ errCode + " " + sState + " " + msg
+ ". Expected: " + expectedState;
util.DEBUG(msg);
BaseJDBCTestCase.assertSQLState(expectedState, se);
}
}
int xFindServerPID(String serverHost, int serverPort)
throws InterruptedException
{
if ( serverHost.equalsIgnoreCase("localhost") )
{ // Assuming we do not need the PID.
return 0;
}
int pid = -1;
String p1 = "ps auxwww"; // "/bin/ps auxwww";
String p2 = " | grep " + serverPort; // /bin/grep
String p3 = " | grep '.NetworkServerControl start -h '"; // /bin/grep
String p4 = ""; // | /bin/grep '/trunk_slave/jars/'"; // Also used for master...
String p5 = " | grep -v grep"; // /bin/grep
String p6 = " | grep -v ssh"; // /bin/grep
String p7 = " | grep -v bash"; // /bin/grep// Assuming always doing remote command (ssh)
String p8 = " | gawk '{ print $2 }'"; // /bin/gawk
String p9 = " | head -1"; // For cases where we also get some error...
String command = p1 + p2 + p3 + p4 + p5 + p6 + p7 /* + p8 */ + ";";
String result = runUserCommandRemotely(
command, serverHost, testUser, "ps");
util.DEBUG("xFindServerPID: '" + result + "'");
// result = result.split(" ")[1]; // Without ' + p8 ' should show full line. But fails with PID less than 10000!
command = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + ";";
result = runUserCommandRemotely(command, serverHost, testUser, "ps");
if ( result == null )
{util.DEBUG("xFindServerPID: Server process not found");return -1;} // Avoid error on parseInt below
util.DEBUG("xFindServerPID: '" + result + "'");
pid = Integer.parseInt(result.trim());
util.DEBUG("xFindServerPID: " + pid);
return pid;
}
void xStopServer(String serverHost, int serverPID)
throws InterruptedException
{
if ( serverPID == -1 || serverPID == 0 )
{util.DEBUG("Illegal PID");return;}
String command = "kill " + serverPID;
runUserCommandRemotely(command,
serverHost,
testUser,
"xStopServer");
}
void verifySlave()
throws Exception
{
util.DEBUG("BEGIN verifySlave "+slaveServerHost+":"
+slaveServerPort+"/"+slaveDbPath( replicatedDb ) );
if ( (replicationTest != null) // If 'replicationTest==null' no table was created/filled
&& simpleLoad )
{
_verifyDatabase(slaveServerHost, slaveServerPort,
slaveDbPath( replicatedDb ),
simpleLoadTuples);
// return;
}
ClientDataSourceInterface ds = configureDataSource
( slaveDbPath( replicatedDb ), slaveServerHost, slaveServerPort, useEncryption(false) );
Connection conn = ds.getConnection();
simpleVerify(conn);
conn.close();
/* BEGIN Distributed repl. tests only */
if ( !slaveServerHost.equalsIgnoreCase("localhost") ){
runSlaveVerificationCLient(jvmVersion,
testClientHost,
replicatedDb,
slaveServerHost, slaveServerPort);}
/* END Distributed repl. tests only */
util.DEBUG("END verifySlave");
}
void verifyMaster()
throws Exception
{
util.DEBUG("BEGIN verifyMaster " + masterServerHost + ":"
+masterServerPort+"/"+masterDbPath( replicatedDb ) );
if ( (replicationTest != null) // If 'replicationTest==null' no table was created/filled
&& simpleLoad )
{
_verifyDatabase(masterServerHost, masterServerPort,
masterDbPath( replicatedDb ),
simpleLoadTuples);
// return;
}
ClientDataSourceInterface ds = configureDataSource
( masterDbPath( replicatedDb ), masterServerHost, masterServerPort, useEncryption(false) );
Connection conn = ds.getConnection();
simpleVerify(conn);
conn.close();
/* BEGIN Distributed repl. tests only */
if ( !masterServerHost.equalsIgnoreCase("localhost") ){
runMasterVerificationCLient(jvmVersion,
testClientHost,
replicatedDb,
masterServerHost, masterServerPort);}
/* END Distributed repl. tests only */
util.DEBUG("END verifyMaster");
}
private void simpleVerify(Connection conn) // Verification code..
throws SQLException
{
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select SCHEMAID, TABLENAME from sys.systables");
while (rs.next())
{
util.DEBUG(rs.getString(1) + " " + rs.getString(2));
}
}
private void runSlaveVerificationCLient(String jvmVersion,
String testClientHost,
String dbName,
String serverHost,
int serverPort)
throws Exception
{
util.DEBUG("runSlaveVerificationCLient");
if ( replicationVerify != null){
runTestOnSlave(replicationVerify,
jvmVersion,
testClientHost,
serverHost,serverPort,
dbName);
}
}
private void runMasterVerificationCLient(String jvmVersion,
String testClientHost,
String dbName,
String serverHost,
int serverPort)
throws Exception
{
util.DEBUG("runMasterVerificationCLient");
if ( replicationVerify != null ){
runTest(replicationVerify,
jvmVersion,
testClientHost,
serverHost,serverPort,
dbName);
}
}
/**
* Run a Java command locally on the test host. The spawned process
* inherits the class path from the main test process.
*
* @param command the arguments to pass to the Java executable
* @param ID an identifier used to prefix debug output
* @param workingDir the directory in which the sub-process should run, or
* {@code null} to run in the same directory as the parent process
*/
private void runUserCommandLocally(
String[] command, String ID, File workingDir) {
util.DEBUG("");
final String debugId = "runUserCommandLocally " + ID + " ";
util.DEBUG(debugId+command);
{
util.DEBUG(debugId + "localCommand: " + Arrays.asList(command));
try
{
Process proc = execJavaCmd(null, null, command, workingDir);
processDEBUGOutput(debugId+"pDo ", proc);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
util.DEBUG(debugId+"--- runUserCommandLocally ");
util.DEBUG("");
}
private String runUserCommandRemotely(String command,
String host,
String testUser,
String id)
{
final String ID= "runUserCommandRemotely "+id+" ";
util.DEBUG(ID+"Execute '"+ command +"' on '"+ host +"'" + " as " + testUser);
String localCommand = remoteShell + " "
+ "-l " + testUser + " " + host + " "
+ command
;
String output = "";
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(localCommand);
output = processOutput(ID, proc);
int exitVal = proc.waitFor();
util.DEBUG("ExitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
return output;
}
/**
* Run a Java command locally on the test host in a separate thread. The
* spawned process inherits the class path from the main test process.
*
* @param command the arguments to pass to the Java executable
* @param dbDir the name of the sub-directory in which the sub-process
* should run, or {@code null} to run in the same directory as the
* parent process
* @param id an identifier used to prefix debug output
*/
private void runUserCommandInThreadLocally(final String[] command,
String dbDir,
String id)
{
util.DEBUG("");
final String ID = "runUserCommandInThread "+id+" ";
util.DEBUG(ID + "Execute '"+ command +"'");
util.DEBUG("+++ "+ID);
util.DEBUG("runUserCommand: " + command );
String workingDirName = System.getProperty("user.dir");
util.DEBUG("user.dir: " + workingDirName);
// If dbDir is specified, start the process in that directory;
// otherwise, inherit working dir from the main test process.
final File workingDir = dbDir == null ?
null : new File(workingDirName, dbDir);
util.DEBUG(ID + "workingDir: " + workingDir);
{
Thread cmdThread = new Thread(
new Runnable()
{
public void run()
{
util.DEBUG(ID+"************** In run().");
runUserCommandLocally(command, ID, workingDir);
util.DEBUG(ID+"************** Done run().");
}
}
);
util.DEBUG(ID+"************** Do .start().");
cmdThread.start();
registerThread(cmdThread);
}
util.DEBUG(ID+"--- ");
util.DEBUG("");
}
// FIXME: NB NB Currently only invoked from startSlave_ij (others unused!)
private void runUserCommandInThreadRemotely(String command,
final String host,
final String testUser,
String id)
{
util.DEBUG("");
final String ID=id+" runUserCommandInThreadRemotely ";
util.DEBUG(ID+"+++ ");
util.DEBUG(ID+"Execute '"+ command +"' on '"+ host +"'");
util.DEBUG(ID + command
+ " @ " + host
+ " as " + testUser);
final String[] envElements = {"CLASS_PATH="+""
, "PATH="+FS+"home"+FS+testUser+FS+"bin:$PATH" // "/../bin" FIXME!!! All such!
};
String workingDirName = System.getProperty("user.home");
util.DEBUG(ID+"user.home: " + workingDirName);
util.DEBUG(ID+"envElements: " + util.splice(envElements, ' '));
util.DEBUG(ID+"workingDir: " + workingDirName);
{
util.DEBUG(ID+"Running command on non-local host "+ host);
String[] shEnvElements = {"CLASS_PATH="+""
, "PATH="+FS+"home"+FS+testUser+FS+"bin:${PATH}"
};
String shellEnv = util.splice(shEnvElements, ';');
util.DEBUG(ID+"shellEnv: " + shellEnv);
// user.home aka workingDirName must be accessible from master,
// slave and client.
final String shellCmd = "cd " + workingDirName + ";pwd;"
+ shellEnv + ";"
+ command;
util.DEBUG(ID+"shellCmd: " + shellCmd);
Thread serverThread = new Thread(
new Runnable()
{
public void run()
{
util.DEBUG(ID+"************** In run().");
runUserCommandRemotely(shellCmd, host, testUser, ID);
util.DEBUG(ID+"************** Done exec().");
}
}
);
util.DEBUG(ID+"************** Do .start(). ");
serverThread.start();
registerThread(serverThread);
}
util.DEBUG(ID+"--- ");
util.DEBUG("");
}
void initEnvironment()
throws IOException
{
util.printDebug = System.getProperty("derby.tests.repltrace", "false")
.equalsIgnoreCase("true");
util.DEBUG("printDebug: " + util.printDebug);
util.DEBUG("*** ReplicationRun.initEnvironment -----------------------------------------");
util.DEBUG("*** Properties -----------------------------------------");
userDir = System.getProperty("user.dir");
util.DEBUG("user.dir: " + userDir);
util.DEBUG("derby.system.home: " + System.getProperty("derby.system.home"));
showSysinfo = true;
util.DEBUG("showSysinfo: " + showSysinfo);
testUser = null;
util.DEBUG("testUser: " + testUser);
masterServerHost = "localhost";
util.DEBUG("masterServerHost: " + masterServerHost);
util.DEBUG("masterServerPort: " + masterServerPort);
slaveServerHost = "localhost";
util.DEBUG("slaveServerHost: " + slaveServerHost);
util.DEBUG("slaveServerPort: " + slaveServerPort);
util.DEBUG("slaveReplPort: " + slaveReplPort);
testClientHost = "localhost";
util.DEBUG("testClientHost: " + testClientHost);
masterDatabasePath = userDir;
util.DEBUG("masterDatabasePath: " + masterDatabasePath);
slaveDatabasePath = userDir;
util.DEBUG("slaveDatabasePath: " + slaveDatabasePath);
replicatedDb = "wombat";
util.DEBUG("replicatedDb: " + replicatedDb);
bootLoad = null;
util.DEBUG("bootLoad: " + bootLoad);
freezeDB = null;
util.DEBUG("freezeDB: " + freezeDB);
unFreezeDB = null;
util.DEBUG("unFreezeDB: " + unFreezeDB);
simpleLoad = System.getProperty("derby.tests.replSimpleLoad", "true")
.equalsIgnoreCase("true");
util.DEBUG("simpleLoad: " + simpleLoad);
/* Done in subclasses
replicationTest = "org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationTestRun";
util.DEBUG("replicationTest: " + replicationTest);
replicationVerify = "org.apache.derbyTesting.functionTests.tests.replicationTests.ReplicationTestRunVerify";
util.DEBUG("replicationVerify: " + replicationVerify);
*/
sqlLoadInit = null;
util.DEBUG("sqlLoadInit: " + sqlLoadInit);
specialTestingJar = null;
util.DEBUG("specialTestingJar: " + specialTestingJar);
jvmVersion = System.getProperty("java.home") +FS+"lib";
util.DEBUG("jvmVersion: " + jvmVersion);
masterJvmVersion = null;
if ( masterJvmVersion == null )
{masterJvmVersion = jvmVersion;}
util.DEBUG("masterJvmVersion: " + masterJvmVersion);
slaveJvmVersion = null;
if ( slaveJvmVersion == null )
{slaveJvmVersion = jvmVersion;}
util.DEBUG("slaveJvmVersion: " + slaveJvmVersion);
classPath = System.getProperty("java.class.path"); util.DEBUG("classPath: " + classPath);
util.DEBUG("derbyVersion: " + derbyVersion);
derbyMasterVersion = null;
if ( derbyMasterVersion == null )
{derbyMasterVersion = derbyVersion;}
util.DEBUG("derbyMasterVersion: " + derbyMasterVersion);
derbySlaveVersion = null;
if ( derbySlaveVersion == null )
{derbySlaveVersion = derbyVersion;}
util.DEBUG("derbySlaveVersion: " + derbySlaveVersion);
String derbyTestingJar = derbyVersion + FS+"derbyTesting.jar";
if ( specialTestingJar != null ) derbyTestingJar = specialTestingJar;
util.DEBUG("derbyTestingJar: " + derbyTestingJar);
junit_jar = derbyVersion + FS+"junit.jar";
util.DEBUG("junit_jar: " + junit_jar);
test_jars = derbyTestingJar
+ PS + junit_jar;
util.DEBUG("test_jars: " + test_jars);
sleepTime = 15000;
util.DEBUG("sleepTime: " + sleepTime);
runUnReplicated = false;
util.DEBUG("runUnReplicated: " + runUnReplicated);
localEnv = false;
util.DEBUG("localEnv: " + localEnv);
derbyProperties =
"derby.infolog.append=true"+LF
+"derby.drda.logConnections=true"+LF
+"derby.drda.traceAll=true"+LF;
util.DEBUG("--------------------------------------------------------");
masterPreRepl = null; // FIXME!
masterPostRepl = null; // FIXME!
slavePreSlave = null; // FIXME!
masterPostSlave = null; // FIXME!
slavePostSlave = null; // FIXME!
util.DEBUG("--------------------------------------------------------");
// for SimplePerfTest
tuplesToInsertPerf = 10000;
commitFreq = 1000; // "0" is autocommit
util.DEBUG("--------------------------------------------------------");
// FIXME! state.initEnvironment(cp);
util.DEBUG("--------------------------------------------------------");
}
void initMaster(String host, String dbName)
throws Exception
{
File masterHome = new File(masterDatabasePath, masterDbSubPath);
File slaveHome = new File(slaveDatabasePath, slaveDbSubPath);
util.DEBUG("initMaster");
/* bootMasterDataBase now does "connect ...;create=true" */
String results = null;
if ( host.equalsIgnoreCase("localhost") || localEnv )
{
if (PrivilegedFileOpsForTests.exists(masterHome)) {
BaseTestCase.assertDirectoryDeleted(masterHome);
}
util.mkDirs(masterHome.getPath()); // Create the directory
// Ditto for slave:
if (PrivilegedFileOpsForTests.exists(slaveHome)) {
BaseTestCase.assertDirectoryDeleted(slaveHome);
}
util.mkDirs(slaveHome.getPath()); // Create the directory
// util.writeToFile(derbyProperties, dir+FS+"derby.properties");
}
else
{
String command = "mkdir -p "+masterDatabasePath+FS+masterDbSubPath+";"
+ " cd "+masterDatabasePath+FS+masterDbSubPath+";"
+ " rm -rf " + dbName + " derby.log;"
+ " rm -f Server*.trace;"
+ " ls -al;"
;
results =
runUserCommandRemotely(command,
host,
testUser,
"initMaster ");
}
util.DEBUG(results);
}
private void removeSlaveDBfiles(String host, String dbName)
throws InterruptedException
{
/*
* PoC:
* cd /home/user/Replication/testing/db_slave
* rm -f test/seg0/*
*/
String command = "cd " + slaveDatabasePath+FS+slaveDbSubPath+";"
+ " rm -f " + dbName + FS + "seg0" + FS + "* ;"
+ " ls -al test test/seg0" // DEBUG
;
String results =
runUserCommandRemotely(command,
host,
testUser,
// dbName, // unneccessary?
"removeSlaveDBfiles ");
util.DEBUG(results);
}
void initSlave(String host, String clientVM, String dbName)
throws Exception
{
util.DEBUG("initSlave");
File slaveHome = new File(slaveDatabasePath, slaveDbSubPath);
File masterHome = new File(masterDatabasePath, masterDbSubPath);
File masterDb = new File(masterHome, dbName);
String results = null;
if ( host.equalsIgnoreCase("localhost") || localEnv )
{
// The slaveDb dir is cleaned by initMaster! NB NB SHOULD THIS BE SO?
// util.cleanDir(slaveDb, true); // true: do delete the db directory itself.
// derby.log etc will be kept.
// Copy (.../master/test) into (.../slave/).
File slaveDb = new File(slaveHome, dbName);
PrivilegedFileOpsForTests.copy(masterDb, slaveDb);
// util.writeToFile(derbyProperties, slaveDir+FS+"derby.properties");
}
else
{
String command = "mkdir -p " + slaveHome.getPath() + ";"
+ " cd " + slaveHome.getPath() +";"
+ " rm -rf " + dbName + " derby.log;"
+ " rm -f Server*.trace;"
+ " scp -r " + masterServerHost + ":" + masterDb.getPath() +"/ .;" // Copying the master DB.
+ " ls -al" // DEBUG
;
results =
runUserCommandRemotely(command,
host,
testUser,
"initSlave ");
}
util.DEBUG(results);
}
// ?? The following should be moved to a separate class, subclass this and
// ?? CompatibilityCombinations
void restartServer(String serverVM, String serverVersion,
String serverHost,
String interfacesToListenOn,
int serverPort,
String dbSubDirPath)
throws Exception
{
stopServer(serverVM, serverVersion,
serverHost, serverPort);
startServer(serverVM, serverVersion,
serverHost,
interfacesToListenOn,
serverPort,
dbSubDirPath); // Distinguishing master/slave
}
void startServer(String serverVM, String serverVersion,
String serverHost,
String interfacesToListenOn,
int serverPort,
String dbSubDirPath)
throws Exception
{
util.DEBUG("");
final String debugId = "startServer@" + serverHost + ":" + serverPort + " ";
util.DEBUG(debugId+"+++ StartServer " + serverVM + " / " + serverVersion);
String serverClassPath = serverVersion + FS+"derby.jar"
+ PS + serverVersion + FS+"derbynet.jar"
+ PS + test_jars; // Required if the test (run on the client)
// defines and uses functions on test classes.
// Example: PADSTRING in StressMultiTest.
final boolean isRemote = !serverHost.equals("localhost");
String workingDirName = masterDatabasePath +FS+ dbSubDirPath;
ArrayList<String> ceArray = new ArrayList<String>();
// For remote tests, we need to specify the Java VM to use and the
// classpath. For local tests, we'll just use the JVM and the classpath
// BaseTestCase.execJavaCmd() gives us. Note that this means we cannot
// vary server versions when running locally.
if (isRemote) {
ceArray.add( ReplicationRun.getMasterJavaExecutableName() );
ceArray.add( "-cp" );
ceArray.add( serverClassPath );
}
ceArray.add( "-Dderby.system.home=" + workingDirName );
ceArray.add( "-Dderby.infolog.append=true" );
//ceArray.add( " -Dderby.language.logStatementText=true" ); // Goes into derby.log: Gets HUGE );
if ( db_uid != null )
{
ceArray.add( "-Dderby.authentication.provider=NATIVE:" + replicatedDb + ":LOCAL" );
}
ceArray.add( networkServerControl );
ceArray.add( "start" );
ceArray.add( "-h" );
ceArray.add( interfacesToListenOn ); // allowedClient
ceArray.add( "-p" );
ceArray.add( String.valueOf( serverPort ) );
ceArray.add( "-noSecurityManager" );
final String[] commandElements = util.toStringArray(ceArray);
if (!isRemote)
{
util.DEBUG(debugId+"Starting server on localhost "+ serverHost);
runUserCommandInThreadLocally(commandElements, null, debugId);
}
else
{
util.DEBUG(debugId+"Starting server on non-local host "+ serverHost);
String fullCmd = util.splice(commandElements, ' ');
runUserCommandInThreadRemotely(
fullCmd, serverHost, testUser, debugId);
}
// Wait for the server to come up in a reasonable time.
pingServer(serverHost, serverPort);
util.DEBUG(debugId+"--- StartServer ");
util.DEBUG("");
}
/*
private NetworkServerControl startServer_direct(String serverHost,
String interfacesToListenOn,
int serverPort,
String fullDbDirPath,
String securityOption) // FIXME? true/false?
throws Exception
{ // Wotk in progress. Not currently used! Only partly tested!
util.DEBUG("startServer_direct " + serverHost
+ " " + interfacesToListenOn + " " + serverPort
+ " " + fullDbDirPath);
assertTrue("Attempt to start server on non-localhost: " + serverHost,
serverHost.equalsIgnoreCase("localhost"));
System.setProperty("derby.system.home", fullDbDirPath);
System.setProperty("user.dir", fullDbDirPath);
NetworkServerControl server = new NetworkServerControl(
InetAddress.getByName(interfacesToListenOn), serverPort);
server.start(null);
pingServer(serverHost, serverPort, 150);
Properties sp = server.getCurrentProperties();
sp.setProperty("noSecurityManager",
securityOption.equalsIgnoreCase("-noSecurityManager")?"true":"false");
// derby.log for both master and slave ends up in masters system!
// Both are run in the same VM! Not a good idea?
return server;
}
*/
void killMaster(String masterServerHost, int masterServerPort)
throws InterruptedException
{
util.DEBUG("killMaster: " + masterServerHost +":" + masterServerPort);
if ( masterServerHost.equals("localhost") )
{
stopServer(masterJvmVersion, derbyMasterVersion,
masterServerHost, masterServerPort);
}
else
{
int pid = xFindServerPID(masterServerHost,masterServerPort);
xStopServer(masterServerHost, pid);
}
}
void killSlave(String slaveServerHost, int slaveServerPort)
throws InterruptedException
{
util.DEBUG("killSlave: " + slaveServerHost +":" + slaveServerPort);
if ( slaveServerHost.equals("localhost") )
{
stopServer(slaveJvmVersion, derbySlaveVersion,
slaveServerHost, slaveServerPort);
}
else
{
int pid = xFindServerPID(slaveServerHost, slaveServerPort);
xStopServer(slaveServerHost, pid);
}
}
void destroySlaveDB(String slaveServerHost)
throws InterruptedException
{
removeSlaveDBfiles(slaveServerHost, replicatedDb);
}
void stopServer(String serverVM, String serverVersion,
String serverHost, int serverPort)
{
util.DEBUG("");
final String debugId = "stopServer@" + serverHost + ":" + serverPort + " ";
util.DEBUG("+++ stopServer " + serverVM + " / " + serverVersion
+ " " + debugId);
String serverJvm = ReplicationRun.getServerJavaExecutableName(serverHost,serverVM);
String serverClassPath = serverVersion + FS+"derby.jar"
+ PS + serverVersion + FS+"derbynet.jar";
final boolean isRemote = !serverHost.equals("localhost");
ArrayList<String> ceArray = new ArrayList<String>();
// For remote tests, we need to specify the Java VM to use and the
// classpath. For local tests, we'll just use the JVM and the classpath
// BaseTestCase.execJavaCmd() gives us. Note that this means we cannot
// vary server versions when running locally.
if (isRemote) {
ceArray.add( serverJvm );
ceArray.add( "-cp" );
ceArray.add( serverClassPath );
}
ceArray.add( "-Dderby.infolog.append=true" );
ceArray.add( networkServerControl );
ceArray.add( "shutdown" );
ceArray.add( "-h" );
ceArray.add( serverHost ); // FIXME! interfacesToListenOn
ceArray.add( "-p" );
ceArray.add( String.valueOf(serverPort ) );
if ( db_uid != null )
{
ceArray.add( "-user" );
ceArray.add( db_uid );
ceArray.add( "-password" );
ceArray.add( db_passwd );
}
final String[] commandElements = util.toStringArray(ceArray);
final String fullCmd = util.splice(commandElements, ' ');
util.DEBUG(debugId+"commandElements: " + fullCmd);
final boolean serverOnLocalhost =
serverHost.equalsIgnoreCase("localhost");
if (serverOnLocalhost)
{
util.DEBUG(debugId+"Stopping server on localhost "+ serverHost);
runUserCommandLocally(commandElements, debugId, null);
}
else
{
util.DEBUG(debugId+"Stopping server on non-local host "+ serverHost);
runUserCommandRemotely(fullCmd, serverHost, testUser, debugId);
}
util.DEBUG(debugId+"--- stopServer ");
util.DEBUG("");
}
private String processOutput(String id, Process proc)
throws Exception
{
InputStream serveInputStream = proc.getInputStream();
InputStream serveErrorStream = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(serveInputStream);
InputStreamReader esr = new InputStreamReader(serveErrorStream);
BufferedReader bir = new BufferedReader(isr);
BufferedReader ber = new BufferedReader(esr);
String line=null;
String result = null;
util.DEBUG(id+"---- out:");
// if ( bir.readLine() != null ) {result = id+" ---- out:";}
while ( (line = bir.readLine()) != null)
{
util.DEBUG(id+line);
result = result + LF + line;
}
util.DEBUG(id+"---- err:");
// if ( ber.readLine() != null ) {result = result + LF + id+" ---- err:";}
while ( (line = ber.readLine()) != null)
{
util.DEBUG(id+line);
result = result + LF + line;
}
util.DEBUG(id+"---- ");
// result = result + LF + id+" ----";
return result;
}
private void processDEBUGOutput(String id, Process proc)
throws Exception
{
InputStream serveInputStream = proc.getInputStream();
InputStream serveErrorStream = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(serveInputStream);
InputStreamReader esr = new InputStreamReader(serveErrorStream);
BufferedReader bir = new BufferedReader(isr);
BufferedReader ber = new BufferedReader(esr);
String line=null;
util.DEBUG(id+"---- out:");
while ( (line = bir.readLine()) != null)
{
util.DEBUG(id+line);
}
util.DEBUG(id+"---- err:");
while ( (line = ber.readLine()) != null)
{
util.DEBUG(id+line);
}
util.DEBUG(id+"---- ");
int exitCode = proc.waitFor();
util.DEBUG(id + "process exit status: " + exitCode);
}
/**
* Register that a thread has been started so that we can wait for it to
* complete in {@link #tearDown()}.
*
* @param thread a thread that has been started
*/
private void registerThread(Thread thread) {
helperThreads.add(thread);
}
private void pingServer( String hostName, int port)
throws Exception
{
util.DEBUG("+++ pingServer: " + hostName +":" + port);
NetworkServerControl controller =
new NetworkServerControl(InetAddress.getByName(hostName), port);
assertTrue("Server did not start in time",
NetworkServerTestSetup.pingForServerStart(controller));
util.DEBUG("--- pingServer: " + hostName +":" + port);
}
void startOptionalLoad(Load load,
String dbSubPath,
String serverHost,
int serverPort)
throws Exception
{
String loadString = load.load;
String database = load.database;
boolean existingDB = load.existingDB;
String testClientHost = load.clientHost;
util.DEBUG("run load " + loadString
+ " on client " + testClientHost
+ " against server " + serverHost + ":" + serverPort
+ " using DB " + database + "["+existingDB+"]"
);
if ( loadString == null )
{
util.DEBUG("No load supplied!");
return;
}
if ( !existingDB )
{
// Create it!
String URL = masterURL(database)
+";create=true"; // Creating! No need for encryption here?
String ijClassPath = derbyVersion +FS+ "derbyclient.jar"
+ PS + derbyVersion +FS+ "derbyTesting.jar"
+ PS + derbyVersion +FS+ "derbytools.jar";
if ( serverHost.equals("localhost") )
{ // Use full classpath when running locally. Can not vary server versions!
ijClassPath = classPath;
}
String clientJvm = ReplicationRun.getClientJavaExecutableName();
String command = "rm -rf /"+masterDbPath( database )+";" // FIXME! for slave load!
+ clientJvm // "java"
+ " -Dij.driver=" + DRIVER_CLASS_NAME
+ " -Dij.connection.create"+database+"=\"" + URL + "\""
+ " -classpath " + ijClassPath + " org.apache.derby.tools.ij"
+ " " + sqlLoadInit // FIXME! Should be load specific!
;
String results =
runUserCommandRemotely(command,
testClientHost,
testUser,
"Create_"+database);
}
// Must run in separate thread!:
runLoad(loadString,
jvmVersion,
testClientHost,
serverHost, serverPort,
dbSubPath+FS+database);
// FIXME! How to join and cleanup....
}
void makeReadyForReplication()
throws Exception
{ // Replace the following code in all tests with a call to makeReadyForReplication()!
cleanAllTestHosts();
initEnvironment();
initMaster(masterServerHost,
replicatedDb);
startServer(masterJvmVersion, derbyMasterVersion,
masterServerHost,
ALL_INTERFACES,
masterServerPort,
masterDbSubPath);
startServer(slaveJvmVersion, derbySlaveVersion,
slaveServerHost,
ALL_INTERFACES,
slaveServerPort,
slaveDbSubPath);
startServerMonitor(slaveServerHost);
bootMasterDatabase(jvmVersion,
masterDatabasePath +FS+ masterDbSubPath,
replicatedDb,
masterServerHost,
masterServerPort,
null // bootLoad, // The "test" to start when booting db.
);
initSlave(slaveServerHost,
jvmVersion,
replicatedDb);
startSlave(jvmVersion, replicatedDb,
slaveServerHost,
slaveServerPort,
slaveServerHost,
slaveReplPort,
testClientHost);
startMaster(jvmVersion, replicatedDb,
masterServerHost,
masterServerPort,
masterServerHost,
slaveServerPort,
slaveServerHost,
slaveReplPort);
}
///////////////////////////////////////////////////////////////////////////
/* Remove any servers or tests still running
*/
void cleanAllTestHosts()
{
util.DEBUG("************************** cleanAllTestHosts() Not yet implemented");
}
///////////////////////////////////////////////////////////////////////////
/* The following is used to run tests in the various states of replication
*/
class State
{
String testPreStartedMasterServer = null;
boolean testPreStartedMasterServerReturn = false;
String testPreStartedSlaveServer = null;
boolean testPreStartedSlaveServerReturn = false;
String testPreStartedMaster = null;
boolean testPreStartedMasterReturn = false;
String testPreInitSlave = null;
boolean testPreInitSlaveReturn = false;
String testPreStartedSlave = null;
boolean testPreStartedSlaveReturn = false;
String testPostStartedMasterAndSlave = null;
boolean testPostStartedMasterAndSlaveReturn = false;
String testPreStoppedMaster = null;
boolean testPreStoppedMasterReturn = false;
String testPreStoppedMasterServer = null;
boolean testPreStoppedMasterServerReturn = false;
String testPreStoppedSlave = null;
boolean testPreStoppedSlaveReturn = false;
String testPreStoppedSlaveServer = null;
boolean testPreStoppedSlaveServerReturn = false;
String testPostStoppedSlave = null;
boolean testPostStoppedSlaveReturn = false;
String testPostStoppedSlaveServer = null;
boolean testPostStoppedSlaveServerReturn = false;
void initEnvironment(Properties cp)
{
testPreStartedMasterServer = cp.getProperty("test.PreStartedMasterServer", null);
testPreStartedMasterServerReturn = cp.getProperty("test.PreStartedMasterServer.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStartedMasterServer:"
+ testPreStartedMasterServer + FS
+ testPreStartedMasterServerReturn);
testPreStartedSlaveServer = cp.getProperty("test.PreStartedSlaveServer", null);
testPreStartedSlaveServerReturn = cp.getProperty("test.PreStartedSlaveServer.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStartedSlaveServer:"
+ testPreStartedSlaveServer + FS
+ testPreStartedSlaveServerReturn);
testPreInitSlave = cp.getProperty("test.PreInitSlave", null);
testPreInitSlaveReturn = cp.getProperty("test.PreInitSlave.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreInitSlave:"
+ testPreInitSlave + FS
+ testPreInitSlaveReturn);
testPreStartedMaster = cp.getProperty("test.PreStartedMaster", null);
testPreStartedMasterReturn = cp.getProperty("test.PreStartedMaster.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStartedMaster:"
+ testPreStartedMaster + FS
+ testPreStartedMasterReturn);
testPreStartedSlave = cp.getProperty("test.PreStartedSlave", null);
testPreStartedSlaveReturn = cp.getProperty("test.PreStartedSlave.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStartedSlave:"
+ testPreStartedSlave + FS
+ testPreStartedSlaveReturn);
testPostStartedMasterAndSlave = cp.getProperty("test.PostStartedMasterAndSlave", null);
testPostStartedMasterAndSlaveReturn = cp.getProperty("test.PostStartedMasterAndSlave.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPostStartedMasterAndSlave:"
+ testPostStartedMasterAndSlave + FS
+ testPostStartedMasterAndSlaveReturn);
testPreStoppedMaster = cp.getProperty("test.PreStoppedMaster", null);
testPreStoppedMasterReturn = cp.getProperty("test.PreStoppedMaster.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStoppedMaster:"
+ testPreStoppedMaster + FS
+ testPreStoppedMasterReturn);
testPreStoppedMasterServer = cp.getProperty("test.PreStoppedMasterServer", null);
testPreStoppedMasterServerReturn = cp.getProperty("test.PreStoppedMasterServer.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStoppedMasterServer:"
+ testPreStoppedMasterServer + FS
+ testPreStoppedMasterServerReturn);
testPreStoppedSlave = cp.getProperty("test.PreStoppedSlave", null);
testPreStoppedSlaveReturn = cp.getProperty("test.PreStoppedSlave.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPreStoppedSlave:"
+ testPreStoppedSlave + FS
+ testPreStoppedSlaveReturn);
testPostStoppedSlave = cp.getProperty("test.PostStoppedSlave", null);
testPostStoppedSlaveReturn = cp.getProperty("test.PostStoppedSlave.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPostStoppedSlave:"
+ testPostStoppedSlave + FS
+ testPostStoppedSlaveReturn);
testPostStoppedSlaveServer = cp.getProperty("test.PostStoppedSlaveServer", null);
testPostStoppedSlaveServerReturn = cp.getProperty("test.PostStoppedSlaveServer.return", "false")
.equalsIgnoreCase("true");
util.DEBUG("testPostStoppedSlaveServer:"
+ testPostStoppedSlaveServer + FS
+ testPostStoppedSlaveServerReturn);
}
boolean testPreStartedMasterServer()
throws Exception
{
/*
# Superflueous? Set .test=null for false? test.preStartedMasterServer=true
# Test to run:
test.preStartedMasterServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartMasterCmdTooEarly
# Return from test framework immediatly:
test.preStartedMasterServer.return=true
*/
util.DEBUG("****** BEGIN testPreStartedMasterServer");
if ( testPreStartedMasterServer != null )
{
runStateTest(testPreStartedMasterServer, // E.g. org.apache.derbyTesting.functionTests.tests.replicationTests.TestPreStartedMasterServer
jvmVersion,
testClientHost, // using connect. On masterServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStartedMasterServerReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStartedMasterServer");
return testPreStartedMasterServerReturn;
}
boolean testPreStartedSlaveServer()
throws Exception
{
/*
# test.preStartedSlaveServer=true
test.preStartedSlaveServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartSlaveCmdTooEarly
test.preStartedSlaveServer.return=true
*/
util.DEBUG("****** BEGIN testPreStartedSlaveServer");
if ( testPreStartedSlaveServer != null )
{
runStateTest(testPreStartedSlaveServer, // E.g. org.apache.derbyTesting.functionTests.tests.replicationTests.TestPreStartedSlaveServer
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStartedSlaveServerReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStartedSlaveServer");
return testPreStartedSlaveServerReturn;
}
boolean testPreStartedMaster()
throws Exception
{
/*
# test.preStartedMaster=true
test.preStartedMaster.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartMasterCmd_OK
# test.preStartedMaster.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartMasterCmd_ERR
test.preStartedMaster.return=true
*/
util.DEBUG("****** BEGIN testPreStartedMaster");
if ( testPreStartedMaster != null )
{
runStateTest(testPreStartedMaster, // E.g. org.apache.derbyTesting.functionTests.tests.replicationTests.TestPreStartedMaster
jvmVersion,
testClientHost, // using connect. On masterServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStartedMasterReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStartedMaster");
return testPreStartedMasterReturn;
}
boolean testPreInitSlave()
throws Exception
{
/*
# test.preInitSlave=true
test.preInitSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartSlaveCmd_OK
# test.preInitSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartSlaveCmd_ERR
test.preInitSlave.return=true
*/
util.DEBUG("****** BEGIN testPreInitSlave");
if ( testPreInitSlave != null )
{
runStateTest(testPreInitSlave,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreInitSlaveReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreInitSlave");
return testPreInitSlaveReturn;
}
boolean testPreStartedSlave()
throws Exception
{
/*
# test.preStartedSlave=true
test.preStartedSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartSlaveCmd_OK
# test.preStartedMaster.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StartSlaveCmd_ERR
test.preStartedSlave.return=true
*/
util.DEBUG("****** BEGIN testPreStartedSlave");
if ( testPreStartedSlave != null )
{
runStateTest(testPreStartedSlave,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStartedSlaveReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStartedSlave");
return testPreStartedSlaveReturn;
}
boolean testPostStartedMasterAndSlave()
throws Exception
{
/*
# test.postStartedMasterAndSlave=true
test.postStartedMasterAndSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.XXXX_OK
# test.postStartedMasterAndSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.XXXX_ERR
test.postStartedMasterAndSlave.return=true
*/
util.DEBUG("****** BEGIN testPostStartedMasterAndSlave");
if ( testPostStartedMasterAndSlave != null )
{
// run testPostStartedMasterAndSlave test
runStateTest(testPostStartedMasterAndSlave,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPostStartedMasterAndSlaveReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPostStartedMasterAndSlave");
return testPostStartedMasterAndSlaveReturn;
}
boolean testPreStoppedMaster()
throws Exception
{
/*
# test.preStoppedMaster=true
test.preStoppedMaster.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StopMasterCmd_OK
# test.preStoppedMaster.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StopMasterCmd_ERR
test.preStoppedMaster.return=true
*/
util.DEBUG("****** BEGIN testPreStoppedMaster");
if ( testPreStoppedMaster != null )
{
runStateTest(testPreStoppedMaster,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStoppedMasterReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStoppedMaster");
return testPreStoppedMasterReturn;
}
boolean testPreStoppedMasterServer()
throws Exception
{
/*
# test.preStoppedMasterServer=true
test.preStoppedMasterServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.YYYYY_OK
# test.preStoppedMasterServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.YYYYY_ERR
test.preStoppedMasterServer.return=true
*/
util.DEBUG("****** BEGIN testPreStoppedMasterServer");
if ( testPreStoppedMasterServer != null )
{
runStateTest(testPreStoppedMasterServer,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStoppedMasterServerReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStoppedMasterServer");
return testPreStoppedMasterServerReturn;
}
boolean testPreStoppedSlave()
throws Exception
{
/*
# test.preStoppedSlave=true
test.preStoppedSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StopSlaveCmd_OK
# test.preStoppedSlave.test=org.apache.derbyTesting.functionTests.tests.replicationTests.StopSlaveCmd_ERR
test.preStoppedSlave.return=true
*/
util.DEBUG("****** BEGIN testPreStoppedSlave");
if ( testPreStoppedSlave != null )
{
runStateTest(testPreStoppedSlave,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStoppedSlaveReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStoppedSlave");
return testPreStoppedSlaveReturn;
}
boolean testPreStoppedSlaveServer()
throws Exception
{
/*
# test.preStoppedSlaveServer=true
test.preStoppedSlaveServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.ZZZZZZ_OK
# test.preStoppedSlaveServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.ZZZZZZ_ERR
test.preStoppedSlaveServer.return=true
*/
util.DEBUG("****** BEGIN testPreStoppedSlaveServer");
if ( testPreStoppedSlaveServer != null )
{
runStateTest(testPreStoppedSlaveServer,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPreStoppedSlaveServerReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPreStoppedSlaveServer");
return testPreStoppedSlaveServerReturn;
}
boolean testPostStoppedSlaveServer()
throws Exception
{
/*
# test.postStoppedSlaveServer=true
test.postStoppedSlaveServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.ZZZXXX_OK
# test.postStoppedSlaveServer.test=org.apache.derbyTesting.functionTests.tests.replicationTests.ZZZXXX_ERR
test.postStoppedSlaveServer.return=true
*/
util.DEBUG("****** BEGIN testPostStoppedSlaveServer");
if ( testPostStoppedSlaveServer != null )
{
runStateTest(testPostStoppedSlaveServer,
jvmVersion,
testClientHost, // using connect. On slaveServerHost using CLI
masterServerHost, masterServerPort,
replicatedDb);
}
if ( testPostStoppedSlaveServerReturn ) cleanupAndShutdown();
util.DEBUG("****** END testPostStoppedSlaveServer");
return testPostStoppedSlaveServerReturn;
}
private void cleanupAndShutdown()
{
stopServer(jvmVersion, derbyVersion,
masterServerHost, masterServerPort);
stopServer(jvmVersion, derbyVersion,
slaveServerHost, slaveServerPort);
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/* Load started in different states of replication. */
class Load
{
Load(String id, Properties testRunProperties)
{
util.DEBUG("Load(): " + id);
String pid = "test." + id;
if ( testRunProperties.getProperty(pid,"false").equalsIgnoreCase("false") )
{
util.DEBUG(pid + " Not defined or set to false!");
}
else
{
pid = "test." + id + ".load";
load = testRunProperties.getProperty(pid,
"org.apache.derbyTesting.functionTests.tests.replicationTests.DefaultLoad");
util.DEBUG(pid+": " + load);
pid = "test." + id + ".database";
database = testRunProperties.getProperty(pid, id);
util.DEBUG(pid+": " + database);
pid = "test." + id + ".existingDB";
existingDB = testRunProperties.getProperty(pid,"false").equalsIgnoreCase("true");
util.DEBUG(pid+": " + existingDB);
pid = "test." + id + ".clientHost";
clientHost = testRunProperties.getProperty(pid, testClientHost);
util.DEBUG(pid+": " + clientHost);
}
}
String load = null; // .sql file or junit class
String database = null; // Database name used by load
boolean existingDB = false; // Database already exists.
String clientHost = null; // Host running load client.
}
static Load masterPreRepl;
static Load masterPostRepl;
static Load slavePreSlave;
static Load masterPostSlave;
static Load slavePostSlave;
///////////////////////////////////////////////////////////////////////////
/**
* Assert that the latest startSlave connection attempt got the expected
* SQLState. The method will wait for upto 5 seconds for the startSlave
* connection attemt to complete. If the connection attempt has not
* completed after 5 seconds it is assumed to have failed.
* @param expected the expected SQLState
* @throws java.lang.Exception the Exception to check the SQLState of
*/
protected void assertSqlStateSlaveConn(String expected) throws Exception {
boolean verified = false;
for (int i = 0; i < 10; i++) {
if (startSlaveException != null) {
if (startSlaveException instanceof SQLException) {
BaseJDBCTestCase.
assertSQLState("Unexpexted SQL State",
expected,
(SQLException)startSlaveException);
verified = true;
break;
} else {
throw startSlaveException;
}
} else {
Thread.sleep(500);
}
}
if (!verified) {
fail("Attempt to start slave hangs. Expected SQL state " +
expected);
}
}
void assertException(SQLException se, String expectedSqlState)
{
if (se == null ) // Did not get an exception
{
util.DEBUG("Got 'null' exception, expected '" + expectedSqlState + "'");
assertTrue("Expected exception: " + expectedSqlState + " got: 'null' exception",
expectedSqlState == null);
return;
}
int ec = se.getErrorCode();
String ss = se.getSQLState();
String msg = "Got " + ec + " " + ss + " " + se.getMessage()
+ ". Expected " + expectedSqlState;
util.DEBUG(msg);
if ( expectedSqlState != null ) // We expect an exception
{
assertTrue(msg, ss.equals(expectedSqlState));
}
else // We do not expect an exception, but got one.
{
assertTrue(msg, false);
}
}
void _testInsertUpdateDeleteOnMaster(String serverHost,
int serverPort,
String dbPath,
int _noTuplesToInsert)
throws SQLException, ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException
{
util.DEBUG("_testInsertUpdateDeleteOnMaster: " + serverHost + ":" +
serverPort + "/" + dbPath + " " + _noTuplesToInsert);
ClientDataSourceInterface ds = configureDataSource(
dbPath, serverHost, serverPort, useEncryption(false) );
Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("create table t(i integer primary key, s varchar(64))");
ps.execute();
ps = conn.prepareStatement("insert into t values (?,?)");
for (int i = 0; i< _noTuplesToInsert; i++)
{
ps.setInt(1,i);
ps.setString(2,"dilldall"+i);
ps.execute();
if ( (i % 10000) == 0 ) conn.commit();
}
_verify(conn, _noTuplesToInsert);
conn.close();
}
void _verifyDatabase(String serverHost,
int serverPort,
String dbPath,
int _noTuplesInserted)
throws SQLException, ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException
{
util.DEBUG("_verifyDatabase: "+serverHost+":"+serverPort+"/"+dbPath);
ClientDataSourceInterface ds = configureDataSource(
dbPath, serverHost, serverPort, useEncryption(false) );
Connection conn = ds.getConnection();
_verify(conn,_noTuplesInserted);
conn.close();
}
void _verify(Connection conn, int _noTuplesInserted)
throws SQLException
{
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select count(*) from t");
rs.next();
int count = rs.getInt(1);
rs = s.executeQuery("select max(i) from t");
rs.next();
int max = rs.getInt(1);
util.DEBUG("_verify: " + count + "/" + _noTuplesInserted + " " + max +
"/" + (_noTuplesInserted - 1));
assertEquals("Expected "+ _noTuplesInserted +" tuples, got "+ count +".",
_noTuplesInserted, count);
assertEquals("Expected " +(_noTuplesInserted-1) +" max, got " + max +".",
_noTuplesInserted - 1, max);
}
Connection getConnection(String serverHost, int serverPort,
String databasePath, String dbSubPath, String replicatedDb)
throws SQLException
{
String db = databasePath +FS+dbSubPath +FS+ replicatedDb;
String connectionURL = serverURL( db, serverHost, serverPort );
//String connectionURL = "jdbc:derby:"
// + "//" + serverHost + ":" + serverPort + "/"
// + db;
util.DEBUG(connectionURL);
return DriverManager.getConnection(connectionURL);
}
String masterDbPath(String dbName)
{
return masterDatabasePath+FS+masterDbSubPath+FS+dbName;
}
String slaveDbPath(String dbName)
{
return slaveDatabasePath+FS+slaveDbSubPath+FS+dbName;
}
String masterURL(String dbName)
{
return serverURL( masterDbPath( dbName ), masterServerHost, masterServerPort );
}
String masterLoadURL(String dbSubPath)
{
return serverURL( masterDatabasePath+FS+dbSubPath, masterServerHost, masterServerPort );
}
String slaveURL(String dbName)
{
return serverURL( slaveDbPath( dbName ), slaveServerHost, slaveServerPort );
}
String serverURL( String fullDbPath, String serverHost, int serverPort )
{
return DB_PROTOCOL
+"://"+serverHost
+":"+serverPort+"/"
+fullDbPath
+useEncryption(false)
+credentials();
}
String credentials()
{
if ( db_uid == null ) { return ""; }
else { return ";user=" + db_uid + ";password=" + db_passwd; }
}
SQLException stopSlave(
String slaveServerHost,
int slaveServerPort,
String slaveDatabasePath,
String replicatedDb,
boolean masterServerAlive)
throws Exception
{
return stopSlave(slaveServerHost,
slaveServerPort,
slaveDatabasePath,
ReplicationRun.slaveDbSubPath,
replicatedDb,
masterServerAlive);
}
SQLException stopSlave(
String slaveServerHost,
int slaveServerPort,
String slaveDatabasePath,
String subPath,
String replicatedDb,
boolean masterServerAlive)
throws Exception
{
util.DEBUG("stopSlave");
String dbPath = slaveDatabasePath + FS + subPath + FS + replicatedDb;
String connectionURL = serverURL( dbPath, slaveServerHost, slaveServerPort ) + ";stopSlave=true";
//String connectionURL = "jdbc:derby:"
// + "//" + slaveServerHost + ":" + slaveServerPort + "/"
// + dbPath
// + ";stopSlave=true"
// + useEncryption(false);
if (masterServerAlive) {
try {
Connection conn = DriverManager.getConnection(connectionURL);
conn.close();
return null; // If successful.
} catch (SQLException se) {
return se;
}
} else {
// We use a loop below, to allow for intermediate states before the
// expected final state REPLICATION_DB_NOT_BOOTED.
//
// If we get here quick enough we see these error states (in order):
// a) SLAVE_OPERATION_DENIED_WHILE_CONNECTED
// b) REPLICATION_SLAVE_SHUTDOWN_OK
//
SQLException gotEx = null;
int tries = 20;
while (tries-- > 0) {
gotEx = null;
try {
DriverManager.getConnection(connectionURL);
fail("Unexpectedly connected");
} catch (SQLException se) {
if (se.getSQLState().
equals(SLAVE_OPERATION_DENIED_WHILE_CONNECTED)) {
// Try again, shutdown did not complete yet..
gotEx = se;
util.DEBUG
("got SLAVE_OPERATION_DENIED_WHILE_CONNECTED, " +
"sleep");
Thread.sleep(1000L);
continue;
} else if (se.getSQLState().
equals(REPLICATION_SLAVE_SHUTDOWN_OK)) {
// Try again, shutdown started but did not complete yet.
gotEx = se;
util.DEBUG("got REPLICATION_SLAVE_SHUTDOWN_OK, " +
"sleep..");
Thread.sleep(1000L);
continue;
} else if (se.getSQLState().
equals(REPLICATION_DB_NOT_BOOTED)) {
// All is fine, so proceed
util.DEBUG("Got REPLICATION_DB_NOT_BOOTED as expected");
break;
} else {
// Something else, so report.
gotEx = se;
break;
}
}
}
if (gotEx != null) {
// We did not get what we expected as the final state
// (REPLICATION_DB_NOT_BOOTED) in reasonable time, or we saw
// something that is not a legal intermediate state, so we fail
// now:
throw gotEx;
}
return null;
}
}
private static String getMasterJavaExecutableName()
{
if ( masterServerHost.matches("localhost") )
{
return BaseTestCase.getJavaExecutableName();
}
return masterJvmVersion+FS+".."+FS+"bin"+FS+"java";
}
private static String getSlaveJavaExecutableName()
{
if ( slaveServerHost.matches("localhost") )
{
return BaseTestCase.getJavaExecutableName();
}
return slaveJvmVersion+FS+".."+FS+"bin"+FS+"java";
}
private static String getClientJavaExecutableName()
{
if ( testClientHost.matches("localhost") )
{
return BaseTestCase.getJavaExecutableName();
}
return jvmVersion+FS+".."+FS+"bin"+FS+"java";
}
private static String getServerJavaExecutableName(String serverHost,String serverVM)
{
if ( serverHost.matches("localhost") )
{
return BaseTestCase.getJavaExecutableName();
}
return serverVM+FS+".."+FS+"bin"+FS+"java";
}
/**
* <p>
* Set up a data source.
* </p>
*/
ClientDataSourceInterface configureDataSource
(
String dbName,
String serverHost,
int serverPort,
String connectionAttributes
) throws ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException
{
ClientDataSourceInterface ds;
Class<?> clazz;
if (JDBC.vmSupportsJNDI()) {
clazz = Class.forName("org.apache.derby.jdbc.ClientDataSource");
ds = (ClientDataSourceInterface) clazz.getConstructor().newInstance();
} else {
clazz = Class.forName("org.apache.derby.jdbc.BasicClientDataSource40");
ds = (ClientDataSourceInterface) clazz.getConstructor().newInstance();
}
ds.setDatabaseName( dbName );
ds.setServerName( serverHost );
ds.setPortNumber(serverPort);
ds.setConnectionAttributes( connectionAttributes );
if ( db_uid != null )
{
ds.setUser( db_uid );
ds.setPassword( db_passwd );
}
return ds;
}
}
|