? ignore_slave_3.patch
? sites/all/modules/admin_menu
? sites/default/files
? sites/default/settings.php
Index: includes/database/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/database.inc,v
retrieving revision 1.50
diff -u -p -r1.50 database.inc
--- includes/database/database.inc	25 Jan 2009 12:19:31 -0000	1.50
+++ includes/database/database.inc	11 Feb 2009 05:51:05 -0000
@@ -2640,6 +2640,22 @@ function db_rewrite_sql($query, $primary
   return $query;
 }
 
+/**
+ * Helper function to get duration lag from variable
+ * if one isn't passed, and then set the session variable
+ * that contains the lag.
+ *
+ * @param $duration
+ *   The amount of time to ignore the slave to give it
+ *   time to refresh after an insert or update.
+ */
+function db_set_ignore_slave($duration = NULL) {
+  if (!$duration) {
+    $duration = variable_get('maximum_replication_lag', 10);
+  }
+  drupal_set_session('ignore_slave_server', REQUEST_TIME + $duration);
+}
+
 
 /**
  * @} End of "ingroup database-legacy".
Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.692
diff -u -p -r1.692 comment.module
--- modules/comment/comment.module	7 Feb 2009 20:10:40 -0000	1.692
+++ modules/comment/comment.module	11 Feb 2009 05:54:14 -0000
@@ -726,6 +726,20 @@ function comment_user_cancel($edit, $acc
 }
 
 /**
+ * Implementation of hook_comment_insert().
+ */
+function comment_comment_insert($comment) {
+  db_set_ignore_slave();
+}
+
+/**
+ * Implementation of hook_comment_update().
+ */
+function comment_comment_update($comment) {
+  db_set_ignore_slave();
+}
+
+/**
  * This is *not* a hook_access() implementation. This function is called
  * to determine whether the current user has access to a particular comment.
  *
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.1023
diff -u -p -r1.1023 node.module
--- modules/node/node.module	6 Feb 2009 16:25:08 -0000	1.1023
+++ modules/node/node.module	4 Apr 2009 01:17:38 -0000
@@ -1595,6 +1595,21 @@ function node_user_cancel($edit, $accoun
 }
 
 /**
+ * Implementation of hook_node_insert().
+ */
+function node_node_insert(&$node) {
+  db_set_ignore_slave();
+}
+
+/**
+ * Implementation of hook_node_update().
+ */
+function node_node_update(&$node) {
+  db_set_ignore_slave();
+}
+
+
+/**
  * Theme the content ranking part of the search settings admin page.
  *
  * @ingroup themeable
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.666
diff -u -p -r1.666 system.module
--- modules/system/system.module	3 Feb 2009 18:55:31 -0000	1.666
+++ modules/system/system.module	24 Mar 2009 18:59:05 -0000
@@ -800,6 +800,28 @@ 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']);
+    }
+  }
 }
 
 /**
