Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 94208f0df999d15d13e598a1ea47e40f8773920f 1 parent c605eec
@dgomezferro dgomezferro authored
View
9 pom.xml
@@ -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>
View
9 src/main/java/com/yahoo/pasc/CrashFailureHandler.java
@@ -0,0 +1,9 @@
+package com.yahoo.pasc;
+
+class CrashFailureHandler implements FailureHandler {
+ @Override
+ public void handleFailure(Exception e) {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+}
View
5 src/main/java/com/yahoo/pasc/FailureHandler.java
@@ -0,0 +1,5 @@
+package com.yahoo.pasc;
+
+public interface FailureHandler {
+ public void handleFailure(Exception e);
+}
View
62 src/main/java/com/yahoo/pasc/PascRuntime.java
@@ -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;
+ }
}
View
5 src/main/java/com/yahoo/pasc/ReadOnly.java
@@ -0,0 +1,5 @@
+package com.yahoo.pasc;
+
+public interface ReadOnly {
+
+}
View
58 src/main/java/com/yahoo/pasc/exceptions/AsymmetricalChangesException.java
@@ -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);
+ }
+}
View
30 src/main/java/com/yahoo/pasc/exceptions/ControlFlowException.java
@@ -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);
+ }
+
+}
View
25 ...main/java/com/yahoo/pasc/CorruptionException.java → ...om/yahoo/pasc/exceptions/CorruptionException.java
@@ -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;
}
}
View
33 src/main/java/com/yahoo/pasc/exceptions/GuardException.java
@@ -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);
+ }
+
+}
View
32 src/main/java/com/yahoo/pasc/exceptions/InputMessageException.java
@@ -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);
+ }
+
+}
View
32 src/main/java/com/yahoo/pasc/exceptions/MessagesGenerationException.java
@@ -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 java.util.List;
+
+/**
+ * Exception thrown in case of message/state corruption or failure.
+ */
+public class MessagesGenerationException extends CorruptionException {
+
+ private static final long serialVersionUID = 5336446438577061165L;
+
+ public MessagesGenerationException(List<?> output, List<?> replica) {
+ super(output + " != " + replica);
+ }
+
+}
View
57 src/main/java/com/yahoo/pasc/exceptions/VariableCorruptionException.java
@@ -0,0 +1,57 @@
+/**
+ * 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 VariableCorruptionException extends CorruptionException {
+
+ private static final long serialVersionUID = 5336446438577061165L;
+
+ public VariableCorruptionException(String variable, Object value, Object replica) {
+ super(variable + " value: " + value + " replica: " + replica);
+ }
+
+ public VariableCorruptionException(String variable, boolean value, boolean replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, int value, int replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, byte value, byte replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, char value, char replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, long value, long replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, float value, float replica) {
+ this(variable, (Object) value, replica);
+ }
+
+ public VariableCorruptionException(String variable, double value, double replica) {
+ this(variable, (Object) value, replica);
+ }
+}
View
69 src/main/java/com/yahoo/pasc/generation/EncapsulatorGenerator.java
@@ -42,12 +42,16 @@
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import org.objenesis.instantiator.ObjectInstantiator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import com.yahoo.pasc.ProcessState;
public class EncapsulatorGenerator {
+ private static final Logger LOG = LoggerFactory.getLogger(EncapsulatorGenerator.class);
+
private Objenesis objenesis = new ObjenesisStd();
private ObjectInstantiator instantiator;
@@ -189,7 +193,6 @@ private String buildSetter(AccessibleField af) {
setterTemplate.add("primitiveKey", primitiveKey);
String result = setterTemplate.render();
- System.out.println("Method: " + result);
return result;
}
@@ -201,14 +204,14 @@ private String buildSetter(AccessibleField af) {
" $var$Read = true;" +
" $if(primitive)$ " +
" if(state.$getter$() != replica.$getter$()) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", " +
+ " state.$getter$(), replica.$getter$());" +
" }" +
" $endif$" +
" $if(!primitive)$" +
" if(!com.yahoo.pasc.PascRuntime.compare(state.$getter$(), replica.$getter$())) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", " +
+ " state.$getter$(), replica.$getter$());" +
" }" +
" $endif$" +
" }" +
@@ -252,15 +255,13 @@ private String buildSetter(AccessibleField af) {
" $if(primitive)$ " +
" $type$ temp = state.$getter$(_key);" +
" if(temp != replica.$getter$(_key)) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", temp, replica.$getter$(_key));" +
" }" +
" $endif$" +
" $if(!primitive)$" +
" $type$ temp = $objectCast$ com.yahoo.pasc.PascRuntime.clone(state.$getter$(_key));" +
" if(!com.yahoo.pasc.PascRuntime.compare(temp, replica.$getter$(_key))) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", temp, replica.$getter$(_key));" +
" }" +
" $endif$" +
// Write in cache
@@ -337,7 +338,7 @@ private String buildGetter(AccessibleField af) {
getterTemplate.add("mapGet", mapGet);
String result = getterTemplate.render();
- System.out.println("Method: " + result);
+ LOG.trace("Method: {}", result);
return result;
}
@@ -375,29 +376,17 @@ private void generateToString(CtClass facadeCtClass, List<AccessibleField> field
private String applySingle = addNewLines(
" if($var$Written) {" +
" if(!lightEncap.$var$Read)" +
- " throw new com.yahoo.pasc.CorruptionException(\"Written records don't match.\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", null, null);" +
" if(!$var$Read)" +
- " throw new com.yahoo.pasc.CorruptionException(\"Written records don't match.\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", null, null);" +
" temp.$setter$($var$Ref);" +
" }");
private String applyMulti = addNewLines(
" if ($var$CacheValid) {" +
- " $typeKey$ tempKey = $var$LatestKey;" +
- " $typeKey$ replicaKey = lightEncap.$var$LatestKey;" +
- " $if(primitiveKey)$" +
- " if(tempKey != replicaKey) " +
- " throw new com.yahoo.pasc.CorruptionException(\"$var$ cached keys don't match.\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
- " $endif$" +
- " $if(!primitiveKey)$" +
- " if(!((Object)tempKey).equals(replicaKey)) " +
- " throw new com.yahoo.pasc.CorruptionException(\"$var$ cached keys don't match. 2\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
- " $endif$" +
- " temp.$setter$($var$LatestKey, $var$LatestValue);" +
+ " $typeKey$ tempKey;" +
+ " $typeKey$ replicaKey;" +
+ // First apply changes from the map
" if ($var$Written != null && !$var$Written.isEmpty()) {" +
" it.unimi.dsi.fastutil.objects.ObjectIterator it = $var$Written.$entrySetName$().fastIterator();" +
" $iteratorName$ itl = lightEncap.$var$Read.iterator();" +
@@ -407,23 +396,31 @@ private void generateToString(CtClass facadeCtClass, List<AccessibleField> field
" replicaKey = $castKey$ itl.$nextName$();" +
" $if(primitiveKey)$" +
" if(tempKey != replicaKey) " +
- " throw new com.yahoo.pasc.CorruptionException(\"$var$ written map keys don't match.\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", tempKey, replicaKey);" +
" $endif$" +
" $if(!primitiveKey)$" +
" if(!((Object)tempKey).equals(replicaKey)) " +
- " throw new com.yahoo.pasc.CorruptionException(\"$var$ written map keys don't match.\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", tempKey, replicaKey);" +
" $endif$" +
" temp.$setter$($castKey$ tempKey, $castValue$ entry.get$valueName$());" +
" }" +
" if (itl.hasNext()) " +
- " throw new com.yahoo.pasc.CorruptionException(\"Written records don't match. 5\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", null, itl.next());" +
" } else if (($var$Read != null && !$var$Read.isEmpty()) || " +
" (lightEncap.$var$Read != null && !lightEncap.$var$Read.isEmpty())) " +
- " throw new com.yahoo.pasc.CorruptionException(\"$var$ empty written, non empty read\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", null, null);" +
+ // then apply changes from the cache (modified last)
+ " tempKey = $var$LatestKey;" +
+ " replicaKey = lightEncap.$var$LatestKey;" +
+ " $if(primitiveKey)$" +
+ " if(tempKey != replicaKey) " +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", tempKey, replicaKey);" +
+ " $endif$" +
+ " $if(!primitiveKey)$" +
+ " if(!((Object)tempKey).equals(replicaKey)) " +
+ " throw new com.yahoo.pasc.exceptions.AsymmetricalChangesException(\"$var$\", tempKey, replicaKey);" +
+ " $endif$" +
+ " temp.$setter$($var$LatestKey, $var$LatestValue);" +
" }"
);
@@ -467,8 +464,6 @@ private String buildApplyModifications(List<AccessibleField> fields) {
method.add("className", className);
method.add("variableApplications", applications);
- System.out.println("Apply modifications: \n " + method.render());
-
return method.render();
}
View
16 src/main/java/com/yahoo/pasc/generation/LightEncapsulatorGenerator.java
@@ -152,14 +152,14 @@ private void generateGettersAndSetters(CtClass facadeCtClass, List<AccessibleFie
" $var$Read = true;" +
" $if(primitive)$ " +
" if(state.$getter$() != replica.$getter$()) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", " +
+ " state.$getter$(), replica.$getter$());" +
" }" +
" $endif$" +
" $if(!primitive)$" +
" if(!com.yahoo.pasc.PascRuntime.compare(state.$getter$(), replica.$getter$())) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\"," +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", " +
+ " state.$getter$(), replica.$getter$());" +
" }" +
" $endif$" +
" }" +
@@ -194,14 +194,12 @@ private void generateGettersAndSetters(CtClass facadeCtClass, List<AccessibleFie
" $type$ temp = state.$getter$(_key);" +
" $if(primitive)$ " +
" if(temp != replica.$getter$(_key)) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", temp, replica.$getter$(_key));" +
" }" +
" $endif$" +
" $if(!primitive)$" +
" if(!com.yahoo.pasc.PascRuntime.compare(temp, replica.$getter$(_key))) {" +
- " throw new com.yahoo.pasc.CorruptionException(\"States differ\", " +
- " com.yahoo.pasc.CorruptionException.Type.STATE);" +
+ " throw new com.yahoo.pasc.exceptions.VariableCorruptionException(\"$var$\", temp, replica.$getter$(_key));" +
" }" +
" $endif$" +
" $var$CacheValid = true;" +
@@ -269,7 +267,6 @@ private String buildSetter(AccessibleField af) {
setterTemplate.add("primitiveKey", primitiveKey);
String result = setterTemplate.render();
- System.out.println("Method: " + result);
return result;
}
@@ -305,7 +302,6 @@ private String buildGetter(AccessibleField af) {
getterTemplate.add("primitiveKey", primitiveKey);
String result = getterTemplate.render();
- System.out.println("Method: " + result);
return result;
}
View
72 src/test/java/com/yahoo/pasc/RuntimeTest.java
@@ -28,17 +28,24 @@
import org.junit.Before;
import org.junit.Test;
-import com.yahoo.pasc.CorruptionException;
-import com.yahoo.pasc.EqualsDeep;
-import com.yahoo.pasc.Message;
-import com.yahoo.pasc.MessageDescriptor;
-import com.yahoo.pasc.MessageHandler;
-import com.yahoo.pasc.PascRuntime;
-import com.yahoo.pasc.ProcessState;
-import com.yahoo.pasc.CorruptionException.Type;
+import com.yahoo.pasc.exceptions.AsymmetricalChangesException;
+import com.yahoo.pasc.exceptions.CorruptionException;
+import com.yahoo.pasc.exceptions.InputMessageException;
+import com.yahoo.pasc.exceptions.MessagesGenerationException;
+import com.yahoo.pasc.exceptions.VariableCorruptionException;
public class RuntimeTest {
+ private class TestFailureHandler implements FailureHandler {
+ @Override
+ public void handleFailure(Exception e) {
+ if (e instanceof CorruptionException) {
+ throw (CorruptionException) e;
+ }
+ throw new RuntimeException(e);
+ }
+ }
+
private static int SIZE = 100;
private PascRuntime<State> runtime;
@@ -47,6 +54,7 @@ public void setUp() throws SecurityException, NoSuchFieldException {
runtime = new PascRuntime<State>();
runtime.setState(new State());
runtime.addHandler(TMessage.class, new Handler());
+ runtime.setFailureHandler(new TestFailureHandler());
}
@Test
@@ -162,6 +170,34 @@ public void indexedVariables() {
}
}
}
+
+ @Test
+ public void applyLastestChane() {
+ Message m = new TMessage(5);
+ m.storeReplica(m);
+ State s = runtime.getState();
+ runtime.addHandler(TMessage.class, new Handler() {
+ @Override
+ public List<TMessage> processMessage(TMessage message, State state) {
+ state.setC(Integer.toString(message.a), 33);
+ state.setC(Integer.toString(message.a + 1), 34);
+ state.setC(Integer.toString(message.a), 35);
+ return Arrays.asList(new TMessage(0));
+ }
+ });
+ List<Message> messages = runtime.handleMessage(m);
+ assertNotNull(messages);
+ assertEquals(1, messages.size());
+ for (int i = 0; i < 10; ++i) {
+ if (i == 5) {
+ assertEquals(35, s.getC(Integer.toString(i)));
+ } else if (i == 6) {
+ assertEquals(34, s.getC(Integer.toString(i)));
+ } else {
+ assertEquals(0, s.getC(Integer.toString(i)));
+ }
+ }
+ }
@Test
public void ignoreCorruptInconmingMessage() {
@@ -185,8 +221,8 @@ public void detectCorruptReplica() {
try {
runtime.handleMessage(m);
fail("Should detect corrupt replica");
- } catch (CorruptionException e) {
- assertEquals(Type.STATE, e.getType());
+ } catch (VariableCorruptionException e) {
+ //ignore
}
}
@@ -204,8 +240,8 @@ public void detectCorruptMessage() {
try {
runtime.handleMessage(m);
fail("Should detect corrupt message");
- } catch (CorruptionException e) {
- assertEquals(Type.MESSAGE, e.getType());
+ } catch (InputMessageException e) {
+ //ignore
}
}
@@ -230,8 +266,8 @@ public void detectCorruptState() {
try {
runtime.handleMessage(m);
fail("Should detect corrupt state");
- } catch (CorruptionException e) {
- assertEquals(Type.STATE, e.getType());
+ } catch (VariableCorruptionException e) {
+ //ignore
}
}
@@ -254,8 +290,8 @@ public void detectInconsistentMessages() {
try {
runtime.handleMessage(m);
fail("Should detect inconsistent change");
- } catch (CorruptionException e) {
- assertEquals(Type.MESSAGE, e.getType());
+ } catch (MessagesGenerationException e) {
+ //ignore
}
}
@@ -280,8 +316,8 @@ public void detectInconsistentChange() {
try {
runtime.handleMessage(m);
fail("Should detect inconsistent change");
- } catch (CorruptionException e) {
- assertEquals(Type.STATE, e.getType());
+ } catch (AsymmetricalChangesException e) {
+ //ignore
}
}
View
15 src/test/java/com/yahoo/pasc/generation/EncapsulatorTest.java
@@ -27,12 +27,9 @@
import org.junit.BeforeClass;
import org.junit.Test;
-import com.yahoo.pasc.CorruptionException;
import com.yahoo.pasc.ProcessState;
-import com.yahoo.pasc.CorruptionException.Type;
-import com.yahoo.pasc.generation.Encapsulator;
-import com.yahoo.pasc.generation.EncapsulatorGenerator;
-import com.yahoo.pasc.generation.LightEncapsulatorGenerator;
+import com.yahoo.pasc.exceptions.AsymmetricalChangesException;
+import com.yahoo.pasc.exceptions.VariableCorruptionException;
public class EncapsulatorTest {
private static EncapsulatorGenerator generator;
@@ -109,8 +106,8 @@ public void consistentStates() {
try {
encapsulator.applyModifications(false, lightEncapsulator);
fail("Didn't raise exception");
- } catch (CorruptionException e) {
- assertEquals(Type.STATE, e.getType());
+ } catch (AsymmetricalChangesException e) {
+ //ignore
}
}
@@ -122,8 +119,8 @@ public void checkInconsistentReplica() {
try {
wrappedState.getA();
fail("Didn't raise exception");
- } catch (CorruptionException e) {
- assertEquals(Type.STATE, e.getType());
+ } catch (VariableCorruptionException e) {
+ //ignore
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.