refactor change testAndSet to CompareAndSwap

This commit is contained in:
Xiang Li 2013-10-14 22:32:22 -07:00
parent 545f8ed6a1
commit 9ebdcb8ae3
13 changed files with 66 additions and 64 deletions

View File

@ -9,7 +9,7 @@ const (
Create = "create"
Update = "update"
Delete = "delete"
TestAndSet = "testAndSet"
CompareAndSwap = "compareAndSwap"
Expire = "expire"
)

View File

@ -12,8 +12,8 @@ const (
DeleteFail = 103
UpdateSuccess = 104
UpdateFail = 105
TestAndSetSuccess = 106
TestAndSetFail = 107
CompareAndSwapSuccess = 106
CompareAndSwapFail = 107
GetSuccess = 110
GetFail = 111
ExpireCount = 112
@ -38,8 +38,9 @@ type Stats struct {
UpdateFail uint64 `json:"updateFail"`
// Number of testAndSet requests
TestAndSetSuccess uint64 `json:"testAndSetSuccess"`
TestAndSetFail uint64 `json:"testAndSetFail"`
CompareAndSwapSuccess uint64 `json:"compareAndSwapSuccess"`
CompareAndSwapFail uint64 `json:"compareAndSwapFail"`
ExpireCount uint64 `json:"expireCount"`
Watchers uint64 `json:"watchers"`
@ -53,7 +54,7 @@ func newStats() *Stats {
func (s *Stats) clone() *Stats {
return &Stats{s.GetSuccess, s.GetFail, s.SetSuccess, s.SetFail,
s.DeleteSuccess, s.DeleteFail, s.UpdateSuccess, s.UpdateFail,
s.TestAndSetSuccess, s.TestAndSetFail, s.Watchers, s.ExpireCount}
s.CompareAndSwapSuccess, s.CompareAndSwapFail, s.Watchers, s.ExpireCount}
}
// Status() return the statistics info of etcd storage its recent start
@ -69,7 +70,7 @@ func (s *Stats) TotalReads() uint64 {
func (s *Stats) TotalWrites() uint64 {
return s.SetSuccess + s.SetFail +
s.DeleteSuccess + s.DeleteFail +
s.TestAndSetSuccess + s.TestAndSetFail +
s.CompareAndSwapSuccess + s.CompareAndSwapFail +
s.UpdateSuccess + s.UpdateFail
}
@ -91,10 +92,10 @@ func (s *Stats) Inc(field int) {
atomic.AddUint64(&s.UpdateSuccess, 1)
case UpdateFail:
atomic.AddUint64(&s.UpdateFail, 1)
case TestAndSetSuccess:
atomic.AddUint64(&s.TestAndSetSuccess, 1)
case TestAndSetFail:
atomic.AddUint64(&s.TestAndSetFail, 1)
case CompareAndSwapSuccess:
atomic.AddUint64(&s.CompareAndSwapSuccess, 1)
case CompareAndSwapFail:
atomic.AddUint64(&s.CompareAndSwapFail, 1)
case ExpireCount:
atomic.AddUint64(&s.ExpireCount, 1)
}

View File

@ -12,7 +12,7 @@ func TestBasicStats(t *testing.T) {
var i uint64
var GetSuccess, GetFail, SetSuccess, SetFail, DeleteSuccess, DeleteFail uint64
var UpdateSuccess, UpdateFail, TestAndSetSuccess, TestAndSetFail, watcher_number uint64
var UpdateSuccess, UpdateFail, CompareAndSwapSuccess, CompareAndSwapFail, watcher_number uint64
for _, k := range keys {
i++
@ -60,9 +60,9 @@ func TestBasicStats(t *testing.T) {
i++
_, err := s.CompareAndSwap(k, "foo", 0, "bar", Permanent, i, 1)
if err != nil {
TestAndSetFail++
CompareAndSwapFail++
} else {
TestAndSetSuccess++
CompareAndSwapSuccess++
}
}
@ -132,12 +132,12 @@ func TestBasicStats(t *testing.T) {
t.Fatalf("UpdateFail [%d] != Stats.UpdateFail [%d]", UpdateFail, s.Stats.UpdateFail)
}
if TestAndSetSuccess != s.Stats.TestAndSetSuccess {
t.Fatalf("TestAndSetSuccess [%d] != Stats.TestAndSetSuccess [%d]", TestAndSetSuccess, s.Stats.TestAndSetSuccess)
if CompareAndSwapSuccess != s.Stats.CompareAndSwapSuccess {
t.Fatalf("TestAndSetSuccess [%d] != Stats.CompareAndSwapSuccess [%d]", CompareAndSwapSuccess, s.Stats.CompareAndSwapSuccess)
}
if TestAndSetFail != s.Stats.TestAndSetFail {
t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", TestAndSetFail, s.Stats.TestAndSetFail)
if CompareAndSwapFail != s.Stats.CompareAndSwapFail {
t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", CompareAndSwapFail, s.Stats.CompareAndSwapFail)
}
s = newStore()

View File

@ -130,19 +130,19 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint
n, err := s.internalGet(nodePath, index, term)
if err != nil {
s.Stats.Inc(TestAndSetFail)
s.Stats.Inc(CompareAndSwapFail)
return nil, err
}
if n.IsDir() { // can only test and set file
s.Stats.Inc(TestAndSetFail)
s.Stats.Inc(CompareAndSwapFail)
return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, index, term)
}
// If both of the prevValue and prevIndex are given, we will test both of them.
// Command will be executed, only if both of the tests are successful.
if (prevValue == "" || n.Value == prevValue) && (prevIndex == 0 || n.ModifiedIndex == prevIndex) {
e := newEvent(TestAndSet, nodePath, index, term)
e := newEvent(CompareAndSwap, nodePath, index, term)
e.PrevValue = n.Value
// if test succeed, write the value
@ -153,12 +153,12 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint
e.Expiration, e.TTL = n.ExpirationAndTTL()
s.WatcherHub.notify(e)
s.Stats.Inc(TestAndSetSuccess)
s.Stats.Inc(CompareAndSwapSuccess)
return e, nil
}
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
s.Stats.Inc(TestAndSetFail)
s.Stats.Inc(CompareAndSwapFail)
return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, index, term)
}

View File

@ -340,8 +340,8 @@ func TestWatch(t *testing.T) {
c, _ = s.Watch("/foo/foo/foo", false, 0, 2, 1)
s.CompareAndSwap("/foo/foo/foo", "car", 0, "bar", Permanent, 3, 1)
e = nonblockingRetrive(c)
if e.Key != "/foo/foo/foo" || e.Action != TestAndSet {
t.Fatal("watch for TestAndSet node fails")
if e.Key != "/foo/foo/foo" || e.Action != CompareAndSwap {
t.Fatal("watch for CompareAndSwap node fails")
}
c, _ = s.Watch("/foo/foo/foo", false, 0, 3, 1)
@ -369,8 +369,8 @@ func TestWatch(t *testing.T) {
c, _ = s.Watch("/foo", true, 0, 6, 1)
s.CompareAndSwap("/foo/foo/boo", "foo", 0, "bar", Permanent, 7, 1)
e = nonblockingRetrive(c)
if e.Key != "/foo/foo/boo" || e.Action != TestAndSet {
t.Fatal("watch for TestAndSet subdirectory fails")
if e.Key != "/foo/foo/boo" || e.Action != CompareAndSwap {
t.Fatal("watch for CompareAndSwap subdirectory fails")
}
c, _ = s.Watch("/foo", true, 0, 7, 1)
@ -404,7 +404,7 @@ func TestWatch(t *testing.T) {
time.Sleep(time.Second * 2)
e = nonblockingRetrive(c)
if e.Key != "/foo/foo/boo" || e.Action != Expire || e.Index != 13 {
t.Fatal("watch for Expiration of TestAndSet() subdirectory fails ", e)
t.Fatal("watch for Expiration of CompareAndSwap() subdirectory fails ", e)
}
}

View File

@ -2,9 +2,9 @@ package etcd
import (
"fmt"
"testing"
"net/url"
"net"
"net/url"
"testing"
)
// To pass this test, we need to create a cluster of 3 machines
@ -19,7 +19,7 @@ func TestSync(t *testing.T) {
t.Fatal("cannot sync machines")
}
for _, m := range(c.GetCluster()) {
for _, m := range c.GetCluster() {
u, err := url.Parse(m)
if err != nil {
t.Fatal(err)

View File

@ -1,4 +1,3 @@
package main
import (

View File

@ -1,4 +1,5 @@
package log
// Copyright 2013, CoreOS, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -42,7 +43,6 @@ func (logger *Logger) Logf(priority Priority, format string, v ...interface{}) {
logger.Log(priority, fmt.Sprintf(format, v...))
}
func (logger *Logger) Emergency(v ...interface{}) {
logger.Log(PriEmerg, v...)
}
@ -99,7 +99,6 @@ func (logger *Logger) Debugf(format string, v ...interface{}) {
logger.Log(PriDebug, fmt.Sprintf(format, v...))
}
func Emergency(v ...interface{}) {
defaultLogger.Log(PriEmerg, v...)
}
@ -185,7 +184,6 @@ func (logger *Logger)Printf (format string, v ...interface{}) {
logger.Logf(PriInfo, format, v...)
}
func Fatalln(v ...interface{}) {
defaultLogger.Log(PriCrit, v...)
os.Exit(1)

View File

@ -1,4 +1,5 @@
package log
// Copyright 2013, CoreOS, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -1,4 +1,5 @@
package log
// Copyright 2013, CoreOS, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -1,4 +1,5 @@
package log
// Copyright 2013, CoreOS, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -1,4 +1,5 @@
package log
// Copyright 2013, CoreOS, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");