Skip to content

Make LocalPolicies immutable to avoid concurrent modify inconsistent. - #9598

Merged
eolivelli merged 6 commits into
apache:masterfrom
lifepuzzlefun:make_LocalPolicies_immutable_to_avoid_concurrent_modification_problem
Apr 27, 2021
Merged

eolivelli merged 6 commits into
apache:masterfrom
lifepuzzlefun:make_LocalPolicies_immutable_to_avoid_concurrent_modification_problem

Conversation

@lifepuzzlefun

@lifepuzzlefun lifepuzzlefun commented Feb 16, 2021 •

Copy link
Copy Markdown
Contributor

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
}

@Override
public String toString() {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

zymap commented Feb 23, 2021

Copy link
Copy Markdown
Member

/pulsarbot run-failure-checks

@sijie

sijie commented Feb 24, 2021

Copy link
Copy Markdown
Member

Ping @merlimat

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

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

zymap commented Mar 1, 2021

Copy link
Copy Markdown
Member

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

@zymap

zymap commented Mar 1, 2021

Copy link
Copy Markdown
Member

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

@315157973

Copy link
Copy Markdown
Contributor

/pulsarbot run-failure-checks

@codelipenghui

Copy link
Copy Markdown
Contributor

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

@315157973

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

@codelipenghui @315157973 rebase current master. please check again

@eolivelli

Copy link
Copy Markdown
Contributor

@merlimat can you take a final look please ?

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

Copy link
Copy Markdown
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