Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] crontab在k8s集群下会导致无法执行 #7005

Closed
wisonlau opened this issue Aug 16, 2024 · 8 comments
Closed

[BUG] crontab在k8s集群下会导致无法执行 #7005

wisonlau opened this issue Aug 16, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@wisonlau
Copy link

wisonlau commented Aug 16, 2024

Execute the command and paste the result below.

PHP 8.1.28
Hyperf 3.0
Swoole 5.1.2

Description:

server.php
'mode' => SWOOLE_BASE

crontab.php
return [
'enable' => true,
'crontab' => [
(new Crontab())->setName('NacosServerListTask')
		    ->setRule('*/30 * * * * *')
		    ->setCallback([App\Task\NacosServerListTask::class, 'execute'])
		   ->setOnOneServer(true)
		    ->setEnable(\Hyperf\Support\make(App\Task\NacosServerListTask::class)->isEnable()),
]
]

服务器情况:
3台服务器,60个k8s pod节点平均分配到每台机20个容器

Steps To Reproduce:

1.首次发布定时任务代码,正常运行
2.第二次在定时任务代码里面加点注释,然后再发布,执行日志如下:
image

代码发布前最后一条记录时间2024-08-16 14:11:33
代码发布后第一条记录时间2024-08-16 14:12:00
看完日志第一时间想到是抢锁出问题,后面看了文档发现还可以用redis,所以就用了下面的方案

目前的临时解决方案:
把setOnOneServer换成setMutexPool

(new Crontab())->setName('NacosServerListTask')
		    ->setRule('*/30 * * * * *')
		    ->setCallback([App\Task\NacosServerListTask::class, 'execute'])
		    ->setMutexPool("default")->setMutexExpires(29)
		    ->setEnable(\Hyperf\Support\make(App\Task\NacosServerListTask::class)->isEnable()),

执行日志如下:
image

@wisonlau wisonlau added the bug Something isn't working label Aug 16, 2024
@wisonlau wisonlau changed the title [BUG] crontab在k8s集群下会导致锁无法释放 [BUG] crontab在k8s集群下会导致无法执行 Aug 16, 2024
@liangchenwsl
Copy link

设置 OnOneServer ,拿到锁的server执行定时任务时,pod突然重启,锁没能释放,会导致这种问题,也无法避免,只能等锁超时。
2个建议,一是调低锁的时长,二是任务本身要有防重复执行代码

@wisonlau
Copy link
Author

设置 OnOneServer ,拿到锁的server执行定时任务时,pod突然重启,锁没能释放,会导致这种问题,也无法避免,只能等锁超时。 2个建议,一是调低锁的时长,二是任务本身要有防重复执行代码

这个锁至少锁了1个小时以上,具体多长时间我倒是没测过

@liangchenwsl
Copy link

追源码可以看到,默认锁3600秒,也就是1小时。
你上面的->setMutexPool("default")->setMutexExpires(29) ,这串代码,也就是调低了锁时长,到29秒。

@liangchenwsl
Copy link

不过你的临时方案没有设置OnOneServer,setMutexExpires 其实也是不起效的,存在多个pod并发执行定时任务的情况。

@limingxinleo
Copy link
Member

crontab 建议自己控制下,参数太多,感觉调参数的成本也挺高的

@wisonlau
Copy link
Author

不过你的临时方案没有设置OnOneServer,setMutexExpires 其实也是不起效的,存在多个pod并发执行定时任务的情况。

看日志没并发,可能节点不够多,工作日我加OnOneServer试试

@wisonlau
Copy link
Author

wisonlau commented Aug 19, 2024

不过你的临时方案没有设置OnOneServer,setMutexExpires 其实也是不起效的,存在多个pod并发执行定时任务的情况。

(new Crontab())->setName('NacosServerListTask')
		    ->setRule('*/30 * * * * *')
		    ->setCallback([App\Task\NacosServerListTask::class, 'execute'])
		    ->setOnOneServer(true)->setMutexPool("default")->setMutexExpires(29)
		    ->setEnable(\Hyperf\Support\make(App\Task\NacosServerListTask::class)->isEnable()),

改成这样

第一次发布的时候正常
image
第二次发布的时候,也正常
image
第三次发布的时候,中间会被锁上1分钟,这就有点难受
image
第四次发布,中间锁30秒
image

感觉这个锁不太准

setMutexExpires(10)这个改成10也会出现一次跳过的,后面又连续发布了4,5次,每次都出现这个问题

@liangchenwsl
Copy link

这个锁是最大的加锁时长。 你锁60秒,01秒加锁,30秒重启服务器,肯定只锁30秒了。。。

如果你希望每次重启后立即执行,就去掉框架提供的锁,自行在要执行的定时任务里做好防并发、防重复执行的逻辑

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants