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

import (
G
gongweibao 已提交
4
	"io"
H
Helin Wang 已提交
5
	"os"
6 7 8
	"time"

	"github.com/PaddlePaddle/Paddle/go/connection"
H
Helin Wang 已提交
9
	"github.com/PaddlePaddle/recordio"
H
Helin Wang 已提交
10
	log "github.com/sirupsen/logrus"
11 12 13 14 15 16 17 18 19 20
)

// 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
G
gongweibao 已提交
21 22 23 24 25 26
	ch   chan record
}

type record struct {
	r   []byte
	err error
27 28 29
}

// NewClient creates a new Client.
30 31 32 33
//
// bufSize is the record buffer size. NextRecord will read from this
// buffer.
func NewClient(addr Addresser, bufSize int) *Client {
34 35
	c := &Client{}
	c.conn = connection.New()
G
gongweibao 已提交
36
	c.ch = make(chan record, bufSize)
37
	go c.monitorMaster(addr)
H
Helin Wang 已提交
38
	go c.getRecords()
39 40 41
	return c
}

H
Helin Wang 已提交
42 43 44 45
func (c *Client) getRecords() {
	for {
		t, err := c.getTask()
		if err != nil {
H
Helin Wang 已提交
46 47
			// TODO(helin): wait before move on with next
			// getTask call.
H
Helin Wang 已提交
48
			log.Errorln(err)
H
Helin Wang 已提交
49 50 51 52 53 54
			continue
		}

		for _, chunk := range t.Chunks {
			f, err := os.Open(chunk.Path)
			if err != nil {
H
Helin Wang 已提交
55
				log.Errorln(err)
H
Helin Wang 已提交
56 57 58 59 60
				continue
			}

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

64
			if s.Err() != nil {
G
gongweibao 已提交
65
				c.ch <- record{nil, s.Err()}
H
Helin Wang 已提交
66
				log.Errorln(err, chunk.Path)
67 68
			}

H
Helin Wang 已提交
69 70
			err = f.Close()
			if err != nil {
H
Helin Wang 已提交
71
				log.Errorln(err)
H
Helin Wang 已提交
72
			}
G
gongweibao 已提交
73 74

			c.ch <- record{nil, io.EOF}
H
Helin Wang 已提交
75
		}
76 77 78 79

		// 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 已提交
80 81 82 83
		c.taskFinished(t.ID)
	}
}

84 85 86
func (c *Client) monitorMaster(addr Addresser) {
	lastMaster := ""
	monitor := func() {
H
Helin Wang 已提交
87 88
		// get the lastest address of the master server,
		// connect to the new address once address changed.
89 90 91 92 93
		curMaster := addr.Address()
		if curMaster != lastMaster {
			if curMaster == "" {
				err := c.conn.Close()
				if err != nil {
H
Helin Wang 已提交
94
					log.Errorln(err)
95 96 97 98
				}
			} else {
				err := c.conn.Connect(curMaster)
				if err != nil {
H
Helin Wang 已提交
99
					log.Errorln(err)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

					// 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()
	}
}

120 121 122 123
// 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.
G
gongweibao 已提交
124
func (c *Client) SetDataset(globPaths []string) error {
125 126 127
	return c.conn.Call("Service.SetDataset", globPaths, nil)
}

H
Helin Wang 已提交
128 129
// getTask gets a new task from the master server.
func (c *Client) getTask() (Task, error) {
130
	var t Task
131
	err := c.conn.Call("Service.GetTask", 0, &t)
132 133 134 135
	return t, err
}

// TaskFinished tells the master server a task is finished.
H
Helin Wang 已提交
136
func (c *Client) taskFinished(taskID int) error {
137
	return c.conn.Call("Service.TaskFinished", taskID, nil)
138
}
H
Helin Wang 已提交
139 140 141

// NextRecord returns next record in the dataset.
//
H
Helin Wang 已提交
142
// NextRecord will block until the next record is available. It is
H
Helin Wang 已提交
143
// thread-safe.
G
gongweibao 已提交
144 145 146
func (c *Client) NextRecord() ([]byte, error) {
	r := <-c.ch
	return r.r, r.err
H
Helin Wang 已提交
147
}