mirror of
https://github.com/pgsty/minio.git
synced 2026-07-22 13:40:22 +03:00
Kafka notify: support batched commits for queue store (#20377)
The items will be saved per target batch and will be committed to the queue store when the batch is full Also, periodically commit the batched items to the queue store based on configured commit_timeout; default is 30s; Bonus: compress queue store multi writes
This commit is contained in:
+86
-63
@@ -18,100 +18,123 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrBatchFull indicates that the batch is full
|
||||
var ErrBatchFull = errors.New("batch is full")
|
||||
|
||||
type key interface {
|
||||
string | int | int64
|
||||
}
|
||||
const defaultCommitTimeout = 30 * time.Second
|
||||
|
||||
// Batch represents an ordered batch
|
||||
type Batch[K key, T any] struct {
|
||||
keys []K
|
||||
items map[K]T
|
||||
limit uint32
|
||||
type Batch[I any] struct {
|
||||
items []I
|
||||
limit uint32
|
||||
store Store[I]
|
||||
quitCh chan struct{}
|
||||
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// BatchConfig represents the batch config
|
||||
type BatchConfig[I any] struct {
|
||||
Limit uint32
|
||||
Store Store[I]
|
||||
CommitTimeout time.Duration
|
||||
Log logger
|
||||
}
|
||||
|
||||
// Add adds the item to the batch
|
||||
func (b *Batch[K, T]) Add(key K, item T) error {
|
||||
func (b *Batch[I]) Add(item I) error {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
if b.isFull() {
|
||||
return ErrBatchFull
|
||||
if b.store == nil {
|
||||
return ErrBatchFull
|
||||
}
|
||||
// commit batch to store
|
||||
if err := b.commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := b.items[key]; !ok {
|
||||
b.keys = append(b.keys, key)
|
||||
}
|
||||
b.items[key] = item
|
||||
|
||||
b.items = append(b.items, item)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAll fetches the items and resets the batch
|
||||
// Returned items are not referenced by the batch
|
||||
func (b *Batch[K, T]) GetAll() (orderedKeys []K, orderedItems []T, err error) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
orderedKeys = append([]K(nil), b.keys...)
|
||||
for _, key := range orderedKeys {
|
||||
item, ok := b.items[key]
|
||||
if !ok {
|
||||
err = fmt.Errorf("item not found for the key: %v; should not happen;", key)
|
||||
return
|
||||
}
|
||||
orderedItems = append(orderedItems, item)
|
||||
delete(b.items, key)
|
||||
}
|
||||
|
||||
b.keys = b.keys[:0]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetByKey will get the batch item by the provided key
|
||||
func (b *Batch[K, T]) GetByKey(key K) (T, bool) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
item, ok := b.items[key]
|
||||
return item, ok
|
||||
}
|
||||
|
||||
// Len returns the no of items in the batch
|
||||
func (b *Batch[K, T]) Len() int {
|
||||
func (b *Batch[_]) Len() int {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
return len(b.keys)
|
||||
return len(b.items)
|
||||
}
|
||||
|
||||
// IsFull checks if the batch is full or not
|
||||
func (b *Batch[K, T]) IsFull() bool {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
|
||||
return b.isFull()
|
||||
}
|
||||
|
||||
func (b *Batch[K, T]) isFull() bool {
|
||||
func (b *Batch[_]) isFull() bool {
|
||||
return len(b.items) >= int(b.limit)
|
||||
}
|
||||
|
||||
// NewBatch creates a new batch
|
||||
func NewBatch[K key, T any](limit uint32) *Batch[K, T] {
|
||||
return &Batch[K, T]{
|
||||
keys: make([]K, 0, limit),
|
||||
items: make(map[K]T, limit),
|
||||
limit: limit,
|
||||
func (b *Batch[I]) commit() error {
|
||||
switch len(b.items) {
|
||||
case 0:
|
||||
return nil
|
||||
case 1:
|
||||
_, err := b.store.Put(b.items[0])
|
||||
return err
|
||||
default:
|
||||
}
|
||||
if _, err := b.store.PutMultiple(b.items); err != nil {
|
||||
return err
|
||||
}
|
||||
b.items = make([]I, 0, b.limit)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close commits the pending items and quits the goroutines
|
||||
func (b *Batch[I]) Close() error {
|
||||
defer func() {
|
||||
close(b.quitCh)
|
||||
}()
|
||||
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
return b.commit()
|
||||
}
|
||||
|
||||
// NewBatch creates a new batch
|
||||
func NewBatch[I any](config BatchConfig[I]) *Batch[I] {
|
||||
if config.CommitTimeout == 0 {
|
||||
config.CommitTimeout = defaultCommitTimeout
|
||||
}
|
||||
quitCh := make(chan struct{})
|
||||
batch := &Batch[I]{
|
||||
items: make([]I, 0, config.Limit),
|
||||
limit: config.Limit,
|
||||
store: config.Store,
|
||||
quitCh: quitCh,
|
||||
}
|
||||
if batch.store != nil {
|
||||
go func() {
|
||||
commitTicker := time.NewTicker(config.CommitTimeout)
|
||||
defer commitTicker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-commitTicker.C:
|
||||
case <-batch.quitCh:
|
||||
return
|
||||
}
|
||||
batch.Lock()
|
||||
err := batch.commit()
|
||||
batch.Unlock()
|
||||
if err != nil {
|
||||
config.Log(context.Background(), err, "")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
return batch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user