Skip to content

Commit 5b229c3

Browse files
authored
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 c4fae37 commit 5b229c3

File tree

3 files changed

+3
-46
lines changed

3 files changed

+3
-46
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
@@ -1342,6 +1342,9 @@ public SyncedFlushResult syncFlush(String syncId, CommitId expectedCommitId) thr
13421342
try (ReleasableLock lock = writeLock.acquire()) {
13431343
ensureOpen();
13441344
ensureCanFlush();
1345+
// 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)
1346+
// or we also have uncommitted changes and that causes this syncFlush to fail.
1347+
refresh("sync_flush", SearcherScope.INTERNAL);
13451348
if (indexWriter.hasUncommittedChanges()) {
13461349
logger.trace("can't sync commit [{}]. have pending changes", syncId);
13471350
return SyncedFlushResult.PENDING_OPERATIONS;
@@ -1354,8 +1357,6 @@ public SyncedFlushResult syncFlush(String syncId, CommitId expectedCommitId) thr
13541357
commitIndexWriter(indexWriter, translog, syncId);
13551358
logger.debug("successfully sync committed. sync id [{}].", syncId);
13561359
lastCommittedSegmentInfos = store.readLastCommittedSegmentsInfo();
1357-
// we are guaranteed to have no operations in the version map here!
1358-
versionMap.adjustMapSizeUnderLock();
13591360
return SyncedFlushResult.SUCCESS;
13601361
} catch (IOException ex) {
13611362
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
@@ -38,18 +38,6 @@ final class LiveVersionMap implements ReferenceManager.RefreshListener, Accounta
3838

3939
private final KeyedLock<BytesRef> keyedLock = new KeyedLock<>();
4040

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

5543
private static final VersionLookup EMPTY = new VersionLookup(Collections.emptyMap());

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,38 +101,6 @@ public void testBasics() throws IOException {
101101
}
102102
}
103103

104-
105-
public void testAdjustMapSizeUnderLock() throws IOException {
106-
LiveVersionMap map = new LiveVersionMap();
107-
try (Releasable r = map.acquireLock(uid("test"))) {
108-
map.putUnderLock(uid("test"), new VersionValue(1, 1, 1));
109-
}
110-
boolean withinRefresh = randomBoolean();
111-
if (withinRefresh) {
112-
map.beforeRefresh();
113-
}
114-
try (Releasable r = map.acquireLock(uid("test"))) {
115-
assertEquals(new VersionValue(1, 1, 1), map.getUnderLock(uid("test")));
116-
}
117-
final String msg;
118-
if (Assertions.ENABLED) {
119-
msg = expectThrows(AssertionError.class, map::adjustMapSizeUnderLock).getMessage();
120-
} else {
121-
msg = expectThrows(IllegalStateException.class, map::adjustMapSizeUnderLock).getMessage();
122-
}
123-
assertEquals("map must be empty", msg);
124-
try (Releasable r = map.acquireLock(uid("test"))) {
125-
assertEquals(new VersionValue(1, 1, 1), map.getUnderLock(uid("test")));
126-
}
127-
if (withinRefresh == false) {
128-
map.beforeRefresh();
129-
}
130-
map.afterRefresh(randomBoolean());
131-
Map<BytesRef, VersionValue> allCurrent = map.getAllCurrent();
132-
map.adjustMapSizeUnderLock();
133-
assertNotSame(allCurrent, map.getAllCurrent());
134-
}
135-
136104
public void testConcurrently() throws IOException, InterruptedException {
137105
HashSet<BytesRef> keySet = new HashSet<>();
138106
int numKeys = randomIntBetween(50, 200);

0 commit comments

Comments
 (0)