Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Please answer some questions before submitting your issue. Thanks!
Which version of XXL-JOB do you using?
2.4.1
Expected behavior
Actual behavior
Steps to reproduce the behavior
问题说明
时间轮Map由于remove和get没有并发控制,存在并发问题。
现假设线程A往map添加jobId,线程2读取jobId, 为了简便,都假设对同一个ringSecond读取
此时,假如A获取了list,并且不为null
B进行remove,也获取了不为空的list并且B.read操作先于A.add
那么A.add的jobId将会丢失,因为A往list里add了该id,但map已经没有该list引用了,下次B执行remove时。获取不到该list
在秒级任务超过1w时,就会出现任务丢失现象,概率很低,但并不是没有
解决方式
1.尝试使用过线程安全list,并不管用
2.尝试使用过 issue: #2892的方案,无效
上述两方案能降低丢失概率,方案1在2w任务级别出现任务丢失
方案2在4w级别出现任务丢失。但没有解决本质问题:向没有被map引用的list添加了数据
最终方案:使用AtomicReferenceArray类CAS操作,取代Map。
关键代码如下:
经过测试,并未发现任务丢失,并且效率很高,单秒10w任务下,put总时间为毫秒级
ps:
Other information