-
Notifications
You must be signed in to change notification settings - Fork 291
/
executor_grpc_client_to_agent.go
92 lines (74 loc) · 2.09 KB
/
executor_grpc_client_to_agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package executor
import (
"fmt"
"log"
"sync"
"time"
"context"
"github.com/chrislusf/gleam/pb"
"github.com/chrislusf/gleam/util"
"google.golang.org/grpc"
)
func (exe *Executor) statusHeartbeat(wg *sync.WaitGroup, finishedChan chan bool) {
defer wg.Done()
err := withClient(exe.Option.AgentAddress, func(client pb.GleamAgentClient) error {
stream, err := client.CollectExecutionStatistics(context.Background(), grpc.FailFast(false))
if err != nil {
return fmt.Errorf("executor => agent %v : %v", exe.Option.AgentAddress, err)
}
tickChan := time.Tick(1 * time.Second)
stat := &pb.ExecutionStat{
FlowHashCode: exe.instructions.FlowHashCode,
Stats: exe.stats,
}
for {
select {
case <-tickChan:
if err := stream.Send(stat); err != nil {
return fmt.Errorf("executor Send(%v): %v", exe.stats, err)
}
case <-finishedChan:
stream.CloseSend()
return nil
}
}
})
if err != nil {
log.Printf("executor heartbeat to agent %v: %v", exe.Option.AgentAddress, err)
}
}
func (exe *Executor) reportStatus() {
err := withClient(exe.Option.AgentAddress, func(client pb.GleamAgentClient) error {
stream, err := client.CollectExecutionStatistics(context.Background(), grpc.FailFast(false))
if err != nil {
return fmt.Errorf("executor => agent %v : %v", exe.Option.AgentAddress, err)
}
// defer stream.CloseSend()
stat := &pb.ExecutionStat{
FlowHashCode: exe.instructions.FlowHashCode,
Stats: exe.stats,
}
if err := stream.Send(stat); err != nil {
return fmt.Errorf("%v.Send(%v) = %v", stream, exe.stats, err)
}
return nil
})
if err != nil {
log.Printf("executor reportStatus to %v: %v", exe.Option.AgentAddress, err)
}
}
func withClient(server string, fn func(client pb.GleamAgentClient) error) error {
grpcConnection, err := util.GleamGrpcDial(server,
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
return fmt.Errorf("executor dial agent: %v", err)
}
defer func() {
time.Sleep(50 * time.Millisecond)
grpcConnection.Close()
}()
client := pb.NewGleamAgentClient(grpcConnection)
return fn(client)
}