? includes/handlers.inc Index: includes/bootstrap.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v retrieving revision 1.290 diff -u -p -r1.290 bootstrap.inc --- includes/bootstrap.inc 19 Jul 2009 05:26:11 -0000 1.290 +++ includes/bootstrap.inc 20 Jul 2009 13:01:13 -0000 @@ -1405,6 +1405,9 @@ function _drupal_bootstrap($phase) { } // Prepare for non-cached page workflow. + require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc'); + lock_init(); + if ($_SERVER['SERVER_SOFTWARE'] !== 'PHP CLI') { ob_start(); drupal_page_header(); Index: includes/lock.inc =================================================================== RCS file: includes/lock.inc diff -N includes/lock.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/lock.inc 20 Jul 2009 13:01:13 -0000 @@ -0,0 +1,252 @@ +fields(array('expire' => $expire)) + ->condition('name', $name) + ->condition('value', _lock_id()) + ->execute(); + if (!$success) { + // The lock was broken. + unset($locks[$name]); + } + return $success; + } + else { + // Optimistically try to acquire the lock, then retry once if it fails. + // The first time through the loop cannot be a retry. + $retry = FALSE; + // We always want to do this code at least once. + do { + try { + db_insert('semaphore') + ->fields(array( + 'name' => $name, + 'value' => _lock_id(), + 'expire' => $expire, + )) + ->execute(); + // We track all acquired locks in the global variable. + $locks[$name] = TRUE; + // We never need to try again. + $retry = FALSE; + } + catch (PDOException $e) { + // Suppress the error. If this is our first pass through the loop, + // then $retry is FALSE. In this case, the insert must have failed + // meaning some other request acquired the lock but did not release it. + // We decide whether to retry by checking lock_may_be_available() + // Since this will break the lock in case it is expired. + $retry = $retry ? FALSE : lock_may_be_available($name); + } + // We only retry in case the first attempt failed, but we then broke + // an expired lock. + } while ($retry); + } + return isset($locks[$name]); +} + +/** + * Check if lock acquired by a different process may be available. + * + * If an existing lock has expired, it is removed. + * + * @param $name + * The name of the lock. + * @return + * TRUE if there is no lock or it was removed, FALSE otherwise. + */ +function lock_may_be_available($name) { + $lock = db_query("SELECT expire, value FROM {semaphore} WHERE name = :name", array(':name' => $name))->fetchAssoc(); + if (!$lock) { + return TRUE; + } + $expire = (float) $lock['expire']; + $now = microtime(TRUE); + if ($now > $expire) { + // We check two conditions to prevent a race condition where another + // request acquired the lock and set a new expire time. We add a small + // number to $expire to avoid errors with float to string conversion. + return (bool) db_delete('semaphore') + ->condition('name', $name) + ->condition('value', $lock['value']) + ->condition('expire', 0.0001 + $expire, '<=') + ->execute(); + } + return FALSE; +} + +/** + * Wait for a lock to be available. + * + * This function may be called in a request that fails to acquire a desired + * lock. This will block further execution until the lock is available or the + * specified delay in seconds is reached. This should not be used with locks + * that are acquired very frequently, since the lock is likely to be acquired + * again by a different request during the sleep(). + * + * @param $name + * The name of the lock. + * @param $delay + * The maximum number of seconds to wait, as an integer. + * @return + * TRUE if the lock holds, FALSE if it is available. + */ +function lock_wait($name, $delay = 30) { + $delay = (int) $delay; + while ($delay--) { + // This function should only be called by a request that failed to get a + // lock, so we sleep first to give the parallel request a chance to finish + // and release the lock. + sleep(1); + if (lock_may_be_available($name)) { + // No longer need to wait. + return FALSE; + } + } + // The caller must still wait longer to get the lock. + return TRUE; +} + +/** + * Release a lock previously acquired by lock_acquire(). + * + * This will release the named lock if it is still held by the current request. + * + * @param $name + * The name of the lock. + */ +function lock_release($name) { + global $locks; + + unset($locks[$name]); + db_delete('semaphore') + ->condition('name', $name) + ->condition('value', _lock_id()) + ->execute(); +} + +/** + * Release all previously acquired locks. + */ +function lock_release_all($lock_id = NULL) { + global $locks; + + $locks = array(); + if (empty($lock_id)) { + $lock_id = _lock_id(); + } + db_delete('semaphore') + ->condition('value', $lock_id) + ->execute(); +} + +/** + * @} End of "defgroup lock". + */ Index: includes/menu.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/menu.inc,v retrieving revision 1.330 diff -u -p -r1.330 menu.inc --- includes/menu.inc 20 Jul 2009 01:28:16 -0000 1.330 +++ includes/menu.inc 20 Jul 2009 13:01:13 -0000 @@ -1837,18 +1837,35 @@ function menu_cache_clear_all() { * schedule a call to itself on the first real page load from * menu_execute_active_handler(), because the maintenance page environment * is different and leaves stale data in the menu tables. + * + * @return + * TRUE if the menu was rebuilt, FALSE if another thread was rebuilding + * in parallel and the current thread just waited for completion. */ function menu_rebuild() { - variable_del('menu_rebuild_needed'); + if (!lock_acquire('menu_rebuild')) { + // Wait for another request that is already doing this work. + // We choose to block here since otherwise the router item may not + // be available in menu_execute_active_handler() resulting in a 404. + lock_wait('menu_rebuild'); + return FALSE; + } + list($menu, $masks) = menu_router_build(); _menu_router_save($menu, $masks); _menu_navigation_links_rebuild($menu); + // Clear the menu, page and block caches. menu_cache_clear_all(); - // Clear the page and block caches. _menu_clear_page_cache(); + if (defined('MAINTENANCE_MODE')) { variable_set('menu_rebuild_needed', TRUE); } + else { + variable_del('menu_rebuild_needed'); + } + lock_release('menu_rebuild'); + return TRUE; } /** Index: modules/locale/locale.module =================================================================== RCS file: /cvs/drupal/drupal/modules/locale/locale.module,v retrieving revision 1.243 diff -u -p -r1.243 locale.module --- modules/locale/locale.module 5 Jul 2009 18:00:09 -0000 1.243 +++ modules/locale/locale.module 20 Jul 2009 13:01:13 -0000 @@ -375,7 +375,7 @@ function locale($string = NULL, $context if ($cache = cache_get('locale:' . $langcode, 'cache')) { $locale_t[$langcode] = $cache->data; } - else { + elseif (lock_acquire('locale_cache_' . $langcode)) { // Refresh database stored cache of translations for given language. // We only store short strings used in current version, to improve // performance and consume less memory. @@ -384,6 +384,7 @@ function locale($string = NULL, $context $locale_t[$langcode][$data->context][$data->source] = (empty($data->translation) ? TRUE : $data->translation); } cache_set('locale:' . $langcode, $locale_t[$langcode]); + lock_release('locale_cache_' . $langcode); } } } Index: modules/simpletest/simpletest.info =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.info,v retrieving revision 1.7 diff -u -p -r1.7 simpletest.info --- modules/simpletest/simpletest.info 1 Jul 2009 13:44:53 -0000 1.7 +++ modules/simpletest/simpletest.info 20 Jul 2009 13:01:13 -0000 @@ -23,6 +23,7 @@ files[] = tests/filetransfer.test files[] = tests/form.test files[] = tests/graph.test files[] = tests/image.test +files[] = tests/lock.test files[] = tests/menu.test files[] = tests/module.test files[] = tests/registry.test Index: modules/simpletest/tests/lock.test =================================================================== RCS file: modules/simpletest/tests/lock.test diff -N modules/simpletest/tests/lock.test --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/simpletest/tests/lock.test 20 Jul 2009 13:01:13 -0000 @@ -0,0 +1,58 @@ + 'Locking framework tests', + 'description' => 'Confirm locking works between two separate requests.', + 'group' => 'System', + ); + } + + function setUp() { + parent::setUp('system_test'); + } + + /** + * Confirm that we can acquire and release locks in two parallel requests. + */ + function testLockAcquire() { + $lock_acquired = 'TRUE: Lock successfully acquired in system_test_lock_acquire()'; + $lock_not_acquired = 'FALSE: Lock not acquired in system_test_lock_acquire()'; + $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock')); + $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock extended by this request.'), t('Lock')); + lock_release('system_test_lock_acquire'); + + // Let another request acquire the lock. + $this->drupalGet('system-test/lock-acquire'); + $this->assertText($lock_acquired, t('Lock acquired by the other request.'), t('Lock')); + // The other request finished and should have released its lock. + $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock')); + // This request holds the lock, the other request cannot get it. + $this->drupalGet('system-test/lock-acquire'); + $this->assertText($lock_not_acquired, t('Lock not acquired by the other request.'), t('Lock')); + lock_release('system_test_lock_acquire'); + + // Try a very short timeout and lock breaking. + $this->assertTrue(lock_acquire('system_test_lock_acquire', 0.5), t('Lock acquired by this request.'), t('Lock')); + sleep(1); + // The other request should break our lock. + $this->drupalGet('system-test/lock-acquire'); + $this->assertText($lock_acquired, t('Lock acquired by the other request, breaking our lock.'), t('Lock')); + // We cannot renew it, since the other thread took it. + $this->assertFalse(lock_acquire('system_test_lock_acquire'), t('Lock cannot be extended by this request.'), t('Lock')); + + // Check the shut-down function. + $lock_acquired_exit = 'TRUE: Lock successfully acquired in system_test_lock_exit()'; + $lock_not_acquired_exit = 'FALSE: Lock not acquired in system_test_lock_exit()'; + $this->drupalGet('system-test/lock-exit'); + $this->assertText($lock_acquired_exit, t('Lock acquired by the other request before exit.'), t('Lock')); + $this->assertTrue(lock_acquire('system_test_lock_exit'), t('Lock acquired by this request after the other request exits.'), t('Lock')); + } +} + Index: modules/simpletest/tests/system_test.module =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/system_test.module,v retrieving revision 1.12 diff -u -p -r1.12 system_test.module --- modules/simpletest/tests/system_test.module 6 Jun 2009 15:43:05 -0000 1.12 +++ modules/simpletest/tests/system_test.module 20 Jul 2009 13:01:13 -0000 @@ -59,6 +59,20 @@ function system_test_menu() { 'type' => MENU_CALLBACK, ); + $items['system-test/lock-acquire'] = array( + 'title' => 'Lock acquire', + 'page callback' => 'system_test_lock_acquire', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + $items['system-test/lock-exit'] = array( + 'title' => 'Lock acquire then exit', + 'page callback' => 'system_test_lock_exit', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -165,3 +179,29 @@ function system_test_exit() { watchdog('system_test', 'hook_exit'); } +/** + * Try to acquire a named lock and report the outcome. + */ +function system_test_lock_acquire() { + if (lock_acquire('system_test_lock_acquire')) { + lock_release('system_test_lock_acquire'); + return 'TRUE: Lock successfully acquired in system_test_lock_acquire()'; + } + else { + return 'FALSE: Lock not acquired in system_test_lock_acquire()'; + } +} + +/** + * Try to acquire a specific lock, and then exit. + */ +function system_test_lock_exit() { + if (lock_acquire('system_test_lock_exit', 900)) { + echo 'TRUE: Lock successfully acquired in system_test_lock_exit()'; + // The shut-down function should release the lock. + exit(); + } + else { + return 'FALSE: Lock not acquired in system_test_lock_exit()'; + } +} Index: modules/system/system.install =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.install,v retrieving revision 1.354 diff -u -p -r1.354 system.install --- modules/system/system.install 10 Jul 2009 04:28:15 -0000 1.354 +++ modules/system/system.install 20 Jul 2009 13:01:14 -0000 @@ -1288,6 +1288,34 @@ function system_schema() { 'primary key' => array('filename'), ); + $schema['semaphore'] = array( + 'description' => 'Table for holding semaphores, locks, flags, etc. that cannot be stored as Drupal variables since they must not be cached.', + 'fields' => array( + 'name' => array( + 'description' => 'Primary Key: Unique name.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '' + ), + 'value' => array( + 'description' => 'A value for the semaphore.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '' + ), + 'expire' => array( + 'description' => 'A Unix timestamp with microseconds indicating when the semaphore should expire.', + 'type' => 'float', + 'size' => 'big', + 'not null' => TRUE + ), + ), + 'indexes' => array('value' => array('value')), + 'primary key' => array('name'), + ); + $schema['sessions'] = array( 'description' => "Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated.", 'fields' => array(