Skip to content

Commit

Permalink
fix: asm leader failing to read and write lease metadata (#2349)
Browse files Browse the repository at this point in the history
fixes #2343
  • Loading branch information
matt2e authored Aug 14, 2024
1 parent e3c96ef commit d5666b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions backend/controller/dal/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion backend/controller/leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d5666b9

Please sign in to comment.