From 44226853029119e195530e78ff7d0ab883b72dff Mon Sep 17 00:00:00 2001 From: Helin Wang Date: Wed, 21 Jun 2017 18:55:49 +0000 Subject: [PATCH] put InMemStore into master package --- go/cmd/master/master.go | 23 +---------------------- go/master/client_internal_test.go | 15 +-------------- go/master/client_test.go | 15 +-------------- go/master/inmem_store.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 50 deletions(-) create mode 100644 go/master/inmem_store.go diff --git a/go/cmd/master/master.go b/go/cmd/master/master.go index 49ad0300b8..48fe2e6f75 100644 --- a/go/cmd/master/master.go +++ b/go/cmd/master/master.go @@ -6,7 +6,6 @@ import ( "net/rpc" "strconv" "strings" - "sync" "time" "github.com/namsral/flag" @@ -15,26 +14,6 @@ import ( "github.com/PaddlePaddle/Paddle/go/master" ) -type inMemStore struct { - mu sync.Mutex - buf []byte -} - -func (m *inMemStore) Save(b []byte) error { - m.mu.Lock() - defer m.mu.Unlock() - - m.buf = b - return nil -} - -func (m *inMemStore) Load() ([]byte, error) { - m.mu.Lock() - defer m.mu.Unlock() - - return m.buf, nil -} - func main() { port := flag.Int("port", 8080, "port of the master server.") @@ -58,7 +37,7 @@ func main() { log.Fatal(err) } } else { - store = &inMemStore{} + store = &master.InMemStore{} } s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax) diff --git a/go/master/client_internal_test.go b/go/master/client_internal_test.go index a5b76fe853..251225780a 100644 --- a/go/master/client_internal_test.go +++ b/go/master/client_internal_test.go @@ -32,19 +32,6 @@ func (a TestAddresser) Address() string { return string(a) } -type myStore struct { - buf []byte -} - -func (m *myStore) Save(b []byte) error { - m.buf = b - return nil -} - -func (m *myStore) Load() ([]byte, error) { - return m.buf, nil -} - func TestGetFinishTask(t *testing.T) { const path = "/tmp/master_client_test_0" @@ -60,7 +47,7 @@ func TestGetFinishTask(t *testing.T) { } go func(l net.Listener) { - s, err := NewService(&myStore{}, chunkPerTask, time.Second, 1) + s, err := NewService(&InMemStore{}, chunkPerTask, time.Second, 1) if err != nil { panic(err) } diff --git a/go/master/client_test.go b/go/master/client_test.go index ae5f17c2d4..85a86761c2 100644 --- a/go/master/client_test.go +++ b/go/master/client_test.go @@ -15,19 +15,6 @@ import ( "github.com/PaddlePaddle/recordio" ) -type myStore struct { - buf []byte -} - -func (m *myStore) Save(b []byte) error { - m.buf = b - return nil -} - -func (m *myStore) Load() ([]byte, error) { - return m.buf, nil -} - func TestNextRecord(t *testing.T) { const ( path = "/tmp/master_client_TestFull" @@ -46,7 +33,7 @@ func TestNextRecord(t *testing.T) { } go func(l net.Listener) { - s, err := master.NewService(&myStore{}, 10, time.Second, 1) + s, err := master.NewService(&master.InMemStore{}, 10, time.Second, 1) if err != nil { panic(err) } diff --git a/go/master/inmem_store.go b/go/master/inmem_store.go new file mode 100644 index 0000000000..bcd549b20e --- /dev/null +++ b/go/master/inmem_store.go @@ -0,0 +1,28 @@ +package master + +import "sync" + +// InMemStore is an in memory implementation of Store interface. +// +// It does not tolerate the fault that casues the program to crash. +type InMemStore struct { + mu sync.Mutex + buf []byte +} + +// Save saves the state into the in-memory store. +func (m *InMemStore) Save(state []byte) error { + m.mu.Lock() + defer m.mu.Unlock() + + m.buf = state + return nil +} + +// Load loads the state from the in-memory store. +func (m *InMemStore) Load() ([]byte, error) { + m.mu.Lock() + defer m.mu.Unlock() + + return m.buf, nil +} -- GitLab