func apiInitSlots(r *http.Request) (int, string) { r.ParseForm() isForce := false val := r.FormValue("is_force") if len(val) > 0 && (val == "1" || val == "true") { isForce = true } conn := CreateZkConn() defer conn.Close() if !isForce { s, _ := models.Slots(conn, globalEnv.ProductName()) if len(s) > 0 { return 500, "slots already initialized, you may use 'is_force' flag and try again." } } if err := models.InitSlotSet(conn, globalEnv.ProductName(), models.DEFAULT_SLOT_NUM); err != nil { log.Warning(err) return 500, err.Error() } return jsonRetSucc() }
func runSlotInit() error { err := models.InitSlotSet(zkConn, productName, models.DEFAULT_SLOT_NUM) if err != nil { return errors.Trace(err) } return nil }
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) }
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) }) }
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) }) }