2023-06-07 14:47:00 -04:00
|
|
|
// Copyright (c) 2015-2023 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
package store
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
import (
|
2024-09-06 19:06:30 -04:00
|
|
|
"bytes"
|
2019-02-25 21:01:13 -05:00
|
|
|
"encoding/json"
|
2023-04-04 13:52:24 -04:00
|
|
|
"errors"
|
2019-02-25 21:01:13 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-05-22 16:34:48 -04:00
|
|
|
"sort"
|
2019-02-25 21:01:13 -05:00
|
|
|
"sync"
|
2022-11-08 19:36:47 -05:00
|
|
|
"time"
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
"github.com/google/uuid"
|
2024-03-25 12:44:20 -04:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-09-06 19:06:30 -04:00
|
|
|
"github.com/klauspost/compress/s2"
|
2024-03-25 12:44:20 -04:00
|
|
|
"github.com/valyala/bytebufferpool"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-18 04:20:56 -04:00
|
|
|
defaultLimit = 100000 // Default store limit.
|
2023-04-04 13:52:24 -04:00
|
|
|
defaultExt = ".unknown"
|
2024-09-06 19:06:30 -04:00
|
|
|
compressExt = ".snappy"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// errLimitExceeded error is sent when the maximum limit is reached.
|
|
|
|
var errLimitExceeded = errors.New("the maximum store limit reached")
|
|
|
|
|
|
|
|
// QueueStore - Filestore for persisting items.
|
|
|
|
type QueueStore[_ any] struct {
|
2019-02-25 21:01:13 -05:00
|
|
|
sync.RWMutex
|
2022-11-08 19:36:47 -05:00
|
|
|
entryLimit uint64
|
|
|
|
directory string
|
2023-04-04 13:52:24 -04:00
|
|
|
fileExt string
|
2022-11-08 19:36:47 -05:00
|
|
|
|
|
|
|
entries map[string]int64 // key -> modtime as unix nano
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueueStore - Creates an instance for QueueStore.
|
2023-04-04 13:52:24 -04:00
|
|
|
func NewQueueStore[I any](directory string, limit uint64, ext string) *QueueStore[I] {
|
2019-02-25 21:01:13 -05:00
|
|
|
if limit == 0 {
|
2020-04-18 04:20:56 -04:00
|
|
|
limit = defaultLimit
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2019-07-11 22:53:20 -04:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
if ext == "" {
|
|
|
|
ext = defaultExt
|
|
|
|
}
|
|
|
|
|
|
|
|
return &QueueStore[I]{
|
2019-10-11 20:46:03 -04:00
|
|
|
directory: directory,
|
|
|
|
entryLimit: limit,
|
2023-04-04 13:52:24 -04:00
|
|
|
fileExt: ext,
|
2022-11-08 19:36:47 -05:00
|
|
|
entries: make(map[string]int64, limit),
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open - Creates the directory if not present.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) Open() error {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
|
2022-01-02 12:15:06 -05:00
|
|
|
if err := os.MkdirAll(store.directory, os.FileMode(0o770)); err != nil {
|
2019-10-11 20:46:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
files, err := store.list()
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi, err := file.Info(); err == nil {
|
2024-09-06 19:06:30 -04:00
|
|
|
store.entries[file.Name()] = fi.ModTime().UnixNano()
|
2022-11-08 19:36:47 -05:00
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-25 12:44:20 -04:00
|
|
|
// Delete - Remove the store directory from disk
|
|
|
|
func (store *QueueStore[_]) Delete() error {
|
|
|
|
return os.Remove(store.directory)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutMultiple - puts an item to the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) PutMultiple(items []I) (Key, error) {
|
2024-03-25 12:44:20 -04:00
|
|
|
// Generate a new UUID for the key.
|
2024-09-06 19:06:30 -04:00
|
|
|
uid, err := uuid.NewRandom()
|
2024-03-25 12:44:20 -04:00
|
|
|
if err != nil {
|
2024-09-06 19:06:30 -04:00
|
|
|
return Key{}, err
|
2024-03-25 12:44:20 -04:00
|
|
|
}
|
2024-08-28 11:31:56 -04:00
|
|
|
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if uint64(len(store.entries)) >= store.entryLimit {
|
2024-09-06 19:06:30 -04:00
|
|
|
return Key{}, errLimitExceeded
|
2024-08-28 11:31:56 -04:00
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
key := Key{
|
|
|
|
Name: uid.String(),
|
|
|
|
ItemCount: len(items),
|
|
|
|
Compress: true,
|
|
|
|
Extension: store.fileExt,
|
|
|
|
}
|
|
|
|
return key, store.multiWrite(key, items)
|
2024-03-25 12:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// multiWrite - writes an item to the directory.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) multiWrite(key Key, items []I) (err error) {
|
2024-03-25 12:44:20 -04:00
|
|
|
buf := bytebufferpool.Get()
|
|
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
|
|
|
|
enc := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(buf)
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
for i := range items {
|
|
|
|
if err = enc.Encode(items[i]); err != nil {
|
2024-03-25 12:44:20 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
path := filepath.Join(store.directory, key.String())
|
|
|
|
if key.Compress {
|
|
|
|
err = os.WriteFile(path, s2.Encode(nil, buf.Bytes()), os.FileMode(0o770))
|
|
|
|
} else {
|
|
|
|
err = os.WriteFile(path, buf.Bytes(), os.FileMode(0o770))
|
|
|
|
}
|
|
|
|
|
2024-03-25 12:44:20 -04:00
|
|
|
buf.Reset()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the item count.
|
2024-09-06 19:06:30 -04:00
|
|
|
store.entries[key.String()] = time.Now().UnixNano()
|
2024-03-25 12:44:20 -04:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
return
|
2024-03-25 12:44:20 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// write - writes an item to the directory.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) write(key Key, item I) error {
|
2023-04-04 13:52:24 -04:00
|
|
|
// Marshalls the item.
|
|
|
|
eventData, err := json.Marshal(item)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
return store.writeBytes(key, eventData)
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
// writeBytes - writes bytes to the directory.
|
|
|
|
func (store *QueueStore[I]) writeBytes(key Key, b []byte) (err error) {
|
|
|
|
path := filepath.Join(store.directory, key.String())
|
|
|
|
|
|
|
|
if key.Compress {
|
|
|
|
err = os.WriteFile(path, s2.Encode(nil, b), os.FileMode(0o770))
|
|
|
|
} else {
|
|
|
|
err = os.WriteFile(path, b, os.FileMode(0o770))
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Increment the item count.
|
2024-09-06 19:06:30 -04:00
|
|
|
store.entries[key.String()] = time.Now().UnixNano()
|
2019-02-25 21:01:13 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Put - puts an item to the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) Put(item I) (Key, error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2022-11-08 19:36:47 -05:00
|
|
|
if uint64(len(store.entries)) >= store.entryLimit {
|
2024-09-06 19:06:30 -04:00
|
|
|
return Key{}, errLimitExceeded
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Generate a new UUID for the key.
|
2024-09-06 19:06:30 -04:00
|
|
|
uid, err := uuid.NewRandom()
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
2024-09-06 19:06:30 -04:00
|
|
|
return Key{}, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
key := Key{
|
|
|
|
Name: uid.String(),
|
|
|
|
Extension: store.fileExt,
|
|
|
|
ItemCount: 1,
|
|
|
|
}
|
|
|
|
return key, store.write(key, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutRaw - puts the raw bytes to the store
|
|
|
|
func (store *QueueStore[I]) PutRaw(b []byte) (Key, error) {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
if uint64(len(store.entries)) >= store.entryLimit {
|
|
|
|
return Key{}, errLimitExceeded
|
|
|
|
}
|
|
|
|
// Generate a new UUID for the key.
|
|
|
|
uid, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return Key{}, err
|
|
|
|
}
|
|
|
|
key := Key{
|
|
|
|
Name: uid.String(),
|
|
|
|
Extension: store.fileExt,
|
|
|
|
}
|
|
|
|
return key, store.writeBytes(key, b)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2024-03-25 12:44:20 -04:00
|
|
|
// GetRaw - gets an item from the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) GetRaw(key Key) (raw []byte, err error) {
|
2024-03-25 12:44:20 -04:00
|
|
|
store.RLock()
|
|
|
|
|
|
|
|
defer func(store *QueueStore[I]) {
|
|
|
|
store.RUnlock()
|
2024-09-06 19:06:30 -04:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2024-03-25 12:44:20 -04:00
|
|
|
// Upon error we remove the entry.
|
|
|
|
store.Del(key)
|
|
|
|
}
|
|
|
|
}(store)
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
raw, err = os.ReadFile(filepath.Join(store.directory, key.String()))
|
2024-03-25 12:44:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) == 0 {
|
|
|
|
return raw, os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Get - gets an item from the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[I]) Get(key Key) (item I, err error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
defer func(store *QueueStore[I]) {
|
2019-10-11 20:46:03 -04:00
|
|
|
store.RUnlock()
|
2024-09-06 19:06:30 -04:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-10-11 20:46:03 -04:00
|
|
|
// Upon error we remove the entry.
|
|
|
|
store.Del(key)
|
|
|
|
}
|
|
|
|
}(store)
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
var eventData []byte
|
2024-09-06 19:06:30 -04:00
|
|
|
eventData, err = os.ReadFile(filepath.Join(store.directory, key.String()))
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventData) == 0 {
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, os.ErrNotExist
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
if err = json.Unmarshal(eventData, &item); err != nil {
|
|
|
|
return item, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
return item, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
// GetMultiple will read the multi payload file and fetch the items
|
|
|
|
func (store *QueueStore[I]) GetMultiple(key Key) (items []I, err error) {
|
|
|
|
store.RLock()
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
defer func(store *QueueStore[I]) {
|
|
|
|
store.RUnlock()
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
// Upon error we remove the entry.
|
|
|
|
store.Del(key)
|
|
|
|
}
|
|
|
|
}(store)
|
2023-10-07 11:07:38 -04:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
raw, err := os.ReadFile(filepath.Join(store.directory, key.String()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var decoder *jsoniter.Decoder
|
|
|
|
if key.Compress {
|
|
|
|
decodedBytes, err := s2.Decode(nil, raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-10-07 11:07:38 -04:00
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
decoder = jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewReader(decodedBytes))
|
|
|
|
} else {
|
|
|
|
decoder = jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewReader(raw))
|
2023-10-07 11:07:38 -04:00
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
for decoder.More() {
|
|
|
|
var item I
|
|
|
|
if err := decoder.Decode(&item); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del - Deletes an entry from the store.
|
|
|
|
func (store *QueueStore[_]) Del(key Key) error {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
return store.del(key)
|
2023-10-07 11:07:38 -04:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Len returns the entry count.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) Len() int {
|
2022-11-08 19:36:47 -05:00
|
|
|
store.RLock()
|
|
|
|
l := len(store.entries)
|
|
|
|
defer store.RUnlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:01:13 -05:00
|
|
|
// lockless call
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[_]) del(key Key) error {
|
|
|
|
err := os.Remove(filepath.Join(store.directory, key.String()))
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Delete as entry no matter the result
|
2024-09-06 19:06:30 -04:00
|
|
|
delete(store.entries, key.String())
|
2022-11-08 19:36:47 -05:00
|
|
|
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// List - lists all files registered in the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
func (store *QueueStore[_]) List() (keys []Key) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
2024-09-06 19:06:30 -04:00
|
|
|
defer store.RUnlock()
|
|
|
|
|
|
|
|
entries := make([]string, 0, len(store.entries))
|
|
|
|
for entry := range store.entries {
|
|
|
|
entries = append(entries, entry)
|
2022-11-08 19:36:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort entries...
|
2024-09-06 19:06:30 -04:00
|
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
|
|
return store.entries[entries[i]] < store.entries[entries[j]]
|
2022-11-08 19:36:47 -05:00
|
|
|
})
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
for i := range entries {
|
|
|
|
keys = append(keys, parseKey(entries[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// list will read all entries from disk.
|
|
|
|
// Entries are returned sorted by modtime, oldest first.
|
|
|
|
// Underlying entry list in store is *not* updated.
|
2023-04-04 13:52:24 -04:00
|
|
|
func (store *QueueStore[_]) list() ([]os.DirEntry, error) {
|
2022-09-19 14:05:16 -04:00
|
|
|
files, err := os.ReadDir(store.directory)
|
2019-10-11 20:46:03 -04:00
|
|
|
if err != nil {
|
2022-11-08 19:36:47 -05:00
|
|
|
return nil, err
|
2019-10-11 20:46:03 -04:00
|
|
|
}
|
2019-05-22 16:34:48 -04:00
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Sort the entries.
|
2019-05-22 16:34:48 -04:00
|
|
|
sort.Slice(files, func(i, j int) bool {
|
2022-09-19 14:05:16 -04:00
|
|
|
ii, err := files[i].Info()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ji, err := files[j].Info()
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ii.ModTime().Before(ji.ModTime())
|
2019-05-22 16:34:48 -04:00
|
|
|
})
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
return files, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|