-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
220 lines (201 loc) · 6.52 KB
/
client.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2023 Block, Inc.
package compute
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/square/finch"
"github.com/square/finch/config"
"github.com/square/finch/data"
"github.com/square/finch/proto"
"github.com/square/finch/stage"
"github.com/square/finch/stats"
)
// Client is a remote Instance that sends everything to the --server specified
// on the command line. The client handles client-server communication, and it
// wraps a Local that runs stages locally.
type Client struct {
name string
addr string
// --
gds *data.Scope
client *proto.Client
}
func NewClient(name, addr string) *Client {
if !strings.HasPrefix(addr, "http://") {
addr = "http://" + addr
}
return &Client{
name: name,
addr: strings.TrimSuffix(addr, "/"),
// --
gds: data.NewScope(),
client: proto.NewClient(name, addr),
}
}
func (c *Client) Run(ctxFinch context.Context) error {
//for {
c.gds.Reset() // keep data from globally-scoped generators; delete the rest
if err := c.run(ctxFinch); err != nil {
if ctxFinch.Err() != nil {
return nil
}
log.Println(err)
time.Sleep(2 * time.Second) // prevent uncontrolled error loop
}
//}
return nil
}
func (c *Client) run(ctxFinch context.Context) error {
// ------------------------------------------------------------------
// Fetch stage fails (wait for GET /boot to return)
var cfg config.Stage
log.Printf("Waiting to boot from %s...", c.addr)
c.client.PrintErrors = false
_, body, err := c.client.Get(ctxFinch, "/boot", nil, proto.R{2 * time.Second, 1 * time.Second, -1})
if err != nil {
return err
}
c.client.PrintErrors = true
if err := json.Unmarshal(body, &cfg); err != nil {
return fmt.Errorf("cannot decode stage config file from server: %s", err)
}
stageName := cfg.Name
c.client.StageId = cfg.Id
defer func() { c.client.StageId = "" }()
fmt.Printf("#\n# %s (%s)\n#\n", stageName, cfg.Id)
// ----------------------------------------------------------------------
// Fetch all stage and trx files from server, put in local temp dir
tmpdir, err := os.MkdirTemp("", "finch")
if err != nil {
return fmt.Errorf("cannot make temp dir: %s", err)
}
if !finch.Debugging {
defer os.RemoveAll(tmpdir)
}
finch.Debug("tmp dir: %s", tmpdir)
if err := c.getTrxFiles(ctxFinch, cfg, tmpdir); err != nil {
return err
}
// ------------------------------------------------------------------
// Local boot and ack
for k := range cfg.Stats.Report {
if k == "stdout" {
continue
}
delete(cfg.Stats.Report, k)
}
cfg.Stats.Report["server"] = map[string]string{
"server": c.addr,
"client": c.name,
"stage-id": c.client.StageId,
}
stats, err := stats.NewCollector(cfg.Stats, c.name, 1)
if err != nil {
return err
}
log.Printf("[%s] Booting", stageName)
local := stage.New(cfg, c.gds, stats)
if err := local.Prepare(ctxFinch); err != nil {
log.Printf("[%s] Boot error, notifying server: %s", stageName, err)
c.client.Send(ctxFinch, "/boot", err.Error(), proto.R{500 * time.Millisecond, 100 * time.Millisecond, 3}) // don't care if this fails
return err // return original error not Send error
}
// Boot ack; don't continue on error because we're no longer in sync with server
log.Printf("[%s] Boot successful, notifying server", stageName)
if err := c.client.Send(ctxFinch, "/boot", nil, proto.R{1 * time.Second, 100 * time.Millisecond, 10}); err != nil {
log.Printf("[%s] Sending book ack to server failed: %s", stageName, err)
return err
}
// ----------------------------------------------------------------------
// Wait for run signal. This might be a little while if server is for
// other remote instances.
log.Printf("[%s] Waiting for run signal", stageName)
resp, _, err := c.client.Get(ctxFinch, "/run", nil, proto.R{60 * time.Second, 100 * time.Millisecond, 3})
if err != nil {
log.Printf("[%s] Timeout waiting for run signal after successful boot, giving up (is the server offline?)", stageName)
return err
}
if resp.StatusCode == http.StatusResetContent {
log.Printf("[%s] Boot test successful", stageName)
return nil
}
// ----------------------------------------------------------------------
// Local run and ack
ctxRun, cancelRun := context.WithCancel(ctxFinch)
doneChan := make(chan struct{})
defer close(doneChan)
lostServer := false
stageDone := false
go func() {
defer cancelRun()
for {
time.Sleep(1 * time.Second)
select {
case <-doneChan:
finch.Debug("stop check goroutine stopped")
return
default:
}
resp, _, err := c.client.Get(ctxFinch, "/ping", nil, proto.R{500 * time.Millisecond, 100 * time.Millisecond, 5})
if err != nil {
log.Printf("[%s] Lost contact with server while running, aborting", stageName)
lostServer = true
return
}
if resp.StatusCode == http.StatusResetContent {
stageDone = true
log.Printf("[%s] Server stopped stage", stageName)
return
}
}
}()
local.Run(ctxRun)
log.Printf("[%s] Run stopped: %v (lost server:%v stage stopped:%v); sending done signal to server (5s timeout)", stageName, err, lostServer, stageDone)
// Run ack; ok if this fails because we're done, nothing left to sync with server
ctxDone, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()
if err := c.client.Send(ctxDone, "/run", err, proto.R{500 * time.Millisecond, 100 * time.Millisecond, 3}); err != nil {
log.Printf("[%s] Sending done signal to server failed, ignoring: %s", stageName, err)
}
return nil
}
func (c *Client) getTrxFiles(ctxFinch context.Context, cfg config.Stage, tmpdir string) error {
trx := cfg.Trx
for i := range trx {
if config.FileExists(trx[i].File) {
log.Printf("Have local stage %s file %s; not fetching from server", cfg.Name, trx[i].File)
continue
}
log.Printf("Fetching stage %s file %s...", cfg.Name, trx[i].File)
ref := [][]string{
{"stage", cfg.Name},
{"i", fmt.Sprintf("%d", i)},
}
resp, body, err := c.client.Get(ctxFinch, "/file", ref, proto.R{5 * time.Second, 100 * time.Millisecond, 3})
if err != nil {
return err // Get retries so error is final
}
finch.Debug("%+v", resp)
filename := filepath.Join(tmpdir, filepath.Base(trx[i].File))
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0440)
if err != nil {
return err
}
if _, err := f.Write(body); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
finch.Debug("wrote %s", filename)
trx[i].File = filename
}
return nil
}