-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement master server client, remove unnecessary dummy variable #2429
Changes from all commits
f05649a
72a73ab
54e8263
41af738
9bd74dd
f6148eb
0bebaa0
102e10a
13867a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package connection | |
|
||
import ( | ||
"errors" | ||
"log" | ||
"net/rpc" | ||
"sync" | ||
) | ||
|
@@ -21,6 +22,18 @@ func New() *Conn { | |
return c | ||
} | ||
|
||
// Close closes the connection. | ||
func (c *Conn) Close() error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if c.client == nil { | ||
return nil | ||
} | ||
|
||
return c.client.Close() | ||
} | ||
|
||
// Connect connects the connection to a address. | ||
func (c *Conn) Connect(addr string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有一个疑问没有看懂:connect的过程为何不能用一次锁,而要分为两次?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 接下来的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 明白了。谢谢! |
||
c.mu.Lock() | ||
|
@@ -50,12 +63,20 @@ func (c *Conn) Connect(addr string) error { | |
c.waitConn = nil | ||
} | ||
} else { | ||
err := client.Close() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
return errors.New("client already set from a concurrent goroutine") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// TODO(helin): refactor Call to be able to perform given retry | ||
// policy. | ||
|
||
// Call make a RPC call. | ||
// | ||
// Call will be blocked until the connection to remote RPC service | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package master | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/PaddlePaddle/Paddle/go/connection" | ||
) | ||
|
||
// Addresser provide the address of the master server. | ||
type Addresser interface { | ||
Address() string | ||
} | ||
|
||
// Client is the client of the master server. | ||
type Client struct { | ||
conn *connection.Conn | ||
} | ||
|
||
// NewClient creates a new Client. | ||
func NewClient(addr Addresser) *Client { | ||
c := &Client{} | ||
c.conn = connection.New() | ||
go c.monitorMaster(addr) | ||
return c | ||
} | ||
|
||
func (c *Client) monitorMaster(addr Addresser) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个地方有些疑惑请教一下:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Done.
|
||
lastMaster := "" | ||
monitor := func() { | ||
// get the lastest address of the master server, | ||
// connect to the new address once address changed. | ||
curMaster := addr.Address() | ||
if curMaster != lastMaster { | ||
if curMaster == "" { | ||
err := c.conn.Close() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} else { | ||
err := c.conn.Connect(curMaster) | ||
if err != nil { | ||
log.Println(err) | ||
|
||
// connect to addr failed, set | ||
// to last known addr in order | ||
// to retry next time. | ||
curMaster = lastMaster | ||
} | ||
|
||
} | ||
} | ||
|
||
lastMaster = curMaster | ||
} | ||
|
||
monitor() | ||
ticker := time.NewTicker(10 * time.Second) | ||
for _ = range ticker.C { | ||
monitor() | ||
} | ||
} | ||
|
||
// SetDataset set dataset for the master server to dispatch. | ||
// | ||
// SetDataset can be call multiple times from different nodes. But | ||
// only the first call will be honored. | ||
func (c *Client) SetDataset(globPaths []string) error { | ||
return c.conn.Call("Service.SetDataset", globPaths, nil) | ||
} | ||
|
||
// GetTask gets a new task from the master server. | ||
func (c *Client) GetTask() (Task, error) { | ||
var t Task | ||
err := c.conn.Call("Service.GetTask", 0, &t) | ||
return t, err | ||
} | ||
|
||
// TaskFinished tells the master server a task is finished. | ||
func (c *Client) TaskFinished(taskID int) error { | ||
return c.conn.Call("Service.TaskFinished", taskID, nil) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package master_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/rpc" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/PaddlePaddle/Paddle/go/master" | ||
"github.com/PaddlePaddle/recordio" | ||
) | ||
|
||
const ( | ||
totalTask = 20 | ||
chunkPerTask = 10 | ||
) | ||
|
||
var port int | ||
|
||
func init() { | ||
log.SetLevel(log.ErrorLevel) | ||
|
||
l, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ss := strings.Split(l.Addr().String(), ":") | ||
p, err := strconv.Atoi(ss[len(ss)-1]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
port = p | ||
|
||
go func(l net.Listener) { | ||
s := master.NewService(chunkPerTask, time.Second, 1) | ||
server := rpc.NewServer() | ||
err := server.Register(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(rpc.DefaultRPCPath, server) | ||
err = http.Serve(l, mux) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}(l) | ||
} | ||
|
||
type addresser string | ||
|
||
func (a addresser) Address() string { | ||
return string(a) | ||
} | ||
|
||
func TestClientFull(t *testing.T) { | ||
const p = "/tmp/master_client_test_0" | ||
f, err := os.Create(p) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for i := 0; i < totalTask*chunkPerTask; i++ { | ||
w := recordio.NewWriter(f, -1, -1) | ||
w.Write(nil) | ||
// call Close to force RecordIO writing a chunk. | ||
w.Close() | ||
} | ||
f.Close() | ||
|
||
c := master.NewClient(addresser(fmt.Sprintf(":%d", port))) | ||
c.SetDataset([]string{p}) | ||
|
||
checkOnePass := func(i int) { | ||
var tasks []master.Task | ||
for i := 0; i < totalTask; i++ { | ||
task, err := c.GetTask() | ||
if err != nil { | ||
t.Fatal(i, err) | ||
} | ||
tasks = append(tasks, task) | ||
} | ||
|
||
_, err = c.GetTask() | ||
if err == nil { | ||
t.Fatal(i, "should get error.") | ||
} | ||
|
||
err = c.TaskFinished(tasks[0].ID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tasks = tasks[1:] | ||
task, err := c.GetTask() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tasks = append(tasks, task) | ||
|
||
for _, task := range tasks { | ||
err = c.TaskFinished(task.ID) | ||
if err != nil { | ||
t.Fatal(i, err) | ||
} | ||
} | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
checkOnePass(i) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will we follow wangyi's suggestion here? a strategy that used to avoid port occupied. server listen on port number of "0", then the operate system(k8s) will assign a idle port, program can query for that port.
I'm not familiar with k8s, is it have different port namespace in each pod? I have googled, but still not sure about that. if so, please ignore this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dzhwinter That strategy of using 0 for port is mostly used for unit tests when we don't have control over the testing environment. E.g., https://github.com/PaddlePaddle/Paddle/blob/develop/go/pserver/client_test.go#L20
For non-container based production use, admin will know which port is free. And if we are using container (k8s is based on container technology) this is not a problem, because typically there is only one program running inside the container, so all ports not reserved by OS will be free.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get it. Thanks!