Archive: Namaste PHP AMQP framework v1.0 (2017-2020)
952 days continuous production uptime, 40k+ tp/s single node. Original corpo Bitbucket history not included — clean archive commit.
This commit is contained in:
342
brokers/adminLogsBroker.php
Normal file
342
brokers/adminLogsBroker.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
/**
|
||||
* adminSyslogBroker.php -- the syslog broker client
|
||||
*
|
||||
*
|
||||
* @author mike@givingassistant.org
|
||||
* @version 1.0
|
||||
*
|
||||
*
|
||||
* HISTORY:
|
||||
* ========
|
||||
* 09-16-19 mks DB-113: original coding
|
||||
* 07-28-20 mks DB-156: broker self-registration installed
|
||||
*
|
||||
*/
|
||||
//use PhpAmqpLib\Connection\AMQPStreamConnection;
|
||||
use PhpAmqpLib\Channel\AMQPChannel;
|
||||
use PhpAmqpLib\Connection\AMQPStreamConnection;
|
||||
use PhpAmqpLib\Exception\AMQPRuntimeException;
|
||||
|
||||
//use PhpAmqpLib\Message\AMQPMessage;
|
||||
//use PhpAmqpLib\Exception\AMQPTimeoutException;
|
||||
|
||||
pcntl_async_signals(true); // enable asynchronous signal handling (PHP 7.1)
|
||||
$_REDIRECT = true;
|
||||
$topDir = dirname(__DIR__);
|
||||
// load the lite version of the framework
|
||||
@require_once($topDir . '/config/sneakerstrap.inc'); // can't be constants b/c this loads the constants
|
||||
$res = 'LOGS: ';
|
||||
|
||||
// event management for children
|
||||
$syslogServiceConfig = gasConfig::$settings[CONFIG_BROKER_SERVICES][CONFIG_ADMIN];
|
||||
$numberChildren = $syslogServiceConfig[CONFIG_BROKER_INSTANCES][CONFIG_LOG_BROKER];
|
||||
$requestsPerInstance = (empty($syslogServiceConfig[CONFIG_BROKER_REQUEST_LIMIT])) ? NUMBER_C : $syslogServiceConfig[CONFIG_BROKER_REQUEST_LIMIT];
|
||||
$numberChildren = ($numberChildren < 1) ? 1 : $numberChildren; // todo -- should this be = 2??
|
||||
$runningBrokers = $numberChildren;
|
||||
$requestCounter = 0;
|
||||
$myRequestsPerInstance = 0;
|
||||
$startingMemory = 0;
|
||||
$groot = rtrim($res, COLON) . UDASH . guid(); // root guid
|
||||
consoleLog($res, CON_SUCCESS, sprintf(INFO_BROKER_STARTUP, substr(basename(__FILE__), 0, -4), $groot));
|
||||
$parentLog = new gacErrorLogger();
|
||||
$errors = null;
|
||||
$file = rtrim(basename(__FILE__), DOT . FILE_TYPE_PHP);
|
||||
$service = ENV_ADMIN;
|
||||
|
||||
if (!validateService($service, $errors)) {
|
||||
$hdr = sprintf(INFO_LOC, $file, __LINE__);
|
||||
$msg = sprintf(ERROR_SERVICE_REG, $file, $service);
|
||||
$parentLog->fatal($hdr . $msg);
|
||||
$parentLog->__destruct();
|
||||
unset($parentLog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// set-up the replacement signal handler that will be called on a child's death //
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//declare( ticks = 1);
|
||||
function sigHandler($_sig) {
|
||||
global $numberChildren;
|
||||
switch ($_sig) {
|
||||
case SIGCHLD :
|
||||
$numberChildren--;
|
||||
while (($pid = pcntl_wait($_sig, WNOHANG)) > 0) {
|
||||
@pcntl_wexitstatus($_sig);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
pcntl_signal(SIGCLD, 'sigHandler');
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// set-up the forking function so that it can be called initially or on a SIGCLD event //
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
function forkMe()
|
||||
{
|
||||
global $thisWatcher, $eos, $res, $parentLog, $requestsPerInstance, $myRequestsPerInstance, $startingMemory, $groot;
|
||||
$myRequestsPerInstance = $requestsPerInstance + (mt_rand(0, 2) * 10) + mt_rand(0, 9);
|
||||
$startingMemory = memory_get_usage(true);
|
||||
|
||||
$thisPid = pcntl_fork();
|
||||
|
||||
switch ($thisPid) {
|
||||
case -1 : // error
|
||||
$cmsg = ERROR_FORK_FAILED . $thisWatcher;
|
||||
$parentLog->fatal($cmsg);
|
||||
die(getDateTime() . CON_ERROR . $res . $cmsg . $eos);
|
||||
break;
|
||||
case 0 : // child (broker daemon)
|
||||
// replace the sigcld signal handler
|
||||
pcntl_signal(SIGCLD, SIG_DFL);
|
||||
$thisPid = getmypid();
|
||||
|
||||
$childGUID = rtrim($res, COLON) . UDASH . guid();
|
||||
|
||||
try {
|
||||
// toss the childGUID unto cache because it does not propagate down to the callback method
|
||||
gasCache::sysAdd(($groot . UDASH . $thisPid), $childGUID);
|
||||
|
||||
// create the child logger object
|
||||
/** @var gacErrorLogger $childLog */
|
||||
$childLog = new gacErrorLogger();
|
||||
|
||||
$queueTag = gasConfig::$settings[CONFIG_BROKER_SERVICES][CONFIG_BROKER_QUEUE_TAG];
|
||||
// $exchange = BROKER_EXCHANGE_A1;
|
||||
|
||||
$queue = $queueTag . BROKER_QUEUE_LOGS;
|
||||
/** @var AMQPStreamConnection $brokerConnection */
|
||||
$brokerConnection = gasResourceManager::fetchResource(RESOURCE_ADMIN);
|
||||
if (is_null($brokerConnection)) {
|
||||
$hdr = basename(__FILE__) . AT . __LINE__ . COLON;
|
||||
$childLog->fatal($hdr . ERROR_RESOURCE_404 . RESOURCE_ADMIN . COLON . BROKER_QUEUE_LOGS);
|
||||
consoleLog($res, CON_ERROR,$hdr . ERROR_RESOURCE_404 . RESOURCE_ADMIN . COLON . BROKER_QUEUE_LOGS);
|
||||
exit(SHELL_FAILURE); // shell-script exit value for fail
|
||||
}
|
||||
|
||||
// declare the channel...
|
||||
$brokerChannel = $brokerConnection->channel();
|
||||
// declare the topic exchange for topic-logging
|
||||
$brokerChannel->exchange_declare(EXCHANGE_NAME_TOPIC_LOGS, EXCHANGE_TYPE_TOPIC, false, false, false);
|
||||
// declare the channel queue and create the queue name
|
||||
list($queueName, ,) = $brokerChannel->queue_declare($queue);
|
||||
// this broker handles all messages passed to the topic_logs exchange (LOGS and METRICS)
|
||||
$brokerChannel->queue_bind($queueName, EXCHANGE_NAME_TOPIC_LOGS, EXCHANGE_QUEUE_BINDING_ALL);
|
||||
} catch (AMQPRuntimeException | Throwable $t) {
|
||||
$hdr = basename(__FILE__) . AT . __LINE__ . COLON;
|
||||
@handleExceptionMessaging($hdr, $t->getMessage(), $foo, true);
|
||||
exit(SHELL_FAILURE);
|
||||
}
|
||||
|
||||
// register the broker child start-up as a system-event
|
||||
$data = [
|
||||
SYSTEM_EVENT_NAME => SYSEV_NAME_CHILD_REG,
|
||||
SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER,
|
||||
SYSTEM_EVENT_BROKER_ROOT_GUID => $groot,
|
||||
SYSTEM_EVENT_BROKER_GUID => $childGUID,
|
||||
SYSTEM_EVENT_KEY => SYSEV_CHILD_RPI,
|
||||
SYSTEM_EVENT_VAL => $myRequestsPerInstance,
|
||||
SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__
|
||||
];
|
||||
@postSystemEvent($data, $childGUID, $childLog);
|
||||
|
||||
register_shutdown_function(BROKER_SHUTDOWN_FUNCTION, $brokerChannel, $brokerConnection, $res);
|
||||
$callback = function($_request) {
|
||||
$startTime = gasStatic::doingTime();
|
||||
global $requestCounter, $res, $eos, $myRequestsPerInstance, $startingMemory, $groot, $service;
|
||||
/** @var AMQPChannel $brokerChannel */
|
||||
global $brokerChannel;
|
||||
/** @var PhpAmqpLib\Connection\AMQPStreamConnection $brokerConnection */
|
||||
global $brokerConnection;
|
||||
|
||||
$childGUID = gasCache::sysGet(($groot . UDASH . getmypid()));
|
||||
if (gasConfig::$settings[CONFIG_DEBUG]) {
|
||||
consoleLog($res, CON_DEBUG, 'Child GUID: ' . $childGUID);
|
||||
consoleLog($res,CON_DEBUG, 'root GUID: ' . $groot);
|
||||
}
|
||||
|
||||
$requestCounter++;
|
||||
$returnData = null;
|
||||
$eventTimer = false;
|
||||
$request = null;
|
||||
$eventSuccess = false;
|
||||
$conMsg = '';
|
||||
$errorList = array();
|
||||
$thisPid = getmypid();
|
||||
$eventGUID = guid();
|
||||
$ogGUID = '';
|
||||
|
||||
// set-up the call-back (relative to the broker) logger
|
||||
$callBackLog = new gacErrorLogger($eventGUID, false);
|
||||
|
||||
if (!firstPassPayloadValidation($_request, $service, $msg, $request, $eventGUID)) {
|
||||
$conMsg = $msg;
|
||||
$callBackLog->info($msg);
|
||||
$event = BROKER_QUEUE_SYSLOG . '(' . ERROR_DATA_VALIDATION_FIRST_PASS . ')';
|
||||
} elseif (!validateMetaData($request, $errorList)) {
|
||||
for ($index = 0, $last = count($errorList); $index < $last; $index++) {
|
||||
$conMsg .= $errorList[$index] . $eos;
|
||||
$callBackLog->error($errorList[$index]);
|
||||
}
|
||||
$conMsg = rtrim($conMsg, $eos);
|
||||
$event = BROKER_QUEUE_LOGS . '(' . ERROR_META_VALIDATION_SECOND_PASS . ')';
|
||||
} else {
|
||||
$event = BROKER_QUEUE_LOGS . '(' . $request[BROKER_REQUEST] . ')';
|
||||
switch ($request[BROKER_REQUEST]) {
|
||||
case BROKER_REQUEST_SHUTDOWN :
|
||||
// /** @noinspection PhpUndefinedFieldInspection PhpUndefinedMethodInspection */
|
||||
// $_request->delivery_info[BROKER_CHANNEL]->basic_cancel($_request->delivery_info[BROKER_DELIVERY_TAG]);
|
||||
$conMsg = SUCCESS_SHUTDOWN;
|
||||
$eventSuccess = true;
|
||||
break;
|
||||
|
||||
case BROKER_REQUEST_PING :
|
||||
$conMsg = SUCCESS_PING . BROKER_QUEUE_SYSLOG;
|
||||
$eventSuccess = true;
|
||||
break;
|
||||
|
||||
case BROKER_REQUEST_LOG :
|
||||
$callBackLog->errStack = $request[BROKER_DATA];
|
||||
$callBackLog->writeLogMessage();
|
||||
if ($callBackLog->status) {
|
||||
$conMsg = SUCCESS_EVENT . BROKER_REQUEST_LOG;
|
||||
$eventSuccess = true;
|
||||
} else {
|
||||
$conMsg = basename(__FILE__) . COLON_NS . __LINE__ . COLON . FAIL_EVENT . BROKER_REQUEST_LOG;
|
||||
}
|
||||
break;
|
||||
|
||||
case BROKER_REQUEST_MET :
|
||||
$callBackLog->errStack = $request[BROKER_DATA];
|
||||
$callBackLog->writeLogMessage(true);
|
||||
if ($callBackLog->status) {
|
||||
$conMsg = SUCCESS_EVENT . BROKER_REQUEST_MET;
|
||||
$eventSuccess = true;
|
||||
} else {
|
||||
$conMsg = basename(__FILE__) . COLON_NS . __LINE__ . COLON . FAIL_EVENT . BROKER_REQUEST_MET;
|
||||
}
|
||||
break;
|
||||
|
||||
default :
|
||||
$conMsg = ERROR_BROKER_EVENT_UNKNOWN . $request[BROKER_REQUEST];
|
||||
$callBackLog->warn(ERROR_BROKER_EVENT_UNKNOWN . $request[BROKER_REQUEST]);
|
||||
// todo - not a supported event so log something dire
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$eventSuccess and empty($conMsg)) {
|
||||
$conMsg = ERROR_FINE_PICKLE;
|
||||
}
|
||||
if (!empty($conMsg)) {
|
||||
consoleLog($res, (($eventSuccess) ? CON_SUCCESS : CON_ERROR), $conMsg . sprintf(ERROR_EVENT_COUNT, $requestCounter, $myRequestsPerInstance));
|
||||
}
|
||||
// /** @noinspection PhpUndefinedFieldInspection PhpUndefinedMethodInspection */
|
||||
// $_request->delivery_info[BROKER_CHANNEL]->basic_ack($_request->delivery_info[BROKER_DELIVERY_TAG]);
|
||||
|
||||
// get the broker-event processing time
|
||||
$eventTime = gasStatic::doingTime($startTime);
|
||||
|
||||
// log a system-event for the event -- unlike the other system events, we're not going to submit
|
||||
// this one via a broker - which is standard but, instead, we're going to write the record out
|
||||
// directly since doing otherwise would cause an infinite loop in processing.
|
||||
if ($eventTime and $eventTimer) {
|
||||
$data = [
|
||||
SYSTEM_EVENT_NAME => SYSEV_NAME_EVENT_TIMER,
|
||||
SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER,
|
||||
SYSTEM_EVENT_BROKER_ROOT_GUID => $groot,
|
||||
SYSTEM_EVENT_BROKER_GUID => $childGUID,
|
||||
DB_EVENT_GUID => $eventGUID,
|
||||
SYSTEM_EVENT_COUNT => $requestCounter,
|
||||
SYSTEM_EVENT_COUNT_TOTAL => $myRequestsPerInstance,
|
||||
SYSTEM_EVENT_TIMER => $eventTime,
|
||||
SYSTEM_EVENT_BROKER_EVENT => $event,
|
||||
SYSTEM_EVENT_META_DATA => $request[BROKER_META_DATA],
|
||||
SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__
|
||||
];
|
||||
if (!empty($ogGUID)) $data[SYSTEM_EVENT_OGUID] = $ogGUID;
|
||||
@postSystemEvent($data, $eventGUID, $callBackLog);
|
||||
}
|
||||
|
||||
// exit the child if we've reached the request limit
|
||||
if ($requestCounter >= $myRequestsPerInstance) {
|
||||
if (getmypid() == $thisPid) {
|
||||
$meta = [
|
||||
META_SESSION_IP => STRING_SESSION_HOME,
|
||||
META_SESSION_DAEMON => 1,
|
||||
META_SESSION_MISC => INFO_BROKER_RECYCLE,
|
||||
META_EVENT_GUID => $eventGUID
|
||||
];
|
||||
$data = [
|
||||
SYSTEM_EVENT_NAME => SYSEV_NAME_BROKER_RECYCLE,
|
||||
SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER,
|
||||
SYSTEM_EVENT_BROKER_ROOT_GUID => $groot,
|
||||
SYSTEM_EVENT_BROKER_GUID => $childGUID,
|
||||
DB_EVENT_GUID => $eventGUID,
|
||||
SYSTEM_EVENT_START => $startingMemory,
|
||||
SYSTEM_EVENT_PEAK => memory_get_peak_usage(true),
|
||||
SYSTEM_EVENT_END => memory_get_usage(true),
|
||||
SYSTEM_EVENT_BROKER_EVENT => $event,
|
||||
SYSTEM_EVENT_COUNT => $requestCounter,
|
||||
SYSTEM_EVENT_COUNT_TOTAL => $myRequestsPerInstance,
|
||||
SYSTEM_EVENT_META_DATA => $meta,
|
||||
SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__
|
||||
];
|
||||
@postSystemEvent($data, $eventGUID, $callBackLog);
|
||||
gasCache::sysDel(($groot . UDASH . $thisPid));
|
||||
}
|
||||
consoleLog($res, CON_SYSTEM, INFO_BROKER_REQ_COUNT);
|
||||
if (is_object($brokerChannel)) $brokerChannel->close();
|
||||
if (is_object($brokerConnection)) $brokerConnection->close();
|
||||
exit(0);
|
||||
}
|
||||
};
|
||||
consoleLog($res, CON_SYSTEM, sprintf(INFO_BROKER_QUEUE_ESTABLISHED, BROKER_QUEUE_SYSLOG, $thisPid, $myRequestsPerInstance));
|
||||
$brokerChannel->basic_consume($queue, '', false, true, false, false, $callback);
|
||||
while (count($brokerChannel->callbacks)) {
|
||||
try {
|
||||
$brokerChannel->wait();
|
||||
} catch (Throwable $t) {
|
||||
$hdr = sprintf(INFO_LOC, basename(__FILE__), __LINE__);
|
||||
@handleExceptionMessaging($hdr, $t->getMessage(), $foo, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 1 : // parent
|
||||
// does nothing
|
||||
break;
|
||||
}
|
||||
return($thisPid);
|
||||
}
|
||||
|
||||
for ($numBrokers = 0; $numBrokers < $runningBrokers; $numBrokers++) {
|
||||
$childrenPidList[] = forkMe();
|
||||
}
|
||||
consoleLog($res, CON_SUCCESS, sprintf(INFO_BROKER_PARENT_STARTED, count($childrenPidList), BROKER_QUEUE_SYSLOG));
|
||||
|
||||
// "register" the broker instantiation event
|
||||
$data = [
|
||||
SYSTEM_EVENT_NAME => SYSEV_NAME_GROOT_REG,
|
||||
SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER,
|
||||
SYSTEM_EVENT_BROKER_ROOT_GUID => $groot,
|
||||
SYSTEM_EVENT_KEY => STRING_NUMBER_CHILDREN,
|
||||
SYSTEM_EVENT_VAL => $numberChildren,
|
||||
SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__,
|
||||
SYSTEM_EVENT_NOTES => BROKER_SYSEV_REG . rtrim($res, ": ")
|
||||
];
|
||||
@postSystemEvent($data, $groot, $parentLog);
|
||||
|
||||
// the parent process continues to run, waking-up every second to monitor it's children...
|
||||
// when a child dies, it's death-rattle is caught and the child is replaced with a new process.
|
||||
while (count($childrenPidList)) {
|
||||
$lastPid = 0;
|
||||
$newPidList = null;
|
||||
$result = pcntl_waitpid(0, $status); // detect any sigchld from the parent-group
|
||||
if (in_array($result, $childrenPidList)) {
|
||||
$key = array_search($result, $childrenPidList);
|
||||
array_splice($childrenPidList, $key, 1);
|
||||
// process has already exited -- restart it
|
||||
$childrenPidList[] = forkMe();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user