Пример #1
0
func Test_GetEyeConfig(t *testing.T) {
	eye := &Eye{
		Servers:   []string{"localhost:7900 0 1 2", "127.0.0.1:7900 A -1 B"},
		Port:      7905,
		WebPort:   7908,
		Threads:   8,
		N:         3,
		W:         2,
		R:         1,
		Buckets:   16,
		Listen:    "0.0.0.0",
		Slow:      200,
		Proxies:   []string{"localhost:7905"},
		AccessLog: "/log/beansproxy/beansproxy.log",
		ErrorLog:  "/log/beansproxy/beansproxy_error.log",
		Basepath:  "/var/lib/beanseye",
		Readonly:  false,
	}

	content1, _ := goyaml.Marshal(eye)
	ioutil.WriteFile("./example.yaml", content1, os.ModePerm)

	var new_eye Eye
	content, _ := ioutil.ReadFile("./example.yaml")

	goyaml.Unmarshal(content, &new_eye)
	fmt.Println("eye")
	fmt.Println(*eye)
	fmt.Println("new_eye")
	fmt.Println(new_eye)

}
Пример #2
0
func GetFarmConfig(filename string) (farm *config.FarmConfig, err error) {
	var f config.FarmConfig
	var all []byte
	all, err = ioutil.ReadFile(filename)
	if err != nil {
		return
	} else {
		goyaml.Unmarshal(all, &f)
	}
	farm = &f
	return
}
Пример #3
0
func main() {
	flag.Parse()
	//c, err := config.ReadDefault(*conf)
	content, err := ioutil.ReadFile(*conf)
	if err != nil {
		log.Fatal("read config failed", *conf, err.Error())
	}

	if err := goyaml.Unmarshal(content, &eyeconfig); err != nil {
		log.Fatal("unmarshal yaml format config failed")
	}
	if *basepath == "" {
		if eyeconfig.Basepath == "" {
			curr_path, err1 := os.Getwd()
			if err1 != nil {
				log.Fatal("Cannot get pwd")
				return
			}
			*basepath = curr_path
		} else {
			*basepath = eyeconfig.Basepath
		}
	}
	Init(*basepath)

	if eyeconfig.Threads > 0 {
		runtime.GOMAXPROCS(eyeconfig.Threads)
	}

	if len(eyeconfig.Servers) == 0 {
		log.Fatal("no servers in conf")
	}
	server_configs := make(map[string][]string, len(eyeconfig.Servers))
	for _, server := range eyeconfig.Servers {
		fields := strings.Split(server, " ")
		server_configs[fields[0]] = fields[1:]
	}
	servers := make([]string, 0, len(server_configs))
	for server, _ := range server_configs {
		servers = append(servers, server)
	}

	if eyeconfig.WebPort <= 0 {
		log.Print("error webport in conf: ", eyeconfig.WebPort)
	} else if eyeconfig.Buckets <= 0 {
		log.Print("error buckets in conf: ", eyeconfig.Buckets)
	} else {
		server_stats = make([]map[string]interface{}, len(servers))
		bucket_stats = make([]string, eyeconfig.Buckets)
		go update_stats(servers, nil, server_stats, true)

		if len(eyeconfig.Proxies) > 0 {
			proxy_stats = make([]map[string]interface{}, len(eyeconfig.Proxies))
			go update_stats(eyeconfig.Proxies, nil, proxy_stats, false)
		}

		http.Handle("/", http.HandlerFunc(makeGzipHandler(Status)))
		http.Handle("/static/", http.FileServer(http.Dir(*basepath)))
		go func() {
			if len(eyeconfig.Listen) == 0 {
				eyeconfig.Listen = "0.0.0.0"
			}
			addr := fmt.Sprintf("%s:%d", eyeconfig.Listen, eyeconfig.WebPort)
			lt, e := net.Listen("tcp", addr)
			if e != nil {
				log.Println("monitor listen failed on ", addr, e)
			}
			log.Println("monitor listen on ", addr)
			http.Serve(lt, nil)
		}()
	}

	AllocLimit = *allocLimit

	var success bool

	if len(eyeconfig.AccessLog) > 0 {
		AccessLogPath = eyeconfig.AccessLog
		if success, err = OpenAccessLog(eyeconfig.AccessLog); !success {
			log.Fatalf("open AccessLog file in path: %s with error : %s", eyeconfig.AccessLog, err.Error())
		}
	}

	if len(eyeconfig.ErrorLog) > 0 {
		ErrorLogPath = eyeconfig.ErrorLog
		if success, err = OpenErrorLog(eyeconfig.ErrorLog); !success {
			log.Fatalf("open ErrorLog file in path: %s with error : %s", eyeconfig.ErrorLog, err.Error())
		}
	}

	slow := eyeconfig.Slow
	if slow == 0 {
		slow = 100
	}
	SlowCmdTime = time.Duration(int64(slow) * 1e6)

	readonly := eyeconfig.Readonly

	n := len(servers)
	if eyeconfig.N == 0 {
		eyeconfig.N = 3
	}
	N := min(eyeconfig.N, n)

	if eyeconfig.W == 0 {
		eyeconfig.W = 2
	}
	W := min(eyeconfig.W, n-1)

	if eyeconfig.R == 0 {
		eyeconfig.R = 1
	}
	R := eyeconfig.R

	//schd = NewAutoScheduler(servers, 16)
	schd = NewManualScheduler(server_configs, eyeconfig.Buckets, N)

	var client DistributeStorage
	if readonly {
		client = NewRClient(schd, N, W, R)
	} else {
		client = NewClient(schd, N, W, R)
	}

	http.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
	})

	proxy := NewServer(client)
	if eyeconfig.Port <= 0 {
		log.Fatal("error proxy port in config it is ", eyeconfig.Port)
	}
	addr := fmt.Sprintf("%s:%d", eyeconfig.Listen, eyeconfig.Port)
	if e := proxy.Listen(addr); e != nil {
		log.Fatal("proxy listen failed", e.Error())
	}

	log.Println("proxy listen on ", addr)
	proxy.Serve()
	log.Print("shut down gracefully.")
}
Пример #4
0
func Test_GetFarmConfig(t *testing.T) {
	var f FarmConfig
	f.Db = "highway"
	f.Addr = "127.0.0.1"
	f.Port = uint16(3000)
	f.Hosts = make([]HostConfig, 4)
	f.Hosts[0].Name = "master"
	f.Hosts[0].Addr = "127.0.0.1:3306"
	f.Hosts[0].Master = true
	f.Hosts[0].Weight = 0
	f.Hosts[0].ConnMax = 400
	f.Hosts[0].UserPools = []UserPoolConfig{
		{User: "******", Passwd: "code", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
	}

	f.Hosts[1].Name = "slave"
	f.Hosts[1].Addr = "127.0.0.1:3306"
	f.Hosts[1].Master = false
	f.Hosts[1].Weight = 50
	f.Hosts[1].ConnMax = 400
	f.Hosts[1].UserPools = []UserPoolConfig{
		{User: "******", Passwd: "code", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
	}

	f.Hosts[2].Name = "backup"
	f.Hosts[2].Addr = "localhost:3306"
	f.Hosts[2].Master = false
	f.Hosts[2].Weight = 30
	f.Hosts[2].ConnMax = 400
	f.Hosts[2].UserPools = []UserPoolConfig{
		{User: "******", Passwd: "code", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
	}

	f.Hosts[3].Name = "query"
	f.Hosts[3].Addr = "localhost:3306"
	f.Hosts[3].Master = false
	f.Hosts[3].Weight = 20
	f.Hosts[3].ConnMax = 400
	f.Hosts[3].UserPools = []UserPoolConfig{
		{User: "******", Passwd: "code", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
		{User: "******", Passwd: "password", Max: 100, Min: 50},
	}

	out, e := goyaml.Marshal(f)
	if e != nil {
		t.Errorf("goyaml marshal failed")
	}

	var new_f FarmConfig
	goyaml.Unmarshal(out, &new_f)
	if !test_comp_farm_config(&f, &new_f) {
		t.Errorf("f not same to new_f")
	}
}