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

Make LocalPolicies immutable to avoid concurrent modify inconsistent. #9598

Conversation

lifepuzzlefun
Copy link
Contributor

@lifepuzzlefun lifepuzzlefun commented Feb 16, 2021

Fixes #9595

Motivation

make LocalPolicies immutable to avoid concurrent modify inconsistent.

NamespaceService.updateNamespaceBundles is not thread safe.
when split the same namespace concurrently,
both 2 thread can get the same LocalPolicies instance from pulsar.getLocalZkCacheService().policiesCache()

and updateNamespaceBundles change the LocalPolicies.bundles without
concurrent protect. which can cause the param NamespaceBundles is inconsistent with the serialized data.

eg.

origin bundle data [1,3,5]

the thread1 split bundle [1,3] and want to set path in zk with bundle data [1, 2, 3,5] and version 3

the thread2 split bundle [3,5] and want to set path in zk with bundle data [1,3, 4, 5] and version 3

one concurrent run can be following:
after thread1 gene bundle data [1,2,3,5] before go to the line zk.setData(version)
thread2 modify the bundle data [1,3,4,5]
then thread1 won the version write. and return success updateNamespaceBundles but the data write is [1,3,4,5]

thread2 will retry and try to split [3,5] again.but [3,5] already not occur in bundleData (because modify the same instance concurrently).then the unit test in #9595 fail with bundle not found in namespace.

Modifications

make LocalPolicies immutable. each time modify field need object copy to avoid change the same instance concurrently.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests, such as V1_AdminApiTest.testNamespaceSplitBundleConcurrent.

Does this pull request potentially affect one of the following parts:

no

Documentation

no

@codelipenghui codelipenghui added the type/bug The PR fixed a bug or issue reported a bug label Feb 16, 2021
@codelipenghui codelipenghui added this to the 2.8.0 milestone Feb 16, 2021
@@ -52,4 +60,12 @@ public boolean equals(Object obj) {
return false;
}

@Override
public String toString() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use @ToString() annotation on the class

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we could similarly take out the equals() and hashCode() methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for reply. i'll change this.

@lifepuzzlefun lifepuzzlefun force-pushed the make_LocalPolicies_immutable_to_avoid_concurrent_modification_problem branch from f794b15 to 19c79ee Compare February 20, 2021 10:43
@zymap
Copy link
Member

zymap commented Feb 23, 2021

/pulsarbot run-failure-checks

@sijie
Copy link
Member

sijie commented Feb 24, 2021

Ping @merlimat

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

I left one comment, can you please take a look ?

@zymap
Copy link
Member

zymap commented Mar 1, 2021

@WJL3333 Could you please take a look at Enrico's comment?

@zymap
Copy link
Member

zymap commented Mar 1, 2021

Because it's flaky tests fix. I will move this to the next release.

@315157973
Copy link
Contributor

/pulsarbot run-failure-checks

@codelipenghui
Copy link
Contributor

@WJL3333 You can rebase to the current master branch to check if the CI can get passed.

@315157973
Copy link
Contributor

Some points are fixed in this pr #9900 . Please take a look @WJL3333

… inconsistent.

`NamespaceService.updateNamespaceBundles` is not thread safe.
when split the same namespace concurrently,
both 2 thread can get the same `LocalPolicies` instance from pulsar.getLocalZkCacheService().policiesCache()

and `updateNamespaceBundles` change the LocalPolicies.bundles without
concurrent protect. which can cause the param `NamespaceBundles` is inconsistent with the serialized data.
default value in default constructor.

2. add timestamp in `ReplicatorTest.testConcurrentReplicator` to
avoid namespace create got namespace already exist.
@lifepuzzlefun lifepuzzlefun force-pushed the make_LocalPolicies_immutable_to_avoid_concurrent_modification_problem branch from 990c2d1 to 2fa1820 Compare April 20, 2021 18:05
@lifepuzzlefun
Copy link
Contributor Author

@codelipenghui @315157973 rebase current master. please check again

@eolivelli
Copy link
Contributor

@merlimat can you take a final look please ?

@eolivelli eolivelli merged commit bbd4e5d into apache:master Apr 27, 2021
@eolivelli
Copy link
Contributor

This patch cannot be applied to branch-2.7, I am removing the release/2.7.2 label

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky-test: V1_AdminApiTest.testNamespaceSplitBundleConcurrent
7 participants