diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-07-01 08:11:27 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-07-01 08:11:27 +0000 |
commit | 8eb8dcc8425295d1a4278613031812bff7d98c15 (patch) | |
tree | 665ff11ef19c4d8f9ea643effb7df5dab552db41 | |
parent | 9ce7b3dcdc2cc964f422594b1cc65440cd7decef (diff) | |
download | brdo-8eb8dcc8425295d1a4278613031812bff7d98c15.tar.gz brdo-8eb8dcc8425295d1a4278613031812bff7d98c15.tar.bz2 |
- Patch #314358 by wonder95, Crell, killes, et al: add option to ignore slave servers. Finally.
-rw-r--r-- | includes/database/database.inc | 13 | ||||
-rw-r--r-- | modules/comment/comment.module | 7 | ||||
-rw-r--r-- | modules/node/node.module | 4 | ||||
-rw-r--r-- | modules/system/system.module | 24 |
4 files changed, 48 insertions, 0 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 44a90c1ee..45f4d748c 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -2687,6 +2687,19 @@ function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $ return $query; } +/** + * Helper function to get duration lag from variable + * and set the session variable that contains the lag. + */ +function db_ignore_slave() { + // Five minutes is long enough to allow the slave to break and resume + // interrupted replication without causing problems on the Drupal site + // from the old data. + $duration = variable_get('maximum_replication_lag', 300); + + // Set session variable with amount of time to delay before using slave. + $_SESSION['ignore_slave_server'] = REQUEST_TIME + $duration; +} /** * @} End of "ingroup database-legacy". diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 7b6180f20..bc578e350 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -962,12 +962,19 @@ function comment_save(&$comment) { 'homepage' => $comment['homepage'], )) ->execute(); + + // Ignore slave server temporarily to give time for the + // saved node to be propagated to the slave. + db_ignore_slave(); + // Tell the other modules a new comment has been submitted. comment_invoke_comment($comment, 'insert'); + // Add an entry to the watchdog log. watchdog('content', 'Comment: added %subject.', array('%subject' => $comment['subject']), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment['cid'], array('fragment' => 'comment-' . $comment['cid']))); } _comment_update_node_statistics($comment['nid']); + // Clear the cache so an anonymous user can see his comment being added. cache_clear_all(); diff --git a/modules/node/node.module b/modules/node/node.module index 6692a20de..9eec39e11 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1003,6 +1003,10 @@ function node_save($node) { // Clear the page and block caches. cache_clear_all(); + + // Ignore slave server temporarily to give time for the + // saved node to be propagated to the slave. + db_ignore_slave(); } /** diff --git a/modules/system/system.module b/modules/system/system.module index 7a2667457..567cf8989 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -872,8 +872,32 @@ function system_init() { drupal_add_css(drupal_get_path('module', 'system') . '/defaults.css'); drupal_add_css(drupal_get_path('module', 'system') . '/system.css'); drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css'); + + + // Ignore slave database servers for this request. + // + // In Drupal's distributed database structure, new data is written to the master + // and then propagated to the slave servers. This means there is a lag + // between when data is written to the master and when it is available on the slave. + // At these times, we will want to avoid using a slave server temporarily. + // For example, if a user posts a new node then we want to disable the slave + // server for that user temporarily to allow the slave server to catch up. + // That way, that user will see their changes immediately while for other + // users we still get the benefits of having a slave server, just with slightly + // stale data. Code that wants to disable the slave server should use the + // db_set_ignore_slave() function to set $_SESSION['ignore_slave_server'] to + // the timestamp after which the slave can be re-enabled. + if (isset($_SESSION['ignore_slave_server'])) { + if ($_SESSION['ignore_slave_server'] >= REQUEST_TIME) { + Database::ignoreTarget('default', 'slave'); + } + else { + unset($_SESSION['ignore_slave_server']); + } + } } + /** * Implement MODULE_preprocess_HOOK(). */ |