From 9ebdcb8ae33a64816a82c71fa6a3d9541bfad6f5 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 14 Oct 2013 22:32:22 -0700 Subject: [PATCH] refactor change testAndSet to CompareAndSwap --- store/event.go | 12 +++--- store/stats.go | 41 ++++++++++--------- store/stats_test.go | 14 +++---- store/store.go | 10 ++--- store/store_test.go | 10 ++--- .../coreos/go-etcd/etcd/client_test.go | 8 ++-- .../examples/sync-cluster/sync-cluster.go | 1 - .../github.com/coreos/go-log/log/commands.go | 28 ++++++------- .../github.com/coreos/go-log/log/fields.go | 1 + .../github.com/coreos/go-log/log/logger.go | 1 + .../github.com/coreos/go-log/log/priority.go | 1 + .../github.com/coreos/go-log/log/sinks.go | 1 + .../coreos/go-systemd/activation/files.go | 2 +- 13 files changed, 66 insertions(+), 64 deletions(-) diff --git a/store/event.go b/store/event.go index 0d9ec0a37..f9ae0938b 100644 --- a/store/event.go +++ b/store/event.go @@ -5,12 +5,12 @@ import ( ) const ( - Get = "get" - Create = "create" - Update = "update" - Delete = "delete" - TestAndSet = "testAndSet" - Expire = "expire" + Get = "get" + Create = "create" + Update = "update" + Delete = "delete" + CompareAndSwap = "compareAndSwap" + Expire = "expire" ) const ( diff --git a/store/stats.go b/store/stats.go index e2053ed42..4c89b93e3 100644 --- a/store/stats.go +++ b/store/stats.go @@ -6,17 +6,17 @@ import ( ) const ( - SetSuccess = 100 - SetFail = 101 - DeleteSuccess = 102 - DeleteFail = 103 - UpdateSuccess = 104 - UpdateFail = 105 - TestAndSetSuccess = 106 - TestAndSetFail = 107 - GetSuccess = 110 - GetFail = 111 - ExpireCount = 112 + SetSuccess = 100 + SetFail = 101 + DeleteSuccess = 102 + DeleteFail = 103 + UpdateSuccess = 104 + UpdateFail = 105 + CompareAndSwapSuccess = 106 + CompareAndSwapFail = 107 + GetSuccess = 110 + GetFail = 111 + ExpireCount = 112 ) type Stats struct { @@ -38,9 +38,10 @@ type Stats struct { UpdateFail uint64 `json:"updateFail"` // Number of testAndSet requests - TestAndSetSuccess uint64 `json:"testAndSetSuccess"` - TestAndSetFail uint64 `json:"testAndSetFail"` - ExpireCount uint64 `json:"expireCount"` + 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) } diff --git a/store/stats_test.go b/store/stats_test.go index fa6483ac2..7c2296fb0 100644 --- a/store/stats_test.go +++ b/store/stats_test.go @@ -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() diff --git a/store/store.go b/store/store.go index f25b4345e..6e0fe4d68 100644 --- a/store/store.go +++ b/store/store.go @@ -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) } diff --git a/store/store_test.go b/store/store_test.go index 457a9b5f5..f26d3d616 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -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) } } diff --git a/third_party/github.com/coreos/go-etcd/etcd/client_test.go b/third_party/github.com/coreos/go-etcd/etcd/client_test.go index 29f138113..bf75d8947 100644 --- a/third_party/github.com/coreos/go-etcd/etcd/client_test.go +++ b/third_party/github.com/coreos/go-etcd/etcd/client_test.go @@ -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) @@ -27,7 +27,7 @@ func TestSync(t *testing.T) { if u.Scheme != "http" { t.Fatal("scheme must be http") } - + host, _, err := net.SplitHostPort(u.Host) if err != nil { t.Fatal(err) diff --git a/third_party/github.com/coreos/go-etcd/examples/sync-cluster/sync-cluster.go b/third_party/github.com/coreos/go-etcd/examples/sync-cluster/sync-cluster.go index 8249b4bdc..8c7e375c5 100644 --- a/third_party/github.com/coreos/go-etcd/examples/sync-cluster/sync-cluster.go +++ b/third_party/github.com/coreos/go-etcd/examples/sync-cluster/sync-cluster.go @@ -1,4 +1,3 @@ - package main import ( diff --git a/third_party/github.com/coreos/go-log/log/commands.go b/third_party/github.com/coreos/go-log/log/commands.go index 94dc9e152..f39fdef97 100644 --- a/third_party/github.com/coreos/go-log/log/commands.go +++ b/third_party/github.com/coreos/go-log/log/commands.go @@ -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...) } @@ -158,57 +157,56 @@ func Debugf(format string, v ...interface{}) { // Standard library log functions -func (logger *Logger)Fatalln (v ...interface{}) { +func (logger *Logger) Fatalln(v ...interface{}) { logger.Log(PriCrit, v...) os.Exit(1) } -func (logger *Logger)Fatalf (format string, v ...interface{}) { +func (logger *Logger) Fatalf(format string, v ...interface{}) { logger.Logf(PriCrit, format, v...) os.Exit(1) } -func (logger *Logger)Panicln (v ...interface{}) { +func (logger *Logger) Panicln(v ...interface{}) { s := fmt.Sprint(v...) logger.Log(PriErr, s) panic(s) } -func (logger *Logger)Panicf (format string, v ...interface{}) { +func (logger *Logger) Panicf(format string, v ...interface{}) { s := fmt.Sprintf(format, v...) logger.Log(PriErr, s) panic(s) } -func (logger *Logger)Println (v ...interface{}) { +func (logger *Logger) Println(v ...interface{}) { 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...) } - -func Fatalln (v ...interface{}) { +func Fatalln(v ...interface{}) { defaultLogger.Log(PriCrit, v...) os.Exit(1) } -func Fatalf (format string, v ...interface{}) { +func Fatalf(format string, v ...interface{}) { defaultLogger.Logf(PriCrit, format, v...) os.Exit(1) } -func Panicln (v ...interface{}) { +func Panicln(v ...interface{}) { s := fmt.Sprint(v...) defaultLogger.Log(PriErr, s) panic(s) } -func Panicf (format string, v ...interface{}) { +func Panicf(format string, v ...interface{}) { s := fmt.Sprintf(format, v...) defaultLogger.Log(PriErr, s) panic(s) } -func Println (v ...interface{}) { +func Println(v ...interface{}) { defaultLogger.Log(PriInfo, v...) } -func Printf (format string, v ...interface{}) { +func Printf(format string, v ...interface{}) { defaultLogger.Logf(PriInfo, format, v...) } diff --git a/third_party/github.com/coreos/go-log/log/fields.go b/third_party/github.com/coreos/go-log/log/fields.go index e8d9698a0..b04edc8eb 100644 --- a/third_party/github.com/coreos/go-log/log/fields.go +++ b/third_party/github.com/coreos/go-log/log/fields.go @@ -1,4 +1,5 @@ package log + // Copyright 2013, CoreOS, Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/third_party/github.com/coreos/go-log/log/logger.go b/third_party/github.com/coreos/go-log/log/logger.go index 2089a11f8..8c3b86c1d 100644 --- a/third_party/github.com/coreos/go-log/log/logger.go +++ b/third_party/github.com/coreos/go-log/log/logger.go @@ -1,4 +1,5 @@ package log + // Copyright 2013, CoreOS, Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/third_party/github.com/coreos/go-log/log/priority.go b/third_party/github.com/coreos/go-log/log/priority.go index ac73fc8a4..c169d6869 100644 --- a/third_party/github.com/coreos/go-log/log/priority.go +++ b/third_party/github.com/coreos/go-log/log/priority.go @@ -1,4 +1,5 @@ package log + // Copyright 2013, CoreOS, Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/third_party/github.com/coreos/go-log/log/sinks.go b/third_party/github.com/coreos/go-log/log/sinks.go index a41f3365d..bdf1e41f1 100644 --- a/third_party/github.com/coreos/go-log/log/sinks.go +++ b/third_party/github.com/coreos/go-log/log/sinks.go @@ -1,4 +1,5 @@ package log + // Copyright 2013, CoreOS, Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/third_party/github.com/coreos/go-systemd/activation/files.go b/third_party/github.com/coreos/go-systemd/activation/files.go index 4b8542370..a0a56f9e6 100644 --- a/third_party/github.com/coreos/go-systemd/activation/files.go +++ b/third_party/github.com/coreos/go-systemd/activation/files.go @@ -24,7 +24,7 @@ func Files() []*os.File { files := []*os.File(nil) for fd := listenFdsStart; fd < listenFdsStart+nfds; 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 }