Пример #1
0
func Start(config BdbConfig) *DbEnv {
	args := make([]string, 0, 20)
	if config.AckAll && config.RepMgr {
		args = append(args, "-a")
	}
	if config.Bulk {
		args = append(args, "-b")
	}
	if config.Master {
		args = append(args, "-M")
	} else if !config.RepMgr {
		args = append(args, "-C")
	}

	if config.HomeDir == "" {
		log.Fatal("bdb|homedir_missing")
	}
	args = append(args, "-h", config.HomeDir)
	_, err := os.Stat(config.HomeDir)
	if err != nil && os.IsNotExist(err) {
		os.Mkdir(config.HomeDir, os.ModePerm)
	}
	if config.LocalAddr == "" {
		log.Fatal("bdb|localaddr_missing")
	}
	if config.RepMgr && config.Master {
		args = append(args, "-L", config.LocalAddr)
	} else {
		args = append(args, "-l", config.LocalAddr)
	}

	if config.DisableElection {
		args = append(args, "-p", "0")
	} else if config.Priority != 0 {
		args = append(args, "-p", strconv.FormatInt(int64(config.Priority), 10))
	}

	if config.RepMgr && config.RemotePeer != "" {
		if len(config.RemoteAddr) != 0 {
			log.Fatal("bdb|conflict_RemoteAddr_RemotePeer")
		}
		args = append(args, "-R", config.RemotePeer)
	} else {
		for _, remote := range config.RemoteAddr {
			args = append(args, "-r", remote)
		}
	}

	if config.Verbose {
		args = append(args, "-v")
	}

	dbenv := new(DbEnv)
	dbenv.waitStop.Add(1)
	dbenv.waitExit.Add(1)
	dbenv.waitReady.Add(1)
	go func() {
		argv := make([]*C.char, len(args)+1)
		for i, arg := range args {
			argv[i+1] = C.CString(arg)
		}
		argv[0] = C.CString("bdbd")

		if config.RepMgr {
			log.Info("start_repmgr|%v", args)
			C.start_mgr(C.int(len(argv)), &argv[0], C.int(config.Flush), unsafe.Pointer(dbenv))
		} else {
			log.Info("start_base|%v", args)
			C.start_base(C.int(len(argv)), &argv[0], C.int(config.Flush), unsafe.Pointer(dbenv))
		}

		for _, arg := range argv {
			C.free(unsafe.Pointer(arg))
		}

		dbenv.waitExit.Done()
	}()
	dbenv.waitReady.Wait()
	return dbenv
}
Пример #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU() * 4)

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	// master
	//go run main.go -h db1 -M -l 127.0.0.1:2345 -r 127.0.0.1:2346
	// slave
	//go run main.go -h db2 -C -l 127.0.0.1:2346 -r 127.0.0.1:2345

	configFile := "bdbd.conf"
	if len(os.Args) > 1 {
		configFile = os.Args[1]
	}
	configstr, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Println("ERROR: read config file failed:", err)
		os.Exit(1)
	}
	var config struct {
		Bdb    bdb.BdbConfig        `toml:"bdb"`
		Logger []mylog.LoggerDefine `toml:"logger"`
		Server struct {
			Listen string
		} `toml:"server"`
	}
	config.Bdb.Flush = 1
	_, err = toml.Decode(string(configstr), &config)
	if err != nil {
		log.Println("ERROR: decode config failed:", err)
		os.Exit(1)
	}

	mylog.Init(config.Logger)

	mylog.Info("bdb|starting")
	dbenv := bdb.Start(config.Bdb)
	mylog.Info("bdb|started")

	/*
		db, err := dbenv.GetDb("test")
		if err != nil {
			mylog.Fatal("dberr:%s", err.Error())
		}
		for i := 0; i < 1000000; i++ {
			if i%1000 == 0 {
				mylog.Info("%d", i)
			}
			k := fmt.Sprintf("k_%v", i)
			db.Set([]byte(k), []byte(k))
		}
		db.Close()
		dbenv.Exit()
		mylog.Fatal("end")
	*/

	go func() {
		addr, err := net.ResolveTCPAddr("tcp", config.Server.Listen)
		if err != nil {
			mylog.Fatal("ResolveTCPAddr|%s", err.Error())
		}
		listener, err := net.ListenTCP("tcp", addr)
		if err != nil {
			mylog.Fatal("Server|ListenTCP|%s", err.Error())
		}
		for {
			client, err := listener.AcceptTCP()
			if err != nil {
				mylog.Error("Server|%s", err.Error())
			}
			client.SetKeepAlive(true)
			conn := server.NewConn(client, dbenv)
			go conn.Start()
		}
	}()

	server.Start(dbenv)
	mylog.Info("start")
	<-signalChan
	server.Exit()
	dbenv.Exit()
	mylog.Info("bye")
}