Esempio n. 1
0
func (top *Topology) Close(proxyName string) {
	// delete fence znode
	pi, err := models.GetProxyInfo(top.zkConn, top.ProductName, proxyName)
	if err != nil {
		log.Errorf("killing fence error, proxy %s is not exists", proxyName)
	} else {
		zkhelper.DeleteRecursive(top.zkConn, path.Join(models.GetProxyFencePath(top.ProductName), pi.Addr), -1)
	}
	// delete ephemeral znode
	zkhelper.DeleteRecursive(top.zkConn, path.Join(models.GetProxyPath(top.ProductName), proxyName), -1)
	top.zkConn.Close()
}
Esempio n. 2
0
func (m *MigrateManager) RemoveMigrations() error {
	tasks, _, _ := safeZkConn.Children(getMigrateTasksPath(m.productName))
	for _, id := range tasks {
		zkhelper.DeleteRecursive(safeZkConn, getMigrateTasksPath(m.productName)+"/"+id, -1)
	}
	return nil
}
Esempio n. 3
0
func ForceRemoveDeadFence(zkConn zkhelper.Conn, productName string) error {
	proxies, err := ProxyList(zkConn, productName, func(p *ProxyInfo) bool {
		return p.State == PROXY_STATE_ONLINE
	})
	if err != nil {
		return errors.Trace(err)
	}
	fenceProxies, err := GetFenceProxyMap(zkConn, productName)
	if err != nil {
		return errors.Trace(err)
	}
	// remove online proxies's fence
	for _, proxy := range proxies {
		delete(fenceProxies, proxy.Addr)
	}

	// delete dead fence in zookeeper
	path := GetProxyFencePath(productName)
	for remainFence, _ := range fenceProxies {
		fencePath := filepath.Join(path, remainFence)
		log.Info("removing fence: ", fencePath)
		if err := zkhelper.DeleteRecursive(zkConn, fencePath, -1); err != nil {
			return errors.Trace(err)
		}
	}
	return nil
}
Esempio n. 4
0
func TestWaitForReceiver(t *testing.T) {
	fakeZkConn := zkhelper.NewConn()
	proxies := []ProxyInfo{}
	for i := 0; i < 5; i++ {
		proxies = append(proxies, ProxyInfo{
			Id:    fmt.Sprintf("proxy_%d", i),
			Addr:  fmt.Sprintf("localhost:%d", i+1234),
			State: PROXY_STATE_ONLINE,
		})
		CreateProxyInfo(fakeZkConn, productName, &proxies[i])
	}
	zkhelper.CreateRecursive(fakeZkConn, GetActionResponsePath(productName)+"/1", "", 0, zkhelper.DefaultDirACLs())
	go func() {
		time.Sleep(time.Second * 2)
		doResponseForTest(fakeZkConn, "1", &proxies[0])
		doResponseForTest(fakeZkConn, "1", &proxies[1])
		doResponseForTest(fakeZkConn, "1", &proxies[2])
		doResponseForTest(fakeZkConn, "1", &proxies[3])
		doResponseForTest(fakeZkConn, "1", &proxies[4])
		for {
			for i := 0; i < 5; i++ {
				pname := fmt.Sprintf("proxy_%d", i)
				p, _ := GetProxyInfo(fakeZkConn, productName, pname)
				if p != nil && p.State == PROXY_STATE_MARK_OFFLINE {
					zkhelper.DeleteRecursive(fakeZkConn, path.Join(GetProxyPath(productName), pname), -1)
				}
			}
		}
	}()
	err := WaitForReceiverWithTimeout(fakeZkConn, productName, GetActionResponsePath(productName)+"/1", proxies, 1000*10)
	if err != nil {
		t.Error("there is error not as expected")
	}
	p, _ := GetProxyInfo(fakeZkConn, productName, "proxy_0")
	if p == nil || p.State != PROXY_STATE_ONLINE {
		t.Error("proxy_0 status is not as expected")
	}
	p, _ = GetProxyInfo(fakeZkConn, productName, "proxy_1")
	if p == nil || p.State != PROXY_STATE_ONLINE {
		t.Error("proxy_1 status is not as expected")
	}
	p, _ = GetProxyInfo(fakeZkConn, productName, "proxy_2")
	if p == nil || p.State != PROXY_STATE_ONLINE {
		t.Error("proxy_2 status is not as expected")
	}
	p, _ = GetProxyInfo(fakeZkConn, productName, "proxy_3")
	if p == nil || p.State != PROXY_STATE_ONLINE {
		t.Error("proxy_3 status is not as expected")
	}
	p, _ = GetProxyInfo(fakeZkConn, productName, "proxy_4")
	if p == nil || p.State != PROXY_STATE_ONLINE {
		t.Error("proxy_4 status is not as expected")
	}
}
Esempio n. 5
0
func (self *ServerGroup) Remove(zkConn zkhelper.Conn) error {
	// check if this group is not used by any slot
	slots, err := Slots(zkConn, self.ProductName)
	if err != nil {
		return errors.Trace(err)
	}

	for _, slot := range slots {
		if slot.GroupId == self.Id {
			return errors.Errorf("group %d is using by slot %d", slot.GroupId, slot.Id)
		}
	}

	// do delete
	zkPath := fmt.Sprintf("/zk/codis/db_%s/servers/group_%d", self.ProductName, self.Id)
	err = zkhelper.DeleteRecursive(zkConn, zkPath, -1)

	// we know that there's no slots affected, so this action doesn't need proxy confirm
	err = NewAction(zkConn, self.ProductName, ACTION_TYPE_SERVER_GROUP_REMOVE, self, "", false)
	return errors.Trace(err)
}
Esempio n. 6
0
func ActionGC(zkConn zkhelper.Conn, productName string, gcType int, keep int) error {
	prefix := GetWatchActionPath(productName)
	respPrefix := GetActionResponsePath(productName)

	exists, err := zkhelper.NodeExists(zkConn, prefix)
	if err != nil {
		return errors.Trace(err)
	}
	if !exists {
		// if action path not exists just return nil
		return nil
	}

	actions, _, err := zkConn.Children(prefix)
	if err != nil {
		return errors.Trace(err)
	}

	var act Action
	currentTs := time.Now().Unix()

	if gcType == GC_TYPE_N {
		sort.Strings(actions)
		// keep last 500 actions
		if len(actions)-500 <= keep {
			return nil
		}
		for _, action := range actions[:len(actions)-keep-500] {
			if err := zkhelper.DeleteRecursive(zkConn, path.Join(prefix, action), -1); err != nil {
				return errors.Trace(err)
			}
			err := zkhelper.DeleteRecursive(zkConn, path.Join(respPrefix, action), -1)
			if err != nil && !zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
				return errors.Trace(err)
			}
		}
	} else if gcType == GC_TYPE_SEC {
		secs := keep
		for _, action := range actions {
			b, _, err := zkConn.Get(path.Join(prefix, action))
			if err != nil {
				return errors.Trace(err)
			}
			if err := json.Unmarshal(b, &act); err != nil {
				return errors.Trace(err)
			}
			log.Infof("action = %s, timestamp = %s", action, act.Ts)
			ts, _ := strconv.ParseInt(act.Ts, 10, 64)

			if currentTs-ts > int64(secs) {
				if err := zkhelper.DeleteRecursive(zkConn, path.Join(prefix, action), -1); err != nil {
					return errors.Trace(err)
				}
				err := zkhelper.DeleteRecursive(zkConn, path.Join(respPrefix, action), -1)
				if err != nil && !zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
					return errors.Trace(err)
				}
			}
		}

		actionResps, _, err := zkConn.Children(respPrefix)
		if err != nil {
			return errors.Trace(err)
		}

		for _, action := range actionResps {
			b, _, err := zkConn.Get(path.Join(respPrefix, action))
			if err != nil {
				return errors.Trace(err)
			}
			if err := json.Unmarshal(b, &act); err != nil {
				return errors.Trace(err)
			}
			log.Infof("action = %s, timestamp = %s", action, act.Ts)
			ts, _ := strconv.ParseInt(act.Ts, 10, 64)

			if currentTs-ts > int64(secs) {
				if err := zkhelper.DeleteRecursive(zkConn, path.Join(respPrefix, action), -1); err != nil {
					return errors.Trace(err)
				}
			}
		}
	}
	return nil
}
Esempio n. 7
0
func (top *Topology) DeleteDir(path string) {
	dir := top.FullPath(path)
	if ok, _ := top.Exist(dir); ok {
		zkhelper.DeleteRecursive(top.zkConn, dir, -1)
	}
}
Esempio n. 8
0
func (top *Topology) DeleteServiceEndPoint(service string, endpoint string) {
	path := top.ProductServiceEndPointPath(service, endpoint)
	zkhelper.DeleteRecursive(top.zkConn, path, -1)
}