package s3api import ( "context" "errors" "fmt" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3notification" ) // notificationTargetRegistry returns the current target registry; nil has no targets. func (s3a *S3ApiServer) notificationTargetRegistry() *s3notification.Registry { return s3a.notificationTargets.Load() } // loadNotificationTargets reads the target registry once at startup; the // metadata subscription keeps it current from then on. func (s3a *S3ApiServer) loadNotificationTargets() { err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { resp, err := filer_pb.LookupEntry(context.Background(), client, &filer_pb.LookupDirectoryEntryRequest{ Directory: s3notification.TargetsDirectory, Name: s3notification.TargetsFile, }) if errors.Is(err, filer_pb.ErrNotFound) { return nil } if err != nil { return err } return s3a.loadNotificationTargetsFromEntry(resp.Entry) }) if err != nil { glog.Warningf("load notification targets: %v", err) } } // loadNotificationTargetsFromEntry loads the registry from the file's inline // content. A file stored in chunks is refused rather than read as empty, which // would drop every target. func (s3a *S3ApiServer) loadNotificationTargetsFromEntry(entry *filer_pb.Entry) error { if len(entry.Content) == 0 && len(entry.GetChunks()) > 0 { return fmt.Errorf("%s/%s is stored in chunks; write it through the filer HTTP API so it is kept inline", s3notification.TargetsDirectory, s3notification.TargetsFile) } return s3a.loadNotificationTargetsFromBytes(entry.Content) } func (s3a *S3ApiServer) loadNotificationTargetsFromBytes(content []byte) error { reg, err := s3notification.ParseTargets(content) if err != nil { return err } s3a.notificationTargets.Store(reg) s3a.syncNotificationPublisher(reg) glog.V(1).Infof("loaded %d notification targets", len(reg.Names())) return nil } // onNotificationTargetsChange reloads the target registry when its file is // written, and empties it when the file is deleted or renamed away. A file // that does not parse leaves the previous registry in place. func (s3a *S3ApiServer) onNotificationTargetsChange(dir string, oldEntry *filer_pb.Entry, newEntry *filer_pb.Entry) { if dir != s3notification.TargetsDirectory { return } if newEntry != nil && newEntry.Name == s3notification.TargetsFile { if err := s3a.loadNotificationTargetsFromEntry(newEntry); err != nil { glog.Warningf("keeping the previous notification targets: %v", err) } return } if oldEntry != nil && oldEntry.Name == s3notification.TargetsFile { s3a.notificationTargets.Store(nil) s3a.syncNotificationPublisher(nil) glog.V(1).Infof("notification targets removed") } }