func (c *ClusterManager) Start() error { logrus.Info("Cluster manager starting...") c.gEnabled = true c.selfNode = api.Node{} c.selfNode.GenNumber = uint64(time.Now().UnixNano()) c.selfNode.Id = c.config.NodeId c.selfNode.Status = api.StatusOk c.selfNode.Ip, _ = externalIp(&c.config) c.selfNode.NodeData = make(map[string]interface{}) c.system = systemutils.New() // Start the gossip protocol. // XXX Make the port configurable. gob.Register(api.Node{}) c.g = gossip.New("0.0.0.0:9002", types.NodeId(c.config.NodeId), c.selfNode.GenNumber) c.g.SetGossipInterval(2 * time.Second) kvdb := kvdb.Instance() kvlock, err := kvdb.Lock("cluster/lock", 60) if err != nil { logrus.Panic("Fatal, Unable to obtain cluster lock.", err) } defer kvdb.Unlock(kvlock) db, err := readDatabase() if err != nil { logrus.Panic(err) } if db.Status == api.StatusInit { logrus.Info("Will initialize a new cluster.") c.status = api.StatusOk db.Status = api.StatusOk self, _ := c.initNode(&db) err = c.initCluster(&db, self, false) if err != nil { logrus.Error("Failed to initialize the cluster.", err) return err } // Update the new state of the cluster in the KV Database err := writeDatabase(&db) if err != nil { logrus.Error("Failed to save the database.", err) return err } } else if db.Status&api.StatusOk > 0 { logrus.Info("Cluster state is OK... Joining the cluster.") c.status = api.StatusOk self, exist := c.initNode(&db) err = c.joinCluster(&db, self, exist) if err != nil { logrus.Error("Failed to join cluster.", err) return err } err := writeDatabase(&db) if err != nil { return err } } else { return errors.New("Fatal, Cluster is in an unexpected state.") } // Start heartbeating to other nodes. c.g.Start() go c.heartBeat() return nil }
func (c *ClusterManager) Start() error { log.Info("Cluster manager starting...") kvdb := kv.Instance() // Start the gossip protocol. // XXX Make the port configurable. gob.Register(api.Node{}) c.g = gossip.New("0.0.0.0:9002", gossiptypes.NodeId(c.config.NodeId)) c.g.SetGossipInterval(2 * time.Second) kvlock, err := kvdb.Lock("cluster/lock", 60) if err != nil { log.Panic("Fatal, Unable to obtain cluster lock.", err) } db, err := readDatabase() if err != nil { log.Panic(err) } if db.Status == api.StatusInit { log.Info("Will initialize a new cluster.") c.status = api.StatusOk db.Status = api.StatusOk self, _ := c.initNode(&db) err = c.initCluster(&db, self, false) if err != nil { kvdb.Unlock(kvlock) log.Error("Failed to initialize the cluster.", err) log.Panic(err) } // Update the new state of the cluster in the KV Database err = writeDatabase(&db) if err != nil { log.Errorf("Failed to save the database.", err) log.Panic(err) } err = kvdb.Unlock(kvlock) if err != nil { log.Panic("Fatal, unable to unlock cluster... Did something take too long to initialize?", err) } } else if db.Status&api.StatusOk > 0 { log.Info("Cluster state is OK... Joining the cluster.") c.status = api.StatusOk self, exist := c.initNode(&db) err = c.joinCluster(&db, self, exist) if err != nil { kvdb.Unlock(kvlock) log.Panic(err) } err = writeDatabase(&db) if err != nil { log.Panic(err) } err = kvdb.Unlock(kvlock) if err != nil { log.Panic("Fatal, unable to unlock cluster... Did something take too long to initialize?", err) } } else { kvdb.Unlock(kvlock) err = errors.New("Fatal, Cluster is in an unexpected state.") log.Panic(err) } // Start heartbeating to other nodes. go c.heartBeat() return nil }
func (c *ClusterManager) Start() error { dlog.Infoln("Cluster manager starting...") c.gEnabled = true c.selfNode = api.Node{} c.selfNode.GenNumber = uint64(time.Now().UnixNano()) c.selfNode.Id = c.config.NodeId c.selfNode.Status = api.Status_STATUS_OK c.selfNode.MgmtIp, c.selfNode.DataIp, _ = ExternalIp(&c.config) c.selfNode.NodeData = make(map[string]interface{}) c.system = systemutils.New() // Start the gossip protocol. // XXX Make the port configurable. gob.Register(api.Node{}) c.gossip = gossip.New("0.0.0.0:9002", types.NodeId(c.config.NodeId), c.selfNode.GenNumber) c.gossip.SetGossipInterval(2 * time.Second) kvdb := kvdb.Instance() kvlock, err := kvdb.Lock(clusterLockKey, 60) if err != nil { dlog.Panicln("Fatal, Unable to obtain cluster lock.", err) } defer kvdb.Unlock(kvlock) db, err := readDatabase() if err != nil { dlog.Panicln(err) } // Cluster database max size... 0 if unlimited. c.size = db.Size if db.Status == api.Status_STATUS_INIT { dlog.Infoln("Will initialize a new cluster.") c.status = api.Status_STATUS_OK db.Status = api.Status_STATUS_OK self, _ := c.initNode(&db) err = c.initCluster(&db, self, false) if err != nil { dlog.Errorln("Failed to initialize the cluster.", err) return err } // Update the new state of the cluster in the KV Database err := writeDatabase(&db) if err != nil { dlog.Errorln("Failed to save the database.", err) return err } } else if db.Status&api.Status_STATUS_OK > 0 { dlog.Infoln("Cluster state is OK... Joining the cluster.") c.status = api.Status_STATUS_OK self, exist := c.initNode(&db) err = c.joinCluster(&db, self, exist) if err != nil { dlog.Errorln("Failed to join cluster.", err) return err } err := writeDatabase(&db) if err != nil { return err } } else { return errors.New("Fatal, Cluster is in an unexpected state.") } // Start heartbeating to other nodes. go c.startHeartBeat() go c.updateClusterStatus() kvdb.WatchKey(ClusterDBKey, 0, nil, c.watchDB) return nil }