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/>.
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
2019-07-23 13:22:08 -04:00
|
|
|
"context"
|
2018-03-15 16:03:41 -04:00
|
|
|
"encoding/json"
|
2019-07-23 13:22:08 -04:00
|
|
|
"errors"
|
2018-03-15 16:03:41 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2019-07-23 13:22:08 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-07-18 14:22:29 -04:00
|
|
|
"strings"
|
2018-03-15 16:03:41 -04:00
|
|
|
"time"
|
|
|
|
|
2019-02-11 08:36:27 -05:00
|
|
|
"github.com/gomodule/redigo/redis"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/event"
|
2021-06-14 17:54:37 -04:00
|
|
|
xnet "github.com/minio/pkg/net"
|
2018-03-15 16:03:41 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Redis constants
|
|
|
|
const (
|
|
|
|
RedisFormat = "format"
|
|
|
|
RedisAddress = "address"
|
|
|
|
RedisPassword = "password"
|
|
|
|
RedisKey = "key"
|
|
|
|
RedisQueueDir = "queue_dir"
|
|
|
|
RedisQueueLimit = "queue_limit"
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvRedisEnable = "MINIO_NOTIFY_REDIS_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvRedisFormat = "MINIO_NOTIFY_REDIS_FORMAT"
|
|
|
|
EnvRedisAddress = "MINIO_NOTIFY_REDIS_ADDRESS"
|
|
|
|
EnvRedisPassword = "MINIO_NOTIFY_REDIS_PASSWORD"
|
|
|
|
EnvRedisKey = "MINIO_NOTIFY_REDIS_KEY"
|
|
|
|
EnvRedisQueueDir = "MINIO_NOTIFY_REDIS_QUEUE_DIR"
|
|
|
|
EnvRedisQueueLimit = "MINIO_NOTIFY_REDIS_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// RedisArgs - Redis target arguments.
|
|
|
|
type RedisArgs struct {
|
2019-07-23 13:22:08 -04:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Addr xnet.Host `json:"address"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 12:23:32 -05:00
|
|
|
// RedisAccessEvent holds event log data and timestamp
|
|
|
|
type RedisAccessEvent struct {
|
|
|
|
Event []event.Event
|
|
|
|
EventTime string
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate RedisArgs fields
|
|
|
|
func (r RedisArgs) Validate() error {
|
|
|
|
if !r.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Format != "" {
|
|
|
|
f := strings.ToLower(r.Format)
|
|
|
|
if f != event.NamespaceFormat && f != event.AccessFormat {
|
|
|
|
return fmt.Errorf("unrecognized format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Key == "" {
|
|
|
|
return fmt.Errorf("empty key")
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
if r.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(r.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RedisArgs) validateFormat(c redis.Conn) error {
|
|
|
|
typeAvailable, err := redis.String(c.Do("TYPE", r.Key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if typeAvailable != "none" {
|
|
|
|
expectedType := "hash"
|
|
|
|
if r.Format == event.AccessFormat {
|
|
|
|
expectedType = "list"
|
|
|
|
}
|
|
|
|
|
|
|
|
if typeAvailable != expectedType {
|
|
|
|
return fmt.Errorf("expected type %v does not match with available type %v", expectedType, typeAvailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// RedisTarget - Redis target.
|
|
|
|
type RedisTarget struct {
|
2019-10-07 01:50:24 -04:00
|
|
|
id event.TargetID
|
|
|
|
args RedisArgs
|
|
|
|
pool *redis.Pool
|
|
|
|
store Store
|
|
|
|
firstPing bool
|
2019-10-11 21:50:54 -04:00
|
|
|
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *RedisTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2020-04-21 12:38:32 -04:00
|
|
|
// HasQueueStore - Checks if the queueStore has been configured for the target
|
|
|
|
func (target *RedisTarget) HasQueueStore() bool {
|
|
|
|
return target.store != nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *RedisTarget) IsActive() (bool, error) {
|
2019-07-23 13:22:08 -04:00
|
|
|
conn := target.pool.Get()
|
|
|
|
defer func() {
|
|
|
|
cErr := conn.Close()
|
2019-10-07 01:50:24 -04:00
|
|
|
target.loggerOnce(context.Background(), cErr, target.ID())
|
2019-07-23 13:22:08 -04:00
|
|
|
}()
|
|
|
|
_, pingErr := conn.Do("PING")
|
|
|
|
if pingErr != nil {
|
|
|
|
if IsConnRefusedErr(pingErr) {
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, errNotConnected
|
2019-07-23 13:22:08 -04:00
|
|
|
}
|
2019-12-11 17:27:03 -05:00
|
|
|
return false, pingErr
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save - saves the events to the store if questore is configured, which will be replayed when the redis connection is active.
|
|
|
|
func (target *RedisTarget) Save(eventData event.Event) error {
|
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-23 13:22:08 -04:00
|
|
|
}
|
2019-04-10 08:46:01 -04:00
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
// send - sends an event to the redis.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *RedisTarget) send(eventData event.Event) error {
|
2018-03-15 16:03:41 -04:00
|
|
|
conn := target.pool.Get()
|
|
|
|
defer func() {
|
2019-07-23 13:22:08 -04:00
|
|
|
cErr := conn.Close()
|
2019-10-07 01:50:24 -04:00
|
|
|
target.loggerOnce(context.Background(), cErr, target.ID())
|
2018-03-15 16:03:41 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
if target.args.Format == event.NamespaceFormat {
|
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
|
|
|
if eventData.EventName == event.ObjectRemovedDelete {
|
|
|
|
_, err = conn.Do("HDEL", target.args.Key, key)
|
|
|
|
} else {
|
|
|
|
var data []byte
|
|
|
|
if data, err = json.Marshal(struct{ Records []event.Event }{[]event.Event{eventData}}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Do("HSET", target.args.Key, key, data)
|
|
|
|
}
|
2019-07-23 13:22:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if target.args.Format == event.AccessFormat {
|
2020-02-26 12:23:32 -05:00
|
|
|
data, err := json.Marshal([]RedisAccessEvent{{Event: []event.Event{eventData}, EventTime: eventData.EventTime}})
|
2018-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 13:22:08 -04:00
|
|
|
if _, err := conn.Do("RPUSH", target.args.Key, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
// Send - reads an event from store and sends it to redis.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *RedisTarget) Send(eventKey string) error {
|
2019-07-23 13:22:08 -04:00
|
|
|
conn := target.pool.Get()
|
|
|
|
defer func() {
|
|
|
|
cErr := conn.Close()
|
2019-10-07 01:50:24 -04:00
|
|
|
target.loggerOnce(context.Background(), cErr, target.ID())
|
2019-07-23 13:22:08 -04:00
|
|
|
}()
|
|
|
|
_, pingErr := conn.Do("PING")
|
|
|
|
if pingErr != nil {
|
|
|
|
if IsConnRefusedErr(pingErr) {
|
|
|
|
return errNotConnected
|
|
|
|
}
|
|
|
|
return pingErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !target.firstPing {
|
|
|
|
if err := target.args.validateFormat(conn); err != nil {
|
|
|
|
if IsConnRefusedErr(err) {
|
|
|
|
return errNotConnected
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
target.firstPing = true
|
|
|
|
}
|
|
|
|
|
|
|
|
eventData, eErr := target.store.Get(eventKey)
|
|
|
|
if eErr != nil {
|
|
|
|
// The last event key in a successful batch will be sent in the channel atmost once by the replayEvents()
|
|
|
|
// Such events will not exist and would've been already been sent successfully.
|
|
|
|
if os.IsNotExist(eErr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := target.send(eventData); err != nil {
|
|
|
|
if IsConnRefusedErr(err) {
|
|
|
|
return errNotConnected
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the event from store.
|
|
|
|
return target.store.Del(eventKey)
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:15:44 -05:00
|
|
|
// Close - releases the resources used by the pool.
|
2018-03-15 16:03:41 -04:00
|
|
|
func (target *RedisTarget) Close() error {
|
2020-01-09 09:15:44 -05:00
|
|
|
return target.pool.Close()
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRedisTarget - creates new Redis target.
|
2020-01-09 09:15:44 -05:00
|
|
|
func NewRedisTarget(id string, args RedisArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{}), test bool) (*RedisTarget, error) {
|
2018-03-15 16:03:41 -04:00
|
|
|
pool := &redis.Pool{
|
|
|
|
MaxIdle: 3,
|
|
|
|
IdleTimeout: 2 * 60 * time.Second,
|
|
|
|
Dial: func() (redis.Conn, error) {
|
|
|
|
conn, err := redis.Dial("tcp", args.Addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:28:28 -04:00
|
|
|
if args.Password != "" {
|
|
|
|
if _, err = conn.Do("AUTH", args.Password); err != nil {
|
|
|
|
cErr := conn.Close()
|
|
|
|
targetID := event.TargetID{ID: id, Name: "redis"}
|
|
|
|
loggerOnce(context.Background(), cErr, targetID)
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:28:28 -04:00
|
|
|
// Must be done after AUTH
|
|
|
|
if _, err = conn.Do("CLIENT", "SETNAME", "MinIO"); err != nil {
|
2019-07-23 13:22:08 -04:00
|
|
|
cErr := conn.Close()
|
|
|
|
targetID := event.TargetID{ID: id, Name: "redis"}
|
2019-10-07 01:50:24 -04:00
|
|
|
loggerOnce(context.Background(), cErr, targetID)
|
2018-03-15 16:03:41 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
},
|
|
|
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
var store Store
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
target := &RedisTarget{
|
2019-10-07 01:50:24 -04:00
|
|
|
id: event.TargetID{ID: id, Name: "redis"},
|
|
|
|
args: args,
|
|
|
|
pool: pool,
|
|
|
|
loggerOnce: loggerOnce,
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:19:25 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-redis-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
|
|
|
if oErr := store.Open(); oErr != nil {
|
|
|
|
target.loggerOnce(context.Background(), oErr, target.ID())
|
|
|
|
return target, oErr
|
|
|
|
}
|
|
|
|
target.store = store
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
conn := target.pool.Get()
|
|
|
|
defer func() {
|
|
|
|
cErr := conn.Close()
|
2019-10-07 01:50:24 -04:00
|
|
|
target.loggerOnce(context.Background(), cErr, target.ID())
|
2019-07-23 13:22:08 -04:00
|
|
|
}()
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
_, pingErr := conn.Do("PING")
|
|
|
|
if pingErr != nil {
|
2019-09-05 16:55:48 -04:00
|
|
|
if target.store == nil || !(IsConnRefusedErr(pingErr) || IsConnResetErr(pingErr)) {
|
2020-04-14 14:19:25 -04:00
|
|
|
target.loggerOnce(context.Background(), pingErr, target.ID())
|
|
|
|
return target, pingErr
|
2019-07-23 13:22:08 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := target.args.validateFormat(conn); err != nil {
|
2020-04-14 14:19:25 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
|
|
|
return target, err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
2019-07-23 13:22:08 -04:00
|
|
|
target.firstPing = true
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.store != nil && !test {
|
2019-07-23 13:22:08 -04:00
|
|
|
// Replays the events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
2019-07-23 13:22:08 -04:00
|
|
|
// Start replaying events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:22:08 -04:00
|
|
|
return target, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|