2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2019-10-11 20:46:03 -04:00
|
|
|
"math"
|
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"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/event"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/sys"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-18 04:20:56 -04:00
|
|
|
defaultLimit = 100000 // Default store limit.
|
|
|
|
eventExt = ".event"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// QueueStore - Filestore for persisting events.
|
|
|
|
type QueueStore struct {
|
|
|
|
sync.RWMutex
|
2019-10-11 20:46:03 -04:00
|
|
|
currentEntries uint64
|
|
|
|
entryLimit uint64
|
|
|
|
directory string
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueueStore - Creates an instance for QueueStore.
|
2019-10-11 20:46:03 -04:00
|
|
|
func NewQueueStore(directory string, limit uint64) Store {
|
2019-02-25 21:01:13 -05:00
|
|
|
if limit == 0 {
|
2020-04-18 04:20:56 -04:00
|
|
|
limit = defaultLimit
|
2019-10-11 20:46:03 -04:00
|
|
|
_, maxRLimit, err := sys.GetMaxOpenFileLimit()
|
2019-07-11 22:53:20 -04:00
|
|
|
if err == nil {
|
2019-10-11 20:46:03 -04:00
|
|
|
// Limit the maximum number of entries
|
|
|
|
// to maximum open file limit
|
|
|
|
if maxRLimit < limit {
|
|
|
|
limit = maxRLimit
|
2019-07-11 22:53:20 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2019-07-11 22:53:20 -04:00
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
return &QueueStore{
|
|
|
|
directory: directory,
|
|
|
|
entryLimit: limit,
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open - Creates the directory if not present.
|
|
|
|
func (store *QueueStore) Open() error {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
if err := os.MkdirAll(store.directory, os.FileMode(0770)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
names, err := store.list()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
currentEntries := uint64(len(names))
|
|
|
|
if currentEntries >= store.entryLimit {
|
2019-04-10 08:46:01 -04:00
|
|
|
return errLimitExceeded
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
store.currentEntries = currentEntries
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write - writes event to the directory.
|
2019-10-15 21:35:41 -04:00
|
|
|
func (store *QueueStore) write(key string, e event.Event) error {
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
// Marshalls the event.
|
|
|
|
eventData, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(store.directory, key+eventExt)
|
|
|
|
if err := ioutil.WriteFile(path, eventData, os.FileMode(0770)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the event count.
|
2019-10-11 20:46:03 -04:00
|
|
|
store.currentEntries++
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put - puts a event to the store.
|
|
|
|
func (store *QueueStore) Put(e event.Event) error {
|
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2019-10-11 20:46:03 -04:00
|
|
|
if store.currentEntries >= store.entryLimit {
|
2019-04-10 08:46:01 -04:00
|
|
|
return errLimitExceeded
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2019-10-11 20:46:03 -04:00
|
|
|
key, err := getNewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2019-10-15 21:35:41 -04:00
|
|
|
return store.write(key, e)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get - gets a event from the store.
|
2019-10-11 20:46:03 -04:00
|
|
|
func (store *QueueStore) Get(key string) (event event.Event, err error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
defer func(store *QueueStore) {
|
|
|
|
store.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
// 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
|
|
|
|
eventData, err = ioutil.ReadFile(filepath.Join(store.directory, key+eventExt))
|
|
|
|
if err != nil {
|
|
|
|
return event, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(eventData) == 0 {
|
2019-10-11 20:46:03 -04:00
|
|
|
return event, os.ErrNotExist
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
if err = json.Unmarshal(eventData, &event); err != nil {
|
|
|
|
return event, err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del - Deletes an entry from the store.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (store *QueueStore) Del(key string) error {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.Lock()
|
|
|
|
defer store.Unlock()
|
2019-04-10 08:46:01 -04:00
|
|
|
return store.del(key)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// lockless call
|
2019-04-10 08:46:01 -04:00
|
|
|
func (store *QueueStore) del(key string) error {
|
2019-10-11 20:46:03 -04:00
|
|
|
if err := os.Remove(filepath.Join(store.directory, key+eventExt)); err != nil {
|
|
|
|
return err
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
// Decrement the current entries count.
|
|
|
|
store.currentEntries--
|
|
|
|
|
|
|
|
// Current entries can underflow, when multiple
|
|
|
|
// events are being pushed in parallel, this code
|
|
|
|
// is needed to ensure that we don't underflow.
|
|
|
|
//
|
|
|
|
// queueStore replayEvents is not serialized,
|
|
|
|
// this code is needed to protect us under
|
|
|
|
// such situations.
|
|
|
|
if store.currentEntries == math.MaxUint64 {
|
|
|
|
store.currentEntries = 0
|
|
|
|
}
|
2019-04-10 08:46:01 -04:00
|
|
|
return nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-05-22 16:34:48 -04:00
|
|
|
// List - lists all files from the directory.
|
2019-10-11 20:46:03 -04:00
|
|
|
func (store *QueueStore) List() ([]string, error) {
|
2019-02-25 21:01:13 -05:00
|
|
|
store.RLock()
|
|
|
|
defer store.RUnlock()
|
2019-05-22 16:34:48 -04:00
|
|
|
return store.list()
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
// list lock less.
|
|
|
|
func (store *QueueStore) list() ([]string, error) {
|
2019-05-22 16:34:48 -04:00
|
|
|
var names []string
|
2019-10-11 20:46:03 -04:00
|
|
|
files, err := ioutil.ReadDir(store.directory)
|
|
|
|
if err != nil {
|
|
|
|
return names, err
|
|
|
|
}
|
2019-05-22 16:34:48 -04:00
|
|
|
|
|
|
|
// Sort the dentries.
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
2019-10-11 20:46:03 -04:00
|
|
|
return files[i].ModTime().Before(files[j].ModTime())
|
2019-05-22 16:34:48 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
names = append(names, file.Name())
|
|
|
|
}
|
|
|
|
|
2019-10-11 20:46:03 -04:00
|
|
|
return names, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|