]> git.mxchange.org Git - friendica.git/blobdiff - include/poller.php
Fetching new queue tasks in a bulk to increase speed
[friendica.git] / include / poller.php
index 4b5d6287eceb28343c4444eafb56ed5a9868d883..4801ea8fdf09aaa20d1998b63b0d4162ccf18b0e 100644 (file)
@@ -54,6 +54,12 @@ function poller_run($argv, $argc){
                poller_kill_stale_workers();
        }
 
+       // Count active workers and compare them with a maximum value that depends on the load
+       if (poller_too_much_workers()) {
+               logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
+               return;
+       }
+
        // Do we have too few memory?
        if ($a->min_memory_reached()) {
                logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
@@ -81,27 +87,21 @@ function poller_run($argv, $argc){
 
        // We fetch the next queue entry that is about to be executed
        while ($r = poller_worker_process()) {
+               foreach ($r AS $entry) {
+                       // Assure that the priority is an integer value
+                       $entry['priority'] = (int)$entry['priority'];
 
-               // Assure that the priority is an integer value
-               $r[0]['priority'] = (int)$r[0]['priority'];
-
-               // If we got that queue entry we claim it for us
-               if (!poller_claim_process($r[0])) {
-                       dba::unlock();
-                       continue;
-               } else {
-                       // Fetch all workerqueue data while the table is still locked
-                       // This is redundant, but this speeds up the processing
-                       $entries = poller_total_entries();
-                       $top_priority = poller_highest_priority();
-                       $high_running = poller_process_with_priority_active($top_priority);
-                       dba::unlock();
+                       // The work will be done
+                       if (!poller_execute($entry)) {
+                               logger('Process execution failed, quitting.', LOGGER_DEBUG);
+                               return;
+                       }
                }
 
                // To avoid the quitting of multiple pollers only one poller at a time will execute the check
                if (Lock::set('poller_worker', 0)) {
                        // Count active workers and compare them with a maximum value that depends on the load
-                       if (poller_too_much_workers($entries, $top_priority, $high_running)) {
+                       if (poller_too_much_workers()) {
                                logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
                                return;
                        }
@@ -114,14 +114,8 @@ function poller_run($argv, $argc){
                        Lock::remove('poller_worker');
                }
 
-               // finally the work will be done
-               if (!poller_execute($r[0])) {
-                       logger('Process execution failed, quitting.', LOGGER_DEBUG);
-                       return;
-               }
-
-               // Quit the poller once every hour
-               if (time() > ($starttime + 3600)) {
+               // Quit the poller once every 5 minutes
+               if (time() > ($starttime + 300)) {
                        logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
                        return;
                }
@@ -471,7 +465,7 @@ function poller_kill_stale_workers() {
  *
  * @return bool Are there too much workers running?
  */
-function poller_too_much_workers($entries = NULL, $top_priority = NULL, $high_running = NULL) {
+function poller_too_much_workers() {
        $queues = Config::get("system", "worker_queues", 4);
 
        $maxqueues = $queues;
@@ -514,16 +508,12 @@ function poller_too_much_workers($entries = NULL, $top_priority = NULL, $high_ru
                        $processlist = ' ('.implode(', ', $listitem).')';
                }
 
-               if (is_null($entries)) {
-                       $entries = poller_total_entries();
-               }
+               $entries = poller_total_entries();
+
                if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
-                       if (is_null($top_priority)) {
-                               $top_priority = poller_highest_priority();
-                       }
-                       if (is_null($high_running)) {
-                               $high_running = poller_process_with_priority_active($top_priority);
-                       }
+                       $top_priority = poller_highest_priority();
+                       $high_running = poller_process_with_priority_active($top_priority);
+
                        if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
                                logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
                                $queues = $active + 1;
@@ -607,87 +597,71 @@ function poller_passing_slow(&$highest_priority) {
 }
 
 /**
- * @brief Returns the next worker process
+ * @brief Find and claim the next worker process for us
  *
- * @return string SQL statement
+ * @return boolean Have we found something?
  */
-function poller_worker_process() {
-
+function find_worker_processes() {
        // Check if we should pass some low priority process
        $highest_priority = 0;
+       $found = false;
 
        if (poller_passing_slow($highest_priority)) {
-               dba::lock('workerqueue');
-
                // Are there waiting processes with a higher priority than the currently highest?
-               $r = q("SELECT * FROM `workerqueue`
-                               WHERE `executed` <= '%s' AND `priority` < %d
-                               ORDER BY `priority`, `created` LIMIT 1",
-                               dbesc(NULL_DATE),
-                               intval($highest_priority));
-               if (dbm::is_result($r)) {
-                       return $r;
+               $result = dba::p("UPDATE `workerqueue` SET `executed` = ?, `pid` = ?
+                                       WHERE `executed` <= ? AND `priority` < ?
+                                       ORDER BY `priority`, `created` LIMIT 5",
+                               datetime_convert(), getmypid(), NULL_DATE, $highest_priority);
+               if (dbm::is_result($result)) {
+                       $found = (dba::num_rows($result) > 0);
                }
-               // Give slower processes some processing time
-               $r = q("SELECT * FROM `workerqueue`
-                               WHERE `executed` <= '%s' AND `priority` > %d
-                               ORDER BY `priority`, `created` LIMIT 1",
-                               dbesc(NULL_DATE),
-                               intval($highest_priority));
-
-               if (dbm::is_result($r)) {
-                       return $r;
+               dba::close($result);
+
+               if (!$found) {
+                       // Give slower processes some processing time
+                       $result = dba::p("UPDATE `workerqueue` SET `executed` = ?, `pid` = ?
+                                               WHERE `executed` <= ? AND `priority` > ?
+                                               ORDER BY `priority`, `created` LIMIT 1",
+                                       datetime_convert(), getmypid(), NULL_DATE, $highest_priority);
+                       if (dbm::is_result($result)) {
+                               $found = (dba::num_rows($result) > 0);
+                       }
+                       dba::close($result);
                }
-       } else {
-               dba::lock('workerqueue');
        }
 
        // If there is no result (or we shouldn't pass lower processes) we check without priority limit
-       if (!dbm::is_result($r)) {
-               $r = q("SELECT * FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority`, `created` LIMIT 1", dbesc(NULL_DATE));
-       }
-
-       // We only unlock the tables here, when we got no data
-       if (!dbm::is_result($r)) {
-               dba::unlock();
+       if (!$found) {
+               $result = dba::p("UPDATE `workerqueue` SET `executed` = ?, `pid` = ? WHERE `executed` <= ? ORDER BY `priority`, `created` LIMIT 5",
+                               datetime_convert(), getmypid(), NULL_DATE);
+               if (dbm::is_result($result)) {
+                       $found = (dba::num_rows($result) > 0);
+               }
+               dba::close($result);
        }
-
-       return $r;
+       return $found;
 }
 
 /**
- * @brief Assigns a workerqueue entry to the current process
- *
- * When we are sure that the table locks are working correctly, we can remove the checks from here
- *
- * @param array $queue Workerqueue entry
+ * @brief Returns the next worker process
  *
- * @return boolean "true" if the claiming was successful
+ * @return string SQL statement
  */
-function poller_claim_process($queue) {
-       $mypid = getmypid();
+function poller_worker_process() {
 
-       $success = dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid),
-                       array('id' => $queue["id"], 'pid' => 0));
+       $stamp = (float)microtime(true);
 
-       if (!$success) {
-               logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
-               return false;
-       }
+       $timeout = 10;
+       do {
+               $found = find_worker_processes();
+       } while (!$found && (poller_total_entries() > 0) && (--$timeout > 0));
 
-       // Assure that there are no tasks executed twice
-       $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
-       if (!$id) {
-               logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
-               return false;
-       } elseif ((strtotime($id[0]["executed"]) <= 0) || ($id[0]["pid"] == 0)) {
-               logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
-               return false;
-       } elseif ($id[0]["pid"] != $mypid) {
-               logger("Queue item ".$queue["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
-               return false;
+       logger('Duration: '.number_format(microtime(true) - $stamp, 3), LOGGER_DEBUG);
+
+       if ($found) {
+               $r = q("SELECT * FROM `workerqueue` WHERE `pid` = %d", intval(getmypid()));
        }
-       return true;
+       return $r;
 }
 
 /**