示例#1
0
// go run goredis-server.go -h localhost -p 1602
// go run goredis-server.go -procs 8 -p 17600
// go run goredis-server.go -slaveof localhost:1603
func main() {
	version := flag.Bool("v", false, "print goredis-server version")
	host := flag.String("h", "", "server host")
	port := flag.Int("p", 1602, "server port")
	slaveof := flag.String("slaveof", "", "replication")
	procs := flag.Int("procs", 8, "GOMAXPROCS")
	repair := flag.Bool("repair", false, "repaire rocksdb")
	flag.Parse()

	if *version {
		fmt.Println("goredis-server", goredis_server.VERSION)
		return
	}

	runtime.GOMAXPROCS(*procs)

	opt := goredis_server.NewOptions()
	opt.SetBind(fmt.Sprintf("%s:%d", *host, *port))
	opt.SetDirectory(dbHome(*port))
	if len(*slaveof) > 0 {
		h, p, e := splitHostPort(*slaveof)
		if e != nil {
			panic(e)
		}
		opt.SetSlaveOf(h, p)
	}

	// 重定向日志输出位置
	redirectLogOutput(opt.Directory())

	// repair
	if *repair {
		dbhome := opt.Directory() + "db0"
		finfo, e1 := os.Stat(dbhome)
		if os.IsNotExist(e1) || !finfo.IsDir() {
			stdlog.Println("db not exist")
			return
		} else {
			stdlog.Println("start repair", dbhome)
			levelredis.Repair(dbhome)
			stdlog.Println("repair finish")
		}
		return
	}

	stdlog.Println("========================================")
	server := goredis_server.NewGoRedisServer(opt)
	if err := server.Init(); err != nil {
		panic(err)
	}
	if err := server.Listen(); err != nil {
		panic(err)
	}
}
示例#2
0
// go run goredis-server.go -h localhost -p 1602
// go run goredis-server.go -procs 8 -p 17600
// go run goredis-server.go -slaveof localhost:1603
// go run goredis-server.go -dbpath /data/ -logpath /home/logs/
func main() {
	version := flag.Bool("v", false, "print version")
	host := flag.String("h", "0.0.0.0", "server host")
	port := flag.Int("p", 1602, "server port")
	slaveof := flag.String("slaveof", "", "replication")
	procs := flag.Int("procs", 8, "GOMAXPROCS, CPU")
	repair := flag.Bool("repair", false, "repair rocksdb")
	dbpath := flag.String("dbpath", "/data/", "rocksdb path, recommend use SSD")
	logpath := flag.String("logpath", "/data/", "all logs, include synclog,aof")
	flag.Parse()

	if *version {
		fmt.Println("goredis-server", goredis_server.VERSION)
		return
	}

	if !dirExist(*dbpath) {
		stdlog.Println("-dbpath", *dbpath, "not exist")
		return
	}
	if !dirExist(*logpath) {
		stdlog.Println("-logpath", *logpath, "not exist")
		return
	}

	runtime.GOMAXPROCS(*procs)

	// Options
	opt := goredis_server.NewOptions()
	opt.SetHost(*host)
	opt.SetPort(*port)
	opt.SetDBPath(joinGoRedisPath(*dbpath, *port))
	opt.SetLogPath(joinGoRedisPath(*logpath, *port))
	// ensure
	os.Mkdir(opt.DBPath(), os.ModePerm)
	os.Mkdir(opt.LogPath(), os.ModePerm)

	// split -slaveof host:port
	if len(*slaveof) > 0 {
		hostPort := strings.Split(*slaveof, ":")
		if len(hostPort) != 2 {
			panic("bad slaveof")
		}
		p, e := strconv.Atoi(hostPort[1])
		if e != nil {
			panic(e)
		}
		opt.SetSlaveOf(hostPort[0], p)
	}

	// 重定向日志输出位置
	if err := redirectStdout(opt.LogPath()); err != nil {
		panic(err)
	}

	// repair
	if *repair {
		dbhome := filepath.Join(opt.DBPath(), "db0")
		if !dirExist(dbhome) {
			stdlog.Println("db not exist")
		} else {
			stdlog.Println("start repair", dbhome)
			levelredis.Repair(dbhome)
			stdlog.Println("repair finish")
		}
		return
	}

	stdlog.Println("========================================")
	stdlog.Println("server init, version", goredis_server.VERSION, "...")
	stdlog.Printf("dbpath:%s, logpath:%s\n", opt.DBPath(), opt.LogPath())

	// GoRedis Server
	server := goredis_server.NewGoRedisServer(opt)
	if err := server.Init(); err != nil {
		panic(err)
	}
	if err := server.Listen(); err != nil {
		panic(err)
	}
}