Permalink
Browse files

Specialized exceptions and failure handler

The user can specify the desired behaviour upong a failure detection (crash, raise exception, notify admin...)
  • Loading branch information...
dgomezferro committed May 4, 2012
1 parent c605eec commit 94208f0df999d15d13e598a1ea47e40f8773920f
View
@@ -27,6 +27,15 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.4</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
</plugins>
</build>
<dependencies>
@@ -0,0 +1,9 @@
+package com.yahoo.pasc;
+
+class CrashFailureHandler implements FailureHandler {
+ @Override
+ public void handleFailure(Exception e) {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+}
@@ -0,0 +1,5 @@
+package com.yahoo.pasc;
+
+public interface FailureHandler {
+ public void handleFailure(Exception e);
+}
@@ -27,7 +27,11 @@
import org.slf4j.LoggerFactory;
import com.rits.cloning.Cloner;
-import com.yahoo.pasc.CorruptionException.Type;
+import com.yahoo.pasc.exceptions.ControlFlowException;
+import com.yahoo.pasc.exceptions.GuardException;
+import com.yahoo.pasc.exceptions.InputMessageException;
+import com.yahoo.pasc.exceptions.MessagesGenerationException;
+import com.yahoo.pasc.exceptions.VariableCorruptionException;
import com.yahoo.pasc.generation.Encapsulator;
import com.yahoo.pasc.generation.EncapsulatorGenerator;
import com.yahoo.pasc.generation.LightEncapsulatorGenerator;
@@ -50,6 +54,8 @@
private S state;
private S replica;
+ private FailureHandler failureHandler = new CrashFailureHandler();
+
private static Cloner cloner = new Cloner();
/**
@@ -62,6 +68,9 @@
*/
@SuppressWarnings("unchecked")
public static <T> T clone(T object) {
+ if (object instanceof ReadOnly) {
+ return object;
+ }
if (object instanceof CloneableDeep) {
return (T) ((CloneableDeep<T>)object).cloneDeep();
}
@@ -120,6 +129,7 @@ public void addHandler(Class<? extends Message> messageType,
private EncapsulatorGenerator encapsulatorGenerator;
private LightEncapsulatorGenerator lightEncapsulatorGenerator;
private final boolean protection;
+ private final boolean protectionReplica;
/**
* Create a new runtime with protection against failures
@@ -134,7 +144,7 @@ public PascRuntime() {
* @param protection Whether to use protection against corruptions/failures or not
*/
public PascRuntime(boolean protection) {
- this.protection = protection;
+ this.protectionReplica = this.protection = protection;
}
private final List<Message> emptyMessages = Collections.emptyList();
@@ -158,13 +168,21 @@ public PascRuntime(boolean protection) {
LOG.warn("Handler's guard predicate doesn't hold: {} {}", handler, receivedMessage);
return emptyMessages;
}
- if (protection) {
- return invoke(handler, receivedMessage, control);
- } else {
- return unsafeInvoke(handler, receivedMessage);
+ try {
+ if (protection != protectionReplica) {
+ throw new VariableCorruptionException("protection", protection, protectionReplica);
+ }
+ if (protection) {
+ return invoke(handler, receivedMessage, control);
+ } else {
+ return unsafeInvoke(handler, receivedMessage);
+ }
+ } catch (Exception e) {
+ failureHandler.handleFailure(e);
+ return emptyMessages;
}
}
-
+
private <D> List<Message> unsafeInvoke(final MessageHandler<Message, S, D> handler,
final Message receivedMessage) {
receivedMessage.verify();
@@ -231,7 +249,7 @@ public ControlObject() {
// check control flow
cfl = cfl_ = ControlFlow.SET;
if (control.cfs != control.cfr || control.cfs != ControlFlow.RESET) {
- throw new CorruptionException("cf ­ cfr or cf ­ RESET", Type.CFLOW);
+ throw new ControlFlowException("cf =/= cfr or cf =/= RESET");
}
control.cfs = control.cfr = ControlFlow.SET;
@@ -240,7 +258,7 @@ public ControlObject() {
List<D> replicaDescriptors = handler.processMessage(clonedMessage, (S) replicaEncapsulator);
if (control.cf_s != control.cf_r || control.cf_s != ControlFlow.RESET) {
- throw new CorruptionException("cf_s ­ cf_r or cf_s ­ RESET", Type.CFLOW);
+ throw new ControlFlowException("cf_s =/= cf_r or cf_s =/= RESET");
}
control.cf_s = control.cf_r = ControlFlow.SET;
cf_l = cf_l_ = ControlFlow.SET;
@@ -250,29 +268,29 @@ public ControlObject() {
// check control flow
if (cfl != cfl_ || cfl != ControlFlow.SET) {
- throw new CorruptionException("cfl ­ cfl_ or cfl ­ SET", Type.CFLOW);
+ throw new ControlFlowException("cfl =/= cfl_ or cfl =/= SET");
}
if (cf_l != cf_l_ || cf_l != ControlFlow.SET) {
- throw new CorruptionException("cf_l ­ cf_l_ or cf_l ­ SET", Type.CFLOW);
+ throw new ControlFlowException("cf_l =/= cf_l_ or cf_l =/= SET");
}
if (control.cf__s != control.cf__r || control.cf__s != ControlFlow.RESET) {
- throw new CorruptionException("cf__s ­ cf__r or cf__s ­ RESET", Type.CFLOW);
+ throw new ControlFlowException("cf__s =/= cf__r or cf__s =/= RESET");
}
control.cf__s = control.cf__r = ControlFlow.SET;
if (descriptors != null && replicaDescriptors != null) {
if (descriptors.size() != replicaDescriptors.size()) {
- throw new CorruptionException("Descriptors don't match", Type.MESSAGE);
+ throw new MessagesGenerationException(descriptors, replicaDescriptors);
}
Iterator<?> it = descriptors.iterator();
Iterator<?> itr = replicaDescriptors.iterator();
while(it.hasNext()) {
if (!compare(it.next(), itr.next())) {
- throw new CorruptionException("Descriptors don't match", Type.MESSAGE);
+ throw new MessagesGenerationException(descriptors, replicaDescriptors);
}
}
} else if (descriptors != null || replicaDescriptors != null) {
- throw new CorruptionException("Descriptors don't match", Type.MESSAGE);
+ throw new MessagesGenerationException(descriptors, replicaDescriptors);
}
// generate messages
@@ -306,15 +324,15 @@ public ControlObject() {
// verify input message again
if (!compare(receivedMessage, result.clonedMessage)) {
- throw new CorruptionException("m ­ m'", Type.MESSAGE);
+ throw new InputMessageException("Not equal", receivedMessage, result.clonedMessage);
}
if (!receivedMessage.verify()) {
- throw new CorruptionException("Corrupted message ", Type.MESSAGE);
+ throw new InputMessageException("Verification failed", receivedMessage, result.clonedMessage);
}
// verify guard
if (!handler.guardPredicate(receivedMessage)) {
- throw new CorruptionException("Guard doesn't hold", Type.GUARD);
+ throw new GuardException("Guard doesn't hold", handler, receivedMessage);
}
responses = result.responses;
@@ -345,4 +363,12 @@ S getState() {
S getReplica() {
return replica;
}
+
+ public FailureHandler getFailureHandler() {
+ return failureHandler;
+ }
+
+ public void setFailureHandler(FailureHandler failureHandler) {
+ this.failureHandler = failureHandler;
+ }
}
@@ -0,0 +1,5 @@
+package com.yahoo.pasc;
+
+public interface ReadOnly {
+
+}
@@ -0,0 +1,58 @@
+/**
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed 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. See accompanying LICENSE file.
+ */
+
+package com.yahoo.pasc.exceptions;
+
+/**
+ * Exception thrown in case of message/state corruption or failure.
+ *
+ */
+public class AsymmetricalChangesException extends CorruptionException {
+
+ private static final long serialVersionUID = 5336446438577061165L;
+
+ public AsymmetricalChangesException(String variable, Object key, Object replicaKey) {
+ super(variable + " key: " + key + " replicaKey: " + replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, boolean key, boolean replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, int key, int replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, byte key, byte replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, char key, char replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, long key, long replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, float key, float replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+
+ public AsymmetricalChangesException(String variable, double key, double replicaKey) {
+ this(variable, (Object) key, replicaKey);
+ }
+}
@@ -0,0 +1,30 @@
+/**
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed 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. See accompanying LICENSE file.
+ */
+
+package com.yahoo.pasc.exceptions;
+
+/**
+ * Exception thrown in case of control flow jump.
+ *
+ */
+public class ControlFlowException extends CorruptionException {
+ private static final long serialVersionUID = 7375338421695449597L;
+
+ public ControlFlowException(String description) {
+ super(description);
+ }
+
+}
@@ -14,36 +14,17 @@
* limitations under the License. See accompanying LICENSE file.
*/
-package com.yahoo.pasc;
+package com.yahoo.pasc.exceptions;
/**
* Exception thrown in case of message/state corruption or failure.
*
*/
public class CorruptionException extends RuntimeException {
-
- private static final long serialVersionUID = 5336446438577061165L;
-
- /**
- * Type of corruption/failure
- *
- */
- public enum Type {
- MESSAGE, STATE, REPLICA, GUARD, CFLOW
- }
-
- private final Type type;
- public CorruptionException(Type type) {
- this(null, type);
- }
+ private static final long serialVersionUID = 5336446438577061165L;
- public CorruptionException(String description, Type type) {
+ public CorruptionException(String description) {
super(description);
- this.type = type;
- }
-
- public Type getType() {
- return type;
}
}
@@ -0,0 +1,33 @@
+/**
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed 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. See accompanying LICENSE file.
+ */
+
+package com.yahoo.pasc.exceptions;
+
+import com.yahoo.pasc.Message;
+import com.yahoo.pasc.MessageHandler;
+
+/**
+ * Exception thrown in case of guard failure.
+ *
+ */
+public class GuardException extends CorruptionException {
+ private static final long serialVersionUID = -6830498977668729617L;
+
+ public GuardException(String description, MessageHandler<?, ?, ?> handler, Message receivedMessage) {
+ super(description + " Handler: " + handler + " Message: " + receivedMessage);
+ }
+
+}
@@ -0,0 +1,32 @@
+/**
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed 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. See accompanying LICENSE file.
+ */
+
+package com.yahoo.pasc.exceptions;
+
+import com.yahoo.pasc.Message;
+
+/**
+ * Exception thrown in case of message/state corruption or failure.
+ */
+public class InputMessageException extends CorruptionException {
+
+ private static final long serialVersionUID = 5336446438577061165L;
+
+ public InputMessageException(String description, Message input, Message clone) {
+ super(description + " Input: " + input + " Clone: " + clone);
+ }
+
+}
Oops, something went wrong.

0 comments on commit 94208f0

Please sign in to comment.