forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write workflow and node execution events asynchronously (flyteorg#174)
- Loading branch information
Katrina Rogan
authored
Apr 9, 2021
1 parent
af505fb
commit 40d6d5c
Showing
34 changed files
with
746 additions
and
347 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
pkg/async/events/implementations/node_execution_event_writer.go
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,45 @@ | ||
package implementations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/flyteorg/flyteadmin/pkg/async/events/interfaces" | ||
"github.com/flyteorg/flyteadmin/pkg/repositories" | ||
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flytestdlib/logger" | ||
) | ||
|
||
// This event writer acts to asynchronously persist node execution events. As flytepropeller sends node | ||
// events, node execution processing doesn't have to wait on these to be committed. | ||
type nodeExecutionEventWriter struct { | ||
db repositories.RepositoryInterface | ||
events chan admin.NodeExecutionEventRequest | ||
} | ||
|
||
func (w *nodeExecutionEventWriter) Write(event admin.NodeExecutionEventRequest) { | ||
w.events <- event | ||
} | ||
|
||
func (w *nodeExecutionEventWriter) Run() { | ||
for event := range w.events { | ||
eventModel, err := transformers.CreateNodeExecutionEventModel(event) | ||
if err != nil { | ||
logger.Warnf(context.TODO(), "Failed to transform event [%+v] to database model with err [%+v]", event, err) | ||
continue | ||
} | ||
err = w.db.NodeExecutionEventRepo().Create(context.TODO(), *eventModel) | ||
if err != nil { | ||
// It's okay to be lossy here. These events aren't used to fetch execution state but rather as a convenience | ||
// to replay and understand the event execution timeline. | ||
logger.Warnf(context.TODO(), "Failed to write event [%+v] to database with err [%+v]", event, err) | ||
} | ||
} | ||
} | ||
|
||
func NewNodeExecutionEventWriter(db repositories.RepositoryInterface, bufferSize int) interfaces.NodeExecutionEventWriter { | ||
return &nodeExecutionEventWriter{ | ||
db: db, | ||
events: make(chan admin.NodeExecutionEventRequest, bufferSize), | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
pkg/async/events/implementations/node_execution_event_writer_test.go
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,37 @@ | ||
package implementations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/flyteorg/flyteadmin/pkg/repositories/mocks" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" | ||
event2 "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event" | ||
) | ||
|
||
func TestNodeExecutionEventWriter(t *testing.T) { | ||
db := mocks.NewMockRepository() | ||
|
||
event := admin.NodeExecutionEventRequest{ | ||
RequestId: "request_id", | ||
Event: &event2.NodeExecutionEvent{ | ||
Id: &core.NodeExecutionIdentifier{ | ||
NodeId: "node_id", | ||
ExecutionId: &core.WorkflowExecutionIdentifier{ | ||
Project: "project", | ||
Domain: "domain", | ||
Name: "exec_name", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
nodeExecEventRepo := mocks.NodeExecutionEventRepoInterface{} | ||
nodeExecEventRepo.On("Create", event).Return(nil) | ||
db.(*mocks.MockRepository).NodeExecutionEventRepoIface = &nodeExecEventRepo | ||
writer := NewNodeExecutionEventWriter(db, 100) | ||
// Assert we can write an event using the buffered channel without holding up this process. | ||
writer.Write(event) | ||
go func() { writer.Run() }() | ||
close(writer.(*nodeExecutionEventWriter).events) | ||
} |
45 changes: 45 additions & 0 deletions
45
pkg/async/events/implementations/workflow_execution_event_writer.go
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,45 @@ | ||
package implementations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/flyteorg/flyteadmin/pkg/async/events/interfaces" | ||
"github.com/flyteorg/flyteadmin/pkg/repositories" | ||
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flytestdlib/logger" | ||
) | ||
|
||
// This event writer acts to asynchronously persist workflow execution events. As flytepropeller sends workflow | ||
// events, workflow execution processing doesn't have to wait on these to be committed. | ||
type workflowExecutionEventWriter struct { | ||
db repositories.RepositoryInterface | ||
events chan admin.WorkflowExecutionEventRequest | ||
} | ||
|
||
func (w *workflowExecutionEventWriter) Write(event admin.WorkflowExecutionEventRequest) { | ||
w.events <- event | ||
} | ||
|
||
func (w *workflowExecutionEventWriter) Run() { | ||
for event := range w.events { | ||
eventModel, err := transformers.CreateExecutionEventModel(event) | ||
if err != nil { | ||
logger.Warnf(context.TODO(), "Failed to transform event [%+v] to database model with err [%+v]", event, err) | ||
continue | ||
} | ||
err = w.db.ExecutionEventRepo().Create(context.TODO(), *eventModel) | ||
if err != nil { | ||
// It's okay to be lossy here. These events aren't used to fetch execution state but rather as a convenience | ||
// to replay and understand the event execution timeline. | ||
logger.Warnf(context.TODO(), "Failed to write event [%+v] to database with err [%+v]", event, err) | ||
} | ||
} | ||
} | ||
|
||
func NewWorkflowExecutionEventWriter(db repositories.RepositoryInterface, bufferSize int) interfaces.WorkflowExecutionEventWriter { | ||
return &workflowExecutionEventWriter{ | ||
db: db, | ||
events: make(chan admin.WorkflowExecutionEventRequest, bufferSize), | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
pkg/async/events/implementations/workflow_execution_event_writer_test.go
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 @@ | ||
package implementations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/flyteorg/flyteadmin/pkg/repositories/mocks" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" | ||
event2 "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event" | ||
) | ||
|
||
func TestWorkflowExecutionEventWriter(t *testing.T) { | ||
db := mocks.NewMockRepository() | ||
|
||
event := admin.WorkflowExecutionEventRequest{ | ||
RequestId: "request_id", | ||
Event: &event2.WorkflowExecutionEvent{ | ||
ExecutionId: &core.WorkflowExecutionIdentifier{ | ||
Project: "project", | ||
Domain: "domain", | ||
Name: "exec_name", | ||
}, | ||
}, | ||
} | ||
|
||
workflowExecEventRepo := mocks.ExecutionEventRepoInterface{} | ||
workflowExecEventRepo.On("Create", event).Return(nil) | ||
db.(*mocks.MockRepository).ExecutionEventRepoIface = &workflowExecEventRepo | ||
writer := NewWorkflowExecutionEventWriter(db, 100) | ||
// Assert we can write an event using the buffered channel without holding up this process. | ||
writer.Write(event) | ||
go func() { writer.Run() }() | ||
close(writer.(*workflowExecutionEventWriter).events) | ||
} |
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,12 @@ | ||
package interfaces | ||
|
||
import ( | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
) | ||
|
||
//go:generate mockery -name=NodeExecutionEventWriter -output=../mocks -case=underscore | ||
|
||
type NodeExecutionEventWriter interface { | ||
Run() | ||
Write(nodeExecutionEvent admin.NodeExecutionEventRequest) | ||
} |
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,12 @@ | ||
package interfaces | ||
|
||
import ( | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" | ||
) | ||
|
||
//go:generate mockery -name=WorkflowExecutionEventWriter -output=../mocks -case=underscore | ||
|
||
type WorkflowExecutionEventWriter interface { | ||
Run() | ||
Write(workflowExecutionEvent admin.WorkflowExecutionEventRequest) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.