Files
seaweedfs/weed/filer/filer_notification_webhooks.go
T
Chris LuandClaude Opus 5.5 95f46c8fff admin: manage filer webhook targets from the UI with live reload (#12690)
* webhook: share the default target settings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: managed webhook targets with a drainable queue

Targets kept as JSON (name, endpoint, token, filters, retry and worker
settings) build their own queue; Close waits for queued and in-flight
deliveries before stopping it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: run webhook targets stored in /etc/seaweedfs when notification.webhook.managed is set

The targets in /etc/seaweedfs/notification_webhooks.json start with the
filer and are rebuilt from the store whenever the file changes on any
filer, alongside notification.toml. The previous queue drains before it
stops. Events for the file itself, which holds bearer tokens, are not
sent to notification queues.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: manage filer webhook targets

A Webhooks page under Management lists, adds, edits and deletes the
targets kept in /etc/seaweedfs/notification_webhooks.json. Bearer
tokens are write-only: the API reports whether one is set, and an empty
token on edit keeps the stored one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep the webhook queue when the stored targets did not change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep a replaced webhook queue open until its senders hand over

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* admin: read chunk-backed webhook targets before editing them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* shell: keep the stored webhook targets out of fs.meta.notify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: reject negative managed target settings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: no signal handler on queues that reloads replace

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: swap the webhook queue without waiting behind blocked senders

The read lock held across SendMessage made a reload wait for any sender
blocked on a saturated target, and the waiting writer stalled every
other filer write with it, which is when an operator disables that
target. Close now refuses new sends and waits for the senders already
inside SendMessage, and a send that hits the closing queue retries on
its replacement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* webhook: release blocked test handlers before the server shuts down

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: always run the webhook targets managed in the admin UI

Drop the notification.webhook.managed switch: every filer loads and live
reloads the stored targets. Without stored targets no queue is created.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: place the admin-managed webhook note above enabled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: separate the admin-managed webhook note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* scaffold: note that admin-managed webhooks follow the notification queue

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retry loading webhook targets after a failed read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: retire the admin-managed webhook queue on shutdown

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:54:54 -08:00

147 lines
4.3 KiB
Go

package filer
import (
"bytes"
"context"
"errors"
"sync"
"sync/atomic"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/notification/webhook"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
const (
NotificationWebhooksConfName = "notification_webhooks.json"
notificationWebhooksDrain = 30 * time.Second
)
var notificationWebhooksRetry = 5 * time.Second
var NotificationWebhooksConfPath = string(util.NewFullPath(DirectoryEtcSeaweedFS, NotificationWebhooksConfName))
// managedWebhooks runs the webhook targets edited from the admin UI, next to
// whatever notification.toml configures.
type managedWebhooks struct {
reloadMu sync.Mutex
applied []byte
retry *time.Timer
queue atomic.Pointer[webhook.Queue]
}
func (f *Filer) LoadNotificationWebhooks() {
f.reloadNotificationWebhooks()
go func() {
<-f.deletionQuit
f.webhooks.reloadMu.Lock()
defer f.webhooks.reloadMu.Unlock()
if f.webhooks.retry != nil {
f.webhooks.retry.Stop()
}
if q := f.webhooks.queue.Swap(nil); q != nil {
go q.Close(notificationWebhooksDrain)
}
}()
}
func (f *Filer) maybeReloadNotificationWebhooks(event *filer_pb.SubscribeMetadataResponse) {
if event == nil || event.EventNotification == nil {
return
}
n := event.EventNotification
touched := n.OldEntry != nil && n.OldEntry.Name == NotificationWebhooksConfName && event.Directory == DirectoryEtcSeaweedFS
if n.NewEntry != nil && n.NewEntry.Name == NotificationWebhooksConfName && filer_pb.MetadataEventTargetDirectory(event) == DirectoryEtcSeaweedFS {
touched = true
}
if touched {
f.reloadNotificationWebhooks()
}
}
// reloadNotificationWebhooks reads the stored targets rather than the event's
// copy, so concurrent change events cannot apply an older version last.
func (f *Filer) reloadNotificationWebhooks() {
f.webhooks.reloadMu.Lock()
defer f.webhooks.reloadMu.Unlock()
select {
case <-f.deletionQuit:
return
default:
}
var content []byte
entry, err := f.FindEntry(context.Background(), util.FullPath(NotificationWebhooksConfPath))
switch {
case err == filer_pb.ErrNotFound:
case err != nil:
glog.Errorf("read %s: %v", NotificationWebhooksConfPath, err)
f.retryNotificationWebhooks()
return
case len(entry.Content) > 0:
content = entry.Content
default:
if content, err = f.readEntry(entry.GetChunks(), entry.Size()); err != nil {
glog.Errorf("read %s: %v", NotificationWebhooksConfPath, err)
f.retryNotificationWebhooks()
return
}
}
if f.webhooks.applied != nil && bytes.Equal(content, f.webhooks.applied) {
return
}
conf, err := webhook.ParseManagedConfig(content)
if err != nil {
glog.Errorf("keep current webhook targets, %s: %v", NotificationWebhooksConfPath, err)
return
}
queue, err := webhook.NewManagedQueue(conf)
if err != nil {
glog.Errorf("keep current webhook targets, start %s: %v", NotificationWebhooksConfPath, err)
return
}
glog.V(0).Infof("apply %d webhook targets from %s", len(conf.Targets), NotificationWebhooksConfPath)
f.webhooks.applied = append([]byte{}, content...)
if old := f.webhooks.queue.Swap(queue); old != nil {
go old.Close(notificationWebhooksDrain)
}
}
// retryNotificationWebhooks keeps at most one pending retry, since no later
// event replays the change whose read failed.
func (f *Filer) retryNotificationWebhooks() {
if f.webhooks.retry != nil {
f.webhooks.retry.Stop()
}
f.webhooks.retry = time.AfterFunc(notificationWebhooksRetry, f.reloadNotificationWebhooks)
}
// sendToNotificationWebhooks hands a send that raced a reload to the queue
// that replaced the closing one.
func (f *Filer) sendToNotificationWebhooks(key string, message *filer_pb.EventNotification) {
for {
q := f.webhooks.queue.Load()
if q == nil {
return
}
err := q.SendMessage(key, message)
if errors.Is(err, webhook.ErrQueueClosed) && f.webhooks.queue.Load() != q {
continue
}
if err != nil {
glog.Error(err)
}
return
}
}
// isNotificationWebhooksConf keeps the stored targets, bearer tokens
// included, out of the events sent to notification queues.
func isNotificationWebhooksConf(oldEntry, newEntry *Entry) bool {
return (oldEntry != nil && string(oldEntry.FullPath) == NotificationWebhooksConfPath) ||
(newEntry != nil && string(newEntry.FullPath) == NotificationWebhooksConfPath)
}