-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
state: ensure that identical manual virtual IP updates result in not bumping the modify indexes #21909
state: ensure that identical manual virtual IP updates result in not bumping the modify indexes #21909
Changes from 1 commit
36d5891
0754411
7e55ff3
90f8e8e
e86a5d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"fmt" | ||
"net" | ||
"reflect" | ||
"slices" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-memdb" | ||
|
@@ -1106,6 +1108,9 @@ func (s *Store) AssignManualServiceVIPs(idx uint64, psn structs.PeeredServiceNam | |
for _, ip := range ips { | ||
assignedIPs[ip] = struct{}{} | ||
} | ||
|
||
txnNeedsCommit := false | ||
|
||
modifiedEntries := make(map[structs.PeeredServiceName]struct{}) | ||
for ip := range assignedIPs { | ||
entry, err := tx.First(tableServiceVirtualIPs, indexManualVIPs, psn.ServiceName.PartitionOrDefault(), ip) | ||
|
@@ -1118,7 +1123,13 @@ func (s *Store) AssignManualServiceVIPs(idx uint64, psn structs.PeeredServiceNam | |
} | ||
|
||
newEntry := entry.(ServiceVirtualIP) | ||
if newEntry.Service.ServiceName.Matches(psn.ServiceName) { | ||
|
||
var ( | ||
thisServiceName = newEntry.Service.ServiceName | ||
thisPeer = newEntry.Service.Peer | ||
) | ||
|
||
if thisServiceName.Matches(psn.ServiceName) && thisPeer == psn.Peer { | ||
continue | ||
} | ||
|
||
|
@@ -1130,13 +1141,20 @@ func (s *Store) AssignManualServiceVIPs(idx uint64, psn structs.PeeredServiceNam | |
filteredIPs = append(filteredIPs, existingIP) | ||
} | ||
} | ||
sort.Strings(filteredIPs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we were storing VIPs in whatever order they happened to be in. It seemed silly to not be sorting them. |
||
|
||
newEntry.ManualIPs = filteredIPs | ||
newEntry.ModifyIndex = idx | ||
if err := tx.Insert(tableServiceVirtualIPs, newEntry); err != nil { | ||
return false, nil, fmt.Errorf("failed inserting service virtual IP entry: %s", err) | ||
} | ||
modifiedEntries[newEntry.Service] = struct{}{} | ||
|
||
if err := updateVirtualIPMaxIndexes(tx, idx, thisServiceName.PartitionOrDefault(), thisPeer); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we were not updating the max index table for the entries that had VIPs stolen from them. |
||
return false, nil, err | ||
} | ||
|
||
txnNeedsCommit = true | ||
} | ||
|
||
entry, err := tx.First(tableServiceVirtualIPs, indexID, psn) | ||
|
@@ -1149,23 +1167,49 @@ func (s *Store) AssignManualServiceVIPs(idx uint64, psn structs.PeeredServiceNam | |
} | ||
|
||
newEntry := entry.(ServiceVirtualIP) | ||
newEntry.ManualIPs = ips | ||
newEntry.ModifyIndex = idx | ||
|
||
if err := tx.Insert(tableServiceVirtualIPs, newEntry); err != nil { | ||
return false, nil, fmt.Errorf("failed inserting service virtual IP entry: %s", err) | ||
} | ||
if err := updateVirtualIPMaxIndexes(tx, idx, psn.ServiceName.PartitionOrDefault(), psn.Peer); err != nil { | ||
return false, nil, err | ||
// Check to see if the slice already contains the same ips. | ||
if !vipSliceEqualsMapKeys(newEntry.ManualIPs, assignedIPs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the key part of the fix. |
||
newEntry.ManualIPs = slices.Clone(ips) | ||
newEntry.ModifyIndex = idx | ||
|
||
sort.Strings(newEntry.ManualIPs) | ||
|
||
if err := tx.Insert(tableServiceVirtualIPs, newEntry); err != nil { | ||
return false, nil, fmt.Errorf("failed inserting service virtual IP entry: %s", err) | ||
} | ||
if err := updateVirtualIPMaxIndexes(tx, idx, psn.ServiceName.PartitionOrDefault(), psn.Peer); err != nil { | ||
return false, nil, err | ||
} | ||
txnNeedsCommit = true | ||
} | ||
if err = tx.Commit(); err != nil { | ||
return false, nil, err | ||
|
||
if txnNeedsCommit { | ||
if err = tx.Commit(); err != nil { | ||
return false, nil, err | ||
} | ||
} | ||
|
||
return true, maps.SliceOfKeys(modifiedEntries), nil | ||
} | ||
|
||
func vipSliceEqualsMapKeys(a []string, b map[string]struct{}) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
for _, ip := range a { | ||
if _, ok := b[ip]; !ok { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func updateVirtualIPMaxIndexes(txn WriteTxn, idx uint64, partition, peerName string) error { | ||
// update global max index (for snapshots) | ||
if err := indexUpdateMaxTxn(txn, idx, tableServiceVirtualIPs); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The snapshot logic grabs the max index from this table without peering/partition prefixes, so in order for that to be more correct we update the un-prefixed index here too. |
||
return fmt.Errorf("failed while updating index: %w", err) | ||
} | ||
// update per-partition max index | ||
if err := indexUpdateMaxTxn(txn, idx, partitionedIndexEntryName(tableServiceVirtualIPs, partition)); err != nil { | ||
return fmt.Errorf("failed while updating partitioned index: %w", err) | ||
|
@@ -3086,6 +3130,7 @@ func servicesVirtualIPsTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, []ServiceVirt | |
vips = append(vips, vip) | ||
} | ||
|
||
// Pull from the global one | ||
idx := maxIndexWatchTxn(tx, nil, tableServiceVirtualIPs) | ||
|
||
return idx, vips, nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is practically an issue, but I did notice that the logic was:
and with this change i fixed it to