You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Creating and modifying authorization policies is not thread safe #9711
Creating and modifying authorization policies is not thread safe. It leads to unexpected NullPointerExceptions for example and could lead to authorization data corruption.
To Reproduce
The flaky test #9709 is one example of the problem.
Expected behavior
Creating and modifying authorization policies should be thread safe.
When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?
When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?
that part is not the concern. The concerns are the maps that are contained in the cached AuthPolicies entry. They are mutated without synchronization on both read & write access. Mutation for example here:
When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?
that part is not the concern. The concerns are the maps that are contained in the cached AuthPolicies entry. They are mutated without synchronization on both read & write access. Mutation for example here:
Normal scene:
t1: thread 1 updates zk successfully
t2: thread 1 updates the cache
t3: thread 2 updates zk successfully
t4: Thread 2 updates the cache
Concurrent scene:
t1: thread 1 updates zk successfully
t2: Thread 2 updates zk, which fails because the version is different
t3: Thread 1 updates the cache
the problem is not about certain sequence of events or interactions with zk. It's about unsafe mutation and access of the TreeMap / HashMap instances contained in the AuthPolicies class. A TreeMap or HashMap can only be safely mutated when all access (mutation and reading) happens under the same object monitor. Violating this rule will lead to nondeterministic behavior.
On a busy system, unsafe HashMap mutation and access will eventually lead to infinite loops and 100% CPU core usage when the HashMap data structures get corrupted.
This problem is demonstrated and explained in this article, in the section "HashMap infinite looping problem replication".
I might be missing something, but I can see that the Pulsar policy creation and modification contains quite a few locations where there is unsafe HashMap/TreeMap mutation and access. @merlimat PTAL
@lhotari How about change HashMap to ConcurrentHashMap, and do not update the cache, only invalidate the cache .
+1, I guess ConcurrentHashMap is one possibility and probably leads to the simplest fix to the issue.
In general, I'd prefer a solution that is based on immutability so that an update returns a new copy and new instance. This approach is used in an open PR #9598 . However in that case, there aren't Maps that are involved.
Using persistent collections such as PCollections library could be useful in the immutable approach.
In general, I'd prefer a solution that is based on immutability so that an update returns a new copy and new instance. This approach is used in an open PR #9598 . However in that case, there aren't Maps that are involved.
Creating new objects will produce garbage, I think ConcurrentHashMap will be better
Creating new objects will produce garbage, I think ConcurrentHashMap will be better
New objects are only a problem if it causes a bottleneck or excessive GC pressure. I don't think that policy modifications would be a large part of the workload. Therefore there isn't a reason to reject solutions around immutability.
btw. Persistent data collections such as PCollections library help minimize the number of new objects that are created when using immutable collections.
New objects are only a problem if it causes a bottleneck or excessive GC pressure. I don't think that policy modifications would be a large part of the workload. Therefore there isn't a reason to reject solutions around immutability.
In the scene of this issue, compared with ConcurrentHashMap, what are its advantages?
In the scene of this issue, compared with ConcurrentHashMap, what are its advantages?
One of the advantages of immutable objects (all fields final in Java) is that there isn't any need for locks at access time after there is a reference to the object. A design where there's some kind of "single writer principle" and immutable objects can be easier to get right. However it might be an overkill to modify an existing solution to follow this type of pattern.
I guess that the benefit of using ConcurrentHashMap (or Collections.synchronizedMap wrapper) is that the change is relatively small to the existing solution as you have already demonstrated in the PR #9850 .
changed the title [-]Creating and modifying authorization policies in not thread safe[/-][+]Creating and modifying authorization policies is not thread safe[/+]on Mar 12, 2021
Activity
eolivelli commentedon Feb 25, 2021
Good catch @lhotari
I believe this is a blocker issue.
@wolfstudy @codelipenghui can we fix it for 2.7.1 ?
315157973 commentedon Feb 26, 2021
When updating, a version will be passed in to determine whether the data has been modified. This is an optimistic lock. Why is there a thread safety issue?
lhotari commentedon Feb 26, 2021
that part is not the concern. The concerns are the maps that are contained in the cached AuthPolicies entry. They are mutated without synchronization on both read & write access. Mutation for example here:
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/NamespacesBase.java
Line 688 in 7001f64
Accessed here:
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
Line 206 in 1fab5aa
315157973 commentedon Feb 26, 2021
Could you give an example like the following?
Normal scene:
t1: thread 1 updates zk successfully
t2: thread 1 updates the cache
t3: thread 2 updates zk successfully
t4: Thread 2 updates the cache
Concurrent scene:
t1: thread 1 updates zk successfully
t2: Thread 2 updates zk, which fails because the version is different
t3: Thread 1 updates the cache
lhotari commentedon Feb 26, 2021
the problem is not about certain sequence of events or interactions with zk. It's about unsafe mutation and access of the TreeMap / HashMap instances contained in the AuthPolicies class. A TreeMap or HashMap can only be safely mutated when all access (mutation and reading) happens under the same object monitor. Violating this rule will lead to nondeterministic behavior.
lhotari commentedon Mar 1, 2021
It seems that ReplicatorTest triggers some of the thread safety issues in updating policies. Some of the reported issues about ReplicatorTest flakiness.
codelipenghui commentedon Mar 2, 2021
@eolivelli we don't want to block the 2.7.1 release, instead we can accelerate the 2.7.2 release. Here is the release proposal that we have discussed before https://github.com/apache/pulsar/wiki/PIP-47%3A-Time-Based-Release-Plan. If the problem is not introduced after 2.7.0 and it does not break the existing behavior, it's better to do not to block the 2.7.1 release.
eolivelli commentedon Mar 2, 2021
@codelipenghui I am fine with keeping it only on master branch.
lhotari commentedon Mar 8, 2021
On a busy system, unsafe HashMap mutation and access will eventually lead to infinite loops and 100% CPU core usage when the HashMap data structures get corrupted.
This problem is demonstrated and explained in this article, in the section "HashMap infinite looping problem replication".
I might be missing something, but I can see that the Pulsar policy creation and modification contains quite a few locations where there is unsafe HashMap/TreeMap mutation and access. @merlimat PTAL
lhotari commentedon Mar 8, 2021
+1, I guess ConcurrentHashMap is one possibility and probably leads to the simplest fix to the issue.
In general, I'd prefer a solution that is based on immutability so that an update returns a new copy and new instance. This approach is used in an open PR #9598 . However in that case, there aren't Maps that are involved.
Using persistent collections such as PCollections library could be useful in the immutable approach.
315157973 commentedon Mar 8, 2021
Creating new objects will produce garbage, I think ConcurrentHashMap will be better
lhotari commentedon Mar 8, 2021
New objects are only a problem if it causes a bottleneck or excessive GC pressure. I don't think that policy modifications would be a large part of the workload. Therefore there isn't a reason to reject solutions around immutability.
btw. Persistent data collections such as PCollections library help minimize the number of new objects that are created when using immutable collections.
315157973 commentedon Mar 8, 2021
In the scene of this issue, compared with ConcurrentHashMap, what are its advantages?
lhotari commentedon Mar 9, 2021
One of the advantages of immutable objects (all fields final in Java) is that there isn't any need for locks at access time after there is a reference to the object. A design where there's some kind of "single writer principle" and immutable objects can be easier to get right. However it might be an overkill to modify an existing solution to follow this type of pattern.
I guess that the benefit of using ConcurrentHashMap (or Collections.synchronizedMap wrapper) is that the change is relatively small to the existing solution as you have already demonstrated in the PR #9850 .
[-]Creating and modifying authorization policies in not thread safe[/-][+]Creating and modifying authorization policies is not thread safe[/+]Make readModifyUpdate in MetadataCacheImpl thread-safe (#9900)
Make readModifyUpdate in MetadataCacheImpl thread-safe (apache#9900)
codelipenghui commentedon Mar 4, 2022
The issue had no activity for 30 days, mark with Stale label.
lhotari commentedon Jun 18, 2025
Addressed for namespace policies by #9900, separate issue for topic policies in #21303