add more log and test about KeySlot

v4
suxb201 2 years ago
parent e844a39e0f
commit 4df84993f7
  1. 10
      internal/commands/keys.go
  2. 46
      internal/commands/keys_test.go
  3. 4
      internal/utils/crc16.go
  4. 6
      internal/utils/crc_test.go
  5. 2
      internal/writer/redis.go

@ -91,6 +91,12 @@ func CalcKeys(argv []string) (cmaName string, group string, keys []string) {
func CalcSlots(keys []string) []int {
slots := make([]int, len(keys))
for inx, key := range keys {
slots[inx] = int(keyHash(key))
}
return slots
}
func keyHash(key string) uint16 {
hashtag := ""
findHashTag:
for i, s := range key {
@ -106,7 +112,5 @@ func CalcSlots(keys []string) []int {
if len(hashtag) > 0 {
key = hashtag
}
slots[inx] = int(utils.KeyHash(key) & 0x3fff)
}
return slots
return utils.Crc16(key) & 0x3FFF
}

@ -40,5 +40,51 @@ func TestCalcKeys(t *testing.T) {
if cmd != "ZUNIONSTORE" || group != "SORTED_SET" || !testEq(keys, []string{"key", "key1", "key2"}) {
t.Errorf("CalcKeys(ZUNIONSTORE key 2 key1 key2) failed. cmd=%s, group=%s, keys=%v", cmd, group, keys)
}
}
func TestKeyHash(t *testing.T) {
ret := keyHash("abcde")
if ret != 16097 {
t.Errorf("keyHash(abcde) = %x", ret)
}
ret = keyHash("abcde{")
if ret != 14689 {
t.Errorf("keyHash(abcde{) = %x", ret)
}
ret = keyHash("abcde}")
if ret != 6567 {
t.Errorf("keyHash(abcde}) = %x", ret)
}
ret = keyHash("{abcde}")
if ret != 16097 {
t.Errorf("keyHash({abcde}) = %x", ret)
}
ret = keyHash("same")
if ret != 13447 {
t.Errorf("keyHash(same) = %x", ret)
}
ret = keyHash("abcdefghi{same}abcdefghi")
if ret != 13447 {
t.Errorf("keyHash(abcdefghi{same}abcdefghi) = %x", ret)
}
ret = keyHash("123456789{same}123456789")
if ret != 13447 {
t.Errorf("keyHash(123456789{same}123456789) = %x", ret)
}
ret = keyHash("1234我是你89{same}12我就456789")
if ret != 13447 {
t.Errorf("keyHash(1234我是你89{same}12我就456789) = %x", ret)
}
ret = keyHash("你你你{你你你}你你你")
if ret != 15023 {
t.Errorf("keyHash(1234我是你89{same}12我就456789) = %x", ret)
}
b := make([]byte, 0)
for i := 0; i < 256; i++ {
b = append(b, byte(i))
}
ret = keyHash(string(b))
if ret != 16155 {
t.Errorf("keyHash(%s) = %x", string(b), ret)
}
}

@ -87,7 +87,3 @@ func Crc16(buf string) uint16 {
}
return crc
}
func KeyHash(buf string) uint16 {
return Crc16(buf) & 0x3FFF
}

@ -3,8 +3,8 @@ package utils
import "testing"
func TestCrc16(t *testing.T) {
ret := KeyHash("你")
if ret != 8522 {
t.Errorf("KeyHash failed, expect: %d, actual: %d", 8522, ret)
ret := Crc16("123456789")
if ret != 0x31c3 {
t.Errorf("Crc16(123456789) = %x", ret)
}
}

@ -68,7 +68,7 @@ func (w *redisWriter) flushInterval() {
log.Panicf("redisWriter received BUSYKEY reply. argv=%v", e.Argv)
}
} else {
log.Panicf("redisWriter received error. error=[%v], argv=%v, reply=[%s]", err, e.Argv, reply)
log.Panicf("redisWriter received error. error=[%v], argv=%v, slots=%v, reply=[%v]", err, e.Argv, e.Slots, reply)
}
}
atomic.AddUint64(&w.UpdateUnansweredBytesCount, ^(e.EncodedSize - 1))

Loading…
Cancel
Save