NCBI C++ ToolKit
|
Search Toolkit Book for CSrvTask
Main working entity in TaskServer. More...
#include "srv_tasks.hpp"
(Private to src/app/netcache
.)
Public Member Functions | |
CSrvTask (void) | |
void | SetRunnable (bool boost=false) |
Set this task "runnable", i.e. More... | |
void | RunAfter (Uint4 delay_sec) |
This call is basically equivalent to SetRunnable() but with guarantee that task will be scheduled for execution no earlier than delay_sec later (approximately, can be up to a second less if measured in absolute wall time). More... | |
virtual void | Terminate (void) |
Stops task's execution and deletes it. More... | |
void | SetPriority (Uint4 prty) |
Set and retrieve task's priority. More... | |
Uint4 | GetPriority (void) |
void | CreateNewDiagCtx (void) |
Create new diagnostic context for this task to work in. More... | |
void | SetDiagCtx (CRequestContext *ctx) |
Set diagnostic context for this task to work in. More... | |
CRequestContext * | GetDiagCtx (void) |
Get current diagnostic context for the task. More... | |
void | ReleaseDiagCtx (void) |
Releases current diagnostic context of the task. More... | |
virtual void | ExecuteSlice (TSrvThreadNum thr_num)=0 |
This is the main method to do all work this task should do. More... | |
virtual | ~CSrvTask (void) |
virtual void | InternalRunSlice (TSrvThreadNum thr_num) |
This is the real time slice execution method called from TaskServer. More... | |
Static Public Member Functions | |
static void | PrintState (CSrvSocketTask &task) |
Public Attributes | |
TSrvTaskListHook | m_TaskListHook |
Hook to put this task into TSrvTaskList. More... | |
TSrvThreadNum | m_LastThread |
Thread number where this task was executed last time. More... | |
TSrvTaskFlags | m_TaskFlags |
Bit-OR of flags for this task. More... | |
int | m_LastActive |
Time (in seconds) when the task was active last time, i.e. More... | |
Uint4 | m_Priority |
Task's priority. More... | |
CRequestContext * | m_DiagCtx |
Current diagnostic context for this task. More... | |
CRequestContext ** | m_DiagChain |
Nested diagnostic contexts of this task. More... | |
size_t | m_DiagChainSize |
STimerTicket * | m_Timer |
Timer ticket assigned to this task when it calls RunAfter(). More... | |
CSrvTaskTerminator | m_Terminator |
Object that will delete this task after call to Terminate(). More... | |
Private Member Functions | |
CSrvTask (const CSrvTask &) | |
CSrvTask & | operator= (const CSrvTask &) |
Main working entity in TaskServer.
It's called "task" because it should work like task/process in OS with cooperative multi-tasking. Everything that task want to do should be divided into relatively small pieces – slices. OS (TaskServer in this case) will provide time slices to each task, they should execute a portion of their work during this slice and then return control back to OS to give other tasks a chance to have their time slice of the processor (thread in case of TaskServer). Each slice of work done by the task should be less than jiffy (0.01 second by default), preferably one or two orders of magnitude less than jiffy. This way TaskServer will be able to schedule all tasks more effectively and spread the load evenly between threads.
Definition at line 87 of file srv_tasks.hpp.
CSrvTask::CSrvTask | ( | void | ) |
Definition at line 60 of file srv_tasks.cpp.
References CMiniMutex::Lock(), and CMiniMutex::Unlock().
|
virtual |
Definition at line 82 of file srv_tasks.cpp.
References _ASSERT, free(), fTaskOnTimer, CMiniMutex::Lock(), m_DiagChain, m_DiagChainSize, m_DiagCtx, m_TaskFlags, m_Timer, and CMiniMutex::Unlock().
void CSrvTask::CreateNewDiagCtx | ( | void | ) |
Create new diagnostic context for this task to work in.
See comments to SetDiagCtx() on the effect of that.
Definition at line 1249 of file logging.cpp.
References ctx, and SetDiagCtx().
Referenced by s_CreateDiagRequest(), s_DoCleanLog(), CNCActiveSyncControl::x_DoPeriodicSync(), CNCMessageHandler::x_ReadCommand(), CBlobCacher::x_StartCaching(), CSpaceShrinker::x_StartMoves(), CExpiredCleaner::x_StartSession(), and CBulkCleaner::x_StartSession().
|
pure virtual |
This is the main method to do all work this task should do.
This method will be called periodically while somebody calls SetRunnable() or RunAfter() on this task. But number of calls to this method won't match number of calls to SetRunnable() – if several calls to SetRunnable() are made before ExecuteSlice() had a chance to execute it will be called only once. Task's code inside ExecuteSlice() should understand itself how much work it needs to do and call SetRunnable() again if needed (or do all the work before returning from ExecuteSlice()). Parameter thr_num is number of thread on which ExecuteSlice() is called.
Implemented in CSrvStatesTask< Derived >, CSrvStatesTask< CBulkCleaner >, CSrvStatesTask< CExpiredCleaner >, CSrvStatesTask< CNCMessageHandler >, CSrvStatesTask< CNCActiveHandler >, CSrvStatesTask< CNCActiveSyncControl >, CSrvStatesTask< CDiskFlusher >, CSrvStatesTask< CSpaceShrinker >, CSrvStatesTask< CBlobCacher >, CSrvListener, CTZAdjuster, CRecNoSaver, CNewFileCreator, CNCPeerControl, CNCHeartBeat, CWriteBackControl, CCurVerReader, CNCBlobAccessor, CNCBlobVerManager, CStatRotator, SNCBlobVerData, CMMFlusher, CLogWriter, CNCActiveHandler_Proxy, and CNCLogCleaner.
Referenced by CSrvSocketTask::InternalRunSlice(), and InternalRunSlice().
|
inline |
Get current diagnostic context for the task.
Definition at line 353 of file srv_inlines.hpp.
References m_DiagCtx.
Referenced by s_CleanSockResources(), s_CreateDiagRequest(), s_DoCleanLog(), CNCActiveHandler::SetProxy(), CNCActiveHandler::SyncBlobsList(), CNCActiveHandler::SyncCancel(), CNCActiveHandler::SyncCommit(), CNCActiveHandler::SyncProlongOur(), CNCActiveHandler::SyncProlongPeer(), CNCActiveHandler::SyncSend(), CNCActiveHandler::SyncStart(), CNCMessageHandler::x_AssignCmdParams(), CBlobCacher::x_CancelCaching(), CNCActiveHandler::x_CleanCmdResources(), CNCMessageHandler::x_CleanCmdResources(), CNCMessageHandler::x_CloseCmdAndConn(), CNCMessageHandler::x_CloseOnPeerError(), CExpiredCleaner::x_DeleteNextData(), CBlobCacher::x_DelFileAndRetryCreate(), CNCMessageHandler::x_DoCmd_CopyProlong(), CNCMessageHandler::x_DoCmd_CopyPut(), CNCMessageHandler::x_DoCmd_Get(), CNCMessageHandler::x_DoCmd_GetStat(), CNCMessageHandler::x_DoCmd_HasBlob(), CNCMessageHandler::x_DoCmd_IC_Store(), CNCMessageHandler::x_DoCmd_Put(), CNCMessageHandler::x_DoCmd_ReConfig(), CNCMessageHandler::x_DoCmd_SetValid(), CNCMessageHandler::x_DoCmd_SyncGet(), CNCMessageHandler::x_DoCmd_SyncStart(), CNCActiveHandler::x_DoSyncGet(), CBulkCleaner::x_Finalize(), CNCMessageHandler::x_FinishCommand(), CSpaceShrinker::x_FinishMoves(), CNCMessageHandler::x_FinishReadingBlob(), CNCActiveSyncControl::x_FinishSync(), CNCActiveHandler::x_ProcessPeerError(), CNCMessageHandler::x_ProxyToNextPeer(), CNCMessageHandler::x_ReadAuthMessage(), CNCMessageHandler::x_ReadBlobChunk(), CNCMessageHandler::x_ReadBlobChunkLength(), CNCMessageHandler::x_ReadBlobSignature(), CNCMessageHandler::x_ReadCommand(), CNCActiveHandler::x_ReadCopyProlong(), CNCActiveHandler::x_ReadDataPrefix(), CNCActiveHandler::x_ReadHttpDataPrefix(), CNCActiveHandler::x_ReplaceServerConn(), CNCMessageHandler::x_ReportError(), CNCMessageHandler::x_SendCmdAsProxy(), CNCActiveHandler::x_SendCopyPutCmd(), CNCMessageHandler::x_SendGetMetaCmd(), CNCMessageHandler::x_SendPurgeToPeerCmd(), CNCMessageHandler::x_SendPutToPeerCmd(), CNCMessageHandler::x_StartCommand(), CNCMessageHandler::x_WaitForPeerAnswer(), CNCMessageHandler::x_WriteBlobData(), CNCMessageHandler::x_WriteHttpResponse(), and CNCActiveHandler::~CNCActiveHandler().
|
inline |
Definition at line 341 of file srv_inlines.hpp.
References m_Priority.
Referenced by s_AddTaskToQueue().
|
virtual |
This is the real time slice execution method called from TaskServer.
By default it just calls ExecuteSlice(). But CSrvSocketTask needs to do some work before calling into user's code (or even instead of it), thus this method was added and made virtual.
Reimplemented in CSrvSocketTask.
Definition at line 100 of file srv_tasks.cpp.
References CSrvTime::CurSecs(), ExecuteSlice(), and m_LastActive.
Referenced by SchedExecuteTask().
|
static |
Referenced by CNCMessageHandler::x_DoCmd_GetConfig().
void CSrvTask::ReleaseDiagCtx | ( | void | ) |
Releases current diagnostic context of the task.
Method must be called for all contexts created via CreateNewDiagCtx() or set via SetDiagCtx(). You cannot make more calls and you must not make less calls than CreateNewDiagCtx() and SetDiagCtx() combined. Both erroneous situations will result in program abort.
Definition at line 1257 of file logging.cpp.
References GetMemSize(), m_DiagChain, m_DiagChainSize, m_DiagCtx, NULL, CObject::RemoveReference(), and SRV_FATAL.
Referenced by s_CleanSockResources(), s_DoCleanLog(), CBlobCacher::x_CancelCaching(), CNCActiveHandler::x_CleanCmdResources(), CNCMessageHandler::x_CleanCmdResources(), CNCActiveHandler::x_CloseConn(), CBlobCacher::x_FinishCaching(), CSpaceShrinker::x_FinishMoves(), CExpiredCleaner::x_FinishSession(), CBulkCleaner::x_FinishSession(), CNCActiveSyncControl::x_FinishSync(), CNCMessageHandler::x_ReadCommand(), CNCActiveHandler::x_ReadCopyProlong(), CNCActiveHandler::x_ReadDataForClient(), CNCActiveHandler::x_ReplaceServerConn(), and CNCActiveHandler::~CNCActiveHandler().
void CSrvTask::RunAfter | ( | Uint4 | delay_sec | ) |
This call is basically equivalent to SetRunnable() but with guarantee that task will be scheduled for execution no earlier than delay_sec later (approximately, can be up to a second less if measured in absolute wall time).
If SetRunnable() is called while task is waiting for its delay timer then timer is canceled and task should set this timer inside ExecuteSlice() again if it still needs it. Timers are never set if TaskServer is shutting down (CTaskServer::IsInShutdown() returns TRUE). In this case ExecuteSlice() will be called immediately after call to RunAfter() and task should recognize state of shutting down and behave accordingly. This method is not thread-safe and should be called only from inside ExecuteSlice() method.
Definition at line 216 of file timers.cpp.
References ACCESS_ONCE, AtomicCAS(), Critical(), CSrvTime::CurSecs(), fTaskOnTimer, fTaskQueued, fTaskRunnable, CTaskServer::IsInShutdown(), CMiniMutex::Lock(), m_TaskFlags, m_Timer, RemoveTaskFromTimer(), s_AddTimerTicket(), s_TimerLock, SetRunnable(), SRV_LOG, STimerTicket::task, STimerTicket::timer_time, and CMiniMutex::Unlock().
Referenced by CStatRotator::CalcNextRun(), CNCLogCleaner::ExecuteSlice(), CMMFlusher::ExecuteSlice(), CNCBlobVerManager::ExecuteSlice(), CWriteBackControl::ExecuteSlice(), CNCHeartBeat::ExecuteSlice(), CNCPeerControl::ExecuteSlice(), CRecNoSaver::ExecuteSlice(), CTZAdjuster::ExecuteSlice(), InitMemoryMan(), CDiskFlusher::x_CheckFlushTime(), CBlobCacher::x_DelFileAndRetryCreate(), CNCActiveSyncControl::x_ExecuteFinalize(), CNCActiveSyncControl::x_FinishScanSlots(), CSpaceShrinker::x_FinishSession(), CExpiredCleaner::x_FinishSession(), CNCMessageHandler::x_ReadBlobChunkLength(), CNCActiveSyncControl::x_StartScanSlots(), CBulkCleaner::x_StartSession(), CNCActiveSyncControl::x_WaitForExecutingTasks(), SNCBlobVerData::x_WriteBlobInfo(), and SNCBlobVerData::x_WriteCurChunk().
void CSrvTask::SetDiagCtx | ( | CRequestContext * | ctx | ) |
Set diagnostic context for this task to work in.
From this moment on all log messages made from inside ExecuteSlice() of this task will be assigned to the provided request context. Diagnostic contexts can be nested – second call to SetDiagCtx() sets new context of this task and next call to ReleaseDiagCtx() restores first context.
Definition at line 1197 of file logging.cpp.
References ctx, GetMemSize(), m_DiagChain, m_DiagChainSize, m_DiagCtx, malloc(), and SRV_FATAL.
Referenced by CNCActiveHandler::CopyPurge(), CNCActiveHandler::CopyPut(), CreateNewDiagCtx(), CNCActiveHandler::ProxyBList(), CNCActiveHandler::ProxyGetMeta(), CNCActiveHandler::ProxyGetSize(), CNCActiveHandler::ProxyHasBlob(), CNCActiveHandler::ProxyProlong(), CNCActiveHandler::ProxyRead(), CNCActiveHandler::ProxyReadLast(), CNCActiveHandler::ProxyRemove(), CNCActiveHandler::ProxySetValid(), CNCActiveHandler::ProxyWrite(), CNCActiveHandler::SearchMeta(), CNCActiveHandler::SetProxy(), CNCActiveHandler::SyncBlobsList(), CNCActiveHandler::SyncCancel(), CNCActiveHandler::SyncCommit(), CNCActiveHandler::SyncProlongOur(), CNCActiveHandler::SyncProlongPeer(), CNCActiveHandler::SyncSend(), CNCActiveHandler::SyncStart(), CNCActiveHandler::x_DoSyncGet(), CNCActiveHandler::x_ReadDataPrefix(), CNCActiveHandler::x_ReadHttpDataPrefix(), and CNCActiveHandler::x_ReplaceServerConn().
|
inline |
Set and retrieve task's priority.
TaskServer has simple notion of priority: all tasks by default have priority 1. For each slice of task with priority 2 TaskServer executes 2 slices of priority 1. For each slice of task with priority 3 TaskServer executes 3 tasks with priority 1 and 2 tasks with priority 2 (rounding goes up, error doesn't accumulate, i.e. for 2 tasks with priority 3 it executes 3 tasks with priority 2). And so on. But this rule works only if there are tasks with higher priorities in the queue. If only tasks with low priority are executing at the moment they will take all available time slices. Counting of already executed tasks and their priorities is done on a per-jiffy basis, i.e. if there were only high priority tasks for some time and then task with low priority appeared it probably will be executed within 0.01 second. That said priority system is not quite usable now because it needs things like priority inheritance from one task to the one created by it, priority combination in CSrvTransitionTask and probably some other mechanisms to work exactly as expected.
Definition at line 347 of file srv_inlines.hpp.
References m_Priority.
Referenced by CNCBlobVerManager::CNCBlobVerManager(), CNCActiveHandler::SetReservedForBG(), SNCBlobVerData::SNCBlobVerData(), CNCMessageHandler::x_CleanCmdResources(), and CNCMessageHandler::x_StartCommand().
Set this task "runnable", i.e.
available for execution, having some pending work requiring some processing power. Call to this method guarantees that ExecuteSlice() will be called some time later. If another execution of ExecuteSlice() is in progress at the time of call to SetRunnable() then TaskServer will call ExecuteSlice() again some time after current ExecuteSlice() finishes. This behavior guarantees that if ExecuteSlice() somewhere checks for more available work, doesn't find it, decides to return and some other thread calls SetRunnable() at this moment task will have a chance to check for available work in next call to ExecuteSlice(). This method is thread-safe and can be called from any thread and any context.
Definition at line 618 of file scheduler.cpp.
References ACCESS_ONCE, AtomicCAS(), fTaskNeedTermination, fTaskQueued, fTaskRunnable, fTaskRunning, fTaskTerminated, GetCurThread(), m_LastThread, m_TaskFlags, RemoveTaskFromTimer(), s_AddTaskToQueue(), and s_FindQueueThread().
Referenced by CNCServer::CachingCompleted(), CNCActiveHandler::CheckCommandTimeout(), CheckConnectsTimeout(), CNCActiveHandler::ClientReleased(), CNCActiveHandler::CloseForShutdown(), CNCActiveSyncControl::CmdFinished(), CNCBlobVerManager::DeadTimeChanged(), SNCBlobVerData::DeleteThis(), CNCBlobVerManager::DeleteThis(), CNCLogCleaner::ExecuteSlice(), CLogWriter::ExecuteSlice(), SNCBlobVerData::ExecuteSlice(), CNCBlobAccessor::ExecuteSlice(), CNewFileCreator::ExecuteSlice(), CSrvStatesTask< CBlobCacher >::ExecuteSlice(), CNCBlobVerManager::FinalizeWriting(), CSrvTransitionTask::FinishTransition(), CNCBlobStorage::Initialize(), CWriteBackControl::Initialize(), CNCPeriodicSync::Initialize(), InitTimeMan(), CSrvSocketTask::InternalRunSlice(), CNCActiveHandler_Proxy::NeedToProxySocket(), CNCPeerControl::Peer(), SNCBlobVerData::RequestDataWrite(), CSrvSocketTask::RequestFlush(), SNCBlobVerData::RequestMemRelease(), CNCBlobVerManager::RequestMemRelease(), RequestStopListening(), CSrvTransitionTask::RequestTransition(), RunAfter(), s_DeleteOldestSockets(), s_DoDataProxy(), s_ExecuteTimerTicket(), s_GetNextWriteCoord(), s_NotifyConsumers(), s_QueueLogWrite(), s_RegisterClientEvent(), s_RegisterListenEvent(), s_StopSync(), CNCBlobStorage::SavePurgeData(), SNCBlobVerData::SetNotCurrent(), CNCActiveClientHub::SetStatus(), CSrvSocketTask::StartProcessing(), CBlobCacher::x_CacheNextRecord(), CNCActiveSyncControl::x_CheckSlotTheirSync(), CExpiredCleaner::x_CleanNextBucket(), CNCActiveHandler::x_CloseConn(), CBlobCacher::x_CreateInitialFile(), CExpiredCleaner::x_DeleteNextData(), CSpaceShrinker::x_DeleteNextFile(), SNCBlobVerData::x_ExecuteWriteAll(), CBlobCacher::x_FinishCaching(), CNCMessageHandler::x_FinishCommand(), CSpaceShrinker::x_FinishMoveRecord(), CSpaceShrinker::x_FinishSession(), CExpiredCleaner::x_FinishSession(), CBulkCleaner::x_FinishSession(), CNCActiveSyncControl::x_FinishSync(), CDiskFlusher::x_FlushNextFile(), CSpaceShrinker::x_MoveNextRecord(), CBlobCacher::x_PreCacheRecNums(), CSpaceShrinker::x_PrepareToShrink(), CNCMessageHandler::x_ReadBlobChunk(), CNCMessageHandler::x_ReadBlobChunkLength(), CNCActiveHandler::x_ReplaceServerConn(), and CNCActiveHandler::x_SetStateAndStartProcessing().
|
virtual |
Stops task's execution and deletes it.
This method should be used for any object derived from CSrvTask instead of direct destruction, otherwise in some situations TaskServer could dereference pointer to this task after its deletion. The only exception from this rule could be when task has been just created and SetRunnable() was never called for it. See more comments on this in the description of CSrvTaskTerminator. This method is thread-safe although it's preferred to call it from inside ExecuteSlice() method. This method is virtual for internal use only - CSrvSocketTask should use different termination sequence. Do not ever override this method in any other class.
Reimplemented in CSrvSocketTask.
Definition at line 612 of file scheduler.cpp.
References MarkTaskTerminated().
Referenced by CNCActiveHandler::CloseForShutdown(), CNCBlobAccessor::Release(), s_ProcessWBAddDel(), CBlobCacher::x_CancelCaching(), CBlobCacher::x_FinishCaching(), CNCActiveHandler::x_MayDeleteThis(), CNCBlobVerManager::x_ReleaseMgr(), and CNCActiveHandler::x_ReplaceServerConn().
CRequestContext** CSrvTask::m_DiagChain |
Nested diagnostic contexts of this task.
This variable is not NULL if this task have ever used nested contexts and contains array of context pointers with deepest context (the one assigned to task first) in the element 0, context set later in the element 1 etc.
Definition at line 221 of file srv_tasks.hpp.
Referenced by ReleaseDiagCtx(), SetDiagCtx(), and ~CSrvTask().
size_t CSrvTask::m_DiagChainSize |
Definition at line 223 of file srv_tasks.hpp.
Referenced by ReleaseDiagCtx(), SetDiagCtx(), and ~CSrvTask().
CRequestContext* CSrvTask::m_DiagCtx |
Current diagnostic context for this task.
Definition at line 216 of file srv_tasks.hpp.
Referenced by GetDiagCtx(), CSrvDiagMsg::PrintExtra(), ReleaseDiagCtx(), SetDiagCtx(), CSrvDiagMsg::StartInfo(), CSrvDiagMsg::StartRequest(), CSrvDiagMsg::StopRequest(), and ~CSrvTask().
int CSrvTask::m_LastActive |
Time (in seconds) when the task was active last time, i.e.
when InternalRunSlice() was called last time.
Definition at line 212 of file srv_tasks.hpp.
Referenced by CNCActiveHandler::CheckCommandTimeout(), CSrvSocketTask::InternalRunSlice(), InternalRunSlice(), s_DeleteOldestSockets(), and s_SaveSocket().
TSrvThreadNum CSrvTask::m_LastThread |
Thread number where this task was executed last time.
Definition at line 206 of file srv_tasks.hpp.
Referenced by SchedExecuteTask(), SetRunnable(), and CSrvSocketTask::StartProcessing().
Uint4 CSrvTask::m_Priority |
Task's priority.
Definition at line 214 of file srv_tasks.hpp.
Referenced by GetPriority(), and SetPriority().
TSrvTaskFlags CSrvTask::m_TaskFlags |
Bit-OR of flags for this task.
For possible flag values see scheduler.hpp.
Definition at line 209 of file srv_tasks.hpp.
Referenced by CleanSocketList(), MarkTaskTerminated(), RemoveTaskFromTimer(), RunAfter(), s_ExecuteTimerTicket(), s_MarkTaskExecuted(), s_MarkTaskRunning(), SetRunnable(), and ~CSrvTask().
TSrvTaskListHook CSrvTask::m_TaskListHook |
Hook to put this task into TSrvTaskList.
Definition at line 204 of file srv_tasks.hpp.
CSrvTaskTerminator CSrvTask::m_Terminator |
Object that will delete this task after call to Terminate().
Definition at line 227 of file srv_tasks.hpp.
Referenced by CSrvTaskTerminator::ExecuteRCU(), and s_DoTermination().
STimerTicket* CSrvTask::m_Timer |
Timer ticket assigned to this task when it calls RunAfter().
Definition at line 225 of file srv_tasks.hpp.
Referenced by RemoveTaskFromTimer(), RunAfter(), s_ExecuteTimerTicket(), and ~CSrvTask().