GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [improve][ml] Share read-permit release callbacks within a batch (#26… · ascentstream/pulsar@30b975a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • managed-ledger/src
    • main/java/org/apache/bookkeeper/mledger/impl/cache
      • RangeEntryCacheImpl.java
    • test/java/org/apache/bookkeeper/mledger/impl/cache
      • RangeEntryCacheImplTest.java
  • microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache
    • ReadBatchCallbackBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java‎

Copy file name to clipboardExpand all lines: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImpl.java
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,13 @@ public void readEntriesComplete(List<Entry> entries, Object ctx2) {
371371
if (!entries.isEmpty()) {
372372
// release permits only when entries have been handled
373373
AtomicInteger remainingCount = new AtomicInteger(entries.size());
374+
Runnable releasePermits = () -> {
375+
if (remainingCount.decrementAndGet() <= 0) {
376+
pendingReadsLimiter.release(handle);
377+
}
378+
};
374379
for (Entry entry : entries) {
375-
((EntryImpl) entry).onDeallocate(() -> {
376-
if (remainingCount.decrementAndGet() <= 0) {
377-
pendingReadsLimiter.release(handle);
378-
}
379-
});
380+
((EntryImpl) entry).onDeallocate(releasePermits);
380381
}
381382
} else {
382383
pendingReadsLimiter.release(handle);
Collapse file

‎managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImplTest.java‎

Copy file name to clipboardExpand all lines: managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/cache/RangeEntryCacheImplTest.java
+40-1Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,45 @@ public void setup() {
107107
expectedReadCount = () -> 1;
108108
}
109109

110+
@Test
111+
public void testReadPermitsReleasedOnlyAfterLastEntryReference() {
112+
InflightReadsLimiter limiter = mockEntryCacheManager.getInflightReadsLimiter();
113+
InflightReadsLimiter.Handle handle = new InflightReadsLimiter.Handle(300, 0, true);
114+
CompletableFuture<List<Entry>> result = new CompletableFuture<>();
115+
rangeEntryCache.doAsyncReadEntriesWithAcquiredPermits(lh,
116+
PositionFactory.create(1, 0), PositionFactory.create(1, 2), 3, expectedReadCount,
117+
new AsyncCallbacks.ReadEntriesCallback() {
118+
@Override
119+
public void readEntriesComplete(List<Entry> entries, Object ctx) {
120+
result.complete(entries);
121+
}
122+
123+
@Override
124+
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
125+
result.completeExceptionally(exception);
126+
}
127+
}, null, handle, 300);
128+
assertThat(result).isCompleted();
129+
List<Entry> entries = result.getNow(null);
130+
try {
131+
assertThat(entries).hasSize(3);
132+
((ReferenceCountedEntry) entries.get(0)).retain();
133+
entries.get(2).release();
134+
entries.get(0).release();
135+
entries.get(1).release();
136+
verify(limiter, never()).release(any());
137+
entries.get(0).release();
138+
verify(limiter, times(1)).release(handle);
139+
} finally {
140+
for (Entry entry : entries) {
141+
ReferenceCountedEntry referenceCountedEntry = (ReferenceCountedEntry) entry;
142+
if (referenceCountedEntry.refCnt() > 0) {
143+
referenceCountedEntry.release(referenceCountedEntry.refCnt());
144+
}
145+
}
146+
}
147+
}
148+
110149
private RangeEntryCacheImpl createRangeEntryCache(boolean copyEntries) {
111150
return new RangeEntryCacheImpl(mockEntryCacheManager, mockManagedLedger, copyEntries,
112151
mockRangeCacheRemovalQueue, EntryLengthFunction.DEFAULT, pendingReadsManager);
@@ -528,4 +567,4 @@ public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
528567
}
529568
});
530569
}
531-
}
570+
}
Collapse file

‎microbench/src/main/java/org/apache/bookkeeper/mledger/impl/cache/ReadBatchCallbackBenchmark.java‎

Copy file name to clipboard
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bookkeeper.mledger.impl.cache;
20+
21+
import io.opentelemetry.api.OpenTelemetry;
22+
import java.util.concurrent.Executors;
23+
import java.util.concurrent.ScheduledExecutorService;
24+
import java.util.concurrent.TimeUnit;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
import org.apache.bookkeeper.mledger.impl.EntryImpl;
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.annotations.BenchmarkMode;
29+
import org.openjdk.jmh.annotations.Fork;
30+
import org.openjdk.jmh.annotations.Measurement;
31+
import org.openjdk.jmh.annotations.Mode;
32+
import org.openjdk.jmh.annotations.OutputTimeUnit;
33+
import org.openjdk.jmh.annotations.Param;
34+
import org.openjdk.jmh.annotations.Scope;
35+
import org.openjdk.jmh.annotations.Setup;
36+
import org.openjdk.jmh.annotations.State;
37+
import org.openjdk.jmh.annotations.TearDown;
38+
import org.openjdk.jmh.annotations.Warmup;
39+
40+
/** Measures real entry-copy/release and read-permit accounting with per-entry or per-batch callbacks. */
41+
@State(Scope.Thread)
42+
@BenchmarkMode(Mode.AverageTime)
43+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
44+
@Warmup(iterations = 3, time = 1)
45+
@Measurement(iterations = 5, time = 1)
46+
@Fork(2)
47+
public class ReadBatchCallbackBenchmark {
48+
private static final long LIMIT_BYTES = 16 * 1024 * 1024;
49+
50+
@Param({"1", "100", "1000"})
51+
public int batchSize;
52+
53+
@Param({"false", "true"})
54+
public boolean sharedCallback;
55+
56+
private EntryImpl[] cachedEntries;
57+
private EntryImpl[] readEntries;
58+
private InflightReadsLimiter limiter;
59+
private ScheduledExecutorService executor;
60+
61+
@Setup
62+
public void setup() {
63+
executor = Executors.newSingleThreadScheduledExecutor();
64+
limiter = new InflightReadsLimiter(LIMIT_BYTES, 1, 0, executor, OpenTelemetry.noop());
65+
cachedEntries = new EntryImpl[batchSize];
66+
readEntries = new EntryImpl[batchSize];
67+
for (int i = 0; i < batchSize; i++) {
68+
cachedEntries[i] = EntryImpl.create(1, i, new byte[128]);
69+
cachedEntries[i].getPosition();
70+
}
71+
}
72+
73+
@Benchmark
74+
public long readAndReleaseBatch() {
75+
InflightReadsLimiter batchLimiter = limiter;
76+
InflightReadsLimiter.Handle handle = batchLimiter.acquire(batchSize * 128L, null).orElseThrow();
77+
AtomicInteger remainingCount = new AtomicInteger(batchSize);
78+
Runnable shared = sharedCallback ? releaseCallback(remainingCount, batchLimiter, handle) : null;
79+
for (int i = 0; i < batchSize; i++) {
80+
EntryImpl entry = EntryImpl.create(cachedEntries[i]);
81+
entry.onDeallocate(sharedCallback ? shared : releaseCallback(remainingCount, batchLimiter, handle));
82+
readEntries[i] = entry;
83+
}
84+
for (int i = batchSize - 1; i >= 0; i--) {
85+
readEntries[i].release();
86+
readEntries[i] = null;
87+
}
88+
return batchLimiter.getRemainingBytes();
89+
}
90+
91+
private static Runnable releaseCallback(AtomicInteger remainingCount, InflightReadsLimiter limiter,
92+
InflightReadsLimiter.Handle handle) {
93+
return () -> {
94+
if (remainingCount.decrementAndGet() <= 0) {
95+
limiter.release(handle);
96+
}
97+
};
98+
}
99+
100+
@TearDown
101+
public void tearDown() {
102+
try {
103+
if (limiter.getRemainingBytes() != LIMIT_BYTES) {
104+
throw new IllegalStateException("Read permits were not released exactly once per batch");
105+
}
106+
} finally {
107+
for (EntryImpl entry : cachedEntries) {
108+
entry.release();
109+
}
110+
limiter.close();
111+
executor.shutdownNow();
112+
}
113+
}
114+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.