@@ -22,6 +22,8 @@ import (
2222 "github.com/riverqueue/river/rivershared/util/testutil"
2323)
2424
25+ const DomainDefault = "default"
26+
2527const (
2628 electIntervalDefault = 5 * time .Second
2729 electIntervalJitterDefault = 1 * time .Second
@@ -82,6 +84,7 @@ func (ts *electorTestSignals) Init(tb testutil.TestingTB) {
8284
8385type Config struct {
8486 ClientID string
87+ Domain string
8588 ElectInterval time.Duration // period on which each elector attempts elect even without having received a resignation notification
8689 ElectIntervalJitter time.Duration
8790 Schema string
@@ -121,6 +124,7 @@ func NewElector(archetype *baseservice.Archetype, exec riverdriver.Executor, not
121124 return baseservice .Init (archetype , & Elector {
122125 config : (& Config {
123126 ClientID : config .ClientID ,
127+ Domain : cmp .Or (config .Domain , string (DomainDefault )),
124128 ElectInterval : cmp .Or (config .ElectInterval , electIntervalDefault ),
125129 ElectIntervalJitter : cmp .Or (config .ElectIntervalJitter , electIntervalJitterDefault ),
126130 Schema : config .Schema ,
@@ -143,9 +147,9 @@ func (e *Elector) Start(ctx context.Context) error {
143147
144148 var sub * notifier.Subscription
145149 if e .notifier == nil {
146- e .Logger .DebugContext (ctx , e .Name + ": No notifier configured; starting in poll mode " , "client_id" , e .config .ClientID )
150+ e .Logger .DebugContext (ctx , e .Name + ": Resigned leadership successfully " , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
147151 } else {
148- e .Logger .DebugContext (ctx , e .Name + ": Listening for leadership changes " , "client_id" , e .config .ClientID , "topic" , notifier .NotificationTopicLeadership )
152+ e .Logger .DebugContext (ctx , e .Name + ": Resigned leadership successfully " , "client_id" , e .config .ClientID , "domain" , e . config . Domain , "topic" , notifier .NotificationTopicLeadership )
149153 var err error
150154 sub , err = e .notifier .Listen (ctx , notifier .NotificationTopicLeadership , func (topic notifier.NotificationTopic , payload string ) {
151155 e .handleLeadershipNotification (ctx , topic , payload )
@@ -180,7 +184,7 @@ func (e *Elector) Start(ctx context.Context) error {
180184 return
181185 }
182186
183- e .Logger .DebugContext (ctx , e .Name + ": Gained leadership" , "client_id" , e .config .ClientID )
187+ e .Logger .DebugContext (ctx , e .Name + ": Gained leadership" , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
184188 e .testSignals .GainedLeadership .Signal (struct {}{})
185189
186190 err := e .keepLeadershipLoop (ctx )
@@ -193,7 +197,7 @@ func (e *Elector) Start(ctx context.Context) error {
193197 continue // lost leadership reelection; unusual but not a problem; don't log
194198 }
195199
196- e .Logger .ErrorContext (ctx , e .Name + ": Error keeping leadership" , "client_id" , e .config .ClientID , "err" , err )
200+ e .Logger .ErrorContext (ctx , e .Name + ": Error keeping leadership" , "client_id" , e .config .ClientID , "domain" , e . config . Domain , " err" , err )
197201 }
198202 }
199203 }()
@@ -205,10 +209,11 @@ func (e *Elector) attemptGainLeadershipLoop(ctx context.Context) error {
205209 var attempt int
206210 for {
207211 attempt ++
208- e .Logger .DebugContext (ctx , e .Name + ": Attempting to gain leadership" , "client_id" , e .config .ClientID )
212+ e .Logger .DebugContext (ctx , e .Name + ": Attempting to gain leadership" , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
209213
210214 elected , err := attemptElectOrReelect (ctx , e .exec , false , & riverdriver.LeaderElectParams {
211215 LeaderID : e .config .ClientID ,
216+ Name : e .config .Domain ,
212217 Now : e .Time .NowUTCOrNil (),
213218 Schema : e .config .Schema ,
214219 TTL : e .leaderTTL (),
@@ -229,7 +234,7 @@ func (e *Elector) attemptGainLeadershipLoop(ctx context.Context) error {
229234
230235 attempt = 0
231236
232- e .Logger .DebugContext (ctx , e .Name + ": Leadership bid was unsuccessful (not an error)" , "client_id" , e .config .ClientID )
237+ e .Logger .DebugContext (ctx , e .Name + ": Leadership bid was unsuccessful (not an error)" , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
233238 e .testSignals .DeniedLeadership .Signal (struct {}{})
234239
235240 select {
@@ -254,17 +259,17 @@ func (e *Elector) attemptGainLeadershipLoop(ctx context.Context) error {
254259func (e * Elector ) handleLeadershipNotification (ctx context.Context , topic notifier.NotificationTopic , payload string ) {
255260 if topic != notifier .NotificationTopicLeadership {
256261 // This should not happen unless the notifier is broken.
257- e .Logger .ErrorContext (ctx , e .Name + ": Received unexpected notification" , "client_id" , e .config .ClientID , "topic" , topic , "payload" , payload )
262+ e .Logger .ErrorContext (ctx , e .Name + ": Received unexpected notification" , "client_id" , e .config .ClientID , "domain" , e . config . Domain , " topic" , topic , "payload" , payload )
258263 return
259264 }
260265
261266 notification := DBNotification {}
262267 if err := json .Unmarshal ([]byte (payload ), & notification ); err != nil {
263- e .Logger .ErrorContext (ctx , e .Name + ": Unable to unmarshal leadership notification" , "client_id" , e .config .ClientID , "err" , err )
268+ e .Logger .ErrorContext (ctx , e .Name + ": Unable to unmarshal leadership notification" , "client_id" , e .config .ClientID , "domain" , e . config . Domain , " err" , err )
264269 return
265270 }
266271
267- e .Logger .DebugContext (ctx , e .Name + ": Received notification from notifier" , "action" , notification .Action , "client_id" , e .config .ClientID )
272+ e .Logger .DebugContext (ctx , e .Name + ": Received notification from notifier" , "action" , notification .Action , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
268273
269274 // Do an initial context check so in case context is done, it always takes
270275 // precedence over sending a leadership notification.
@@ -359,7 +364,7 @@ func (e *Elector) keepLeadershipLoop(ctx context.Context) error {
359364 case <- e .requestResignChan :
360365 // Receive a notification telling current leader to resign.
361366
362- e .Logger .InfoContext (ctx , e .Name + ": Current leader received forced resignation" , "client_id" , e .config .ClientID )
367+ e .Logger .InfoContext (ctx , e .Name + ": Current leader received forced resignation" , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
363368
364369 if ! timer .Stop () {
365370 <- timer .C
@@ -383,10 +388,11 @@ func (e *Elector) keepLeadershipLoop(ctx context.Context) error {
383388 // Reelect timer expired; attempt reelection below.
384389 }
385390
386- e .Logger .DebugContext (ctx , e .Name + ": Current leader attempting reelect " , "client_id" , e .config .ClientID )
391+ e .Logger .InfoContext (ctx , e .Name + ": Current leader received forced resignation " , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
387392
388393 reelected , err := attemptElectOrReelect (ctx , e .exec , true , & riverdriver.LeaderElectParams {
389394 LeaderID : e .config .ClientID ,
395+ Name : e .config .Domain ,
390396 Now : e .Time .NowUTCOrNil (),
391397 Schema : e .config .Schema ,
392398 TTL : e .leaderTTL (),
@@ -424,7 +430,7 @@ func (e *Elector) keepLeadershipLoop(ctx context.Context) error {
424430// always surrendered in a timely manner so it can be picked up quickly by
425431// another client, even in the event of a cancellation.
426432func (e * Elector ) attemptResignLoop (ctx context.Context ) {
427- e .Logger .DebugContext (ctx , e .Name + ": Attempting to resign leadership " , "client_id" , e .config .ClientID )
433+ e .Logger .InfoContext (ctx , e .Name + ": Current leader received forced resignation " , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
428434
429435 // Make a good faith attempt to resign, even in the presence of errors, but
430436 // don't keep hammering if it doesn't work. In case a resignation failure,
@@ -469,7 +475,7 @@ func (e *Elector) attemptResign(ctx context.Context, attempt int) error {
469475 }
470476
471477 if resigned {
472- e .Logger .DebugContext (ctx , e .Name + ": Resigned leadership successfully" , "client_id" , e .config .ClientID )
478+ e .Logger .DebugContext (ctx , e .Name + ": Resigned leadership successfully" , "client_id" , e .config .ClientID , "domain" , e . config . Domain )
473479 e .testSignals .ResignedLeadership .Signal (struct {}{})
474480 }
475481
@@ -484,6 +490,7 @@ func (e *Elector) errorSlogArgs(err error, attempt int, sleepDuration time.Durat
484490 return []any {
485491 slog .Int ("attempt" , attempt ),
486492 slog .String ("client_id" , e .config .ClientID ),
493+ slog .String ("domain" , e .config .Domain ),
487494 slog .String ("err" , err .Error ()),
488495 slog .String ("sleep_duration" , sleepDuration .String ()),
489496 }
0 commit comments