Swarmkit筆記(3)——swarmd程式架構

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

agent.Node結構體有4channel,理解它們的作用就可以理解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關閉。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.