Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:youtube/vitess
Browse files Browse the repository at this point in the history
  • Loading branch information
ryszard committed Jun 17, 2014
2 parents 0cbbee6 + 7a425d8 commit 430792b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
6 changes: 5 additions & 1 deletion go/vt/topo/srvshard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const SHARD_ZERO = "0"
// SrvShard contains a roll-up of the shard in the local namespace.
// In zk, it is under /zk/<cell>/vt/ns/<keyspace>/<shard>
type SrvShard struct {
// Copied from Shard
// Copied / infered from Shard
Name string
KeyRange key.KeyRange
ServedTypes []TabletType

Expand Down Expand Up @@ -57,6 +58,9 @@ func NewSrvShard(version int64) *SrvShard {

// ShardName returns the name of a shard.
func (ss *SrvShard) ShardName() string {
if ss.Name != "" {
return ss.Name
}
if !ss.KeyRange.IsPartial() {
return SHARD_ZERO
}
Expand Down
3 changes: 3 additions & 0 deletions go/vt/topo/srvshard_bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (srvShard *SrvShard) MarshalBson(buf *bytes2.ChunkedWriter, key string) {
bson.EncodeOptionalPrefix(buf, bson.Object, key)
lenWriter := bson.NewLenWriter(buf)

bson.EncodeString(buf, "Name", srvShard.Name)
srvShard.KeyRange.MarshalBson(buf, "KeyRange")
// []TabletType
{
Expand Down Expand Up @@ -56,6 +57,8 @@ func (srvShard *SrvShard) UnmarshalBson(buf *bytes.Buffer, kind byte) {

for kind := bson.NextByte(buf); kind != bson.EOO; kind = bson.NextByte(buf) {
switch bson.ReadCString(buf) {
case "Name":
srvShard.Name = bson.DecodeString(buf, kind)
case "KeyRange":
srvShard.KeyRange.UnmarshalBson(buf, kind)
case "ServedTypes":
Expand Down
2 changes: 2 additions & 0 deletions go/vt/topo/srvshard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestSrvKeySpace(t *testing.T) {
string(TYPE_MASTER): &KeyspacePartition{
Shards: []SrvShard{
SrvShard{
Name: "test_shard",
ServedTypes: []TabletType{TYPE_MASTER},
},
},
Expand All @@ -61,6 +62,7 @@ func TestSrvKeySpace(t *testing.T) {
TYPE_MASTER: &KeyspacePartition{
Shards: []SrvShard{
SrvShard{
Name: "test_shard",
ServedTypes: []TabletType{TYPE_MASTER},
TabletTypes: []TabletType{},
},
Expand Down
1 change: 1 addition & 0 deletions go/vt/topotools/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func rebuildCellSrvShard(ts topo.Server, shardInfo *topo.ShardInfo, cell string,
go func() {
log.Infof("updating shard serving graph in cell %v for %v/%v", cell, shardInfo.Keyspace(), shardInfo.ShardName())
srvShard := &topo.SrvShard{
Name: shardInfo.ShardName(),
KeyRange: shardInfo.KeyRange,
ServedTypes: shardInfo.ServedTypes,
TabletTypes: make([]topo.TabletType, 0, len(locationAddrsMap)),
Expand Down
28 changes: 20 additions & 8 deletions go/vt/vtgate/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import (
"github.com/youtube/vitess/go/vt/vtgate/proto"
)

var (
separator = []byte(", ")
sqlVarIdentifier = []byte(":")
openBracket = []byte(" in (")
closeBracket = []byte(")")
kwAnd = []byte(" and ")
kwWhere = []byte(" where ")
)

// Resolver is the layer to resolve KeyspaceIds and KeyRanges
// to shards. It will try to re-resolve shards if ScatterConn
// returns retryable error, which may imply horizontal or vertical
Expand Down Expand Up @@ -334,13 +343,14 @@ func StrsEquals(a, b []string) bool {
}

func buildEntityIds(shardIDMap map[string][]interface{}, qSql, entityColName string, qBindVars map[string]interface{}) ([]string, map[string]string, map[string]map[string]interface{}) {
shards := make([]string, 0, 1)
shards := make([]string, len(shardIDMap))
shardsIdx := 0
sqls := make(map[string]string)
bindVars := make(map[string]map[string]interface{})
for shard, ids := range shardIDMap {
var b bytes.Buffer
b.Write([]byte(entityColName))
b.Write([]byte(" in ("))
b.Write(openBracket)
bindVar := make(map[string]interface{})
for k, v := range qBindVars {
bindVar[k] = v
Expand All @@ -349,14 +359,16 @@ func buildEntityIds(shardIDMap map[string][]interface{}, qSql, entityColName str
bvName := fmt.Sprintf("%v%v", entityColName, i)
bindVar[bvName] = id
if i > 0 {
b.Write([]byte(", "))
b.Write(separator)
}
b.Write([]byte(fmt.Sprintf(":%v", bvName)))
b.Write(sqlVarIdentifier)
b.Write([]byte(bvName))
}
b.Write([]byte(")"))
shards = append(shards, shard)
b.Write(closeBracket)
sqls[shard] = insertSqlClause(qSql, b.String())
bindVars[shard] = bindVar
shards[shardsIdx] = shard
shardsIdx++
}
return shards, sqls, bindVars
}
Expand All @@ -381,9 +393,9 @@ func insertSqlClause(querySql, clause string) string {
var b bytes.Buffer
b.Write([]byte(querySql[:idxExtra]))
if strings.Contains(sql, "where") {
b.Write([]byte(" and "))
b.Write(kwAnd)
} else {
b.Write([]byte(" where "))
b.Write(kwWhere)
}
b.Write([]byte(clause))
if idxExtra < len(sql) {
Expand Down
3 changes: 0 additions & 3 deletions go/vt/vtgate/scatter_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func (stc *ScatterConn) ExecuteEntityIds(
tabletType topo.TabletType,
session *SafeSession,
) (*mproto.QueryResult, error) {
var lock sync.Mutex
results, allErrors := stc.multiGo(
context,
"ExecuteEntityIds",
Expand All @@ -113,10 +112,8 @@ func (stc *ScatterConn) ExecuteEntityIds(
session,
func(sdc *ShardConn, transactionId int64, sResults chan<- interface{}) error {
shard := sdc.shard
lock.Lock()
sql := sqls[shard]
bindVar := bindVars[shard]
lock.Unlock()
innerqr, err := sdc.Execute(context, sql, bindVar, transactionId)
if err != nil {
return err
Expand Down

0 comments on commit 430792b

Please sign in to comment.