noderunner.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package cluster
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "runtime"
  6. "strings"
  7. "sync"
  8. "time"
  9. types "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/daemon/cluster/executor/container"
  11. lncluster "github.com/docker/libnetwork/cluster"
  12. swarmapi "github.com/docker/swarmkit/api"
  13. swarmnode "github.com/docker/swarmkit/node"
  14. "github.com/pkg/errors"
  15. "github.com/sirupsen/logrus"
  16. "golang.org/x/net/context"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/codes"
  19. "google.golang.org/grpc/status"
  20. )
  21. // nodeRunner implements a manager for continuously running swarmkit node, restarting them with backoff delays if needed.
  22. type nodeRunner struct {
  23. nodeState
  24. mu sync.RWMutex
  25. done chan struct{} // closed when swarmNode exits
  26. ready chan struct{} // closed when swarmNode becomes active
  27. reconnectDelay time.Duration
  28. config nodeStartConfig
  29. repeatedRun bool
  30. cancelReconnect func()
  31. stopping bool
  32. cluster *Cluster // only for accessing config helpers, never call any methods. TODO: change to config struct
  33. }
  34. // nodeStartConfig holds configuration needed to start a new node. Exported
  35. // fields of this structure are saved to disk in json. Unexported fields
  36. // contain data that shouldn't be persisted between daemon reloads.
  37. type nodeStartConfig struct {
  38. // LocalAddr is this machine's local IP or hostname, if specified.
  39. LocalAddr string
  40. // RemoteAddr is the address that was given to "swarm join". It is used
  41. // to find LocalAddr if necessary.
  42. RemoteAddr string
  43. // ListenAddr is the address we bind to, including a port.
  44. ListenAddr string
  45. // AdvertiseAddr is the address other nodes should connect to,
  46. // including a port.
  47. AdvertiseAddr string
  48. // DataPathAddr is the address that has to be used for the data path
  49. DataPathAddr string
  50. // JoinInProgress is set to true if a join operation has started, but
  51. // not completed yet.
  52. JoinInProgress bool
  53. joinAddr string
  54. forceNewCluster bool
  55. joinToken string
  56. lockKey []byte
  57. autolock bool
  58. availability types.NodeAvailability
  59. }
  60. func (n *nodeRunner) Ready() chan error {
  61. c := make(chan error, 1)
  62. n.mu.RLock()
  63. ready, done := n.ready, n.done
  64. n.mu.RUnlock()
  65. go func() {
  66. select {
  67. case <-ready:
  68. case <-done:
  69. }
  70. select {
  71. case <-ready:
  72. default:
  73. n.mu.RLock()
  74. c <- n.err
  75. n.mu.RUnlock()
  76. }
  77. close(c)
  78. }()
  79. return c
  80. }
  81. func (n *nodeRunner) Start(conf nodeStartConfig) error {
  82. n.mu.Lock()
  83. defer n.mu.Unlock()
  84. n.reconnectDelay = initialReconnectDelay
  85. return n.start(conf)
  86. }
  87. func (n *nodeRunner) start(conf nodeStartConfig) error {
  88. var control string
  89. if runtime.GOOS == "windows" {
  90. control = `\\.\pipe\` + controlSocket
  91. } else {
  92. control = filepath.Join(n.cluster.runtimeRoot, controlSocket)
  93. }
  94. joinAddr := conf.joinAddr
  95. if joinAddr == "" && conf.JoinInProgress {
  96. // We must have been restarted while trying to join a cluster.
  97. // Continue trying to join instead of forming our own cluster.
  98. joinAddr = conf.RemoteAddr
  99. }
  100. // Hostname is not set here. Instead, it is obtained from
  101. // the node description that is reported periodically
  102. swarmnodeConfig := swarmnode.Config{
  103. ForceNewCluster: conf.forceNewCluster,
  104. ListenControlAPI: control,
  105. ListenRemoteAPI: conf.ListenAddr,
  106. AdvertiseRemoteAPI: conf.AdvertiseAddr,
  107. JoinAddr: joinAddr,
  108. StateDir: n.cluster.root,
  109. JoinToken: conf.joinToken,
  110. Executor: container.NewExecutor(n.cluster.config.Backend, n.cluster.config.PluginBackend),
  111. HeartbeatTick: 1,
  112. ElectionTick: 3,
  113. UnlockKey: conf.lockKey,
  114. AutoLockManagers: conf.autolock,
  115. PluginGetter: n.cluster.config.Backend.PluginGetter(),
  116. }
  117. if conf.availability != "" {
  118. avail, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(conf.availability))]
  119. if !ok {
  120. return fmt.Errorf("invalid Availability: %q", conf.availability)
  121. }
  122. swarmnodeConfig.Availability = swarmapi.NodeSpec_Availability(avail)
  123. }
  124. node, err := swarmnode.New(&swarmnodeConfig)
  125. if err != nil {
  126. return err
  127. }
  128. if err := node.Start(context.Background()); err != nil {
  129. return err
  130. }
  131. n.done = make(chan struct{})
  132. n.ready = make(chan struct{})
  133. n.swarmNode = node
  134. if conf.joinAddr != "" {
  135. conf.JoinInProgress = true
  136. }
  137. n.config = conf
  138. savePersistentState(n.cluster.root, conf)
  139. ctx, cancel := context.WithCancel(context.Background())
  140. go func() {
  141. n.handleNodeExit(node)
  142. cancel()
  143. }()
  144. go n.handleReadyEvent(ctx, node, n.ready)
  145. go n.handleControlSocketChange(ctx, node)
  146. return nil
  147. }
  148. func (n *nodeRunner) handleControlSocketChange(ctx context.Context, node *swarmnode.Node) {
  149. for conn := range node.ListenControlSocket(ctx) {
  150. n.mu.Lock()
  151. if n.grpcConn != conn {
  152. if conn == nil {
  153. n.controlClient = nil
  154. n.logsClient = nil
  155. } else {
  156. n.controlClient = swarmapi.NewControlClient(conn)
  157. n.logsClient = swarmapi.NewLogsClient(conn)
  158. // push store changes to daemon
  159. go n.watchClusterEvents(ctx, conn)
  160. }
  161. }
  162. n.grpcConn = conn
  163. n.mu.Unlock()
  164. n.cluster.SendClusterEvent(lncluster.EventSocketChange)
  165. }
  166. }
  167. func (n *nodeRunner) watchClusterEvents(ctx context.Context, conn *grpc.ClientConn) {
  168. client := swarmapi.NewWatchClient(conn)
  169. watch, err := client.Watch(ctx, &swarmapi.WatchRequest{
  170. Entries: []*swarmapi.WatchRequest_WatchEntry{
  171. {
  172. Kind: "node",
  173. Action: swarmapi.WatchActionKindCreate | swarmapi.WatchActionKindUpdate | swarmapi.WatchActionKindRemove,
  174. },
  175. {
  176. Kind: "service",
  177. Action: swarmapi.WatchActionKindCreate | swarmapi.WatchActionKindUpdate | swarmapi.WatchActionKindRemove,
  178. },
  179. {
  180. Kind: "network",
  181. Action: swarmapi.WatchActionKindCreate | swarmapi.WatchActionKindUpdate | swarmapi.WatchActionKindRemove,
  182. },
  183. {
  184. Kind: "secret",
  185. Action: swarmapi.WatchActionKindCreate | swarmapi.WatchActionKindUpdate | swarmapi.WatchActionKindRemove,
  186. },
  187. {
  188. Kind: "config",
  189. Action: swarmapi.WatchActionKindCreate | swarmapi.WatchActionKindUpdate | swarmapi.WatchActionKindRemove,
  190. },
  191. },
  192. IncludeOldObject: true,
  193. })
  194. if err != nil {
  195. logrus.WithError(err).Error("failed to watch cluster store")
  196. return
  197. }
  198. for {
  199. msg, err := watch.Recv()
  200. if err != nil {
  201. // store watch is broken
  202. errStatus, ok := status.FromError(err)
  203. if !ok || errStatus.Code() != codes.Canceled {
  204. logrus.WithError(err).Error("failed to receive changes from store watch API")
  205. }
  206. return
  207. }
  208. select {
  209. case <-ctx.Done():
  210. return
  211. case n.cluster.watchStream <- msg:
  212. }
  213. }
  214. }
  215. func (n *nodeRunner) handleReadyEvent(ctx context.Context, node *swarmnode.Node, ready chan struct{}) {
  216. select {
  217. case <-node.Ready():
  218. n.mu.Lock()
  219. n.err = nil
  220. if n.config.JoinInProgress {
  221. n.config.JoinInProgress = false
  222. savePersistentState(n.cluster.root, n.config)
  223. }
  224. n.mu.Unlock()
  225. close(ready)
  226. case <-ctx.Done():
  227. }
  228. n.cluster.SendClusterEvent(lncluster.EventNodeReady)
  229. }
  230. func (n *nodeRunner) handleNodeExit(node *swarmnode.Node) {
  231. err := detectLockedError(node.Err(context.Background()))
  232. if err != nil {
  233. logrus.Errorf("cluster exited with error: %v", err)
  234. }
  235. n.mu.Lock()
  236. n.swarmNode = nil
  237. n.err = err
  238. close(n.done)
  239. select {
  240. case <-n.ready:
  241. n.enableReconnectWatcher()
  242. default:
  243. if n.repeatedRun {
  244. n.enableReconnectWatcher()
  245. }
  246. }
  247. n.repeatedRun = true
  248. n.mu.Unlock()
  249. }
  250. // Stop stops the current swarm node if it is running.
  251. func (n *nodeRunner) Stop() error {
  252. n.mu.Lock()
  253. if n.cancelReconnect != nil { // between restarts
  254. n.cancelReconnect()
  255. n.cancelReconnect = nil
  256. }
  257. if n.swarmNode == nil {
  258. n.mu.Unlock()
  259. return nil
  260. }
  261. n.stopping = true
  262. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  263. defer cancel()
  264. n.mu.Unlock()
  265. if err := n.swarmNode.Stop(ctx); err != nil && !strings.Contains(err.Error(), "context canceled") {
  266. return err
  267. }
  268. n.cluster.SendClusterEvent(lncluster.EventNodeLeave)
  269. <-n.done
  270. return nil
  271. }
  272. func (n *nodeRunner) State() nodeState {
  273. if n == nil {
  274. return nodeState{status: types.LocalNodeStateInactive}
  275. }
  276. n.mu.RLock()
  277. defer n.mu.RUnlock()
  278. ns := n.nodeState
  279. if ns.err != nil || n.cancelReconnect != nil {
  280. if errors.Cause(ns.err) == errSwarmLocked {
  281. ns.status = types.LocalNodeStateLocked
  282. } else {
  283. ns.status = types.LocalNodeStateError
  284. }
  285. } else {
  286. select {
  287. case <-n.ready:
  288. ns.status = types.LocalNodeStateActive
  289. default:
  290. ns.status = types.LocalNodeStatePending
  291. }
  292. }
  293. return ns
  294. }
  295. func (n *nodeRunner) enableReconnectWatcher() {
  296. if n.stopping {
  297. return
  298. }
  299. n.reconnectDelay *= 2
  300. if n.reconnectDelay > maxReconnectDelay {
  301. n.reconnectDelay = maxReconnectDelay
  302. }
  303. logrus.Warnf("Restarting swarm in %.2f seconds", n.reconnectDelay.Seconds())
  304. delayCtx, cancel := context.WithTimeout(context.Background(), n.reconnectDelay)
  305. n.cancelReconnect = cancel
  306. go func() {
  307. <-delayCtx.Done()
  308. if delayCtx.Err() != context.DeadlineExceeded {
  309. return
  310. }
  311. n.mu.Lock()
  312. defer n.mu.Unlock()
  313. if n.stopping {
  314. return
  315. }
  316. if err := n.start(n.config); err != nil {
  317. n.err = err
  318. }
  319. }()
  320. }
  321. // nodeState represents information about the current state of the cluster and
  322. // provides access to the grpc clients.
  323. type nodeState struct {
  324. swarmNode *swarmnode.Node
  325. grpcConn *grpc.ClientConn
  326. controlClient swarmapi.ControlClient
  327. logsClient swarmapi.LogsClient
  328. status types.LocalNodeState
  329. actualLocalAddr string
  330. err error
  331. }
  332. // IsActiveManager returns true if node is a manager ready to accept control requests. It is safe to access the client properties if this returns true.
  333. func (ns nodeState) IsActiveManager() bool {
  334. return ns.controlClient != nil
  335. }
  336. // IsManager returns true if node is a manager.
  337. func (ns nodeState) IsManager() bool {
  338. return ns.swarmNode != nil && ns.swarmNode.Manager() != nil
  339. }
  340. // NodeID returns node's ID or empty string if node is inactive.
  341. func (ns nodeState) NodeID() string {
  342. if ns.swarmNode != nil {
  343. return ns.swarmNode.NodeID()
  344. }
  345. return ""
  346. }