|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
The request-fabric router/load-balancer (see the file header for the full role description). More...
#include <RequestGateway.h>
Public Member Functions | |
| RequestGateway (uint32 id, const char *version=NULL) | |
| virtual | ~RequestGateway () |
| bool | init (const char *sslCertPath=NULL, const char *sslKeyPath=NULL) |
| Start the gateway threads and network stack. | |
| bool | setWebServerInfo (const char *name, const char *rootdir, const char *indexfile) |
| Configure the built-in file web server. | |
| bool | setExternalAPILabel (const char *label) |
| Set the URI label under which the external (public) API is exposed. | |
| bool | setInternalAPILabel (const char *label) |
| Set the URI label under which the internal (management) API is exposed. | |
| bool | setCacheFiles (bool cache) |
| Enable/disable HTTP caching headers for served files. | |
| bool | setResponseType (const char *type) |
| Choose the reply serialisation ("xml" or "json"). | |
| bool | setLongRequestLimit (uint32 limit) |
| Cap concurrent long requests per executor. | |
| bool | setExecutorHeartbeatTimeout (uint32 timeout) |
| Set how long an executor may go silent before being declared dead (its queue is then redistributed). | |
| bool | setMaxExecutorRequestTimeout (uint32 timeout) |
| Set the maximum time a request may sit with an executor before being failed/retried. | |
| bool | setQueuingParameters (uint32 maxRequestQueueSize, uint32 maxRequestProcessingSize, uint32 priorityThreshold) |
| Tune queuing/backpressure behaviour. | |
| bool | addPort (uint16 port, uint8 encryption, bool enableHTTP=false, uint32 timeout=3000) |
| Open a listening port for clients/executors (call before init()). | |
| bool | addGateway (uint32 id, std::string addr, uint16 port) |
| Register a peer gateway (multi-gateway federation). | |
| bool | addAuthUser (const char *user, const char *password) |
| Add a basic-auth user for the web interface/APIs. | |
| bool | receiveNetworkEvent (NetworkEvent *evt, NetworkChannel *channel, uint64 conid) |
| NetworkReceiver hook: connect/disconnect events for clients and executors. | |
| bool | receiveMessage (DataMessage *msg, NetworkChannel *channel, uint64 conid) |
| NetworkReceiver hook: binary requests/replies/heartbeats from clients and executors. | |
| bool | receiveHTTPRequest (HTTPRequest *req, NetworkChannel *channel, uint64 conid) |
| NetworkReceiver hook: web ingress — serves files, APIs, or converts the request into an internal DataMessage request. | |
| bool | receiveWebsocketData (WebsocketData *wsData, NetworkChannel *channel, uint64 conid) |
| NetworkReceiver hook: WebSocket ingress carrying request payloads. | |
| Public Member Functions inherited from cmlabs::Runnable | |
| Runnable () | |
| Initialise flags: not running, allowed to continue. | |
| virtual | ~Runnable () |
| Destructor requests a stop (with the default timeout) before destruction proceeds. | |
| virtual bool | stop (uint32 timeout=200) |
| Ask the worker loop to finish and wait for it to do so. | |
| Public Member Functions inherited from cmlabs::NetworkReceiver | |
| NetworkReceiver () | |
| virtual bool | receiveTelnetLine (TelnetLine *line, NetworkChannel *channel, uint64 conid) |
| A Telnet line arrived. | |
| virtual bool | receiveHTTPReply (HTTPReply *reply, HTTPRequest *req, NetworkChannel *channel, uint64 conid) |
| An HTTP reply to an earlier async request arrived. | |
Static Public Member Functions | |
| static bool | UnitTest (uint16 port, uint32 executorCount, uint32 gatewayCount, uint32 clientCount, uint32 webCount, uint32 webSocketCount, double sendfreq, int32 reconnect, uint32 payload, uint32 processTime, uint32 runtime) |
| End-to-end stress test spinning up a gateway plus the given numbers of executors, extra gateways and binary/web/websocket test clients. | |
Protected Member Functions | |
| bool | runExec () |
| Executor-side worker loop: dispatch queued requests, watch heartbeats, redistribute dead executors' work. | |
| bool | runClient () |
| Client-side worker loop: route replies back to callers, expire timed-out requests. | |
| bool | callInternalAPI (const char *apiName, HTTPRequest *req, NetworkChannel *channel, uint64 conid) |
| Handle a management/API call on the internal API label. | |
| bool | callExternalAPI (const char *apiName, HTTPRequest *req, NetworkChannel *channel, uint64 conid) |
| Handle a public API call on the external API label. | |
| bool | sendRestartToExecutor (uint64 id) |
| Ask an executor to restart itself (sent when it misbehaves). | |
| bool | distributeDeadExecutorRequests (std::list< RequestReply * > &shortQueue, std::list< RequestReply * > &longQueue) |
| Requeue a dead executor's outstanding requests onto surviving executors. | |
| uint64 | getBestExecutorID (std::string requestString, uint32 reqSize, bool &isLongReq) |
| Pick the executor to receive a request — the core load-balancing decision, based on reported queue sizes, rolling latency stats and liveness. | |
| bool | replyToClient (DataMessage *msg) |
| Route a reply message back to the client that issued the request. | |
| bool | addToRequestQueue (DataMessage *msg, uint64 origin, uint64 conID, uint64 clientRef) |
| Wrap an incoming message in a RequestReply and enqueue it for dispatch. | |
| bool | addRequestReplyToRequestQueue (RequestReply *reply) |
| Enqueue an already-wrapped request (e.g. | |
| bool | addToCallLog (uint64 id, uint8 conType, uint8 messageType, bool longRequest, uint64 source, uint16 port, uint64 executorID, uint64 clientID, uint64 startTime, uint64 endTime, uint64 queueTime, uint64 processTime, uint8 requestType, uint32 requestSize, uint32 replySize, const char *status, const char *requestPath, const char *agent, const char *output) |
| Append one completed call to the rolling call log (trimmed to ::callLogMax). | |
Protected Attributes | |
| utils::Mutex | callLogMutex |
| Guards the call log. | |
| std::list< CallLogEntry * > | callLog |
| Rolling log of completed calls (owned). | |
| uint32 | callLogMax |
| Max entries kept in ::callLog. | |
| uint64 | callLogCount |
| Total calls logged since start. | |
| NetworkManager * | manager |
| Owned network stack (listeners + connections). | |
| NetworkChannel * | channel |
| Channel carrying all gateway traffic. | |
| std::map< uint64, RequestConnection > | executors |
| Connected executors by id. | |
| std::map< uint64, RequestConnection > | clients |
| Connected binary clients by id. | |
| std::map< uint64, RequestConnection > | webClients |
| Connected HTTP clients by id. | |
| std::map< uint64, RequestConnection > | webSockets |
| Connected WebSocket clients by id. | |
| utils::WaitQueue< RequestReply * > | execQ |
| Requests awaiting executor dispatch. | |
| utils::WaitQueuePointer< DataMessage * > | replyQ |
| Replies awaiting client routing. | |
| std::map< uint64, RequestReply * > | requestMap |
| In-flight requests by gateway ref id. | |
| std::vector< std::string > | longReqNames |
| Request names classified as long. | |
| std::map< std::string, uint64 > | httpAuth |
| Basic-auth users (user -> credential hash). | |
| utils::Mutex | mutex |
| Guards shared gateway state. | |
| uint64 | systemStartTime |
| Gateway start time (ms epoch), for uptime reporting. | |
| uint32 | threadIDClient |
| Client-side worker thread id. | |
| uint64 | lastRefID |
| Last issued gateway reference id. | |
| uint64 | clientSentCount |
| Messages sent to clients. | |
| uint64 | clientReceivedCount |
| Messages received from clients. | |
| uint64 | execSentCount |
| Messages sent to executors. | |
| uint64 | execReceivedCount |
| Messages received from executors. | |
| uint32 | id |
| This gateway's id. | |
| uint16 | port |
| Primary listening port. | |
| uint32 | longReqLimit |
| Concurrent long-request cap per executor. | |
| uint32 | executorHeartbeatTimeout |
| Silence threshold before an executor is declared dead (ms). | |
| uint32 | maxRequestQueueSize |
| Backpressure: max queued requests before TOOBUSY. | |
| uint32 | maxRequestProcessingSize |
| Backpressure: max requests dispatched concurrently. | |
| uint32 | priorityThreshold |
| Queue length above which priority ordering applies. | |
| uint32 | maxExecutorRequestTimeoutMS |
| Max time a request may sit with an executor (ms). | |
| bool | sslSupport |
| True when an SSL certificate was configured. | |
| bool | cacheFiles |
| Send caching headers for served files. | |
| bool | replyXML |
| Reply in XML (true) or JSON (false). | |
| std::string | webServerName |
| Server header name of the built-in web server. | |
| std::string | rootdir |
| Document root for file serving. | |
| std::string | indexFilename |
| Default index file name. | |
| std::string | versionString |
| Version shown in status pages. | |
| std::string | internalAPITitle |
| URI label of the internal (management) API. | |
| std::string | externalAPITitle |
| URI label of the external (public) API. | |
| MovingAverage | shortAvgStats |
| Rolling latency stats for short requests. | |
| MovingAverage | longAvgStats |
| Rolling latency stats for long requests. | |
| Protected Attributes inherited from cmlabs::Runnable | |
| uint32 | threadID |
| ThreadManager slot ID of the worker thread (0 until known). | |
| bool | shouldContinue |
| Loop-continuation flag; cleared by stop(). | |
| bool | isRunning |
| Set by the worker while its loop is active. | |
Friends | |
| THREAD_RET THREAD_FUNCTION_CALL | RequestGatewayExecRun (THREAD_ARG arg) |
| Thread entry point for the gateway's executor-side loop. | |
| THREAD_RET THREAD_FUNCTION_CALL | RequestGatewayClientRun (THREAD_ARG arg) |
| Thread entry point for the gateway's client-side loop. | |
The request-fabric router/load-balancer (see the file header for the full role description).
Runs two worker threads: one servicing executor traffic (runExec()) and one servicing client traffic (runClient()). All ingress arrives through the NetworkReceiver callbacks below; ports are added with addPort() and may simultaneously carry binary, HTTP and WebSocket traffic thanks to protocol auto-detection.
Definition at line 138 of file RequestGateway.h.
| cmlabs::RequestGateway::RequestGateway | ( | uint32 | id, |
| const char * | version = NULL ) |
| id | Gateway id (reported to peers). |
| version | Optional version string shown in status pages. |
Definition at line 316 of file RequestGateway.cpp.
References cacheFiles, callLogCount, callLogMax, channel, clientReceivedCount, clientSentCount, execReceivedCount, execSentCount, executorHeartbeatTimeout, externalAPITitle, cmlabs::GetTimeNow(), internalAPITitle, cmlabs::Runnable::isRunning, lastRefID, longReqLimit, manager, maxExecutorRequestTimeoutMS, maxRequestProcessingSize, maxRequestQueueSize, priorityThreshold, replyXML, cmlabs::Runnable::shouldContinue, sslSupport, systemStartTime, cmlabs::Runnable::threadID, threadIDClient, versionString, and webServerName.
Referenced by RequestGatewayClientRun, RequestGatewayExecRun, and UnitTest().
|
virtual |
Definition at line 348 of file RequestGateway.cpp.
References cmlabs::ThreadManager::IsThreadRunning(), manager, mutex, requestMap, cmlabs::Runnable::shouldContinue, cmlabs::utils::Sleep(), cmlabs::Runnable::stop(), cmlabs::Runnable::threadID, and threadIDClient.
| bool cmlabs::RequestGateway::addAuthUser | ( | const char * | user, |
| const char * | password ) |
Add a basic-auth user for the web interface/APIs.
| user | Username. |
| password | Password. |
Definition at line 493 of file RequestGateway.cpp.
References base64_encode(), cmlabs::GetTimeNow(), httpAuth, and cmlabs::utils::StringFormat().
| bool cmlabs::RequestGateway::addGateway | ( | uint32 | id, |
| std::string | addr, | ||
| uint16 | port ) |
Register a peer gateway (multi-gateway federation).
| id | Gateway id. |
| addr | Host. |
| port | Port. |
Definition at line 503 of file RequestGateway.cpp.
References port.
| bool cmlabs::RequestGateway::addPort | ( | uint16 | port, |
| uint8 | encryption, | ||
| bool | enableHTTP = false, | ||
| uint32 | timeout = 3000 ) |
Open a listening port for clients/executors (call before init()).
| port | TCP port. |
| encryption | NOENC or SSLENC. |
| enableHTTP | Also accept HTTP/WebSocket on this port when true. |
| timeout | Protocol auto-detection timeout in ms. |
Definition at line 448 of file RequestGateway.cpp.
References channel, LOG_SYSTEM, LogPrint, manager, mutex, port, PROTOCOL_HTTP_SERVER, PROTOCOL_MESSAGE, SSLENC, and sslSupport.
Referenced by UnitTest().
|
protected |
Enqueue an already-wrapped request (e.g.
redistributed work).
Definition at line 665 of file RequestGateway.cpp.
References cmlabs::RequestQueue::addRequest(), clients, execQ, executors, cmlabs::FAILED, cmlabs::RequestReply::gatewayRef, getBestExecutorID(), cmlabs::RequestQueue::getCount(), cmlabs::DataMessage::getSize(), cmlabs::DataMessage::getString(), cmlabs::GetTimeAgeMS(), cmlabs::RequestReply::isLongReq, LOG_SYSTEM, LogPrint, cmlabs::RequestConnection::longReqQProcessingSize, cmlabs::RequestConnection::longReqQueue, maxRequestProcessingSize, maxRequestQueueSize, cmlabs::RequestReply::origin, cmlabs::RequestReply::peekRequestMessage(), priorityThreshold, cmlabs::PROCESSING, cmlabs::RequestReply::processor, replyQ, replyXML, cmlabs::DataMessage::setReference(), cmlabs::RequestReply::setStatus(), cmlabs::DataMessage::setString(), cmlabs::RequestConnection::shortReqQueue, cmlabs::TOOBUSY, webClients, and webSockets.
Referenced by addToRequestQueue(), and distributeDeadExecutorRequests().
|
protected |
Append one completed call to the rolling call log (trimmed to ::callLogMax).
Parameters mirror the CallLogEntry fields.
Definition at line 1839 of file RequestGateway.cpp.
References cmlabs::CallLogEntry::agent, callLog, callLogMax, callLogMutex, cmlabs::CallLogEntry::clientID, cmlabs::CallLogEntry::conType, cmlabs::CallLogEntry::endTime, cmlabs::CallLogEntry::executorID, cmlabs::CallLogEntry::longRequest, cmlabs::CallLogEntry::messageType, cmlabs::CallLogEntry::output, cmlabs::CallLogEntry::port, port, cmlabs::CallLogEntry::processTime, cmlabs::CallLogEntry::queueTime, cmlabs::CallLogEntry::replySize, cmlabs::CallLogEntry::requestPath, cmlabs::CallLogEntry::requestSize, cmlabs::CallLogEntry::requestType, cmlabs::CallLogEntry::source, cmlabs::CallLogEntry::startTime, and cmlabs::CallLogEntry::status.
Referenced by callInternalAPI(), receiveHTTPRequest(), and replyToClient().
|
protected |
Wrap an incoming message in a RequestReply and enqueue it for dispatch.
| msg | Request message (ownership transfers). |
| origin | Caller endpoint. |
| conID | Ingress connection id. |
| clientRef | Caller's reference id. |
Definition at line 642 of file RequestGateway.cpp.
References addRequestReplyToRequestQueue(), clientReceivedCount, cmlabs::RequestReply::clientRef, cmlabs::RequestReply::gatewayRef, GETIPADDRESSQUADPORT, cmlabs::RequestReply::giveRequestMessage(), lastRefID, cmlabs::RequestReply::origin, cmlabs::QUEUED, requestMap, cmlabs::DataMessage::setReference(), cmlabs::RequestReply::setStatus(), cmlabs::DataMessage::setString(), and cmlabs::utils::StringFormat().
Referenced by callExternalAPI(), receiveMessage(), and receiveWebsocketData().
|
protected |
Handle a public API call on the external API label.
Definition at line 1042 of file RequestGateway.cpp.
References addToRequestQueue(), cmlabs::utils::Ascii2Uint64(), channel, cmlabs::RequestConnection::clear(), cmlabs::RequestConnection::conID, cmlabs::HTTPRequest::convertToMessage(), cmlabs::HTTPRequest::getParameter(), cmlabs::GetTimeNow(), cmlabs::RequestConnection::lastConTime, cmlabs::DataMessage::setString(), cmlabs::HTTPRequest::source, and webClients.
Referenced by receiveHTTPRequest().
|
protected |
Handle a management/API call on the internal API label.
Definition at line 1067 of file RequestGateway.cpp.
References addToCallLog(), cmlabs::utils::Ascii2Uint32(), cmlabs::utils::Ascii2Uint64(), cacheFiles, callLog, callLogCount, callLogMutex, channel, clients, cmlabs::HTTPRequest::contentLength, cmlabs::HTTPReply::CreateAuthorizationReply(), cmlabs::HTTPReply::createFromFile(), cmlabs::HTTPReply::createPage(), cmlabs::HTTPRequest::endReceiveTime, executors, cmlabs::HTTPRequest::getBasicAuthorization(), cmlabs::HTTPRequest::getHeaderEntry(), GETIPADDRESSQUADPORT, cmlabs::HTTPRequest::getParameter(), cmlabs::HTTPReply::getSize(), cmlabs::HTTPRequest::getSize(), cmlabs::GetTimeAge(), cmlabs::GetTimeNow(), HTTP_OK, HTTP_SERVICE_NOT_AVAILABLE, httpAuth, cmlabs::HTTPRequest::ifModifiedSince, indexFilename, longAvgStats, maxRequestProcessingSize, maxRequestQueueSize, port, cmlabs::PrintTimeDifString(), replyXML, rootdir, sendRestartToExecutor(), shortAvgStats, cmlabs::HTTPRequest::source, stricmp, cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), systemStartTime, cmlabs::utils::TextStartsWith(), cmlabs::HTTPRequest::type, versionString, webClients, webServerName, and webSockets.
Referenced by receiveHTTPRequest().
|
protected |
Requeue a dead executor's outstanding requests onto surviving executors.
| shortQueue | Its short-request backlog. |
| longQueue | Its long-request backlog. |
Definition at line 2412 of file RequestGateway.cpp.
References addRequestReplyToRequestQueue(), executors, cmlabs::QUEUED, and cmlabs::RequestReply::setStatus().
Referenced by runExec().
|
protected |
Pick the executor to receive a request — the core load-balancing decision, based on reported queue sizes, rolling latency stats and liveness.
| requestString | Request name (matched against long-request names). |
| reqSize | Payload size. |
| isLongReq | Out: classification result. |
Definition at line 1798 of file RequestGateway.cpp.
References executors, longReqLimit, longReqNames, MAXVALUINT32, and cmlabs::utils::stristr().
Referenced by addRequestReplyToRequestQueue().
| bool cmlabs::RequestGateway::init | ( | const char * | sslCertPath = NULL, |
| const char * | sslKeyPath = NULL ) |
Start the gateway threads and network stack.
| sslCertPath | Optional PEM certificate enabling SSL ports. |
| sslKeyPath | Matching PEM private key. |
Definition at line 367 of file RequestGateway.cpp.
References cmlabs::ThreadManager::CreateThread(), LOG_SYSTEM, LogPrint, manager, mutex, RequestGatewayClientRun, RequestGatewayExecRun, cmlabs::Runnable::shouldContinue, sslSupport, cmlabs::Runnable::threadID, and threadIDClient.
Referenced by UnitTest().
|
virtual |
NetworkReceiver hook: web ingress — serves files, APIs, or converts the request into an internal DataMessage request.
Reimplemented from cmlabs::NetworkReceiver.
Definition at line 808 of file RequestGateway.cpp.
References addToCallLog(), cacheFiles, callExternalAPI(), callInternalAPI(), callLogCount, channel, cmlabs::HTTPReply::createFromFile(), cmlabs::HTTPRequest::endReceiveTime, externalAPITitle, cmlabs::HTTPRequest::getHeaderEntry(), cmlabs::HTTPReply::getSize(), cmlabs::HTTPRequest::getSize(), cmlabs::GetTimeAge(), cmlabs::GetTimeNow(), cmlabs::HTTPRequest::getURI(), cmlabs::HTTPRequest::ifModifiedSince, indexFilename, internalAPITitle, LOG_NETWORK, LogPrint, mutex, rootdir, cmlabs::HTTPRequest::source, cmlabs::utils::StringFormat(), cmlabs::HTTPRequest::type, and webServerName.
|
virtual |
NetworkReceiver hook: binary requests/replies/heartbeats from clients and executors.
Reimplemented from cmlabs::NetworkReceiver.
Definition at line 565 of file RequestGateway.cpp.
References addToRequestQueue(), channel, cmlabs::RequestConnection::clear(), clients, cmlabs::RequestConnection::conID, execReceivedCount, executors, cmlabs::DataMessage::getInt(), cmlabs::DataMessage::getReference(), cmlabs::DataMessage::getString(), cmlabs::GetTimeNow(), cmlabs::RequestConnection::lastConTime, cmlabs::RequestConnection::location, LOG_NETWORK, LOG_SYSTEM, LogPrint, longReqNames, mutex, replyQ, requestMap, cmlabs::DataMessage::setRecvTime(), cmlabs::SUCCESS, and cmlabs::utils::TextListSplit().
|
virtual |
NetworkReceiver hook: connect/disconnect events for clients and executors.
Reimplemented from cmlabs::NetworkReceiver.
Definition at line 507 of file RequestGateway.cpp.
References channel, clients, executors, cmlabs::GetTimeNow(), mutex, NETWORKEVENT_DISCONNECT, NETWORKEVENT_RECONNECT, cmlabs::NetworkEvent::type, webClients, and webSockets.
|
virtual |
NetworkReceiver hook: WebSocket ingress carrying request payloads.
Reimplemented from cmlabs::NetworkReceiver.
Definition at line 929 of file RequestGateway.cpp.
References addToRequestQueue(), channel, cmlabs::RequestConnection::clear(), cmlabs::RequestConnection::conID, cmlabs::WebsocketData::contentSize, cmlabs::JSONM::convertToMessage(), cmlabs::WebsocketData::data, cmlabs::JSONM::entries, cmlabs::JSONM::getJSON(), GetJSONChildValueString(), GetJSONChildValueUint64(), cmlabs::GetTimeNow(), HTTP_DELETE, HTTP_GET, HTTP_POST, HTTP_PUT, cmlabs::JSONM::isValid(), cmlabs::JSONM::jmSize, jsmn_init(), jsmn_parse(), JSMN_UNDEFINED, cmlabs::RequestConnection::lastConTime, LOG_NETWORK, LOG_SYSTEM, LogPrint, mutex, cmlabs::DataMessage::setInt(), cmlabs::DataMessage::setString(), cmlabs::WebsocketData::source, stricmp, and webSockets.
|
protected |
Route a reply message back to the client that issued the request.
Definition at line 2043 of file RequestGateway.cpp.
References cmlabs::JSONM::addData(), addToCallLog(), AddToJSONRoot(), cmlabs::WebsocketData::BINARY, callLogCount, channel, cmlabs::RequestReply::clientRef, clients, clientSentCount, cmlabs::HTTPReply::createErrorPage(), cmlabs::HTTPReply::createOptionsResponse(), cmlabs::HTTPReply::createPage(), executors, cmlabs::RequestReply::gatewayRef, cmlabs::DataMessage::getData(), cmlabs::DataMessage::getInt(), cmlabs::DataMessage::getReference(), cmlabs::DataMessage::getSize(), cmlabs::RequestReply::getStatus(), cmlabs::DataMessage::getString(), cmlabs::DataMessage::getTime(), cmlabs::GetTimeAge(), cmlabs::GetTimeNow(), HTTP_OK, HTTP_SERVER_NOREPLY, cmlabs::HTTP_Status, cmlabs::RequestReply::isLongReq, cmlabs::JSONM::jmData, cmlabs::JSONM::jmSize, LOG_SYSTEM, LogPrint, longAvgStats, mutex, cmlabs::RequestReply::origin, cmlabs::RequestReply::peekRequestMessage(), cmlabs::RequestReply::processor, requestMap, cmlabs::WebsocketData::setData(), cmlabs::DataMessage::setFrom(), cmlabs::JSONM::setJSON(), cmlabs::DataMessage::setReference(), cmlabs::DataMessage::setStatus(), shortAvgStats, cmlabs::RequestReply::startTime, cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), webClients, webServerName, and webSockets.
Referenced by runClient().
|
protected |
Client-side worker loop: route replies back to callers, expire timed-out requests.
Definition at line 2440 of file RequestGateway.cpp.
References clients, cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::Runnable::isRunning, replyQ, replyToClient(), and cmlabs::Runnable::shouldContinue.
Referenced by RequestGatewayClientRun.
|
protected |
Executor-side worker loop: dispatch queued requests, watch heartbeats, redistribute dead executors' work.
Definition at line 2265 of file RequestGateway.cpp.
References channel, clients, distributeDeadExecutorRequests(), execQ, execSentCount, executorHeartbeatTimeout, executors, cmlabs::RequestReply::gatewayRef, cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::RequestReply::isLongReq, cmlabs::Runnable::isRunning, LOG_SYSTEM, LogPrint, maxExecutorRequestTimeoutMS, maxRequestProcessingSize, mutex, cmlabs::RequestReply::origin, cmlabs::RequestReply::peekRequestMessage(), cmlabs::PrintTimeDifString(), cmlabs::RequestReply::processor, replyQ, replyXML, requestMap, sendRestartToExecutor(), cmlabs::SENT, cmlabs::SERVERERROR, cmlabs::DataMessage::setReference(), cmlabs::RequestReply::setStatus(), cmlabs::DataMessage::setString(), cmlabs::Runnable::shouldContinue, webClients, and webSockets.
Referenced by RequestGatewayExecRun.
|
protected |
Ask an executor to restart itself (sent when it misbehaves).
Definition at line 1955 of file RequestGateway.cpp.
References channel, executors, and cmlabs::DataMessage::setString().
Referenced by callInternalAPI(), and runExec().
| bool cmlabs::RequestGateway::setCacheFiles | ( | bool | cache | ) |
Enable/disable HTTP caching headers for served files.
Definition at line 436 of file RequestGateway.cpp.
References cacheFiles.
| bool cmlabs::RequestGateway::setExecutorHeartbeatTimeout | ( | uint32 | timeout | ) |
Set how long an executor may go silent before being declared dead (its queue is then redistributed).
| timeout | ms. |
Definition at line 395 of file RequestGateway.cpp.
References executorHeartbeatTimeout.
| bool cmlabs::RequestGateway::setExternalAPILabel | ( | const char * | label | ) |
Set the URI label under which the external (public) API is exposed.
Definition at line 422 of file RequestGateway.cpp.
References externalAPITitle.
| bool cmlabs::RequestGateway::setInternalAPILabel | ( | const char * | label | ) |
Set the URI label under which the internal (management) API is exposed.
Definition at line 429 of file RequestGateway.cpp.
References internalAPITitle.
| bool cmlabs::RequestGateway::setLongRequestLimit | ( | uint32 | limit | ) |
Cap concurrent long requests per executor.
| limit | Maximum. |
Definition at line 1784 of file RequestGateway.cpp.
References longReqLimit.
| bool cmlabs::RequestGateway::setMaxExecutorRequestTimeout | ( | uint32 | timeout | ) |
Set the maximum time a request may sit with an executor before being failed/retried.
| timeout | ms. |
Definition at line 400 of file RequestGateway.cpp.
References maxExecutorRequestTimeoutMS.
| bool cmlabs::RequestGateway::setQueuingParameters | ( | uint32 | maxRequestQueueSize, |
| uint32 | maxRequestProcessingSize, | ||
| uint32 | priorityThreshold ) |
Tune queuing/backpressure behaviour.
| maxRequestQueueSize | Max queued requests before rejecting with TOOBUSY. |
| maxRequestProcessingSize | Max requests dispatched concurrently. |
| priorityThreshold | Queue length above which priority ordering kicks in. |
Definition at line 441 of file RequestGateway.cpp.
References maxRequestProcessingSize, maxRequestQueueSize, and priorityThreshold.
| bool cmlabs::RequestGateway::setResponseType | ( | const char * | type | ) |
Choose the reply serialisation ("xml" or "json").
Definition at line 405 of file RequestGateway.cpp.
References replyXML.
Referenced by UnitTest().
| bool cmlabs::RequestGateway::setWebServerInfo | ( | const char * | name, |
| const char * | rootdir, | ||
| const char * | indexfile ) |
Configure the built-in file web server.
| name | Server header name. |
| rootdir | Document root directory. |
| indexfile | Default index filename. |
Definition at line 412 of file RequestGateway.cpp.
References indexFilename, rootdir, and webServerName.
Referenced by UnitTest().
|
static |
End-to-end stress test spinning up a gateway plus the given numbers of executors, extra gateways and binary/web/websocket test clients.
| port | Base port. |
| executorCount | Executors. |
| gatewayCount | Gateways. |
| clientCount | Binary clients. |
| webCount | HTTP clients. |
| webSocketCount | WebSocket clients. |
| sendfreq | Requests per second per client. |
| reconnect | Client reconnect interval (ms; <0 = never). |
| payload | Request payload size in bytes. |
| processTime | Simulated executor processing time (ms). |
| runtime | Test duration (s). |
Definition at line 2492 of file RequestGateway.cpp.
References cmlabs::RequestClient::addGateway(), cmlabs::RequestExecutor::addGateway(), cmlabs::RequestExecutor::addLongRequestName(), addPort(), clients, cmlabs::unittest::detail(), executors, cmlabs::unittest::fail(), cmlabs::RequestClient::finishRequest(), cmlabs::RequestReply::getRequestDuration(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), init(), cmlabs::TestRequestClient::init(), cmlabs::TestWebRequestClient::init(), cmlabs::TestWebSocketRequestClient::init(), cmlabs::unittest::metric(), NOENC, port, cmlabs::RequestClient::postRequest(), cmlabs::PrintTimeDifString(), cmlabs::unittest::progress(), RequestGateway(), setResponseType(), cmlabs::DataMessage::setString(), setWebServerInfo(), cmlabs::utils::Sleep(), SSLENC, cmlabs::SUCCESS, cmlabs::RequestClient::waitForConnection(), and cmlabs::RequestReply::waitForResult().
Referenced by cmlabs::Test_RequestGateway().
|
friend |
Thread entry point for the gateway's client-side loop.
Definition at line 2470 of file RequestGateway.cpp.
References RequestGateway(), runClient(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by init().
|
friend |
Thread entry point for the gateway's executor-side loop.
Definition at line 2465 of file RequestGateway.cpp.
References RequestGateway(), runExec(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by init().
|
protected |
Send caching headers for served files.
Definition at line 308 of file RequestGateway.h.
Referenced by callInternalAPI(), receiveHTTPRequest(), RequestGateway(), and setCacheFiles().
|
protected |
Rolling log of completed calls (owned).
Definition at line 273 of file RequestGateway.h.
Referenced by addToCallLog(), and callInternalAPI().
|
protected |
Total calls logged since start.
Definition at line 275 of file RequestGateway.h.
Referenced by callInternalAPI(), receiveHTTPRequest(), replyToClient(), and RequestGateway().
|
protected |
Max entries kept in ::callLog.
Definition at line 274 of file RequestGateway.h.
Referenced by addToCallLog(), and RequestGateway().
|
protected |
Guards the call log.
Definition at line 272 of file RequestGateway.h.
Referenced by addToCallLog(), and callInternalAPI().
|
protected |
Channel carrying all gateway traffic.
Definition at line 278 of file RequestGateway.h.
Referenced by addPort(), callExternalAPI(), callInternalAPI(), receiveHTTPRequest(), receiveMessage(), receiveNetworkEvent(), receiveWebsocketData(), replyToClient(), RequestGateway(), runExec(), and sendRestartToExecutor().
|
protected |
Messages received from clients.
Definition at line 295 of file RequestGateway.h.
Referenced by addToRequestQueue(), and RequestGateway().
|
protected |
Connected binary clients by id.
Definition at line 280 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), receiveMessage(), receiveNetworkEvent(), replyToClient(), runClient(), runExec(), and UnitTest().
|
protected |
Messages sent to clients.
Definition at line 294 of file RequestGateway.h.
Referenced by replyToClient(), and RequestGateway().
|
protected |
Requests awaiting executor dispatch.
Definition at line 283 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), and runExec().
|
protected |
Messages received from executors.
Definition at line 297 of file RequestGateway.h.
Referenced by receiveMessage(), and RequestGateway().
|
protected |
Messages sent to executors.
Definition at line 296 of file RequestGateway.h.
Referenced by RequestGateway(), and runExec().
|
protected |
Silence threshold before an executor is declared dead (ms).
Definition at line 302 of file RequestGateway.h.
Referenced by RequestGateway(), runExec(), and setExecutorHeartbeatTimeout().
|
protected |
Connected executors by id.
Definition at line 279 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), distributeDeadExecutorRequests(), getBestExecutorID(), receiveMessage(), receiveNetworkEvent(), replyToClient(), runExec(), sendRestartToExecutor(), and UnitTest().
|
protected |
URI label of the external (public) API.
Definition at line 317 of file RequestGateway.h.
Referenced by receiveHTTPRequest(), RequestGateway(), and setExternalAPILabel().
|
protected |
Basic-auth users (user -> credential hash).
Definition at line 287 of file RequestGateway.h.
Referenced by addAuthUser(), and callInternalAPI().
|
protected |
This gateway's id.
Definition at line 299 of file RequestGateway.h.
|
protected |
Default index file name.
Definition at line 313 of file RequestGateway.h.
Referenced by callInternalAPI(), receiveHTTPRequest(), and setWebServerInfo().
|
protected |
URI label of the internal (management) API.
Definition at line 316 of file RequestGateway.h.
Referenced by receiveHTTPRequest(), RequestGateway(), and setInternalAPILabel().
|
protected |
Last issued gateway reference id.
Definition at line 293 of file RequestGateway.h.
Referenced by addToRequestQueue(), and RequestGateway().
|
protected |
Rolling latency stats for long requests.
Definition at line 320 of file RequestGateway.h.
Referenced by callInternalAPI(), and replyToClient().
|
protected |
Concurrent long-request cap per executor.
Definition at line 301 of file RequestGateway.h.
Referenced by getBestExecutorID(), RequestGateway(), and setLongRequestLimit().
|
protected |
Request names classified as long.
Definition at line 286 of file RequestGateway.h.
Referenced by getBestExecutorID(), and receiveMessage().
|
protected |
Owned network stack (listeners + connections).
Definition at line 277 of file RequestGateway.h.
Referenced by addPort(), init(), RequestGateway(), and ~RequestGateway().
|
protected |
Max time a request may sit with an executor (ms).
Definition at line 306 of file RequestGateway.h.
Referenced by RequestGateway(), runExec(), and setMaxExecutorRequestTimeout().
|
protected |
Backpressure: max requests dispatched concurrently.
Definition at line 304 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), RequestGateway(), runExec(), and setQueuingParameters().
|
protected |
Backpressure: max queued requests before TOOBUSY.
Definition at line 303 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), RequestGateway(), and setQueuingParameters().
|
protected |
Guards shared gateway state.
Definition at line 289 of file RequestGateway.h.
Referenced by addPort(), init(), receiveHTTPRequest(), receiveMessage(), receiveNetworkEvent(), receiveWebsocketData(), replyToClient(), runExec(), and ~RequestGateway().
|
protected |
Primary listening port.
Definition at line 300 of file RequestGateway.h.
Referenced by addGateway(), addPort(), addToCallLog(), callInternalAPI(), and UnitTest().
|
protected |
Queue length above which priority ordering applies.
Definition at line 305 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), RequestGateway(), and setQueuingParameters().
|
protected |
Replies awaiting client routing.
Definition at line 284 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), receiveMessage(), runClient(), and runExec().
|
protected |
Reply in XML (true) or JSON (false).
Definition at line 309 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), RequestGateway(), runExec(), and setResponseType().
|
protected |
In-flight requests by gateway ref id.
Definition at line 285 of file RequestGateway.h.
Referenced by addToRequestQueue(), receiveMessage(), replyToClient(), runExec(), and ~RequestGateway().
|
protected |
Document root for file serving.
Definition at line 312 of file RequestGateway.h.
Referenced by callInternalAPI(), receiveHTTPRequest(), and setWebServerInfo().
|
protected |
Rolling latency stats for short requests.
Definition at line 319 of file RequestGateway.h.
Referenced by callInternalAPI(), and replyToClient().
|
protected |
True when an SSL certificate was configured.
Definition at line 307 of file RequestGateway.h.
Referenced by addPort(), init(), and RequestGateway().
|
protected |
Gateway start time (ms epoch), for uptime reporting.
Definition at line 291 of file RequestGateway.h.
Referenced by callInternalAPI(), and RequestGateway().
|
protected |
Client-side worker thread id.
Definition at line 292 of file RequestGateway.h.
Referenced by init(), RequestGateway(), and ~RequestGateway().
|
protected |
Version shown in status pages.
Definition at line 314 of file RequestGateway.h.
Referenced by callInternalAPI(), and RequestGateway().
|
protected |
Connected HTTP clients by id.
Definition at line 281 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callExternalAPI(), callInternalAPI(), receiveNetworkEvent(), replyToClient(), and runExec().
|
protected |
Server header name of the built-in web server.
Definition at line 311 of file RequestGateway.h.
Referenced by callInternalAPI(), receiveHTTPRequest(), replyToClient(), RequestGateway(), and setWebServerInfo().
|
protected |
Connected WebSocket clients by id.
Definition at line 282 of file RequestGateway.h.
Referenced by addRequestReplyToRequestQueue(), callInternalAPI(), receiveNetworkEvent(), receiveWebsocketData(), replyToClient(), and runExec().