Skip to content

Commit

Permalink
restrict shardkey autodisc cal event to str shard keys (#384)
Browse files Browse the repository at this point in the history
* add log for shardkey autodisc and restrict cal event

* shard_basic test - check shardkey autodisc in log
  • Loading branch information
rsh-yam authored Jan 8, 2024
1 parent 84dc20c commit 192e0e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
31 changes: 23 additions & 8 deletions lib/coordinatorsharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,30 @@ func (crd *Coordinator) PreprocessSharding(requests []*netstring.Netstring) (boo

if len(crd.shard.shardValues) > 0 {
// shard_key_auto_discovery
evt := cal.NewCalEvent(EvtTypeSharding, EvtNameShardKeyAutodisc, cal.TransOK, "")
evt.AddDataStr("shardkey", GetConfig().ShardKeyName+"|"+crd.shard.shardValues[0])
evt.AddDataInt("shardid", int64(crd.shard.shardID))
if len(crd.shard.shardRecs) > 0 {
evt.AddDataInt("scuttleid", int64(crd.shard.shardRecs[0].bin))
evt.AddDataInt("flags", int64(crd.shard.shardRecs[0].flags))
shardkey := GetConfig().ShardKeyName + "|" + crd.shard.shardValues[0]
shardid := int64(crd.shard.shardID)
shardRecs := crd.shard.shardRecs
sqlhash := int64(uint32(crd.sqlhash))
if logger.GetLogger().V(logger.Verbose) {
logmsg := fmt.Sprintf("shard key auto discovery: shardkey=%s&shardid=%d", shardkey, shardid)
if len(shardRecs) > 0 {
logmsg += fmt.Sprintf("&scuttleid=%d&flags=%d", int64(shardRecs[0].bin), int64(shardRecs[0].flags))
}
logmsg += fmt.Sprintf("&sqlhash=%d", sqlhash)
logger.GetLogger().Log(logger.Verbose, logmsg)
}
// restricting cal event to only String type Shard Keys
if GetConfig().ShardKeyValueTypeIsString {
evt := cal.NewCalEvent(EvtTypeSharding, EvtNameShardKeyAutodisc, cal.TransOK, "")
evt.AddDataStr("shardkey", shardkey)
evt.AddDataInt("shardid", shardid)
if len(shardRecs) > 0 {
evt.AddDataInt("scuttleid", int64(shardRecs[0].bin))
evt.AddDataInt("flags", int64(shardRecs[0].flags))
}
evt.AddDataInt("sqlhash", sqlhash)
evt.Completed()
}
evt.AddDataInt("sqlhash", int64(uint32(crd.sqlhash)))
evt.Completed()
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/functionaltest/sharding_tests/shard_basic/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ func TestShardBasic(t *testing.T) {
t.Fatalf ("Error: No Shard key event for fetch request in CAL");
}

fmt.Println ("Check CAL log for correct events");
cal_count = testutil.RegexCountFile ("SHARDING.*shard_key_auto_discovery.*0.*shardkey=accountid|12346&shardid=3&scuttleid=", "cal.log")
if (cal_count < 1) {
t.Fatalf ("Error: No shard_key_auto_discovery event seen in CAL");
fmt.Println ("Check log for shard key auto discovery");
count = testutil.RegexCount ("shard key auto discovery: shardkey=accountid|12346&shardid=3&scuttleid=")
if (count < 1) {
t.Fatalf ("Error: Did NOT get shard key auto discovery in log");
}

fmt.Println ("Check CAL log for correct events")
cal_count = testutil.RegexCountFile ("T.*API.*CLIENT_SESSION_3", "cal.log")
if (cal_count < 1) {
t.Fatalf ("Error: Request is not executed by shard 3 as expected");
Expand Down

0 comments on commit 192e0e8

Please sign in to comment.