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-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
2019-07-25 14:20:24 -04:00
|
|
|
"context"
|
2018-03-15 16:03:41 -04:00
|
|
|
"encoding/json"
|
2019-07-25 14:20:24 -04:00
|
|
|
"errors"
|
2021-06-14 16:28:44 -04:00
|
|
|
"fmt"
|
2018-03-15 16:03:41 -04:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2019-07-25 14:20:24 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-15 16:03:41 -04:00
|
|
|
"sync"
|
|
|
|
|
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"
|
2023-09-04 15:57:37 -04:00
|
|
|
xnet "github.com/minio/pkg/v2/net"
|
2022-11-28 19:05:06 -05:00
|
|
|
"github.com/rabbitmq/amqp091-go"
|
2018-03-15 16:03:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AMQPArgs - AMQP target arguments.
|
|
|
|
type AMQPArgs struct {
|
2021-06-14 16:28:44 -04:00
|
|
|
Enable bool `json:"enable"`
|
|
|
|
URL xnet.URL `json:"url"`
|
|
|
|
Exchange string `json:"exchange"`
|
|
|
|
RoutingKey string `json:"routingKey"`
|
|
|
|
ExchangeType string `json:"exchangeType"`
|
|
|
|
DeliveryMode uint8 `json:"deliveryMode"`
|
|
|
|
Mandatory bool `json:"mandatory"`
|
|
|
|
Immediate bool `json:"immediate"`
|
|
|
|
Durable bool `json:"durable"`
|
|
|
|
Internal bool `json:"internal"`
|
|
|
|
NoWait bool `json:"noWait"`
|
|
|
|
AutoDeleted bool `json:"autoDeleted"`
|
|
|
|
PublisherConfirms bool `json:"publisherConfirms"`
|
|
|
|
QueueDir string `json:"queueDir"`
|
|
|
|
QueueLimit uint64 `json:"queueLimit"`
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2020-03-18 19:19:29 -04:00
|
|
|
//lint:file-ignore ST1003 We cannot change these exported names.
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// AMQP input constants.
|
|
|
|
const (
|
|
|
|
AmqpQueueDir = "queue_dir"
|
|
|
|
AmqpQueueLimit = "queue_limit"
|
|
|
|
|
|
|
|
AmqpURL = "url"
|
|
|
|
AmqpExchange = "exchange"
|
|
|
|
AmqpRoutingKey = "routing_key"
|
|
|
|
AmqpExchangeType = "exchange_type"
|
|
|
|
AmqpDeliveryMode = "delivery_mode"
|
|
|
|
AmqpMandatory = "mandatory"
|
|
|
|
AmqpImmediate = "immediate"
|
|
|
|
AmqpDurable = "durable"
|
|
|
|
AmqpInternal = "internal"
|
|
|
|
AmqpNoWait = "no_wait"
|
|
|
|
AmqpAutoDeleted = "auto_deleted"
|
|
|
|
AmqpArguments = "arguments"
|
2021-06-14 16:28:44 -04:00
|
|
|
AmqpPublisherConfirms = "publisher_confirms"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
EnvAMQPEnable = "MINIO_NOTIFY_AMQP_ENABLE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvAMQPURL = "MINIO_NOTIFY_AMQP_URL"
|
|
|
|
EnvAMQPExchange = "MINIO_NOTIFY_AMQP_EXCHANGE"
|
|
|
|
EnvAMQPRoutingKey = "MINIO_NOTIFY_AMQP_ROUTING_KEY"
|
|
|
|
EnvAMQPExchangeType = "MINIO_NOTIFY_AMQP_EXCHANGE_TYPE"
|
|
|
|
EnvAMQPDeliveryMode = "MINIO_NOTIFY_AMQP_DELIVERY_MODE"
|
|
|
|
EnvAMQPMandatory = "MINIO_NOTIFY_AMQP_MANDATORY"
|
|
|
|
EnvAMQPImmediate = "MINIO_NOTIFY_AMQP_IMMEDIATE"
|
|
|
|
EnvAMQPDurable = "MINIO_NOTIFY_AMQP_DURABLE"
|
|
|
|
EnvAMQPInternal = "MINIO_NOTIFY_AMQP_INTERNAL"
|
|
|
|
EnvAMQPNoWait = "MINIO_NOTIFY_AMQP_NO_WAIT"
|
|
|
|
EnvAMQPAutoDeleted = "MINIO_NOTIFY_AMQP_AUTO_DELETED"
|
|
|
|
EnvAMQPArguments = "MINIO_NOTIFY_AMQP_ARGUMENTS"
|
2021-06-14 16:28:44 -04:00
|
|
|
EnvAMQPPublisherConfirms = "MINIO_NOTIFY_AMQP_PUBLISHING_CONFIRMS"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvAMQPQueueDir = "MINIO_NOTIFY_AMQP_QUEUE_DIR"
|
|
|
|
EnvAMQPQueueLimit = "MINIO_NOTIFY_AMQP_QUEUE_LIMIT"
|
|
|
|
)
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
// Validate AMQP arguments
|
|
|
|
func (a *AMQPArgs) Validate() error {
|
|
|
|
if !a.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-28 19:05:06 -05:00
|
|
|
if _, err := amqp091.ParseURI(a.URL.String()); err != nil {
|
2018-07-18 14:22:29 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-25 14:20:24 -04:00
|
|
|
if a.QueueDir != "" {
|
|
|
|
if !filepath.IsAbs(a.QueueDir) {
|
|
|
|
return errors.New("queueDir path should be absolute")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:22:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// AMQPTarget - AMQP target
|
|
|
|
type AMQPTarget struct {
|
2023-05-09 00:20:31 -04:00
|
|
|
initOnce once.Init
|
2022-09-27 20:23:28 -04:00
|
|
|
|
2019-10-07 01:50:24 -04:00
|
|
|
id event.TargetID
|
|
|
|
args AMQPArgs
|
2022-11-28 19:05:06 -05:00
|
|
|
conn *amqp091.Connection
|
2019-10-07 01:50:24 -04:00
|
|
|
connMutex sync.Mutex
|
2023-04-04 13:52:24 -04:00
|
|
|
store store.Store[event.Event]
|
2022-07-27 12:44:59 -04:00
|
|
|
loggerOnce logger.LogOnce
|
2022-09-27 20:23:28 -04:00
|
|
|
|
|
|
|
quitCh chan struct{}
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID - returns TargetID.
|
|
|
|
func (target *AMQPTarget) ID() event.TargetID {
|
|
|
|
return target.id
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Name - returns the Name of the target.
|
|
|
|
func (target *AMQPTarget) Name() string {
|
|
|
|
return target.ID().String()
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// Store returns any underlying store if set.
|
|
|
|
func (target *AMQPTarget) 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 *AMQPTarget) 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 *AMQPTarget) isActive() (bool, error) {
|
2021-06-14 16:28:44 -04:00
|
|
|
ch, _, err := target.channel()
|
2019-12-11 17:27:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
ch.Close()
|
|
|
|
}()
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:05:06 -05:00
|
|
|
func (target *AMQPTarget) channel() (*amqp091.Channel, chan amqp091.Confirmation, error) {
|
2019-07-25 14:20:24 -04:00
|
|
|
var err error
|
2022-11-28 19:05:06 -05:00
|
|
|
var conn *amqp091.Connection
|
|
|
|
var ch *amqp091.Channel
|
2019-07-25 14:20:24 -04:00
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
isAMQPClosedErr := func(err error) bool {
|
2022-11-28 19:05:06 -05:00
|
|
|
if err == amqp091.ErrClosed {
|
2018-03-15 16:03:41 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if nerr, ok := err.(*net.OpError); ok {
|
|
|
|
return (nerr.Err.Error() == "use of closed network connection")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
target.connMutex.Lock()
|
|
|
|
defer target.connMutex.Unlock()
|
|
|
|
|
2019-07-25 14:20:24 -04:00
|
|
|
if target.conn != nil {
|
|
|
|
ch, err = target.conn.Channel()
|
|
|
|
if err == nil {
|
2021-06-14 16:28:44 -04:00
|
|
|
if target.args.PublisherConfirms {
|
2022-11-28 19:05:06 -05:00
|
|
|
confirms := ch.NotifyPublish(make(chan amqp091.Confirmation, 1))
|
2021-06-14 16:28:44 -04:00
|
|
|
if err := ch.Confirm(false); err != nil {
|
|
|
|
ch.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return ch, confirms, nil
|
|
|
|
}
|
|
|
|
return ch, nil, nil
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-07-25 14:20:24 -04:00
|
|
|
if !isAMQPClosedErr(err) {
|
2021-06-14 16:28:44 -04:00
|
|
|
return nil, nil, err
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
2021-06-14 16:28:44 -04:00
|
|
|
|
|
|
|
// close when we know this is a network error.
|
|
|
|
target.conn.Close()
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:05:06 -05:00
|
|
|
conn, err = amqp091.Dial(target.args.URL.String())
|
2019-07-25 14:20:24 -04:00
|
|
|
if err != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
if xnet.IsConnRefusedErr(err) {
|
|
|
|
return nil, nil, store.ErrNotConnected
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
2021-06-14 16:28:44 -04:00
|
|
|
return nil, nil, err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2019-07-25 14:20:24 -04:00
|
|
|
ch, err = conn.Channel()
|
|
|
|
if err != nil {
|
2021-06-14 16:28:44 -04:00
|
|
|
return nil, nil, err
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
target.conn = conn
|
|
|
|
|
2021-06-14 16:28:44 -04:00
|
|
|
if target.args.PublisherConfirms {
|
2022-11-28 19:05:06 -05:00
|
|
|
confirms := ch.NotifyPublish(make(chan amqp091.Confirmation, 1))
|
2021-06-14 16:28:44 -04:00
|
|
|
if err := ch.Confirm(false); err != nil {
|
|
|
|
ch.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return ch, confirms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch, nil, nil
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:05:06 -05:00
|
|
|
// send - sends an event to the AMQP091.
|
|
|
|
func (target *AMQPTarget) send(eventData event.Event, ch *amqp091.Channel, confirms chan amqp091.Confirmation) error {
|
2018-03-15 16:03:41 -04: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-03-15 16:03:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:05:06 -05:00
|
|
|
headers := make(amqp091.Table)
|
2021-08-12 01:24:19 -04:00
|
|
|
// Add more information here as required, but be aware to not overload headers
|
|
|
|
headers["minio-bucket"] = eventData.S3.Bucket.Name
|
|
|
|
headers["minio-event"] = eventData.EventName.String()
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
if err = ch.ExchangeDeclare(target.args.Exchange, target.args.ExchangeType, target.args.Durable,
|
|
|
|
target.args.AutoDeleted, target.args.Internal, target.args.NoWait, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:28:44 -04:00
|
|
|
if err = ch.Publish(target.args.Exchange, target.args.RoutingKey, target.args.Mandatory,
|
2022-11-28 19:05:06 -05:00
|
|
|
target.args.Immediate, amqp091.Publishing{
|
2021-08-12 01:24:19 -04:00
|
|
|
Headers: headers,
|
2018-03-15 16:03:41 -04:00
|
|
|
ContentType: "application/json",
|
|
|
|
DeliveryMode: target.args.DeliveryMode,
|
|
|
|
Body: data,
|
2021-06-14 16:28:44 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for publisher confirms only if its enabled
|
|
|
|
if target.args.PublisherConfirms {
|
|
|
|
confirmed := <-confirms
|
|
|
|
if !confirmed.Ack {
|
|
|
|
return fmt.Errorf("failed delivery of delivery tag: %d", confirmed.DeliveryTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save - saves the events to the store which will be replayed when the amqp connection is active.
|
|
|
|
func (target *AMQPTarget) Save(eventData event.Event) error {
|
|
|
|
if target.store != nil {
|
|
|
|
return target.store.Put(eventData)
|
|
|
|
}
|
2023-05-02 10:53:13 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-14 16:28:44 -04:00
|
|
|
ch, confirms, err := target.channel()
|
2019-07-25 14:20:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-27 12:44:59 -04:00
|
|
|
defer ch.Close()
|
2019-07-25 14:20:24 -04:00
|
|
|
|
2021-06-14 16:28:44 -04:00
|
|
|
return target.send(eventData, ch, confirms)
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
// SendFromStore - reads an event from store and sends it to AMQP091.
|
|
|
|
func (target *AMQPTarget) SendFromStore(eventKey string) error {
|
2022-09-27 20:23:28 -04:00
|
|
|
if err := target.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:28:44 -04:00
|
|
|
ch, confirms, err := target.channel()
|
2019-07-25 14:20:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-27 12:44:59 -04:00
|
|
|
defer ch.Close()
|
2019-07-25 14:20:24 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:28:44 -04:00
|
|
|
if err := target.send(eventData, ch, confirms); err != nil {
|
2019-07-25 14:20:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the event from store.
|
|
|
|
return target.store.Del(eventKey)
|
2019-04-10 08:46:01 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
// Close - does nothing and available for interface compatibility.
|
|
|
|
func (target *AMQPTarget) Close() error {
|
2022-09-27 20:23:28 -04:00
|
|
|
close(target.quitCh)
|
2020-01-09 09:15:44 -05:00
|
|
|
if target.conn != nil {
|
|
|
|
return target.conn.Close()
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
func (target *AMQPTarget) init() error {
|
2023-05-09 00:20:31 -04:00
|
|
|
return target.initOnce.Do(target.initAMQP)
|
2022-09-27 20:23:28 -04:00
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
func (target *AMQPTarget) initAMQP() error {
|
2022-11-28 19:05:06 -05:00
|
|
|
conn, err := amqp091.Dial(target.args.URL.String())
|
2019-07-25 14:20:24 -04:00
|
|
|
if err != nil {
|
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-25 14:20:24 -04:00
|
|
|
}
|
2022-09-27 20:23:28 -04:00
|
|
|
return err
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
2020-04-14 14:19:25 -04:00
|
|
|
target.conn = conn
|
2019-07-25 14:20:24 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-11 20:46:03 -04:00
|
|
|
|
2022-09-27 20:23:28 -04:00
|
|
|
// NewAMQPTarget - creates new AMQP target.
|
|
|
|
func NewAMQPTarget(id string, args AMQPArgs, loggerOnce logger.LogOnce) (*AMQPTarget, 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+"-amqp-"+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 AMQP `%s`: %w", id, err)
|
|
|
|
}
|
2019-07-25 14:20:24 -04:00
|
|
|
}
|
|
|
|
|
2022-11-07 11:01:24 -05:00
|
|
|
target := &AMQPTarget{
|
2022-09-27 20:23:28 -04:00
|
|
|
id: event.TargetID{ID: id, Name: "amqp"},
|
|
|
|
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-03-15 16:03:41 -04:00
|
|
|
}
|