示例#1
0
// main loop of the HostAgent
func (a *HostAgent) start() {
	glog.V(1).Info("Starting HostAgent")
	for {
		// create a wrapping function so that client.Close() can be handled via defer
		keepGoing := func() bool {
			conn, zkEvt, err := zk.Connect(a.zookeepers, time.Second*10)
			if err != nil {
				glog.V(0).Info("Unable to connect, retrying.")
				return true
			}

			connectEvent := false
			for !connectEvent {
				select {
				case errc := <-a.closing:
					glog.V(0).Info("Received shutdown notice")
					errc <- errors.New("Unable to connect to zookeeper")
					return false

				case evt := <-zkEvt:
					glog.V(1).Infof("Got ZK connect event: %v", evt)
					if evt.State == zk.StateConnected {
						connectEvent = true
					}
				}
			}
			defer conn.Close() // Executed after lambda function finishes

			zzk.CreateNode(zzk.SCHEDULER_PATH, conn)
			node_path := zzk.HostPath(a.hostId)
			zzk.CreateNode(node_path, conn)
			glog.V(0).Infof("Connected to zookeeper node %s", node_path)
			return a.processChildrenAndWait(conn)
		}()
		if !keepGoing {
			break
		}
	}
}
示例#2
0
func (s *scheduler) loop() {
	glog.V(3).Infoln("entering scheduler")

	var err error
	var this_node string
	defer func() {
		glog.V(3).Infoln("leaving scheduler")
		s.shutdown <- err
	}()

	// create scheduler node
	scheduler_path := s.cluster_path + "/election"
	err = zzk.CreateNode(scheduler_path, s.conn)
	if err != nil {
		glog.Error("could not create scheduler node: ", err)
		return
	}

	services_path := s.cluster_path + "/services"
	err = zzk.CreateNode(services_path, s.conn)
	if err != nil {
		glog.Error("could not create services node: ", err)
		return
	}

	// voter node path
	voter_path := scheduler_path + "/"
	instance_data := []byte(s.instance_id)
	err = zzk.DeleteNodebyData(scheduler_path, s.conn, instance_data)
	if err != nil {
		glog.Error("could not remove old over node: ", err)
		return
	}

	// create voting node
	this_node, err = s.conn.Create(
		voter_path, instance_data,
		zk.FlagEphemeral|zk.FlagSequence,
		zk.WorldACL(zk.PermAll))
	if err != nil {
		glog.Error("Could not create voting node:", err)
		return
	}
	glog.V(1).Infof("Created voting node: %s", this_node)

	for {
		s.conn.Sync(scheduler_path)
		// get children
		children, _, err := s.conn.Children(scheduler_path)
		if err != nil {
			glog.Error("Could not get children of schduler path")
			return
		}
		sort.Strings(children)

		leader_path := voter_path + children[0]
		if this_node == leader_path {
			glog.V(0).Info("I am the leader!")
			exists, _, event, err := s.conn.ExistsW(leader_path)
			if err != nil {
				if err == zk.ErrNoNode {
					continue
				}
				return
			}
			if !exists {
				continue
			}
			s.zkleaderFunc(s.cpDao, s.conn, event)
			return
		} else {
			glog.V(1).Infof("I must wait for %s to die.", children[0])

			exists, _, event, err := s.conn.ExistsW(leader_path)
			if err != nil && err != zk.ErrNoNode {
				return
			}
			if err == zk.ErrNoNode {
				continue
			}
			if err == nil && !exists {
				continue
			}
			select {
			case <-zzk.TimeoutAfter(time.Second * 30):
				glog.V(1).Info("I've been listening. I'm going to reinitialize.")
				continue
			case errc := <-s.closing:
				errc <- err
				return
			case evt := <-event:
				if evt.Type == zk.EventNodeDeleted {
					continue
				}
				return
			}
		}
	}
}