Skip to content

fix flaky-test: AuthorizationTest.simple - #9740

Merged
codelipenghui merged 3 commits into
apache:masterfrom
315157973:flaky-test-1
Mar 4, 2021
Merged

codelipenghui merged 3 commits into
apache:masterfrom
315157973:flaky-test-1

Conversation

@315157973

@315157973 315157973 commented Feb 26, 2021 •

Copy link
Copy Markdown
Contributor

Fixes #9709

Motivation

Caused by: java.lang.NullPointerException
	at java.util.TreeMap.getEntry(TreeMap.java:347)

From the stack trace, it is caused by the key read by TreeMap being null. Therefore, the role must be null.

//PulsarAuthorizationProvider.java:404
Map<String, Set<AuthAction>> namespaceRoles = policies.get().auth_policies.namespace_auth;
Set<AuthAction> namespaceActions = namespaceRoles.get(role);

According to the code, the role is read through httpRequest.getAttribute in PulsarWebResource#clientAppId, so it is normal to be null.

Although the role is null, why do unit tests pass occasionally?
I added logs and found that the implementation class of Map is not always TreeMap, but occasionally HashMap.
When it is a HashMap, no error will be reported if the key is null. This problem is caused by Jackson's serialization.

I checked AuthPolicies, all Maps are initialized as TreeMap, but we have never used any features of TreeMap.
In order to avoid serialization problems elsewhere, I changed TreeMap to HashMap.
Another way is to add the annotation @JsonDeserialize(as = TreeMap.class), and then make the judgment that the key is null.
Why was TreeMap used here? May need @merlimat to help explain.

@lhotari

lhotari commented Feb 26, 2021

Copy link
Copy Markdown
Member

@315157973 This won't fix #9711 . I have put the explanation about the thread safety issue as a comment in #9711 (comment) .

@lhotari

lhotari commented Feb 26, 2021

Copy link
Copy Markdown
Member

The issue in #9711 is most likely different than the issue which makes AuthorizationTest.simple flaky.

@lhotari

lhotari commented Feb 27, 2021 •

Copy link
Copy Markdown
Member

I created an alternative fix which fixes a problem with the test itself: #9746 . I put some notes in the description about the relation to this fix.

Although the role is null, why do unit tests pass occasionally?
I added logs and found that the implementation class of Map is not always TreeMap, but occasionally HashMap.
When it is a HashMap, no error will be reported if the key is null. This problem is caused by Jackson's serialization.

I guess this explains why it passed in some cases. It's odd that the behavior is so nondeterministic. Why does the serialization get used at all? I had the impression that there's a local cache that will be used if the entries are available locally, however I haven't checked that aspect.

btw. The null value for the role shouldn't be a normal case. The fix in #9746 fixes the test itself that was invalid.

@315157973

Copy link
Copy Markdown
Contributor Author

I created an alternative fix which fixes a problem with the test itself: #9746 . I put some notes in the description about the relation to this fix.

Although the role is null, why do unit tests pass occasionally?
I added logs and found that the implementation class of Map is not always TreeMap, but occasionally HashMap.
When it is a HashMap, no error will be reported if the key is null. This problem is caused by Jackson's serialization.

I guess this explains why it passed in some cases. It's odd that the behavior is so nondeterministic. Why does the serialization get used at all? I had the impression that there's a local cache that will be used if the entries are available locally, however I haven't checked that aspect.

btw. The null value for the role shouldn't be a normal case. The fix in #9746 fixes the test itself that was invalid.

As long as it is serialized and deserialized once, it will become a hashMap. Because zk update takes time, sometimes it is still TreeMap locally.

@eolivelli

Copy link
Copy Markdown
Contributor

I am fine with having the HashMap and use it consistently.
so +1 from me to this patch.

This is definitely a good idea.
Using TreeMap consistently is okay as well, but if usually we have an HashMap (in the majority of cases) it is better to go with HashMap.
It would be better to have some way to ensure that we won't change it again in the future with some refactor.
So I don't see this patch as a direct fix for the test.

by the way I share @lhotari concerns, we should add explicit checks about security/auth data structures and consistency of the runtime environment.

so probably the best is to have both of the two patches merged.

namespaceResources().set(path(POLICIES, namespaceName.toString()), (policies) -> {
if (!policies.auth_policies.destination_auth.containsKey(topicUri)) {
policies.auth_policies.destination_auth.put(topicUri, new TreeMap<String, Set<AuthAction>>());
policies.auth_policies.destination_auth.put(topicUri, new HashMap<>());

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.

why don't we use Maps.newHashMap ?

@aahmed-se
aahmed-se self-requested a review March 4, 2021 23:33
@codelipenghui codelipenghui added this to the 2.8.0 milestone Mar 4, 2021
@codelipenghui
codelipenghui merged commit b2796f3 into apache:master Mar 4, 2021
mlyahmed pushed a commit to mlyahmed/pulsar that referenced this pull request Mar 5, 2021
Fixes apache#9709

### Motivation
```
Caused by: java.lang.NullPointerException
	at java.util.TreeMap.getEntry(TreeMap.java:347)
```
From the stack trace, it is caused by the key read by `TreeMap` being null. Therefore, the role must be null.
```
//PulsarAuthorizationProvider.java:404
Map<String, Set<AuthAction>> namespaceRoles = policies.get().auth_policies.namespace_auth;
Set<AuthAction> namespaceActions = namespaceRoles.get(role);
```

According to the code, the role is read through `httpRequest.getAttribute` in `PulsarWebResource#clientAppId`, so it is normal to be null.

Although the role is null, why do unit tests pass occasionally?
I added logs and found that the implementation class of Map is not always TreeMap, but occasionally HashMap.
When it is a HashMap, no error will be reported if the key is null. This problem is caused by Jackson's serialization.

I checked AuthPolicies, all Maps are initialized as TreeMap, but we have never used any features of TreeMap. 
In order to avoid serialization problems elsewhere, I changed TreeMap to HashMap.
Another way is to add the annotation @JsonDeserialize(as = TreeMap.class), and then make the judgment that the key is null.
Why was TreeMap used here? May need @merlimat  to help explain.
@315157973
315157973 deleted the flaky-test-1 branch March 12, 2021 13:52
@codelipenghui codelipenghui added the cherry-picked/branch-2.7 Archived: 2.7 is end of life label Mar 23, 2021
codelipenghui pushed a commit that referenced this pull request Mar 23, 2021
Fixes #9709

```
Caused by: java.lang.NullPointerException
	at java.util.TreeMap.getEntry(TreeMap.java:347)
```
From the stack trace, it is caused by the key read by `TreeMap` being null. Therefore, the role must be null.
```
//PulsarAuthorizationProvider.java:404
Map<String, Set<AuthAction>> namespaceRoles = policies.get().auth_policies.namespace_auth;
Set<AuthAction> namespaceActions = namespaceRoles.get(role);
```

According to the code, the role is read through `httpRequest.getAttribute` in `PulsarWebResource#clientAppId`, so it is normal to be null.

Although the role is null, why do unit tests pass occasionally?
I added logs and found that the implementation class of Map is not always TreeMap, but occasionally HashMap.
When it is a HashMap, no error will be reported if the key is null. This problem is caused by Jackson's serialization.

I checked AuthPolicies, all Maps are initialized as TreeMap, but we have never used any features of TreeMap.
In order to avoid serialization problems elsewhere, I changed TreeMap to HashMap.
Another way is to add the annotation @JsonDeserialize(as = TreeMap.class), and then make the judgment that the key is null.
Why was TreeMap used here? May need @merlimat  to help explain.

(cherry picked from commit b2796f3)
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.

Flaky-test: AuthorizationTest.simple

5 participants