Skip to content

Commit

Permalink
Merge pull request kubernetes#895 from smarterclayton/only_write_when…
Browse files Browse the repository at this point in the history
…_value_changes

Only write to etcd if values have changed
  • Loading branch information
lavalamp committed Aug 15, 2014
2 parents 778c89f + befbf84 commit 2d7a0c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ type EtcdUpdateFunc func(input interface{}) (output interface{}, err error)
// // Before this function is called, currentObj has been reset to etcd's current
// // contents for "myKey".
//
// cur := input.(*MyType) // Gauranteed to work.
// cur := input.(*MyType) // Guaranteed to work.
//
// // Make a *modification*.
// cur.Counter++
Expand Down Expand Up @@ -290,6 +290,10 @@ func (h *EtcdHelper) AtomicUpdate(key string, ptrToType interface{}, tryUpdate E
return err
}

if string(data) == origBody {
return nil
}

_, err = h.Client.CompareAndSwap(key, string(data), 0, origBody, index)
if IsEtcdTestFailed(err) {
continue
Expand Down
32 changes: 32 additions & 0 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package tools

import (
"errors"
"fmt"
"reflect"
"sync"
Expand Down Expand Up @@ -282,6 +283,37 @@ func TestAtomicUpdate(t *testing.T) {
}
}

func TestAtomicUpdateNoChange(t *testing.T) {
fakeClient := MakeFakeEtcdClient(t)
fakeClient.TestIndex = true
helper := EtcdHelper{fakeClient, scheme, api.NewJSONBaseResourceVersioner()}

// Create a new node.
fakeClient.ExpectNotFoundGet("/some/key")
obj := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 1}
err := helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
return obj, nil
})
if err != nil {
t.Errorf("Unexpected error %#v", err)
}

// Update an existing node with the same data
callbackCalled := false
objUpdate := &TestResource{JSONBase: api.JSONBase{ID: "foo"}, Value: 1}
fakeClient.Err = errors.New("should not be called")
err = helper.AtomicUpdate("/some/key", &TestResource{}, func(in interface{}) (interface{}, error) {
callbackCalled = true
return objUpdate, nil
})
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
if !callbackCalled {
t.Errorf("tryUpdate callback should have been called.")
}
}

func TestAtomicUpdate_CreateCollision(t *testing.T) {
fakeClient := MakeFakeEtcdClient(t)
fakeClient.TestIndex = true
Expand Down

0 comments on commit 2d7a0c3

Please sign in to comment.