Skip to content

Commit

Permalink
replace gatherer.target with .targetName
Browse files Browse the repository at this point in the history
- add plan to manager

Should fix a possible race condition on the target.
  • Loading branch information
barrettj12 committed Jul 12, 2023
1 parent b9d2214 commit ecd54b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 8 additions & 8 deletions internals/overlord/logstate/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const parserSize = 4 * 1024
// Its client may also flush itself when its internal buffer reaches a certain
// size.
type logGatherer struct {
target *plan.LogTarget
targetName string
tickPeriod time.Duration
cancel chan struct{}
client logClient
Expand All @@ -56,7 +56,7 @@ func newLogGatherer(target *plan.LogTarget) (*logGatherer, error) {
}

return &logGatherer{
target: target,
targetName: target.Name,
tickPeriod: 1 * time.Second,
cancel: make(chan struct{}),
client: client,
Expand All @@ -69,11 +69,11 @@ func (g *logGatherer) planChanged(pl *plan.Plan, buffers map[string]*servicelog.
g.pullersLock.Lock()
defer g.pullersLock.Unlock()

g.target = pl.LogTargets[g.target.Name]
newPullers := map[string]*logPuller{}

for _, service := range pl.Services {
if !service.LogsTo(g.target) {
target := pl.LogTargets[g.targetName]
if !service.LogsTo(target) {
continue
}

Expand All @@ -100,7 +100,7 @@ func (g *logGatherer) planChanged(pl *plan.Plan, buffers map[string]*servicelog.
err := puller.tomb.Killf("gatherer closed")
if err != nil {
logger.Noticef("Error shutting down puller for target %q, service %q: %v",
g.target.Name, svc, err)
g.targetName, svc, err)
}
}
g.pullers = newPullers
Expand All @@ -122,21 +122,21 @@ func (g *logGatherer) loop() {
// Timeout - flush
err := g.client.Flush()
if err != nil {
logger.Noticef("Cannot send logs to target %q: %v", g.target.Name, err)
logger.Noticef("Cannot send logs to target %q: %v", g.targetName, err)
}

case <-g.cancel:
// Gatherer has been stopped - flush any remaining logs
err := g.client.Flush()
if err != nil {
logger.Noticef("Cannot send logs to target %q: %v", g.target.Name, err)
logger.Noticef("Cannot send logs to target %q: %v", g.targetName, err)
}
return

case entry := <-g.entryCh:
err := g.client.Write(entry)
if err != nil {
logger.Noticef("Cannot write logs to target %q: %v", g.target.Name, err)
logger.Noticef("Cannot write logs to target %q: %v", g.targetName, err)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion internals/overlord/logstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type LogManager struct {
mu sync.Mutex
gatherers map[string]*logGatherer
buffers map[string]*servicelog.RingBuffer
plan *plan.Plan
}

func NewLogManager() *LogManager {
Expand Down Expand Up @@ -72,6 +73,7 @@ func (m *LogManager) PlanChanged(pl *plan.Plan) {
gatherer.stop()
}
m.gatherers = newGatherers
m.plan = pl
}

// ServiceStarted notifies the log manager that the named service has started,
Expand All @@ -83,7 +85,8 @@ func (m *LogManager) ServiceStarted(service *plan.Service, buffer *servicelog.Ri
m.buffers[service.Name] = buffer

for _, gatherer := range m.gatherers {
if !service.LogsTo(gatherer.target) {
target := m.plan.LogTargets[gatherer.targetName]
if !service.LogsTo(target) {
continue
}
gatherer.serviceStarted(service, buffer)
Expand Down

0 comments on commit ecd54b7

Please sign in to comment.