/
sync.go
397 lines (342 loc) · 11.1 KB
/
sync.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Package sync contains logic for establishing a file sync between a local machine and a Coder workspace.
package sync
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
"github.com/rjeczalik/notify"
"golang.org/x/sync/semaphore"
"golang.org/x/xerrors"
"cdr.dev/wsep"
"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/activity"
"cdr.dev/coder-cli/internal/coderutil"
"cdr.dev/coder-cli/pkg/clog"
)
// Sync runs a live sync daemon.
type Sync struct {
// Init sets whether the sync will do the initial init and then return fast.
Init bool
// LocalDir is an absolute path.
LocalDir string
// RemoteDir is an absolute path.
RemoteDir string
// DisableMetrics disables activity metric pushing.
DisableMetrics bool
Workspace coder.Workspace
Client coder.Client
OutW io.Writer
ErrW io.Writer
InputReader io.Reader
IsInteractiveOutput bool
}
// See https://lxadm.com/Rsync_exit_codes#List_of_standard_rsync_exit_codes.
const (
rsyncExitCodeIncompat = 2
rsyncExitCodeDataStream = 12
)
func (s Sync) syncPaths(delete bool, local, remote string) error {
self := os.Args[0]
args := []string{"-zz",
"-a",
"--delete",
"-e", self + " sh", local, s.Workspace.Name + ":" + remote,
}
if delete {
args = append([]string{"--delete"}, args...)
}
if os.Getenv("DEBUG_RSYNC") != "" {
args = append([]string{"--progress"}, args...)
}
// See https://unix.stackexchange.com/questions/188737/does-compression-option-z-with-rsync-speed-up-backup
// on compression level.
// (AB): compression sped up the initial sync of the enterprise repo by 30%, leading me to believe it's
// good in general for codebases.
cmd := exec.Command("rsync", args...)
cmd.Stdout = s.OutW
cmd.Stderr = ioutil.Discard
cmd.Stdin = s.InputReader
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
switch {
case exitError.ExitCode() == rsyncExitCodeIncompat:
return xerrors.Errorf("no compatible rsync on remote machine: rsync: %w", err)
case exitError.ExitCode() == rsyncExitCodeDataStream:
return xerrors.Errorf("protocol datastream error or no remote rsync found: %w", err)
}
return xerrors.Errorf("rsync: %w", err)
}
return xerrors.Errorf("rsync: %w", err)
}
return nil
}
func (s Sync) remoteCmd(ctx context.Context, prog string, args ...string) error {
conn, err := coderutil.DialWorkspaceWsep(ctx, s.Client, &s.Workspace)
if err != nil {
return xerrors.Errorf("dial executor: %w", err)
}
defer func() { _ = conn.Close(websocket.CloseNormalClosure, "") }() // Best effort.
execer := wsep.RemoteExecer(conn)
process, err := execer.Start(ctx, wsep.Command{
Command: prog,
Args: args,
})
if err != nil {
return xerrors.Errorf("exec remote process: %w", err)
}
defer process.Close()
// NOTE: If the copy routine fail, it will result in `process.Wait` to unblock and report an error.
go func() { _, _ = io.Copy(s.OutW, process.Stdout()) }() // Best effort.
go func() { _, _ = io.Copy(s.ErrW, process.Stderr()) }() // Best effort.
if err := process.Wait(); err != nil {
if code, ok := err.(wsep.ExitError); ok {
return xerrors.Errorf("%s exit status: %d", prog, code)
}
return xerrors.Errorf("execution failure: %w", err)
}
return nil
}
// initSync performs the initial synchronization of the directory.
func (s Sync) initSync() error {
clog.LogInfo(fmt.Sprintf("doing initial sync (%s -> %s)", s.LocalDir, s.RemoteDir))
start := time.Now()
// Delete old files on initial sync (e.g git checkout).
// Add the "/." to the local directory so rsync doesn't try to place the directory
// into the remote dir.
if err := s.syncPaths(true, s.LocalDir+"/.", s.RemoteDir); err != nil {
return err
}
clog.LogSuccess(
fmt.Sprintf("finished initial sync (%s)", time.Since(start).Truncate(time.Millisecond)),
)
return nil
}
func (s Sync) convertPath(local string) string {
relLocalPath, err := filepath.Rel(s.LocalDir, local)
if err != nil {
panic(err)
}
return filepath.Join(s.RemoteDir, relLocalPath)
}
func (s Sync) handleCreate(localPath string) error {
target := s.convertPath(localPath)
if err := s.syncPaths(false, localPath, target); err != nil {
// File was quickly deleted.
if _, e1 := os.Stat(localPath); os.IsNotExist(e1) { // NOTE: Discard any other stat error and just expose the syncPath one.
return nil
}
return err
}
return nil
}
func (s Sync) handleDelete(localPath string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
return s.remoteCmd(ctx, "rm", "-rf", s.convertPath(localPath))
}
func (s Sync) handleRename(localPath string) error {
// The rename operation is sent in two events, one
// for the old (gone) file and one for the new file.
// Catching both would require complex state.
// Instead, we turn it into a Create or Delete based
// on file existence.
info, err := os.Stat(localPath)
if err != nil {
if os.IsNotExist(err) {
return s.handleDelete(localPath)
}
return err
}
if info.IsDir() {
// Without this, the directory will be created as a subdirectory.
localPath += "/."
}
return s.handleCreate(localPath)
}
func (s Sync) work(ev timedEvent) {
var (
localPath = ev.Path()
err error
)
switch ev.Event() {
case notify.Write, notify.Create:
err = s.handleCreate(localPath)
case notify.Rename:
err = s.handleRename(localPath)
case notify.Remove:
err = s.handleDelete(localPath)
default:
clog.LogInfo(fmt.Sprintf("unhandled event %+v %s", ev.Event(), ev.Path()))
}
log := fmt.Sprintf("%v %s (%s)",
ev.Event(), filepath.Base(localPath), time.Since(ev.CreatedAt).Truncate(time.Millisecond*10),
)
if err != nil {
clog.Log(clog.Error(fmt.Sprintf("%s: %s", log, err)))
} else {
clog.LogSuccess(log)
}
}
// ErrRestartSync describes a known error case that can be solved by re-starting the command.
var ErrRestartSync = errors.New("the sync exited because it was overloaded, restart it")
// workEventGroup converges a group of events to prevent duplicate work.
func (s Sync) workEventGroup(evs []timedEvent) {
cache := eventCache{}
for _, ev := range evs {
cache.Add(ev)
}
// We want to process events concurrently but safely for speed.
// Because the event cache prevents duplicate events for the same file, race conditions of that type
// are impossible.
// What is possible is a dependency on a previous Rename or Create. For example, if a directory is renamed
// and then a file is moved to it. AFAIK this dependecy only exists with Directories.
// So, we sequentially process the list of directory Renames and Creates, and then concurrently
// perform all Writes.
for _, ev := range cache.SequentialEvents() {
s.work(ev)
}
sem := semaphore.NewWeighted(8)
var wg sync.WaitGroup
for _, ev := range cache.ConcurrentEvents() {
setConsoleTitle(fmtUpdateTitle(ev.Path()), s.IsInteractiveOutput)
wg.Add(1)
// TODO: Document why this error is discarded. See https://github.com/cdr/coder-cli/issues/122 for reference.
_ = sem.Acquire(context.Background(), 1)
ev := ev // Copy the event in the scope to make sure the go routine use the proper value.
go func() {
defer sem.Release(1)
defer wg.Done()
s.work(ev)
}()
}
wg.Wait()
}
const (
// maxinflightInotify sets the maximum number of inotifies before the
// sync just restarts. Syncing a large amount of small files (e.g .git
// or node_modules) is impossible to do performantly with individual
// rsyncs.
maxInflightInotify = 8
maxEventDelay = 7 * time.Second
// maxAcceptableDispatch is the maximum amount of time before an event
// should begin its journey to the server. This sets a lower bound for
// perceivable latency, but the higher it is, the better the
// optimization.
maxAcceptableDispatch = 50 * time.Millisecond
)
// Version returns remote protocol version as a string.
// Or, an error if one exists.
func (s Sync) Version() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
conn, err := coderutil.DialWorkspaceWsep(ctx, s.Client, &s.Workspace)
if err != nil {
return "", xerrors.Errorf("dial workspace executor: %w", err)
}
defer func() { _ = conn.Close(websocket.CloseNormalClosure, "") }() // Best effort.
execer := wsep.RemoteExecer(conn)
process, err := execer.Start(ctx, wsep.Command{
Command: "rsync",
Args: []string{"--version"},
})
if err != nil {
return "", err
}
defer process.Close()
buf := &bytes.Buffer{}
_, _ = io.Copy(buf, process.Stdout()) // Ignore error, if any, it would be handled by the process.Wait return.
if err := process.Wait(); err != nil {
return "", err
}
firstLine, err := buf.ReadString('\n')
if err != nil {
return "", err
}
versionString := strings.Split(firstLine, "protocol version ")
return versionString[1], nil
}
// Run starts the sync synchronously.
// Use this command to debug what wasn't sync'd correctly:
// rsync -e "coder sh" -nicr ~/Projects/cdr/coder-cli/. ammar:/home/coder/coder-cli/.
func (s Sync) Run() error {
events := make(chan notify.EventInfo, maxInflightInotify)
// Set up a recursive watch.
// We do this before the initial sync so we can capture any changes
// that may have happened during sync.
if err := notify.Watch(path.Join(s.LocalDir, "..."), events, notify.All); err != nil {
return xerrors.Errorf("create watch: %w", err)
}
defer notify.Stop(events)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if err := s.remoteCmd(ctx, "mkdir", "-p", s.RemoteDir); err != nil {
return xerrors.Errorf("create remote directory: %w", err)
}
ap := activity.NewPusher(s.Client, s.Workspace.ID, activityName)
ap.Push(ctx)
setConsoleTitle("⏳ syncing project", s.IsInteractiveOutput)
if err := s.initSync(); err != nil {
return err
}
if s.Init {
return nil
}
clog.LogInfo(fmt.Sprintf("watching %s for changes", s.LocalDir))
var droppedEvents uint64
// Timed events lets us track how long each individual file takes to
// update.
timedEvents := make(chan timedEvent, cap(events))
go func() {
defer close(timedEvents)
for event := range events {
select {
case timedEvents <- timedEvent{
CreatedAt: time.Now(),
EventInfo: event,
}:
default:
if atomic.AddUint64(&droppedEvents, 1) == 1 {
clog.LogInfo("dropped event, sync should restart soon")
}
}
}
}()
var eventGroup []timedEvent
dispatchEventGroup := time.NewTicker(maxAcceptableDispatch)
defer dispatchEventGroup.Stop()
for {
const watchingFilesystemTitle = "🛰 watching filesystem"
setConsoleTitle(watchingFilesystemTitle, s.IsInteractiveOutput)
select {
case ev := <-timedEvents:
if atomic.LoadUint64(&droppedEvents) > 0 {
return ErrRestartSync
}
eventGroup = append(eventGroup, ev)
case <-dispatchEventGroup.C:
if len(eventGroup) == 0 {
continue
}
// We're too backlogged and should restart the sync.
if time.Since(eventGroup[0].CreatedAt) > maxEventDelay {
return ErrRestartSync
}
s.workEventGroup(eventGroup)
eventGroup = eventGroup[:0]
ap.Push(context.TODO())
}
}
}
const activityName = "sync"