func (m *MysqlProxy) DeleteTable(table *schema.MysqlTable) { curTables := []*schema.MysqlTable{} curTableIds := []string{} for _, one := range m.Tables { if one.Name != table.Name { curTables = append(curTables, one) } } for _, one := range m.TableIds { if one != table.Id { curTableIds = append(curTableIds, one) } } // delete the relations. m.TableIds = curTableIds m.Tables = curTables err := redis.UpdateDB("main", redis.EncodeData(m), "MysqlProxy") if err != nil { fmt.Printf("Delete table error when write redis: %s\n", err) return } schema.Tables = curTables // delete selfs. table.Destroy() }
func (tbl *MysqlTable) GetGId() string { curId := tbl.CurGId tbl.CurGId++ err := redis.UpdateDB(tbl.Id, redis.EncodeData(tbl), "MysqlTable") if err != nil { log.Printf("get gid error: %s", err) } return strconv.FormatUint(curId, 10) }
// To init the necessary data. func (m *MysqlProxy) Init() { m.InitMain() m.InitMysqlDB() m.InitMysqlTable() m.InitConnPooling() if isUpdated { // save mysql proxy. err := redis.UpdateDB("main", redis.EncodeData(m), "MysqlProxy") CheckError(err) } // panic(fmt.Sprintf("OK: %#v", m)) }
func (m *MysqlProxy) UpdateToRedisDB() error { return redis.UpdateDB("main", redis.EncodeData(m), "MysqlProxy") }
// 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)) }
func (stb *MysqlShardTable) UpdateToRedisDB() error { return redis.UpdateDB(stb.Id, redis.EncodeData(stb), "MysqlShardTable") }
func (sdb *MysqlShardDB) UpdateToRedisDB() error { return redis.UpdateDB(sdb.Id, redis.EncodeData(sdb), "MysqlShardDB") }
func (tbl *MysqlTable) UpdateToRedisDB() error { return redis.UpdateDB(tbl.Id, redis.EncodeData(tbl), "MysqlTable") }
// restore the schema ddl. func (tbl *MysqlTable) BuildNewShardTable() (*MysqlShardTable, error) { curSharded := len(tbl.Shards) tTableName := tbl.Name + "_shard" + strconv.Itoa(curSharded+1) newTabId := redis.BuildPrimaryKey(tTableName, true) shardTable := &MysqlShardTable{ Id: newTabId, Name: tTableName, RowTotal: uint64(0), ShardDBId: "", Created: time.Now().Unix(), ShardDB: nil, } // get bettet group. group := host.GetBetterMasterGroup() shardDB, err := tbl.GetMasterShardDBByGroup(group) if err != nil { return nil, err } shardTable.ShardDBId = shardDB.Id shardTable.ShardDB = shardDB betterHost := host.GetBetterHost(group.Master, "master") ddlSql, err := tbl.GetSchemaDDLByDb() if err != nil { return nil, err } ddlSql = strings.Replace(ddlSql, tbl.Shards[0].Name, tTableName, -1) db, err := betterHost.ConnToDB(shardDB.Name) if err != nil { return nil, err } stmt, err := db.Prepare(ddlSql) if err != nil { return nil, err } _, err = stmt.Exec() if err != nil { return nil, err } stmt.Close() // write the new shard table to redis err = redis.WriteDB(newTabId, redis.EncodeData(shardTable), "MysqlShardTable") if err != nil { return nil, err } tbl.Shards = append(tbl.Shards, shardTable) tbl.ShardIds = append(tbl.ShardIds, newTabId) err = redis.UpdateDB(tbl.Id, redis.EncodeData(tbl), "MysqlTable") if err != nil { return nil, err } return shardTable, nil }