-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: add saga statelang semantic validation (#5928)
- Loading branch information
Showing
20 changed files
with
679 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/StateMachineUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.parser.utils; | ||
|
||
import io.seata.common.util.StringUtils; | ||
import io.seata.saga.statelang.domain.ChoiceState; | ||
import io.seata.saga.statelang.domain.DomainConstants; | ||
import io.seata.saga.statelang.domain.State; | ||
import io.seata.saga.statelang.domain.TaskState; | ||
|
||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Util class for parsing StateMachine | ||
* | ||
* @author ptyin | ||
*/ | ||
public class StateMachineUtils { | ||
public static Set<String> getAllPossibleSubsequentStates(State state) { | ||
Set<String> subsequentStates = new HashSet<>(); | ||
// Next state | ||
subsequentStates.add(state.getNext()); | ||
switch (state.getType()) { | ||
case DomainConstants.STATE_TYPE_SCRIPT_TASK: | ||
case DomainConstants.STATE_TYPE_SERVICE_TASK: | ||
case DomainConstants.STATE_TYPE_SUB_STATE_MACHINE: | ||
case DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION: | ||
// Next state in catches | ||
Optional.ofNullable(((TaskState) state).getCatches()) | ||
.ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); | ||
break; | ||
|
||
case DomainConstants.STATE_TYPE_CHOICE: | ||
// Choice state | ||
Optional.ofNullable(((ChoiceState) state).getChoices()) | ||
.ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); | ||
// Default choice | ||
subsequentStates.add(((ChoiceState) state).getDefault()); | ||
break; | ||
default: | ||
// Otherwise do nothing | ||
} | ||
return subsequentStates.stream().filter(StringUtils::isNotBlank).collect(Collectors.toSet()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/validator/Rule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.validator; | ||
|
||
import io.seata.saga.statelang.domain.StateMachine; | ||
|
||
/** | ||
* Validation rule interface, use SPI to inject rules | ||
* | ||
* @author ptyin | ||
*/ | ||
public interface Rule { | ||
/** | ||
* Validate a state machine | ||
* | ||
* @param stateMachine state machine | ||
* @return true if passes, false if fails | ||
*/ | ||
boolean validate(StateMachine stateMachine); | ||
|
||
/** | ||
* Get the rule name | ||
* | ||
* @return name of the rule | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Get hints why validation passes or fails. Use this method to show more messages about validation result. | ||
* | ||
* @return hint of the rule | ||
*/ | ||
String getHint(); | ||
} |
34 changes: 34 additions & 0 deletions
34
saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/validator/RuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.validator; | ||
|
||
import io.seata.common.loader.EnhancedServiceLoader; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Factorial class to get all rules. | ||
* | ||
* @author ptyin | ||
*/ | ||
public class RuleFactory { | ||
private static final List<Rule> RULES = EnhancedServiceLoader.loadAll(Rule.class); | ||
|
||
public static List<Rule> getRules() { | ||
return RULES; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...saga-statelang/src/main/java/io/seata/saga/statelang/validator/StateMachineValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.validator; | ||
|
||
import io.seata.saga.statelang.domain.StateMachine; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* State machine validator used to validate rules. | ||
* | ||
* @author ptyin | ||
*/ | ||
public class StateMachineValidator { | ||
|
||
/** | ||
* Validate on state machine | ||
* | ||
* @param stateMachine state machine | ||
* @throws ValidationException throws if there is a validation rule failed | ||
*/ | ||
public void validate(StateMachine stateMachine) throws ValidationException { | ||
List<Rule> rules = RuleFactory.getRules(); | ||
for (Rule rule: rules) { | ||
boolean pass; | ||
try { | ||
pass = rule.validate(stateMachine); | ||
} catch (Throwable e) { | ||
throw new ValidationException(rule, "Exception occurs", e); | ||
} | ||
if (!pass) { | ||
throw new ValidationException(rule, "Failed"); | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...a-saga-statelang/src/main/java/io/seata/saga/statelang/validator/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.validator; | ||
|
||
import io.seata.common.util.StringUtils; | ||
|
||
/** | ||
* Validation exception throws if exception occurs in validation phase. | ||
* | ||
* @author ptyin | ||
*/ | ||
public class ValidationException extends RuntimeException { | ||
|
||
public ValidationException(Rule rule, String message) { | ||
super(spliceMessage(rule, message)); | ||
} | ||
|
||
public ValidationException(Rule rule, String message, Throwable cause) { | ||
super(spliceMessage(rule, message), cause); | ||
} | ||
|
||
private static String spliceMessage(Rule rule, String message) { | ||
String canonicalMessage = String.format("Rule [%s]: %s", rule.getName(), message); | ||
if (StringUtils.isNotBlank(rule.getHint())) { | ||
canonicalMessage = canonicalMessage + ", hints: " + rule.getHint(); | ||
} | ||
return canonicalMessage; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...ata-saga-statelang/src/main/java/io/seata/saga/statelang/validator/impl/AbstractRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.seata.saga.statelang.validator.impl; | ||
|
||
import io.seata.saga.statelang.validator.Rule; | ||
|
||
/** | ||
* Abstract class for {@link Rule} | ||
* | ||
* @author ptyin | ||
*/ | ||
public abstract class AbstractRule implements Rule { | ||
|
||
protected String hint; | ||
|
||
@Override | ||
public String getName() { | ||
return getClass().getSimpleName(); | ||
} | ||
|
||
@Override | ||
public String getHint() { | ||
return hint; | ||
} | ||
} |
Oops, something went wrong.