Skip to content

Make event handling sequential and set the correct timestamp #4644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions pkg/skaffold/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sync"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"

sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand All @@ -40,18 +41,36 @@ const (
Terminated = "Terminated"
)

var handler = &eventHandler{}
var handler = newHandler()

func newHandler() *eventHandler {
h := &eventHandler{
eventChan: make(chan firedEvent),
}
go func() {
for {
ev := <-h.eventChan
h.handleExec(ev)
}
}()
return h
}

type eventHandler struct {
eventLog []proto.LogEntry
logLock sync.Mutex

state proto.State
stateLock sync.Mutex

eventChan chan firedEvent
listeners []*listener
}

type firedEvent struct {
event *proto.Event
ts *timestamp.Timestamp
}

type listener struct {
callback func(*proto.LogEntry) error
errors chan error
Expand Down Expand Up @@ -361,7 +380,7 @@ func FileSyncSucceeded(fileCount int, image string) {

// PortForwarded notifies that a remote port has been forwarded locally.
func PortForwarded(localPort, remotePort int32, podName, containerName, namespace string, portName string, resourceType, resourceName, address string) {
go handler.handle(&proto.Event{
handler.handle(&proto.Event{
EventType: &proto.Event_PortEvent{
PortEvent: &proto.PortEvent{
LocalPort: localPort,
Expand All @@ -380,7 +399,7 @@ func PortForwarded(localPort, remotePort int32, podName, containerName, namespac

// DebuggingContainerStarted notifies that a debuggable container has appeared.
func DebuggingContainerStarted(podName, containerName, namespace, artifact, runtime, workingDir string, debugPorts map[string]uint32) {
go handler.handle(&proto.Event{
handler.handle(&proto.Event{
EventType: &proto.Event_DebuggingContainerEvent{
DebuggingContainerEvent: &proto.DebuggingContainerEvent{
Status: Started,
Expand All @@ -398,7 +417,7 @@ func DebuggingContainerStarted(podName, containerName, namespace, artifact, runt

// DebuggingContainerTerminated notifies that a debuggable container has disappeared.
func DebuggingContainerTerminated(podName, containerName, namespace, artifact, runtime, workingDir string, debugPorts map[string]uint32) {
go handler.handle(&proto.Event{
handler.handle(&proto.Event{
EventType: &proto.Event_DebuggingContainerEvent{
DebuggingContainerEvent: &proto.DebuggingContainerEvent{
Status: Terminated,
Expand All @@ -421,47 +440,47 @@ func (ev *eventHandler) setState(state proto.State) {
}

func (ev *eventHandler) handleDeployEvent(e *proto.DeployEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_DeployEvent{
DeployEvent: e,
},
})
}

func (ev *eventHandler) handleStatusCheckEvent(e *proto.StatusCheckEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_StatusCheckEvent{
StatusCheckEvent: e,
},
})
}

func (ev *eventHandler) handleResourceStatusCheckEvent(e *proto.ResourceStatusCheckEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_ResourceStatusCheckEvent{
ResourceStatusCheckEvent: e,
},
})
}

func (ev *eventHandler) handleBuildEvent(e *proto.BuildEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_BuildEvent{
BuildEvent: e,
},
})
}

func (ev *eventHandler) handleDevLoopEvent(e *proto.DevLoopEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_DevLoopEvent{
DevLoopEvent: e,
},
})
}

func (ev *eventHandler) handleFileSyncEvent(e *proto.FileSyncEvent) {
go ev.handle(&proto.Event{
ev.handle(&proto.Event{
EventType: &proto.Event_FileSyncEvent{
FileSyncEvent: e,
},
Expand All @@ -484,12 +503,21 @@ func LogMetaEvent() {
}

func (ev *eventHandler) handle(event *proto.Event) {
go func(t *timestamp.Timestamp) {
ev.eventChan <- firedEvent{
event: event,
ts: t,
}
}(ptypes.TimestampNow())
}

func (ev *eventHandler) handleExec(f firedEvent) {
logEntry := &proto.LogEntry{
Timestamp: ptypes.TimestampNow(),
Event: event,
Timestamp: f.ts,
Event: f.event,
}

switch e := event.GetEventType().(type) {
switch e := f.event.GetEventType().(type) {
case *proto.Event_BuildEvent:
be := e.BuildEvent
ev.stateLock.Lock()
Expand Down
Loading