Skip to content

[fix][broker]Cache invalidation due to concurrent access - #18076

Merged
Technoboy- merged 2 commits into
apache:masterfrom
poorbarcode:flaky/ManagedLedgerBkTest
Oct 19, 2022
Merged

Technoboy- merged 2 commits into
apache:masterfrom
poorbarcode:flaky/ManagedLedgerBkTest

Conversation

@poorbarcode

@poorbarcode poorbarcode commented Oct 17, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #17908

Motivation

  • ManagedCursorImpl.setReadPosition() will trigger cleaning of caches smaller than the earliest readPosition
  • In ManagedLedgerFactoryImpl, there is a scheduled task that deletes inactive cache data every second

<strong(High light)>When the cursor updates the readPosition and clears the outdated ledger cache concurrency, an entry's cache is incorrectly deleted.

E.g.

ManagedCursorImpl.setReadPosition ManagedLedgerFactory.doCacheEviction
get the first entry in the Cache
invalidate position 1:1
delete 1:1 from EntryCache
recycle entry and set entry.timestamp to -1
check: entry.timestamp > maxTimestamp
(High light)pop first entry from EntryCache: expecting 1:1, but really 1:2

So entry 1:2 is incorrectly deleted.

Map.Entry<Key, Value> entry = entries.firstEntry();
if (entry == null || timestampExtractor.getTimestamp(entry.getValue()) > maxTimestamp) {
break;
}
entry = entries.pollFirstEntry();
if (entry == null) {
break;
}
Value value = entry.getValue();
removedSize += weighter.getSize(value);
removedCount++;
value.release();

You can reproduce the problem by runningManagedLedgerBkTest.verifyConcurrentUsage 200~1000 times

Modifications

Use the instruction remove(key) instead of popFirst() to delete the target Entry

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository:

@nodece nodece 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

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

Nice catch!


entry = entries.pollFirstEntry();
if (entry == null) {
Value value = entries.remove(entry.getKey());

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.

Suggested change
Value value = entries.remove(entry.getKey());
Value value = entries.remove(entry.getKey(), entry.getValue());

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.

Already fixed

@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

is there anyway to add a unit test ?

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #18076 (a7b6598) into master (6c65ca0) will increase coverage by 15.90%.
The diff coverage is 57.14%.

Impacted file tree graph

@@              Coverage Diff              @@
##             master   #18076       +/-   ##
=============================================
+ Coverage     34.91%   50.81%   +15.90%     
- Complexity     5707     8697     +2990     
=============================================
  Files           607      607               
  Lines         53396    53398        +2     
  Branches       5712     5714        +2     
=============================================
+ Hits          18644    27136     +8492     
+ Misses        32119    23242     -8877     
- Partials       2633     3020      +387     
Flag Coverage Δ
unittests 50.81% <57.14%> (+15.90%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
.../service/SystemTopicBasedTopicPoliciesService.java 61.39% <0.00%> (+9.80%) ⬆️
.../pulsar/broker/stats/BrokerOperabilityMetrics.java 98.21% <ø> (+5.56%) ⬆️
...g/apache/pulsar/compaction/CompactedTopicImpl.java 69.28% <0.00%> (+58.57%) ⬆️
.../org/apache/pulsar/broker/admin/v2/Namespaces.java 54.72% <50.00%> (+46.69%) ⬆️
...apache/pulsar/proxy/server/DirectProxyHandler.java 63.63% <50.00%> (ø)
...broker/delayed/InMemoryDelayedDeliveryTracker.java 65.00% <75.00%> (+65.00%) ⬆️
...pulsar/broker/admin/impl/PersistentTopicsBase.java 51.48% <100.00%> (+40.08%) ⬆️
...rg/apache/pulsar/broker/service/BrokerService.java 58.04% <100.00%> (+10.03%) ⬆️
...e/pulsar/broker/service/EntryBatchIndexesAcks.java 82.14% <0.00%> (-10.72%) ⬇️
.../apache/pulsar/broker/namespace/LookupOptions.java 87.50% <0.00%> (ø)
... and 146 more

@poorbarcode

Copy link
Copy Markdown
Contributor Author

Hi @eolivelli

Sorry, I have been too busy these two days to add a test. I created a new pr to make up for it.

lhotari pushed a commit to datastax/pulsar that referenced this pull request Oct 20, 2022
congbobo184 pushed a commit that referenced this pull request Nov 14, 2022
@congbobo184 congbobo184 added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label Nov 14, 2022
aloyszhang pushed a commit to aloyszhang/pulsar that referenced this pull request Nov 30, 2022
…t !155) (merge request !156)

Squash merge branch 'release-2.8.1.4-fix-invalid-cache' into 'release-2.8.1.4'
Fixes #<xyz>

### Motivation
--bug=104424941 Pulsar Broker cache失效问题修复(apache#18076) (merge request !155)

TAPD: --bug=104424941
congbobo184 pushed a commit that referenced this pull request Dec 7, 2022
liangyepianzhou pushed a commit that referenced this pull request Dec 12, 2022
aloyszhang pushed a commit to aloyszhang/pulsar that referenced this pull request Jan 9, 2023
…t !155) (merge request !156)

Squash merge branch 'release-2.8.1.4-fix-invalid-cache' into 'release-2.8.1.4'
Fixes #<xyz>

### Motivation
--bug=104424941 Pulsar Broker cache失效问题修复(apache#18076) (merge request !155)

TAPD: --bug=104424941
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: ManagedLedgerBkTest.verifyConcurrentUsage

8 participants