Merge pull request #47044 from corhere/lazy-health-status

Lazily checkpoint container health status to disk
This commit is contained in:
Sebastiaan van Stijn 2024-01-17 14:12:38 +01:00 committed by GitHub
commit 60ed73bb91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View file

@ -267,6 +267,32 @@ func (container *Container) WriteHostConfig() (*containertypes.HostConfig, error
return &deepCopy, nil
}
// CommitInMemory makes the Container's current state visible to queries,
// but does not persist state.
//
// Callers must hold a Container lock.
func (container *Container) CommitInMemory(store *ViewDB) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(container); err != nil {
return err
}
var deepCopy Container
if err := json.NewDecoder(&buf).Decode(&deepCopy); err != nil {
return err
}
buf.Reset()
if err := json.NewEncoder(&buf).Encode(container.HostConfig); err != nil {
return err
}
if err := json.NewDecoder(&buf).Decode(&deepCopy.HostConfig); err != nil {
return err
}
return store.Save(&deepCopy)
}
// SetupWorkingDirectory sets up the container's working directory as set in container.Config.WorkingDir
func (container *Container) SetupWorkingDirectory(rootIdentity idtools.Identity) error {
if container.Config.WorkingDir == "" {

View file

@ -231,8 +231,11 @@ func handleProbeResult(d *Daemon, c *container.Container, result *types.Healthch
// Else we're starting or healthy. Stay in that state.
}
// replicate Health status changes
if err := c.CheckpointTo(d.containersReplica); err != nil {
// Replicate Health status changes to the API, skipping persistent storage
// to avoid unnecessary disk writes. The health state is only best-effort
// persisted across of the daemon. It will get written to disk on the next
// checkpoint, such as when the container state changes.
if err := c.CommitInMemory(d.containersReplica); err != nil {
// queries will be inconsistent until the next probe runs or other state mutations
// checkpoint the container
log.G(context.TODO()).Errorf("Error replicating health state for container %s: %v", c.ID, err)