revert tweaks for old docker. needs more testing.

This commit is contained in:
Henry Dollman
2024-11-02 14:43:35 -04:00
parent 055fc39305
commit 741575df15
2 changed files with 17 additions and 29 deletions

View File

@@ -84,22 +84,18 @@ func (a *Agent) Run(pubKey []byte, addr string) {
func (a *Agent) gatherStats() system.CombinedData { func (a *Agent) gatherStats() system.CombinedData {
slog.Debug("Getting stats") slog.Debug("Getting stats")
// systemData := system.CombinedData{ systemData := system.CombinedData{
// Stats: a.getSystemStats(), Stats: a.getSystemStats(),
// Info: a.systemInfo, Info: a.systemInfo,
// } }
systemData := system.CombinedData{} slog.Debug("System stats", "data", systemData)
// add docker stats (testing doing this first for docker 24) // add docker stats
if containerStats, err := a.dockerManager.getDockerStats(); err == nil { if containerStats, err := a.dockerManager.getDockerStats(); err == nil {
systemData.Containers = containerStats systemData.Containers = containerStats
slog.Debug("Docker stats", "data", systemData.Containers) slog.Debug("Docker stats", "data", systemData.Containers)
} else { } else {
slog.Debug("Error getting docker stats", "err", err) slog.Debug("Error getting docker stats", "err", err)
} }
systemData.Stats = a.getSystemStats()
systemData.Info = a.systemInfo
slog.Debug("System stats", "data", systemData)
// add extra filesystems // add extra filesystems
systemData.Stats.ExtraFs = make(map[string]*system.FsStats) systemData.Stats.ExtraFs = make(map[string]*system.FsStats)
for name, stats := range a.fsStats { for name, stats := range a.fsStats {

View File

@@ -25,23 +25,18 @@ type dockerManager struct {
apiContainerList *[]container.ApiInfo // List of containers from Docker API apiContainerList *[]container.ApiInfo // List of containers from Docker API
containerStatsMap map[string]*container.Stats // Keeps track of container stats containerStatsMap map[string]*container.Stats // Keeps track of container stats
validIds map[string]struct{} // Map of valid container ids, used to prune invalid containers from containerStatsMap validIds map[string]struct{} // Map of valid container ids, used to prune invalid containers from containerStatsMap
goodDockerVersion bool // Whether docker version is at least 25.0.0 (one-shot works correctly)
} }
// Add goroutine to the queue // Add goroutine to the queue
func (d *dockerManager) queue() { func (d *dockerManager) queue() {
d.sem <- struct{}{}
d.wg.Add(1) d.wg.Add(1)
if d.goodDockerVersion {
d.sem <- struct{}{}
}
} }
// Remove goroutine from the queue // Remove goroutine from the queue
func (d *dockerManager) dequeue() { func (d *dockerManager) dequeue() {
<-d.sem
d.wg.Done() d.wg.Done()
if d.goodDockerVersion {
<-d.sem
}
} }
// Returns stats for all running containers // Returns stats for all running containers
@@ -52,11 +47,6 @@ func (dm *dockerManager) getDockerStats() ([]*container.Stats, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
// test sleeping for 1 second if docker 24
if !dm.goodDockerVersion {
time.Sleep(time.Millisecond * 1100)
}
if err := json.NewDecoder(resp.Body).Decode(&dm.apiContainerList); err != nil { if err := json.NewDecoder(resp.Body).Decode(&dm.apiContainerList); err != nil {
return nil, err return nil, err
} }
@@ -99,11 +89,11 @@ func (dm *dockerManager) getDockerStats() ([]*container.Stats, error) {
// retry failed containers separately so we can run them in parallel (docker 24 bug) // retry failed containers separately so we can run them in parallel (docker 24 bug)
if len(failedContainters) > 0 { if len(failedContainters) > 0 {
slog.Debug("Retrying failed containers", "count", len(failedContainters)) slog.Debug("Retrying failed containers", "count", len(failedContainters))
time.Sleep(time.Millisecond * 1100) // this is a test for docker 24 bug // time.Sleep(time.Millisecond * 1100)
for _, ctr := range failedContainters { for _, ctr := range failedContainters {
dm.queue() dm.wg.Add(1)
go func() { go func() {
defer dm.dequeue() defer dm.wg.Done()
err = dm.updateContainerStats(ctr) err = dm.updateContainerStats(ctr)
if err != nil { if err != nil {
slog.Error("Error getting container stats", "err", err) slog.Error("Error getting container stats", "err", err)
@@ -261,9 +251,12 @@ func newDockerManager() *dockerManager {
Transport: transport, Transport: transport,
}, },
containerStatsMap: make(map[string]*container.Stats), containerStatsMap: make(map[string]*container.Stats),
sem: make(chan struct{}, 5),
} }
// Make sure sem is initialized
concurrency := 200
defer func() { dockerClient.sem = make(chan struct{}, concurrency) }()
// Check docker version // Check docker version
// (versions before 25.0.0 have a bug with one-shot which requires all requests to be made in one batch) // (versions before 25.0.0 have a bug with one-shot which requires all requests to be made in one batch)
var versionInfo struct { var versionInfo struct {
@@ -280,10 +273,9 @@ func newDockerManager() *dockerManager {
// if version > 24, one-shot works correctly and we can limit concurrent operations // if version > 24, one-shot works correctly and we can limit concurrent operations
if dockerVersion, err := semver.Parse(versionInfo.Version); err == nil && dockerVersion.Major > 24 { if dockerVersion, err := semver.Parse(versionInfo.Version); err == nil && dockerVersion.Major > 24 {
dockerClient.goodDockerVersion = true concurrency = 5
} else {
slog.Info(fmt.Sprintf("Docker %s is outdated. Upgrade if possible. See https://github.com/henrygd/beszel/issues/58", versionInfo.Version))
} }
slog.Debug("Docker", "version", versionInfo.Version, "concurrency", concurrency)
return dockerClient return dockerClient
} }