summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/index/shard
diff options
context:
space:
mode:
authorSimon Willnauer <simonw@apache.org>2015-07-03 15:40:09 +0200
committerSimon Willnauer <simonw@apache.org>2015-07-14 16:31:49 +0200
commit7db293c6167179bd470bb8a189223fcb67c0fae1 (patch)
tree5275a0b0cb4e49d7b31659cb254c34510283d27c /core/src/main/java/org/elasticsearch/index/shard
parentc6b110c6ef3004807f9257b68385c3d2e52635d5 (diff)
Generify Index and Shard exceptions
Today we have a intermediate hierarchy for shard and index exceptions which makes it hard to introduce generic exceptions like ResourceNotFoundException intoduced in this commit. This commit breaks up the hierarchy by adding index and shard as a special internal header that gets rendered for every exception that fills that header. This commit removes dedicated exceptions like `IndexMissingException` or `IndexShardMissingException` in favour of `ResourceNotFoundException`
Diffstat (limited to 'core/src/main/java/org/elasticsearch/index/shard')
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/IllegalIndexShardStateException.java20
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/IndexShard.java25
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/IndexShardException.java76
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/IndexShardRecoveryException.java8
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/ShardNotFoundException.java (renamed from core/src/main/java/org/elasticsearch/index/shard/IndexShardCreationException.java)16
-rw-r--r--core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java5
6 files changed, 44 insertions, 106 deletions
diff --git a/core/src/main/java/org/elasticsearch/index/shard/IllegalIndexShardStateException.java b/core/src/main/java/org/elasticsearch/index/shard/IllegalIndexShardStateException.java
index 0cde08f43c..31c235e09e 100644
--- a/core/src/main/java/org/elasticsearch/index/shard/IllegalIndexShardStateException.java
+++ b/core/src/main/java/org/elasticsearch/index/shard/IllegalIndexShardStateException.java
@@ -19,6 +19,8 @@
package org.elasticsearch.index.shard;
+import org.elasticsearch.ElasticsearchException;
+import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus;
@@ -28,17 +30,17 @@ import java.io.IOException;
/**
*
*/
-public class IllegalIndexShardStateException extends IndexShardException {
+public class IllegalIndexShardStateException extends ElasticsearchException {
private final IndexShardState currentState;
public IllegalIndexShardStateException(ShardId shardId, IndexShardState currentState, String msg) {
- super(shardId, "CurrentState[" + currentState + "] " + msg);
- this.currentState = currentState;
+ this(shardId, currentState, msg, null);
}
public IllegalIndexShardStateException(ShardId shardId, IndexShardState currentState, String msg, Throwable ex) {
- super(shardId, "CurrentState[" + currentState + "] ", ex);
+ super("CurrentState[" + currentState + "] " + msg, ex);
+ setShard(shardId);
this.currentState = currentState;
}
@@ -46,11 +48,6 @@ public class IllegalIndexShardStateException extends IndexShardException {
return currentState;
}
- @Override
- public RestStatus status() {
- return RestStatus.NOT_FOUND;
- }
-
public IllegalIndexShardStateException(StreamInput in) throws IOException{
super(in);
currentState = IndexShardState.fromId(in.readByte());
@@ -61,4 +58,9 @@ public class IllegalIndexShardStateException extends IndexShardException {
super.writeTo(out);
out.writeByte(currentState.id());
}
+
+ @Override
+ public RestStatus status() {
+ return RestStatus.NOT_FOUND;
+ }
}
diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java
index 13b34a48d8..dd4b6bc7dc 100644
--- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java
+++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java
@@ -24,11 +24,13 @@ import com.google.common.base.Preconditions;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.index.CheckIndex;
+import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.UsageTrackingQueryCachingPolicy;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.ThreadInterruptedException;
+import org.elasticsearch.ElasticsearchCorruptionException;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
@@ -104,6 +106,7 @@ import org.elasticsearch.indices.IndicesLifecycle;
import org.elasticsearch.indices.IndicesWarmer;
import org.elasticsearch.indices.InternalIndicesLifecycle;
import org.elasticsearch.indices.cache.query.IndicesQueryCache;
+import org.elasticsearch.indices.recovery.RecoveryFailedException;
import org.elasticsearch.indices.recovery.RecoveryState;
import org.elasticsearch.search.suggest.completion.Completion090PostingsFormat;
import org.elasticsearch.search.suggest.completion.CompletionStats;
@@ -839,7 +842,11 @@ public class IndexShard extends AbstractIndexShardComponent {
recoveryState.setStage(RecoveryState.Stage.VERIFY_INDEX);
// also check here, before we apply the translog
if (Booleans.parseBoolean(checkIndexOnStartup, false)) {
- checkIndex();
+ try {
+ checkIndex();
+ } catch (IOException ex) {
+ throw new RecoveryFailedException(recoveryState, "check index failed", ex);
+ }
}
recoveryState.setStage(RecoveryState.Stage.TRANSLOG);
// we disable deletes since we allow for operations to be executed against the shard while recovering
@@ -1182,19 +1189,17 @@ public class IndexShard extends AbstractIndexShardComponent {
}
}
- private void checkIndex() throws IndexShardException {
+ private void checkIndex() throws IOException {
if (store.tryIncRef()) {
try {
doCheckIndex();
- } catch (IOException e) {
- throw new IndexShardException(shardId, "exception during checkindex", e);
} finally {
store.decRef();
}
}
}
- private void doCheckIndex() throws IndexShardException, IOException {
+ private void doCheckIndex() throws IOException {
long timeNS = System.nanoTime();
if (!Lucene.indexExists(store.directory())) {
return;
@@ -1204,7 +1209,7 @@ public class IndexShard extends AbstractIndexShardComponent {
if ("checksum".equalsIgnoreCase(checkIndexOnStartup)) {
// physical verification only: verify all checksums for the latest commit
- boolean corrupt = false;
+ IOException corrupt = null;
MetadataSnapshot metadata = store.getMetadata();
for (Map.Entry<String, StoreFileMetaData> entry : metadata.asMap().entrySet()) {
try {
@@ -1213,13 +1218,13 @@ public class IndexShard extends AbstractIndexShardComponent {
} catch (IOException exc) {
out.println("checksum failed: " + entry.getKey());
exc.printStackTrace(out);
- corrupt = true;
+ corrupt = exc;
}
}
out.flush();
- if (corrupt) {
+ if (corrupt != null) {
logger.warn("check index [failure]\n{}", new String(os.bytes().toBytes(), Charsets.UTF_8));
- throw new IndexShardException(shardId, "index check failure");
+ throw corrupt;
}
} else {
// full checkindex
@@ -1244,7 +1249,7 @@ public class IndexShard extends AbstractIndexShardComponent {
}
} else {
// only throw a failure if we are not going to fix the index
- throw new IndexShardException(shardId, "index check failure");
+ throw new IllegalStateException("index check failure but can't fix it");
}
}
}
diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShardException.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShardException.java
deleted file mode 100644
index 23fde2abc1..0000000000
--- a/core/src/main/java/org/elasticsearch/index/shard/IndexShardException.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Licensed to Elasticsearch under one or more contributor
- * license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright
- * ownership. Elasticsearch licenses this file to you under
- * the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.elasticsearch.index.shard;
-
-import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.index.IndexException;
-
-import java.io.IOException;
-
-/**
- *
- */
-public class IndexShardException extends IndexException {
-
- private final ShardId shardId;
-
- public IndexShardException(ShardId shardId, String msg) {
- this(shardId, msg, null);
- }
-
- public IndexShardException(ShardId shardId, String msg, Throwable cause) {
- super(shardId == null ? null : shardId.index(), msg, cause);
- this.shardId = shardId;
- }
-
- public ShardId shardId() {
- return shardId;
- }
-
- @Override
- public String toString() {
- return (shardId == null ? "_na" : shardId) + " " + getMessage();
- }
-
- @Override
- protected void innerToXContent(XContentBuilder builder, Params params) throws IOException {
- if (shardId != null) {
- builder.field("shard", shardId.getId());
- }
- super.innerToXContent(builder, params);
- }
-
- public IndexShardException(StreamInput in) throws IOException{
- super(in);
- if (in.readBoolean()) {
- shardId = ShardId.readShardId(in);
- } else {
- shardId = null;
- }
- }
-
- @Override
- public void writeTo(StreamOutput out) throws IOException {
- super.writeTo(out);
- out.writeOptionalStreamable(shardId);
- }
-}
diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShardRecoveryException.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShardRecoveryException.java
index d53d1eaa6a..8ed3c95f92 100644
--- a/core/src/main/java/org/elasticsearch/index/shard/IndexShardRecoveryException.java
+++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShardRecoveryException.java
@@ -19,18 +19,18 @@
package org.elasticsearch.index.shard;
+import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.index.shard.IndexShardException;
-import org.elasticsearch.index.shard.ShardId;
import java.io.IOException;
/**
*
*/
-public class IndexShardRecoveryException extends IndexShardException {
+public class IndexShardRecoveryException extends ElasticsearchException {
public IndexShardRecoveryException(ShardId shardId, String msg, Throwable cause) {
- super(shardId, msg, cause);
+ super(msg, cause);
+ setShard(shardId);
}
public IndexShardRecoveryException(StreamInput in) throws IOException{
diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShardCreationException.java b/core/src/main/java/org/elasticsearch/index/shard/ShardNotFoundException.java
index a6d879e5b0..fa2c8ce710 100644
--- a/core/src/main/java/org/elasticsearch/index/shard/IndexShardCreationException.java
+++ b/core/src/main/java/org/elasticsearch/index/shard/ShardNotFoundException.java
@@ -19,19 +19,25 @@
package org.elasticsearch.index.shard;
+import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.common.io.stream.StreamInput;
import java.io.IOException;
/**
+ *
*/
-public class IndexShardCreationException extends IndexShardException {
-
- public IndexShardCreationException(ShardId shardId, Throwable cause) {
- super(shardId, "failed to create shard", cause);
+public class ShardNotFoundException extends ResourceNotFoundException {
+ public ShardNotFoundException(ShardId shardId) {
+ this(shardId, null);
}
- public IndexShardCreationException(StreamInput in) throws IOException{
+ public ShardNotFoundException(ShardId shardId, Throwable ex) {
+ super("no such shard", ex);
+ setShard(shardId);
+
+ }
+ public ShardNotFoundException(StreamInput in) throws IOException{
super(in);
}
}
diff --git a/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java b/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java
index 91da224ec3..1a54c748c3 100644
--- a/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java
+++ b/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java
@@ -90,12 +90,13 @@ public class TranslogRecoveryPerformer {
return numOps;
}
- public static class BatchOperationException extends IndexShardException {
+ public static class BatchOperationException extends ElasticsearchException {
private final int completedOperations;
public BatchOperationException(ShardId shardId, String msg, int completedOperations, Throwable cause) {
- super(shardId, msg, cause);
+ super(msg, cause);
+ setShard(shardId);
this.completedOperations = completedOperations;
}