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-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"
|
|
|
|
"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"
|
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 {
|
2020-04-14 14:19:25 -04:00
|
|
|
id event.TargetID
|
|
|
|
args NSQArgs
|
|
|
|
producer *nsq.Producer
|
|
|
|
store Store
|
|
|
|
config *nsq.Config
|
2022-07-27 12:44:59 -04:00
|
|
|
loggerOnce logger.LogOnce
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *NSQTarget) 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 *NSQTarget) 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 *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.
|
|
|
|
if IsConnRefusedErr(err) {
|
|
|
|
return false, errNotConnected
|
|
|
|
}
|
|
|
|
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 {
|
2019-07-12 05:41:57 -04:00
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
2019-12-11 17:27:03 -05:00
|
|
|
_, err := target.IsActive()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
// Send - reads an event from store and sends it to NSQ.
|
2019-04-10 08:46:01 -04:00
|
|
|
func (target *NSQTarget) Send(eventKey string) error {
|
2019-12-11 17:27:03 -05:00
|
|
|
_, err := target.IsActive()
|
|
|
|
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) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNSQTarget - creates new NSQ target.
|
2022-07-27 12:44:59 -04:00
|
|
|
func NewNSQTarget(id string, args NSQArgs, doneCh <-chan struct{}, loggerOnce logger.LogOnce, test bool) (*NSQTarget, error) {
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
var store Store
|
|
|
|
|
2020-04-14 14:19:25 -04:00
|
|
|
target := &NSQTarget{
|
|
|
|
id: event.TargetID{ID: id, Name: "nsq"},
|
|
|
|
args: args,
|
|
|
|
config: config,
|
|
|
|
loggerOnce: loggerOnce,
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:41:57 -04:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nsq-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
|
|
|
if oErr := store.Open(); oErr != nil {
|
2022-07-27 12:44:59 -04:00
|
|
|
target.loggerOnce(context.Background(), oErr, target.ID().String())
|
2020-07-23 15:19:36 -04:00
|
|
|
return target, oErr
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
target.store = store
|
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())
|
2020-04-14 14:19:25 -04:00
|
|
|
return target, 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
|
|
|
|
|
|
|
if err := target.producer.Ping(); err != nil {
|
|
|
|
// To treat "connection refused" errors as errNotConnected.
|
2019-09-05 16:55:48 -04:00
|
|
|
if target.store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
2022-07-27 12:44:59 -04:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID().String())
|
2020-07-23 15:19:36 -04:00
|
|
|
return target, err
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.store != nil && !test {
|
2019-07-12 05:41:57 -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-12 05:41:57 -04:00
|
|
|
// Start replaying events from the store.
|
2020-04-14 14:19:25 -04:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
2019-07-12 05:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|