提交 44226853 编写于 作者: H Helin Wang

put InMemStore into master package

上级 fd893755
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"net/rpc" "net/rpc"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/namsral/flag" "github.com/namsral/flag"
...@@ -15,26 +14,6 @@ import ( ...@@ -15,26 +14,6 @@ import (
"github.com/PaddlePaddle/Paddle/go/master" "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() { func main() {
port := flag.Int("port", 8080, "port of the master server.") port := flag.Int("port", 8080, "port of the master server.")
...@@ -58,7 +37,7 @@ func main() { ...@@ -58,7 +37,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {
store = &inMemStore{} store = &master.InMemStore{}
} }
s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax) s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax)
......
...@@ -32,19 +32,6 @@ func (a TestAddresser) Address() string { ...@@ -32,19 +32,6 @@ func (a TestAddresser) Address() string {
return string(a) 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) { func TestGetFinishTask(t *testing.T) {
const path = "/tmp/master_client_test_0" const path = "/tmp/master_client_test_0"
...@@ -60,7 +47,7 @@ func TestGetFinishTask(t *testing.T) { ...@@ -60,7 +47,7 @@ func TestGetFinishTask(t *testing.T) {
} }
go func(l net.Listener) { go func(l net.Listener) {
s, err := NewService(&myStore{}, chunkPerTask, time.Second, 1) s, err := NewService(&InMemStore{}, chunkPerTask, time.Second, 1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -15,19 +15,6 @@ import ( ...@@ -15,19 +15,6 @@ import (
"github.com/PaddlePaddle/recordio" "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) { func TestNextRecord(t *testing.T) {
const ( const (
path = "/tmp/master_client_TestFull" path = "/tmp/master_client_TestFull"
...@@ -46,7 +33,7 @@ func TestNextRecord(t *testing.T) { ...@@ -46,7 +33,7 @@ func TestNextRecord(t *testing.T) {
} }
go func(l net.Listener) { 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 { if err != nil {
panic(err) panic(err)
} }
......
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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册