[Zen2] Storage layer WriteStateException propagation (#36052)

Currently, we only log that WriteStateException has occurred, in
GatewayMetaState.

This PR goal is to re-throw WriteStateException to upper layers.
If dirty flag is set to false, we wrap WriteStateException in
UncheckedIOException, we prefer unchecked exception not to add
explicit throws in the multitude of layers.
If dirty flag is set to true - the world is broken. And we need to
halt the JVM. Instead of explicit halting in GatewayMetaState, we
prefer to throw IOError, which will be subsequently handled by
ElasticsearchUncaughtExceptionHandler and JVM will be halted.

This PR also adds tests for WriteStateException.
This commit is contained in:
Andrey Ershov 2018-12-06 17:02:02 +01:00 committed by GitHub
parent b08cffad48
commit f0340d6d32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 4 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.gateway;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterName;
@ -235,8 +236,8 @@ public class GatewayMetaState implements ClusterStateApplier, CoordinationState.
try {
innerSetCurrentTerm(currentTerm);
} catch (WriteStateException e) {
logger.warn("Exception occurred when setting current term", e);
//TODO re-throw exception
logger.error(new ParameterizedMessage("Failed to set current term to {}", currentTerm), e);
e.rethrowAsErrorOrUncheckedException();
}
}
@ -253,8 +254,8 @@ public class GatewayMetaState implements ClusterStateApplier, CoordinationState.
incrementalWrite = previousClusterState.term() == clusterState.term();
updateClusterState(clusterState, previousClusterState);
} catch (WriteStateException e) {
logger.warn("Exception occurred when setting last accepted state", e);
//TODO re-throw exception
logger.error(new ParameterizedMessage("Failed to set last accepted state with version {}", clusterState.version()), e);
e.rethrowAsErrorOrUncheckedException();
}
}

View File

@ -18,7 +18,9 @@
*/
package org.elasticsearch.gateway;
import java.io.IOError;
import java.io.IOException;
import java.io.UncheckedIOException;
/**
* This exception is thrown when there is a problem of writing state to disk.
@ -38,4 +40,16 @@ public class WriteStateException extends IOException {
public boolean isDirty() {
return dirty;
}
/**
* Rethrows this {@link WriteStateException} as {@link IOError} if dirty flag is set, which will lead to JVM shutdown.
* If dirty flag is not set, this exception is wrapped into {@link UncheckedIOException}.
*/
public void rethrowAsErrorOrUncheckedException() {
if (isDirty()) {
throw new IOError(this);
} else {
throw new UncheckedIOException(this);
}
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.gateway;
import org.elasticsearch.test.ESTestCase;
import java.io.IOError;
import java.io.UncheckedIOException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
public class WriteStateExceptionTests extends ESTestCase {
public void testDirtyFlag() {
boolean dirty = randomBoolean();
WriteStateException ex = new WriteStateException(dirty, "test", null);
assertThat(ex.isDirty(), equalTo(dirty));
}
public void testNonDirtyRethrow() {
WriteStateException ex = new WriteStateException(false, "test", null);
UncheckedIOException ex2 = expectThrows(UncheckedIOException.class, () -> ex.rethrowAsErrorOrUncheckedException());
assertThat(ex2.getCause(), instanceOf(WriteStateException.class));
}
public void testDirtyRethrow() {
WriteStateException ex = new WriteStateException(true, "test", null);
IOError err = expectThrows(IOError.class, () -> ex.rethrowAsErrorOrUncheckedException());
assertThat(err.getCause(), instanceOf(WriteStateException.class));
}
}