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

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

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

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

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

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

H
Helin Wang 已提交
36 37 38 39
func (c *Client) getRecords() {
	for {
		t, err := c.getTask()
		if err != nil {
H
Helin Wang 已提交
40
			// getTask call.
41 42
			log.Errorf("Get task failed, sleep 3 seconds and continue, %s", err)
			time.Sleep(3 * time.Second)
H
Helin Wang 已提交
43 44 45 46 47 48
			continue
		}

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

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

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

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

		// 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.
H
Helin Wang 已提交
72 73 74 75
		err = c.taskFinished(t.Meta.ID)
		if err != nil {
			log.Errorln(err)
		}
H
Helin Wang 已提交
76 77 78
	}
}

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

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

105 106 107 108 109 110 111 112
// 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 已提交
113 114
// getTask gets a new task from the master server.
func (c *Client) getTask() (Task, error) {
115
	var t Task
116
	err := c.conn.Call("Service.GetTask", 0, &t)
117 118 119 120
	return t, err
}

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

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

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