2018-11-07 19:23:13 +01:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-11-07 19:23:13 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
2019-10-12 02:46:03 +02:00
|
|
|
"context"
|
2018-11-07 19:23:13 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
2019-07-12 11:41:57 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-07 19:23:13 +01:00
|
|
|
|
|
|
|
"github.com/nsqio/go-nsq"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
|
|
|
)
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
// NSQ constants
|
|
|
|
const (
|
|
|
|
NSQAddress = "nsqd_address"
|
|
|
|
NSQTopic = "topic"
|
2019-11-14 02:38:05 +01:00
|
|
|
NSQTLS = "tls"
|
2019-10-23 07:59:13 +02:00
|
|
|
NSQTLSSkipVerify = "tls_skip_verify"
|
|
|
|
NSQQueueDir = "queue_dir"
|
|
|
|
NSQQueueLimit = "queue_limit"
|
|
|
|
|
2019-12-05 00:32:37 +01:00
|
|
|
EnvNSQEnable = "MINIO_NOTIFY_NSQ"
|
2019-10-23 07:59:13 +02:00
|
|
|
EnvNSQAddress = "MINIO_NOTIFY_NSQ_NSQD_ADDRESS"
|
|
|
|
EnvNSQTopic = "MINIO_NOTIFY_NSQ_TOPIC"
|
2019-11-14 02:38:05 +01:00
|
|
|
EnvNSQTLS = "MINIO_NOTIFY_NSQ_TLS"
|
2019-10-23 07:59:13 +02:00
|
|
|
EnvNSQTLSSkipVerify = "MINIO_NOTIFY_NSQ_TLS_SKIP_VERIFY"
|
|
|
|
EnvNSQQueueDir = "MINIO_NOTIFY_NSQ_QUEUE_DIR"
|
|
|
|
EnvNSQQueueLimit = "MINIO_NOTIFY_NSQ_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-11-07 19:23:13 +01: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 11:41:57 +02:00
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-11-07 19:23:13 +01: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 11:41:57 +02:00
|
|
|
if n.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(n.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 19:23:13 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NSQTarget - NSQ target.
|
|
|
|
type NSQTarget struct {
|
2020-04-14 20:19:25 +02:00
|
|
|
id event.TargetID
|
|
|
|
args NSQArgs
|
|
|
|
producer *nsq.Producer
|
|
|
|
store Store
|
|
|
|
config *nsq.Config
|
|
|
|
loggerOnce func(ctx context.Context, err error, id interface{}, errKind ...interface{})
|
2018-11-07 19:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns target ID.
|
|
|
|
func (target *NSQTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:38:32 +02:00
|
|
|
// HasQueueStore - Checks if the queueStore has been configured for the target
|
|
|
|
func (target *NSQTarget) HasQueueStore() bool {
|
|
|
|
return target.store != nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:27:03 +01:00
|
|
|
// IsActive - Return true if target is up and active
|
|
|
|
func (target *NSQTarget) IsActive() (bool, error) {
|
2020-05-13 15:37:22 +02:00
|
|
|
if target.producer == nil {
|
2020-04-14 20:19:25 +02:00
|
|
|
producer, err := nsq.NewProducer(target.args.NSQDAddress.String(), target.config)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
target.producer = producer
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:27:03 +01: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 11:41:57 +02:00
|
|
|
// Save - saves the events to the store which will be replayed when the nsq connection is active.
|
2019-04-10 14:46:01 +02:00
|
|
|
func (target *NSQTarget) Save(eventData event.Event) error {
|
2019-07-12 11:41:57 +02:00
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
2019-12-11 23:27:03 +01:00
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
2019-07-12 11:41:57 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-04-10 14:46:01 +02:00
|
|
|
return target.send(eventData)
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:41:57 +02:00
|
|
|
// send - sends an event to the NSQ.
|
|
|
|
func (target *NSQTarget) send(eventData event.Event) error {
|
2018-11-07 19:23:13 +01:00
|
|
|
objectName, err := url.QueryUnescape(eventData.S3.Object.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := eventData.S3.Bucket.Name + "/" + objectName
|
|
|
|
|
2018-11-14 19:23:44 +01:00
|
|
|
data, err := json.Marshal(event.Log{EventName: eventData.EventName, Key: key, Records: []event.Event{eventData}})
|
2018-11-07 19:23:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:41:57 +02:00
|
|
|
return target.producer.Publish(target.args.Topic, data)
|
2018-11-07 19:23:13 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 11:41:57 +02:00
|
|
|
// Send - reads an event from store and sends it to NSQ.
|
2019-04-10 14:46:01 +02:00
|
|
|
func (target *NSQTarget) Send(eventKey string) error {
|
2019-12-11 23:27:03 +01:00
|
|
|
_, err := target.IsActive()
|
|
|
|
if err != nil {
|
2019-07-12 11:41:57 +02: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 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:23:13 +01:00
|
|
|
// Close - closes underneath connections to NSQD server.
|
|
|
|
func (target *NSQTarget) Close() (err error) {
|
2020-01-09 15:15:44 +01:00
|
|
|
if target.producer != nil {
|
|
|
|
// this blocks until complete:
|
|
|
|
target.producer.Stop()
|
|
|
|
}
|
2018-11-07 19:23:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNSQTarget - creates new NSQ target.
|
2020-01-09 15:15:44 +01:00
|
|
|
func NewNSQTarget(id string, args NSQArgs, doneCh <-chan struct{}, loggerOnce func(ctx context.Context, err error, id interface{}, kind ...interface{}), test bool) (*NSQTarget, error) {
|
2018-11-07 19:23:13 +01:00
|
|
|
config := nsq.NewConfig()
|
|
|
|
if args.TLS.Enable {
|
|
|
|
config.TlsV1 = true
|
|
|
|
config.TlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: args.TLS.SkipVerify,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:41:57 +02:00
|
|
|
var store Store
|
|
|
|
|
2020-04-14 20:19:25 +02:00
|
|
|
target := &NSQTarget{
|
|
|
|
id: event.TargetID{ID: id, Name: "nsq"},
|
|
|
|
args: args,
|
|
|
|
config: config,
|
|
|
|
loggerOnce: loggerOnce,
|
|
|
|
}
|
|
|
|
|
2019-07-12 11:41:57 +02:00
|
|
|
if args.QueueDir != "" {
|
|
|
|
queueDir := filepath.Join(args.QueueDir, storePrefix+"-nsq-"+id)
|
|
|
|
store = NewQueueStore(queueDir, args.QueueLimit)
|
|
|
|
if oErr := store.Open(); oErr != nil {
|
2020-04-14 20:19:25 +02:00
|
|
|
target.loggerOnce(context.Background(), oErr, target.ID())
|
2019-07-12 11:41:57 +02:00
|
|
|
return nil, oErr
|
|
|
|
}
|
2020-04-14 20:19:25 +02:00
|
|
|
target.store = store
|
2019-07-12 11:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
producer, err := nsq.NewProducer(args.NSQDAddress.String(), config)
|
2018-11-07 19:23:13 +01:00
|
|
|
if err != nil {
|
2020-04-14 20:19:25 +02:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
|
|
|
return target, err
|
2019-07-12 11:41:57 +02:00
|
|
|
}
|
2020-04-14 20:19:25 +02:00
|
|
|
target.producer = producer
|
2019-07-12 11:41:57 +02:00
|
|
|
|
|
|
|
if err := target.producer.Ping(); err != nil {
|
|
|
|
// To treat "connection refused" errors as errNotConnected.
|
2019-09-05 22:55:48 +02:00
|
|
|
if target.store == nil || !(IsConnRefusedErr(err) || IsConnResetErr(err)) {
|
2020-04-14 20:19:25 +02:00
|
|
|
target.loggerOnce(context.Background(), err, target.ID())
|
2019-07-12 11:41:57 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 15:15:44 +01:00
|
|
|
if target.store != nil && !test {
|
2019-07-12 11:41:57 +02:00
|
|
|
// Replays the events from the store.
|
2020-04-14 20:19:25 +02:00
|
|
|
eventKeyCh := replayEvents(target.store, doneCh, target.loggerOnce, target.ID())
|
2019-07-12 11:41:57 +02:00
|
|
|
// Start replaying events from the store.
|
2020-04-14 20:19:25 +02:00
|
|
|
go sendEvents(target, eventKeyCh, doneCh, target.loggerOnce)
|
2019-07-12 11:41:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
2018-11-07 19:23:13 +01:00
|
|
|
}
|