Skip to content

Commit 8f105a0

Browse files
committed
Use _refresh to shrink the version map on inactivity (#27918)
We used to shrink the version map under an external lock. This is quite ambigious and instead we can simply issue an empty refresh to shrink it. Closes #27852
1 parent ef23eea commit 8f105a0

File tree

3 files changed

+3
-41
lines changed

3 files changed

+3
-41
lines changed

core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,9 @@ public SyncedFlushResult syncFlush(String syncId, CommitId expectedCommitId) thr
13811381
try (ReleasableLock lock = writeLock.acquire()) {
13821382
ensureOpen();
13831383
ensureCanFlush();
1384+
// lets do a refresh to make sure we shrink the version map. This refresh will be either a no-op (just shrink the version map)
1385+
// or we also have uncommitted changes and that causes this syncFlush to fail.
1386+
refresh("sync_flush", SearcherScope.INTERNAL);
13841387
if (indexWriter.hasUncommittedChanges()) {
13851388
logger.trace("can't sync commit [{}]. have pending changes", syncId);
13861389
return SyncedFlushResult.PENDING_OPERATIONS;
@@ -1393,8 +1396,6 @@ public SyncedFlushResult syncFlush(String syncId, CommitId expectedCommitId) thr
13931396
commitIndexWriter(indexWriter, translog, syncId);
13941397
logger.debug("successfully sync committed. sync id [{}].", syncId);
13951398
lastCommittedSegmentInfos = store.readLastCommittedSegmentsInfo();
1396-
// we are guaranteed to have no operations in the version map here!
1397-
versionMap.adjustMapSizeUnderLock();
13981399
return SyncedFlushResult.SUCCESS;
13991400
} catch (IOException ex) {
14001401
maybeFailEngine("sync commit", ex);

core/src/main/java/org/elasticsearch/index/engine/LiveVersionMap.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@
3434
/** Maps _uid value to its version information. */
3535
class LiveVersionMap implements ReferenceManager.RefreshListener, Accountable {
3636

37-
/**
38-
* Resets the internal map and adjusts it's capacity as if there were no indexing operations.
39-
* This must be called under write lock in the engine
40-
*/
41-
void adjustMapSizeUnderLock() {
42-
if (maps.current.isEmpty() == false || maps.old.isEmpty() == false) {
43-
assert false : "map must be empty"; // fail hard if not empty and fail with assertion in tests to ensure we never swallow it
44-
throw new IllegalStateException("map must be empty");
45-
}
46-
maps = new Maps();
47-
}
48-
4937
private static class Maps {
5038

5139
// All writes (adds and deletes) go into here:

core/src/test/java/org/elasticsearch/index/engine/LiveVersionMapTests.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.lucene.util.BytesRefBuilder;
2424
import org.apache.lucene.util.RamUsageTester;
2525
import org.apache.lucene.util.TestUtil;
26-
import org.elasticsearch.Assertions;
2726
import org.elasticsearch.bootstrap.JavaVersion;
2827
import org.elasticsearch.common.lease.Releasable;
2928
import org.elasticsearch.common.util.concurrent.KeyedLock;
@@ -97,32 +96,6 @@ public void testBasics() throws IOException {
9796
assertNull(map.getUnderLock(uid("test")));
9897
}
9998

100-
101-
public void testAdjustMapSizeUnderLock() throws IOException {
102-
LiveVersionMap map = new LiveVersionMap();
103-
map.putUnderLock(uid("test"), new VersionValue(1,1,1));
104-
boolean withinRefresh = randomBoolean();
105-
if (withinRefresh) {
106-
map.beforeRefresh();
107-
}
108-
assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test")));
109-
final String msg;
110-
if (Assertions.ENABLED) {
111-
msg = expectThrows(AssertionError.class, map::adjustMapSizeUnderLock).getMessage();
112-
} else {
113-
msg = expectThrows(IllegalStateException.class, map::adjustMapSizeUnderLock).getMessage();
114-
}
115-
assertEquals("map must be empty", msg);
116-
assertEquals(new VersionValue(1,1,1), map.getUnderLock(uid("test")));
117-
if (withinRefresh == false) {
118-
map.beforeRefresh();
119-
}
120-
map.afterRefresh(randomBoolean());
121-
Map<BytesRef, VersionValue> allCurrent = map.getAllCurrent();
122-
map.adjustMapSizeUnderLock();
123-
assertNotSame(allCurrent, map.getAllCurrent());
124-
}
125-
12699
public void testConcurrently() throws IOException, InterruptedException {
127100
HashSet<BytesRef> keySet = new HashSet<>();
128101
int numKeys = randomIntBetween(50, 200);

0 commit comments

Comments
 (0)