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

@ -5,12 +5,12 @@ import (
) )
const ( const (
Get = "get" Get = "get"
Create = "create" Create = "create"
Update = "update" Update = "update"
Delete = "delete" Delete = "delete"
TestAndSet = "testAndSet" CompareAndSwap = "compareAndSwap"
Expire = "expire" Expire = "expire"
) )
const ( const (

View File

@ -6,17 +6,17 @@ import (
) )
const ( const (
SetSuccess = 100 SetSuccess = 100
SetFail = 101 SetFail = 101
DeleteSuccess = 102 DeleteSuccess = 102
DeleteFail = 103 DeleteFail = 103
UpdateSuccess = 104 UpdateSuccess = 104
UpdateFail = 105 UpdateFail = 105
TestAndSetSuccess = 106 CompareAndSwapSuccess = 106
TestAndSetFail = 107 CompareAndSwapFail = 107
GetSuccess = 110 GetSuccess = 110
GetFail = 111 GetFail = 111
ExpireCount = 112 ExpireCount = 112
) )
type Stats struct { type Stats struct {
@ -38,9 +38,10 @@ type Stats struct {
UpdateFail uint64 `json:"updateFail"` UpdateFail uint64 `json:"updateFail"`
// Number of testAndSet requests // Number of testAndSet requests
TestAndSetSuccess uint64 `json:"testAndSetSuccess"` CompareAndSwapSuccess uint64 `json:"compareAndSwapSuccess"`
TestAndSetFail uint64 `json:"testAndSetFail"` CompareAndSwapFail uint64 `json:"compareAndSwapFail"`
ExpireCount uint64 `json:"expireCount"`
ExpireCount uint64 `json:"expireCount"`
Watchers uint64 `json:"watchers"` Watchers uint64 `json:"watchers"`
} }
@ -53,7 +54,7 @@ func newStats() *Stats {
func (s *Stats) clone() *Stats { func (s *Stats) clone() *Stats {
return &Stats{s.GetSuccess, s.GetFail, s.SetSuccess, s.SetFail, return &Stats{s.GetSuccess, s.GetFail, s.SetSuccess, s.SetFail,
s.DeleteSuccess, s.DeleteFail, s.UpdateSuccess, s.UpdateFail, 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 // 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 { func (s *Stats) TotalWrites() uint64 {
return s.SetSuccess + s.SetFail + return s.SetSuccess + s.SetFail +
s.DeleteSuccess + s.DeleteFail + s.DeleteSuccess + s.DeleteFail +
s.TestAndSetSuccess + s.TestAndSetFail + s.CompareAndSwapSuccess + s.CompareAndSwapFail +
s.UpdateSuccess + s.UpdateFail s.UpdateSuccess + s.UpdateFail
} }
@ -91,10 +92,10 @@ func (s *Stats) Inc(field int) {
atomic.AddUint64(&s.UpdateSuccess, 1) atomic.AddUint64(&s.UpdateSuccess, 1)
case UpdateFail: case UpdateFail:
atomic.AddUint64(&s.UpdateFail, 1) atomic.AddUint64(&s.UpdateFail, 1)
case TestAndSetSuccess: case CompareAndSwapSuccess:
atomic.AddUint64(&s.TestAndSetSuccess, 1) atomic.AddUint64(&s.CompareAndSwapSuccess, 1)
case TestAndSetFail: case CompareAndSwapFail:
atomic.AddUint64(&s.TestAndSetFail, 1) atomic.AddUint64(&s.CompareAndSwapFail, 1)
case ExpireCount: case ExpireCount:
atomic.AddUint64(&s.ExpireCount, 1) atomic.AddUint64(&s.ExpireCount, 1)
} }

View File

@ -12,7 +12,7 @@ func TestBasicStats(t *testing.T) {
var i uint64 var i uint64
var GetSuccess, GetFail, SetSuccess, SetFail, DeleteSuccess, DeleteFail 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 { for _, k := range keys {
i++ i++
@ -60,9 +60,9 @@ func TestBasicStats(t *testing.T) {
i++ i++
_, err := s.CompareAndSwap(k, "foo", 0, "bar", Permanent, i, 1) _, err := s.CompareAndSwap(k, "foo", 0, "bar", Permanent, i, 1)
if err != nil { if err != nil {
TestAndSetFail++ CompareAndSwapFail++
} else { } else {
TestAndSetSuccess++ CompareAndSwapSuccess++
} }
} }
@ -132,12 +132,12 @@ func TestBasicStats(t *testing.T) {
t.Fatalf("UpdateFail [%d] != Stats.UpdateFail [%d]", UpdateFail, s.Stats.UpdateFail) t.Fatalf("UpdateFail [%d] != Stats.UpdateFail [%d]", UpdateFail, s.Stats.UpdateFail)
} }
if TestAndSetSuccess != s.Stats.TestAndSetSuccess { if CompareAndSwapSuccess != s.Stats.CompareAndSwapSuccess {
t.Fatalf("TestAndSetSuccess [%d] != Stats.TestAndSetSuccess [%d]", TestAndSetSuccess, s.Stats.TestAndSetSuccess) t.Fatalf("TestAndSetSuccess [%d] != Stats.CompareAndSwapSuccess [%d]", CompareAndSwapSuccess, s.Stats.CompareAndSwapSuccess)
} }
if TestAndSetFail != s.Stats.TestAndSetFail { if CompareAndSwapFail != s.Stats.CompareAndSwapFail {
t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", TestAndSetFail, s.Stats.TestAndSetFail) t.Fatalf("TestAndSetFail [%d] != Stats.TestAndSetFail [%d]", CompareAndSwapFail, s.Stats.CompareAndSwapFail)
} }
s = newStore() 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) n, err := s.internalGet(nodePath, index, term)
if err != nil { if err != nil {
s.Stats.Inc(TestAndSetFail) s.Stats.Inc(CompareAndSwapFail)
return nil, err return nil, err
} }
if n.IsDir() { // can only test and set file 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) return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, index, term)
} }
// If both of the prevValue and prevIndex are given, we will test both of them. // 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. // Command will be executed, only if both of the tests are successful.
if (prevValue == "" || n.Value == prevValue) && (prevIndex == 0 || n.ModifiedIndex == prevIndex) { 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 e.PrevValue = n.Value
// if test succeed, write the 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() e.Expiration, e.TTL = n.ExpirationAndTTL()
s.WatcherHub.notify(e) s.WatcherHub.notify(e)
s.Stats.Inc(TestAndSetSuccess) s.Stats.Inc(CompareAndSwapSuccess)
return e, nil return e, nil
} }
cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) 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) 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) c, _ = s.Watch("/foo/foo/foo", false, 0, 2, 1)
s.CompareAndSwap("/foo/foo/foo", "car", 0, "bar", Permanent, 3, 1) s.CompareAndSwap("/foo/foo/foo", "car", 0, "bar", Permanent, 3, 1)
e = nonblockingRetrive(c) e = nonblockingRetrive(c)
if e.Key != "/foo/foo/foo" || e.Action != TestAndSet { if e.Key != "/foo/foo/foo" || e.Action != CompareAndSwap {
t.Fatal("watch for TestAndSet node fails") t.Fatal("watch for CompareAndSwap node fails")
} }
c, _ = s.Watch("/foo/foo/foo", false, 0, 3, 1) 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) c, _ = s.Watch("/foo", true, 0, 6, 1)
s.CompareAndSwap("/foo/foo/boo", "foo", 0, "bar", Permanent, 7, 1) s.CompareAndSwap("/foo/foo/boo", "foo", 0, "bar", Permanent, 7, 1)
e = nonblockingRetrive(c) e = nonblockingRetrive(c)
if e.Key != "/foo/foo/boo" || e.Action != TestAndSet { if e.Key != "/foo/foo/boo" || e.Action != CompareAndSwap {
t.Fatal("watch for TestAndSet subdirectory fails") t.Fatal("watch for CompareAndSwap subdirectory fails")
} }
c, _ = s.Watch("/foo", true, 0, 7, 1) c, _ = s.Watch("/foo", true, 0, 7, 1)
@ -404,7 +404,7 @@ func TestWatch(t *testing.T) {
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
e = nonblockingRetrive(c) e = nonblockingRetrive(c)
if e.Key != "/foo/foo/boo" || e.Action != Expire || e.Index != 13 { 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 ( import (
"fmt" "fmt"
"testing"
"net/url"
"net" "net"
"net/url"
"testing"
) )
// To pass this test, we need to create a cluster of 3 machines // 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") t.Fatal("cannot sync machines")
} }
for _, m := range(c.GetCluster()) { for _, m := range c.GetCluster() {
u, err := url.Parse(m) u, err := url.Parse(m)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -27,7 +27,7 @@ func TestSync(t *testing.T) {
if u.Scheme != "http" { if u.Scheme != "http" {
t.Fatal("scheme must be http") t.Fatal("scheme must be http")
} }
host, _, err := net.SplitHostPort(u.Host) host, _, err := net.SplitHostPort(u.Host)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

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

View File

@ -1,4 +1,5 @@
package log package log
// Copyright 2013, CoreOS, Inc. All rights reserved. // Copyright 2013, CoreOS, Inc. All rights reserved.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // 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...)) logger.Log(priority, fmt.Sprintf(format, v...))
} }
func (logger *Logger) Emergency(v ...interface{}) { func (logger *Logger) Emergency(v ...interface{}) {
logger.Log(PriEmerg, v...) logger.Log(PriEmerg, v...)
} }
@ -99,7 +99,6 @@ func (logger *Logger) Debugf(format string, v ...interface{}) {
logger.Log(PriDebug, fmt.Sprintf(format, v...)) logger.Log(PriDebug, fmt.Sprintf(format, v...))
} }
func Emergency(v ...interface{}) { func Emergency(v ...interface{}) {
defaultLogger.Log(PriEmerg, v...) defaultLogger.Log(PriEmerg, v...)
} }
@ -158,57 +157,56 @@ func Debugf(format string, v ...interface{}) {
// Standard library log functions // Standard library log functions
func (logger *Logger)Fatalln (v ...interface{}) { func (logger *Logger) Fatalln(v ...interface{}) {
logger.Log(PriCrit, v...) logger.Log(PriCrit, v...)
os.Exit(1) os.Exit(1)
} }
func (logger *Logger)Fatalf (format string, v ...interface{}) { func (logger *Logger) Fatalf(format string, v ...interface{}) {
logger.Logf(PriCrit, format, v...) logger.Logf(PriCrit, format, v...)
os.Exit(1) os.Exit(1)
} }
func (logger *Logger)Panicln (v ...interface{}) { func (logger *Logger) Panicln(v ...interface{}) {
s := fmt.Sprint(v...) s := fmt.Sprint(v...)
logger.Log(PriErr, s) logger.Log(PriErr, s)
panic(s) panic(s)
} }
func (logger *Logger)Panicf (format string, v ...interface{}) { func (logger *Logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...) s := fmt.Sprintf(format, v...)
logger.Log(PriErr, s) logger.Log(PriErr, s)
panic(s) panic(s)
} }
func (logger *Logger)Println (v ...interface{}) { func (logger *Logger) Println(v ...interface{}) {
logger.Log(PriInfo, v...) logger.Log(PriInfo, v...)
} }
func (logger *Logger)Printf (format string, v ...interface{}) { func (logger *Logger) Printf(format string, v ...interface{}) {
logger.Logf(PriInfo, format, v...) logger.Logf(PriInfo, format, v...)
} }
func Fatalln(v ...interface{}) {
func Fatalln (v ...interface{}) {
defaultLogger.Log(PriCrit, v...) defaultLogger.Log(PriCrit, v...)
os.Exit(1) os.Exit(1)
} }
func Fatalf (format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) {
defaultLogger.Logf(PriCrit, format, v...) defaultLogger.Logf(PriCrit, format, v...)
os.Exit(1) os.Exit(1)
} }
func Panicln (v ...interface{}) { func Panicln(v ...interface{}) {
s := fmt.Sprint(v...) s := fmt.Sprint(v...)
defaultLogger.Log(PriErr, s) defaultLogger.Log(PriErr, s)
panic(s) panic(s)
} }
func Panicf (format string, v ...interface{}) { func Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...) s := fmt.Sprintf(format, v...)
defaultLogger.Log(PriErr, s) defaultLogger.Log(PriErr, s)
panic(s) panic(s)
} }
func Println (v ...interface{}) { func Println(v ...interface{}) {
defaultLogger.Log(PriInfo, v...) defaultLogger.Log(PriInfo, v...)
} }
func Printf (format string, v ...interface{}) { func Printf(format string, v ...interface{}) {
defaultLogger.Logf(PriInfo, format, v...) defaultLogger.Logf(PriInfo, format, v...)
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -24,7 +24,7 @@ func Files() []*os.File {
files := []*os.File(nil) files := []*os.File(nil)
for fd := listenFdsStart; fd < listenFdsStart+nfds; fd++ { for fd := listenFdsStart; fd < listenFdsStart+nfds; fd++ {
syscall.CloseOnExec(fd) syscall.CloseOnExec(fd)
files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_" + strconv.Itoa(fd))) files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_"+strconv.Itoa(fd)))
} }
return files return files
} }