-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an abstraction layer for state machine handling to avoid duplicat…
…e code
- Loading branch information
1 parent
14ef711
commit 5400eb8
Showing
14 changed files
with
138 additions
and
82 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
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
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
65 changes: 65 additions & 0 deletions
65
data-node/src/main/java/org/graylog/datanode/process/statemachine/ProcessStateMachine.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,65 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
package org.graylog.datanode.process.statemachine; | ||
|
||
import com.github.oxo42.stateless4j.StateMachine; | ||
import com.github.oxo42.stateless4j.StateMachineConfig; | ||
import org.graylog.datanode.process.statemachine.tracer.StateMachineTracer; | ||
import org.graylog.datanode.process.statemachine.tracer.StateMachineTracerAggregator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Set; | ||
|
||
public abstract class ProcessStateMachine<STATE, EVENT> extends StateMachine<STATE, EVENT> { | ||
|
||
private final Logger LOG = LoggerFactory.getLogger(ProcessStateMachine.class); | ||
|
||
private final StateMachineTracerAggregator<STATE, EVENT> tracerAggregator = new StateMachineTracerAggregator<>(); | ||
|
||
public ProcessStateMachine(STATE initialState, | ||
StateMachineConfig<STATE, EVENT> config, | ||
Set<StateMachineTracer<STATE, EVENT>> tracer) { | ||
super(initialState, config); | ||
addTracer(tracer); | ||
setTrace(tracerAggregator); | ||
} | ||
|
||
private void addTracer(Set<StateMachineTracer<STATE, EVENT>> tracers) { | ||
tracers.forEach(tracer -> { | ||
tracer.setStateMachine(this); | ||
tracerAggregator.addTracer(tracer); | ||
}); | ||
} | ||
|
||
protected abstract EVENT getErrorEvent(); | ||
|
||
private void fire(EVENT trigger, EVENT errorEvent) { | ||
try { | ||
super.fire(trigger); | ||
} catch (Exception e) { | ||
LOG.error("Failed to fire event " + trigger, e); | ||
super.fire(errorEvent); | ||
} | ||
} | ||
|
||
@Override | ||
public void fire(EVENT trigger) { | ||
fire(trigger, getErrorEvent()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...de/src/main/java/org/graylog/datanode/process/statemachine/tracer/StateMachineTracer.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,26 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
package org.graylog.datanode.process.statemachine.tracer; | ||
|
||
import com.github.oxo42.stateless4j.StateMachine; | ||
import com.github.oxo42.stateless4j.delegates.Trace; | ||
|
||
public interface StateMachineTracer<STATE, EVENT> extends Trace<STATE, EVENT> { | ||
|
||
default void setStateMachine(StateMachine<STATE, EVENT> stateMachine) { | ||
} | ||
} |
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
Oops, something went wrong.