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
|
<?php
/** @noinspection PhpStaticAsDynamicMethodCallInspection */
namespace Wikimedia\Tests\ObjectCache;
use ArrayIterator;
use MediaWikiUnitTestCase;
use Psr\Log\NullLogger;
use UnexpectedValueException;
use Wikimedia\ObjectCache\BagOStuff;
use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \Wikimedia\ObjectCache\WANObjectCache
*/
class WANObjectCacheTest extends MediaWikiUnitTestCase {
/**
* @param array $params
* @return array{WANObjectCache,HashBagOStuff}
*/
private function newWanCache( array $params = [] ) {
if ( isset( $params['broadcastRoutingPrefix'] ) ) {
// Convert mcrouter broadcast keys to regular keys in HashBagOStuff::delete() calls
$bag = new McrouterHashBagOStuff();
} elseif ( isset( $params['serialize'] ) ) {
$bag = new SerialHashBagOStuff();
} else {
$bag = new HashBagOStuff();
}
$cache = new WANObjectCache( [ 'cache' => $bag ] + $params );
return [ $cache, $bag ];
}
/**
* @dataProvider provideSetAndGet
*/
public function testSetAndGet( $value, $ttl ) {
[ $cache ] = $this->newWanCache();
$curTTL = null;
$asOf = null;
$key = $cache->makeKey( 'x', wfRandomString() );
$cache->get( $key, $curTTL, [], $asOf );
$this->assertSame( null, $curTTL, "Current TTL (absent)" );
$this->assertSame( null, $asOf, "Current as-of-time (absent)" );
$t = microtime( true );
$cache->set( $key, $value, $cache::TTL_UNCACHEABLE );
$cache->get( $key, $curTTL, [], $asOf );
$this->assertSame( null, $curTTL, "Current TTL (TTL_UNCACHEABLE)" );
$this->assertSame( null, $asOf, "Current as-of-time (TTL_UNCACHEABLE)" );
$cache->set( $key, $value, $ttl );
$this->assertSame( $value, $cache->get( $key, $curTTL, [], $asOf ) );
if ( $ttl === INF ) {
$this->assertSame( INF, $curTTL, "Current TTL" );
} else {
$this->assertGreaterThan( 0, $curTTL, "Current TTL" );
$this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
}
$this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
$this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
}
public static function provideSetAndGet() {
$a1 = [ 1 ];
$a2 = [ 'a' => &$a1 ];
$o1 = (object)[ 'v' => 1 ];
$o2 = (object)[ 'a' => &$o1 ];
$co = (object)[ 'p' => 93 ];
$co->f =& $co;
return [
// value, ttl
[ 14141, 3 ],
[ 3535.666, 3 ],
[ [], 3 ],
[ '0', 3 ],
[ (object)[ 'meow' ], 3 ],
[ INF, 3 ],
[ '', 3 ],
[ 'pizzacat', INF ],
[ null, 80 ],
[ $a2, 3 ],
[ $o2, 3 ],
[ $co, 3 ]
];
}
public function testGetNotExists() {
[ $cache ] = $this->newWanCache();
$key = $cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
$curTTL = null;
$value = $cache->get( $key, $curTTL );
$this->assertSame( false, $value, "Return value" );
$this->assertSame( null, $curTTL, "current TTL" );
}
public function testSetOver() {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
for ( $i = 0; $i < 3; ++$i ) {
$value = wfRandomString();
$cache->set( $key, $value, 3 );
$this->assertSame( $cache->get( $key ), $value );
}
}
public static function provideStaleSetParams() {
return [
// Given a db transaction (trx lag) that started 30s ago,
// we generally don't want to cache its values.
[ 30, 0.0, false ],
[ 30, 2, false ],
[ 30, 10, false ],
[ 30, 20, false ],
// If the main reason we've hit 30s is that we spent
// a lot of time in the regeneration callback (as opposed
// to time mainly having passed before the cache computation)
// then cache it for at least a little while.
[ 30, 28, true ],
// Also if we don't know, cache it for a little while.
[ 30, null, true ],
];
}
/**
* @dataProvider provideStaleSetParams
* @param int $ago
* @param float|null $walltime
* @param bool $cacheable
*/
public function testStaleSet( $ago, $walltime, $cacheable ) {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$key = wfRandomString();
$value = wfRandomString();
$cache->set(
$key,
$value,
$cache::TTL_MINUTE,
[ 'since' => $mockWallClock - $ago, 'walltime' => $walltime ]
);
$this->assertSame(
$cacheable ? $value : false,
$cache->get( $key ),
"Stale set() value ignored"
);
}
public function testProcessCacheTTL() {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$key = "mykey-" . wfRandomString();
$hits = 0;
$callback = static function ( $oldValue, &$ttl, &$setOpts ) use ( &$hits ) {
++$hits;
return 42;
};
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
$cache->delete( $key, $cache::HOLDOFF_TTL_NONE ); // clear persistent cache
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
$this->assertSame( 1, $hits, "Value process cached" );
$mockWallClock += 6;
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
$this->assertSame( 2, $hits, "Value expired in process cache" );
}
public function testProcessCacheLruAndDelete() {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$hit = 0;
$fn = static function () use ( &$hit ) {
++$hit;
return 42;
};
$keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
$keysB = [ wfRandomString(), wfRandomString(), wfRandomString() ];
$pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
foreach ( $keysA as $i => $key ) {
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 3, $hit, "Values not cached yet" );
foreach ( $keysA as $i => $key ) {
// Should not evict from process cache
$cache->delete( $key );
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 3, $hit, "Values cached; not cleared by delete()" );
foreach ( $keysB as $i => $key ) {
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 6, $hit, "New values not cached yet" );
foreach ( $keysB as $i => $key ) {
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 6, $hit, "New values cached" );
foreach ( $keysA as $i => $key ) {
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 9, $hit, "Prior values evicted by new values" );
}
public function testProcessCacheInterimKeys() {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$hit = 0;
$fn = static function () use ( &$hit ) {
++$hit;
return 42;
};
$keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
$pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
// Tombstone the keys
foreach ( $keysA as $key ) {
$cache->delete( $key );
}
$mockWallClock += 1; // cached values will be newer than tombstone
foreach ( $keysA as $i => $key ) {
// Get into process cache (specific group) and interim cache
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
}
$this->assertSame( 3, $hit );
// Get into process cache (default group)
$key = reset( $keysA );
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
$this->assertSame( 3, $hit, "Value recently interim-cached" );
$mockWallClock += 1; // interim key not brand new
$cache->clearProcessCache();
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
$this->assertSame( 4, $hit, "Value calculated (interim key not recent and reset)" );
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
$this->assertSame( 4, $hit, "Value process cached" );
}
/**
*/
public function testProcessCacheNesting() {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$keyOuter = "outer-" . wfRandomString();
$keyInner = "inner-" . wfRandomString();
$innerHit = 0;
$innerFn = static function () use ( &$innerHit ) {
++$innerHit;
return 42;
};
$outerHit = 0;
$outerFn = static function () use ( $keyInner, $innerFn, $cache, &$outerHit ) {
++$outerHit;
$v = $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
return 43 + $v;
};
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
$this->assertSame( 1, $innerHit, "Inner callback value cached" );
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE );
$mockWallClock += 1;
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
$this->assertSame( 1, $innerHit, "Inner callback process cached" );
// Outer key misses and inner key process cache value is refused
$cache->getWithSetCallback( $keyOuter, 100, $outerFn );
$this->assertSame( 1, $outerHit, "Outer callback value not yet cached" );
$this->assertSame( 2, $innerHit, "Inner callback value process cache skipped" );
$cache->getWithSetCallback( $keyOuter, 100, $outerFn );
$this->assertSame( 1, $outerHit, "Outer callback value cached" );
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE );
$cache->delete( $keyOuter, $cache::HOLDOFF_TTL_NONE );
$mockWallClock += 1;
$cache->clearProcessCache();
$cache->getWithSetCallback( $keyOuter, 100, $outerFn );
$this->assertSame( 2, $outerHit, "Outer callback value not yet cached" );
$this->assertSame( 3, $innerHit, "Inner callback value not yet cached" );
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE );
$mockWallClock += 1;
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
$this->assertSame( 3, $innerHit, "Inner callback value process cached" );
}
/**
* @dataProvider getWithSetCallback_provider
* @param array $extOpts
*/
public function testGetWithSetCallback( array $extOpts ) {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
$value = wfRandomString();
$cKey1 = wfRandomString();
$cKey2 = wfRandomString();
$priorValue = null;
$priorAsOf = null;
$wasSet = 0;
$func = static function ( $old, &$ttl, &$opts, $asOf )
use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) {
++$wasSet;
$priorValue = $old;
$priorAsOf = $asOf;
$ttl = 20; // override with another value
return $value;
};
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$wasSet = 0;
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$this->assertSame( false, $priorValue, "No prior value" );
$this->assertSame( null, $priorAsOf, "No prior value" );
$curTTL = null;
$cache->get( $key, $curTTL );
$this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overridden)' );
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overridden)' );
$wasSet = 0;
$v = $cache->getWithSetCallback(
$key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 0, $wasSet, "Value not regenerated" );
$mockWallClock += 1;
$wasSet = 0;
$v = $cache->getWithSetCallback(
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
);
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated due to check keys" );
$this->assertSame( $value, $priorValue, "Has prior value" );
$this->assertIsFloat( $priorAsOf, "Has prior value" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
$t2 = $cache->getCheckKeyTime( $cKey2 );
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
$mockWallClock += 1; // interim key is not brand new and check keys have past values
$priorTime = $mockWallClock; // reference time
$wasSet = 0;
$v = $cache->getWithSetCallback(
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
);
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated due to still-recent check keys" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
$t2 = $cache->getCheckKeyTime( $cKey2 );
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
$curTTL = null;
$v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
$this->assertSame( $value, $v, "Value returned" );
$this->assertGreaterThan( 0, $curTTL, "Value has current TTL > 0 due to T344191" );
$wasSet = 0;
$key = wfRandomString();
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( $value, $v, "Value returned" );
$cache->delete( $key );
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( $value, $v, "Value still returned after deleted" );
$this->assertSame( 1, $wasSet, "Value process cached while deleted" );
$oldValReceived = -1;
$oldAsOfReceived = -1;
$checkFunc = static function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
++$wasSet;
$oldValReceived = $oldVal;
$oldAsOfReceived = $oldAsOf;
return 'xxx' . $wasSet;
};
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$wasSet = 0;
$key = wfRandomString();
$v = $cache->getWithSetCallback(
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
$this->assertSame( 'xxx1', $v, "Value returned" );
$this->assertSame( false, $oldValReceived, "Callback got no stale value" );
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" );
$mockWallClock += 40;
$v = $cache->getWithSetCallback(
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
$this->assertSame( 'xxx2', $v, "Value still returned after expired" );
$this->assertSame( 2, $wasSet, "Value recalculated while expired" );
$this->assertSame( 'xxx1', $oldValReceived, "Callback got stale value" );
$this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
$mockWallClock += 260;
$v = $cache->getWithSetCallback(
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
$this->assertSame( 'xxx3', $v, "Value still returned after expired" );
$this->assertSame( 3, $wasSet, "Value recalculated while expired" );
$this->assertSame( false, $oldValReceived, "Callback got no stale value" );
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" );
$mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL - 1 );
$wasSet = 0;
$key = wfRandomString();
$checkKey = $cache->makeKey( 'template', 'X' );
$cache->touchCheckKey( $checkKey ); // init check key
$mockWallClock = $priorTime;
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
);
$this->assertSame( 'xxx1', $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value computed" );
$this->assertSame( false, $oldValReceived, "Callback got no stale value" );
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" );
$mockWallClock += $cache::TTL_HOUR; // some time passes
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[
'graceTTL' => $cache::TTL_WEEK,
'checkKeys' => [ $checkKey ],
'ageNew' => -1
] + $extOpts
);
$this->assertSame( 'xxx1', $v, "Cached value returned" );
$this->assertSame( 1, $wasSet, "Cached value returned" );
$cache->touchCheckKey( $checkKey ); // make key stale
$mockWallClock += 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[
'graceTTL' => $cache::TTL_WEEK,
'checkKeys' => [ $checkKey ],
'ageNew' => -1,
] + $extOpts
);
$this->assertSame( 'xxx1', $v, "Value still returned after expired (in grace)" );
$this->assertSame( 1, $wasSet, "Value still returned after expired (in grace)" );
// Chance of refresh increase to unity as staleness approaches graceTTL
$mockWallClock += $cache::TTL_WEEK; // 8 days of being stale
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
);
$this->assertSame( 'xxx2', $v, "Value was recomputed (past grace)" );
$this->assertSame( 2, $wasSet, "Value was recomputed (past grace)" );
$this->assertSame( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
$this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
}
/**
* @dataProvider getWithSetCallback_provider
* @param array $extOpts
*/
public function testGetWithSetCallback_touched( array $extOpts ) {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$checkFunc = static function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
use ( &$wasSet ) {
++$wasSet;
return 'xxx' . $wasSet;
};
$key = wfRandomString();
$wasSet = 0;
$touched = null;
$touchedCallback = static function () use ( &$touched ) {
return $touched;
};
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'touchedCallback' => $touchedCallback ] + $extOpts
);
$mockWallClock += 60;
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'touchedCallback' => $touchedCallback ] + $extOpts
);
$this->assertSame( 'xxx1', $v, "Value was computed once" );
$this->assertSame( 1, $wasSet, "Value was computed once" );
$touched = $mockWallClock - 10;
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'touchedCallback' => $touchedCallback ] + $extOpts
);
$v = $cache->getWithSetCallback(
$key,
$cache::TTL_INDEFINITE,
$checkFunc,
[ 'touchedCallback' => $touchedCallback ] + $extOpts
);
$this->assertSame( 'xxx2', $v, "Value was recomputed once" );
$this->assertSame( 2, $wasSet, "Value was recomputed once" );
}
public static function getWithSetCallback_provider() {
return [
[ [], false ],
[ [ 'version' => 1 ], true ]
];
}
public function testPreemptiveRefresh() {
// (T353180) Flaky test, to fix and re-enable
$this->markTestSkippedIfPhp( '>=', '8.2' );
$value = 'KatCafe';
$wasSet = 0;
$func = static function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
{
++$wasSet;
return $value;
};
$cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$wasSet = 0;
$key = wfRandomString();
$opts = [ 'lowTTL' => 30 ];
$v = $cache->getWithSetCallback( $key, 20, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value calculated" );
$mockWallClock += 1; // interim key is not brand new
$v = $cache->getWithSetCallback( $key, 20, $func, $opts );
$this->assertSame( 2, $wasSet, "Value re-calculated" );
$wasSet = 0;
$key = wfRandomString();
$opts = [ 'lowTTL' => 1 ];
$v = $cache->getWithSetCallback( $key, 30, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value calculated" );
$v = $cache->getWithSetCallback( $key, 30, $func, $opts );
$this->assertSame( 1, $wasSet, "Value cached" );
$asycList = [];
$asyncHandler = static function ( $callback ) use ( &$asycList ) {
$asycList[] = $callback;
};
$cache = new NearExpiringWANObjectCache( [
'cache' => new HashBagOStuff(),
'asyncHandler' => $asyncHandler
] );
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$wasSet = 0;
$key = wfRandomString();
$opts = [ 'lowTTL' => 100 ];
$v = $cache->getWithSetCallback( $key, 300, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value calculated" );
$v = $cache->getWithSetCallback( $key, 300, $func, $opts );
$this->assertSame( 1, $wasSet, "Cached value used" );
$this->assertSame( $v, $value, "Value cached" );
$mockWallClock += 250;
$v = $cache->getWithSetCallback( $key, 300, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Stale value used" );
$this->assertCount( 1, $asycList, "Refresh deferred." );
$value = 'NewCatsInTown'; // change callback return value
$asycList[0](); // run the refresh callback
$asycList = [];
$this->assertSame( 2, $wasSet, "Value calculated at later time" );
$this->assertSame( [], $asycList, "No deferred refreshes added." );
$v = $cache->getWithSetCallback( $key, 300, $func, $opts );
$this->assertSame( $value, $v, "New value stored" );
$cache = new PopularityRefreshingWANObjectCache( [
'cache' => new HashBagOStuff()
] );
$mockWallClock = $priorTime;
$cache->setMockTime( $mockWallClock );
$wasSet = 0;
$key = wfRandomString();
$opts = [ 'hotTTR' => 900 ];
$v = $cache->getWithSetCallback( $key, 60, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value calculated" );
$mockWallClock += 30;
$v = $cache->getWithSetCallback( $key, 60, $func, $opts );
$this->assertSame( 1, $wasSet, "Value cached" );
$mockWallClock = $priorTime;
$wasSet = 0;
$key = wfRandomString();
$opts = [ 'hotTTR' => 10 ];
$v = $cache->getWithSetCallback( $key, 60, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value calculated" );
$mockWallClock += 30;
$v = $cache->getWithSetCallback( $key, 60, $func, $opts );
$this->assertSame( $value, $v, "Value returned" );
$this->assertSame( 2, $wasSet, "Value re-calculated" );
}
/**
* @dataProvider getWithSetCallback_provider
* @param array $extOpts
*/
public function testGetMultiWithSetCallback( array $extOpts ) {
[ $cache ] = $this->newWanCache();
$keyA = wfRandomString();
$keyB = wfRandomString();
$keyC = wfRandomString();
$cKey1 = wfRandomString();
$cKey2 = wfRandomString();
$priorValue = null;
$priorAsOf = null;
$wasSet = 0;
$genFunc = static function ( $id, $old, &$ttl, &$opts, $asOf ) use (
&$wasSet, &$priorValue, &$priorAsOf
) {
++$wasSet;
$priorValue = $old;
$priorAsOf = $asOf;
$ttl = 20; // override with another value
return "@$id$";
};
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$wasSet = 0;
$keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
$value = "@3353$";
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
$this->assertSame( $value, $v[$keyA], "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$this->assertSame( false, $priorValue, "No prior value" );
$this->assertSame( null, $priorAsOf, "No prior value" );
$curTTL = null;
$cache->get( $keyA, $curTTL );
$this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overridden)' );
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overridden)' );
$wasSet = 0;
$value = "@efef$";
$keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
$this->assertSame( $value, $v[$keyB], "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
$this->assertSame( $value, $v[$keyB], "Value returned" );
$this->assertSame( 1, $wasSet, "Value not regenerated" );
$this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
$mockWallClock += 1;
$cache->touchCheckKey( $cKey1 );
$cache->touchCheckKey( $cKey2 );
$wasSet = 0;
$keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
);
$this->assertSame( $value, $v[$keyB], "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated due to check keys" );
$this->assertSame( $value, $priorValue, "Has prior value" );
$this->assertIsFloat( $priorAsOf, "Has prior value" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
$t2 = $cache->getCheckKeyTime( $cKey2 );
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
$mockWallClock += 0.01;
$priorTime = $mockWallClock;
$value = "@43636$";
$wasSet = 0;
$keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
);
$this->assertSame( $value, $v[$keyC], "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated due to still-recent check keys" );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
$t2 = $cache->getCheckKeyTime( $cKey2 );
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
$curTTL = null;
$v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
$this->assertSame( $value, $v, "Value returned" );
$this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
$wasSet = 0;
$key = wfRandomString();
$keyedIds = new ArrayIterator( [ $key => 242424 ] );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
$cache->delete( $key );
$keyedIds = new ArrayIterator( [ $key => 242424 ] );
$v = $cache->getMultiWithSetCallback(
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
$this->assertSame( 1, $wasSet, "Value process cached while deleted" );
$calls = 0;
$ids = [ 1, 2, 3, 4, 5, 6 ];
$keyFunc = static function ( $id, WANObjectCache $wanCache ) {
return $wanCache->makeKey( 'test', $id );
};
$keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
$genFunc = static function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
++$calls;
return "val-{$id}";
};
$values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
$this->assertSame(
[ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
array_values( $values ),
"Correct values in correct order"
);
$this->assertSame(
array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $cache ) ),
array_keys( $values ),
"Correct keys in correct order"
);
$this->assertSame( count( $ids ), $calls );
$cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
$this->assertSame( count( $ids ), $calls, "Values cached" );
// Mock the BagOStuff to assure only one getMulti() call given process caching
$localBag = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'getMulti' ] )->getMock();
$localBag->expects( $this->once() )->method( 'getMulti' )->willReturn( [
'WANCache:v:' . 'k1' => 'val-id1',
'WANCache:v:' . 'k2' => 'val-id2'
] );
$wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
// Warm the process cache
$keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
$this->assertSame(
[ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
$wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
);
// Use the process cache
$this->assertSame(
[ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
$wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
);
}
/**
* @dataProvider getMultiWithSetCallbackRefresh_provider
* @param bool $expiring
* @param bool $popular
* @param array $idsByKey
*/
public function testGetMultiWithSetCallbackRefresh( $expiring, $popular, array $idsByKey ) {
$deferredCbs = [];
$bag = new HashBagOStuff();
$cache = $this->getMockBuilder( WANObjectCache::class )
->onlyMethods( [ 'worthRefreshExpiring', 'worthRefreshPopular' ] )
->setConstructorArgs( [
[
'cache' => $bag,
'asyncHandler' => static function ( $callback ) use ( &$deferredCbs ) {
$deferredCbs[] = $callback;
}
]
] )
->getMock();
$cache->method( 'worthRefreshExpiring' )->willReturn( $expiring );
$cache->method( 'worthRefreshPopular' )->willReturn( $popular );
$wasSet = 0;
$keyedIds = new ArrayIterator( $idsByKey );
$genFunc = static function ( $id, $old, &$ttl, &$opts, $asOf ) use ( &$wasSet ) {
++$wasSet;
$ttl = 20; // override with another value
return "@$id$";
};
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame( count( $idsByKey ), $wasSet, "Initial sets" );
$this->assertSame( [], $deferredCbs, "No deferred callbacks yet" );
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Initial cache value generation" );
}
$wasSet = 0;
$preemptiveRefresh = ( $expiring || $popular );
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame( 0, $wasSet, "No values generated" );
$this->assertCount(
$preemptiveRefresh ? count( $idsByKey ) : 0,
$deferredCbs,
"Deferred callbacks queued"
);
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Cached value reused; refresh scheduled" );
}
// Run the deferred callbacks...
$deferredCbsReady = $deferredCbs;
$deferredCbs = []; // empty by-reference queue
foreach ( $deferredCbsReady as $deferredCb ) {
$deferredCb();
}
$this->assertSame(
( $preemptiveRefresh ? count( $idsByKey ) : 0 ),
$wasSet,
"Deferred callback regenerations"
);
$this->assertSame( [], $deferredCbs, "Deferred callbacks queue empty" );
$wasSet = 0;
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame(
0,
$wasSet,
"Deferred callbacks did not run again"
);
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Cached value OK after deferred refresh run" );
}
}
public static function getMultiWithSetCallbackRefresh_provider() {
return [
[ true, true, [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ] ],
[ true, false, [ 'a' => 'x', 'b' => 'y', 'c' => 'z', 'd' => 'w' ] ],
[ false, true, [ 'a' => 'p', 'b' => 'q', 'c' => 'r', 'd' => 's' ] ],
[ false, false, [ 'a' => '%', 'b' => '^', 'c' => '&', 'd' => 'ç' ] ]
];
}
public static function getMultiWithUnionSetCallback_provider() {
yield 'default' => [ [] ];
yield 'versioned' => [ [ 'version' => 1 ] ];
}
/**
* @dataProvider getMultiWithUnionSetCallback_provider
* @param array $extOpts
*/
public function testGetMultiWithUnionSetCallback( array $extOpts ) {
[ $cache ] = $this->newWanCache();
$wasSet = 0;
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use (
&$wasSet
) {
$wasSet++;
$newValues = [];
foreach ( $ids as $id ) {
$newValues[$id] = "@$id$";
// test that custom TTLs work
$ttls[$id] = 20;
}
return $newValues;
};
$mockWallClock = 1549343530.0;
$t0 = $mockWallClock;
$cache->setMockTime( $mockWallClock );
// A: Basic test case.
// Uses ArrayIterator to emulate makeMultiKeys(), later cases integrate that fully.
$wasSet = 0;
$keyedIds = new ArrayIterator( [ 'keyA' => 'apple' ] );
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc, $extOpts );
$this->assertSame( '@apple$', $v['keyA'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated' );
$curTTL = null;
$cache->get( 'keyA', $curTTL );
$this->assertLessThanOrEqual( 20, $curTTL, 'Custom TTL between 19-20' );
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Custom TTL between 19-20' );
// B: Repeat case.
// Disables lowTTL to avoid random interference.
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyB' => 'bat' ] ),
30,
$genFunc,
[ 'lowTTL' => 0 ] + $extOpts
);
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated' );
$this->assertSame( 0, $cache->getWarmupKeyMisses(), 'Warmup batch covered all fetches' );
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyB' => 'bat' ] ),
30,
$genFunc,
[ 'lowTTL' => 0 ] + $extOpts
);
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' );
$this->assertSame( 0, $wasSet, 'Value not regenerated' );
$this->assertSame( 0, $cache->getWarmupKeyMisses(), 'Warmup batch covered all fetches' );
$mockWallClock += 1;
$t1 = $mockWallClock;
// B: Repeat case with new check keys
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyB' => 'bat' ] ),
30,
$genFunc,
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts
);
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated due to check keys' );
$time = $cache->getCheckKeyTime( 'check1' );
$this->assertGreaterThanOrEqual( $t1, $time, 'Check key 1 was autocreated' );
$time = $cache->getCheckKeyTime( 'check2' );
$this->assertGreaterThanOrEqual( $t1, $time, 'Check key 2 was autocreated' );
$mockWallClock += 1;
$t2 = $mockWallClock;
// C: Repeat case with recently created check keys
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyC' => 'cat' ] ),
30,
$genFunc,
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts
);
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' );
$time = $cache->getCheckKeyTime( 'check1' );
$this->assertLessThanOrEqual( $t1, $time, 'Check key 1 did not change' );
$time = $cache->getCheckKeyTime( 'check2' );
$this->assertLessThanOrEqual( $t1, $time, 'Check key 2 did not change' );
$curTTL = null;
$v = $cache->get( 'keyC', $curTTL, [ 'check1', 'check2' ] );
$this->assertSame( '@cat$', $v, 'Value returned' );
$this->assertGreaterThan( 0, $curTTL, 'No hold-off for new check key (T344191)' );
// Touch one of the check keys so that we have a hold-off period
$mockWallClock += 1;
$cache->touchCheckKey( 'check1' );
$mockWallClock += 1;
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyC' => 'cat' ] ),
30,
$genFunc,
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts
);
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' );
$curTTL = null;
$v = $cache->get( 'keyC', $curTTL, [ 'check1', 'check2' ] );
$this->assertSame( '@cat$', $v, 'Value returned' );
$this->assertLessThanOrEqual( 0, $curTTL, 'Value is expired during hold-off from new check key' );
// While the newly-generated value is considered expired on arrival during the
// hold-off from the check key, it may still be used as valid for a second, until
// the hold-off period is over.
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyC' => 'cat' ] ),
30,
$genFunc,
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts
);
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' );
$this->assertSame( 0, $wasSet, 'Value not regenerated within a second' );
$mockWallClock += 1;
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
new ArrayIterator( [ 'keyC' => 'cat' ] ),
30,
$genFunc,
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts
);
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated due to check key hold-off' );
// D: Process cache should return recently deleted value
$wasSet = 0;
$keyedIds = new ArrayIterator( [ 'keyD' => 'derk' ] );
$v = $cache->getMultiWithUnionSetCallback(
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( '@derk$', $v['keyD'], 'Value returned' );
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' );
$cache->delete( 'keyD' );
$wasSet = 0;
$v = $cache->getMultiWithUnionSetCallback(
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
$this->assertSame( '@derk$', $v['keyD'], 'Value returned from process cache' );
$this->assertSame( 0, $wasSet, 'Value not regenerated' );
$ids = [ 2, 6, 4, 7 ];
$keyedIds = $cache->makeMultiKeys( $ids, static function ( $id, WANObjectCache $cache ) {
return $cache->makeKey( 'test', $id );
} );
$wasSet = 0;
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use ( &$wasSet ) {
$newValues = [];
foreach ( $ids as $id ) {
$wasSet++;
$newValues[$id] = ( $id <= 6 ) ? "val-{$id}" : false;
}
return $newValues;
};
$values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
$this->assertSame( [ 'val-2', 'val-6', 'val-4', false ], array_values( $values ), 'Values in order' );
$this->assertSame(
array_keys( iterator_to_array( $keyedIds ) ),
array_keys( $values ),
'Correct keys in correct order'
);
$this->assertSame( 4, $wasSet, 'Values generated' );
$wasSet = 0;
$cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
$this->assertSame( [ 'val-2', 'val-6', 'val-4', false ], array_values( $values ), 'Values in order' );
$this->assertSame( 1, $wasSet, 'Values not regenerated, except for the missing item 7' );
}
public static function provideCoalesceAndMcrouterSettings() {
return [
[ [ 'coalesceScheme' => 'hash_tag' ], '{' ],
[ [ 'broadcastRoutingPrefix' => '/*/test/', 'coalesceScheme' => 'hash_stop' ], '|#|' ],
];
}
/**
* @dataProvider getMultiWithSetCallbackRefresh_provider
* @param bool $expiring
* @param bool $popular
* @param array $idsByKey
*/
public function testGetMultiWithUnionSetCallbackRefresh( $expiring, $popular, array $idsByKey ) {
$deferredCbs = [];
$bag = new HashBagOStuff();
$cache = $this->getMockBuilder( WANObjectCache::class )
->onlyMethods( [ 'worthRefreshExpiring', 'worthRefreshPopular' ] )
->setConstructorArgs( [
[
'cache' => $bag,
'asyncHandler' => static function ( $callback ) use ( &$deferredCbs ) {
$deferredCbs[] = $callback;
}
]
] )
->getMock();
$cache->method( 'worthRefreshExpiring' )->willReturn( $expiring );
$cache->method( 'worthRefreshPopular' )->willReturn( $popular );
$wasSet = 0;
$keyedIds = new ArrayIterator( $idsByKey );
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use ( &$wasSet ) {
$newValues = [];
foreach ( $ids as $id ) {
++$wasSet;
$newValues[$id] = "@$id$";
$ttls[$id] = 20; // override with another value
}
return $newValues;
};
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame( count( $idsByKey ), $wasSet, "Initial sets" );
$this->assertSame( [], $deferredCbs, "No deferred callbacks yet" );
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Initial cache value generation" );
}
$preemptiveRefresh = ( $expiring || $popular );
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame( count( $idsByKey ), $wasSet, "Deferred callbacks did not run yet" );
$this->assertCount(
$preemptiveRefresh ? count( $idsByKey ) : 0,
$deferredCbs,
"Deferred callbacks queued"
);
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Cached value reused; refresh scheduled" );
}
// Run the deferred callbacks...
$deferredCbsReady = $deferredCbs;
$deferredCbs = []; // empty by-reference queue
foreach ( $deferredCbsReady as $deferredCb ) {
$deferredCb();
}
$this->assertSame(
count( $idsByKey ) * ( $preemptiveRefresh ? 2 : 1 ),
$wasSet,
"Deferred callback regenerations"
);
$this->assertSame( [], $deferredCbs, "Deferred callbacks queue empty" );
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc );
$this->assertSame(
count( $idsByKey ) * ( $preemptiveRefresh ? 2 : 1 ),
$wasSet,
"Deferred callbacks did not run again yet"
);
foreach ( $idsByKey as $key => $id ) {
$this->assertSame( "@$id$", $v[$key], "Cached value OK after deferred refresh run" );
}
}
/**
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testLockTSE( array $params ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$key = wfRandomString();
$value = wfRandomString();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$calls = 0;
$func = static function () use ( &$calls, $value ) {
++$calls;
return $value;
};
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 1, $calls, 'Value was populated' );
// Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
$this->setMutexKey( $bag, $key );
$checkKeys = [ wfRandomString() ]; // new check keys => force misses
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Old value used' );
$this->assertSame( 1, $calls, 'Callback was not used' );
$cache->delete( $key ); // no value at all anymore and still locked
$mockWallClock += 0.001; // cached values will be newer than tombstone
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Callback was used; interim saved' );
$this->assertSame( 2, $calls, 'Callback was used; interim saved' );
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
$this->assertSame( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
}
private function setMutexKey( BagOStuff $bag, $key ) {
// Cover all formats for "coalesceScheme"
$bag->add( "WANCache:$key|#|m", 1 );
$bag->add( "WANCache:{" . $key . "}:m", 1 );
}
private function clearMutexKey( BagOStuff $bag, $key ) {
// Cover all formats for "coalesceScheme"
$bag->delete( "WANCache:$key|#|m" );
$bag->delete( "WANCache:{" . $key . "}:m" );
}
private function setCheckKey( BagOStuff $bag, $key, $time ) {
// Cover all formats for "coalesceScheme"
$bag->set( "WANCache:$key|#|t", "PURGED:$time" );
$bag->set( "WANCache:{" . $key . "}:t", "PURGED:$time" );
}
/**
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testLockTSESlow( array $params ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$key = 'myfirstkey';
$key2 = 'mysecondkey';
$value = 'some_slow_value';
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$calls = 0;
$lastCallOldValue = null;
$func = static function ( $oldValue, &$ttl, &$setOpts ) use (
&$calls, $value, &$mockWallClock, &$lastCallOldValue
) {
++$calls;
$lastCallOldValue = $oldValue;
// Value should be given a low logical TTL due to high snapshot lag
$setOpts['since'] = $mockWallClock;
$mockWallClock += 10;
return $value;
};
$curTTL = null;
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( $value, $cache->get( $key, $curTTL ), 'Value populated' );
$this->assertEqualsWithDelta( 30.0, $curTTL, 0.01, 'Value has reduced logical TTL' );
$this->assertSame( 1, $calls, 'Value was generated' );
$this->assertSame( false, $lastCallOldValue, 'No old value for callback' );
// Just a few seconds after the (reduced) logical TTL expires
$mockWallClock += 32;
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 2, $calls, 'Callback used (stale, mutex acquired, regenerated)' );
$this->assertSame( $value, $lastCallOldValue, 'Old value for callback' );
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5, 'lowTTL' => -1 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 2, $calls, 'Callback not used (extremely new value reused)' );
// Just a few seconds after the (reduced) logical TTL expires
$mockWallClock += 32;
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
$this->setMutexKey( $bag, $key );
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 2, $calls, 'Callback not used (mutex not acquired, stale value used)' );
$mockWallClock += 301; // physical TTL expired
// Acquire a lock to verify that getWithSetCallback uses lockTSE properly
$this->setMutexKey( $bag, $key );
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' );
$calls = 0;
$func2 = static function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
++$calls;
$setOpts['lag'] = 15;
return $value;
};
// Value should be given a low logical TTL due to replication lag
$curTTL = null;
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
$this->assertSame( 30.0, $curTTL, 'Value has reduced logical TTL', 0.01 );
$this->assertSame( 1, $calls, 'Value was generated' );
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 1, $calls, 'Callback was used (not expired)' );
$mockWallClock += 31;
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
$this->assertSame( $value, $ret );
$this->assertSame( 2, $calls, 'Callback was used (mutex acquired)' );
}
/**
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testBusyValueBasic( array $params ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$key = wfRandomString();
$value = wfRandomString();
$busyValue = wfRandomString();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$calls = 0;
$func = static function () use ( &$calls, $value ) {
++$calls;
return $value;
};
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
$this->assertSame( $value, $ret );
$this->assertSame( 1, $calls, 'Value was populated' );
$mockWallClock += 0.2; // interim keys not brand new
// Acquire a lock to verify that getWithSetCallback uses busyValue properly
$this->setMutexKey( $bag, $key );
$checkKeys = [ wfRandomString() ]; // new check keys => force misses
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Callback used' );
$this->assertSame( 2, $calls, 'Callback used' );
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Old value used' );
$this->assertSame( 2, $calls, 'Callback was not used' );
$cache->delete( $key ); // no value at all anymore and still locked
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
$this->assertSame( $busyValue, $ret, 'Callback was not used; used busy value' );
$this->assertSame( 2, $calls, 'Callback was not used; used busy value' );
$this->clearMutexKey( $bag, $key );
$mockWallClock += 0.001; // cached values will be newer than tombstone
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Callback was used; saved interim' );
$this->assertSame( 3, $calls, 'Callback was used; saved interim' );
$this->setMutexKey( $bag, $key );
$ret = $cache->getWithSetCallback( $key, 30, $func,
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
$this->assertSame( $value, $ret, 'Callback was not used; used interim' );
$this->assertSame( 3, $calls, 'Callback was not used; used interim' );
}
public static function getBusyValues_Provider() {
$hash = new HashBagOStuff( [] );
return [
[
static function () {
return "Saint Oliver Plunckett";
},
'Saint Oliver Plunckett'
],
[ 'strlen', 'strlen' ],
[ 'WANObjectCache::newEmpty', 'WANObjectCache::newEmpty' ],
[ [ 'WANObjectCache', 'newEmpty' ], [ 'WANObjectCache', 'newEmpty' ] ],
[ [ $hash, 'getLastError' ], [ $hash, 'getLastError' ] ],
[ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
];
}
/**
* @dataProvider getBusyValues_Provider
*/
public function testBusyValueTypes( $busyValue, $expected ) {
[ $cache, $bag ] = $this->newWanCache();
$key = wfRandomString();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$calls = 0;
$func = static function () use ( &$calls ) {
++$calls;
return 418;
};
// Acquire a lock to verify that getWithSetCallback uses busyValue properly
$this->setMutexKey( $bag, $key );
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
$this->assertSame( $expected, $ret, 'busyValue used as expected' );
$this->assertSame( 0, $calls, 'busyValue was used' );
}
/**
*/
public function testGetMulti() {
[ $cache ] = $this->newWanCache();
$value1 = [ 'this' => 'is', 'a' => 'test' ];
$value2 = [ 'this' => 'is', 'another' => 'test' ];
$key1 = wfRandomString();
$key2 = wfRandomString();
$key3 = wfRandomString();
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
$cache->set( $key1, $value1, 5 );
$cache->set( $key2, $value2, 10 );
$curTTLs = [];
$this->assertSame(
[ $key1 => $value1, $key2 => $value2 ],
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
'Result array populated'
);
$this->assertCount( 2, $curTTLs, "Two current TTLs in array" );
$this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
$this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
$cKey1 = wfRandomString();
$cKey2 = wfRandomString();
$mockWallClock += 1;
$cache->touchCheckKey( $cKey1 );
$cache->touchCheckKey( $cKey2 );
$t1 = $cache->getCheckKeyTime( $cKey1 );
$this->assertSame( $mockWallClock, $t1, 'Check key 1 generated' );
$t2 = $cache->getCheckKeyTime( $cKey2 );
$this->assertSame( $mockWallClock, $t2, 'Check key 2 generated' );
$curTTLs = [];
$this->assertSame(
[ $key1 => $value1, $key2 => $value2 ],
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
"Result array populated even with new check keys"
);
$this->assertCount( 2, $curTTLs, "Current TTLs array set" );
$this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
$this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
$mockWallClock += 1;
$curTTLs = [];
$this->assertSame(
[ $key1 => $value1, $key2 => $value2 ],
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
"Result array still populated even with new check keys"
);
$this->assertCount( 2, $curTTLs, "Current TTLs still array set" );
$this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
$this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
}
/**
* @param array $params
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testGetMultiCheckKeys( array $params ) {
[ $cache ] = $this->newWanCache( $params );
$checkAll = wfRandomString();
$check1 = wfRandomString();
$check2 = wfRandomString();
$check3 = wfRandomString();
$value1 = wfRandomString();
$value2 = wfRandomString();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
// Fake initial check key to be set in the past. Otherwise we'd have to sleep for
// several seconds during the test to assert the behaviour.
foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
$cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_TTL_NONE );
}
$mockWallClock += 0.100;
$cache->set( 'key1', $value1, 10 );
$cache->set( 'key2', $value2, 10 );
$curTTLs = [];
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
] );
$this->assertSame(
[ 'key1' => $value1, 'key2' => $value2 ],
$result,
'Initial values'
);
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
$this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
$this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
$mockWallClock += 0.100;
$cache->touchCheckKey( $check1 );
$curTTLs = [];
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
] );
$this->assertSame(
[ 'key1' => $value1, 'key2' => $value2 ],
$result,
'key1 expired by check1, but value still provided'
);
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
$this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
$cache->touchCheckKey( $checkAll );
$curTTLs = [];
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
'key1' => $check1,
$checkAll,
'key2' => $check2,
'key3' => $check3,
] );
$this->assertSame(
[ 'key1' => $value1, 'key2' => $value2 ],
$result,
'All keys expired by checkAll, but value still provided'
);
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
$this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
}
/**
*/
public function testCheckKeyHoldoff() {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
$checkKey = wfRandomString();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$cache->touchCheckKey( $checkKey, 8 );
$mockWallClock += 1;
$cache->set( $key, 1, 60 );
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
$this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
$mockWallClock += 3;
$cache->set( $key, 1, 60 );
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
$this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
$mockWallClock += 10;
$cache->set( $key, 1, 60 );
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
$this->assertGreaterThan( 0, $curTTL, "Key not in hold-off due to check key" );
}
public function testDelete() {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
$value = wfRandomString();
$cache->set( $key, $value );
$curTTL = null;
$v = $cache->get( $key, $curTTL );
$this->assertSame( $value, $v, "Key was created with value" );
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
$cache->delete( $key );
$curTTL = null;
$v = $cache->get( $key, $curTTL );
$this->assertSame( false, $v, "Deleted key has false value" );
$this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
$cache->set( $key, $value . 'more' );
$v = $cache->get( $key, $curTTL );
$this->assertSame( false, $v, "Deleted key is tombstoned and has false value" );
$this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
$cache->set( $key, $value );
$cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE );
$curTTL = null;
$v = $cache->get( $key, $curTTL );
$this->assertSame( false, $v, "Deleted key has false value" );
$this->assertSame( null, $curTTL, "Deleted key has null current TTL" );
$cache->set( $key, $value );
$v = $cache->get( $key, $curTTL );
$this->assertSame( $value, $v, "Key was created with value" );
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
}
/**
* @dataProvider getWithSetCallback_provider
* @param array $extOpts
* @param bool $versioned
*/
public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
$valueV1 = wfRandomString();
$valueV2 = [ wfRandomString() ];
$wasSet = 0;
$funcV1 = static function () use ( &$wasSet, $valueV1 ) {
++$wasSet;
return $valueV1;
};
$priorValue = false;
$priorAsOf = null;
$funcV2 = static function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
$priorValue = $oldValue;
$priorAsOf = $oldAsOf;
++$wasSet;
return $valueV2; // new array format
};
// Set the main key (version N if versioned)
$wasSet = 0;
$v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
$this->assertSame( $valueV1, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
$this->assertSame( 1, $wasSet, "Value not regenerated" );
$this->assertSame( $valueV1, $v, "Value not regenerated" );
if ( $versioned ) {
// Set the key for version N+1 format
$verOpts = [ 'version' => $extOpts['version'] + 1 ];
} else {
// Start versioning now with the unversioned key still there
$verOpts = [ 'version' => 1 ];
}
// Value goes to secondary key since V1 already used $key
$wasSet = 0;
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
$this->assertSame( $valueV2, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$this->assertSame( false, $priorValue, "Old value not given due to old format" );
$this->assertSame( null, $priorAsOf, "Old value not given due to old format" );
$wasSet = 0;
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
$this->assertSame( $valueV2, $v, "Value not regenerated (secondary key)" );
$this->assertSame( 0, $wasSet, "Value not regenerated (secondary key)" );
// Clear out the older or unversioned key
$cache->delete( $key, 0 );
// Set the key for next/first versioned format
$wasSet = 0;
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
$this->assertSame( $valueV2, $v, "Value returned" );
$this->assertSame( 1, $wasSet, "Value regenerated" );
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
$this->assertSame( $valueV2, $v, "Value not regenerated (main key)" );
$this->assertSame( 1, $wasSet, "Value not regenerated (main key)" );
}
/**
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testInterimHoldOffCaching( array $params ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$value = 'CRL-40-940';
$wasCalled = 0;
$func = static function () use ( &$wasCalled, $value ) {
$wasCalled++;
return $value;
};
$cache->useInterimHoldOffCaching( true );
$key = wfRandomString( 32 );
$cache->getWithSetCallback( $key, 60, $func );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 1, $wasCalled, 'Value cached' );
$cache->delete( $key ); // no value at all anymore and still locked
$mockWallClock += 1; // cached values will be newer than tombstone
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 2, $wasCalled, 'Value interim cached' ); // reuses interim
$mockWallClock += 1; // interim key not brand new
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
// Lock up the mutex so interim cache is used
$this->setMutexKey( $bag, $key );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 3, $wasCalled, 'Value interim cached (failed mutex)' );
$this->clearMutexKey( $bag, $key );
$cache->useInterimHoldOffCaching( false );
$wasCalled = 0;
$key = wfRandomString( 32 );
$cache->getWithSetCallback( $key, 60, $func );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 1, $wasCalled, 'Value cached' );
$cache->delete( $key ); // no value at all anymore and still locked
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 2, $wasCalled, 'Value regenerated (got mutex)' );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 3, $wasCalled, 'Value still regenerated (got mutex)' );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 4, $wasCalled, 'Value still regenerated (got mutex)' );
// Lock up the mutex so interim cache is used
$this->setMutexKey( $bag, $key );
$cache->getWithSetCallback( $key, 60, $func );
$this->assertSame( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
}
public function testTouchKeys() {
[ $cache ] = $this->newWanCache();
$key = wfRandomString();
$mockWallClock = 1549343530.0;
$priorTime = floor( $mockWallClock ); // reference time
$cache->setMockTime( $mockWallClock );
$t0 = $cache->getCheckKeyTime( $key );
$this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
$mockWallClock += 1.100;
$priorTime = floor( $mockWallClock );
$cache->touchCheckKey( $key );
$t1 = $cache->getCheckKeyTime( $key );
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
$mockWallClock += 1.100;
$t2 = $cache->getCheckKeyTime( $key );
$this->assertSame( $t1, $t2, 'Check key time did not change' );
$mockWallClock += 1.100;
$cache->touchCheckKey( $key );
$t3 = $cache->getCheckKeyTime( $key );
$this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
$mockWallClock += 1.100;
$t4 = $cache->getCheckKeyTime( $key );
$this->assertSame( $t3, $t4, 'Check key time did not change' );
$mockWallClock += 1.100;
$cache->resetCheckKey( $key );
$t5 = $cache->getCheckKeyTime( $key );
$this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
$mockWallClock += 1.100;
$t6 = $cache->getCheckKeyTime( $key );
$this->assertSame( $t5, $t6, 'Check key time did not change' );
}
/**
* @param array $params
* @dataProvider provideCoalesceAndMcrouterSettings
*/
public function testGetWithSeveralCheckKeys( array $params ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$key = wfRandomString();
$tKey1 = wfRandomString();
$tKey2 = wfRandomString();
$value = 'meow';
$mockWallClock = 1549343530.0;
$priorTime = $mockWallClock; // reference time
$cache->setMockTime( $mockWallClock );
// Two check keys are newer (given hold-off) than $key, another is older
$this->setCheckKey( $bag, $tKey2, $priorTime - 3 );
$this->setCheckKey( $bag, $tKey2, $priorTime - 5 );
$this->setCheckKey( $bag, $tKey1, $priorTime - 30 );
$cache->set( $key, $value, 30 );
$curTTL = null;
$v = $cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
$this->assertSame( $value, $v, "Value matches" );
$this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
$this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
}
/**
*/
public function testSetWithLag() {
[ $cache ] = $this->newWanCache();
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$v = 1;
$key = wfRandomString();
$opts = [ 'lag' => 300, 'since' => $mockWallClock, 'walltime' => 0.1 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written." );
$key = wfRandomString();
$opts = [ 'lag' => 300, 'since' => $mockWallClock ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written (no walltime)." );
$key = wfRandomString();
$cache->get( $key );
$mockWallClock += 15;
$opts = [ 'lag' => 300, 'since' => $mockWallClock ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written (auto-walltime)." );
$key = wfRandomString();
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300, 'walltime' => 0.1 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value written." );
$key = wfRandomString();
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( $v, $cache->get( $key ), "Trx-lagged value written (no walltime)." );
$key = wfRandomString();
$cache->get( $key );
$mockWallClock += 15;
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value not written (auto-walltime)." );
$key = wfRandomString();
$opts = [ 'lag' => 5, 'since' => $mockWallClock - 5, 'walltime' => 0.1 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value written." );
$key = wfRandomString();
$opts = [ 'lag' => 3, 'since' => $mockWallClock - 3 ];
$cache->set( $key, $v, 30, $opts );
$this->assertSame( $v, $cache->get( $key ), "Lagged value written (no walltime)." );
}
/**
*/
public function testWritePending() {
[ $cache ] = $this->newWanCache();
$value = 1;
$key = wfRandomString();
$opts = [ 'pending' => true ];
$cache->set( $key, $value, 30, $opts );
$this->assertSame( false, $cache->get( $key ), "Pending value not written." );
}
public function testMcRouterSupport() {
$localBag = $this->getMockBuilder( EmptyBagOStuff::class )
->onlyMethods( [ 'set', 'delete' ] )->getMock();
$localBag->expects( $this->never() )->method( 'set' );
$localBag->expects( $this->never() )->method( 'delete' );
$wanCache = new WANObjectCache( [
'cache' => $localBag,
'broadcastRoutingPrefix' => '/*/mw-wan/',
] );
$valFunc = static function () {
return 1;
};
// None of these should use broadcasting commands (e.g. SET, DELETE)
$wanCache->get( 'x' );
$wanCache->get( 'x', $ctl, [ 'check1' ] );
$wanCache->getMulti( [ 'x', 'y' ] );
$wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
$wanCache->getWithSetCallback( 'p', 30, $valFunc );
$wanCache->getCheckKeyTime( 'zzz' );
}
public function testMcRouterSupportBroadcastDelete() {
$localBag = $this->getMockBuilder( EmptyBagOStuff::class )
->onlyMethods( [ 'set' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
'broadcastRoutingPrefix' => '/*/mw-wan/',
] );
$localBag->expects( $this->once() )->method( 'set' )
->with( "/*/mw-wan/WANCache:test|#|v" );
$wanCache->delete( 'test' );
}
public function testMcRouterSupportBroadcastTouchCK() {
$localBag = $this->getMockBuilder( EmptyBagOStuff::class )
->onlyMethods( [ 'set' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
'broadcastRoutingPrefix' => '/*/mw-wan/',
] );
$localBag->expects( $this->once() )->method( 'set' )
->with( "/*/mw-wan/WANCache:test|#|t" );
$wanCache->touchCheckKey( 'test' );
}
public function testMcRouterSupportBroadcastResetCK() {
$localBag = $this->getMockBuilder( EmptyBagOStuff::class )
->onlyMethods( [ 'delete' ] )->getMock();
$wanCache = new WANObjectCache( [
'cache' => $localBag,
'broadcastRoutingPrefix' => '/*/mw-wan/',
] );
$localBag->expects( $this->once() )->method( 'delete' )
->with( "/*/mw-wan/WANCache:test|#|t" );
$wanCache->resetCheckKey( 'test' );
}
public function testEpoch() {
$bag = new HashBagOStuff();
$cache = new WANObjectCache( [ 'cache' => $bag ] );
$key = $cache->makeGlobalKey( 'The whole of the Law' );
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$cache->set( $key, 'Do what thou Wilt' );
$cache->touchCheckKey( $key );
$then = $mockWallClock;
$mockWallClock += 30;
$this->assertSame( 'Do what thou Wilt', $cache->get( $key ) );
$this->assertEqualsWithDelta(
$then,
$cache->getCheckKeyTime( $key ),
0.01,
'Check key init'
);
$cache = new WANObjectCache( [
'cache' => $bag,
'epoch' => $mockWallClock - 3600
] );
$cache->setMockTime( $mockWallClock );
$this->assertSame( 'Do what thou Wilt', $cache->get( $key ) );
$this->assertEqualsWithDelta(
$then,
$cache->getCheckKeyTime( $key ),
0.01,
'Check key kept'
);
$mockWallClock += 30;
$cache = new WANObjectCache( [
'cache' => $bag,
'epoch' => $mockWallClock + 3600
] );
$cache->setMockTime( $mockWallClock );
$this->assertSame( false, $cache->get( $key ), 'Key rejected due to epoch' );
$this->assertEqualsWithDelta(
$mockWallClock,
$cache->getCheckKeyTime( $key ),
0.01,
'Check key reset'
);
}
/**
* @dataProvider provideAdaptiveTTL
* @param float|int $ago
* @param int $maxTTL
* @param int $minTTL
* @param float $factor
* @param int $adaptiveTTL
*/
public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
[ $cache ] = $this->newWanCache();
$mtime = $ago ? time() - $ago : $ago;
$margin = 5;
$ttl = $cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
$this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
$this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
$ttl = $cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
$this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
$this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
}
public static function provideAdaptiveTTL() {
return [
[ 3600, 900, 30, 0.2, 720 ],
[ 3600, 500, 30, 0.2, 500 ],
[ 3600, 86400, 800, 0.2, 800 ],
[ false, 86400, 800, 0.2, 800 ],
[ null, 86400, 800, 0.2, 800 ]
];
}
/**
*/
public function testNewEmpty() {
$this->assertInstanceOf(
WANObjectCache::class,
WANObjectCache::newEmpty()
);
}
/**
*/
public function testSetLogger() {
[ $cache ] = $this->newWanCache();
$this->assertSame( null, $cache->setLogger( new NullLogger ) );
}
/**
*/
public function testGetQoS() {
$backend = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'getQoS' ] )->getMock();
$backend->expects( $this->once() )->method( 'getQoS' )
->willReturn( BagOStuff::QOS_UNKNOWN );
$wanCache = new WANObjectCache( [ 'cache' => $backend ] );
$this->assertSame(
$wanCache::QOS_UNKNOWN,
$wanCache->getQoS( $wanCache::ATTR_DURABILITY )
);
}
/**
*/
public function testMakeKey() {
$backend = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeKey' ] )->getMock();
$backend->expects( $this->once() )->method( 'makeKey' )
->willReturn( 'special' );
$wanCache = new WANObjectCache( [
'cache' => $backend
] );
$this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
}
/**
*/
public function testMakeGlobalKey() {
$backend = $this->getMockBuilder( HashBagOStuff::class )
->onlyMethods( [ 'makeGlobalKey' ] )->getMock();
$backend->expects( $this->once() )->method( 'makeGlobalKey' )
->willReturn( 'special' );
$wanCache = new WANObjectCache( [
'cache' => $backend
] );
$this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
}
public static function statsKeyProvider() {
return [
[ 'domain:page:5', 'page' ],
[ 'domain:main-key', 'main-key' ],
[ 'domain:page:history', 'page' ],
// Regression test for T232907
[ 'domain:foo-bar-1.2:abc:v2', 'foo-bar-1_2' ],
[ 'missingdomainkey', 'missingdomainkey' ]
];
}
/**
* @dataProvider statsKeyProvider
* @param string $key
* @param string $class
*/
public function testStatsKeyClass( $key, $class ) {
/** @var WANObjectCache $wanCache */
$wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
'cache' => new HashBagOStuff
] ) );
$this->assertSame( $class, $wanCache->determineKeyGroupForStats( $key ) );
}
/**
*/
public function testMakeMultiKeys() {
[ $cache ] = $this->newWanCache();
$ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
$keyCallback = static function ( $id, WANObjectCache $cache ) {
return $cache->makeKey( 'key', $id );
};
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
$expected = [
"local:key:1" => 1,
"local:key:2" => 2,
"local:key:3" => 3,
"local:key:4" => 4,
"local:key:5" => 5,
"local:key:6" => 6,
"local:key:7" => 7
];
$this->assertSame( $expected, iterator_to_array( $keyedIds ) );
$ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
$keyCallback = static function ( $id, WANObjectCache $cache ) {
return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
};
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
$expected = [
"global:key:1:a:1:b" => '1',
"global:key:2:a:2:b" => '2',
"global:key:3:a:3:b" => '3',
"global:key:4:a:4:b" => '4',
"global:key:5:a:5:b" => '5',
"global:key:6:a:6:b" => '6',
"global:key:7:a:7:b" => '7'
];
$this->assertSame( $expected, iterator_to_array( $keyedIds ) );
}
/**
*/
public function testMakeMultiKeysIntString() {
[ $cache ] = $this->newWanCache();
$ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
$keyCallback = static function ( $id, WANObjectCache $cache ) {
return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
};
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
$expected = [
"global:key:1:a:1:b" => 1,
"global:key:2:a:2:b" => 2,
"global:key:3:a:3:b" => 3,
"global:key:4:a:4:b" => 4,
"global:key:5:a:5:b" => 5,
"global:key:6:a:6:b" => 6,
"global:key:7:a:7:b" => 7
];
$this->assertSame( $expected, iterator_to_array( $keyedIds ) );
}
/**
*/
public function testMakeMultiKeysCollision() {
[ $cache ] = $this->newWanCache();
$ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
$this->expectException( UnexpectedValueException::class );
$cache->makeMultiKeys(
$ids,
static function ( $id ) {
return "keymod:" . $id % 3;
}
);
}
/**
*/
public function testMultiRemap() {
[ $cache ] = $this->newWanCache();
$ids = [ 'a', 'b', 'c' ];
$res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
$this->assertSame(
[ 'a' => 1, 'b' => 2, 'c' => 3 ],
$cache->multiRemap( $ids, $res )
);
$ids = [ 'd', 'c' ];
$res = [ 'keyD' => 40, 'keyC' => 30 ];
$this->assertSame(
[ 'd' => 40, 'c' => 30 ],
$cache->multiRemap( $ids, $res )
);
}
/**
*/
public function testHash256() {
[ $cache ] = $this->newWanCache( [ 'epoch' => 5 ] );
$this->assertEquals(
'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
$cache->hash256( 'x' )
);
[ $cache ] = $this->newWanCache( [ 'epoch' => 50 ] );
$this->assertSame(
'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
$cache->hash256( 'x' )
);
[ $cache ] = $this->newWanCache( [ 'secret' => 'garden' ] );
$this->assertSame(
'48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
$cache->hash256( 'x' )
);
[ $cache ] = $this->newWanCache( [ 'secret' => 'garden', 'epoch' => 3 ] );
$this->assertSame(
'48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
$cache->hash256( 'x' )
);
}
/**
*
* @dataProvider provideCoalesceAndMcrouterSettings
* @param array $params
* @param string|null $keyNeedle
*/
public function testCoalesceKeys( array $params, $keyNeedle ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$key = wfRandomString();
$callback = static function () {
return 2020;
};
$cache->getWithSetCallback( $key, 60, $callback );
$wrapper = TestingAccessWrapper::newFromObject( $bag );
foreach ( $wrapper->bag as $bagKey => $_ ) {
if ( $keyNeedle === null ) {
$this->assertDoesNotMatchRegularExpression( '/[#{}]/', $bagKey, 'Respects "coalesceKeys"' );
} else {
$this->assertStringContainsString(
$keyNeedle,
$bagKey,
'Respects "coalesceKeys"'
);
}
}
}
/**
* @dataProvider provideCoalesceAndMcrouterSettings
* @param array $params
* @param string|null $keyNeedle
*/
public function testSegmentableValues( array $params, $keyNeedle ) {
[ $cache, $bag ] = $this->newWanCache( $params );
$mockWallClock = 1549343530.0;
$cache->setMockTime( $mockWallClock );
$key = $cache->makeGlobalKey( 'z', wfRandomString() );
$tiny = 418;
$small = wfRandomString( 32 );
// 64 * 8 * 32768 = 16 MiB, which will trigger segmentation
// assuming segmentationSize at default of 8 MiB.
$big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 );
$cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ];
foreach ( $cases as $case => $value ) {
$cache->set( $key, $value, 10, [ 'segmentable' => 1 ] );
$this->assertEquals( $value, $cache->get( $key ), "get $case" );
$this->assertEquals( [ $key => $value ], $cache->getMulti( [ $key ] ), "get $case" );
$this->assertTrue( $cache->delete( $key ), "delete $case" );
$this->assertFalse( $cache->get( $key ), "deleted $case" );
$this->assertEquals( [], $cache->getMulti( [ $key ] ), "deleted $case" );
$mockWallClock += 40;
$v = $cache->getWithSetCallback(
$key,
10,
static function ( $cache, $key, $oldValue ) use ( $value ) {
return "@$value";
},
[ 'segmentable' => 1 ]
);
$this->assertEquals( "@$value", $v, "get $case" );
$this->assertEquals( "@$value", $cache->get( $key ), "get $case" );
$this->assertTrue(
$cache->delete( $key ),
"prune $case"
);
$this->assertFalse( $cache->get( $key ), "pruned $case" );
$this->assertEquals( [], $cache->getMulti( [ $key ] ), "pruned $case" );
$mockWallClock += 40;
}
}
}
class McrouterHashBagOStuff extends HashBagOStuff {
public function set( $key, $value, $exptime = 0, $flags = 0 ) {
// Convert mcrouter broadcast keys to regular keys in HashBagOStuff::set() calls
// https://github.com/facebook/mcrouter/wiki/Multi-cluster-broadcast-setup
if ( preg_match( '#^/\*/[^/]+/(.*)$#', $key, $m ) ) {
$key = $m[1];
}
return parent::set( $key, $value, $exptime, $flags );
}
public function delete( $key, $flags = 0 ) {
// Convert mcrouter broadcast keys to regular keys in HashBagOStuff::delete() calls
// https://github.com/facebook/mcrouter/wiki/Multi-cluster-broadcast-setup
if ( preg_match( '#^/\*/[^/]+/(.*)$#', $key, $m ) ) {
$key = $m[1];
}
return parent::delete( $key, $flags );
}
}
class NearExpiringWANObjectCache extends WANObjectCache {
private const CLOCK_SKEW = 1;
protected function worthRefreshExpiring( $curTTL, $logicalTTL, $lowTTL ) {
return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
}
}
class PopularityRefreshingWANObjectCache extends WANObjectCache {
protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
return ( ( $now - $asOf ) > $timeTillRefresh );
}
}
class SerialHashBagOStuff extends HashBagOStuff {
protected function doGet( $key, $flags = 0, &$casToken = null ) {
$serialized = parent::doGet( $key, $flags, $casToken );
return ( $serialized !== false ) ? $this->unserialize( $serialized ) : false;
}
protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
$serialized = $this->getSerialized( $value, $key );
return parent::doSet( $key, $serialized, $exptime, $flags );
}
}
|