Skip to content

Commit a16cb89

Browse files
committed
Revert "Sync translog without lock when trim unreferenced readers (#46203)"
Unfortunately, with this change, we won't clean up all unreferenced generations when reopening. We assume that there's at most one unreferenced generation when reopening translog. The previous implementation guarantees this assumption by syncing translog every time after we remove a translog reader. This change, however, only syncs translog once after we have removed all unreferenced readers (can be more than one) and breaks the assumption. Closes #46267 This reverts commit fd8183e.
1 parent d5ad86d commit a16cb89

File tree

1 file changed

+9
-20
lines changed
  • server/src/main/java/org/elasticsearch/index/translog

1 file changed

+9
-20
lines changed

server/src/main/java/org/elasticsearch/index/translog/Translog.java

+9-20
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,6 @@ public void rollGeneration() throws IOException {
16781678
* required generation
16791679
*/
16801680
public void trimUnreferencedReaders() throws IOException {
1681-
List<TranslogReader> toDeleteReaderList = new ArrayList<>();
16821681
try (ReleasableLock ignored = writeLock.acquire()) {
16831682
if (closed.get()) {
16841683
// we're shutdown potentially on some tragic event, don't delete anything
@@ -1692,14 +1691,22 @@ public void trimUnreferencedReaders() throws IOException {
16921691
"deletion policy requires a minReferenceGen of [" + minReferencedGen + "] which is higher than the current generation ["
16931692
+ currentFileGeneration() + "]";
16941693

1694+
16951695
for (Iterator<TranslogReader> iterator = readers.iterator(); iterator.hasNext(); ) {
16961696
TranslogReader reader = iterator.next();
16971697
if (reader.getGeneration() >= minReferencedGen) {
16981698
break;
16991699
}
17001700
iterator.remove();
1701-
toDeleteReaderList.add(reader);
17021701
IOUtils.closeWhileHandlingException(reader);
1702+
final Path translogPath = reader.path();
1703+
logger.trace("delete translog file [{}], not referenced and not current anymore", translogPath);
1704+
// The checkpoint is used when opening the translog to know which files should be recovered from.
1705+
// We now update the checkpoint to ignore the file we are going to remove.
1706+
// Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint
1707+
// but crashed before we could delete the file.
1708+
current.sync();
1709+
deleteReaderFiles(reader);
17031710
}
17041711
assert readers.isEmpty() == false || current.generation == minReferencedGen :
17051712
"all readers were cleaned but the minReferenceGen [" + minReferencedGen + "] is not the current writer's gen [" +
@@ -1708,24 +1715,6 @@ public void trimUnreferencedReaders() throws IOException {
17081715
closeOnTragicEvent(ex);
17091716
throw ex;
17101717
}
1711-
// Do sync outside the writeLock to avoid blocking all writing thread.
1712-
if (toDeleteReaderList.isEmpty() == false) {
1713-
try {
1714-
// The checkpoint is used when opening the translog to know which files should be recovered from.
1715-
// We now update the checkpoint to ignore the file we are going to remove.
1716-
// Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint
1717-
// but crashed before we could delete the file.
1718-
sync();
1719-
for (TranslogReader reader : toDeleteReaderList) {
1720-
final Path translogPath = reader.path();
1721-
logger.trace("delete translog file [{}], not referenced and not current anymore", translogPath);
1722-
deleteReaderFiles(reader);
1723-
}
1724-
} catch (final Exception ex) {
1725-
closeOnTragicEvent(ex);
1726-
throw ex;
1727-
}
1728-
}
17291718
}
17301719

17311720
/**

0 commit comments

Comments
 (0)