Files
archie/internal/infrastructure/staterpc/task_logs.go
T

157 lines
5.2 KiB
Go

package staterpc
import (
"context"
"errors"
"io"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/logging"
)
type taskLogServer struct {
pb.UnimplementedTaskLogServiceServer
*server
}
func (s *server) taskLogs() (storecontract.TaskLogStore, error) {
if s.deps.TaskLogs == nil {
return nil, errTaskLogsUnavailable
}
return s.deps.TaskLogs, nil
}
func (s *taskLogServer) ReadTaskLog(ctx context.Context, r *pb.ReadTaskLogRequest) (*pb.ReadTaskLogResponse, error) {
logs, err := s.taskLogs()
if err != nil {
return nil, err
}
page, err := logs.TaskLog(ctx, r.TaskId, int(r.Attempt), taskLogQueryValue(r))
if err != nil {
// A store that cannot read its own state directory keeps its own
// identity: this is unavailability, not a missing log, and a caller
// has to be able to tell them apart.
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
return nil, errTaskLogsUnavailable
}
return nil, s.logErr("ReadTaskLog", err)
}
if !page.Found {
return &pb.ReadTaskLogResponse{Attempt: r.Attempt}, nil
}
return &pb.ReadTaskLogResponse{
Entries: mapValues(page.Entries, taskLogEntryProto),
Truncated: page.Truncated,
MoreAvailable: page.MoreAvailable,
Cursor: page.Cursor,
Attempt: r.Attempt,
Found: true,
Components: page.Components,
File: page.File,
}, nil
}
// StreamTaskLogContent sends one attempt's log verbatim, one chunk per
// message, for a download. A single reply reports found=false with no chunks:
// the attempt has no log file, which is a normal state rather than a failure.
func (s *taskLogServer) StreamTaskLogContent(r *pb.StreamTaskLogContentRequest, stream pb.TaskLogService_StreamTaskLogContentServer) error {
logs, err := s.taskLogs()
if err != nil {
return err
}
// The chunking the client sees is owned by the reader that writes into
// this buffer, so the wire never holds a whole log to re-slice it.
buf := &chunkWriter{stream: stream, attempt: r.Attempt}
found, err := logs.TaskLogContent(stream.Context(), r.TaskId, int(r.Attempt), buf)
if err != nil {
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
return errTaskLogsUnavailable
}
return s.logErr("StreamTaskLogContent", err)
}
if err := buf.flush(); err != nil {
return err
}
// A found log with no bytes still reports found: an attempt whose file is
// empty did produce a log, and found is what tells the download it may
// write an empty file rather than answer 404.
if found && buf.sent {
return nil
}
return stream.Send(&pb.StreamTaskLogContentResponse{Attempt: r.Attempt, Found: found})
}
// chunkWriter packs everything a log reader writes into stream-sized messages
// on its way through. It exists so the read can be one io.CopyBuffer of the
// file into this writer: the reader keeps its own chunk size, and the bytes
// are never all held at once.
type chunkWriter struct {
stream pb.TaskLogService_StreamTaskLogContentServer
attempt int64
pending []byte
// sent records whether any chunk reached the client, which is how the
// caller tells "the file was empty" from "the file was read".
sent bool
}
// TaskLog reads one task attempt's decoded log over the wire. found=false with
// a nil error is the attempt-has-no-log answer; a process with no reader at
// all comes back as an unavailable error, not as an empty page, so a caller
// can tell the two apart.
func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error) {
r, err := c.taskLog.ReadTaskLog(ctx, taskLogRequestProto(taskID, attempt, q))
if err != nil {
return logging.TaskLogPage{}, unmapError(err)
}
if !r.Found {
return logging.TaskLogPage{
Entries: []logging.Entry{},
Components: []string{},
}, nil
}
// Both collections are normalised: protobuf decodes an empty `repeated`
// field to nil, and a nil slice marshals to JSON null rather than an empty
// list, so the field's type would otherwise depend on whether it happened
// to have contents. mapValues owns the entries side.
components := r.Components
if components == nil {
components = []string{}
}
return logging.TaskLogPage{
Entries: mapValues(r.Entries, taskLogEntryValue),
Truncated: r.Truncated,
MoreAvailable: r.MoreAvailable,
Cursor: r.Cursor,
File: r.File,
Found: true,
Components: components,
}, nil
}
// TaskLogContent streams one attempt's log verbatim into w, for a download.
// found is false, with a nil error, when the attempt has no log file.
func (c *Client) TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error) {
stream, err := c.taskLog.StreamTaskLogContent(ctx, &pb.StreamTaskLogContentRequest{TaskId: taskID, Attempt: int64(attempt)})
if err != nil {
return false, unmapError(err)
}
found := false
for {
r, err := stream.Recv()
if errors.Is(err, io.EOF) {
return found, nil
}
if err != nil {
return found, unmapError(err)
}
found = found || r.Found
if len(r.Chunk) == 0 {
continue
}
if _, err := w.Write(r.Chunk); err != nil {
return found, err
}
}
}