Example #1
0
func (s *testUtilsSuite) testCreateServer(c *C, port int, auth string) *testServer {
	base := fmt.Sprintf("/tmp/test_reborn/test_proxy_utils/%d", port)

	err := os.MkdirAll(base, 0700)
	c.Assert(err, IsNil)

	conf := goleveldb.NewDefaultConfig()
	testdb, err := goleveldb.Open(path.Join(base, "db"), conf, false)
	c.Assert(err, IsNil)

	cfg := service.NewDefaultConfig()
	cfg.Listen = fmt.Sprintf("127.0.0.1:%d", port)
	cfg.PidFile = fmt.Sprintf(base, "qdb.pid")
	cfg.DumpPath = path.Join(base, "rdb.dump")
	cfg.SyncFilePath = path.Join(base, "sync.pipe")
	cfg.Auth = auth
	cfg.ReplBacklogSize = bytesize.MB

	store := store.New(testdb)
	server, err := service.NewServer(cfg, store)
	c.Assert(err, IsNil)
	go server.Serve()

	ss := new(testServer)
	ss.addr = cfg.Listen
	ss.store = store
	ss.server = server

	c.Assert(ss, NotNil)

	return ss
}
Example #2
0
File: main.go Project: CowLeo/qdb
func main() {
	usage := `
Usage:
    qdb-server [options]

Options:
    -L logfile                        log file path, if empty, use stdout
    -n N, --ncpu=N                    set runtime.GOMAXPROCS to N
    -c CONF, --config=CONF            specify the config file
    --repair                          repair database
    --dbtype=TYPE                     dtabase type, like rocksdb, leveldb, goleveldb	
    --dbpath=PATH                     database store path						
    --addr=ADDR                       service listening address	
    --auth=AUTH                       service auth
    --pidfile=FILE                    service pid file 
    --conn_timeout=N                  connection timeout after N seconds
    --dump_path=PATH                  path saving snapshot rdb file
    --sync_file_path=PATH             path saving replication syncing data
    --sync_file_size=SIZE             maximum file(bytes) size for replication syncing 
    --sync_buff_size=SIZE             maximum memory buffer size(bytes) for replication syncing
    --repl_backlog_file_path=PATH     path saving replication backlog data, if empty, use memory instead
    --repl_backlog_size=SIZE          maximum backlog size(bytes)
    --repl_ping_slave_period=N        Master pings slave in an interval(seconds) when replication
    --master_auth=MASTERAUTH          Master auth for replication
`
	d, err := docopt.Parse(usage, nil, true, "", false)
	if err != nil {
		log.Fatalf("parse arguments failed - %s", err)
	}

	if s, ok := d["-L"].(string); ok && len(s) > 0 {
		log.SetHighlighting(false)
		err = log.SetOutputByName(s)
		if err != nil {
			log.Fatalf("set log name failed - %s", err)
		}
	}

	if s, ok := d["--ncpu"].(string); ok && len(s) != 0 {
		if n, err := strconv.ParseInt(s, 10, 64); err != nil {
			log.Fatalf("parse --ncpu failed - %s", err)
		} else if n <= 0 || n > 64 {
			log.Fatalf("parse --ncpu = %d, only accept [1,64]", n)
		} else {
			runtime.GOMAXPROCS(int(n))
		}
	}

	args.config, _ = d["--config"].(string)
	args.repair, _ = d["--repair"].(bool)

	conf := &Config{
		DBType:    "goleveldb",
		DBPath:    "./var/testdb-goleveldb",
		LevelDB:   leveldb.NewDefaultConfig(),
		RocksDB:   rocksdb.NewDefaultConfig(),
		GoLevelDB: goleveldb.NewDefaultConfig(),
		Service:   service.NewDefaultConfig(),
	}

	if args.config != "" {
		if err := conf.LoadFromFile(args.config); err != nil {
			log.Fatalf("load config failed - %s", err)
		}
	}

	setStringFromOpt(&conf.DBType, d, "--dbtype")
	setStringFromOpt(&conf.DBPath, d, "--dbpath")

	setStringFromOpt(&conf.Service.Listen, d, "--addr")
	setStringFromOpt(&conf.Service.PidFile, d, "--pidfile")
	setStringFromOpt(&conf.Service.Auth, d, "--auth")
	setIntFromOpt(&conf.Service.ConnTimeout, d, "--conn_timeout")
	setStringFromOpt(&conf.Service.DumpPath, d, "--dump_path")
	setStringFromOpt(&conf.Service.SyncFilePath, d, "--sync_file_path")
	setIntFromOpt(&conf.Service.SyncFileSize, d, "--sync_file_size")
	setIntFromOpt(&conf.Service.SyncBuffSize, d, "--sync_buff_size")
	setStringFromOpt(&conf.Service.ReplBacklogFilePath, d, "--repl_backlog_file_path")
	setIntFromOpt(&conf.Service.ReplBacklogSize, d, "--repl_backlog_size")
	setIntFromOpt(&conf.Service.ReplPingSlavePeriod, d, "--repl_ping_slave_period")
	setStringFromOpt(&conf.Service.MasterAuth, d, "--master_auth")

	log.Infof("load config\n%s\n\n", conf)

	var db engine.Database
	var dbConf interface{}
	switch t := strings.ToLower(conf.DBType); t {
	default:
		log.Fatalf("unknown db type = '%s'", conf.DBType)
	case "leveldb":
		dbConf = conf.LevelDB
	case "rocksdb":
		dbConf = conf.RocksDB
	case "goleveldb":
		dbConf = conf.GoLevelDB
	}

	db, err = engine.Open(conf.DBType, conf.DBPath, dbConf, args.repair)

	if err != nil {
		log.Fatalf("open database failed - %s", err)
	}

	dbStore := store.New(db)

	if args.repair {
		return
	}

	server, err := service.NewServer(conf.Service, dbStore)
	if err != nil {
		log.Fatalf("create server failed - %s", err)
	}

	// create pid file
	createPidFile(conf.Service.PidFile)

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGTERM, os.Interrupt, os.Kill)

	go func(s *service.Server) {
		for _ = range c {
			log.Infof("interrupt and shutdown")

			// close server
			s.Close()

			// shutdown gracefully, remove pidfile
			os.Remove(conf.Service.PidFile)

			os.Exit(0)
		}
	}(server)

	if err := server.Serve(); err != nil {
		log.Errorf("service failed - %s", err)
	}

	server.Close()
}