This function handles 'background' operations we are required to do * incrementally in Redis databases, such as active key expiring, resizing, * rehashing. */
| 1857 | * incrementally in Redis databases, such as active key expiring, resizing, |
| 1858 | * rehashing. */ |
| 1859 | void databasesCron(void) { |
| 1860 | /* Expire keys by random sampling. Not required for slaves |
| 1861 | * as master will synthesize DELs for us. */ |
| 1862 | if (server.active_expire_enabled) { |
| 1863 | if (iAmMaster()) { |
| 1864 | activeExpireCycle(ACTIVE_EXPIRE_CYCLE_SLOW); |
| 1865 | } else { |
| 1866 | expireSlaveKeys(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* Defrag keys gradually. */ |
| 1871 | activeDefragCycle(); |
| 1872 | |
| 1873 | /* Perform hash tables rehashing if needed, but only if there are no |
| 1874 | * other processes saving the DB on disk. Otherwise rehashing is bad |
| 1875 | * as will cause a lot of copy-on-write of memory pages. */ |
| 1876 | if (!hasActiveChildProcess()) { |
| 1877 | /* We use global counters so if we stop the computation at a given |
| 1878 | * DB we'll be able to start from the successive in the next |
| 1879 | * cron loop iteration. */ |
| 1880 | static unsigned int resize_db = 0; |
| 1881 | static unsigned int rehash_db = 0; |
| 1882 | int dbs_per_call = CRON_DBS_PER_CALL; |
| 1883 | int j; |
| 1884 | |
| 1885 | /* Don't test more DBs than we have. */ |
| 1886 | if (dbs_per_call > server.dbnum) dbs_per_call = server.dbnum; |
| 1887 | |
| 1888 | /* Resize */ |
| 1889 | for (j = 0; j < dbs_per_call; j++) { |
| 1890 | tryResizeHashTables(resize_db % server.dbnum); |
| 1891 | resize_db++; |
| 1892 | } |
| 1893 | |
| 1894 | /* Rehash */ |
| 1895 | if (server.activerehashing) { |
| 1896 | for (j = 0; j < dbs_per_call; j++) { |
| 1897 | int work_done = incrementallyRehash(rehash_db); |
| 1898 | if (work_done) { |
| 1899 | /* If the function did some work, stop here, we'll do |
| 1900 | * more at the next cron loop. */ |
| 1901 | break; |
| 1902 | } else { |
| 1903 | /* If this db didn't need rehash, we'll try the next one. */ |
| 1904 | rehash_db++; |
| 1905 | rehash_db %= server.dbnum; |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | /* We take a cached value of the unix time in the global state because with |
| 1913 | * virtual memory and aging there is to store the current time in objects at |
no test coverage detected