Skip to content

Commit

Permalink
WriteStateException extends IOException
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Ershov committed Oct 23, 2018
1 parent 0cfffd5 commit b0ac9aa
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.gateway.MetaDataStateFormat;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.shard.ShardId;
Expand Down Expand Up @@ -391,11 +390,7 @@ private static NodeMetaData loadOrCreateNodeMetaData(Settings settings, Logger l
metaData = new NodeMetaData(generateNodeId(settings));
}
// we write again to make sure all paths have the latest state file
try {
NodeMetaData.FORMAT.write(metaData, paths);
} catch (WriteStateException e) {
throw new IOException(e);
}
NodeMetaData.FORMAT.write(metaData, paths);
return metaData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.elasticsearch.gateway;

import java.io.IOException;

/**
* This exception is thrown when there is a problem of writing state to disk. <br>
* If {@link #isDirty()} returns false, state is guaranteed to be not written to disk.
* If {@link #isDirty()} returns true, we don't know if state is written to disk.
*/
public class WriteStateException extends Exception {
public class WriteStateException extends IOException {
private boolean dirty;

public WriteStateException(boolean dirty, String message, Exception cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.elasticsearch.common.util.concurrent.AsyncIOProcessor;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexModule;
import org.elasticsearch.index.IndexNotFoundException;
Expand Down Expand Up @@ -2244,11 +2243,7 @@ private static void persistMetadata(
logger.trace("{} writing shard state, reason [{}]", shardId, writeReason);
final ShardStateMetaData newShardStateMetadata =
new ShardStateMetaData(newRouting.primary(), indexSettings.getUUID(), newRouting.allocationId());
try {
ShardStateMetaData.FORMAT.write(newShardStateMetadata, shardPath.getShardStatePath());
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(newShardStateMetadata, shardPath.getShardStatePath());
} else {
logger.trace("{} skip writing shard state, has been written before", shardId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.env.NodeMetaData;
import org.elasticsearch.gateway.MetaDataStateFormat;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.engine.Engine;
Expand Down Expand Up @@ -461,11 +460,8 @@ protected void newAllocationId(Environment environment, ShardPath shardPath, Ter
final ShardStateMetaData newShardStateMetaData =
new ShardStateMetaData(shardStateMetaData.primary, shardStateMetaData.indexUUID, newAllocationId);

try {
ShardStateMetaData.FORMAT.write(newShardStateMetaData, shardStatePath);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(newShardStateMetaData, shardStatePath);

terminal.println("");
terminal.println("You should run the following command to allocate this shard:");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public MetaData fromXContent(XContentParser parser) throws IOException {
// indices are empty since they are serialized separately
}

public void testReadWriteState() throws IOException, WriteStateException {
public void testReadWriteState() throws IOException {
Path[] dirs = new Path[randomIntBetween(1, 5)];
for (int i = 0; i < dirs.length; i++) {
dirs[i] = createTempDir();
Expand Down Expand Up @@ -136,7 +136,7 @@ public void testReadWriteState() throws IOException, WriteStateException {
}
}

public void testVersionMismatch() throws IOException, WriteStateException {
public void testVersionMismatch() throws IOException {
Path[] dirs = new Path[randomIntBetween(1, 5)];
for (int i = 0; i < dirs.length; i++) {
dirs[i] = createTempDir();
Expand All @@ -161,7 +161,7 @@ public void testVersionMismatch() throws IOException, WriteStateException {
}
}

public void testCorruption() throws IOException, WriteStateException {
public void testCorruption() throws IOException {
Path[] dirs = new Path[randomIntBetween(1, 5)];
for (int i = 0; i < dirs.length; i++) {
dirs[i] = createTempDir();
Expand Down Expand Up @@ -233,7 +233,7 @@ public static void corruptFile(Path file, Logger logger) throws IOException {
}
}

public void testLoadState() throws IOException, WriteStateException {
public void testLoadState() throws IOException {
final Path[] dirs = new Path[randomIntBetween(1, 5)];
int numStates = randomIntBetween(1, 5);
List<MetaData> meta = new ArrayList<>();
Expand Down Expand Up @@ -291,7 +291,7 @@ public void testLoadState() throws IOException, WriteStateException {
}
}

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

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

Expand All @@ -330,7 +330,7 @@ public void testFailWriteAndReadPreviousState() throws IOException, WriteStateEx
writeAndReadStateSuccessfully(format, path);
}

public void testFailWriteAndReadAnyState() throws IOException, WriteStateException {
public void testFailWriteAndReadAnyState() throws IOException {
Path path = createTempDir();
Format format = new Format("foo-");
Set<DummyState> possibleStates = new HashSet<>();
Expand Down Expand Up @@ -377,7 +377,7 @@ public void testFailCopyTmpFileToExtraLocation() throws IOException, WriteStateE
}


public void testFailRandomlyAndReadAnyState() throws IOException, WriteStateException {
public void testFailRandomlyAndReadAnyState() throws IOException {
Path paths[] = new Path[randomIntBetween(1, 5)];
for (int i = 0; i < paths.length; i++) {
paths[i] = createTempDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.engine.CommitStats;
import org.elasticsearch.index.engine.Engine;
Expand Down Expand Up @@ -195,11 +194,7 @@ public static ShardStateMetaData load(Logger logger, Path... shardPaths) throws

public static void write(ShardStateMetaData shardStateMetaData,
Path... shardPaths) throws IOException {
try {
ShardStateMetaData.FORMAT.write(shardStateMetaData, shardPaths);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(shardStateMetaData, shardPaths);
}

public static Engine getEngineFromShard(IndexShard shard) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.env.TestEnvironment;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.MergePolicyConfig;
import org.elasticsearch.index.engine.EngineException;
Expand Down Expand Up @@ -402,11 +401,7 @@ private void writeIndexState() throws IOException {
// create _state of IndexMetaData
try(NodeEnvironment nodeEnvironment = new NodeEnvironment(environment.settings(), environment)) {
final Path[] paths = nodeEnvironment.indexPaths(indexMetaData.getIndex());
try {
IndexMetaData.FORMAT.write(indexMetaData, paths);
} catch (WriteStateException e) {
throw new IOException(e);
}
IndexMetaData.FORMAT.write(indexMetaData, paths);
logger.info("--> index metadata persisted to {} ", Arrays.toString(paths));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.gateway.WriteStateException;
import org.elasticsearch.index.Index;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule;
Expand All @@ -44,11 +43,7 @@ public void testLoadShardPath() throws IOException {
ShardId shardId = new ShardId("foo", "0xDEADBEEF", 0);
Path[] paths = env.availableShardPaths(shardId);
Path path = randomFrom(paths);
try {
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
ShardPath shardPath = ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings));
assertEquals(path, shardPath.getDataPath());
assertEquals("0xDEADBEEF", shardPath.getShardId().getIndex().getUUID());
Expand All @@ -67,11 +62,7 @@ public void testFailLoadShardPathOnMultiState() throws IOException {
ShardId shardId = new ShardId("foo", indexUUID, 0);
Path[] paths = env.availableShardPaths(shardId);
assumeTrue("This test tests multi data.path but we only got one", paths.length > 1);
try {
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), paths);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), paths);
Exception e = expectThrows(IllegalStateException.class, () ->
ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings)));
assertThat(e.getMessage(), containsString("more than one shard state found"));
Expand All @@ -86,11 +77,7 @@ public void testFailLoadShardPathIndexUUIDMissmatch() throws IOException {
ShardId shardId = new ShardId("foo", "foobar", 0);
Path[] paths = env.availableShardPaths(shardId);
Path path = randomFrom(paths);
try {
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
Exception e = expectThrows(IllegalStateException.class, () ->
ShardPath.loadShardPath(logger, env, shardId, IndexSettingsModule.newIndexSettings(shardId.getIndex(), settings)));
assertThat(e.getMessage(), containsString("expected: foobar on shard path"));
Expand Down Expand Up @@ -137,11 +124,7 @@ public void testGetRootPaths() throws IOException {
ShardId shardId = new ShardId("foo", indexUUID, 0);
Path[] paths = env.availableShardPaths(shardId);
Path path = randomFrom(paths);
try {
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), path);
} catch (WriteStateException e) {
throw new IOException(e);
}
ShardStateMetaData.FORMAT.write(new ShardStateMetaData(true, indexUUID, AllocationId.newInitializing()), path);
ShardPath shardPath = ShardPath.loadShardPath(logger, env, shardId,
IndexSettingsModule.newIndexSettings(shardId.getIndex(), indexSettings, nodeSettings));
boolean found = false;
Expand Down

0 comments on commit b0ac9aa

Please sign in to comment.