2023-05-02 10:53:13 -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/>.
|
2018-11-07 13:23:13 -05:00
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
2019-10-11 20:46:03 -04:00
|
|
|
"context"
|
2018-11-07 13:23:13 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-09-27 20:23:28 -04:00
|
|
|
"fmt"
|
2018-11-07 13:23:13 -05:00
|
|
|
"net/url"
|
2019-07-12 05:41:57 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-07 13:23:13 -05:00
|
|
|
|
|
|
|
"github.com/nsqio/go-nsq"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/event"
|
2022-07-27 12:44:59 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2023-05-09 00:20:31 -04:00
|
|
|
"github.com/minio/minio/internal/once"
|
2023-04-04 13:52:24 -04:00
|
|
|
"github.com/minio/minio/internal/store"
|
2021-06-14 17:54:37 -04:00
|
|
|
xnet "github.com/minio/pkg/net"
|
2018-11-07 13:23:13 -05:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// NSQ constants
|
|
|
|
const (
|
|
|
|
NSQAddress = "nsqd_address"
|
|
|
|
NSQTopic = "topic"
|
2019-11-13 20:38:05 -05:00
|
|
|
NSQTLS = "tls"
|
2019-10-23 01:59:13 -04:00
|
|
|
NSQTLSSkipVerify = "tls_skip_verify"
|
|
|
|
NSQQueueDir = "queue_dir"
|
|
|
|
NSQQueueLimit = "queue_limit"
|
|
|
|
|
2020-07-23 15:19:36 -04:00
|
|
|
EnvNSQEnable = "MINIO_NOTIFY_NSQ_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvNSQAddress = "MINIO_NOTIFY_NSQ_NSQD_ADDRESS"
|
|
|
|
EnvNSQTopic = "MINIO_NOTIFY_NSQ_TOPIC"
|
2019-11-13 20:38:05 -05:00
|
|
|
EnvNSQTLS = "MINIO_NOTIFY_NSQ_TLS"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvNSQTLSSkipVerify = "MINIO_NOTIFY_NSQ_TLS_SKIP_VERIFY"
|
|
|
|
EnvNSQQueueDir = "MINIO_NOTIFY_NSQ_QUEUE_DIR"
|
|
|
|
EnvNSQQueueLimit = "MINIO_NOTIFY_NSQ_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-11-07 13:23:13 -05:00
|
|
|
// NSQArgs - NSQ target arguments.
|
|
|
|
type NSQArgs struct {
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
NSQDAddress xnet.Host `json:"nsqdAddress"`
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
TLS struct {
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
SkipVerify bool `json:"skipVerify"`
|
|
|
|
} `json:"tls"`
|
2019-07-12 05:41:57 -04:00
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate NSQArgs fields
|
|
|
|
func (n NSQArgs) Validate() error {
|
|
|
|
if !n.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.NSQDAddress.IsEmpty() {
|
|
|
|
return errors.New("empty nsqdAddress")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Topic == "" {
|
|
|
|
return errors.New("empty topic")
|
|
|
|
}
|
2019-07-12 05:41:57 -04:00
|
|
|
if n.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(n.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 13:23:13 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NSQTarget - NSQ target.
|
|
|
|
type NSQTarget struct {
|
2023-05-09 00:20:31 -04:00
|
|
|
initOnce once.Init
|
2022-09-27 20:23:28 -04:00
|
|
|
|
2020-04-14 14:19:25 -04:00
|
|
|
id event.TargetID
|
|
|
|
args NSQArgs
|
|
|
|
producer *nsq.Producer
|
2023-04-04 13:52:24 -04:00
|
|
|
store store.Store[event.Event]
|
2020-04-14 14:19:25 -04:00
|
|
|
config *nsq.Config
|
2022-07-27 12:44:59 -04:00
|
|
|
loggerOnce logger.LogOnce
|
2022-09-27 20:23:28 -04:00
|
|
|
quitCh chan struct{}
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *NSQTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Name - returns the Name of the target.
|
|
|
|
func (target *NSQTarget) Name() string {
|
|
|
|
return target.ID().String()
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Store returns any underlying store if set.
|
|
|
|
func (target *NSQTarget) Store() event.TargetStore {
|
|
|
|
return target.store
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *NSQTarget) IsActive() (bool, error) {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return target.isActive()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (target *NSQTarget) isActive() (bool, error) {
|
2020-05-13 09:37:22 -04:00
|
|
|
if target.producer == nil {
|
2020-04-14 14:19:25 -04:00
|
|
|
producer, err := nsq.NewProducer(target.args.NSQDAddress.String(), target.config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
target.producer = producer
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:27:03 -05:00
|
|
|
if err := target.producer.Ping(); err != nil {
|
|
|
|
// To treat "connection refused" errors as errNotConnected.
|
2023-04-04 13:52:24 -04:00
|
|
|
if xnet.IsConnRefusedErr(err) {
|
|
|
|
return false, store.ErrNotConnected
|
2019-12-11 17:27:03 -05:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
// Save - saves the events to the store which will be replayed when the nsq connection is active.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *NSQTarget) Save(eventData event.Event) error {
|
2023-05-02 10:53:13 -04:00
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := target.isActive()
|
2019-12-11 17:27:03 -05:00
|
|
|
if err != nil {
|
2019-07-12 05:41:57 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-04-10 08:46:01 -04:00
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
// send - sends an event to the NSQ.
|
|
|
|
func (target *NSQTarget) send(eventData event.Event) error {
|
2018-11-07 13:23:13 -05:00
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
2018-11-14 13:23:44 -05:00
|
|
|
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
2018-11-07 13:23:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
return target.producer.Publish(target.args.Topic, data)
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
// SendFromStore - reads an event from store and sends it to NSQ.
|
|
|
|
func (target *NSQTarget) SendFromStore(eventKey string) error {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := target.isActive()
|
2019-12-11 17:27:03 -05:00
|
|
|
if err != nil {
|
2019-07-12 05:41:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 wouldve been already been sent successfully.
|
|
|
|
if os.IsNotExist(eErr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := target.send(eventData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the event from store.
|
|
|
|
return target.store.Del(eventKey)
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2018-11-07 13:23:13 -05:00
|
|
|
// Close - closes underneath connections to NSQD server.
|
|
|
|
func (target *NSQTarget) Close() (err error) {
|
2022-09-27 20:23:28 -04:00
|
|
|
close(target.quitCh)
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.producer != nil {
|
|
|
|
// this blocks until complete:
|
|
|
|
target.producer.Stop()
|
|
|
|
}
|
2018-11-07 13:23:13 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
func (target *NSQTarget) init() error {
|
2023-05-09 00:20:31 -04:00
|
|
|
return target.initOnce.Do(target.initNSQ)
|
2022-09-27 20:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (target *NSQTarget) initNSQ() error {
|
|
|
|
args := target.args
|
|
|
|
|
2018-11-07 13:23:13 -05:00
|
|
|
config := nsq.NewConfig()
|
|
|
|
if args.TLS.Enable {
|
|
|
|
config.TlsV1 = true
|
|
|
|
config.TlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: args.TLS.SkipVerify,
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 20:23:28 -04:00
|
|
|
target.config = config
|
2019-07-12 05:41:57 -04:00
|
|
|
|
|
|
|
producer, err := nsq.NewProducer(args.NSQDAddress.String(), config)
|
2018-11-07 13:23:13 -05:00
|
|
|
if err != nil {
|
2022-07-27 12:44:59 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID().String())
|
2022-09-27 20:23:28 -04:00
|
|
|
return err
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
target.producer = producer
|
2019-07-12 05:41:57 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
err = target.producer.Ping()
|
|
|
|
if err != nil {
|
2019-07-12 05:41:57 -04:00
|
|
|
// To treat "connection refused" errors as errNotConnected.
|
2023-04-04 13:52:24 -04:00
|
|
|
if !(xnet.IsConnRefusedErr(err) || xnet.IsConnResetErr(err)) {
|
2022-07-27 12:44:59 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID().String())
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
2022-09-27 20:23:28 -04:00
|
|
|
target.producer.Stop()
|
|
|
|
return err
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
yes, err := target.isActive()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !yes {
|
2023-04-04 13:52:24 -04:00
|
|
|
return store.ErrNotConnected
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNSQTarget - creates new NSQ target.
|
|
|
|
func NewNSQTarget(id string, args NSQArgs, loggerOnce logger.LogOnce) (*NSQTarget, error) {
|
2023-04-04 13:52:24 -04:00
|
|
|
var queueStore store.Store[event.Event]
|
2022-09-27 20:23:28 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nsq-"+id)
|
2023-04-04 13:52:24 -04:00
|
|
|
queueStore = store.NewQueueStore[event.Event](queueDir, args.QueueLimit, event.StoreExtension)
|
|
|
|
if err := queueStore.Open(); err != nil {
|
2022-09-27 20:23:28 -04:00
|
|
|
return nil, fmt.Errorf("unable to initialize the queue store of NSQ `%s`: %w", id, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:01:24 -05:00
|
|
|
target := &NSQTarget{
|
2022-09-27 20:23:28 -04:00
|
|
|
id: event.TargetID{ID: id, Name: "nsq"},
|
|
|
|
args: args,
|
|
|
|
loggerOnce: loggerOnce,
|
2023-04-04 13:52:24 -04:00
|
|
|
store: queueStore,
|
2022-09-27 20:23:28 -04:00
|
|
|
quitCh: make(chan struct{}),
|
2022-11-07 11:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if target.store != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
store.StreamItems(target.store, target, target.quitCh, target.loggerOnce)
|
2022-11-07 11:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|