這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
agent.Node結構體有4個channel,理解它們的作用就可以理解swarmd程式的架構:
// Node implements the primary node functionality for a member of a swarm// cluster. Node handles workloads and may also run as a manager.type Node struct { ...... started chan struct{} stopped chan struct{} ready chan struct{} // closed when agent has completed registration and manager(if enabled) is ready to receive control requests ...... closed chan struct{} ......}
swarmd程式的架構(其中executor通過engine-addr得到,代表最終運行task的實體,實際是一個Docker engineapi.APIClient。其它參數都通過命令列直接得到。):
...... // Create a context for our GRPC call ctx, cancel := context.WithCancel(context.Background()) defer cancel() ...... n, err := agent.NewNode(&agent.NodeConfig{ Hostname: hostname, ForceNewCluster: forceNewCluster, ListenControlAPI: unix, ListenRemoteAPI: addr, JoinAddr: managerAddr, StateDir: stateDir, JoinToken: joinToken, ExternalCAs: externalCAOpt.Value(), Executor: executor, HeartbeatTick: hb, ElectionTick: election, }) if err != nil { return err } if err := n.Start(ctx); err != nil { return err } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { <-c n.Stop(ctx) }() go func() { select { case <-n.Ready(): case <-ctx.Done(): } if ctx.Err() == nil { logrus.Info("node is ready") } }() return n.Err(ctx)
(1)
if err := n.Start(ctx); err != nil { return err}
看一下Node.Start()函數的實現:
// Start starts a node instance.func (n *Node) Start(ctx context.Context) error { select { case <-n.started: select { case <-n.closed: return n.err case <-n.stopped: return errAgentStopped case <-ctx.Done(): return ctx.Err() default: return errAgentStarted } case <-ctx.Done(): return ctx.Err() default: } close(n.started) go n.run(ctx) return nil}
如果執行Node.Start()時沒有任何異常發生,就會把Node.started這個channel關掉(close(n.started)),然後啟動這個節點初始化過程:go n.run(ctx)。
(2)
c := make(chan os.Signal, 1)signal.Notify(c, os.Interrupt)go func() { <-c n.Stop(ctx)}()
這段代碼的含義是使用者按Ctrl+C可以中斷程式。Node.Stop()函數實現如下:
// Stop stops node executionfunc (n *Node) Stop(ctx context.Context) error { select { case <-n.started: select { case <-n.closed: return n.err case <-n.stopped: select { case <-n.closed: return n.err case <-ctx.Done(): return ctx.Err() } case <-ctx.Done(): return ctx.Err() default: close(n.stopped) // recurse and wait for closure return n.Stop(ctx) } case <-ctx.Done(): return ctx.Err() default: return errAgentNotStarted }}
由於此時Node.started這個channel已經被關掉,所以會永遠執行select的第一個case分支:case <-n.started。然後會根據當時的情況,再決定執行哪個分支。
(3)
go func() { select { case <-n.Ready(): case <-ctx.Done(): } if ctx.Err() == nil { logrus.Info("node is ready") }}()
Node.Ready()函數會返回Node.ready這個channel:
// Ready returns a channel that is closed after node's initialization has// completes for the first time.func (n *Node) Ready() <-chan struct{} { return n.ready}
當Node初始化完成後,Node.ready這個channel就會被關掉。因此如果一切順利的話,就會看到“node is ready”的log。
(4)
return n.Err(ctx)
Node.Err()函數的實現:
// Err returns the error that caused the node to shutdown or nil. Err blocks// until the node has fully shut down.func (n *Node) Err(ctx context.Context) error { select { case <-n.closed: return n.err case <-ctx.Done(): return ctx.Err() }}
Node.Err()函數阻塞在這裡,等待Node關閉。