Docker Swarm Code Analysis Notes (9)--swarm Cluster,engine and Addengine

Source: Internet
Author: User
Tags docker swarm
This is a creation in Article, where the information may have evolved or changed.

swarm/cluster.goBelonging swarm package to this, it defines swarm the driver structure of the Cluster body:

// Cluster is exportedtype Cluster struct {    sync.RWMutex    eventHandlers     *cluster.EventHandlers    engines           map[string]*cluster.Engine    pendingEngines    map[string]*cluster.Engine    scheduler         *scheduler.Scheduler    discovery         discovery.Backend    pendingContainers map[string]*pendingContainer    overcommitRatio float64    engineOpts      *cluster.EngineOpts    createRetry     int64    TLSConfig       *tls.Config}

It is important that the cluster.Engine definition of this struct ( cluster/engine.go ), each cluster.Engine representing one Docker engine , namely Docker daemon :

// Engine represents a docker enginetype Engine struct {    sync.RWMutex    ID  string    IP  string    Addr string    Name string    Cpus int64    Memory  int64    Labels  map[string]string    Version string    stopCh  chan struct{}    refreshDelayer  *delayer    containers  map[string]*Container    images  []*Image    networksmap[string]*Network    volumes map[string]*Volume    client  dockerclient.Client    apiClient   engineapi.APIClient    eventHandlerEventHandler    state   engineState    lastError   string    updatedAt   time.Time    failureCountint    overcommitRatio int64    opts*EngineOpts    eventsMonitor   *EventsMonitor}

The cluster most important part of creating addEngine this method is Docker engine adding to cluster the medium:

  func (c *cluster) addengine (addr string) bool {//Check the engine is already registered by address. If C.hasenginebyaddr (addr) {return false} Engine: = cluster. Newengine (addr, c.overcommitratio, c.engineopts) if err: = engine. Registereventhandler (c); Err! = Nil {log. Error (ERR)}//ADD it to Pending engine map, indexed by address. This would prevent//duplicates from entering C.lock () c.pendingengines[addr] = Engine C.unlock ()//Valida    Tependingengine'll start a thread to validate the engine.    If the engine is reachable and valid, it's ll be monitored and updated in a loop. If engine is not reachable, pending engines'll be examined once in a while Go C.validatependingengine (engine) R Eturn true}  

addEngineFirst check whether the current is engine already present cluster , if not, it will be assigned a new one engine , add it first Cluster.pendingEngines , and start a new goroutine ( validatePendingEngine ) to check if it is a valid one engine .

In addition, the Engine.RegisterEventHandler member is essentially Cluster eventHandlers assigned to Engine.eventHandler :

// RegisterEventHandler registers an event handler.func (e *Engine) RegisterEventHandler(h EventHandler) error {    if e.eventHandler != nil {        return errors.New("event handler already set")    }    e.eventHandler = h    return nil}

It Cluster is eventHandlers assigned in newprimary, so it is essentially a set of Engine Cluster processing functions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.