]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge remote-tracking branch 'upstream/develop' into 1610-performance
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 use \Friendica\Core\Config;
14 use \Friendica\Core\PConfig;
15
16 require_once("boot.php");
17
18 function poller_run(&$argv, &$argc){
19         global $a, $db;
20
21         if(is_null($a)) {
22                 $a = new App;
23         }
24
25         if(is_null($db)) {
26                 @include(".htconfig.php");
27                 require_once("include/dba.php");
28                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
29                 unset($db_host, $db_user, $db_pass, $db_data);
30         };
31
32         // Quit when in maintenance
33         if (get_config('system', 'maintenance', true))
34                 return;
35
36         $a->start_process();
37
38         $mypid = getmypid();
39
40         if ($a->max_processes_reached())
41                 return;
42
43         if (poller_max_connections_reached())
44                 return;
45
46         if (App::maxload_reached())
47                 return;
48
49         // Checking the number of workers
50         if (poller_too_much_workers()) {
51                 poller_kill_stale_workers();
52                 return;
53         }
54
55         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
56                 // Run the cron job that calls all other jobs
57                 proc_run(PRIORITY_MEDIUM, "include/cron.php");
58
59                 // Run the cronhooks job separately from cron for being able to use a different timing
60                 proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
61
62                 // Cleaning dead processes
63                 poller_kill_stale_workers();
64         } else
65                 // Sleep four seconds before checking for running processes again to avoid having too many workers
66                 sleep(4);
67
68         // Checking number of workers
69         if (poller_too_much_workers())
70                 return;
71
72         $cooldown = Config::get("system", "worker_cooldown", 0);
73
74         $starttime = time();
75
76         while ($r = poller_worker_process()) {
77
78                 // Quit when in maintenance
79                 if (get_config('system', 'maintenance', true))
80                         return;
81
82                 // Constantly check the number of parallel database processes
83                 if ($a->max_processes_reached())
84                         return;
85
86                 // Constantly check the number of available database connections to let the frontend be accessible at any time
87                 if (poller_max_connections_reached())
88                         return;
89
90                 // Count active workers and compare them with a maximum value that depends on the load
91                 if (poller_too_much_workers())
92                         return;
93
94                 $upd = q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `pid` = 0",
95                         dbesc(datetime_convert()),
96                         intval($mypid),
97                         intval($r[0]["id"]));
98
99                 if (!$upd) {
100                         logger("Couldn't update queue entry ".$r[0]["id"]." - skip this execution", LOGGER_DEBUG);
101                         continue;
102                 }
103
104                 // Assure that there are no tasks executed twice
105                 $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
106                 if (!$id) {
107                         logger("Queue item ".$r[0]["id"]." vanished - skip this execution", LOGGER_DEBUG);
108                         continue;
109                 } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
110                         logger("Entry for queue item ".$r[0]["id"]." wasn't stored - we better stop here", LOGGER_DEBUG);
111                         return;
112                 } elseif ($id[0]["pid"] != $mypid) {
113                         logger("Queue item ".$r[0]["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
114                         continue;
115                 }
116
117                 $argv = json_decode($r[0]["parameter"]);
118
119                 $argc = count($argv);
120
121                 // Check for existance and validity of the include file
122                 $include = $argv[0];
123
124                 if (!validate_include($include)) {
125                         logger("Include file ".$argv[0]." is not valid!");
126                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
127                         continue;
128                 }
129
130                 require_once($include);
131
132                 $funcname = str_replace(".php", "", basename($argv[0]))."_run";
133
134                 if (function_exists($funcname)) {
135                         logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
136
137                         // For better logging create a new process id for every worker call
138                         // But preserve the old one for the worker
139                         $old_process_id = $a->process_id;
140                         $a->process_id = uniqid("wrk", true);
141
142                         $funcname($argv, $argc);
143
144                         $a->process_id = $old_process_id;
145
146                         if ($cooldown > 0) {
147                                 logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
148                                 sleep($cooldown);
149                         }
150
151                         logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
152
153                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
154                 } else
155                         logger("Function ".$funcname." does not exist");
156
157                 // Quit the poller once every hour
158                 if (time() > ($starttime + 3600))
159                         return;
160         }
161
162 }
163
164 /**
165  * @brief Checks if the number of database connections has reached a critical limit.
166  *
167  * @return bool Are more than 3/4 of the maximum connections used?
168  */
169 function poller_max_connections_reached() {
170
171         // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
172         $max = get_config("system", "max_connections");
173
174         // Fetch the percentage level where the poller will get active
175         $maxlevel = get_config("system", "max_connections_level");
176         if ($maxlevel == 0)
177                 $maxlevel = 75;
178
179         if ($max == 0) {
180                 // the maximum number of possible user connections can be a system variable
181                 $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
182                 if ($r)
183                         $max = $r[0]["Value"];
184
185                 // Or it can be granted. This overrides the system variable
186                 $r = q("SHOW GRANTS");
187                 if ($r)
188                         foreach ($r AS $grants) {
189                                 $grant = array_pop($grants);
190                                 if (stristr($grant, "GRANT USAGE ON"))
191                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match))
192                                                 $max = $match[1];
193                         }
194         }
195
196         // If $max is set we will use the processlist to determine the current number of connections
197         // The processlist only shows entries of the current user
198         if ($max != 0) {
199                 $r = q("SHOW PROCESSLIST");
200                 if (!$r)
201                         return false;
202
203                 $used = count($r);
204
205                 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
206
207                 $level = ($used / $max) * 100;
208
209                 if ($level >= $maxlevel) {
210                         logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
211                         return true;
212                 }
213         }
214
215         // We will now check for the system values.
216         // This limit could be reached although the user limits are fine.
217         $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
218         if (!$r)
219                 return false;
220
221         $max = intval($r[0]["Value"]);
222         if ($max == 0)
223                 return false;
224
225         $r = q("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
226         if (!$r)
227                 return false;
228
229         $used = intval($r[0]["Value"]);
230         if ($used == 0)
231                 return false;
232
233         logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
234
235         $level = $used / $max * 100;
236
237         if ($level < $maxlevel)
238                 return false;
239
240         logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
241         return true;
242 }
243
244 /**
245  * @brief fix the queue entry if the worker process died
246  *
247  */
248 function poller_kill_stale_workers() {
249         $r = q("SELECT `pid`, `executed`, `priority`, `parameter` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
250
251         if (!dbm::is_result($r)) {
252                 // No processing here needed
253                 return;
254         }
255
256         foreach($r AS $pid)
257                 if (!posix_kill($pid["pid"], 0))
258                         q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
259                                 intval($pid["pid"]));
260                 else {
261                         // Kill long running processes
262
263                         // Check if the priority is in a valid range
264                         if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE)))
265                                 $pid["priority"] = PRIORITY_MEDIUM;
266
267                         // Define the maximum durations
268                         $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
269                         $max_duration = $max_duration_defaults[$pid["priority"]];
270
271                         $argv = json_decode($pid["parameter"]);
272                         $argv[0] = basename($argv[0]);
273
274                         // How long is the process already running?
275                         $duration = (time() - strtotime($pid["executed"])) / 60;
276                         if ($duration > $max_duration) {
277                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
278                                 posix_kill($pid["pid"], SIGTERM);
279
280                                 // We killed the stale process.
281                                 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
282                                 // Additionally we are lowering the priority.
283                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `created` = '%s',
284                                                         `priority` = %d, `pid` = 0 WHERE `pid` = %d",
285                                         dbesc(datetime_convert()),
286                                         intval(PRIORITY_NEGLIGIBLE),
287                                         intval($pid["pid"]));
288                         } else
289                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
290                 }
291 }
292
293 function poller_too_much_workers() {
294
295
296         $queues = get_config("system", "worker_queues");
297
298         if ($queues == 0)
299                 $queues = 4;
300
301         $maxqueues = $queues;
302
303         $active = poller_active_workers();
304
305         // Decrease the number of workers at higher load
306         $load = current_load();
307         if($load) {
308                 $maxsysload = intval(get_config('system','maxloadavg'));
309                 if($maxsysload < 1)
310                         $maxsysload = 50;
311
312                 $maxworkers = $queues;
313
314                 // Some magical mathemathics to reduce the workers
315                 $exponent = 3;
316                 $slope = $maxworkers / pow($maxsysload, $exponent);
317                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
318
319                 $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00'");
320                 $entries = $s[0]["total"];
321
322                 if (Config::get("system", "worker_fastlane", false) AND ($queues > 0) AND ($entries > 0) AND ($active >= $queues)) {
323                         $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority` LIMIT 1");
324                         $top_priority = $s[0]["priority"];
325
326                         $s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` != '0000-00-00 00:00:00' LIMIT 1",
327                                 intval($top_priority));
328                         $high_running = dbm::is_result($s);
329
330                         if (!$high_running AND ($top_priority > PRIORITY_UNDEFINED) AND ($top_priority < PRIORITY_NEGLIGIBLE)) {
331                                 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
332                                 $queues = $active + 1;
333                         }
334                 }
335
336                 // Create a list of queue entries grouped by their priority
337                 $running = array(PRIORITY_CRITICAL => 0,
338                                 PRIORITY_HIGH => 0,
339                                 PRIORITY_MEDIUM => 0,
340                                 PRIORITY_LOW => 0,
341                                 PRIORITY_NEGLIGIBLE => 0);
342
343                 $r = q("SELECT COUNT(*) AS `running`, `priority` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` GROUP BY `priority`");
344                 if (dbm::is_result($r))
345                         foreach ($r AS $process)
346                                 $running[$process["priority"]] = $process["running"];
347
348                 $processlist = "";
349                 $r = q("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` GROUP BY `priority`");
350                 if (dbm::is_result($r))
351                         foreach ($r as $entry) {
352                                 if ($processlist != "")
353                                         $processlist .= ", ";
354                                 $processlist .= $entry["priority"].":".$running[$entry["priority"]]."/".$entry["entries"];
355                         }
356
357                 logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries." (".$processlist.") - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
358
359                 // Are there fewer workers running as possible? Then fork a new one.
360                 if (!get_config("system", "worker_dont_fork") AND ($queues > ($active + 1)) AND ($entries > 1)) {
361                         logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
362                         $args = array("php", "include/poller.php", "no_cron");
363                         $a = get_app();
364                         $a->proc_run($args);
365                 }
366         }
367
368         return($active >= $queues);
369 }
370
371 function poller_active_workers() {
372         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
373
374         return($workers[0]["processes"]);
375 }
376
377 /**
378  * @brief Check if we should pass some slow processes
379  *
380  * When the active processes of the highest priority are using more than 2/3
381  * of all processes, we let pass slower processes.
382  *
383  * @param string $highest_priority Returns the currently highest priority
384  * @return bool We let pass a slower process than $highest_priority
385  */
386 function poller_passing_slow(&$highest_priority) {
387
388         $highest_priority = 0;
389
390         $r = q("SELECT `priority`
391                 FROM `process`
392                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid`
393                 WHERE `process`.`command` = 'poller.php'");
394
395         // No active processes at all? Fine
396         if (!dbm::is_result($r))
397                 return(false);
398
399         $priorities = array();
400         foreach ($r AS $line)
401                 $priorities[] = $line["priority"];
402
403         // Should not happen
404         if (count($priorities) == 0)
405                 return(false);
406
407         $highest_priority = min($priorities);
408
409         // The highest process is already the slowest one?
410         // Then we quit
411         if ($highest_priority == PRIORITY_NEGLIGIBLE)
412                 return(false);
413
414         $high = 0;
415         foreach ($priorities AS $priority)
416                 if ($priority == $highest_priority)
417                         ++$high;
418
419         logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
420         $passing_slow = (($high/count($priorities)) > (2/3));
421
422         if ($passing_slow)
423                 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
424
425         return($passing_slow);
426 }
427
428 /**
429  * @brief Returns the next worker process
430  *
431  * @return string SQL statement
432  */
433
434 function poller_worker_process() {
435
436         // Check if we should pass some low priority process
437         $highest_priority = 0;
438
439         if (poller_passing_slow($highest_priority)) {
440                 // Are there waiting processes with a higher priority than the currently highest?
441                 $r = q("SELECT * FROM `workerqueue`
442                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` < %d
443                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
444                 if (dbm::is_result($r))
445                         return $r;
446
447                 // Give slower processes some processing time
448                 $r = q("SELECT * FROM `workerqueue`
449                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` > %d
450                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
451         }
452
453         // If there is no result (or we shouldn't pass lower processes) we check without priority limit
454         if (($highest_priority == 0) OR !dbm::is_result($r))
455                 $r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority`, `created` LIMIT 1");
456
457         return $r;
458 }
459
460 if (array_search(__file__,get_included_files())===0){
461         poller_run($_SERVER["argv"],$_SERVER["argc"]);
462
463         get_app()->end_process();
464
465         killme();
466 }
467 ?>