-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from spiral/feature/fsm
Feature/fsm
- Loading branch information
Showing
30 changed files
with
1,430 additions
and
891 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Spiral Scout | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
test: | ||
go test -v -race ./tests/backoff | ||
go test -v -race ./tests/happy_scenarios | ||
go test -v -race ./tests/interfaces | ||
go test -v -race ./tests/issues | ||
go test -v -race ./tests/stress | ||
go test -v -race ./tests/disabled_vertices | ||
go test -v -race -tags=debug ./tests/backoff | ||
go test -v -race -tags=debug ./tests/happy_scenarios | ||
go test -v -race -tags=debug ./tests/interfaces | ||
go test -v -race -tags=debug ./tests/issues | ||
go test -v -race -tags=debug ./tests/stress | ||
go test -v -race -tags=debug ./tests/disabled_vertices |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package endure | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/spiral/errors" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (e *Endure) sendResultToUser(res *result) { | ||
e.userResultsCh <- &Result{ | ||
Error: res.err, | ||
VertexID: res.vertexID, | ||
} | ||
} | ||
|
||
// traverseBackStop used to visit every Prev node and internalStop vertices | ||
func (e *Endure) traverseBackStop(n *DllNode) { | ||
const op = errors.Op("traverse_back_stop") | ||
e.logger.Debug("stopping vertex in the first Serve call", zap.String("vertex id", n.Vertex.ID)) | ||
nCopy := n | ||
err := e.shutdown(nCopy, false) | ||
if err != nil { | ||
nCopy.Vertex.SetState(Error) | ||
// ignore errors from internal_stop | ||
e.logger.Error("failed to traverse vertex back", zap.String("vertex id", nCopy.Vertex.ID), zap.Error(errors.E(op, err))) | ||
} | ||
} | ||
|
||
func (e *Endure) retryHandler(res *result) { | ||
const op = errors.Op("internal_retry_handler") | ||
// get vertex from the graph | ||
vertex := e.graph.GetVertex(res.vertexID) | ||
if vertex == nil { | ||
e.logger.Error("failed to get vertex from the graph, vertex is nil", zap.String("vertex id from the handleErrorCh channel", res.vertexID)) | ||
e.userResultsCh <- &Result{ | ||
Error: errors.E(op, errors.Traverse, errors.Str("failed to get vertex from the graph, vertex is nil")), | ||
VertexID: res.vertexID, | ||
} | ||
return | ||
} | ||
|
||
// stop without setting Stopped state to the Endure | ||
n := e.runList.Head | ||
err := e.shutdown(n, true) | ||
if err != nil { | ||
e.logger.Error("error happened during shutdown", zap.Error(err)) | ||
} | ||
|
||
// reset vertex and dependencies to the initial state | ||
// numOfDeps and visited/visiting | ||
vertices := e.graph.Reset(vertex) | ||
|
||
// Topologically sort the graph | ||
sorted, err := TopologicalSort(vertices) | ||
if err != nil { | ||
e.logger.Error("error sorting the graph", zap.Error(err)) | ||
return | ||
} | ||
if sorted == nil { | ||
e.logger.Error("sorted list should not be nil", zap.String("vertex id from the handleErrorCh channel", res.vertexID)) | ||
e.userResultsCh <- &Result{ | ||
Error: errors.E(op, errors.Traverse, errors.Str("failed to topologically sort the graph")), | ||
VertexID: res.vertexID, | ||
} | ||
return | ||
} | ||
|
||
// Init backoff | ||
b := backoff.NewExponentialBackOff() | ||
b.MaxElapsedTime = e.maxInterval | ||
b.InitialInterval = e.initialInterval | ||
|
||
affectedRunList := NewDoublyLinkedList() | ||
for i := 0; i <= len(sorted)-1; i++ { | ||
affectedRunList.Push(sorted[i]) | ||
} | ||
|
||
// call internal_init | ||
headCopy := affectedRunList.Head | ||
for headCopy != nil { | ||
berr := backoff.Retry(e.backoffInit(headCopy.Vertex), b) | ||
if berr != nil { | ||
e.logger.Error("backoff failed", zap.String("vertex id", headCopy.Vertex.ID), zap.Error(berr)) | ||
e.userResultsCh <- &Result{ | ||
Error: errors.E(op, errors.FunctionCall, errors.Errorf("error during the Init function call")), | ||
VertexID: headCopy.Vertex.ID, | ||
} | ||
return | ||
} | ||
headCopy = headCopy.Next | ||
} | ||
|
||
// call serveInternal | ||
headCopy = affectedRunList.Head | ||
for headCopy != nil { | ||
err := e.serveInternal(headCopy) | ||
if err != nil { | ||
e.userResultsCh <- &Result{ | ||
Error: errors.E(op, errors.FunctionCall, errors.Errorf("error during the Serve function call")), | ||
VertexID: headCopy.Vertex.ID, | ||
} | ||
e.logger.Error("fatal error during the serveInternal in the main thread", zap.String("vertex id", headCopy.Vertex.ID), zap.Error(err)) | ||
return | ||
} | ||
headCopy = headCopy.Next | ||
} | ||
|
||
e.sendResultToUser(res) | ||
} | ||
|
||
func (e *Endure) backoffInit(v *Vertex) func() error { | ||
return func() error { | ||
const op = errors.Op("internal_backoff_init") | ||
// we already checked the Interface satisfaction | ||
// at this step absence of Init() is impossible | ||
init, _ := reflect.TypeOf(v.Iface).MethodByName(InitMethodName) | ||
v.SetState(Initializing) | ||
err := e.callInitFn(init, v) | ||
if err != nil { | ||
v.SetState(Error) | ||
e.logger.Error("error occurred during the call INIT function", zap.String("vertex id", v.ID), zap.Error(err)) | ||
return errors.E(op, errors.FunctionCall, err) | ||
} | ||
|
||
v.SetState(Initialized) | ||
return nil | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package endure | ||
|
||
/* | ||
MIT License | ||
Copyright (c) 2020 Spiral Scout | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ |
Oops, something went wrong.