From d5666b9e4363ae042d69a0d92ea627e69a1f3487 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Wed, 14 Aug 2024 11:24:59 +1000 Subject: [PATCH] fix: asm leader failing to read and write lease metadata (#2349) fixes #2343 --- backend/controller/dal/lease.go | 8 +++++--- backend/controller/leader/leader.go | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/controller/dal/lease.go b/backend/controller/dal/lease.go index 7bcfc64fc0..8bbc1fd5b0 100644 --- a/backend/controller/dal/lease.go +++ b/backend/controller/dal/lease.go @@ -96,7 +96,7 @@ func (d *DAL) AcquireLease(ctx context.Context, key leases.Key, ttl time.Duratio return nil, nil, fmt.Errorf("failed to marshal lease metadata: %w", err) } } - idempotencyKey, err := d.db.NewLease(ctx, key, sqltypes.Duration(ttl), pqtype.NullRawMessage{RawMessage: metadataBytes}) + idempotencyKey, err := d.db.NewLease(ctx, key, sqltypes.Duration(ttl), pqtype.NullRawMessage{RawMessage: metadataBytes, Valid: true}) if err != nil { err = dalerrs.TranslatePGError(err) if errors.Is(err, dalerrs.ErrConflict) { @@ -130,8 +130,10 @@ func (d *DAL) GetLeaseInfo(ctx context.Context, key leases.Key, metadata any) (e if err != nil { return expiry, dalerrs.TranslatePGError(err) } - if err := json.Unmarshal(l.Metadata.RawMessage, metadata); err != nil { - return expiry, fmt.Errorf("could not unmarshal lease metadata: %w", err) + if l.Metadata.Valid { + if err := json.Unmarshal(l.Metadata.RawMessage, metadata); err != nil { + return expiry, fmt.Errorf("could not unmarshal lease metadata: %w", err) + } } return l.ExpiresAt, nil } diff --git a/backend/controller/leader/leader.go b/backend/controller/leader/leader.go index 643adc327e..79c4081a2b 100644 --- a/backend/controller/leader/leader.go +++ b/backend/controller/leader/leader.go @@ -182,7 +182,9 @@ func (c *Coordinator[P]) createFollower() (out P, err error) { } return out, fmt.Errorf("could not get lease for %s: %w", c.key, err) } - if urlString == c.advertise.String() { + if urlString == "" { + return out, fmt.Errorf("%s leader lease missing url in metadata", c.key) + } else if urlString == c.advertise.String() { // This prevents endless loops after a lease breaks. // If we create a follower pointing locally, the receiver will likely try to then call the leader, which starts the loop again. return out, fmt.Errorf("could not follow %s leader at own url: %s", c.key, urlString)