client.go 2.9 KB
Newer Older
1 2 3
package master

import (
H
Helin Wang 已提交
4
	"os"
5 6

	"github.com/PaddlePaddle/Paddle/go/connection"
H
Helin Wang 已提交
7
	"github.com/PaddlePaddle/recordio"
H
Helin Wang 已提交
8
	log "github.com/sirupsen/logrus"
9 10 11 12 13
)

// Client is the client of the master server.
type Client struct {
	conn *connection.Conn
G
gongweibao 已提交
14 15 16 17 18 19
	ch   chan record
}

type record struct {
	r   []byte
	err error
20 21 22
}

// NewClient creates a new Client.
23 24 25
//
// bufSize is the record buffer size. NextRecord will read from this
// buffer.
26
func NewClient(addrCh <-chan string, bufSize int) *Client {
27 28
	c := &Client{}
	c.conn = connection.New()
G
gongweibao 已提交
29
	c.ch = make(chan record, bufSize)
30
	go c.monitorMaster(addrCh)
H
Helin Wang 已提交
31
	go c.getRecords()
32 33 34
	return c
}

H
Helin Wang 已提交
35 36 37 38
func (c *Client) getRecords() {
	for {
		t, err := c.getTask()
		if err != nil {
H
Helin Wang 已提交
39 40
			// TODO(helin): wait before move on with next
			// getTask call.
H
Helin Wang 已提交
41
			log.Errorln(err)
H
Helin Wang 已提交
42 43 44 45 46 47
			continue
		}

		for _, chunk := range t.Chunks {
			f, err := os.Open(chunk.Path)
			if err != nil {
H
Helin Wang 已提交
48
				log.Errorln(err)
H
Helin Wang 已提交
49 50 51 52 53
				continue
			}

			s := recordio.NewRangeScanner(f, &chunk.Index, -1, -1)
			for s.Scan() {
G
gongweibao 已提交
54
				c.ch <- record{s.Record(), nil}
H
Helin Wang 已提交
55 56
			}

57
			if s.Err() != nil {
G
gongweibao 已提交
58
				c.ch <- record{nil, s.Err()}
H
Helin Wang 已提交
59
				log.Errorln(err, chunk.Path)
60 61
			}

H
Helin Wang 已提交
62 63
			err = f.Close()
			if err != nil {
H
Helin Wang 已提交
64
				log.Errorln(err)
H
Helin Wang 已提交
65 66
			}
		}
67 68 69 70

		// We treat a task as finished whenever the last data
		// instance of the task is read. This is not exactly
		// correct, but a reasonable approximation.
G
gongweibao 已提交
71
		c.taskFinished(t.Meta.ID)
H
Helin Wang 已提交
72 73 74
	}
}

75
func (c *Client) monitorMaster(addrCh <-chan string) {
76
	lastMaster := ""
77
	for curMaster := range addrCh {
H
Helin Wang 已提交
78
		// connect to the new address once address changed.
79 80 81 82
		if curMaster != lastMaster {
			if curMaster == "" {
				err := c.conn.Close()
				if err != nil {
H
Helin Wang 已提交
83
					log.Errorln(err)
84 85 86 87
				}
			} else {
				err := c.conn.Connect(curMaster)
				if err != nil {
H
Helin Wang 已提交
88
					log.Errorln(err)
89 90 91 92 93 94 95 96 97 98 99 100

					// connect to addr failed, set
					// to last known addr in order
					// to retry next time.
					curMaster = lastMaster
				}
			}
		}
		lastMaster = curMaster
	}
}

101 102 103 104 105 106 107 108
// 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)
}

H
Helin Wang 已提交
109 110
// getTask gets a new task from the master server.
func (c *Client) getTask() (Task, error) {
111
	var t Task
112
	err := c.conn.Call("Service.GetTask", 0, &t)
113 114 115 116
	return t, err
}

// TaskFinished tells the master server a task is finished.
H
Helin Wang 已提交
117
func (c *Client) taskFinished(taskID int) error {
118
	return c.conn.Call("Service.TaskFinished", taskID, nil)
119
}
H
Helin Wang 已提交
120

G
gongweibao 已提交
121
// TaskFailed tell the master server as task is failed.
G
gongweibao 已提交
122
func (c *Client) taskFailed(meta TaskMeta) error {
G
gongweibao 已提交
123
	return c.conn.Call("Service.TaskFailed", meta, nil)
G
gongweibao 已提交
124 125
}

H
Helin Wang 已提交
126 127
// NextRecord returns next record in the dataset.
//
H
Helin Wang 已提交
128
// NextRecord will block until the next record is available. It is
H
Helin Wang 已提交
129
// thread-safe.
G
gongweibao 已提交
130 131 132
func (c *Client) NextRecord() ([]byte, error) {
	r := <-c.ch
	return r.r, r.err
H
Helin Wang 已提交
133
}