| 1 | package valley |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os/exec" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // FollowTimeout bounds one `valley telemetry follow`. The host ends a follow |
| 16 | // after its follow_seconds, which is at most 300. |
| 17 | const FollowTimeout = 6 * time.Minute |
| 18 | |
| 19 | // maxTelemetryLine caps one line of a follow's answer; the host caps a whole |
| 20 | // answer at its response_bytes. |
| 21 | const maxTelemetryLine = 8 << 20 |
| 22 | |
| 23 | // wakeTypes are the telemetry record types after which a change can stand |
| 24 | // elsewhere: the integrator's decision on a request, stored evidence, and |
| 25 | // the end of the check worker's batch. Records of every other type, from |
| 26 | // any stream, pass without a wake. |
| 27 | var wakeTypes = map[string]bool{"decision": true, "evidence.stored": true, "batch.end": true} |
| 28 | |
| 29 | // telemetryLine is the part of a valley-telemetry/v1 answer line klaus |
| 30 | // reads: a record's type, and the cursor the end line carries. |
| 31 | type telemetryLine struct { |
| 32 | Kind string `json:"kind"` |
| 33 | Type string `json:"type"` |
| 34 | Cursor string `json:"cursor"` |
| 35 | } |
| 36 | |
| 37 | // Follow runs `valley telemetry --json follow` in dir, after since when it |
| 38 | // isn't "", until the host ends the follow. Naming no source, it takes one |
| 39 | // of the principal's follow slots on the host for every stream the |
| 40 | // principal may observe. It calls wake as each record arrives whose type |
| 41 | // can move a change: a decision, stored evidence or a batch's end. A record |
| 42 | // says only that something happened, never where a change stands. Follow |
| 43 | // returns the cursor the follow ended with, one position per stream, to |
| 44 | // resume from; an answer that doesn't end with one is an error, as is a |
| 45 | // valley without telemetry, a key without the host's observe grant, or a |
| 46 | // host whose follow slots for the principal are all taken. |
| 47 | func Follow(ctx context.Context, dir, since string, wake func()) (string, error) { |
| 48 | args := []string{"telemetry", "--json", "follow"} |
| 49 | if since != "" { |
| 50 | args = append(args, "--since", since) |
| 51 | } |
| 52 | ctx, cancel := context.WithTimeout(ctx, FollowTimeout) |
| 53 | defer cancel() |
| 54 | cmd := exec.CommandContext(ctx, "valley", args...) |
| 55 | cmd.Dir = dir |
| 56 | cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0") |
| 57 | isolate(cmd) |
| 58 | var errOut bytes.Buffer |
| 59 | cmd.Stderr = &errOut |
| 60 | out, err := cmd.StdoutPipe() |
| 61 | if err != nil { |
| 62 | return "", &Error{Verb: "telemetry", Reason: err.Error()} |
| 63 | } |
| 64 | if err := cmd.Start(); err != nil { |
| 65 | return "", &Error{Verb: "telemetry", Reason: err.Error()} |
| 66 | } |
| 67 | cursor := "" |
| 68 | sc := bufio.NewScanner(out) |
| 69 | sc.Buffer(make([]byte, 0, 64<<10), maxTelemetryLine) |
| 70 | for sc.Scan() { |
| 71 | var line telemetryLine |
| 72 | if json.Unmarshal(sc.Bytes(), &line) != nil { |
| 73 | continue |
| 74 | } |
| 75 | switch line.Kind { |
| 76 | case "record": |
| 77 | if wakeTypes[line.Type] { |
| 78 | wake() |
| 79 | } |
| 80 | case "end": |
| 81 | cursor = line.Cursor |
| 82 | } |
| 83 | } |
| 84 | // A line too long to read leaves the rest unread; valley must not block |
| 85 | // writing it. |
| 86 | _, _ = io.Copy(io.Discard, out) |
| 87 | err = cmd.Wait() |
| 88 | e := &Error{Verb: "telemetry"} |
| 89 | var exitErr *exec.ExitError |
| 90 | switch { |
| 91 | case ctx.Err() != nil && errors.Is(ctx.Err(), context.DeadlineExceeded): |
| 92 | e.Reason = fmt.Sprintf("timed out after %s", FollowTimeout) |
| 93 | case ctx.Err() != nil: |
| 94 | return "", ctx.Err() |
| 95 | case errors.As(err, &exitErr): |
| 96 | e.exited = true |
| 97 | e.Reason = shortReason(errOut.Bytes()) |
| 98 | if e.Reason == "" { |
| 99 | e.Reason = err.Error() |
| 100 | } |
| 101 | case err != nil: |
| 102 | e.Reason = err.Error() |
| 103 | case sc.Err() != nil: |
| 104 | e.Reason = "reading the follow: " + sc.Err().Error() |
| 105 | case cursor == "": |
| 106 | e.Reason = "the follow ended without a cursor" |
| 107 | default: |
| 108 | return cursor, nil |
| 109 | } |
| 110 | return "", e |
| 111 | } |