master.go 1.9 KB
Newer Older
H
Helin Wang 已提交
1 2 3
package main

import (
4
	"fmt"
H
Helin Wang 已提交
5 6 7 8
	"net"
	"net/http"
	"net/rpc"
	"strconv"
9
	"strings"
H
Helin Wang 已提交
10 11
	"time"

H
Helin Wang 已提交
12
	"github.com/namsral/flag"
13
	log "github.com/sirupsen/logrus"
14
	"github.com/topicai/candy"
H
Helin Wang 已提交
15

16
	"github.com/PaddlePaddle/Paddle/go/master"
17
	"github.com/PaddlePaddle/Paddle/go/utils/networkhelper"
H
Helin Wang 已提交
18 19 20
)

func main() {
H
Helin Wang 已提交
21
	port := flag.Int("port", 8080, "port of the master server.")
22
	ttlSec := flag.Int("ttl", 60, "etcd lease TTL in seconds.")
23
	endpoints := flag.String("endpoints", "http://127.0.0.1:2379", "comma separated etcd endpoints. If empty, fault tolerance will not be enabled.")
24 25 26 27 28
	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.")
	logLevel := flag.String("log-level", "info",
		"log level, possible values: debug, info, warning, error, fatal, panic")
H
Helin Wang 已提交
29 30
	flag.Parse()

31 32 33 34 35
	level, e := log.ParseLevel(*logLevel)
	candy.Must(e)

	log.SetLevel(level)

36 37 38 39 40 41 42
	if *endpoints == "" {
		log.Warningln("-endpoints not set, fault tolerance not be enabled.")
	}

	var store master.Store
	if *endpoints != "" {
		eps := strings.Split(*endpoints, ",")
43 44 45 46 47 48 49
		ip, err := networkhelper.GetExternalIP()
		if err != nil {
			log.Fatal(err)
		}

		addr := fmt.Sprintf("%s:%d", ip, *port)
		store, err = master.NewEtcdClient(eps, addr, master.DefaultLockPath, master.DefaultAddrPath, master.DefaultStatePath, *ttlSec)
50 51 52 53
		if err != nil {
			log.Fatal(err)
		}
	} else {
H
Helin Wang 已提交
54
		store = &master.InMemStore{}
55
	}
H
Helin Wang 已提交
56

57 58 59
	s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax)
	if err != nil {
		log.Fatal(err)
H
Helin Wang 已提交
60 61
	}

62
	err = rpc.Register(s)
H
Helin Wang 已提交
63
	if err != nil {
64
		log.Fatal(err)
H
Helin Wang 已提交
65 66 67 68 69
	}

	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if err != nil {
70
		log.Fatal(err)
H
Helin Wang 已提交
71 72 73 74
	}

	err = http.Serve(l, nil)
	if err != nil {
75
		log.Fatal(err)
H
Helin Wang 已提交
76 77
	}
}