Skip to content

Commit 66118bd

Browse files
committed
refactor , logic improvement to check only forward updates are present, comments
1 parent b32109b commit 66118bd

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

agent/consul/acl_replication.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ type aclTypeReplicator interface {
6666
// correction phase).
6767
FetchUpdated(srv *Server, updates []string) (int, error)
6868

69-
//ensureUpdatedConsistent compares the updated items with the upsertable remotes
69+
//ensureRemoteConsistent compares the updated items with the upsertable remotes
7070
//returns consistent update check post stale batch read or other scenarios
7171
//[]string of items missing from remote, []string of items updated remote, error if inconsistent
72-
ensureUpdatedConsistent(updates []string) ([]string, []string, error)
72+
ensureRemoteConsistent(updates []string) ([]string, []string, error)
7373

7474
// LenPendingUpdates should be the size of the data retrieved in
7575
// FetchUpdated.
@@ -448,7 +448,10 @@ func (s *Server) replicateACLType(ctx context.Context, logger hclog.Logger, tr a
448448
}
449449
return 0, false, fmt.Errorf("failed to retrieve ACL %s updates: %v", tr.SingularNoun(), err)
450450
}
451-
_, _, err = tr.ensureUpdatedConsistent(res.LocalUpserts)
451+
//if fetch updated gets stale inconsistent data then we should not proceed with applying
452+
//the updates as that would lead to partial/stale data being replicated
453+
//hence, we call ensureRemoteConsistent to validate the fetched updates with diff results
454+
_, _, err = tr.ensureRemoteConsistent(res.LocalUpserts)
452455
if err != nil {
453456
if err == errContainsStaleData {
454457
return 0, false, fmt.Errorf("failed to ensure consistent %s replication updates - stale data", tr.PluralNoun())

agent/consul/acl_replication_types.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (r *aclTokenReplicator) FetchUpdated(srv *Server, updates []string) (int, e
8787
return len(r.updated), nil
8888
}
8989

90-
func (r *aclTokenReplicator) ensureUpdatedConsistent(updates []string) ([]string, []string, error) {
90+
func (r *aclTokenReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) {
9191
//return true if consistent updates,
9292
return []string{}, []string{}, nil
9393
}
@@ -192,7 +192,7 @@ func (r *aclPolicyReplicator) FetchUpdated(srv *Server, updates []string) (int,
192192
return len(r.updated), nil
193193
}
194194

195-
func (r *aclPolicyReplicator) ensureUpdatedConsistent(updates []string) ([]string, []string, error) {
195+
func (r *aclPolicyReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) {
196196
updatedMap := make(map[string]*structs.ACLPolicy)
197197
for _, policy := range r.updated {
198198
updatedMap[policy.ID] = policy
@@ -205,30 +205,30 @@ func (r *aclPolicyReplicator) ensureUpdatedConsistent(updates []string) ([]strin
205205

206206
//iterate over all updates array which are policy IDs check if the hash match for both
207207
var consistent bool = true
208-
var deleted []string
209-
var updated []string
208+
var remoteNotCreated []string
209+
var remoteNotUpdated []string
210210
var err error = nil
211211

212212
for _, policyID := range updates {
213213
if updatedPolicy, ok := updatedMap[policyID]; ok {
214214
if remotePolicy, ok := remoteMap[policyID]; ok {
215215
if !bytes.Equal(updatedPolicy.Hash, remotePolicy.Hash) && updatedPolicy.ModifyIndex < remotePolicy.ModifyIndex {
216216
// remote stale batch did not get modified policy than what local diff calculated
217-
updated = append(updated, policyID)
217+
remoteNotUpdated = append(remoteNotUpdated, policyID)
218218
consistent = false
219219
}
220220
}
221221
} else if remotePolicy, ok := remoteMap[policyID]; ok && remotePolicy.ModifyIndex == remotePolicy.CreateIndex {
222222
// remote stale batch did not get the created policy than what local diff calculated
223-
deleted = append(deleted, policyID)
223+
remoteNotCreated = append(remoteNotCreated, policyID)
224224
consistent = false
225225
}
226226
}
227227

228228
if !consistent {
229229
err = errContainsStaleData
230230
}
231-
return deleted, updated, err
231+
return remoteNotCreated, remoteNotUpdated, err
232232
}
233233

234234
func (r *aclPolicyReplicator) DeleteLocalBatch(srv *Server, batch []string) error {
@@ -352,7 +352,7 @@ func (r *aclRoleReplicator) FetchUpdated(srv *Server, updates []string) (int, er
352352
return len(r.updated), nil
353353
}
354354

355-
func (r *aclRoleReplicator) ensureUpdatedConsistent(updates []string) ([]string, []string, error) {
355+
func (r *aclRoleReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) {
356356
//return true if consistent updates,
357357
return []string{}, []string{}, nil
358358
}

0 commit comments

Comments
 (0)