Skip to content

Commit f231042

Browse files
committed
lock directory when checkIndex
1 parent 1a8bdd5 commit f231042

File tree

2 files changed

+3
-49
lines changed

2 files changed

+3
-49
lines changed

core/src/main/java/org/elasticsearch/index/store/Store.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,24 +352,12 @@ public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
352352
* @param out where infoStream messages should go. See {@link CheckIndex#setInfoStream(PrintStream)}
353353
*/
354354
public CheckIndex.Status checkIndex(PrintStream out) throws IOException {
355-
// We don't need to lock the directory here as we are not changing the index files.
356-
final Lock noDirectoryLock = new Lock() {
357-
@Override
358-
public void close() throws IOException {
359-
360-
}
361-
362-
@Override
363-
public void ensureValid() throws IOException {
364-
365-
}
366-
};
367-
metadataLock.readLock().lock();
368-
try (CheckIndex checkIndex = new CheckIndex(directory, noDirectoryLock)) {
355+
metadataLock.writeLock().lock();
356+
try (CheckIndex checkIndex = new CheckIndex(directory)) {
369357
checkIndex.setInfoStream(out);
370358
return checkIndex.checkIndex();
371359
} finally {
372-
metadataLock.readLock().unlock();
360+
metadataLock.writeLock().unlock();
373361
}
374362
}
375363

core/src/test/java/org/elasticsearch/index/store/StoreTests.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.apache.lucene.document.SortedDocValuesField;
2626
import org.apache.lucene.document.StringField;
2727
import org.apache.lucene.document.TextField;
28-
import org.apache.lucene.index.CheckIndex;
2928
import org.apache.lucene.index.CorruptIndexException;
3029
import org.apache.lucene.index.DirectoryReader;
3130
import org.apache.lucene.index.IndexFileNames;
@@ -47,8 +46,6 @@
4746
import org.apache.lucene.store.IOContext;
4847
import org.apache.lucene.store.IndexInput;
4948
import org.apache.lucene.store.IndexOutput;
50-
import org.apache.lucene.store.Lock;
51-
import org.apache.lucene.store.LockObtainFailedException;
5249
import org.apache.lucene.store.RAMDirectory;
5350
import org.apache.lucene.util.BytesRef;
5451
import org.apache.lucene.util.IOUtils;
@@ -57,7 +54,6 @@
5754
import org.elasticsearch.ExceptionsHelper;
5855
import org.elasticsearch.cluster.metadata.IndexMetaData;
5956
import org.elasticsearch.common.UUIDs;
60-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
6157
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
6258
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
6359
import org.elasticsearch.common.lucene.Lucene;
@@ -79,8 +75,6 @@
7975
import java.io.ByteArrayOutputStream;
8076
import java.io.FileNotFoundException;
8177
import java.io.IOException;
82-
import java.io.PrintStream;
83-
import java.nio.charset.StandardCharsets;
8478
import java.nio.file.NoSuchFileException;
8579
import java.nio.file.Path;
8680
import java.util.ArrayList;
@@ -1077,32 +1071,4 @@ public Directory newDirectory() throws IOException {
10771071
store.close();
10781072
}
10791073

1080-
public void testCheckIndex() throws Exception {
1081-
final ShardId shardId = new ShardId("index", "_na_", 1);
1082-
final Store store = new Store(shardId, INDEX_SETTINGS, new LuceneManagedDirectoryService(random()), new DummyShardLock(shardId));
1083-
try (IndexWriter writer = new IndexWriter(store.directory(), newIndexWriterConfig())) {
1084-
writer.commit();
1085-
}
1086-
// Check index does not need to lock the store directory
1087-
try (Lock directoryLock = store.directory().obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
1088-
BytesStreamOutput os = new BytesStreamOutput();
1089-
PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name());
1090-
assertThat(store.checkIndex(out).clean, equalTo(true));
1091-
}
1092-
// exorciseIndex requires directory lock
1093-
try (Lock directoryLock = store.directory().obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
1094-
BytesStreamOutput os = new BytesStreamOutput();
1095-
PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name());
1096-
assertThat(store.checkIndex(out).clean, equalTo(true));
1097-
final CheckIndex.Status status = store.checkIndex(out);
1098-
try {
1099-
store.exorciseIndex(status);
1100-
fail("exorciseIndex should acquire directory lock");
1101-
} catch (LockObtainFailedException ignore) {
1102-
1103-
}
1104-
}
1105-
store.close();
1106-
}
1107-
11081074
}

0 commit comments

Comments
 (0)