Skip to content

Commit 60e5199

Browse files
Simplify CacheFile acquire and release Methods (elastic#64139) (elastic#64165)
Follow up to elastic#63911 making these methods either work out or throw an already closed exception and resulting possible simplifications.
1 parent ea2ebe7 commit 60e5199

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CacheFile.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ FileChannel getChannel() {
128128
return reference == null ? null : reference.fileChannel;
129129
}
130130

131-
public boolean acquire(final EvictionListener listener) throws IOException {
131+
public void acquire(final EvictionListener listener) throws IOException {
132132
assert listener != null;
133133

134134
ensureOpen();
@@ -150,12 +150,14 @@ public boolean acquire(final EvictionListener listener) throws IOException {
150150
refCounter.decRef();
151151
}
152152
}
153+
} else {
154+
assert evicted.get();
155+
throwAlreadyEvicted();
153156
}
154157
assert invariant();
155-
return success;
156158
}
157159

158-
public boolean release(final EvictionListener listener) {
160+
public void release(final EvictionListener listener) {
159161
assert listener != null;
160162

161163
boolean success = false;
@@ -179,7 +181,6 @@ public boolean release(final EvictionListener listener) {
179181
}
180182
}
181183
assert invariant();
182-
return success;
183184
}
184185

185186
private boolean assertNoPendingListeners() {
@@ -244,10 +245,14 @@ public String toString() {
244245

245246
private void ensureOpen() {
246247
if (evicted.get()) {
247-
throw new AlreadyClosedException("Cache file is evicted");
248+
throwAlreadyEvicted();
248249
}
249250
}
250251

252+
private static void throwAlreadyEvicted() {
253+
throw new AlreadyClosedException("Cache file is evicted");
254+
}
255+
251256
@FunctionalInterface
252257
interface RangeAvailableHandler {
253258
int onRangeAvailable(FileChannel channel) throws IOException;

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/cache/CachedBlobContainerIndexInput.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.action.ActionListener;
1919
import org.elasticsearch.blobstore.cache.BlobStoreCacheService;
2020
import org.elasticsearch.blobstore.cache.CachedBlob;
21-
import org.elasticsearch.common.Nullable;
2221
import org.elasticsearch.common.SuppressForbidden;
2322
import org.elasticsearch.common.bytes.BytesReference;
2423
import org.elasticsearch.common.collect.Tuple;
@@ -135,14 +134,6 @@ private Tuple<Long, Long> computeRange(long position) {
135134
return Tuple.tuple(start, end);
136135
}
137136

138-
private CacheFile getCacheFileSafe() throws Exception {
139-
final CacheFile cacheFile = cacheFileReference.get();
140-
if (cacheFile == null) {
141-
throw new AlreadyClosedException("Failed to acquire a non-evicted cache file");
142-
}
143-
return cacheFile;
144-
}
145-
146137
@Override
147138
protected void readInternal(ByteBuffer b) throws IOException {
148139
ensureContext(ctx -> ctx != CACHE_WARMING_CONTEXT);
@@ -163,7 +154,7 @@ protected void readInternal(ByteBuffer b) throws IOException {
163154
logger.trace("readInternal: read [{}-{}] ([{}] bytes) from [{}]", position, position + length, length, this);
164155

165156
try {
166-
final CacheFile cacheFile = getCacheFileSafe();
157+
final CacheFile cacheFile = cacheFileReference.get();
167158

168159
// Can we serve the read directly from disk? If so, do so and don't worry about anything else.
169160

@@ -447,7 +438,7 @@ public void prefetchPart(final int part) throws IOException {
447438
assert assertRangeIsAlignedWithPart(partRange);
448439

449440
try {
450-
final CacheFile cacheFile = getCacheFileSafe();
441+
final CacheFile cacheFile = cacheFileReference.get();
451442

452443
final Tuple<Long, Long> range = cacheFile.getAbsentRangeWithin(partRange.v1(), partRange.v2());
453444
if (range == null) {
@@ -775,7 +766,6 @@ private CacheFileReference(SearchableSnapshotDirectory directory, String fileNam
775766
this.directory = directory;
776767
}
777768

778-
@Nullable
779769
CacheFile get() throws Exception {
780770
CacheFile currentCacheFile = cacheFile.get();
781771
if (currentCacheFile != null) {
@@ -788,13 +778,11 @@ CacheFile get() throws Exception {
788778
if (currentCacheFile != null) {
789779
return currentCacheFile;
790780
}
791-
if (newCacheFile.acquire(this)) {
792-
final CacheFile previousCacheFile = cacheFile.getAndSet(newCacheFile);
793-
assert previousCacheFile == null;
794-
return newCacheFile;
795-
}
781+
newCacheFile.acquire(this);
782+
final CacheFile previousCacheFile = cacheFile.getAndSet(newCacheFile);
783+
assert previousCacheFile == null;
784+
return newCacheFile;
796785
}
797-
return null;
798786
}
799787

800788
@Override

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/cache/CacheFileTests.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,18 @@ public void testAcquireAndRelease() throws Exception {
3939
assertThat("Cache file is not acquired: file does not exist", Files.exists(file), is(false));
4040

4141
final TestEvictionListener listener = new TestEvictionListener();
42-
boolean acquired = cacheFile.acquire(listener);
43-
assertThat("Cache file has been acquired", acquired, is(true));
42+
cacheFile.acquire(listener);
4443
assertThat("Cache file has been acquired: file should exists", Files.exists(file), is(true));
4544
assertThat("Cache file has been acquired: channel should exists", cacheFile.getChannel(), notNullValue());
4645
assertThat("Cache file has been acquired: channel is open", cacheFile.getChannel().isOpen(), is(true));
4746
assertThat("Cache file has been acquired: eviction listener is not executed", listener.isCalled(), is(false));
4847

49-
boolean released = cacheFile.release(listener);
50-
assertThat("Cache file has been released", released, is(true));
48+
cacheFile.release(listener);
5149
assertThat("Cache file has been released: eviction listener is not executed", listener.isCalled(), is(false));
5250
assertThat("Cache file has been released: channel does not exist", cacheFile.getChannel(), nullValue());
5351
assertThat("Cache file is not evicted: file still exists after release", Files.exists(file), is(true));
5452

55-
acquired = cacheFile.acquire(listener);
56-
assertThat("Cache file is acquired again", acquired, is(true));
53+
cacheFile.acquire(listener);
5754

5855
FileChannel fileChannel = cacheFile.getChannel();
5956
assertThat("Channel should exists", fileChannel, notNullValue());
@@ -68,8 +65,7 @@ public void testAcquireAndRelease() throws Exception {
6865
assertThat("Cache file is evicted but not fully released: channel is open", cacheFile.getChannel().isOpen(), is(true));
6966
assertThat("Channel didn't change after eviction", cacheFile.getChannel(), sameInstance(fileChannel));
7067

71-
released = cacheFile.release(listener);
72-
assertTrue("Cache file is fully released", released);
68+
cacheFile.release(listener);
7369
assertThat("Cache file evicted and fully released: channel does not exist", cacheFile.getChannel(), nullValue());
7470
assertThat("Cache file has been deleted", Files.exists(file), is(false));
7571
}
@@ -83,14 +79,12 @@ public void testCacheFileNotAcquired() throws IOException {
8379

8480
if (randomBoolean()) {
8581
final TestEvictionListener listener = new TestEvictionListener();
86-
boolean acquired = cacheFile.acquire(listener);
87-
assertTrue("Cache file is acquired", acquired);
82+
cacheFile.acquire(listener);
8883

8984
assertThat(cacheFile.getChannel(), notNullValue());
9085
assertThat(Files.exists(file), is(true));
9186

92-
boolean released = cacheFile.release(listener);
93-
assertTrue("Cache file is released", released);
87+
cacheFile.release(listener);
9488
}
9589

9690
cacheFile.startEviction();
@@ -105,7 +99,7 @@ public void testDeleteOnCloseAfterLastRelease() throws Exception {
10599
final List<TestEvictionListener> acquiredListeners = new ArrayList<>();
106100
for (int i = 0; i < randomIntBetween(1, 20); i++) {
107101
TestEvictionListener listener = new TestEvictionListener();
108-
assertTrue(cacheFile.acquire(listener));
102+
cacheFile.acquire(listener);
109103
assertThat(cacheFile.getChannel(), notNullValue());
110104
acquiredListeners.add(listener);
111105
}
@@ -135,7 +129,7 @@ public void testConcurrentAccess() throws Exception {
135129
final CacheFile cacheFile = new CacheFile("test", randomLongBetween(1, 100), file);
136130

137131
final TestEvictionListener evictionListener = new TestEvictionListener();
138-
assertTrue(cacheFile.acquire(evictionListener));
132+
cacheFile.acquire(evictionListener);
139133
final long length = cacheFile.getLength();
140134
final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(
141135
builder().put(NODE_NAME_SETTING.getKey(), getTestName()).build(),

0 commit comments

Comments
 (0)