mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 12:32:03 +00:00
Recheck if transformed data is stale when doing live lookup during update
This commit is contained in:
parent
23881a9055
commit
070089c6bf
@ -332,13 +332,16 @@ func (s *store) GuaranteedUpdate(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
mustCheckData = false
|
||||||
if !bytes.Equal(data, origState.data) {
|
if !bytes.Equal(data, origState.data) {
|
||||||
// original data changed, restart loop
|
// original data changed, restart loop
|
||||||
mustCheckData = false
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return decode(s.codec, s.versioner, origState.data, out, origState.rev)
|
// recheck that the data from etcd is not stale before short-circuiting a write
|
||||||
|
if !origState.stale {
|
||||||
|
return decode(s.codec, s.versioner, origState.data, out, origState.rev)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newData, err := s.transformer.TransformToStorage(data, transformContext)
|
newData, err := s.transformer.TransformToStorage(data, transformContext)
|
||||||
|
@ -526,6 +526,8 @@ func TestGuaranteedUpdateChecksStoredData(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
store.transformer = prefixTransformer{prefix: []byte(defaultTestPrefix)}
|
||||||
|
|
||||||
// this update should write the canonical value to etcd because the new serialization differs
|
// this update should write the canonical value to etcd because the new serialization differs
|
||||||
// from the stored serialization
|
// from the stored serialization
|
||||||
input.ResourceVersion = strconv.FormatInt(resp.Header.Revision, 10)
|
input.ResourceVersion = strconv.FormatInt(resp.Header.Revision, 10)
|
||||||
@ -540,6 +542,36 @@ func TestGuaranteedUpdateChecksStoredData(t *testing.T) {
|
|||||||
if out.ResourceVersion == strconv.FormatInt(resp.Header.Revision, 10) {
|
if out.ResourceVersion == strconv.FormatInt(resp.Header.Revision, 10) {
|
||||||
t.Errorf("guaranteed update should have updated the serialized data, got %#v", out)
|
t.Errorf("guaranteed update should have updated the serialized data, got %#v", out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastVersion := out.ResourceVersion
|
||||||
|
|
||||||
|
// this update should not write to etcd because the input matches the stored data
|
||||||
|
input = out
|
||||||
|
out = &example.Pod{}
|
||||||
|
err = store.GuaranteedUpdate(ctx, key, out, true, nil,
|
||||||
|
func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
|
||||||
|
return input, nil, nil
|
||||||
|
}, input)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Update failed: %v", err)
|
||||||
|
}
|
||||||
|
if out.ResourceVersion != lastVersion {
|
||||||
|
t.Errorf("guaranteed update should have short-circuited write, got %#v", out)
|
||||||
|
}
|
||||||
|
|
||||||
|
store.transformer = prefixTransformer{prefix: []byte(defaultTestPrefix), stale: true}
|
||||||
|
|
||||||
|
// this update should write to etcd because the transformer reported stale
|
||||||
|
err = store.GuaranteedUpdate(ctx, key, out, true, nil,
|
||||||
|
func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
|
||||||
|
return input, nil, nil
|
||||||
|
}, input)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Update failed: %v", err)
|
||||||
|
}
|
||||||
|
if out.ResourceVersion == lastVersion {
|
||||||
|
t.Errorf("guaranteed update should have written to etcd when transformer reported stale, got %#v", out)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGuaranteedUpdateWithConflict(t *testing.T) {
|
func TestGuaranteedUpdateWithConflict(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user