open-nomad/command/agent/fs_endpoint.go

1126 lines
27 KiB
Go
Raw Normal View History

package agent
2017-09-11 22:40:27 +00:00
//go:generate codecgen -d 101 -o fs_endpoint.generated.go fs_endpoint.go
2016-12-10 01:33:10 +00:00
import (
"bytes"
"fmt"
2016-01-14 21:35:42 +00:00
"io"
2016-07-18 16:48:29 +00:00
"math"
"net/http"
2016-07-18 16:48:29 +00:00
"os"
"path/filepath"
2016-07-19 22:58:02 +00:00
"sort"
2016-01-13 06:06:42 +00:00
"strconv"
"strings"
"sync"
2016-07-20 20:06:05 +00:00
"syscall"
2016-07-06 00:08:58 +00:00
"time"
"gopkg.in/tomb.v1"
2016-07-06 03:48:25 +00:00
"github.com/docker/docker/pkg/ioutils"
"github.com/hashicorp/nomad/acl"
2016-07-07 15:15:22 +00:00
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hpcloud/tail/watch"
2016-07-06 00:08:58 +00:00
"github.com/ugorji/go/codec"
)
2016-01-13 19:49:39 +00:00
var (
allocIDNotPresentErr = fmt.Errorf("must provide a valid alloc id")
fileNameNotPresentErr = fmt.Errorf("must provide a file name")
2016-07-18 16:48:29 +00:00
taskNotPresentErr = fmt.Errorf("must provide task name")
logTypeNotPresentErr = fmt.Errorf("must provide log type (stdout/stderr)")
clientNotRunning = fmt.Errorf("node is not running a Nomad Client")
2016-07-06 00:08:58 +00:00
invalidOrigin = fmt.Errorf("origin must be start or end")
)
const (
2016-07-10 17:55:52 +00:00
// streamFrameSize is the maximum number of bytes to send in a single frame
streamFrameSize = 64 * 1024
2016-07-06 00:08:58 +00:00
// streamHeartbeatRate is the rate at which a heartbeat will occur to detect
// a closed connection without sending any additional data
2016-07-19 17:04:57 +00:00
streamHeartbeatRate = 1 * time.Second
2016-07-06 00:08:58 +00:00
// streamBatchWindow is the window in which file content is batched before
// being flushed if the frame size has not been hit.
streamBatchWindow = 200 * time.Millisecond
2016-07-20 21:14:54 +00:00
// nextLogCheckRate is the rate at which we check for a log entry greater
// than what we are watching for. This is to handle the case in which logs
// rotate faster than we can detect and we have to rely on a normal
// directory listing.
nextLogCheckRate = 100 * time.Millisecond
2016-07-20 20:06:05 +00:00
// deleteEvent and truncateEvent are the file events that can be sent in a
// StreamFrame
2016-07-06 00:08:58 +00:00
deleteEvent = "file deleted"
truncateEvent = "file truncated"
2016-07-20 20:06:05 +00:00
// OriginStart and OriginEnd are the available parameters for the origin
// argument when streaming a file. They respectively offset from the start
// and end of a file.
OriginStart = "start"
OriginEnd = "end"
2016-01-13 19:49:39 +00:00
)
func (s *HTTPServer) FsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if s.agent.client == nil {
return nil, clientNotRunning
}
var secret string
s.parseToken(req, &secret)
var namespace string
parseNamespace(req, &namespace)
aclObj, err := s.agent.Client().ResolveToken(secret)
if err != nil {
return nil, err
}
path := strings.TrimPrefix(req.URL.Path, "/v1/client/fs/")
switch {
case strings.HasPrefix(path, "ls/"):
if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS) {
return nil, structs.ErrPermissionDenied
}
return s.DirectoryListRequest(resp, req)
case strings.HasPrefix(path, "stat/"):
if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS) {
return nil, structs.ErrPermissionDenied
}
return s.FileStatRequest(resp, req)
case strings.HasPrefix(path, "readat/"):
if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS) {
return nil, structs.ErrPermissionDenied
}
return s.FileReadAtRequest(resp, req)
2016-03-28 18:06:22 +00:00
case strings.HasPrefix(path, "cat/"):
if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS) {
return nil, structs.ErrPermissionDenied
}
return s.FileCatRequest(resp, req)
2016-07-06 00:08:58 +00:00
case strings.HasPrefix(path, "stream/"):
if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS) {
return nil, structs.ErrPermissionDenied
}
return s.Stream(resp, req)
2016-07-18 16:48:29 +00:00
case strings.HasPrefix(path, "logs/"):
// Logs can be accessed with ReadFS or ReadLogs caps
if aclObj != nil {
readfs := aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadFS)
logs := aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadLogs)
if !readfs && !logs {
return nil, structs.ErrPermissionDenied
}
}
2016-07-18 16:48:29 +00:00
return s.Logs(resp, req)
default:
return nil, CodedError(404, ErrInvalidMethod)
}
}
func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2016-01-13 06:25:12 +00:00
var allocID, path string
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/ls/"); allocID == "" {
2016-01-13 19:49:39 +00:00
return nil, allocIDNotPresentErr
}
2016-01-13 06:25:12 +00:00
if path = req.URL.Query().Get("path"); path == "" {
path = "/"
}
2016-01-14 21:35:42 +00:00
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
return fs.List(path)
}
func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2016-01-13 06:25:12 +00:00
var allocID, path string
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/stat/"); allocID == "" {
2016-01-13 19:49:39 +00:00
return nil, allocIDNotPresentErr
2016-01-12 23:25:51 +00:00
}
if path = req.URL.Query().Get("path"); path == "" {
2016-01-13 19:49:39 +00:00
return nil, fileNameNotPresentErr
2016-01-12 23:25:51 +00:00
}
2016-01-14 21:35:42 +00:00
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
return fs.Stat(path)
}
func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2016-01-13 06:25:12 +00:00
var allocID, path string
var offset, limit int64
var err error
2016-01-13 06:06:42 +00:00
2016-01-13 06:25:12 +00:00
q := req.URL.Query()
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/readat/"); allocID == "" {
2016-01-13 19:49:39 +00:00
return nil, allocIDNotPresentErr
2016-01-13 06:06:42 +00:00
}
2016-01-13 06:25:12 +00:00
if path = q.Get("path"); path == "" {
2016-01-13 19:49:39 +00:00
return nil, fileNameNotPresentErr
2016-01-13 06:06:42 +00:00
}
2016-01-13 06:25:12 +00:00
if offset, err = strconv.ParseInt(q.Get("offset"), 10, 64); err != nil {
return nil, fmt.Errorf("error parsing offset: %v", err)
2016-01-13 06:06:42 +00:00
}
// Parse the limit
if limitStr := q.Get("limit"); limitStr != "" {
if limit, err = strconv.ParseInt(limitStr, 10, 64); err != nil {
return nil, fmt.Errorf("error parsing limit: %v", err)
}
2016-01-13 06:06:42 +00:00
}
2016-01-14 21:35:42 +00:00
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
2016-07-12 23:01:33 +00:00
rc, err := fs.ReadAt(path, offset)
if limit > 0 {
2016-07-12 23:01:33 +00:00
rc = &ReadCloserWrapper{
Reader: io.LimitReader(rc, limit),
Closer: rc,
}
}
2016-01-14 21:35:42 +00:00
if err != nil {
2016-01-13 06:06:42 +00:00
return nil, err
}
io.Copy(resp, rc)
2016-07-12 23:01:33 +00:00
return nil, rc.Close()
}
// ReadCloserWrapper wraps a LimitReader so that a file is closed once it has been
// read
type ReadCloserWrapper struct {
io.Reader
io.Closer
}
2016-03-28 18:06:22 +00:00
func (s *HTTPServer) FileCatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var allocID, path string
var err error
q := req.URL.Query()
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/cat/"); allocID == "" {
return nil, allocIDNotPresentErr
}
if path = q.Get("path"); path == "" {
return nil, fileNameNotPresentErr
}
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
fileInfo, err := fs.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.IsDir {
2016-04-19 01:53:05 +00:00
return nil, fmt.Errorf("file %q is a directory", path)
2016-03-28 18:06:22 +00:00
}
2016-07-06 00:08:58 +00:00
r, err := fs.ReadAt(path, int64(0))
2016-06-16 21:32:07 +00:00
if err != nil {
return nil, err
}
2016-03-28 18:06:22 +00:00
io.Copy(resp, r)
2016-07-12 23:01:33 +00:00
return nil, r.Close()
2016-03-28 18:06:22 +00:00
}
2016-07-06 00:08:58 +00:00
2016-12-10 01:33:10 +00:00
var (
2016-12-10 01:51:50 +00:00
// HeartbeatStreamFrame is the StreamFrame to send as a heartbeat, avoiding
// creating many instances of the empty StreamFrame
2016-12-10 01:33:10 +00:00
HeartbeatStreamFrame = &StreamFrame{}
)
2016-07-07 15:15:22 +00:00
// StreamFrame is used to frame data of a file when streaming
2016-07-06 00:08:58 +00:00
type StreamFrame struct {
2016-07-07 15:15:22 +00:00
// Offset is the offset the data was read from
2016-07-10 22:56:13 +00:00
Offset int64 `json:",omitempty"`
2016-07-07 15:15:22 +00:00
2016-07-10 22:56:13 +00:00
// Data is the read data
Data []byte `json:",omitempty"`
2016-07-07 15:15:22 +00:00
// File is the file that the data was read from
2016-07-10 22:56:13 +00:00
File string `json:",omitempty"`
2016-07-07 15:15:22 +00:00
2016-07-18 14:24:46 +00:00
// FileEvent is the last file event that occurred that could cause the
2016-07-07 15:15:22 +00:00
// streams position to change or end
2016-07-10 22:56:13 +00:00
FileEvent string `json:",omitempty"`
2016-07-06 00:08:58 +00:00
}
2016-07-10 17:55:52 +00:00
// IsHeartbeat returns if the frame is a heartbeat frame
func (s *StreamFrame) IsHeartbeat() bool {
2016-07-10 22:56:13 +00:00
return s.Offset == 0 && len(s.Data) == 0 && s.File == "" && s.FileEvent == ""
2016-07-10 17:55:52 +00:00
}
2016-12-10 01:33:10 +00:00
func (s *StreamFrame) Clear() {
s.Offset = 0
s.Data = nil
s.File = ""
s.FileEvent = ""
}
func (s *StreamFrame) IsCleared() bool {
if s.Offset != 0 {
return false
} else if s.Data != nil {
return false
} else if s.File != "" {
return false
} else if s.FileEvent != "" {
return false
} else {
return true
}
}
// StreamFramer is used to buffer and send frames as well as heartbeat.
type StreamFramer struct {
2017-01-24 00:06:31 +00:00
// plainTxt determines whether we frame or just send plain text data.
2017-01-13 21:12:36 +00:00
plainTxt bool
out io.WriteCloser
enc *codec.Encoder
encLock sync.Mutex
frameSize int
heartbeat *time.Ticker
flusher *time.Ticker
2016-07-12 16:45:05 +00:00
shutdownCh chan struct{}
exitCh chan struct{}
// The mutex protects everything below
l sync.Mutex
// The current working frame
2016-12-10 01:33:10 +00:00
f StreamFrame
data *bytes.Buffer
2016-07-18 14:24:46 +00:00
// Captures whether the framer is running and any error that occurred to
// cause it to stop.
running bool
err error
}
// NewStreamFramer creates a new stream framer that will output StreamFrames to
2017-01-24 00:06:31 +00:00
// the passed output. If plainTxt is set we do not frame and just batch plain
// text data.
func NewStreamFramer(out io.WriteCloser, plainTxt bool,
heartbeatRate, batchWindow time.Duration, frameSize int) *StreamFramer {
// Create a JSON encoder
enc := codec.NewEncoder(out, structs.JsonHandle)
// Create the heartbeat and flush ticker
2017-01-24 00:04:50 +00:00
heartbeat := time.NewTicker(heartbeatRate)
2016-07-10 17:55:52 +00:00
flusher := time.NewTicker(batchWindow)
return &StreamFramer{
2017-01-13 21:12:36 +00:00
plainTxt: plainTxt,
2016-07-12 16:45:05 +00:00
out: out,
enc: enc,
frameSize: frameSize,
heartbeat: heartbeat,
flusher: flusher,
data: bytes.NewBuffer(make([]byte, 0, 2*frameSize)),
shutdownCh: make(chan struct{}),
exitCh: make(chan struct{}),
}
}
// Destroy is used to cleanup the StreamFramer and flush any pending frames
func (s *StreamFramer) Destroy() {
s.l.Lock()
2016-07-12 16:45:05 +00:00
close(s.shutdownCh)
2017-01-24 00:04:50 +00:00
s.heartbeat.Stop()
s.flusher.Stop()
running := s.running
s.l.Unlock()
// Ensure things were flushed
if running {
<-s.exitCh
}
2016-07-19 01:41:21 +00:00
s.out.Close()
}
// Run starts a long lived goroutine that handles sending data as well as
// heartbeating
func (s *StreamFramer) Run() {
s.l.Lock()
2016-07-13 19:23:33 +00:00
defer s.l.Unlock()
2016-07-10 22:56:13 +00:00
if s.running {
return
}
s.running = true
go s.run()
}
// ExitCh returns a channel that will be closed when the run loop terminates.
func (s *StreamFramer) ExitCh() <-chan struct{} {
return s.exitCh
}
// Err returns the error that caused the StreamFramer to exit
func (s *StreamFramer) Err() error {
s.l.Lock()
defer s.l.Unlock()
return s.err
}
// run is the internal run method. It exits if Destroy is called or an error
// occurs, in which case the exit channel is closed.
func (s *StreamFramer) run() {
var err error
defer func() {
2016-07-20 22:18:54 +00:00
s.l.Lock()
2016-07-19 17:04:57 +00:00
s.running = false
s.err = err
s.l.Unlock()
close(s.exitCh)
}()
OUTER:
for {
select {
2016-07-12 16:45:05 +00:00
case <-s.shutdownCh:
break OUTER
case <-s.flusher.C:
// Skip if there is nothing to flush
s.l.Lock()
2016-12-10 01:33:10 +00:00
if s.f.IsCleared() {
s.l.Unlock()
continue
}
// Read the data for the frame, and send it
s.f.Data = s.readData()
2016-12-10 01:33:10 +00:00
err = s.send(&s.f)
s.f.Clear()
s.l.Unlock()
if err != nil {
return
}
2017-01-24 00:04:50 +00:00
case <-s.heartbeat.C:
// Send a heartbeat frame
2016-12-10 01:33:10 +00:00
if err = s.send(HeartbeatStreamFrame); err != nil {
return
}
}
}
2016-07-20 17:18:05 +00:00
s.l.Lock()
2016-12-10 01:33:10 +00:00
if !s.f.IsCleared() {
s.f.Data = s.readData()
2016-12-10 01:33:10 +00:00
err = s.send(&s.f)
s.f.Clear()
}
2016-07-19 17:04:57 +00:00
s.l.Unlock()
}
// send takes a StreamFrame, encodes and sends it
func (s *StreamFramer) send(f *StreamFrame) error {
s.encLock.Lock()
defer s.encLock.Unlock()
2017-01-13 21:12:36 +00:00
if s.plainTxt {
_, err := io.Copy(s.out, bytes.NewReader(f.Data))
return err
}
return s.enc.Encode(f)
}
2016-07-10 22:56:13 +00:00
// readData is a helper which reads the buffered data returning up to the frame
// size of data. Must be called with the lock held. The returned value is
// invalid on the next read or write into the StreamFramer buffer
func (s *StreamFramer) readData() []byte {
// Compute the amount to read from the buffer
size := s.data.Len()
2016-07-10 17:55:52 +00:00
if size > s.frameSize {
size = s.frameSize
}
2016-07-10 22:56:13 +00:00
if size == 0 {
return nil
}
2016-07-19 17:04:57 +00:00
d := s.data.Next(size)
2016-12-10 01:33:10 +00:00
return d
}
// Send creates and sends a StreamFrame based on the passed parameters. An error
// is returned if the run routine hasn't run or encountered an error. Send is
// asynchronous and does not block for the data to be transferred.
func (s *StreamFramer) Send(file, fileEvent string, data []byte, offset int64) error {
s.l.Lock()
defer s.l.Unlock()
// If we are not running, return the error that caused us to not run or
// indicated that it was never started.
if !s.running {
if s.err != nil {
return s.err
}
return fmt.Errorf("StreamFramer not running")
}
// Check if not mergeable
2016-12-10 01:33:10 +00:00
if !s.f.IsCleared() && (s.f.File != file || s.f.FileEvent != fileEvent) {
// Flush the old frame
2016-12-10 01:33:10 +00:00
s.f.Data = s.readData()
2016-07-19 17:04:57 +00:00
select {
case <-s.exitCh:
return nil
default:
}
2016-12-10 01:33:10 +00:00
err := s.send(&s.f)
s.f.Clear()
if err != nil {
return err
}
}
// Store the new data as the current frame.
2016-12-10 01:33:10 +00:00
if s.f.IsCleared() {
s.f.Offset = offset
s.f.File = file
s.f.FileEvent = fileEvent
}
// Write the data to the buffer
s.data.Write(data)
// Handle the delete case in which there is no data
2016-12-10 01:33:10 +00:00
force := false
if s.data.Len() == 0 && s.f.FileEvent != "" {
2016-12-10 01:33:10 +00:00
force = true
}
// Flush till we are under the max frame size
2016-12-10 01:33:10 +00:00
for s.data.Len() >= s.frameSize || force {
// Clear
if force {
force = false
}
// Create a new frame to send it
2016-12-10 01:33:10 +00:00
s.f.Data = s.readData()
2016-07-19 17:04:57 +00:00
select {
case <-s.exitCh:
return nil
default:
}
2016-12-10 01:33:10 +00:00
if err := s.send(&s.f); err != nil {
return err
}
2016-12-10 01:33:10 +00:00
// Update the offset
s.f.Offset += int64(len(s.f.Data))
}
if s.data.Len() == 0 {
2016-12-10 01:33:10 +00:00
s.f.Clear()
}
return nil
}
2016-07-07 15:15:22 +00:00
// Stream streams the content of a file blocking on EOF.
// The parameters are:
// * path: path to file to stream.
// * offset: The offset to start streaming data at, defaults to zero.
// * origin: Either "start" or "end" and defines from where the offset is
// applied. Defaults to "start".
2016-07-06 00:08:58 +00:00
func (s *HTTPServer) Stream(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var allocID, path string
var err error
q := req.URL.Query()
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/stream/"); allocID == "" {
return nil, allocIDNotPresentErr
}
if path = q.Get("path"); path == "" {
return nil, fileNameNotPresentErr
}
var offset int64
offsetString := q.Get("offset")
if offsetString != "" {
var err error
if offset, err = strconv.ParseInt(offsetString, 10, 64); err != nil {
return nil, fmt.Errorf("error parsing offset: %v", err)
}
}
origin := q.Get("origin")
switch origin {
case "start", "end":
case "":
origin = "start"
default:
return nil, invalidOrigin
}
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
fileInfo, err := fs.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.IsDir {
return nil, fmt.Errorf("file %q is a directory", path)
}
// If offsetting from the end subtract from the size
if origin == "end" {
offset = fileInfo.Size - offset
}
2016-07-06 03:48:25 +00:00
// Create an output that gets flushed on every write
output := ioutils.NewWriteFlusher(resp)
2016-07-18 16:48:29 +00:00
// Create the framer
2017-01-13 21:12:36 +00:00
framer := NewStreamFramer(output, false, streamHeartbeatRate, streamBatchWindow, streamFrameSize)
2016-07-18 16:48:29 +00:00
framer.Run()
defer framer.Destroy()
2016-07-22 22:07:11 +00:00
err = s.stream(offset, path, fs, framer, nil)
2016-07-22 22:01:56 +00:00
if err != nil && err != syscall.EPIPE {
return nil, err
}
return nil, nil
2016-07-07 15:15:22 +00:00
}
// parseFramerErr takes an error and returns an error. The error will
// potentially change if it was caused by the connection being closed.
func parseFramerErr(err error) error {
if err == nil {
return nil
}
2017-12-01 05:30:20 +00:00
errMsg := err.Error()
if strings.Contains(errMsg, io.ErrClosedPipe.Error()) {
// The pipe check is for tests
return syscall.EPIPE
}
// The connection was closed by our peer
2017-12-01 05:30:20 +00:00
if strings.Contains(errMsg, syscall.EPIPE.Error()) || strings.Contains(errMsg, syscall.ECONNRESET.Error()) {
return syscall.EPIPE
}
// Windows version of ECONNRESET
//XXX(schmichael) I could find no existing error or constant to
// compare this against.
if strings.Contains(errMsg, "forcibly closed") {
return syscall.EPIPE
}
return err
}
2016-07-22 22:01:56 +00:00
// stream is the internal method to stream the content of a file. eofCancelCh is
// used to cancel the stream if triggered while at EOF. If the connection is
// broken an EPIPE error is returned
2016-07-18 16:48:29 +00:00
func (s *HTTPServer) stream(offset int64, path string,
fs allocdir.AllocDirFS, framer *StreamFramer,
eofCancelCh chan error) error {
2016-07-06 00:08:58 +00:00
// Get the reader
f, err := fs.ReadAt(path, offset)
if err != nil {
2016-07-07 15:15:22 +00:00
return err
2016-07-06 00:08:58 +00:00
}
defer f.Close()
// Create a tomb to cancel watch events
t := tomb.Tomb{}
defer func() {
t.Kill(nil)
t.Done()
}()
2016-07-06 00:08:58 +00:00
// Create a variable to allow setting the last event
var lastEvent string
// Only create the file change watcher once. But we need to do it after we
// read and reach EOF.
var changes *watch.FileChanges
2016-07-06 00:08:58 +00:00
// Start streaming the data
2016-07-10 17:55:52 +00:00
data := make([]byte, streamFrameSize)
2016-07-06 00:08:58 +00:00
OUTER:
for {
// Read up to the max frame size
n, readErr := f.Read(data)
2016-07-06 00:08:58 +00:00
// Update the offset
offset += int64(n)
// Return non-EOF errors
if readErr != nil && readErr != io.EOF {
return readErr
2016-07-06 00:08:58 +00:00
}
// Send the frame
2017-05-11 20:05:53 +00:00
if n != 0 || lastEvent != "" {
if err := framer.Send(path, lastEvent, data[:n], offset); err != nil {
2016-12-09 18:49:39 +00:00
return parseFramerErr(err)
}
}
// Clear the last event
if lastEvent != "" {
lastEvent = ""
2016-07-06 00:08:58 +00:00
}
// Just keep reading
if readErr == nil {
2016-07-06 00:08:58 +00:00
continue
}
// If EOF is hit, wait for a change to the file
if changes == nil {
changes, err = fs.ChangeEvents(path, offset, &t)
if err != nil {
return err
}
2016-07-06 00:08:58 +00:00
}
for {
select {
case <-changes.Modified:
continue OUTER
case <-changes.Deleted:
2016-12-09 22:44:50 +00:00
return parseFramerErr(framer.Send(path, deleteEvent, nil, offset))
2016-07-06 00:08:58 +00:00
case <-changes.Truncated:
// Close the current reader
if err := f.Close(); err != nil {
2016-07-07 15:15:22 +00:00
return err
2016-07-06 00:08:58 +00:00
}
// Get a new reader at offset zero
offset = 0
var err error
f, err = fs.ReadAt(path, offset)
if err != nil {
2016-07-07 15:15:22 +00:00
return err
2016-07-06 00:08:58 +00:00
}
defer f.Close()
// Store the last event
lastEvent = truncateEvent
continue OUTER
case <-framer.ExitCh():
return parseFramerErr(framer.Err())
2016-07-20 17:18:05 +00:00
case err, ok := <-eofCancelCh:
if !ok {
return nil
}
2016-07-18 16:48:29 +00:00
return err
2016-07-06 00:08:58 +00:00
}
}
}
}
2016-07-18 16:48:29 +00:00
// Logs streams the content of a log blocking on EOF. The parameters are:
// * task: task name to stream logs for.
// * type: stdout/stderr to stream.
2016-07-20 17:18:05 +00:00
// * follow: A boolean of whether to follow the logs.
2016-07-18 16:48:29 +00:00
// * offset: The offset to start streaming data at, defaults to zero.
// * origin: Either "start" or "end" and defines from where the offset is
// applied. Defaults to "start".
func (s *HTTPServer) Logs(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
var allocID, task, logType string
2017-01-13 21:12:36 +00:00
var plain, follow bool
2016-07-18 16:48:29 +00:00
var err error
q := req.URL.Query()
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/logs/"); allocID == "" {
return nil, allocIDNotPresentErr
}
if task = q.Get("task"); task == "" {
return nil, taskNotPresentErr
}
2017-01-24 00:58:53 +00:00
if followStr := q.Get("follow"); followStr != "" {
2017-01-13 21:12:36 +00:00
if follow, err = strconv.ParseBool(followStr); err != nil {
return nil, fmt.Errorf("Failed to parse follow field to boolean: %v", err)
}
}
2017-01-24 00:58:53 +00:00
if plainStr := q.Get("plain"); plainStr != "" {
2017-01-13 21:12:36 +00:00
if plain, err = strconv.ParseBool(plainStr); err != nil {
return nil, fmt.Errorf("Failed to parse plain field to boolean: %v", err)
}
2016-07-20 17:18:05 +00:00
}
2016-07-18 16:48:29 +00:00
logType = q.Get("type")
switch logType {
case "stdout", "stderr":
default:
return nil, logTypeNotPresentErr
}
var offset int64
offsetString := q.Get("offset")
if offsetString != "" {
var err error
if offset, err = strconv.ParseInt(offsetString, 10, 64); err != nil {
return nil, fmt.Errorf("error parsing offset: %v", err)
}
}
origin := q.Get("origin")
switch origin {
case "start", "end":
case "":
origin = "start"
default:
return nil, invalidOrigin
}
fs, err := s.agent.client.GetAllocFS(allocID)
if err != nil {
return nil, err
}
alloc, err := s.agent.client.GetClientAlloc(allocID)
if err != nil {
return nil, err
}
// Check that the task is there
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
if tg == nil {
return nil, fmt.Errorf("Failed to lookup task group for allocation")
} else if taskStruct := tg.LookupTask(task); taskStruct == nil {
return nil, CodedError(404, fmt.Sprintf("task group %q does not have task with name %q", alloc.TaskGroup, task))
}
state, ok := alloc.TaskStates[task]
if !ok || state.StartedAt.IsZero() {
return nil, CodedError(404, fmt.Sprintf("task %q not started yet. No logs available", task))
}
2016-07-18 16:48:29 +00:00
// Create an output that gets flushed on every write
output := ioutils.NewWriteFlusher(resp)
2017-01-13 21:12:36 +00:00
return nil, s.logs(follow, plain, offset, origin, task, logType, fs, output)
2016-07-18 16:48:29 +00:00
}
2017-01-13 21:12:36 +00:00
func (s *HTTPServer) logs(follow, plain bool, offset int64,
2016-07-20 17:18:05 +00:00
origin, task, logType string,
fs allocdir.AllocDirFS, output io.WriteCloser) error {
2016-07-18 16:48:29 +00:00
// Create the framer
2017-01-13 21:12:36 +00:00
framer := NewStreamFramer(output, plain, streamHeartbeatRate, streamBatchWindow, streamFrameSize)
2016-07-18 16:48:29 +00:00
framer.Run()
defer framer.Destroy()
// Path to the logs
logPath := filepath.Join(allocdir.SharedAllocName, allocdir.LogDirName)
// nextIdx is the next index to read logs from
var nextIdx int64
switch origin {
case "start":
nextIdx = 0
case "end":
nextIdx = math.MaxInt64
offset *= -1
default:
return invalidOrigin
}
// Create a tomb to cancel watch events
t := tomb.Tomb{}
defer func() {
t.Kill(nil)
t.Done()
}()
for {
// Logic for picking next file is:
// 1) List log files
// 2) Pick log file closest to desired index
// 3) Open log file at correct offset
// 3a) No error, read contents
// 3b) If file doesn't exist, goto 1 as it may have been rotated out
entries, err := fs.List(logPath)
if err != nil {
return fmt.Errorf("failed to list entries: %v", err)
}
2016-07-20 17:18:05 +00:00
// If we are not following logs, determine the max index for the logs we are
// interested in so we can stop there.
maxIndex := int64(math.MaxInt64)
if !follow {
_, idx, _, err := findClosest(entries, maxIndex, 0, task, logType)
if err != nil {
return err
}
maxIndex = idx
}
2016-07-19 22:58:02 +00:00
logEntry, idx, openOffset, err := findClosest(entries, nextIdx, offset, task, logType)
2016-07-18 16:48:29 +00:00
if err != nil {
return err
}
2016-07-20 17:18:05 +00:00
var eofCancelCh chan error
exitAfter := false
if !follow && idx > maxIndex {
// Exceeded what was there initially so return
return nil
} else if !follow && idx == maxIndex {
// At the end
eofCancelCh = make(chan error)
close(eofCancelCh)
exitAfter = true
} else {
2016-07-20 21:14:54 +00:00
eofCancelCh = blockUntilNextLog(fs, &t, logPath, task, logType, idx+1)
2016-07-20 17:18:05 +00:00
}
2016-07-18 16:48:29 +00:00
p := filepath.Join(logPath, logEntry.Name)
2016-07-20 17:18:05 +00:00
err = s.stream(openOffset, p, fs, framer, eofCancelCh)
2016-07-18 16:48:29 +00:00
if err != nil {
2016-07-20 20:06:05 +00:00
// Check if there was an error where the file does not exist. That means
// it got rotated out from under us.
2016-07-18 16:48:29 +00:00
if os.IsNotExist(err) {
continue
}
2016-07-20 20:06:05 +00:00
// Check if the connection was closed
if err == syscall.EPIPE {
2016-07-20 20:06:05 +00:00
return nil
}
return fmt.Errorf("failed to stream %q: %v", p, err)
2016-07-18 16:48:29 +00:00
}
2016-07-20 17:18:05 +00:00
if exitAfter {
return nil
}
// defensively check to make sure StreamFramer hasn't stopped
// running to avoid tight loops with goroutine leaks as in
// #3342
select {
case <-framer.ExitCh():
err := parseFramerErr(framer.Err())
if err == syscall.EPIPE {
// EPIPE just means the connection was closed
return nil
}
return err
default:
}
// Since we successfully streamed, update the overall offset/idx.
2016-07-18 16:48:29 +00:00
offset = int64(0)
2016-07-19 17:04:57 +00:00
nextIdx = idx + 1
2016-07-18 16:48:29 +00:00
}
}
2016-07-20 21:14:54 +00:00
// blockUntilNextLog returns a channel that will have data sent when the next
// log index or anything greater is created.
func blockUntilNextLog(fs allocdir.AllocDirFS, t *tomb.Tomb, logPath, task, logType string, nextIndex int64) chan error {
nextPath := filepath.Join(logPath, fmt.Sprintf("%s.%s.%d", task, logType, nextIndex))
next := make(chan error, 1)
go func() {
eofCancelCh, err := fs.BlockUntilExists(nextPath, t)
if err != nil {
next <- err
close(next)
return
}
2016-12-10 01:33:10 +00:00
ticker := time.NewTicker(nextLogCheckRate)
defer ticker.Stop()
scanCh := ticker.C
2016-07-20 21:14:54 +00:00
for {
select {
case <-t.Dead():
next <- fmt.Errorf("shutdown triggered")
close(next)
return
2016-07-20 21:14:54 +00:00
case err := <-eofCancelCh:
next <- err
close(next)
return
case <-scanCh:
entries, err := fs.List(logPath)
if err != nil {
next <- fmt.Errorf("failed to list entries: %v", err)
close(next)
return
}
indexes, err := logIndexes(entries, task, logType)
if err != nil {
next <- err
close(next)
return
}
// Scan and see if there are any entries larger than what we are
// waiting for.
for _, entry := range indexes {
if entry.idx >= nextIndex {
next <- nil
close(next)
return
}
}
}
}
}()
return next
}
2016-07-19 22:58:02 +00:00
// indexTuple and indexTupleArray are used to find the correct log entry to
// start streaming logs from
type indexTuple struct {
idx int64
entry *allocdir.AllocFileInfo
}
2016-07-18 16:48:29 +00:00
2016-07-19 22:58:02 +00:00
type indexTupleArray []indexTuple
2016-07-18 16:48:29 +00:00
2016-07-19 22:58:02 +00:00
func (a indexTupleArray) Len() int { return len(a) }
func (a indexTupleArray) Less(i, j int) bool { return a[i].idx < a[j].idx }
func (a indexTupleArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
2016-07-18 16:48:29 +00:00
2016-07-20 21:14:54 +00:00
// logIndexes takes a set of entries and returns a indexTupleArray of
// the desired log file entries. If the indexes could not be determined, an
// error is returned.
func logIndexes(entries []*allocdir.AllocFileInfo, task, logType string) (indexTupleArray, error) {
2016-07-19 22:58:02 +00:00
var indexes []indexTuple
prefix := fmt.Sprintf("%s.%s.", task, logType)
2016-07-18 16:48:29 +00:00
for _, entry := range entries {
if entry.IsDir {
continue
}
// If nothing was trimmed, then it is not a match
2016-07-19 22:58:02 +00:00
idxStr := strings.TrimPrefix(entry.Name, prefix)
2016-07-18 16:48:29 +00:00
if idxStr == entry.Name {
continue
}
// Convert to an int
idx, err := strconv.Atoi(idxStr)
if err != nil {
2016-07-20 21:14:54 +00:00
return nil, fmt.Errorf("failed to convert %q to a log index: %v", idxStr, err)
2016-07-18 16:48:29 +00:00
}
2016-07-19 22:58:02 +00:00
indexes = append(indexes, indexTuple{idx: int64(idx), entry: entry})
}
2016-07-18 16:48:29 +00:00
2016-07-20 21:14:54 +00:00
return indexTupleArray(indexes), nil
}
// findClosest takes a list of entries, the desired log index and desired log
// offset (which can be negative, treated as offset from end), task name and log
// type and returns the log entry, the log index, the offset to read from and a
// potential error.
func findClosest(entries []*allocdir.AllocFileInfo, desiredIdx, desiredOffset int64,
task, logType string) (*allocdir.AllocFileInfo, int64, int64, error) {
// Build the matching indexes
indexes, err := logIndexes(entries, task, logType)
if err != nil {
return nil, 0, 0, err
}
2016-07-19 22:58:02 +00:00
if len(indexes) == 0 {
return nil, 0, 0, fmt.Errorf("log entry for task %q and log type %q not found", task, logType)
}
// Binary search the indexes to get the desiredIdx
2017-09-26 22:26:33 +00:00
sort.Sort(indexes)
2016-07-19 22:58:02 +00:00
i := sort.Search(len(indexes), func(i int) bool { return indexes[i].idx >= desiredIdx })
l := len(indexes)
if i == l {
// Use the last index if the number is bigger than all of them.
i = l - 1
2016-07-18 16:48:29 +00:00
}
2016-07-19 22:58:02 +00:00
// Get to the correct offset
offset := desiredOffset
idx := int64(i)
for {
s := indexes[idx].entry.Size
// Base case
if offset == 0 {
break
} else if offset < 0 {
// Going backwards
if newOffset := s + offset; newOffset >= 0 {
// Current file works
offset = newOffset
break
} else if idx == 0 {
// Already at the end
offset = 0
break
} else {
// Try the file before
offset = newOffset
idx -= 1
continue
}
} else {
// Going forward
if offset <= s {
// Current file works
break
} else if idx == int64(l-1) {
// Already at the end
offset = s
break
} else {
// Try the next file
offset = offset - s
idx += 1
continue
}
}
2016-07-18 16:48:29 +00:00
}
2016-07-19 22:58:02 +00:00
return indexes[idx].entry, indexes[idx].idx, offset, nil
2016-07-18 16:48:29 +00:00
}