fix: implement group commit collection window (500µs or 32KB)

Replace polling loop (Collect + Sleep) with proper batch collection:
- Block on first request via channel receive
- Start 500µs timer for collection window
- Accumulate requests until timer fires or batch reaches 32KB
- Shutdown handling at all blocking points

Design doc §3.2.5: '等待组提交触发(500µs 或 32KB,先到者触发)'

Add GroupCommitDelay config field (default 500µs, must be > 0 and < 10ms).
This commit is contained in:
dailz
2026-06-12 16:15:08 +08:00
parent 56bec62a6e
commit e34de4acc9
3 changed files with 96 additions and 15 deletions
+26
View File
@@ -2,6 +2,7 @@ package config
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -13,17 +14,42 @@ func TestValidateDefaults(t *testing.T) {
require.NoError(t, err, "default WalConfig should pass validation")
d := Defaults()
assert.Equal(t, 500*time.Microsecond, d.GroupCommitDelay)
assert.Equal(t, d.MaxSegmentSize, cfg.MaxSegmentSize, "MaxSegmentSize should be defaulted")
assert.Equal(t, d.BlockSize, cfg.BlockSize, "BlockSize should be defaulted")
assert.Equal(t, d.SyncMode, cfg.SyncMode, "SyncMode should be defaulted")
assert.Equal(t, d.MaxBatchEntries, cfg.MaxBatchEntries, "MaxBatchEntries should be defaulted")
assert.Equal(t, d.MaxBatchSize, cfg.MaxBatchSize, "MaxBatchSize should be defaulted")
assert.Equal(t, d.GroupCommitDelay, cfg.GroupCommitDelay, "GroupCommitDelay should be defaulted")
assert.Equal(t, d.MaxKeyBytes, cfg.MaxKeyBytes, "MaxKeyBytes should be defaulted")
assert.Equal(t, d.MaxInlineValue, cfg.MaxInlineValue, "MaxInlineValue should be defaulted")
assert.Equal(t, d.MemTableSize, cfg.MemTableSize, "MemTableSize should be defaulted")
assert.Equal(t, d.MaxImmutableCount, cfg.MaxImmutableCount, "MaxImmutableCount should be defaulted")
}
func TestValidateGroupCommitDelay(t *testing.T) {
tests := []struct {
name string
delay time.Duration
want string
}{
{name: "negative delay", delay: -time.Microsecond, want: "GroupCommitDelay"},
{name: "ten milliseconds rejected", delay: 10 * time.Millisecond, want: "GroupCommitDelay"},
{name: "above ten milliseconds rejected", delay: 11 * time.Millisecond, want: "GroupCommitDelay"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := Defaults()
cfg.GroupCommitDelay = tt.delay
err := cfg.Validate()
require.Error(t, err)
assert.Contains(t, err.Error(), tt.want)
})
}
}
func TestValidateSegmentTooSmall(t *testing.T) {
cfg := WalConfig{MaxSegmentSize: 1024}
err := cfg.Validate()