By writing dblog entries to a queue rather than the database we can improve performance.

This can then be written to the database when cron runs or when admin/reports/dblog is viewed.

Comments

pdrake’s picture

So, it sounds like what you're attempting to accomplish is allowing the current execution to continue and not wait for the database insert to complete. This can be accomplished with INSERT DELAYED in MySQL, or it could be accomplished by loading all db log entries into (drupal_static variable? MemoryQueue?) and processing them in a shutdown function.

timmillwood’s picture

The advantage of using a queue is if using a different backend for the queue such as mongodb or redis.

pdrake’s picture

In that case, the log entries would still have to be queried out of the mongodb queue backend via a worker and inserted into MySQL, so I'm not sure what the gain is over using INSERT DELAYED or inserting during a shutdown function (except that the processing could occur on a different server). If you're wanting to simply log watchdog entries to a different backend (such as mongodb), there are modules available for that, such as mongodb/mongodb_watchdog.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

fgm’s picture

Issue summary: View changes

One thing we might do in this case is rely on the Drupal\Core\Queue\Memory queue backend which is essentially a local buffer.

The problem with any such buffering (think Monolog BufferHandler) is that if any page encounters a fatal failure, nothing gets logged at all, so you don't even get any trace that anything has gone wrong. It means we may need to add an ability to force a flush (but this is not present in PSR-3 or Drupal's own logging API), or use some strategy to stop queuing in some conditions (think Monolog FingersCrossedHandler).

It also means there has to be a setting to switch queuing on/off in dblog, and possibly one for a fingerscrossed-like behaviour.

fgm’s picture

Assigned: Unassigned » fgm
Issue tags: +Vienna2017

Working on an initial patch at DC Vienna.

fgm’s picture

Version: 8.4.x-dev » 8.5.x-dev
Assigned: fgm » Unassigned
Status: Active » Needs review
StatusFileSize
new11.29 KB

Here is an initial stab at what it could look like:

  • config now includes queue.enabled and queue.level
  • any log below the minimum level logs everything in waiting
  • any log above the minimum is just enqueued
  • synchronous logs remaining the default for backwards compatibility and for safety

Note that, unlike the summary, it is still written at the end of the page, since this uses the in-memory queue to limit impact and make it available on all sites. It's just buffering, in the end.

Regarding deferred building of the logs from an external queue is something which does not make sense without contrib, since memory queue is empty after the page end, and the default DB queue, well... is written to the DB anyway.

Status: Needs review » Needs work

The last submitted patch, 10: 1506262-dblog_queue.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

fgm’s picture

Status: Needs work » Needs review
StatusFileSize
new11.29 KB

Kwalify expects "integer", not "int". Rerolled.

fgm’s picture

StatusFileSize
new18.62 KB

* DRYed the config name by extracting it to a constant
* Moved the setttings form building to a class
* Synchronized form label/description with the schema.

fgm’s picture

Gave more thought given to the off-page-cycle writing using an external queue like MongoDB or Redis: one downside would be the fact that this would make the DB writes extremely bursty, with possibly very high quantities of rows being written during these cron/worker jobs in rapide succession, likely to bury the DB server under the write load and disturb the normal page production far more than the spread-over-time behavior we have with the current implementation or with a buffered one like this patch.

Overall, what I've seen over the years is rather the straight use of MongoDB Watchdog, or pushing to syslog or local filesystem buffering, thence Filebeat (Lumberjack, LMogstash forwarder), injecting (directly or not, via Logstash) to Elastic for display using Kibana. In all cases, this gets DB load entirely out of MySQL (which is the main goal), and brings a much improved UI: MongoDB Watchdog remaing on-site and is easy to use and quite convenient, while the BELK stack is considerably more flexible but requires a lot more system administration and configuratoin.

dagmar’s picture

+++ b/core/modules/dblog/src/Logger/DbLog.php
@@ -109,4 +155,59 @@ public function log($level, $message, array $context = []) {
+    if (!$this->useQueue) {
+      // ksm("Not queued write $message");
+      $this->dbLog($level, $message, $message_placeholders, $context);
+    }
+    elseif ($level <= $this->flushLevel) {
+      // ksm("Queued flush write $message");
+      // Log previously stored message.
+      $this->dbLogQueue();
+      // Then log this one to preserve ordering.
+      $this->dbLog($level, $message, $message_placeholders, $context);
+    }
+    else {
+      // ksm("Enqueueing $message");
+      $this->queue->createItem(compact('level', 'message', 'message_placeholders', 'context'));
+    }

The idea of having an alternative backend for dblog is considered here: #2458191: Provide a storage backend for dblog module. I think this will simplify the logic behind the code provided here. Once you be comfortable with the approach of using I queue for dblog, I think your review on that patch could give us and idea of if is flexible enough to support cases like this.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dagmar’s picture

Status: Needs review » Closed (won't fix)

Since this this issue was created 10 years ago, and given contrib module can write to any backend without needing to write to the db, like redis_watchdog or https://git.drupalcode.org/project/mongodb/-/tree/8.x-2.x/modules/mongod... and potentially in the future use the same dblog ui thanks to #2401463: Make dblog entities I'm marking this issue as won't fix.