Skip to content

Commit b0ac9aa

Browse files
author
Andrey Ershov
committed
WriteStateException extends IOException
1 parent 0cfffd5 commit b0ac9aa

File tree

8 files changed

+21
-60
lines changed

8 files changed

+21
-60
lines changed

server/src/main/java/org/elasticsearch/env/NodeEnvironment.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.elasticsearch.common.unit.TimeValue;
5252
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
5353
import org.elasticsearch.gateway.MetaDataStateFormat;
54-
import org.elasticsearch.gateway.WriteStateException;
5554
import org.elasticsearch.index.Index;
5655
import org.elasticsearch.index.IndexSettings;
5756
import org.elasticsearch.index.shard.ShardId;
@@ -391,11 +390,7 @@ private static NodeMetaData loadOrCreateNodeMetaData(Settings settings, Logger l
391390
metaData = new NodeMetaData(generateNodeId(settings));
392391
}
393392
// we write again to make sure all paths have the latest state file
394-
try {
395-
NodeMetaData.FORMAT.write(metaData, paths);
396-
} catch (WriteStateException e) {
397-
throw new IOException(e);
398-
}
393+
NodeMetaData.FORMAT.write(metaData, paths);
399394
return metaData;
400395
}
401396

server/src/main/java/org/elasticsearch/gateway/WriteStateException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
*/
1919
package org.elasticsearch.gateway;
2020

21+
import java.io.IOException;
22+
2123
/**
2224
* This exception is thrown when there is a problem of writing state to disk. <br>
2325
* If {@link #isDirty()} returns false, state is guaranteed to be not written to disk.
2426
* If {@link #isDirty()} returns true, we don't know if state is written to disk.
2527
*/
26-
public class WriteStateException extends Exception {
28+
public class WriteStateException extends IOException {
2729
private boolean dirty;
2830

2931
public WriteStateException(boolean dirty, String message, Exception cause) {

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import org.elasticsearch.common.util.concurrent.AsyncIOProcessor;
6565
import org.elasticsearch.common.xcontent.XContentHelper;
6666
import org.elasticsearch.core.internal.io.IOUtils;
67-
import org.elasticsearch.gateway.WriteStateException;
6867
import org.elasticsearch.index.Index;
6968
import org.elasticsearch.index.IndexModule;
7069
import org.elasticsearch.index.IndexNotFoundException;
@@ -2244,11 +2243,7 @@ private static void persistMetadata(
22442243
logger.trace("{} writing shard state, reason [{}]", shardId, writeReason);
22452244
final ShardStateMetaData newShardStateMetadata =
22462245
new ShardStateMetaData(newRouting.primary(), indexSettings.getUUID(), newRouting.allocationId());
2247-
try {
2248-
ShardStateMetaData.FORMAT.write(newShardStateMetadata, shardPath.getShardStatePath());
2249-
} catch (WriteStateException e) {
2250-
throw new IOException(e);
2251-
}
2246+
ShardStateMetaData.FORMAT.write(newShardStateMetadata, shardPath.getShardStatePath());
22522247
} else {
22532248
logger.trace("{} skip writing shard state, has been written before", shardId);
22542249
}

server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import org.elasticsearch.env.NodeEnvironment;
5454
import org.elasticsearch.env.NodeMetaData;
5555
import org.elasticsearch.gateway.MetaDataStateFormat;
56-
import org.elasticsearch.gateway.WriteStateException;
5756
import org.elasticsearch.index.Index;
5857
import org.elasticsearch.index.IndexSettings;
5958
import org.elasticsearch.index.engine.Engine;
@@ -461,11 +460,8 @@ protected void newAllocationId(Environment environment, ShardPath shardPath, Ter
461460
final ShardStateMetaData newShardStateMetaData =
462461
new ShardStateMetaData(shardStateMetaData.primary, shardStateMetaData.indexUUID, newAllocationId);
463462

464-
try {
465-
ShardStateMetaData.FORMAT.write(newShardStateMetaData, shardStatePath);
466-
} catch (WriteStateException e) {
467-
throw new IOException(e);
468-
}
463+
ShardStateMetaData.FORMAT.write(newShardStateMetaData, shardStatePath);
464+
469465
terminal.println("");
470466
terminal.println("You should run the following command to allocate this shard:");
471467

server/src/test/java/org/elasticsearch/gateway/MetaDataStateFormatTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public MetaData fromXContent(XContentParser parser) throws IOException {
9595
// indices are empty since they are serialized separately
9696
}
9797

98-
public void testReadWriteState() throws IOException, WriteStateException {
98+
public void testReadWriteState() throws IOException {
9999
Path[] dirs = new Path[randomIntBetween(1, 5)];
100100
for (int i = 0; i < dirs.length; i++) {
101101
dirs[i] = createTempDir();
@@ -136,7 +136,7 @@ public void testReadWriteState() throws IOException, WriteStateException {
136136
}
137137
}
138138

139-
public void testVersionMismatch() throws IOException, WriteStateException {
139+
public void testVersionMismatch() throws IOException {
140140
Path[] dirs = new Path[randomIntBetween(1, 5)];
141141
for (int i = 0; i < dirs.length; i++) {
142142
dirs[i] = createTempDir();
@@ -161,7 +161,7 @@ public void testVersionMismatch() throws IOException, WriteStateException {
161161
}
162162
}
163163

164-
public void testCorruption() throws IOException, WriteStateException {
164+
public void testCorruption() throws IOException {
165165
Path[] dirs = new Path[randomIntBetween(1, 5)];
166166
for (int i = 0; i < dirs.length; i++) {
167167
dirs[i] = createTempDir();
@@ -233,7 +233,7 @@ public static void corruptFile(Path file, Logger logger) throws IOException {
233233
}
234234
}
235235

236-
public void testLoadState() throws IOException, WriteStateException {
236+
public void testLoadState() throws IOException {
237237
final Path[] dirs = new Path[randomIntBetween(1, 5)];
238238
int numStates = randomIntBetween(1, 5);
239239
List<MetaData> meta = new ArrayList<>();
@@ -291,7 +291,7 @@ public void testLoadState() throws IOException, WriteStateException {
291291
}
292292
}
293293

294-
private DummyState writeAndReadStateSuccessfully(Format format, Path... paths) throws IOException, WriteStateException {
294+
private DummyState writeAndReadStateSuccessfully(Format format, Path... paths) throws IOException {
295295
format.noFailures();
296296
DummyState state = new DummyState(randomRealisticUnicodeOfCodepointLengthBetween(1, 100), randomInt(), randomLong(),
297297
randomDouble(), randomBoolean());
@@ -309,7 +309,7 @@ private static void ensureOnlyOneStateFile(Path[] paths) throws IOException {
309309
}
310310
}
311311

312-
public void testFailWriteAndReadPreviousState() throws IOException, WriteStateException {
312+
public void testFailWriteAndReadPreviousState() throws IOException {
313313
Path path = createTempDir();
314314
Format format = new Format("foo-");
315315

@@ -330,7 +330,7 @@ public void testFailWriteAndReadPreviousState() throws IOException, WriteStateEx
330330
writeAndReadStateSuccessfully(format, path);
331331
}
332332

333-
public void testFailWriteAndReadAnyState() throws IOException, WriteStateException {
333+
public void testFailWriteAndReadAnyState() throws IOException {
334334
Path path = createTempDir();
335335
Format format = new Format("foo-");
336336
Set<DummyState> possibleStates = new HashSet<>();
@@ -377,7 +377,7 @@ public void testFailCopyTmpFileToExtraLocation() throws IOException, WriteStateE
377377
}
378378

379379

380-
public void testFailRandomlyAndReadAnyState() throws IOException, WriteStateException {
380+
public void testFailRandomlyAndReadAnyState() throws IOException {
381381
Path paths[] = new Path[randomIntBetween(1, 5)];
382382
for (int i = 0; i < paths.length; i++) {
383383
paths[i] = createTempDir();

server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
import org.elasticsearch.common.xcontent.XContentType;
7777
import org.elasticsearch.core.internal.io.IOUtils;
7878
import org.elasticsearch.env.NodeEnvironment;
79-
import org.elasticsearch.gateway.WriteStateException;
8079
import org.elasticsearch.index.IndexSettings;
8180
import org.elasticsearch.index.engine.CommitStats;
8281
import org.elasticsearch.index.engine.Engine;
@@ -195,11 +194,7 @@ public static ShardStateMetaData load(Logger logger, Path... shardPaths) throws
195194

196195
public static void write(ShardStateMetaData shardStateMetaData,
197196
Path... shardPaths) throws IOException {
198-
try {
199-
ShardStateMetaData.FORMAT.write(shardStateMetaData, shardPaths);
200-
} catch (WriteStateException e) {
201-
throw new IOException(e);
202-
}
197+
ShardStateMetaData.FORMAT.write(shardStateMetaData, shardPaths);
203198
}
204199

205200
public static Engine getEngineFromShard(IndexShard shard) {

server/src/test/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommandTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.elasticsearch.env.Environment;
3737
import org.elasticsearch.env.NodeEnvironment;
3838
import org.elasticsearch.env.TestEnvironment;
39-
import org.elasticsearch.gateway.WriteStateException;
4039
import org.elasticsearch.index.IndexSettings;
4140
import org.elasticsearch.index.MergePolicyConfig;
4241
import org.elasticsearch.index.engine.EngineException;
@@ -402,11 +401,7 @@ private void writeIndexState() throws IOException {
402401
// create _state of IndexMetaData
403402
try(NodeEnvironment nodeEnvironment = new NodeEnvironment(environment.settings(), environment)) {
404403
final Path[] paths = nodeEnvironment.indexPaths(indexMetaData.getIndex());
405-
try {
406-
IndexMetaData.FORMAT.write(indexMetaData, paths);
407-
} catch (WriteStateException e) {
408-
throw new IOException(e);
409-
}
404+
IndexMetaData.FORMAT.write(indexMetaData, paths);
410405
logger.info("--> index metadata persisted to {} ", Arrays.toString(paths));
411406
}
412407
}

server/src/test/java/org/elasticsearch/index/shard/ShardPathTests.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.common.settings.Settings;
2525
import org.elasticsearch.env.Environment;
2626
import org.elasticsearch.env.NodeEnvironment;
27-
import org.elasticsearch.gateway.WriteStateException;
2827
import org.elasticsearch.index.Index;
2928
import org.elasticsearch.test.ESTestCase;
3029
import org.elasticsearch.test.IndexSettingsModule;
@@ -44,11 +43,7 @@ public void testLoadShardPath() throws IOException {
4443
ShardId shardId = new ShardId("foo", "0xDEADBEEF", 0);
4544
Path[] paths = env.availableShardPaths(shardId);
4645
Path path = randomFrom(paths);
47-
try {
48-
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
49-
} catch (WriteStateException e) {
50-
throw new IOException(e);
51-
}
46+
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
5247
ShardPath shardPath = ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings));
5348
assertEquals(path, shardPath.getDataPath());
5449
assertEquals("0xDEADBEEF", shardPath.getShardId().getIndex().getUUID());
@@ -67,11 +62,7 @@ public void testFailLoadShardPathOnMultiState() throws IOException {
6762
ShardId shardId = new ShardId("foo", indexUUID, 0);
6863
Path[] paths = env.availableShardPaths(shardId);
6964
assumeTrue("This test tests multi data.path but we only got one", paths.length > 1);
70-
try {
71-
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), paths);
72-
} catch (WriteStateException e) {
73-
throw new IOException(e);
74-
}
65+
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), paths);
7566
Exception e = expectThrows(IllegalStateException.class, () ->
7667
ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings)));
7768
assertThat(e.getMessage(), containsString("more than one shard state found"));
@@ -86,11 +77,7 @@ public void testFailLoadShardPathIndexUUIDMissmatch() throws IOException {
8677
ShardId shardId = new ShardId("foo", "foobar", 0);
8778
Path[] paths = env.availableShardPaths(shardId);
8879
Path path = randomFrom(paths);
89-
try {
90-
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
91-
} catch (WriteStateException e) {
92-
throw new IOException(e);
93-
}
80+
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
9481
Exception e = expectThrows(IllegalStateException.class, () ->
9582
ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings)));
9683
assertThat(e.getMessage(), containsString("expected: foobar on shard path"));
@@ -137,11 +124,7 @@ public void testGetRootPaths() throws IOException {
137124
ShardId shardId = new ShardId("foo", indexUUID, 0);
138125
Path[] paths = env.availableShardPaths(shardId);
139126
Path path = randomFrom(paths);
140-
try {
141-
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), path);
142-
} catch (WriteStateException e) {
143-
throw new IOException(e);
144-
}
127+
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), path);
145128
ShardPath shardPath = ShardPath.loadShardPath(logger, env, shardId,
146129
IndexSettingsModule.newIndexSettings(shardId.getIndex(), indexSettings, nodeSettings));
147130
boolean found = false;

0 commit comments

Comments
 (0)