示例#1
0
// add redis server to exist server group
func apiAddServerToGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	lock.Lock(fmt.Sprintf("add server to group,  %+v", server))
	defer func() {
		err := lock.Unlock()
		if err != nil {
			log.Warning(err)
		}
	}()
	// check group exists first
	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)

	exists, err := serverGroup.Exists(safeZkConn)
	if err != nil {
		log.Warning(err)
		return 500, err.Error()
	}

	// create new group if not exists
	if !exists {
		if err := serverGroup.Create(safeZkConn); err != nil {
			return 500, err.Error()
		}
	}

	if err := serverGroup.AddServer(safeZkConn, &server); err != nil {
		log.Warning(errors.ErrorStack(err))
		return 500, err.Error()
	}

	return jsonRetSucc()
}
示例#2
0
// add redis server to exist server group
func apiAddServerToGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	if err := lock.LockWithTimeout(0, fmt.Sprintf("add server to group,  %+v", server)); err != nil {
		return 500, err.Error()
	}
	defer func() {
		err := lock.Unlock()
		if err != nil && err != zk.ErrNoNode {
			log.ErrorErrorf(err, "unlock node failed")
		}
	}()
	// check group exists first
	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)

	exists, err := serverGroup.Exists(safeZkConn)
	if err != nil {
		log.ErrorErrorf(err, "check group exits failed")
		return 500, err.Error()
	}

	// create new group if not exists
	if !exists {
		if err := serverGroup.Create(safeZkConn); err != nil {
			return 500, err.Error()
		}
	}

	if err := serverGroup.AddServer(safeZkConn, &server, globalEnv.Password()); err != nil {
		log.ErrorErrorf(err, "add server to group failed")
		return 500, err.Error()
	}

	return jsonRetSucc()
}
示例#3
0
func runAddServerGroup(groupId int) error {
	serverGroup := models.NewServerGroup(productName, groupId)
	if err := serverGroup.Create(zkConn); err != nil {
		return err
	}
	return nil
}
示例#4
0
// add redis server to exist server group
func apiAddServerToGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])

	conn := CreateZkConn()
	defer conn.Close()

	lock := utils.GetZkLock(conn, productName)
	lock.Lock(fmt.Sprintf("add server to group,  %+v", server))
	defer func() {
		err := lock.Unlock()
		if err != nil {
			log.Warning(err)
		}
	}()
	// check group exists first
	serverGroup := models.NewServerGroup(productName, groupId)

	exists, err := serverGroup.Exists(conn)
	if err != nil {
		log.Warning(err)
		return 500, err.Error()
	}
	if !exists {
		return jsonRetFail(-1, "group not exists")
	}

	if err := serverGroup.AddServer(conn, &server); err != nil {
		log.Warning(err)
		return 500, err.Error()
	}

	return jsonRetSucc()
}
示例#5
0
func init() {
	conn = zkhelper.NewConn()
	conf = &Config{
		proxyId:     "proxy_test",
		productName: "test",
		zkAddr:      "localhost:2181",
		fact:        func(string, int) (zkhelper.Conn, error) { return conn, nil },
		proto:       "tcp4",
	}

	//init action path
	prefix := models.GetWatchActionPath(conf.productName)
	err := models.CreateActionRootPath(conn, prefix)
	assert.MustNoError(err)

	//init slot
	err = models.InitSlotSet(conn, conf.productName, 1024)
	assert.MustNoError(err)

	//init  server group
	g1 := models.NewServerGroup(conf.productName, 1)
	g1.Create(conn)
	g2 := models.NewServerGroup(conf.productName, 2)
	g2.Create(conn)

	redis1, _ = miniredis.Run()
	redis2, _ = miniredis.Run()

	s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
	s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

	g1.AddServer(conn, s1, "")
	g2.AddServer(conn, s2, "")

	//set slot range
	err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	s = New(":19000", ":11000", conf)

	err = models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
	assert.MustNoError(err)
}
示例#6
0
func runRemoveServerGroup(groupId int) error {
	serverGroup := models.NewServerGroup(productName, groupId)
	err := serverGroup.Remove(zkConn)
	if err != nil {
		log.Warning(err)
		return err
	}
	return nil
}
示例#7
0
func runAddServerGroup(groupId int) error {
	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
	var v interface{}
	err := callApi(METHOD_PUT, "/api/server_groups", serverGroup, &v)
	if err != nil {
		return err
	}
	fmt.Println(jsonify(v))
	return nil
}
示例#8
0
func apiRemoveServerFromGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	lock.Lock(fmt.Sprintf("removing server from group, %+v", server))
	defer func() {
		err := lock.Unlock()
		if err != nil {
			log.Warning(err)
		}
	}()

	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
	err := serverGroup.RemoveServer(safeZkConn, server.Addr)
	if err != nil {
		log.Warning(err)
		return 500, err.Error()
	}
	return jsonRetSucc()
}
示例#9
0
func apiRemoveServerGroup(param martini.Params) (int, string) {
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	lock.Lock(fmt.Sprintf("removing group %s", param["id"]))

	defer func() {
		err := lock.Unlock()
		if err != nil {
			log.Warning(err)
		}
	}()

	groupId, _ := strconv.Atoi(param["id"])
	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
	if err := serverGroup.Remove(safeZkConn); err != nil {
		log.Error(errors.ErrorStack(err))
		return 500, err.Error()
	}

	return jsonRetSucc()
}
示例#10
0
func runAddServerToGroup(groupId int, addr string, role string) error {
	serverGroup := models.NewServerGroup(productName, groupId)
	if len(addr) > 0 && len(role) > 0 {
		exists, err := serverGroup.Exists(zkConn)
		if err != nil {
			log.Warning(err)
			return err
		}
		// if server group not exists, create it first
		if !exists {
			serverGroup.Create(zkConn)
		}
		server := models.NewServer(role, addr)
		if err := serverGroup.AddServer(zkConn, server); err != nil {
			log.Warning(err)
			return err
		}
	}
	return nil
}
示例#11
0
func apiRemoveServerFromGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	if err := lock.LockWithTimeout(0, fmt.Sprintf("removing server from group, %+v", server)); err != nil {
		return 500, err.Error()
	}
	defer func() {
		err := lock.Unlock()
		if err != nil && err != zk.ErrNoNode {
			log.ErrorErrorf(err, "unlock node failed")
		}
	}()

	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
	err := serverGroup.RemoveServer(safeZkConn, server.Addr)
	if err != nil {
		log.ErrorErrorf(err, "remove group %d failed", groupId)
		return 500, err.Error()
	}
	return jsonRetSucc()
}
示例#12
0
func apiRemoveServerGroup(param martini.Params) (int, string) {
	lock := utils.GetZkLock(safeZkConn, globalEnv.ProductName())
	if err := lock.LockWithTimeout(0, fmt.Sprintf("removing group %s", param["id"])); err != nil {
		return 500, err.Error()
	}

	defer func() {
		err := lock.Unlock()
		if err != nil && err != zk.ErrNoNode {
			log.ErrorErrorf(err, "unlock node failed")
		}
	}()

	groupId, _ := strconv.Atoi(param["id"])
	serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
	if err := serverGroup.Remove(safeZkConn); err != nil {
		log.ErrorErrorf(err, "remove server group failed")
		return 500, err.Error()
	}

	return jsonRetSucc()
}
示例#13
0
func apiRemoveServerFromGroup(server models.Server, param martini.Params) (int, string) {
	groupId, _ := strconv.Atoi(param["id"])

	conn := CreateZkConn()
	defer conn.Close()

	lock := utils.GetZkLock(conn, productName)
	lock.Lock(fmt.Sprintf("remove server from group, %+v", server))
	defer func() {
		err := lock.Unlock()
		if err != nil {
			log.Warning(err)
		}
	}()

	serverGroup := models.NewServerGroup(productName, groupId)
	err := serverGroup.RemoveServer(conn, server)
	if err != nil {
		log.Warning(err)
		return 500, err.Error()
	}
	return jsonRetSucc()
}
示例#14
0
文件: router_test.go 项目: jcru/codis
func InitEnv() {
	go once.Do(func() {
		conn = zkhelper.NewConn()
		conf = &Config{
			proxyId:     "proxy_test",
			productName: "test",
			zkAddr:      "localhost:2181",
			fact:        func(string) (zkhelper.Conn, error) { return conn, nil },
			proto:       "tcp4",
		}

		//init action path
		prefix := models.GetWatchActionPath(conf.productName)
		err := models.CreateActionRootPath(conn, prefix)
		assert.MustNoError(err)

		//init slot
		err = models.InitSlotSet(conn, conf.productName, 1024)
		assert.MustNoError(err)

		//init  server group
		g1 := models.NewServerGroup(conf.productName, 1)
		g1.Create(conn)
		g2 := models.NewServerGroup(conf.productName, 2)
		g2.Create(conn)

		redis1, _ = miniredis.Run()
		redis2, _ = miniredis.Run()

		s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
		s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

		g1.AddServer(conn, s1, "")
		g2.AddServer(conn, s2, "")

		//set slot range
		err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
		assert.MustNoError(err)

		err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
		assert.MustNoError(err)

		go func() { //set proxy online
			time.Sleep(3 * time.Second)
			err := models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
			assert.MustNoError(err)

			time.Sleep(2 * time.Second)
			proxyMutex.Lock()
			defer proxyMutex.Unlock()
			assert.Must(s.info.State == models.PROXY_STATE_ONLINE)
		}()

		proxyMutex.Lock()
		s, err = NewServer(":19000", ":11000", conf)
		assert.MustNoError(err)
		proxyMutex.Unlock()
		s.Serve()
	})

	waitonce.Do(func() {
		time.Sleep(10 * time.Second)
	})
}
示例#15
0
func InitEnv() {
	go once.Do(func() {
		conn = zkhelper.NewConn()
		conf = &Conf{
			proxyId:     "proxy_test",
			productName: "test",
			zkAddr:      "localhost:2181",
			netTimeout:  5,
			f:           func(string) (zkhelper.Conn, error) { return conn, nil },
			proto:       "tcp4",
		}

		//init action path
		prefix := models.GetWatchActionPath(conf.productName)
		err := models.CreateActionRootPath(conn, prefix)
		if err != nil {
			log.Fatal(err)
		}

		//init slot
		err = models.InitSlotSet(conn, conf.productName, 1024)
		if err != nil {
			log.Fatal(err)
		}

		//init  server group
		g1 := models.NewServerGroup(conf.productName, 1)
		g1.Create(conn)
		g2 := models.NewServerGroup(conf.productName, 2)
		g2.Create(conn)

		redis1, _ = miniredis.Run()
		redis2, _ = miniredis.Run()

		s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
		s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

		g1.AddServer(conn, s1)
		g2.AddServer(conn, s2)

		//set slot range
		err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
		if err != nil {
			log.Fatal(err)
		}

		err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
		if err != nil {
			log.Fatal(err)
		}

		go func() { //set proxy online
			time.Sleep(3 * time.Second)
			err := models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
			if err != nil {
				log.Fatal(errors.ErrorStack(err))
			}
			time.Sleep(2 * time.Second)
			proxyMutex.Lock()
			defer proxyMutex.Unlock()
			pi := s.getProxyInfo()
			if pi.State != models.PROXY_STATE_ONLINE {
				log.Fatalf("should be online, we got %s", pi.State)
			}
		}()

		proxyMutex.Lock()
		s = NewServer(":19000", ":11000",
			conf,
		)
		proxyMutex.Unlock()
		s.Run()
	})

	waitonce.Do(func() {
		time.Sleep(10 * time.Second)
	})
}