Example #1
0
func main() {
	flag.Parse()
	cfgFile := flag.String("conf", "redis_ha.conf", "General configuration file")

	var cfg *config.Config
	cfg, err := config.ReadDefault(*cfgFile)
	if err != nil {
		log.Criticalf("Fail to find", *cfgFile, err)
	}

	// Get the logfile config
	logCfgFile, _ := cfg.String(S_SEC_PUBCONF, SC_SEELOG_CFG_FILE)
	InitSeeLog(logCfgFile)
	log.Info("REDIS HIGH AVAILABITY START")

	// Parse the config
	ParseConfig(cfg)

	log.Infof("Sentinel host: %s", gSentinelHost)
	// Get the master, and slave
	gRedisPool = RedisPoolConn()

	// create zknodes
	gZKConn = ZKConnect()

	for item := range gHANodeMap {
		haNode := gHANodeMap[item]

		GetRedisMaster(haNode)
		GetRedisSlaves(haNode)

		fmt.Printf("%s Master=%s\n", item, haNode.Master)
		for e := haNode.Slaves.Front(); e != nil; e = e.Next() {
			fmt.Printf("Slave=%s\n", e.Value.(string))
		}

		CreateRecursivePath(haNode.ZooMaster)
		CreateRecursivePath(haNode.ZooSlave)

		CreateAllNodes(haNode)
	}

	go RecoveryMain()
	MonitorSentinel()
	for {
		/*
		   gConn, err = redis.Dial("tcp", gSentinelHost)
		   if err != nil {
		       log.Criticalf("Connect to sentinel error:%s", err.Error())
		       continue
		   }
		*/
		MonitorSentinel()
		time.Sleep(1e9)
	}

	defer gZKConn.Close()
}
Example #2
0
func GetDB(inicfg *config.Config) *mgo.Database {
	ip, _ := inicfg.String("db", "ip")
	dbname, _ := inicfg.String("db", "db")
	session, err := mgo.Dial(ip)
	if err != nil {
		panic(err)
	}
	session.SetMode(mgo.Monotonic, true)
	return session.DB(dbname)
}
Example #3
0
func ParseConfig(cfg *config.Config) {
	gSentinelHost, _ = cfg.String(S_SEC_PUBCONF, SC_HOST)
	gZKSrv, _ = cfg.String(S_SEC_PUBCONF, SC_ZK_SRV)
	biStr, _ := cfg.String(S_SEC_PUBCONF, SC_NODELIST)
	biList := strings.Split(biStr, ",")
	for i := 0; i < len(biList); i++ {
		node := new(HANodeSt)
		node.Name = biList[i]
		node.Weight, _ = cfg.String(biList[i], SC_WEIGHT)
		node.ZooMaster, _ = cfg.String(biList[i], SC_ZK_PATH_M)
		node.ZooSlave, _ = cfg.String(biList[i], SC_ZK_PATH_S)
		gHANodeMap[biList[i]] = node
	}
}