diff --git a/weed/pb/grpc_buffer_pool_test.go b/weed/pb/grpc_buffer_pool_test.go new file mode 100644 index 0000000000..230feb2cd7 --- /dev/null +++ b/weed/pb/grpc_buffer_pool_test.go @@ -0,0 +1,38 @@ +package pb + +import ( + "strconv" + "testing" + + util_mem "github.com/seaweedfs/seaweedfs/weed/util/mem" + "google.golang.org/grpc/mem" +) + +// The proto codec takes its buffers from the process-wide default pool. +func TestGrpcBufferPoolIsTheDefault(t *testing.T) { + if mem.DefaultBufferPool() != (util_mem.SlotPool{}) { + t.Fatal("gRPC's default buffer pool is not the slot pool") + } +} + +// gRPC's default pool zeroes the whole buffer on every Get, and between 32 KB and 1 MB that buffer +// is 1 MB. Compares it with the slot pool. +func BenchmarkGrpcBufferPoolGet(b *testing.B) { + defaultTiers, err := mem.NewBinaryTieredBufferPool(8, 12, 14, 15, 20) + if err != nil { + b.Fatal(err) + } + for _, size := range []int{40 << 10, 100 << 10, 300 << 10, 3 << 20} { + for _, p := range []struct { + name string + pool mem.BufferPool + }{{"grpc-default", defaultTiers}, {"slots", util_mem.SlotPool{}}} { + b.Run(p.name+"/"+strconv.Itoa(size>>10)+"KB", func(b *testing.B) { + b.SetBytes(int64(size)) + for i := 0; i < b.N; i++ { + p.pool.Put(p.pool.Get(size)) + } + }) + } + } +} diff --git a/weed/pb/grpc_client_server.go b/weed/pb/grpc_client_server.go index 2e25cc5589..001a71cf48 100644 --- a/weed/pb/grpc_client_server.go +++ b/weed/pb/grpc_client_server.go @@ -23,9 +23,11 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" + util_mem "github.com/seaweedfs/seaweedfs/weed/util/mem" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/experimental" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/status" @@ -264,6 +266,15 @@ func (l *localSocketListener) Accept() (net.Conn, error) { return c, nil } +// gRPC's default tiers jump from 32 KB to 1 MB, and a pooled buffer is zeroed whole on every Get, so +// a 40 KB message zeroed 1 MB. The slot pool rounds up to the next power of two and skips the +// zeroing, since gRPC writes a pooled buffer before reading it. Dials read the default pool per +// connection; servers copy gRPC's defaults, which captured the old pool before this init ran, so +// NewGrpcServer passes it explicitly. +func init() { + experimental.SetDefaultBufferPool(util_mem.SlotPool{}) +} + func NewGrpcServer(opts ...grpc.ServerOption) *grpc.Server { var options []grpc.ServerOption options = append(options, @@ -282,6 +293,7 @@ func NewGrpcServer(opts ...grpc.ServerOption) *grpc.Server { grpc.InitialConnWindowSize(16*1024*1024), // 16MB connection window grpc.MaxHeaderListSize(8*1024*1024), // 8MB header list limit grpc.UnaryInterceptor(requestIDUnaryInterceptor()), + experimental.BufferPool(util_mem.SlotPool{}), ) for _, opt := range opts { if opt != nil { diff --git a/weed/util/log_buffer/log_buffer.go b/weed/util/log_buffer/log_buffer.go index e0a6c0e649..a1bca39f24 100644 --- a/weed/util/log_buffer/log_buffer.go +++ b/weed/util/log_buffer/log_buffer.go @@ -1077,9 +1077,7 @@ func (logBuffer *LogBuffer) SetLastFlushTsNs(ts int64) { } func (d *dataToFlush) releaseMemory() { - // Guard nil: mem.Free(nil) would put a zero-cap slice into the smallest slot - // pool, so a later Allocate could hand back nil and panic. Also makes a double - // release harmless. + // Clearing data makes a double release harmless. if d.data != nil { mem.Free(d.data) d.data = nil diff --git a/weed/util/mem/slot_pool.go b/weed/util/mem/slot_pool.go index 897034e649..cabed2ffeb 100644 --- a/weed/util/mem/slot_pool.go +++ b/weed/util/mem/slot_pool.go @@ -46,16 +46,29 @@ func AllocationSize(size int) int { return size } -func Allocate(size int) []byte { +// SlotPool serves the slots through gRPC's mem.BufferPool interface. Buffers are not zeroed. +type SlotPool struct{} + +func (SlotPool) Get(size int) *[]byte { if pool, found := getSlotPool(size); found { - slab := *pool.Get().(*[]byte) - return slab[:size] + buf := pool.Get().(*[]byte) + *buf = (*buf)[:size] + return buf } - return make([]byte, size) + buf := make([]byte, size) + return &buf +} + +func (SlotPool) Put(buf *[]byte) { + if pool, found := getSlotPool(cap(*buf)); found && cap(*buf) == AllocationSize(cap(*buf)) { + pool.Put(buf) + } +} + +func Allocate(size int) []byte { + return *SlotPool{}.Get(size) } func Free(buf []byte) { - if pool, found := getSlotPool(cap(buf)); found { - pool.Put(&buf) - } + SlotPool{}.Put(&buf) } diff --git a/weed/util/mem/slot_pool_test.go b/weed/util/mem/slot_pool_test.go index 620f2da8b0..b2c1841d1a 100644 --- a/weed/util/mem/slot_pool_test.go +++ b/weed/util/mem/slot_pool_test.go @@ -56,3 +56,31 @@ func TestAllocationSize(t *testing.T) { } assert.Equal(t, 257<<20, AllocationSize(257<<20)) } + +func TestFreeKeepsOnlySlotSizedBuffers(t *testing.T) { + for _, buf := range [][]byte{nil, make([]byte, 1500), make([]byte, 3000)} { + Free(buf) + for _, size := range []int{1, 2048, 4096} { + got := Allocate(size) + assert.Equal(t, AllocationSize(size), cap(got)) + Free(got) + } + } +} + +func TestSlotPoolGetPut(t *testing.T) { + for _, tc := range []struct{ size, wantCap int }{ + {0, 1 << 10}, + {4 << 10, 4 << 10}, + {(32 << 10) + 1, 64 << 10}, + {40 << 10, 64 << 10}, + {(1 << 20) + 1, 2 << 20}, + {3 << 20, 4 << 20}, + } { + buf := SlotPool{}.Get(tc.size) + if len(*buf) != tc.size || cap(*buf) != tc.wantCap { + t.Errorf("Get(%d): len %d cap %d, want len %d cap %d", tc.size, len(*buf), cap(*buf), tc.size, tc.wantCap) + } + SlotPool{}.Put(buf) + } +}