master.go 1.5 KB
Newer Older
H
Helin Wang 已提交
1 2 3 4 5 6 7
package main

import (
	"net"
	"net/http"
	"net/rpc"
	"strconv"
8
	"strings"
H
Helin Wang 已提交
9 10
	"time"

H
Helin Wang 已提交
11
	"github.com/namsral/flag"
12
	log "github.com/sirupsen/logrus"
H
Helin Wang 已提交
13

14
	"github.com/PaddlePaddle/Paddle/go/master"
H
Helin Wang 已提交
15 16 17
)

func main() {
H
Helin Wang 已提交
18
	port := flag.Int("port", 8080, "port of the master server.")
19

20 21
	ttlSec := flag.Int("ttl", 60, "etcd lease TTL in seconds.")
	endpoints := flag.String("endpoints", "", "comma separated etcd endpoints. If empty, fault tolerance will not be enabled.")
H
Helin Wang 已提交
22 23 24
	taskTimeoutDur := flag.Duration("task_timout_dur", 20*time.Minute, "task timout duration.")
	taskTimeoutMax := flag.Int("task_timeout_max", 3, "max timtout count for each task before it being declared failed task.")
	chunkPerTask := flag.Int("chunk_per_task", 10, "chunk per task.")
H
Helin Wang 已提交
25 26
	flag.Parse()

27 28 29 30 31 32 33 34
	if *endpoints == "" {
		log.Warningln("-endpoints not set, fault tolerance not be enabled.")
	}

	var store master.Store
	if *endpoints != "" {
		eps := strings.Split(*endpoints, ",")
		var err error
H
Helin Wang 已提交
35
		store, err = master.NewEtcd(eps, master.DefaultLockPath, master.DefaultStatePath, *ttlSec)
36 37 38 39
		if err != nil {
			log.Fatal(err)
		}
	} else {
H
Helin Wang 已提交
40
		store = &master.InMemStore{}
41
	}
H
Helin Wang 已提交
42

43 44 45
	s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax)
	if err != nil {
		log.Fatal(err)
H
Helin Wang 已提交
46 47
	}

48
	err = rpc.Register(s)
H
Helin Wang 已提交
49
	if err != nil {
50
		log.Fatal(err)
H
Helin Wang 已提交
51 52 53 54 55
	}

	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if err != nil {
56
		log.Fatal(err)
H
Helin Wang 已提交
57 58 59 60
	}

	err = http.Serve(l, nil)
	if err != nil {
61
		log.Fatal(err)
H
Helin Wang 已提交
62 63
	}
}