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

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

	"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 15 16 17 18 19
)

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

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

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

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

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

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

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

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

		// 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 已提交
77 78 79 80
		c.taskFinished(t.ID)
	}
}

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

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

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

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

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

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