Change timed thread/go task's state to ERROR when timeout.

This commit is contained in:
Xie Han
2023-10-12 19:16:12 +08:00
parent 4c9e97c63e
commit f7cdcad111
7 changed files with 19 additions and 17 deletions

View File

@@ -62,7 +62,7 @@ int main(void)
class WFTaskFactory
{
/* Create 'Go' task with running time limit in seconds plus nanoseconds.
* If time exceeded, state WFT_STATE_ABORTED will be got in callback. */
* If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */
template<class FUNC, class... ARGS>
static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds,
const std::string& queue_name,
@@ -70,7 +70,7 @@ class WFTaskFactory
};
~~~
相比创建普通的go taskcreate_timedgo_task函数需要多传两个参数seconds和nanoseconds。
如果func的运行时间到达seconds+nanosconds时限task直接callback且state为WFT_STATE_ABORTED
如果func的运行时间到达seconds+nanosconds时限task直接callback且state为WFT_STATE_SYS_ERRORerror为ETIMEDOUT
注意框架无法中断用户执行中的任务。func依然会继续执行到结束但不会再次callback。另外nanoseconds取值区间在\[0,10亿
另外当我们给go task加上了运行时间限制callback的时机可能会先于func函数的结束任务所在series可能也会先于func结束。
如果我们在func里访问series可能就是一个错误了。例如
@@ -99,7 +99,7 @@ int main()
{
...
}
else // state == WFT_STATE_ABORTED. // 超过运行时间限制
else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制
{
...
}
@@ -120,7 +120,7 @@ int main()
{
int result = (int)task->user_data;
}
else // state == WFT_STATE_ABORTED. // 超过运行时间限制
else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制
{
...
}

View File

@@ -57,14 +57,14 @@ You may create a go task with running time limit by calling WFTaskFactory::creat
class WFTaskFactory
{
/* Create 'Go' task with running time limit in seconds plus nanoseconds.
* If time exceeded, state WFT_STATE_ABORTED will be got in callback. */
* If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */
template<class FUNC, class... ARGS>
static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds,
const std::string& queue_name,
FUNC&& func, ARGS&&... args);
};
~~~
Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED.
Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT.
Note that the framework cannot interrupt the user's ongoing task. ``func`` will still continue to execute to the end, but will not callback again. In addition, the value range of nanoseconds is [0,1 billion).

View File

@@ -172,7 +172,7 @@ Obviously, our framework can not interrupt a computing task because it's a user
...
};
~~~
This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED. But the task routine will continue to run till the end.
This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT. But the task routine will continue to run till the end.
# Symmetry of the algorithm and the protocol

View File

@@ -164,7 +164,7 @@ public:
};
~~~
参数seconds和nanoseconds构成了运行时限。在这里nanoseconds的取值范围在\[0,1000000000)。
当任务无法在运行时限内结束会直接回到callback并且任务的状态为WFT_STATE_ABORTED
当任务无法在运行时限内结束会直接回到callback并且任务的状态为WFT_STATE_SYS_ERROR且错误码为ETIMEDOUT
还是用matrix_multiply的例子我们可以这样写
~~~cpp
void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
@@ -172,12 +172,12 @@ void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
MMInput *input = task->get_input();
MMOutput *output = task->get_output();
if (task->get_state() == WFT_STATE_ABORTED)
if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT)
{
printf("Run out of time.\n");
return;
}
assert(task->get_state() == WFT_STATE_SUCCESS)
if (output->error)
@@ -210,13 +210,13 @@ int main()
...
}
~~~
上面的示例限制了任务运行时间不超过1毫秒否则以WFT_STATE_ABORTD的状态返回。
上面的示例限制了任务运行时间不超过1毫秒否则以WFT_STATE_SYS_ERROR的状态返回。
再次提醒我们并不会中断用户的实际运行函数。当任务超时并callback计算函数还会一直运行直到结束。
如果用户希望函数不再继续执行需要在代码中自行加入检查点来实现这样的功能。可以在INPUT里加入flag例如
~~~cpp
void callback(MMTask *task) // MMtask = WFThreadTask<MMInput, MMOutput>
{
if (task->get_state() == WFT_STATE_ABORTED)
if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT)
{
task->get_input()->flag = true;
printf("Run out of time.\n");