Documentation
¶
Overview ¶
Package base provides a shared framework for managing containerd-based service components. Components like etcd, victorialogs, and victoriametrics embed BaseComponent to share common functionality for container lifecycle, exit monitoring, and auto-restart.
Index ¶
- func ReapLeakedTask(ctx context.Context, cc *containerd.Client, containerID string) error
- func ReplaceTask(ctx context.Context, log *slog.Logger, componentName string, ...) (containerd.Task, error)
- type BaseComponent
- func (b *BaseComponent) CleanupExistingContainer(ctx context.Context, container containerd.Container) error
- func (b *BaseComponent) ClearRuntimeState()
- func (b *BaseComponent) DeleteContainerWithRetry(ctx context.Context) error
- func (b *BaseComponent) GetContainer() containerd.Container
- func (b *BaseComponent) GetTask() containerd.Task
- func (b *BaseComponent) IfRunning(fn func() string) string
- func (b *BaseComponent) IsRunning() bool
- func (b *BaseComponent) LockOp()
- func (b *BaseComponent) ReplaceTask(ctx context.Context, container containerd.Container, create TaskCreator) (containerd.Task, error)
- func (b *BaseComponent) SetContainer(container containerd.Container)
- func (b *BaseComponent) SetRunning(running bool)
- func (b *BaseComponent) SetTask(task containerd.Task)
- func (b *BaseComponent) StartExitMonitor(ctx context.Context)
- func (b *BaseComponent) Stop(ctx context.Context) error
- func (b *BaseComponent) StopTask(ctx context.Context, task containerd.Task) error
- func (b *BaseComponent) UnlockOp()
- func (b *BaseComponent) WaitForReady(ctx context.Context, host string, port int) error
- type ReadyCheckConfig
- type ReadyPortGetter
- type RestartPolicy
- type TaskCreator
- type TaskEvictor
- type TaskFactory
- type TaskReaper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReapLeakedTask ¶ added in v0.16.0
ReapLeakedTask asks containerd to delete a task by container ID without first loading it. The loaded-task path (container.Task followed by task.Delete) is closed to a leaked record because the lookup itself fails, but the delete RPC goes straight to containerd's runtime, which tolerates the shim reporting the container gone, shuts the shim down, and drops the record. containerd still surfaces the shim's NotFound as the RPC result, so that is treated as success.
func ReplaceTask ¶ added in v0.16.0
func ReplaceTask(ctx context.Context, log *slog.Logger, componentName string, container taskHost, evict TaskEvictor, reap TaskReaper, create TaskFactory) (containerd.Task, error)
ReplaceTask creates a fresh task on a container, first evicting any task a previous miren process left behind.
A previous process killed part-way through deleting its task can leave containerd in a state where the two normal client calls disagree: Task() reports NotFound, because the shim has already dropped the container and answers State() that way, while NewTask() reports AlreadyExists, because the cancelled delete never removed the record from containerd's task list. That record does not clear on its own for as long as that containerd keeps running, so ReplaceTask treats the combination as a leaked record, reaps it, and creates again.
Types ¶
type BaseComponent ¶
type BaseComponent struct {
Log *slog.Logger
CC *containerd.Client
Namespace string
DataPath string
// ComponentName is used in log messages to identify the component
ComponentName string
// Callbacks for component-specific behavior
CreateTask TaskCreator
GetReadyPort ReadyPortGetter
RestartPolicy RestartPolicy
ReadyConfig ReadyCheckConfig
// contains filtered or unexported fields
}
BaseComponent provides shared functionality for containerd-based service components.
func NewBaseComponent ¶
func NewBaseComponent(log *slog.Logger, cc *containerd.Client, namespace, dataPath, componentName string) *BaseComponent
NewBaseComponent creates a new BaseComponent with default policies.
func (*BaseComponent) CleanupExistingContainer ¶
func (b *BaseComponent) CleanupExistingContainer(ctx context.Context, container containerd.Container) error
CleanupExistingContainer stops the task and deletes a container during restart scenarios. It shares the same task-delete and snapshot-cleanup path as the graceful Stop flow, so a task left registered by an unclean shutdown still gets force-deleted and its snapshot removed rather than stranding both and wedging the recreate (MIR-1463).
func (*BaseComponent) ClearRuntimeState ¶ added in v0.15.0
func (b *BaseComponent) ClearRuntimeState()
ClearRuntimeState forgets a task and container after their resources have been cleaned up, so a failed start can be retried on the same component.
func (*BaseComponent) DeleteContainerWithRetry ¶
func (b *BaseComponent) DeleteContainerWithRetry(ctx context.Context) error
DeleteContainerWithRetry is the exported version for use by components.
func (*BaseComponent) GetContainer ¶
func (b *BaseComponent) GetContainer() containerd.Container
GetContainer returns the current container reference.
func (*BaseComponent) GetTask ¶
func (b *BaseComponent) GetTask() containerd.Task
GetTask returns the current task reference.
func (*BaseComponent) IfRunning ¶
func (b *BaseComponent) IfRunning(fn func() string) string
IfRunning executes the given function while holding the state lock, but only if the component is currently running. Returns empty string if not running. This is useful for safely reading component state that should only be accessed when the component is running.
func (*BaseComponent) IsRunning ¶
func (b *BaseComponent) IsRunning() bool
IsRunning returns whether the component is currently running.
func (*BaseComponent) LockOp ¶
func (b *BaseComponent) LockOp()
LockOp acquires the operation mutex for serializing major operations.
func (*BaseComponent) ReplaceTask ¶ added in v0.16.0
func (b *BaseComponent) ReplaceTask(ctx context.Context, container containerd.Container, create TaskCreator) (containerd.Task, error)
ReplaceTask is the BaseComponent form of the package-level ReplaceTask: the eviction goes through the component's own stopTask (SIGTERM, SIGKILL, delete), leaked records are reaped through the component's containerd client, and create is the component's TaskCreator bound to container.
func (*BaseComponent) SetContainer ¶
func (b *BaseComponent) SetContainer(container containerd.Container)
SetContainer sets the container reference.
func (*BaseComponent) SetRunning ¶
func (b *BaseComponent) SetRunning(running bool)
SetRunning sets the running state.
func (*BaseComponent) SetTask ¶
func (b *BaseComponent) SetTask(task containerd.Task)
SetTask sets the task reference and marks the component as running.
func (*BaseComponent) StartExitMonitor ¶
func (b *BaseComponent) StartExitMonitor(ctx context.Context)
StartExitMonitor starts a goroutine that monitors for unexpected task exits. If a monitor is already running, this is a no-op.
func (*BaseComponent) Stop ¶
func (b *BaseComponent) Stop(ctx context.Context) error
Stop stops the component, including the exit monitor, task, and container. This method acquires the operation mutex.
func (*BaseComponent) StopTask ¶
func (b *BaseComponent) StopTask(ctx context.Context, task containerd.Task) error
StopTask is the exported version of stopTask for use by components.
func (*BaseComponent) UnlockOp ¶
func (b *BaseComponent) UnlockOp()
UnlockOp releases the operation mutex.
func (*BaseComponent) WaitForReady ¶
WaitForReady waits until the component is ready by checking TCP connectivity.
type ReadyCheckConfig ¶
type ReadyCheckConfig struct {
MaxAttempts int // Maximum number of attempts (default: 30)
DialTimeout time.Duration // Timeout for each dial attempt (default: 2s)
Interval time.Duration // Time between attempts (default: 2s)
}
ReadyCheckConfig configures the readiness check behavior.
func DefaultReadyCheckConfig ¶
func DefaultReadyCheckConfig() ReadyCheckConfig
DefaultReadyCheckConfig returns the default readiness check configuration.
type ReadyPortGetter ¶
type ReadyPortGetter func() int
ReadyPortGetter is a callback that returns the port to check for readiness.
type RestartPolicy ¶
type RestartPolicy struct {
MaxRestarts int // Maximum restart attempts before giving up (0 = unlimited)
BackoffBase time.Duration // Base delay for exponential backoff (default: 2s)
BackoffMax time.Duration // Maximum backoff delay (default: 60s)
ResetWindow time.Duration // Time after which restart count resets (default: 5m)
}
RestartPolicy configures the auto-restart behavior for a component.
func AggressiveRestartPolicy ¶
func AggressiveRestartPolicy() RestartPolicy
AggressiveRestartPolicy returns a restart policy with shorter backoff times, suitable for critical components like etcd that the system cannot run without.
func DefaultRestartPolicy ¶
func DefaultRestartPolicy() RestartPolicy
DefaultRestartPolicy returns the default restart policy with unlimited restarts.
type TaskCreator ¶
type TaskCreator func(ctx context.Context, container containerd.Container) (containerd.Task, error)
TaskCreator is a callback that creates a new containerd task for the component. Each component implements this to provide component-specific task creation (e.g., logging options).
type TaskEvictor ¶ added in v0.16.0
type TaskEvictor func(ctx context.Context, task containerd.Task) error
TaskEvictor stops and deletes a task that a previous process left registered on a container, so a fresh one can be created in its place.
type TaskFactory ¶ added in v0.16.0
type TaskFactory func(ctx context.Context) (containerd.Task, error)
TaskFactory creates a new task on the container the caller has already bound it to.