Example #1
0
// get the shard db by ids.
func (m *MysqlProxy) GetShardDbById(sid string) (*schema.MysqlShardDB, error) {
	if sid == "" {
		return nil, errors.New("Sorry, the shard db id connot is empty")
	}

	sdb, err := redis.ReadDB("MysqlShardDB", sid)
	if err != nil {
		return nil, err
	}

	if len(sdb) != 1 {
		return nil, errors.New("Load shard db wrong!")
	}
	tsdb := sdb[0][sid].(map[string]interface{})
	groupId := tsdb["HostGroupId"].(string)
	curGroup, err := host.GetHostGroupById(groupId)
	if err != nil {
		return nil, err
	}

	shardDB := &schema.MysqlShardDB{
		Id:          tsdb["Id"].(string),
		Name:        tsdb["Name"].(string),
		TableTotal:  uint64(tsdb["TableTotal"].(float64)),
		SizeTotal:   uint64(tsdb["SizeTotal"].(float64)),
		HostGroupId: groupId,
		Created:     int64(tsdb["Created"].(float64)),
		HostGroup:   curGroup,
	}

	schema.ShardDBCnt++

	return shardDB, nil
}
Example #2
0
// get the current db cluster data infomations
func (m *MysqlProxy) InitMysqlDB() {
	// panic(fmt.Sprintf("%#v, %#v", m.ShardDBIds, len(m.ShardDBIds)))
	if len(m.ShardDBIds) == 0 {
		// init the shard DB
		shardDBs := []*schema.MysqlShardDB{}
		shardDBIds := []string{}
		m.ShardDBCnt = 0

		for _, group := range host.Groups {
			m.ShardDBCnt++
			shardDb, err := m.BuildNewShardDB(&group, "shard"+strconv.Itoa(m.ShardDBCnt))

			CheckError(err)

			shardDBs = append(shardDBs, shardDb)
			shardDBIds = append(shardDBIds, shardDb.Id)
		}

		m.ShardDBs = shardDBs
		m.ShardDBIds = shardDBIds

		// to prepare save new data.
		isUpdated = true

		// add shard dbs map.
		schema.Sdbs = shardDBs

	} else {
		// 分析数据,并恢复至MysqlProxy结构体中.
		shardDBs := []*schema.MysqlShardDB{}
		for _, sid := range m.ShardDBIds {
			dbs, err := redis.ReadDB("MysqlShardDB", sid)
			CheckError(err)
			if len(dbs) != 1 {
				panic("no found relation shard db for id:" + sid)
			}

			sdb := dbs[0][sid].(map[string]interface{})
			groupId := sdb["HostGroupId"].(string)
			curGroup, err := host.GetHostGroupById(groupId)
			CheckError(err)

			shardDB := &schema.MysqlShardDB{
				Id:          sdb["Id"].(string),
				Name:        sdb["Name"].(string),
				TableTotal:  uint64(sdb["TableTotal"].(float64)),
				SizeTotal:   uint64(sdb["SizeTotal"].(float64)),
				HostGroupId: groupId,
				Created:     int64(sdb["Created"].(float64)),
				HostGroup:   curGroup,
			}

			shardDBs = append(shardDBs, shardDB)
		}

		m.ShardDBs = shardDBs

		// add shard dbs map.
		schema.Sdbs = shardDBs
	}

	// listen the sharddb change status.
	locker := &sync.Mutex{}

	go func() {
		for {
			newShardDB := <-schema.NewShardDBCh

			locker.Lock()
			defer locker.Unlock()
			m.ShardDBIds = append(m.ShardDBIds, newShardDB.Id)
			m.ShardDBs = append(m.ShardDBs, newShardDB)
			schema.Sdbs = m.ShardDBs

			err := redis.UpdateDB("main", redis.EncodeData(m), "MysqlProxy")
			if err != nil {
				log.Printf("new shard db listener error:%s", err)
			}

			m.ShardDBCnt++
			schema.ShardDBCnt = m.ShardDBCnt
			fmt.Printf("current shard total: %d\n", schema.ShardDBCnt)

		}
	}()

	// listen the table drop action.
	go func() {
		for {
			dropedTable := <-schema.DropedTableCh
			m.DeleteTable(dropedTable)
		}
	}()

	// panic(fmt.Sprintf("in init shard db: %#v, %#v", m))
}