2015-11-12 19:55:17 +00:00
|
|
|
package container
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-30 20:52:40 +00:00
|
|
|
"errors"
|
2013-03-12 12:36:37 +00:00
|
|
|
"fmt"
|
2013-04-09 16:09:54 +00:00
|
|
|
"sync"
|
2013-01-28 22:30:05 +00:00
|
|
|
"time"
|
2014-05-12 23:40:19 +00:00
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-07-15 18:21:19 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-16 17:26:49 +00:00
|
|
|
"github.com/docker/go-units"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// State holds the current container state, and has methods to get and
|
|
|
|
// set the state. Container has an embed, which allows all of the
|
|
|
|
// functions defined against State to run against Container.
|
2013-01-19 00:13:39 +00:00
|
|
|
type State struct {
|
2014-08-28 11:39:27 +00:00
|
|
|
sync.Mutex
|
2015-07-30 21:01:53 +00:00
|
|
|
// FIXME: Why do we have both paused and running if a
|
|
|
|
// container cannot be paused and running at the same time?
|
2015-03-12 19:26:17 +00:00
|
|
|
Running bool
|
|
|
|
Paused bool
|
|
|
|
Restarting bool
|
|
|
|
OOMKilled bool
|
2015-11-12 19:55:17 +00:00
|
|
|
RemovalInProgress bool // Not need for this to be persistent on disk.
|
2015-03-12 19:26:17 +00:00
|
|
|
Dead bool
|
|
|
|
Pid int
|
2016-08-30 16:44:06 +00:00
|
|
|
ExitCodeValue int `json:"ExitCode"`
|
|
|
|
ErrorMsg string `json:"Error"` // contains last known error when starting the container
|
2015-03-12 19:26:17 +00:00
|
|
|
StartedAt time.Time
|
|
|
|
FinishedAt time.Time
|
2016-04-18 09:48:13 +00:00
|
|
|
Health *Health
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
waitStop chan struct{}
|
|
|
|
waitRemove chan struct{}
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// StateStatus is used to return container wait results.
|
|
|
|
// Implements exec.ExitCode interface.
|
2016-08-30 16:44:06 +00:00
|
|
|
// This type is needed as State include a sync.Mutex field which make
|
|
|
|
// copying it unsafe.
|
|
|
|
type StateStatus struct {
|
|
|
|
exitCode int
|
2017-03-30 20:52:40 +00:00
|
|
|
err error
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns current exitcode for the state.
|
2017-03-31 03:01:41 +00:00
|
|
|
func (s StateStatus) ExitCode() int {
|
|
|
|
return s.exitCode
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// Err returns current error for the state. Returns nil if the container had
|
|
|
|
// exited on its own.
|
2017-03-31 03:01:41 +00:00
|
|
|
func (s StateStatus) Err() error {
|
|
|
|
return s.err
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// NewState creates a default state object with a fresh channel for state changes.
|
2014-06-06 11:28:12 +00:00
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
2017-03-30 20:52:40 +00:00
|
|
|
waitStop: make(chan struct{}),
|
|
|
|
waitRemove: make(chan struct{}),
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:42:42 +00:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
2014-05-21 21:06:18 +00:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 18:35:18 +00:00
|
|
|
if s.Restarting {
|
2016-08-30 16:44:06 +00:00
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2014-08-11 18:35:18 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
if h := s.Health; h != nil {
|
|
|
|
return fmt.Sprintf("Up %s (%s)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)), h.String())
|
|
|
|
}
|
2016-07-15 18:21:19 +00:00
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
if s.RemovalInProgress {
|
2015-03-12 19:26:17 +00:00
|
|
|
return "Removal In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "Dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "Created"
|
|
|
|
}
|
|
|
|
|
2014-03-19 03:05:54 +00:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2016-08-30 16:44:06 +00:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 18:21:19 +00:00
|
|
|
// HealthString returns a single string to describe health status.
|
|
|
|
func (s *State) HealthString() string {
|
|
|
|
if s.Health == nil {
|
|
|
|
return types.NoHealthcheck
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Health.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidHealthString checks if the provided string is a valid container health status or not.
|
|
|
|
func IsValidHealthString(s string) bool {
|
|
|
|
return s == types.Starting ||
|
|
|
|
s == types.Healthy ||
|
|
|
|
s == types.Unhealthy ||
|
|
|
|
s == types.NoHealthcheck
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00:00
|
|
|
// StateString returns a single string to describe state
|
|
|
|
func (s *State) StateString() string {
|
|
|
|
if s.Running {
|
|
|
|
if s.Paused {
|
|
|
|
return "paused"
|
|
|
|
}
|
|
|
|
if s.Restarting {
|
|
|
|
return "restarting"
|
|
|
|
}
|
|
|
|
return "running"
|
|
|
|
}
|
2015-03-12 19:26:17 +00:00
|
|
|
|
2016-08-15 11:33:56 +00:00
|
|
|
if s.RemovalInProgress {
|
|
|
|
return "removing"
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:26:17 +00:00
|
|
|
if s.Dead {
|
|
|
|
return "dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00:00
|
|
|
return "exited"
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// IsValidStateString checks if the provided string is a valid container state or not.
|
|
|
|
func IsValidStateString(s string) bool {
|
2015-06-30 21:40:27 +00:00
|
|
|
if s != "paused" &&
|
|
|
|
s != "restarting" &&
|
2016-08-15 11:33:56 +00:00
|
|
|
s != "removing" &&
|
2015-06-30 21:40:27 +00:00
|
|
|
s != "running" &&
|
|
|
|
s != "dead" &&
|
|
|
|
s != "created" &&
|
|
|
|
s != "exited" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// WaitCondition is an enum type for different states to wait for.
|
|
|
|
type WaitCondition int
|
|
|
|
|
|
|
|
// Possible WaitCondition Values.
|
|
|
|
//
|
|
|
|
// WaitConditionNotRunning (default) is used to wait for any of the non-running
|
|
|
|
// states: "created", "exited", "dead", "removing", or "removed".
|
|
|
|
//
|
|
|
|
// WaitConditionNextExit is used to wait for the next time the state changes
|
|
|
|
// to a non-running state. If the state is currently "created" or "exited",
|
|
|
|
// this would cause Wait() to block until either the container runs and exits
|
|
|
|
// or is removed.
|
|
|
|
//
|
|
|
|
// WaitConditionRemoved is used to wait for the container to be removed.
|
|
|
|
const (
|
|
|
|
WaitConditionNotRunning WaitCondition = iota
|
|
|
|
WaitConditionNextExit
|
|
|
|
WaitConditionRemoved
|
|
|
|
)
|
2014-06-06 11:28:12 +00:00
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// Wait waits until the continer is in a certain state indicated by the given
|
|
|
|
// condition. A context must be used for cancelling the request, controlling
|
|
|
|
// timeouts, and avoiding goroutine leaks. Wait must be called without holding
|
|
|
|
// the state lock. Returns a channel from which the caller will receive the
|
|
|
|
// result. If the container exited on its own, the result's Err() method will
|
|
|
|
// be nil and its ExitCode() method will return the conatiners exit code,
|
|
|
|
// otherwise, the results Err() method will return an error indicating why the
|
|
|
|
// wait operation failed.
|
|
|
|
func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateStatus {
|
2017-03-30 20:52:40 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
if condition == WaitConditionNotRunning && !s.Running {
|
|
|
|
// Buffer so we can put it in the channel now.
|
|
|
|
resultC := make(chan StateStatus, 1)
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// Send the current status.
|
|
|
|
resultC <- StateStatus{
|
|
|
|
exitCode: s.ExitCode(),
|
|
|
|
err: s.Err(),
|
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
return resultC
|
2016-11-17 03:08:37 +00:00
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// If we are waiting only for removal, the waitStop channel should
|
|
|
|
// remain nil and block forever.
|
2017-03-30 20:52:40 +00:00
|
|
|
var waitStop chan struct{}
|
2017-03-31 03:01:41 +00:00
|
|
|
if condition < WaitConditionRemoved {
|
2017-03-30 20:52:40 +00:00
|
|
|
waitStop = s.waitStop
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// Always wait for removal, just in case the container gets removed
|
|
|
|
// while it is still in a "created" state, in which case it is never
|
|
|
|
// actually stopped.
|
|
|
|
waitRemove := s.waitRemove
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
resultC := make(chan StateStatus)
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Context timeout or cancellation.
|
2017-03-31 03:01:41 +00:00
|
|
|
resultC <- StateStatus{
|
|
|
|
exitCode: -1,
|
|
|
|
err: ctx.Err(),
|
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
return
|
|
|
|
case <-waitStop:
|
|
|
|
case <-waitRemove:
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2016-06-14 18:11:43 +00:00
|
|
|
s.Lock()
|
2017-03-31 03:01:41 +00:00
|
|
|
result := StateStatus{
|
|
|
|
exitCode: s.ExitCode(),
|
|
|
|
err: s.Err(),
|
|
|
|
}
|
2016-06-14 02:52:49 +00:00
|
|
|
s.Unlock()
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
resultC <- result
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resultC
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// IsRunning returns whether the running flag is set. Used by Container to check whether a container is running.
|
2013-11-21 20:21:03 +00:00
|
|
|
func (s *State) IsRunning() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Running
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
|
|
|
}
|
2013-11-21 20:21:03 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// GetPID holds the process id of a container.
|
2015-11-02 23:25:26 +00:00
|
|
|
func (s *State) GetPID() int {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Pid
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 18:11:43 +00:00
|
|
|
// ExitCode returns current exitcode for the state. Take lock before if state
|
|
|
|
// may be shared.
|
|
|
|
func (s *State) ExitCode() int {
|
2016-08-30 16:44:06 +00:00
|
|
|
return s.ExitCodeValue
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 12:47:39 +00:00
|
|
|
// SetExitCode sets current exitcode for the state. Take lock before if state
|
2016-06-14 18:11:43 +00:00
|
|
|
// may be shared.
|
|
|
|
func (s *State) SetExitCode(ec int) {
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ExitCodeValue = ec
|
2016-06-14 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetRunning sets the state of the container to "running".
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetRunning(pid int, initial bool) {
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ErrorMsg = ""
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = true
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ExitCodeValue = 0
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Pid = pid
|
2016-03-18 18:50:19 +00:00
|
|
|
if initial {
|
|
|
|
s.StartedAt = time.Now().UTC()
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetStopped sets the container state to "stopped" without locking.
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetStopped(exitStatus *ExitStatus) {
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = false
|
2016-03-18 18:50:19 +00:00
|
|
|
s.Paused = false
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-11-01 02:31:24 +00:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2017-03-30 20:52:40 +00:00
|
|
|
close(s.waitStop) // Fire waiters for stop
|
|
|
|
s.waitStop = make(chan struct{})
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2014-05-21 21:06:18 +00:00
|
|
|
|
2016-09-20 11:09:18 +00:00
|
|
|
// SetRestarting sets the container state to "restarting" without locking.
|
2015-11-12 19:55:17 +00:00
|
|
|
// It also sets the container PID to 0.
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetRestarting(exitStatus *ExitStatus) {
|
2014-08-13 21:56:35 +00:00
|
|
|
// we should consider the container running when it is restarting because of
|
|
|
|
// all the checks in docker around rm/stop/etc
|
|
|
|
s.Running = true
|
|
|
|
s.Restarting = true
|
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2015-11-01 02:31:24 +00:00
|
|
|
s.setFromExitStatus(exitStatus)
|
2017-03-30 20:52:40 +00:00
|
|
|
close(s.waitStop) // Fire waiters for stop
|
|
|
|
s.waitStop = make(chan struct{})
|
2014-08-11 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetError sets the container's error state. This is useful when we want to
|
2014-09-30 08:30:58 +00:00
|
|
|
// know the error that occurred when container transits to another state
|
|
|
|
// when inspecting it
|
2015-11-12 19:55:17 +00:00
|
|
|
func (s *State) SetError(err error) {
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ErrorMsg = err.Error()
|
2014-09-30 08:30:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// IsPaused returns whether the container is paused or not.
|
|
|
|
func (s *State) IsPaused() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Paused
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
2015-03-12 19:26:17 +00:00
|
|
|
|
2016-01-25 15:38:03 +00:00
|
|
|
// IsRestarting returns whether the container is restarting or not.
|
|
|
|
func (s *State) IsRestarting() bool {
|
|
|
|
s.Lock()
|
|
|
|
res := s.Restarting
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetRemovalInProgress sets the container state as being removed.
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
// It returns true if the container was already in that state.
|
|
|
|
func (s *State) SetRemovalInProgress() bool {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2015-11-12 19:55:17 +00:00
|
|
|
if s.RemovalInProgress {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
return true
|
2015-03-12 19:26:17 +00:00
|
|
|
}
|
2015-11-12 19:55:17 +00:00
|
|
|
s.RemovalInProgress = true
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
return false
|
2015-03-12 19:26:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 12:47:39 +00:00
|
|
|
// ResetRemovalInProgress makes the RemovalInProgress state to false.
|
2015-11-12 19:55:17 +00:00
|
|
|
func (s *State) ResetRemovalInProgress() {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
2015-11-12 19:55:17 +00:00
|
|
|
s.RemovalInProgress = false
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetDead sets the container state to "dead"
|
|
|
|
func (s *State) SetDead() {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
|
|
|
s.Dead = true
|
|
|
|
s.Unlock()
|
|
|
|
}
|
2016-06-14 18:11:43 +00:00
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// SetRemoved assumes this container is already in the "dead" state and
|
|
|
|
// closes the internal waitRemove channel to unblock callers waiting for a
|
|
|
|
// container to be removed.
|
|
|
|
func (s *State) SetRemoved() {
|
|
|
|
s.Lock()
|
|
|
|
close(s.waitRemove) // Unblock those waiting on remove.
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns an error if there is one.
|
|
|
|
func (s *State) Err() error {
|
|
|
|
if s.ErrorMsg != "" {
|
|
|
|
return errors.New(s.ErrorMsg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|