mirror of
https://github.com/minio/minio.git
synced 2024-12-25 06:35:56 -05:00
d4b391de1b
Replace the `io.Pipe` from streamingBitrotWriter -> CreateFile with a fixed size ring buffer. This will add an output buffer for encoded shards to be written to disk - potentially via RPC. This will remove blocking when `(*streamingBitrotWriter).Write` is called, and it writes hashes and data. With current settings, the write looks like this: ``` Outbound ┌───────────────────┐ ┌────────────────┐ ┌───────────────┐ ┌────────────────┐ │ │ Parr. │ │ (http body) │ │ │ │ │ Bitrot Hash │ Write │ Pipe │ Read │ HTTP buffer │ Write (syscall) │ TCP Buffer │ │ Erasure Shard │ ──────────► │ (unbuffered) │ ────────────► │ (64K Max) │ ───────────────────► │ (4MB) │ │ │ │ │ │ (io.Copy) │ │ │ └───────────────────┘ └────────────────┘ └───────────────┘ └────────────────┘ ``` We write a Hash (32 bytes). Since the pipe is unbuffered, it will block until the 32 bytes have been delivered to the TCP buffer, and the next Read hits the Pipe. Then we write the shard data. This will typically be bigger than 64KB, so it will block until two blocks have been read from the pipe. When we insert a ring buffer: ``` Outbound ┌───────────────────┐ ┌────────────────┐ ┌───────────────┐ ┌────────────────┐ │ │ │ │ (http body) │ │ │ │ │ Bitrot Hash │ Write │ Ring Buffer │ Read │ HTTP buffer │ Write (syscall) │ TCP Buffer │ │ Erasure Shard │ ──────────► │ (2MB) │ ────────────► │ (64K Max) │ ───────────────────► │ (4MB) │ │ │ │ │ │ (io.Copy) │ │ │ └───────────────────┘ └────────────────┘ └───────────────┘ └────────────────┘ ``` The hash+shard will fit within the ring buffer, so writes will not block - but will complete after a memcopy. Reads can fill the 64KB buffer if there is data for it. If the network is congested, the ring buffer will become filled, and all syscalls will be on full buffers. Only when the ring buffer is filled will erasure coding start blocking. Since there is always "space" to write output data, we remove the parallel writing since we are always writing to memory now, and the goroutine synchronization overhead probably not worth taking. If the output were blocked in the existing, we would still wait for it to unblock in parallel write, so it would make no difference there - except now the ring buffer smoothes out the load. There are some micro-optimizations we could look at later. The biggest is that, in most cases, we could encode directly to the ring buffer - if we are not at a boundary. Also, "force filling" the Read requests (i.e., blocking until a full read can be completed) could be investigated and maybe allow concurrent memory on read and write.
112 lines
1.7 KiB
Go
112 lines
1.7 KiB
Go
package ringbuffer
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkRingBuffer_Sync(b *testing.B) {
|
|
rb := New(1024)
|
|
data := []byte(strings.Repeat("a", 512))
|
|
buf := make([]byte, 512)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
rb.Write(data)
|
|
rb.Read(buf)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRingBuffer_AsyncRead(b *testing.B) {
|
|
// Pretty useless benchmark, but it's here for completeness.
|
|
rb := New(1024)
|
|
data := []byte(strings.Repeat("a", 512))
|
|
buf := make([]byte, 512)
|
|
|
|
go func() {
|
|
for {
|
|
rb.Read(buf)
|
|
}
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
rb.Write(data)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRingBuffer_AsyncReadBlocking(b *testing.B) {
|
|
const sz = 512
|
|
const buffers = 10
|
|
rb := New(sz * buffers)
|
|
rb.SetBlocking(true)
|
|
data := []byte(strings.Repeat("a", sz))
|
|
buf := make([]byte, sz)
|
|
|
|
go func() {
|
|
for {
|
|
rb.Read(buf)
|
|
}
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
rb.Write(data)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRingBuffer_AsyncWrite(b *testing.B) {
|
|
rb := New(1024)
|
|
data := []byte(strings.Repeat("a", 512))
|
|
buf := make([]byte, 512)
|
|
|
|
go func() {
|
|
for {
|
|
rb.Write(data)
|
|
}
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
rb.Read(buf)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRingBuffer_AsyncWriteBlocking(b *testing.B) {
|
|
const sz = 512
|
|
const buffers = 10
|
|
rb := New(sz * buffers)
|
|
rb.SetBlocking(true)
|
|
data := []byte(strings.Repeat("a", sz))
|
|
buf := make([]byte, sz)
|
|
|
|
go func() {
|
|
for {
|
|
rb.Write(data)
|
|
}
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
rb.Read(buf)
|
|
}
|
|
}
|
|
|
|
func BenchmarkIoPipeReader(b *testing.B) {
|
|
pr, pw := io.Pipe()
|
|
data := []byte(strings.Repeat("a", 512))
|
|
buf := make([]byte, 512)
|
|
|
|
go func() {
|
|
for {
|
|
pw.Write(data)
|
|
}
|
|
}()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
pr.Read(buf)
|
|
}
|
|
}
|