Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 9cfa395

Browse files
authored
Remove the IndexCommitRef class (opensearch-project#2421)
This inner class is no longer required because its functionality has been moved to the generic GatedCloseable class. Signed-off-by: Kartik Ganesh <[email protected]>
1 parent c8d8009 commit 9cfa395

File tree

15 files changed

+135
-124
lines changed

15 files changed

+135
-124
lines changed

server/src/internalClusterTest/java/org/opensearch/action/admin/indices/forcemerge/ForceMergeIT.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232

3333
package org.opensearch.action.admin.indices.forcemerge;
3434

35+
import org.apache.lucene.index.IndexCommit;
3536
import org.opensearch.action.admin.indices.flush.FlushResponse;
3637
import org.opensearch.cluster.ClusterState;
3738
import org.opensearch.cluster.metadata.IndexMetadata;
3839
import org.opensearch.cluster.routing.IndexRoutingTable;
3940
import org.opensearch.cluster.routing.IndexShardRoutingTable;
41+
import org.opensearch.common.concurrent.GatedCloseable;
4042
import org.opensearch.common.settings.Settings;
4143
import org.opensearch.index.Index;
4244
import org.opensearch.index.engine.Engine;
@@ -99,8 +101,8 @@ public void testForceMergeUUIDConsistent() throws IOException {
99101
}
100102

101103
private static String getForceMergeUUID(IndexShard indexShard) throws IOException {
102-
try (Engine.IndexCommitRef indexCommitRef = indexShard.acquireLastIndexCommit(true)) {
103-
return indexCommitRef.get().getUserData().get(Engine.FORCE_MERGE_UUID_KEY);
104+
try (GatedCloseable<IndexCommit> wrappedIndexCommit = indexShard.acquireLastIndexCommit(true)) {
105+
return wrappedIndexCommit.get().getUserData().get(Engine.FORCE_MERGE_UUID_KEY);
104106
}
105107
}
106108
}

server/src/internalClusterTest/java/org/opensearch/indices/recovery/IndexRecoveryIT.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
package org.opensearch.indices.recovery;
3434

3535
import org.apache.lucene.analysis.TokenStream;
36+
import org.apache.lucene.index.IndexCommit;
3637
import org.apache.lucene.util.SetOnce;
37-
3838
import org.opensearch.OpenSearchException;
3939
import org.opensearch.Version;
4040
import org.opensearch.action.admin.cluster.health.ClusterHealthResponse;
@@ -75,6 +75,7 @@
7575
import org.opensearch.common.Strings;
7676
import org.opensearch.common.breaker.CircuitBreaker;
7777
import org.opensearch.common.breaker.CircuitBreakingException;
78+
import org.opensearch.common.concurrent.GatedCloseable;
7879
import org.opensearch.common.settings.Settings;
7980
import org.opensearch.common.unit.ByteSizeUnit;
8081
import org.opensearch.common.unit.ByteSizeValue;
@@ -88,7 +89,6 @@
8889
import org.opensearch.index.MockEngineFactoryPlugin;
8990
import org.opensearch.index.analysis.AbstractTokenFilterFactory;
9091
import org.opensearch.index.analysis.TokenFilterFactory;
91-
import org.opensearch.index.engine.Engine;
9292
import org.opensearch.index.mapper.MapperParsingException;
9393
import org.opensearch.index.recovery.RecoveryStats;
9494
import org.opensearch.index.seqno.ReplicationTracker;
@@ -114,11 +114,11 @@
114114
import org.opensearch.snapshots.SnapshotState;
115115
import org.opensearch.tasks.Task;
116116
import org.opensearch.test.BackgroundIndexer;
117+
import org.opensearch.test.InternalSettingsPlugin;
118+
import org.opensearch.test.InternalTestCluster;
117119
import org.opensearch.test.OpenSearchIntegTestCase;
118120
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
119121
import org.opensearch.test.OpenSearchIntegTestCase.Scope;
120-
import org.opensearch.test.InternalSettingsPlugin;
121-
import org.opensearch.test.InternalTestCluster;
122122
import org.opensearch.test.engine.MockEngineSupport;
123123
import org.opensearch.test.store.MockFSIndexStore;
124124
import org.opensearch.test.transport.MockTransportService;
@@ -151,12 +151,6 @@
151151

152152
import static java.util.Collections.singletonMap;
153153
import static java.util.stream.Collectors.toList;
154-
import static org.opensearch.action.DocWriteResponse.Result.CREATED;
155-
import static org.opensearch.action.DocWriteResponse.Result.UPDATED;
156-
import static org.opensearch.node.RecoverySettingsChunkSizePlugin.CHUNK_SIZE_SETTING;
157-
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
158-
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
159-
160154
import static org.hamcrest.Matchers.empty;
161155
import static org.hamcrest.Matchers.equalTo;
162156
import static org.hamcrest.Matchers.everyItem;
@@ -167,6 +161,11 @@
167161
import static org.hamcrest.Matchers.isOneOf;
168162
import static org.hamcrest.Matchers.lessThanOrEqualTo;
169163
import static org.hamcrest.Matchers.not;
164+
import static org.opensearch.action.DocWriteResponse.Result.CREATED;
165+
import static org.opensearch.action.DocWriteResponse.Result.UPDATED;
166+
import static org.opensearch.node.RecoverySettingsChunkSizePlugin.CHUNK_SIZE_SETTING;
167+
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
168+
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
170169

171170
@ClusterScope(scope = Scope.TEST, numDataNodes = 0)
172171
public class IndexRecoveryIT extends OpenSearchIntegTestCase {
@@ -1599,9 +1598,9 @@ public void testRecoverLocallyUpToGlobalCheckpoint() throws Exception {
15991598
.getShardOrNull(new ShardId(resolveIndex(indexName), 0));
16001599
final long lastSyncedGlobalCheckpoint = shard.getLastSyncedGlobalCheckpoint();
16011600
final long localCheckpointOfSafeCommit;
1602-
try (Engine.IndexCommitRef safeCommitRef = shard.acquireSafeIndexCommit()) {
1601+
try (GatedCloseable<IndexCommit> wrappedSafeCommit = shard.acquireSafeIndexCommit()) {
16031602
localCheckpointOfSafeCommit = SequenceNumbers.loadSeqNoInfoFromLuceneCommit(
1604-
safeCommitRef.get().getUserData().entrySet()
1603+
wrappedSafeCommit.get().getUserData().entrySet()
16051604
).localCheckpoint;
16061605
}
16071606
final long maxSeqNo = shard.seqNoStats().getMaxSeqNo();

server/src/main/java/org/opensearch/index/engine/Engine.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.apache.lucene.util.SetOnce;
5656
import org.opensearch.ExceptionsHelper;
5757
import org.opensearch.action.index.IndexRequest;
58-
import org.opensearch.common.CheckedRunnable;
5958
import org.opensearch.common.Nullable;
6059
import org.opensearch.common.bytes.BytesReference;
6160
import org.opensearch.common.collect.ImmutableOpenMap;
@@ -1109,12 +1108,12 @@ public abstract void forceMerge(
11091108
*
11101109
* @param flushFirst indicates whether the engine should flush before returning the snapshot
11111110
*/
1112-
public abstract IndexCommitRef acquireLastIndexCommit(boolean flushFirst) throws EngineException;
1111+
public abstract GatedCloseable<IndexCommit> acquireLastIndexCommit(boolean flushFirst) throws EngineException;
11131112

11141113
/**
11151114
* Snapshots the most recent safe index commit from the engine.
11161115
*/
1117-
public abstract IndexCommitRef acquireSafeIndexCommit() throws EngineException;
1116+
public abstract GatedCloseable<IndexCommit> acquireSafeIndexCommit() throws EngineException;
11181117

11191118
/**
11201119
* @return a summary of the contents of the current safe commit
@@ -1829,12 +1828,6 @@ private void awaitPendingClose() {
18291828
}
18301829
}
18311830

1832-
public static class IndexCommitRef extends GatedCloseable<IndexCommit> {
1833-
public IndexCommitRef(IndexCommit indexCommit, CheckedRunnable<IOException> onClose) {
1834-
super(indexCommit, onClose);
1835-
}
1836-
}
1837-
18381831
public void onSettingsChanged(TimeValue translogRetentionAge, ByteSizeValue translogRetentionSize, long softDeletesRetentionOps) {
18391832

18401833
}

server/src/main/java/org/opensearch/index/engine/InternalEngine.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import org.opensearch.common.Booleans;
7373
import org.opensearch.common.Nullable;
7474
import org.opensearch.common.SuppressForbidden;
75+
import org.opensearch.common.concurrent.GatedCloseable;
7576
import org.opensearch.common.lease.Releasable;
7677
import org.opensearch.common.lucene.LoggerInfoStream;
7778
import org.opensearch.common.lucene.Lucene;
@@ -103,10 +104,10 @@
103104
import org.opensearch.index.seqno.SequenceNumbers;
104105
import org.opensearch.index.shard.OpenSearchMergePolicy;
105106
import org.opensearch.index.shard.ShardId;
107+
import org.opensearch.index.translog.DefaultTranslogDeletionPolicy;
106108
import org.opensearch.index.translog.Translog;
107109
import org.opensearch.index.translog.TranslogConfig;
108110
import org.opensearch.index.translog.TranslogCorruptedException;
109-
import org.opensearch.index.translog.DefaultTranslogDeletionPolicy;
110111
import org.opensearch.index.translog.TranslogDeletionPolicy;
111112
import org.opensearch.index.translog.TranslogStats;
112113
import org.opensearch.search.suggest.completion.CompletionStats;
@@ -2193,7 +2194,7 @@ public void forceMerge(
21932194
}
21942195

21952196
@Override
2196-
public IndexCommitRef acquireLastIndexCommit(final boolean flushFirst) throws EngineException {
2197+
public GatedCloseable<IndexCommit> acquireLastIndexCommit(final boolean flushFirst) throws EngineException {
21972198
// we have to flush outside of the readlock otherwise we might have a problem upgrading
21982199
// the to a write lock when we fail the engine in this operation
21992200
if (flushFirst) {
@@ -2202,13 +2203,13 @@ public IndexCommitRef acquireLastIndexCommit(final boolean flushFirst) throws En
22022203
logger.trace("finish flush for snapshot");
22032204
}
22042205
final IndexCommit lastCommit = combinedDeletionPolicy.acquireIndexCommit(false);
2205-
return new Engine.IndexCommitRef(lastCommit, () -> releaseIndexCommit(lastCommit));
2206+
return new GatedCloseable<>(lastCommit, () -> releaseIndexCommit(lastCommit));
22062207
}
22072208

22082209
@Override
2209-
public IndexCommitRef acquireSafeIndexCommit() throws EngineException {
2210+
public GatedCloseable<IndexCommit> acquireSafeIndexCommit() throws EngineException {
22102211
final IndexCommit safeCommit = combinedDeletionPolicy.acquireIndexCommit(true);
2211-
return new Engine.IndexCommitRef(safeCommit, () -> releaseIndexCommit(safeCommit));
2212+
return new GatedCloseable<>(safeCommit, () -> releaseIndexCommit(safeCommit));
22122213
}
22132214

22142215
private void releaseIndexCommit(IndexCommit snapshot) throws IOException {

server/src/main/java/org/opensearch/index/engine/ReadOnlyEngine.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.lucene.store.Lock;
4242
import org.opensearch.LegacyESVersion;
4343
import org.opensearch.Version;
44+
import org.opensearch.common.concurrent.GatedCloseable;
4445
import org.opensearch.common.lucene.Lucene;
4546
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
4647
import org.opensearch.common.util.concurrent.ReleasableLock;
@@ -49,9 +50,9 @@
4950
import org.opensearch.index.seqno.SeqNoStats;
5051
import org.opensearch.index.seqno.SequenceNumbers;
5152
import org.opensearch.index.store.Store;
53+
import org.opensearch.index.translog.DefaultTranslogDeletionPolicy;
5254
import org.opensearch.index.translog.Translog;
5355
import org.opensearch.index.translog.TranslogConfig;
54-
import org.opensearch.index.translog.DefaultTranslogDeletionPolicy;
5556
import org.opensearch.index.translog.TranslogDeletionPolicy;
5657
import org.opensearch.index.translog.TranslogStats;
5758
import org.opensearch.search.suggest.completion.CompletionStats;
@@ -413,13 +414,13 @@ public void forceMerge(
413414
) {}
414415

415416
@Override
416-
public IndexCommitRef acquireLastIndexCommit(boolean flushFirst) {
417+
public GatedCloseable<IndexCommit> acquireLastIndexCommit(boolean flushFirst) {
417418
store.incRef();
418-
return new IndexCommitRef(indexCommit, store::decRef);
419+
return new GatedCloseable<>(indexCommit, store::decRef);
419420
}
420421

421422
@Override
422-
public IndexCommitRef acquireSafeIndexCommit() {
423+
public GatedCloseable<IndexCommit> acquireSafeIndexCommit() {
423424
return acquireLastIndexCommit(false);
424425
}
425426

server/src/main/java/org/opensearch/index/shard/IndexShard.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
import org.apache.lucene.util.SetOnce;
5252
import org.apache.lucene.util.ThreadInterruptedException;
5353
import org.opensearch.Assertions;
54+
import org.opensearch.ExceptionsHelper;
5455
import org.opensearch.LegacyESVersion;
5556
import org.opensearch.OpenSearchException;
56-
import org.opensearch.ExceptionsHelper;
5757
import org.opensearch.action.ActionListener;
5858
import org.opensearch.action.ActionRunnable;
5959
import org.opensearch.action.admin.indices.flush.FlushRequest;
@@ -73,6 +73,7 @@
7373
import org.opensearch.common.CheckedRunnable;
7474
import org.opensearch.common.Nullable;
7575
import org.opensearch.common.collect.Tuple;
76+
import org.opensearch.common.concurrent.GatedCloseable;
7677
import org.opensearch.common.io.stream.BytesStreamOutput;
7778
import org.opensearch.common.lease.Releasable;
7879
import org.opensearch.common.lease.Releasables;
@@ -1409,7 +1410,7 @@ public org.apache.lucene.util.Version minimumCompatibleVersion() {
14091410
*
14101411
* @param flushFirst <code>true</code> if the index should first be flushed to disk / a low level lucene commit should be executed
14111412
*/
1412-
public Engine.IndexCommitRef acquireLastIndexCommit(boolean flushFirst) throws EngineException {
1413+
public GatedCloseable<IndexCommit> acquireLastIndexCommit(boolean flushFirst) throws EngineException {
14131414
final IndexShardState state = this.state; // one time volatile read
14141415
// we allow snapshot on closed index shard, since we want to do one after we close the shard and before we close the engine
14151416
if (state == IndexShardState.STARTED || state == IndexShardState.CLOSED) {
@@ -1423,7 +1424,7 @@ public Engine.IndexCommitRef acquireLastIndexCommit(boolean flushFirst) throws E
14231424
* Snapshots the most recent safe index commit from the currently running engine.
14241425
* All index files referenced by this index commit won't be freed until the commit/snapshot is closed.
14251426
*/
1426-
public Engine.IndexCommitRef acquireSafeIndexCommit() throws EngineException {
1427+
public GatedCloseable<IndexCommit> acquireSafeIndexCommit() throws EngineException {
14271428
final IndexShardState state = this.state; // one time volatile read
14281429
// we allow snapshot on closed index shard, since we want to do one after we close the shard and before we close the engine
14291430
if (state == IndexShardState.STARTED || state == IndexShardState.CLOSED) {
@@ -1448,24 +1449,24 @@ public Engine.IndexCommitRef acquireSafeIndexCommit() throws EngineException {
14481449
*/
14491450
public Store.MetadataSnapshot snapshotStoreMetadata() throws IOException {
14501451
assert Thread.holdsLock(mutex) == false : "snapshotting store metadata under mutex";
1451-
Engine.IndexCommitRef indexCommit = null;
1452+
GatedCloseable<IndexCommit> wrappedIndexCommit = null;
14521453
store.incRef();
14531454
try {
14541455
synchronized (engineMutex) {
14551456
// if the engine is not running, we can access the store directly, but we need to make sure no one starts
14561457
// the engine on us. If the engine is running, we can get a snapshot via the deletion policy of the engine.
14571458
final Engine engine = getEngineOrNull();
14581459
if (engine != null) {
1459-
indexCommit = engine.acquireLastIndexCommit(false);
1460+
wrappedIndexCommit = engine.acquireLastIndexCommit(false);
14601461
}
1461-
if (indexCommit == null) {
1462+
if (wrappedIndexCommit == null) {
14621463
return store.getMetadata(null, true);
14631464
}
14641465
}
1465-
return store.getMetadata(indexCommit.get());
1466+
return store.getMetadata(wrappedIndexCommit.get());
14661467
} finally {
14671468
store.decRef();
1468-
IOUtils.close(indexCommit);
1469+
IOUtils.close(wrappedIndexCommit);
14691470
}
14701471
}
14711472

@@ -3913,7 +3914,7 @@ void resetEngineToGlobalCheckpoint() throws IOException {
39133914
true
39143915
) {
39153916
@Override
3916-
public IndexCommitRef acquireLastIndexCommit(boolean flushFirst) {
3917+
public GatedCloseable<IndexCommit> acquireLastIndexCommit(boolean flushFirst) {
39173918
synchronized (engineMutex) {
39183919
if (newEngineReference.get() == null) {
39193920
throw new AlreadyClosedException("engine was closed");
@@ -3924,7 +3925,7 @@ public IndexCommitRef acquireLastIndexCommit(boolean flushFirst) {
39243925
}
39253926

39263927
@Override
3927-
public IndexCommitRef acquireSafeIndexCommit() {
3928+
public GatedCloseable<IndexCommit> acquireSafeIndexCommit() {
39283929
synchronized (engineMutex) {
39293930
if (newEngineReference.get() == null) {
39303931
throw new AlreadyClosedException("engine was closed");

server/src/main/java/org/opensearch/index/shard/LocalShardSnapshot.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232

3333
package org.opensearch.index.shard;
3434

35+
import org.apache.lucene.index.IndexCommit;
3536
import org.apache.lucene.store.Directory;
3637
import org.apache.lucene.store.FilterDirectory;
3738
import org.apache.lucene.store.IOContext;
3839
import org.apache.lucene.store.IndexOutput;
3940
import org.apache.lucene.store.Lock;
4041
import org.apache.lucene.store.NoLockFactory;
4142
import org.opensearch.cluster.metadata.IndexMetadata;
43+
import org.opensearch.common.concurrent.GatedCloseable;
4244
import org.opensearch.index.Index;
4345
import org.opensearch.index.engine.Engine;
4446
import org.opensearch.index.store.Store;
@@ -52,7 +54,7 @@
5254
final class LocalShardSnapshot implements Closeable {
5355
private final IndexShard shard;
5456
private final Store store;
55-
private final Engine.IndexCommitRef indexCommit;
57+
private final GatedCloseable<IndexCommit> wrappedIndexCommit;
5658
private final AtomicBoolean closed = new AtomicBoolean(false);
5759

5860
LocalShardSnapshot(IndexShard shard) {
@@ -61,7 +63,7 @@ final class LocalShardSnapshot implements Closeable {
6163
store.incRef();
6264
boolean success = false;
6365
try {
64-
indexCommit = shard.acquireLastIndexCommit(true);
66+
wrappedIndexCommit = shard.acquireLastIndexCommit(true);
6567
success = true;
6668
} finally {
6769
if (success == false) {
@@ -88,7 +90,7 @@ Directory getSnapshotDirectory() {
8890
return new FilterDirectory(store.directory()) {
8991
@Override
9092
public String[] listAll() throws IOException {
91-
Collection<String> fileNames = indexCommit.get().getFileNames();
93+
Collection<String> fileNames = wrappedIndexCommit.get().getFileNames();
9294
final String[] fileNameArray = fileNames.toArray(new String[fileNames.size()]);
9395
return fileNameArray;
9496
}
@@ -143,7 +145,7 @@ public Set<String> getPendingDeletions() throws IOException {
143145
public void close() throws IOException {
144146
if (closed.compareAndSet(false, true)) {
145147
try {
146-
indexCommit.close();
148+
wrappedIndexCommit.close();
147149
} finally {
148150
store.decRef();
149151
}
@@ -156,6 +158,6 @@ IndexMetadata getIndexMetadata() {
156158

157159
@Override
158160
public String toString() {
159-
return "local_shard_snapshot:[" + shard.shardId() + " indexCommit: " + indexCommit + "]";
161+
return "local_shard_snapshot:[" + shard.shardId() + " indexCommit: " + wrappedIndexCommit + "]";
160162
}
161163
}

0 commit comments

Comments
 (0)