-
Notifications
You must be signed in to change notification settings - Fork 1
[LFXV2-1223] Event processor for indexer & access-control handlers #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mauriciozanettisalomao
merged 12 commits into
linuxfoundation:main
from
mauriciozanettisalomao:feat/lfxv2-1223-eventing-handlers-indexer-access
Mar 17, 2026
+2,555
−19
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7840bdc
feat(eventing): implement data stream processing for GroupsIO entities
mauriciozanettisalomao f2bfb19
feat(eventing): refactor data stream processing to use NATS client an…
mauriciozanettisalomao b9c9acb
feat(eventing): update environment variable names and enhance member …
mauriciozanettisalomao 4b8df54
feat(eventing): implement NATS JetStream KV-bucket event processor fo…
mauriciozanettisalomao 2b96419
feat(eventing): enhance data stream processing and add unit tests for…
mauriciozanettisalomao 5368c99
feat(eventing): add tombstone handling for member and subgroup update…
mauriciozanettisalomao b182d8e
feat(eventing): add subgroup mapping setup for subgroup delete happy …
mauriciozanettisalomao 6033090
feat(eventing): add error handling for missing parent_id in subgroup …
mauriciozanettisalomao 1389aa4
Merge branch 'main' into feat/lfxv2-1223-eventing-handlers-indexer-ac…
mauriciozanettisalomao 8d81c82
feat(mapconv): remove unused IntVal and BoolVal functions and their t…
mauriciozanettisalomao b9849c8
feat(eventing): enhance data models and handlers with additional fiel…
mauriciozanettisalomao 02bdc3a
feat(principal): implement username conversion to Auth0 "sub" format …
mauriciozanettisalomao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright The Linux Foundation and each contributor to LFX. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "strconv" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/linuxfoundation/lfx-v2-mailing-list-service/cmd/mailing-list-api/eventing" | ||
| "github.com/linuxfoundation/lfx-v2-mailing-list-service/cmd/mailing-list-api/service" | ||
| infraNATS "github.com/linuxfoundation/lfx-v2-mailing-list-service/internal/infrastructure/nats" | ||
| "github.com/linuxfoundation/lfx-v2-mailing-list-service/pkg/constants" | ||
| ) | ||
|
|
||
| // handleDataStream starts the durable JetStream consumer that processes DynamoDB KV | ||
| // change events for GroupsIO entities (service, subgroup, member). | ||
| // | ||
| // Enabled only when EVENTING_ENABLED=true. If disabled, the function | ||
| // is a no-op and returns nil. | ||
| func handleDataStream(ctx context.Context, wg *sync.WaitGroup) error { | ||
| if !dataStreamEnabled() { | ||
| slog.InfoContext(ctx, "data stream processor disabled (EVENTING_ENABLED not set to true)") | ||
| return nil | ||
| } | ||
|
|
||
| natsClient := service.GetNATSClient(ctx) | ||
|
|
||
| handler := eventing.NewEventHandler(service.MessagePublisher(ctx), service.MappingReaderWriter(ctx)) | ||
| streamConsumer := infraNATS.NewDataStreamConsumer(handler) | ||
|
|
||
| cfg := dataStreamConfig() | ||
| processor, err := eventing.NewEventProcessor(ctx, cfg, natsClient) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create data stream processor: %w", err) | ||
| } | ||
|
|
||
| slog.InfoContext(ctx, "data stream processor created", | ||
| "consumer_name", cfg.ConsumerName, | ||
| "stream_name", cfg.StreamName, | ||
| ) | ||
|
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| if err := processor.Start(ctx, streamConsumer); err != nil { | ||
| slog.ErrorContext(ctx, "data stream processor exited with error", "error", err) | ||
| } | ||
| }() | ||
|
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| <-ctx.Done() | ||
| stopCtx, cancel := context.WithTimeout(context.Background(), gracefulShutdownSeconds*time.Second) | ||
| defer cancel() | ||
| if err := processor.Stop(stopCtx); err != nil { | ||
| slog.ErrorContext(stopCtx, "error stopping data stream processor", "error", err) | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // dataStreamEnabled reports whether the data stream processor has been opted into. | ||
| func dataStreamEnabled() bool { | ||
| return os.Getenv("EVENTING_ENABLED") == "true" | ||
| } | ||
|
|
||
| // dataStreamConfig builds eventing.Config from environment variables with | ||
| // sensible defaults. | ||
| func dataStreamConfig() eventing.Config { | ||
| consumerName := os.Getenv("EVENTING_CONSUMER_NAME") | ||
| if consumerName == "" { | ||
| consumerName = "mailing-list-service-kv-consumer" | ||
| } | ||
|
|
||
| maxDeliver := envInt("EVENTING_MAX_DELIVER", 3) | ||
| maxAckPending := envInt("EVENTING_MAX_ACK_PENDING", 1000) | ||
| ackWaitSecs := envInt("EVENTING_ACK_WAIT_SECS", 30) | ||
|
|
||
| return eventing.Config{ | ||
| ConsumerName: consumerName, | ||
| StreamName: "KV_" + constants.KVBucketV1Objects, | ||
| MaxDeliver: maxDeliver, | ||
| AckWait: time.Duration(ackWaitSecs) * time.Second, | ||
| MaxAckPending: maxAckPending, | ||
| } | ||
| } | ||
|
|
||
| // envInt reads an integer environment variable, returning defaultVal if the | ||
| // variable is absent or cannot be parsed. | ||
| func envInt(key string, defaultVal int) int { | ||
| s := os.Getenv(key) | ||
| if s == "" { | ||
| return defaultVal | ||
| } | ||
| n, err := strconv.Atoi(s) | ||
| if err != nil { | ||
| return defaultVal | ||
| } | ||
| return n | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright The Linux Foundation and each contributor to LFX. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package eventing | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/linuxfoundation/lfx-v2-mailing-list-service/internal/domain/model" | ||
| "github.com/linuxfoundation/lfx-v2-mailing-list-service/internal/domain/port" | ||
| infraNATS "github.com/linuxfoundation/lfx-v2-mailing-list-service/internal/infrastructure/nats" | ||
| "github.com/nats-io/nats.go/jetstream" | ||
| ) | ||
|
|
||
| // Config holds the configuration for an EventProcessor. | ||
| type Config struct { | ||
| // ConsumerName is the durable consumer name (survives restarts). | ||
| ConsumerName string | ||
| // StreamName is the JetStream stream to consume from (e.g. "KV_v1-objects"). | ||
| StreamName string | ||
| // MaxDeliver is the maximum number of delivery attempts before giving up. | ||
| MaxDeliver int | ||
| // AckWait is how long the server waits for an ACK before redelivering. | ||
| AckWait time.Duration | ||
| // MaxAckPending is the maximum number of unacknowledged messages in flight. | ||
| MaxAckPending int | ||
| } | ||
|
|
||
| // EventProcessor is the interface for JetStream KV bucket event consumers. | ||
| // Start blocks until ctx is cancelled; Stop performs a graceful shutdown. | ||
| type EventProcessor interface { | ||
| Start(ctx context.Context, streamConsumer port.DataStreamProcessor) error | ||
| Stop(ctx context.Context) error | ||
| } | ||
|
|
||
| // natsEventProcessor is the NATS JetStream implementation of EventProcessor. | ||
| type natsEventProcessor struct { | ||
| natsClient *infraNATS.NATSClient | ||
| consumer jetstream.Consumer | ||
| consumeCtx jetstream.ConsumeContext | ||
| config Config | ||
| } | ||
|
|
||
| // NewEventProcessor creates an EventProcessor backed by the given NATSClient. | ||
| func NewEventProcessor(_ context.Context, cfg Config, natsClient *infraNATS.NATSClient) (EventProcessor, error) { | ||
| return &natsEventProcessor{ | ||
| natsClient: natsClient, | ||
| config: cfg, | ||
| }, nil | ||
| } | ||
|
|
||
| // Start creates (or resumes) the durable JetStream consumer and processes messages | ||
| // until ctx is cancelled. | ||
| func (ep *natsEventProcessor) Start(ctx context.Context, streamConsumer port.DataStreamProcessor) error { | ||
| slog.InfoContext(ctx, "starting data stream processor", "consumer_name", ep.config.ConsumerName) | ||
|
|
||
| consumer, err := ep.natsClient.CreateOrUpdateConsumer(ctx, ep.config.StreamName, jetstream.ConsumerConfig{ | ||
| Name: ep.config.ConsumerName, | ||
| Durable: ep.config.ConsumerName, | ||
| // DeliverLastPerSubjectPolicy resumes from the last seen record per KV key after a | ||
| // restart, avoiding a full replay while ensuring no in-flight event is dropped. | ||
| DeliverPolicy: jetstream.DeliverLastPerSubjectPolicy, | ||
| AckPolicy: jetstream.AckExplicitPolicy, | ||
| FilterSubjects: []string{ | ||
| "$KV.v1-objects.itx-groupsio-v2-service.>", | ||
| "$KV.v1-objects.itx-groupsio-v2-subgroup.>", | ||
| "$KV.v1-objects.itx-groupsio-v2-member.>", | ||
| }, | ||
| MaxDeliver: ep.config.MaxDeliver, | ||
| AckWait: ep.config.AckWait, | ||
| MaxAckPending: ep.config.MaxAckPending, | ||
| Description: "Durable KV watcher for mailing-list-service GroupsIO entities", | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create or update consumer: %w", err) | ||
| } | ||
| ep.consumer = consumer | ||
|
|
||
| consumeCtx, err := consumer.Consume( | ||
| func(jMsg jetstream.Msg) { | ||
| meta, err := jMsg.Metadata() | ||
| if err != nil { | ||
| slog.ErrorContext(ctx, "failed to read stream message metadata, ACKing to avoid poison pill", | ||
| "subject", jMsg.Subject(), "error", err) | ||
| _ = jMsg.Ack() | ||
| return | ||
| } | ||
| streamConsumer.Process(ctx, model.StreamMessage{ | ||
| Key: kvKey(jMsg.Subject()), | ||
| Data: jMsg.Data(), | ||
| IsRemoval: isKVRemoval(jMsg), | ||
| DeliveryCount: meta.NumDelivered, | ||
| Ack: jMsg.Ack, | ||
| Nak: jMsg.NakWithDelay, | ||
| }) | ||
| }, | ||
| jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { | ||
| slog.With("error", err).Error("data stream KV consumer error") | ||
| }), | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to start consuming messages: %w", err) | ||
| } | ||
| ep.consumeCtx = consumeCtx | ||
|
|
||
| slog.InfoContext(ctx, "data stream processor started successfully") | ||
| <-ctx.Done() | ||
| slog.InfoContext(ctx, "data stream processor context cancelled") | ||
| return nil | ||
| } | ||
|
|
||
| // Stop halts the JetStream consumer. The NATS connection lifecycle is managed | ||
| // by the caller (NATSClient). | ||
| func (ep *natsEventProcessor) Stop(ctx context.Context) error { | ||
| slog.InfoContext(ctx, "stopping data stream processor") | ||
|
|
||
| if ep.consumeCtx != nil { | ||
| ep.consumeCtx.Stop() | ||
| slog.InfoContext(ctx, "data stream consumer stopped") | ||
| } | ||
|
|
||
| slog.InfoContext(ctx, "data stream processor stopped") | ||
| return nil | ||
| } | ||
|
|
||
| // kvKey strips the "$KV.<bucket>." prefix from a JetStream KV subject, | ||
| // returning the bare key. Subject format: $KV.<bucket>.<key> | ||
| func kvKey(subject string) string { | ||
| idx := strings.Index(subject, ".") | ||
| if idx == -1 { | ||
| return subject | ||
| } | ||
| idx2 := strings.Index(subject[idx+1:], ".") | ||
| if idx2 == -1 { | ||
| return subject | ||
| } | ||
| return subject[idx+idx2+2:] | ||
| } | ||
|
|
||
| func isKVRemoval(msg jetstream.Msg) bool { | ||
| op := msg.Headers().Get("Kv-Operation") | ||
| return op == "DEL" || op == "PURGE" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.