Skip to content

AbstractMetadataStore: invalidate childrenCache correctly when node created - #14177

Merged
eolivelli merged 1 commit into
apache:branch-2.8from
michaeljmarshall:invalidate-children-cache-properly
Feb 9, 2022
Merged

eolivelli merged 1 commit into
apache:branch-2.8from
michaeljmarshall:invalidate-children-cache-properly

Conversation

@michaeljmarshall

Copy link
Copy Markdown
Member

Motivation

In Pulsar 2.8, there is currently a bug that can lead to an incorrectly cached value in the childrenCache. The resulting behavior is that the broker serves the stale cache value until it is evicted from the cache.

Steps to Reproduce Issue

Start a 2.8 cluster with at least 2 brokers. It is helpful to run with debug logging to observe the ZK watch notifications. Run the following bash commands in order:

BROKER_1=192.168.6.228
BROKER_2=192.168.79.61
bin/pulsar-admin --admin-url http://$BROKER_1:8080 tenants create test
bin/pulsar-admin --admin-url http://$BROKER_1:8080 namespaces create test/a
bin/pulsar-admin --admin-url http://$BROKER_2:8080 topics list test/a
bin/pulsar-admin --admin-url http://$BROKER_1:8080 topics create persistent://test/a/a
bin/pulsar-admin --admin-url http://$BROKER_2:8080 topics list test/a

When broker 2 handles the command for bin/pulsar-admin --admin-url http://$BROKER_2:8080 topics list test/a, it caches a miss in the childrenCache in AbstractMetadataStore for path /managed-ledgers/test/a/persistent.

After caching the miss, broker 2 only logs two ZK events:

05:21:16.810 [main-EventThread] DEBUG org.apache.pulsar.metadata.impl.ZKMetadataStore - Received ZK watch : WatchedEvent state:SyncConnected type:NodeCreated path:/admin/local-policies/test/a

05:21:19.808 [main-EventThread] DEBUG org.apache.pulsar.metadata.impl.ZKMetadataStore - Received ZK watch : WatchedEvent state:SyncConnected type:NodeCreated path:/managed-ledgers/test/a/persistent

Note that the second even is of type NodeCreated. Because of its type, the AbstractMetadataStore does not invalidate the correct node in the childrenCache:

public void accept(Notification n) {
String path = n.getPath();
NotificationType type = n.getType();
if (type == NotificationType.Created || type == NotificationType.Deleted) {
existsCache.synchronous().invalidate(path);
String parent = parent(path);
if (parent != null) {
childrenCache.synchronous().invalidate(parent);
}
}
if (type == NotificationType.ChildrenChanged) {
childrenCache.synchronous().invalidate(path);
}
if (type == NotificationType.Created || type == NotificationType.Deleted || type == NotificationType.Modified) {
metadataCaches.forEach(c -> c.accept(n));
}
}

I was not able to reproduce this issue in 2.9. My theory is that we get around it because we have a persistent watch at /.

Note also that when creating a second topic in the namespace, we see the following notification:

07:14:42.181 [main-EventThread] DEBUG org.apache.pulsar.metadata.impl.ZKMetadataStore - Received ZK watch : WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/managed-ledgers/test/a/persistent

In this case, we properly invalidate the child node in the cache.

Also note that in 2.7 we always invalidate the child node for a notification. I don't believe this is strictly necessary because we'll get NodeChildrenChanged notifications when the event is not created/deleted.

Modifications

  • Invalidate the path for childrenCache when the path is created or deleted.

Verifying this change

I added a test that failed before this change and passes after the change.

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

This is an internal change.

Documentation

  • no-need-doc

@Jason918

Jason918 commented Feb 9, 2022

Copy link
Copy Markdown
Contributor

After caching the miss, broker 2 only logs two ZK events:

This is interesting that broker2 does not receive the event NodeCreated path:/managed-ledgers/test/a/persistent/a. I think this is the root cause. If it does receive this event, the childrenCache would be properly invalidated, right?

@michaeljmarshall

Copy link
Copy Markdown
Member Author

After caching the miss, broker 2 only logs two ZK events:

This is interesting that broker2 does not receive the event NodeCreated path:/managed-ledgers/test/a/persistent/a. I think this is the root cause. If it does receive this event, the childrenCache would be properly invalidated, right?

@Jason918 - you're correct, broker 2 does not receive this notification. If I understand correctly, it doesn't get that notification because it isn't watching that node. It is watching /managed-ledgers/test/a/persistent because the internalGetList method triggers the "exists" watch when it also creates the cached value for /managed-ledgers/test/a/persistent.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, Great analysis and good work @michaeljmarshall

@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

@Jason918 Jason918 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.

Great catch!

@eolivelli
eolivelli merged commit 7716894 into apache:branch-2.8 Feb 9, 2022
@michaeljmarshall
michaeljmarshall deleted the invalidate-children-cache-properly branch February 9, 2022 17:30
List<String> topics = second.topics().getList("public/default");
assertTrue(topics.contains("persistent://public/default/my-topic"));
}
}

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.

@michaeljmarshall This test should go in the pulsar-metadata module (without using the `PulsarAdmin) and we can test across all the backends

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are you asking for a more generic test in addition to this one or instead of this one? Since we don't have the broker in pulsar-metadata, it seems like I can only write a test that ensures a notification for Created/Deleted triggers cache invalidation for the path on childrenCache. We'd also lose the double broker integration test.

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.

yes, we can double the test to just verify the cache correctness without needing a broker.

though, I just saw that this is for 2.8 and I believe we should already have tests covering this scenario in master (or 2.9)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

By my testing, this regression was only in 2.8. #14178 was just merged to master though, which contains the test (with slight modifications) from this PR. Are you saying we should revert #14178?

@zymap zymap added the cherry-picked/branch-2.8 Archived: 2.8 is end of life label Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/metadata cherry-picked/branch-2.8 Archived: 2.8 is end of life doc-not-needed Your PR changes do not impact docs release/2.8.3 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.

7 participants