Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the log streamer respects forced shutdown of the agent #3180

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions agent/log_streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type LogStreamer struct {

// Have we stopped?
stopped bool

// have we been asked to exit immediately?
exitImmediately bool
}

// NewLogStreamer creates a new instance of the log streamer.
Expand Down Expand Up @@ -161,7 +164,7 @@ func (ls *LogStreamer) Process(ctx context.Context, output []byte) error {
}

// Stop stops the streamer.
func (ls *LogStreamer) Stop() {
func (ls *LogStreamer) Stop(graceful bool) {
ls.processMutex.Lock()
if ls.stopped {
ls.processMutex.Unlock()
Expand All @@ -171,8 +174,13 @@ func (ls *LogStreamer) Stop() {
close(ls.queue)
ls.processMutex.Unlock()

ls.logger.Debug("[LogStreamer] Waiting for workers to shut down")
ls.workerWG.Wait()
if graceful {
ls.workerWG.Wait()
ls.logger.Info("[LogStreamer] Waiting for workers to shut down and outstanding chunks to be uploaded")
} else {
ls.exitImmediately = true
ls.logger.Warn("[LogStreamer] NOT waiting for outstanding chunks to be uploaded")
}
}

// The actual log streamer worker
Expand All @@ -189,6 +197,15 @@ func (ls *LogStreamer) worker(ctx context.Context, id int) {
for {
setStat("⌚️ Waiting for a chunk")

if len(ls.queue) > 0 {
ls.logger.Debug("[LogStreamer/Worker#%d] Queue length: %d", id, len(ls.queue))
}

if ls.exitImmediately {
ls.logger.Warn("[LogStreamer/Worker#%d] Worker is shutting down immediately, Outstanding Queue length: %d", id, len(ls.queue))
return
}

// Get the next chunk (pointer) from the queue. This will block
// until something is returned.
var chunk *api.Chunk
Expand All @@ -201,6 +218,9 @@ func (ls *LogStreamer) worker(ctx context.Context, id int) {
return
}

// used to simulate a slow upload
// time.Sleep(30 * time.Second)

setStat("📨 Uploading chunk")

// Upload the chunk
Expand Down
46 changes: 45 additions & 1 deletion agent/log_streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestLogStreamer(t *testing.T) {
t.Errorf("LogStreamer.Process(ctx, %q) = %v", input, err)
}

ls.Stop()
ls.Stop(true)

want := []*api.Chunk{
{
Expand Down Expand Up @@ -94,3 +94,47 @@ func TestLogStreamer(t *testing.T) {
t.Errorf("after Stop: LogStreamer.Process(ctx, %q) err = %v, want %v", input, err, errStreamerStopped)
}
}

func TestLogStreamerExitImmediately(t *testing.T) {
t.Parallel()
ctx := context.Background()

logger := logger.NewConsoleLogger(
logger.NewTextPrinter(os.Stderr),
func(c int) { t.Errorf("exit(%d)", c) },
)

var mu sync.Mutex
var got []*api.Chunk
callback := func(ctx context.Context, chunk *api.Chunk) error {
mu.Lock()
got = append(got, chunk)
mu.Unlock()
return nil
}

ls := NewLogStreamer(logger, callback, LogStreamerConfig{
Concurrency: 3,
MaxChunkSizeBytes: 10,
MaxSizeBytes: 30,
})

if err := ls.Start(ctx); err != nil {
t.Fatalf("LogStreamer.Start(ctx) = %v", err)
}

input := "0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()" // 46 bytes
if err := ls.Process(ctx, []byte(input)); err != nil {
t.Errorf("LogStreamer.Process(ctx, %q) = %v", input, err)
}

ls.Stop(false)

if !ls.exitImmediately {
t.Errorf("LogStreamer.Stop(false) did not set exitImmediately")
}

if len(got) > 0 {
t.Errorf("LogStreamer.Stop(false) did not exit immediately")
}
}
3 changes: 2 additions & 1 deletion agent/run_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (r *JobRunner) cleanup(ctx context.Context, wg *sync.WaitGroup, exit core.P
r.logStreamer.Process(ctx, r.output.ReadAndTruncate())

// Stop the log streamer. This will block until all the chunks have been uploaded
r.logStreamer.Stop()
r.logStreamer.Stop(true)

// Stop the header time streamer. This will block until all the chunks have been uploaded
r.headerTimesStreamer.Stop()
Expand Down Expand Up @@ -471,6 +471,7 @@ func (r *JobRunner) CancelAndStop() error {
r.cancelLock.Lock()
r.stopped = true
r.cancelLock.Unlock()
r.logStreamer.Stop(false)
return r.Cancel()
}

Expand Down