From f14edefc9d6a503d8ad9695dea489685e4e44b38 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Tue, 1 Jul 2014 08:54:31 -0400 Subject: [PATCH] [TEST] Fix possible race condition in checksum name generator When three threads are trying to write checksums at the same time, it's possible for all three threads to obtain the same checksum file name A. Then the first thread enters the synchronized section, creates the file with name A and exits. The second thread enters the synchronized section, checks that A exists, creates file A+1 and exits the critical section. Then it proceeds to clean up and deletes all checksum files including A. If it happens before the third thread enters the synchronized section, it's possible for the third thread to check for A and since it no longer exists create the checksum file A the second time, which triggers "file _checksums-XXXXXXXXXXXXX was already written to" exception in MockDirectoryWrapper and fails recovery. --- src/main/java/org/elasticsearch/index/store/Store.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/index/store/Store.java b/src/main/java/org/elasticsearch/index/store/Store.java index 797cb86ce4f..9b476392b1f 100644 --- a/src/main/java/org/elasticsearch/index/store/Store.java +++ b/src/main/java/org/elasticsearch/index/store/Store.java @@ -224,8 +224,9 @@ public class Store extends AbstractIndexShardComponent implements CloseableIndex public void writeChecksums() throws IOException { ensureOpen(); ImmutableMap files = list(); - String checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis(); + String checksumName; synchronized (mutex) { + checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis(); Map checksums = new HashMap<>(); for (StoreFileMetaData metaData : files.values()) { if (metaData.checksum() != null) {