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

About NullPointerException in Recycles #355

Merged
merged 4 commits into from
Nov 28, 2019

Conversation

luozhiyun993
Copy link
Contributor

Motivation:

issue : #353

Modification:

Describe the idea and modifications you've done.

Result:

Fixes #.

If there is no issue then describe the changes introduced by this PR.

@fengjiachun
Copy link
Contributor

CLA is required @Devyun

@fengjiachun
Copy link
Contributor

And can you add some unit test?

@luozhiyun993
Copy link
Contributor Author

And can you add some unit test?

ok

@sofastack-bot sofastack-bot bot added size/M and removed size/XS labels Nov 25, 2019
@fengjiachun
Copy link
Contributor

CLA is required @Devyun

@luozhiyun993
Copy link
Contributor Author

CLA is required @Devyun

I have signed this CLA. but clahub is still show X

@fengjiachun
Copy link
Contributor

Running com.alipay.sofa.jraft.util.RecyclersTest
Exception in thread "Thread-6" Exception in thread "Thread-5" Exception in thread "Thread-11" java.lang.IllegalStateException: recycled already
	at com.alipay.sofa.jraft.util.Recyclers.recycle(Recyclers.java:104)
	at com.alipay.sofa.jraft.util.RecyclersTest.lambda$testRecycleMultipleAtDifferentThread$1(RecyclersTest.java:77)
	at java.lang.Thread.run(Thread.java:748)
java.lang.IllegalStateException: recycled already
	at com.alipay.sofa.jraft.util.Recyclers.recycle(Recyclers.java:104)
	at com.alipay.sofa.jraft.util.RecyclersTest.lambda$testRecycleMultipleAtDifferentThread$1(RecyclersTest.java:77)
	at java.lang.Thread.run(Thread.java:748)

ut is failed @Devyun

@fengjiachun
Copy link
Contributor


Stack<?> stack = this.stack;
if ( lastRecycledId != recycleId || stack == null) {
throw new IllegalStateException("recycled already");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -326,7 +329,8 @@ DefaultHandle pop() {
size--;
DefaultHandle ret = elements[size];
if (ret.lastRecycledId != ret.recycleId) {
throw new IllegalStateException("recycled multiple times");
LOG.warn("recycled already");
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pop 出现 (ret.lastRecycledId != ret.recycleId) 的情况应该是 recycle 逻辑有问题,这里并不是事故第一现场,并且这里 DefaultHandle 也没有做一些释放处理,所以我建议这里就维持刨出异常

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实

@fengjiachun
Copy link
Contributor

需要改一下原有的单侧: testMultipleRecycle
取消了抛出异常后这个单测就不对了

@fengjiachun
Copy link
Contributor

@Devyun

@luozhiyun993
Copy link
Contributor Author

@Devyun

好的

@killme2008
Copy link
Contributor

@Devyun Please rebase this branch with master.

@luozhiyun993
Copy link
Contributor Author

@Devyun Please rebase this branch with master.

I'm done

@fengjiachun
Copy link
Contributor

@Devyun
还有两个 test case 要麻烦你继续改下
ByteBufferCollectorTest.testMultipleRecycle
RecyclableByteBufferListTest.testMultipleRecycle

改为如下逻辑:

final RecyclableByteBufferList object = RecyclableByteBufferList.newInstance();
assertTrue(object.recycle());
assertFalse(object.recycle());

@luozhiyun993
Copy link
Contributor Author

@Devyun
还有两个 test case 要麻烦你继续改下
ByteBufferCollectorTest.testMultipleRecycle
RecyclableByteBufferListTest.testMultipleRecycle

改为如下逻辑:

final RecyclableByteBufferList object = RecyclableByteBufferList.newInstance();
assertTrue(object.recycle());
assertFalse(object.recycle());

如果要assertFalse(object.recycle());成立的话,需要改push和DefaultHandle#recycle的返回值,要改吗?

@fengjiachun
Copy link
Contributor

@Devyun
还有两个 test case 要麻烦你继续改下
ByteBufferCollectorTest.testMultipleRecycle
RecyclableByteBufferListTest.testMultipleRecycle
改为如下逻辑:

final RecyclableByteBufferList object = RecyclableByteBufferList.newInstance();
assertTrue(object.recycle());
assertFalse(object.recycle());

如果要assertFalse(object.recycle());成立的话,需要改push和DefaultHandle#recycle的返回值,要改吗?

牵一发动全身,我觉得我们可能错了 @killme2008 ,现在我更倾向于维持原有的抛出异常的策略,本身重复 recycle() 就是个严重的错误行为,应该禁止,没必要在这里为用户的错误行为容错

@@ -133,6 +140,13 @@ public final int threadLocalSize() {

public void recycle() {
Thread thread = Thread.currentThread();

final Stack<?> stack = this.stack;
if ( lastRecycledId != recycleId || stack == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行代码格式要调整下, lastRecycledId 前边不应该有空格,recycleId 前边有两个空格,应该改为一个

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

喜欢有代码洁癖的人,哈哈哈

@fengjiachun
Copy link
Contributor

@Devyun 我觉得要么我们还是恢复抛出异常? 也方便跟进 netty 原版 code 持续更新

@luozhiyun993
Copy link
Contributor Author

@Devyun 我觉得要么我们还是恢复抛出异常? 也方便跟进 netty 原版 code 持续更新

好滴~

luozhiyun added 2 commits November 28, 2019 18:49
* 'master' of https://github.com/sofastack/sofa-jraft:
  Feature/some changes (sofastack#358)
  fix elect priority (sofastack#357)
  Remove unused imports (sofastack#356)
  feat/priority_by_zongtanghu (sofastack#345)
@fengjiachun
Copy link
Contributor

@Devyun tks

@fengjiachun fengjiachun merged commit 0cf0c8e into sofastack:master Nov 28, 2019
@fengjiachun fengjiachun mentioned this pull request Nov 29, 2019
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants