示例#1
0
func TestRecoverManager(t *testing.T) {

	pipeline := NewDefaultPipeline()

	kitedb := memory.NewKiteMemoryStore(100, 100)

	messageid := store.MessageId()
	t.Logf("messageid:%s\b", messageid)
	entity := store.NewMessageEntity(protocol.NewQMessage(buildStringMessage(messageid)))
	kitedb.Save(entity)
	go func() {
		for {
			log.Println(kitedb.Monitor())
			time.Sleep(1 * time.Second)
		}
	}()

	fs := stat.NewFlowStat("recover")
	fs.Start()
	ch := make(chan bool, 1)

	// 临时在这里创建的BindExchanger
	exchanger := binding.NewBindExchanger("localhost:2181", "127.0.0.1:13800")
	pipeline.RegisteHandler("deliverpre", handler.NewDeliverPreHandler("deliverpre", kitedb, exchanger, fs, 100))
	pipeline.RegisteHandler("deliver", newmockDeliverHandler("deliver", ch))
	hostname, _ := os.Hostname()
	rm := NewRecoverManager(hostname, 16*time.Second, pipeline, kitedb)
	rm.Start()
	select {
	case succ := <-ch:
		log.Printf("--------------recover %s\n", succ)
	case <-time.After(5 * time.Second):
		t.Fail()
		log.Println("waite recover  deliver timeout\n")
	}

}
示例#2
0
func parseDB(kc KiteQConfig) store.IKiteStore {
	db := kc.db

	var kitedb store.IKiteStore
	if strings.HasPrefix(db, "mock://") {
		kitedb = &store.MockKiteStore{}
	} else if strings.HasPrefix(db, "memory://") {
		url := strings.TrimPrefix(db, "memory://")
		split := strings.Split(url, "&")
		params := make(map[string]string, len(split))
		for _, v := range split {
			p := strings.SplitN(v, "=", 2)
			if len(p) >= 2 {
				params[p[0]] = p[1]
			}
		}

		initval := 10 * 10000
		initcap, ok := params["initcap"]
		if ok {
			v, e := strconv.ParseInt(initcap, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|INIT CAP|%s\n", db)
			}
			initval = int(v)
		}
		max := 50 * 10000
		maxcap, ok := params["maxcap"]
		if ok {
			v, e := strconv.ParseInt(maxcap, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|MAX CAP|%s\n", db)
			}
			max = int(v)
		}
		kitedb = sm.NewKiteMemoryStore(initval, max)
	} else if strings.HasPrefix(db, "mysql://") {
		url := strings.TrimPrefix(db, "mysql://")
		mp := strings.Split(url, "?")
		params := make(map[string]string, 5)
		if len(mp) > 1 {
			split := strings.Split(mp[1], "&")
			for _, v := range split {
				p := strings.SplitN(v, "=", 2)
				params[p[0]] = p[1]
			}
		}

		bus := 100
		u, ok := params["batchUpdateSize"]
		if ok {
			v, e := strconv.ParseInt(u, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|batchUpdateSize|%s\n", db)
			}
			bus = int(v)
		}

		bds := 100
		d, ok := params["batchDelSize"]
		if ok {
			v, e := strconv.ParseInt(d, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db)
			}
			bds = int(v)
		}

		flushPeriod := 1 * time.Second
		fp, ok := params["flushPeriod"]
		if ok {
			v, e := strconv.ParseInt(fp, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db)
			}
			flushPeriod = time.Duration(v * int64(flushPeriod))
		}

		maxConn := 500
		mc, ok := params["maxConn"]
		if ok {
			v, e := strconv.ParseInt(mc, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|batchDelSize|%s\n", db)
			}
			maxConn = int(v)
		}

		//解析Mysql的host
		master := mp[0]
		slave := ""
		mysqlHosts := strings.Split(mp[0], ",")
		if len(mysqlHosts) > 1 {
			master = mysqlHosts[0]
			slave = mysqlHosts[1]
		}

		//解析用户名密码:
		username, _ := params["username"]
		password, pok := params["password"]
		if !pok {
			password = ""
		}

		//shard的数量
		shardnum := 8
		sd, sdok := params["shardnum"]
		if sdok {
			v, e := strconv.ParseInt(sd, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|ShardNum|%s\n", db)
			}
			shardnum = int(v)
		}

		options := smq.MysqlOptions{
			Addr:         master,
			SlaveAddr:    slave,
			ShardNum:     shardnum,
			DB:           params["db"],
			Username:     username,
			Password:     password,
			BatchUpSize:  bus,
			BatchDelSize: bds,
			FlushPeriod:  flushPeriod,
			MaxIdleConn:  maxConn / 2,
			MaxOpenConn:  maxConn}
		kitedb = smq.NewKiteMysql(options)
	} else if strings.HasPrefix(db, "file://") {
		url := strings.TrimPrefix(db, "file://")
		mp := strings.Split(url, "?")
		params := make(map[string]string, 5)
		if len(mp) > 1 {
			split := strings.Split(mp[1], "&")
			for _, v := range split {
				p := strings.SplitN(v, "=", 2)
				params[p[0]] = p[1]
			}
		}
		if len(mp[0]) <= 0 {
			log.Crashf("NewKiteQServer|INVALID|FilePath|%s\n", db)
		}

		//最大消息容量
		maxcap := 100
		d, ok := params["cap"]
		if ok {
			v, e := strconv.ParseInt(d, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|cap|%s\n", db)
			}
			maxcap = int(v)
		}

		//检查文件过期时间
		checkPeriod := 1 * time.Second
		fp, ok := params["checkPeriod"]
		if ok {
			v, e := strconv.ParseInt(fp, 10, 32)
			if nil != e {
				log.Crashf("NewKiteQServer|INVALID|checkPeriod|%s\n", db)
			}
			checkPeriod = time.Duration(v * int64(checkPeriod))
		}

		kitedb = smf.NewKiteFileStore(mp[0], maxcap, checkPeriod)
		log.Debug("NewKiteQServer|FILESTORE|%s|%d|%d", mp[0], maxcap, int(checkPeriod.Seconds()))
	} else {
		log.Crashf("NewKiteQServer|UNSUPPORT DB PROTOCOL|%s\n", db)
	}
	kc.flowstat.Kitestore = kitedb
	return kitedb
}