Skip to content

Commit

Permalink
New for loops and some assert/require (#15194)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <andrew@planetscale.com>
  • Loading branch information
Andrew Mason authored Feb 15, 2024
1 parent c0b303d commit 3a5907f
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 197 deletions.
2 changes: 1 addition & 1 deletion examples/compose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {

// Insert some messages on random pages.
fmt.Println("Inserting into primary...")
for i := 0; i < 3; i++ {
for range 3 {
tx, err := db.Begin()
if err != nil {
fmt.Printf("begin failed: %v\n", err)
Expand Down
46 changes: 3 additions & 43 deletions examples/compose/vtcompose/vtcompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,6 @@ func main() {
writeFile(dockerComposeFile, "docker-compose.yml")
}

func applyFilePatch(dockerYaml []byte, patchFile string) []byte {
yamlPatch, err := os.ReadFile(patchFile)
if err != nil {
log.Fatalf("reading yaml patch file %s: %s", patchFile, err)
}

patch, err := yamlpatch.DecodePatch(yamlPatch)
if err != nil {
log.Fatalf("decoding patch failed: %s", err)
}

bs, err := patch.Apply(dockerYaml)
if err != nil {
log.Fatalf("applying patch failed: %s", err)
}
return bs
}

func applyJsonInMemoryPatch(vSchemaFile []byte, patchString string) []byte {
patch, err := jsonpatch.DecodePatch([]byte(patchString))
if err != nil {
Expand Down Expand Up @@ -446,7 +428,7 @@ func applyKeyspaceDependentPatches(
dockerComposeFile = applyShardPatches(dockerComposeFile, tabAlias, shard, keyspaceData, externalDbInfoMap, opts)
} else {
// Determine shard range
for i := 0; i < keyspaceData.shards; i++ {
for i := range keyspaceData.shards {
if i == 0 {
shard = fmt.Sprintf("-%x", interval)
} else if i == (keyspaceData.shards - 1) {
Expand Down Expand Up @@ -517,28 +499,6 @@ func applyShardPatches(
return dockerComposeFile
}

func generateDefaultShard(tabAlias int, shard string, keyspaceData keyspaceInfo, opts vtOptions) string {
aliases := []int{tabAlias + 1} // primary alias, e.g. 201
for i := 0; i < keyspaceData.replicaTablets; i++ {
aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ...
}
tabletDepends := make([]string, len(aliases))
for i, tabletId := range aliases {
tabletDepends[i] = fmt.Sprintf("vttablet%d: {condition : service_healthy}", tabletId)
}
// Wait on all shard tablets to be healthy
dependsOn := "depends_on: {" + strings.Join(tabletDepends, ", ") + "}"

return fmt.Sprintf(`
- op: add
path: /services/init_shard_primary%[2]d
value:
image: vitess/lite:${VITESS_TAG:-latest}
command: ["sh", "-c", "/vt/bin/vtctldclient %[5]s InitShardPrimary --force %[4]s/%[3]s %[6]s-%[2]d "]
%[1]s
`, dependsOn, aliases[0], shard, keyspaceData.keyspace, opts.topologyFlags, opts.cell)
}

func generateExternalPrimary(
tabAlias int,
shard string,
Expand All @@ -548,7 +508,7 @@ func generateExternalPrimary(
) string {

aliases := []int{tabAlias + 1} // primary alias, e.g. 201
for i := 0; i < keyspaceData.replicaTablets; i++ {
for i := range keyspaceData.replicaTablets {
aliases = append(aliases, tabAlias+2+i) // replica aliases, e.g. 202, 203, ...
}

Expand Down Expand Up @@ -611,7 +571,7 @@ func applyTabletPatches(
dbInfo = val
}
dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+1, shard, "primary", keyspaceData.keyspace, dbInfo, opts))
for i := 0; i < keyspaceData.replicaTablets; i++ {
for i := range keyspaceData.replicaTablets {
dockerComposeFile = applyInMemoryPatch(dockerComposeFile, generateDefaultTablet(tabAlias+2+i, shard, "replica", keyspaceData.keyspace, dbInfo, opts))
}
return dockerComposeFile
Expand Down
141 changes: 37 additions & 104 deletions go/bucketpool/bucketpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,166 +19,105 @@ package bucketpool
import (
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPool(t *testing.T) {
maxSize := 16384
pool := New(1024, maxSize)
if pool.maxSize != maxSize {
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
}
if len(pool.pools) != 5 {
t.Fatalf("Invalid number of pools: %d, expected %d", len(pool.pools), 5)
}
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
require.Len(t, pool.pools, 5, "Invalid number of pools")

buf := pool.Get(64)
if len(*buf) != 64 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1024 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 64, "unexpected buf length")
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")

// get from same pool, check that length is right
buf = pool.Get(128)
if len(*buf) != 128 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1024 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 128, "unexpected buf length")
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
pool.Put(buf)

// get boundary size
buf = pool.Get(1024)
if len(*buf) != 1024 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1024 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 1024, "unexpected buf length")
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
pool.Put(buf)

// get from the middle
buf = pool.Get(5000)
if len(*buf) != 5000 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 8192 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 5000, "unexpected buf length")
require.Equal(t, 8192, cap(*buf), "unexpected buf cap")
pool.Put(buf)

// check last pool
buf = pool.Get(16383)
if len(*buf) != 16383 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 16384 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 16383, "unexpected buf length")
require.Equal(t, 16384, cap(*buf), "unexpected buf cap")
pool.Put(buf)

// get big buffer
buf = pool.Get(16385)
if len(*buf) != 16385 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 16385 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 16385, "unexpected buf length")
require.Equal(t, 16385, cap(*buf), "unexpected buf cap")
pool.Put(buf)
}

func TestPoolOneSize(t *testing.T) {
maxSize := 1024
pool := New(1024, maxSize)
if pool.maxSize != maxSize {
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
}
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
buf := pool.Get(64)
if len(*buf) != 64 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1024 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 64, "unexpected buf length")
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
pool.Put(buf)

buf = pool.Get(1025)
if len(*buf) != 1025 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1025 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 1025, "unexpected buf length")
require.Equal(t, 1025, cap(*buf), "unexpected buf cap")
pool.Put(buf)
}

func TestPoolTwoSizeNotMultiplier(t *testing.T) {
maxSize := 2000
pool := New(1024, maxSize)
if pool.maxSize != maxSize {
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
}
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")
buf := pool.Get(64)
if len(*buf) != 64 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 1024 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 64, "unexpected buf length")
require.Equal(t, 1024, cap(*buf), "unexpected buf cap")
pool.Put(buf)

buf = pool.Get(2001)
if len(*buf) != 2001 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 2001 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 2001, "unexpected buf length")
require.Equal(t, 2001, cap(*buf), "unexpected buf cap")
pool.Put(buf)
}

func TestPoolMaxSizeLessThanMinSize(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected the code to panic")
}
}()

New(15000, 1024)
assert.Panics(t, func() { New(15000, 1024) })
}

func TestPoolWeirdMaxSize(t *testing.T) {
maxSize := 15000
pool := New(1024, maxSize)
if pool.maxSize != maxSize {
t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
}
require.Equal(t, maxSize, pool.maxSize, "Invalid max pool size")

buf := pool.Get(14000)
if len(*buf) != 14000 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 15000 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 14000, "unexpected buf length")
require.Equal(t, 15000, cap(*buf), "unexpected buf cap")
pool.Put(buf)

buf = pool.Get(16383)
if len(*buf) != 16383 {
t.Fatalf("unexpected buf length: %d", len(*buf))
}
if cap(*buf) != 16383 {
t.Fatalf("unexpected buf cap: %d", cap(*buf))
}
require.Len(t, *buf, 16383, "unexpected buf length")
require.Equal(t, 16383, cap(*buf), "unexpected buf cap")
pool.Put(buf)
}

func TestFuzz(t *testing.T) {
maxTestSize := 16384
for i := 0; i < 20000; i++ {
for range 20000 {
minSize := rand.Intn(maxTestSize)
if minSize == 0 {
minSize = 1
Expand All @@ -187,18 +126,12 @@ func TestFuzz(t *testing.T) {
p := New(minSize, maxSize)
bufSize := rand.Intn(maxTestSize)
buf := p.Get(bufSize)
if len(*buf) != bufSize {
t.Fatalf("Invalid length %d, expected %d", len(*buf), bufSize)
}
require.Len(t, *buf, bufSize, "unexpected buf length")
sPool := p.findPool(bufSize)
if sPool == nil {
if cap(*buf) != len(*buf) {
t.Fatalf("Invalid cap %d, expected %d", cap(*buf), len(*buf))
}
require.Equal(t, len(*buf), cap(*buf), "unexpected buf cap")
} else {
if cap(*buf) != sPool.size {
t.Fatalf("Invalid cap %d, expected %d", cap(*buf), sPool.size)
}
require.Equal(t, sPool.size, cap(*buf), "unexpected buf cap")
}
p.Put(buf)
}
Expand Down
4 changes: 2 additions & 2 deletions go/cache/theine/bf/bf.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (d *Bloomfilter) EnsureCapacity(capacity int) {
func (d *Bloomfilter) Exist(h uint64) bool {
h1, h2 := uint32(h), uint32(h>>32)
var o uint = 1
for i := uint32(0); i < d.K; i++ {
for i := range d.K {
o &= d.Filter.get((h1 + (i * h2)) & (d.M - 1))
}
return o == 1
Expand All @@ -65,7 +65,7 @@ func (d *Bloomfilter) Exist(h uint64) bool {
func (d *Bloomfilter) Insert(h uint64) bool {
h1, h2 := uint32(h), uint32(h>>32)
var o uint = 1
for i := uint32(0); i < d.K; i++ {
for i := range d.K {
o &= d.Filter.getset((h1 + (i * h2)) & (d.M - 1))
}
return o == 1
Expand Down
10 changes: 5 additions & 5 deletions go/cache/theine/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestList(t *testing.T) {
l := NewList[StringKey, string](5, LIST_PROBATION)
require.Equal(t, uint(5), l.capacity)
require.Equal(t, LIST_PROBATION, l.listType)
for i := 0; i < 5; i++ {
for i := range 5 {
evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1))
require.Nil(t, evicted)
}
Expand All @@ -42,15 +42,15 @@ func TestList(t *testing.T) {
require.Equal(t, "5/4/3/2/1", l.display())
require.Equal(t, "1/2/3/4/5", l.displayReverse())

for i := 0; i < 5; i++ {
for i := range 5 {
entry := l.PopTail()
require.Equal(t, StringKey(fmt.Sprintf("%d", i+1)), entry.key)
}
entry := l.PopTail()
require.Nil(t, entry)

var entries []*Entry[StringKey, string]
for i := 0; i < 5; i++ {
for i := range 5 {
new := NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 1)
evicted := l.PushFront(new)
entries = append(entries, new)
Expand All @@ -76,13 +76,13 @@ func TestListCountCost(t *testing.T) {
l := NewList[StringKey, string](100, LIST_PROBATION)
require.Equal(t, uint(100), l.capacity)
require.Equal(t, LIST_PROBATION, l.listType)
for i := 0; i < 5; i++ {
for i := range 5 {
evicted := l.PushFront(NewEntry(StringKey(fmt.Sprintf("%d", i)), "", 20))
require.Nil(t, evicted)
}
require.Equal(t, 100, l.len)
require.Equal(t, 5, l.count)
for i := 0; i < 3; i++ {
for range 3 {
entry := l.PopTail()
require.NotNil(t, entry)
}
Expand Down
Loading

0 comments on commit 3a5907f

Please sign in to comment.