Beispiel #1
0
// build a new shard DB after more the limit
// or no any one shard DB.
func buildNewShardDB(grp *host.Group) (*MysqlShardDB, error) {
	mut.Lock()
	defer mut.Unlock()

	shardDBName := "shard" + strconv.Itoa(ShardDBCnt+1)

	// to check this new shard db has been exists.
	isExists := IsExistsShardDB(shardDBName)
	if isExists != nil {
		return isExists, nil
	}

	newShardDBId := redis.BuildPrimaryKey(shardDBName, true)

	shardDB := &MysqlShardDB{
		Id:          newShardDBId,
		Name:        shardDBName,
		TableTotal:  uint64(0),
		SizeTotal:   uint64(0),
		HostGroupId: grp.Id,
		Created:     time.Now().Unix(),
		HostGroup:   grp,
	}

	// create the database to host.
	//
	master := host.GetBetterHost(grp.Master, "master")
	db, err := (&master).ConnToDB("mysql")
	if err != nil {
		return nil, err
	}

	stmt, err := db.Prepare(fmt.Sprintf("CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", shardDBName))
	if err != nil {
		return nil, err
	}

	_, err = stmt.Exec()
	if err != nil {
		return nil, err
	}
	stmt.Close()
	db.Close()

	// to write the new shard db to redis
	//
	err = redis.WriteDB(newShardDBId, redis.EncodeData(shardDB), "MysqlShardDB")
	if err != nil {
		return nil, err
	}

	// notice Mysql Project object to add a new shard db memery.
	NewShardDBCh <- shardDB

	return shardDB, nil
}
Beispiel #2
0
func (m *MysqlProxy) BuildNewShardDB(group *host.Group, name string) (*schema.MysqlShardDB, error) {
	if name == "" {
		return nil, errors.New("Sorry, can not build the no name databases")
	}

	// init the shard db to host.
	master := group.Master[0]
	db, err := (&master).ConnToDB("mysql")
	if err != nil {
		return nil, err
	}

	stmt, err := db.Prepare(fmt.Sprintf("CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", name))
	if err != nil {
		return nil, err
	}

	_, err = stmt.Exec()
	if err != nil {
		return nil, err
	}
	stmt.Close()

	shardDbId := redis.BuildPrimaryKey(name, true)
	shardDb := &schema.MysqlShardDB{
		Id:          shardDbId,
		Name:        name,
		TableTotal:  0,
		SizeTotal:   0,
		HostGroupId: group.Id,
		Created:     redis.GetCurTime(),
		HostGroup:   group,
	}

	// save this new shard database to tracker.
	err = redis.WriteDB(shardDbId, redis.EncodeData(shardDb), "MysqlShardDB")
	if err != nil {
		return nil, err
	}

	(&master).CloseDB()

	schema.ShardDBCnt++

	return shardDb, nil
}
Beispiel #3
0
func (sdb *MysqlShardDB) InitShardTable(tName string) (*MysqlShardTable, error) {
	shardTabId := redis.BuildPrimaryKey(tName, true)
	newSTable := &MysqlShardTable{
		Id:        shardTabId,
		Name:      tName + "_shard1",
		RowTotal:  0,
		ShardDBId: sdb.Id,
		Created:   time.Now().Unix(),
		ShardDB:   sdb,
	}

	err := redis.WriteDB(shardTabId, redis.EncodeData(newSTable), "MysqlShardTable")
	if err != nil {
		return nil, err
	}

	return newSTable, nil
}
Beispiel #4
0
func (sdb *MysqlShardDB) InitTable(tName string, sql string) (*MysqlTable, error) {
	// 为了避免高并发时,整体tabletotal延迟执行导致的total数据不准确,
	// 所有在后面如果出现错误时, 需要将此添加回退.
	sdb.TableTotal += 1

	tableId := redis.BuildPrimaryKey(tName, true)
	newTable := &MysqlTable{
		Id:       tableId,
		Name:     tName,
		CurGId:   1,
		RowTotal: 0,
		Created:  time.Now().Unix(),
		// Columns: make([]TableColumn, 0, 16),
		Indexes: make([]*Index, 0, 8),
	}

	newShardTable, err := sdb.InitShardTable(tName)
	if err != nil {
		sdb.TableTotal -= 1
		return nil, err
	}

	newTable.ShardIds = []string{newShardTable.Id}
	newTable.Shards = []*MysqlShardTable{newShardTable}

	// add column data to memery.
	newTable.RestoreColumnsByDB()

	err = redis.WriteDB(tableId, redis.EncodeData(newTable), "MysqlTable")
	if err != nil {
		sdb.TableTotal -= 1
		return nil, err
	}

	sdb.UpdateToRedisDB()

	return newTable, nil
}
Beispiel #5
0
// 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
}