master.go 1.7 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"
H
Helin Wang 已提交
14

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

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

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, ",")
35 36 37 38 39 40 41
		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)
42 43 44 45
		if err != nil {
			log.Fatal(err)
		}
	} else {
H
Helin Wang 已提交
46
		store = &master.InMemStore{}
47
	}
H
Helin Wang 已提交
48

49 50 51
	s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax)
	if err != nil {
		log.Fatal(err)
H
Helin Wang 已提交
52 53
	}

54
	err = rpc.Register(s)
H
Helin Wang 已提交
55
	if err != nil {
56
		log.Fatal(err)
H
Helin Wang 已提交
57 58 59 60 61
	}

	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if err != nil {
62
		log.Fatal(err)
H
Helin Wang 已提交
63 64 65 66
	}

	err = http.Serve(l, nil)
	if err != nil {
67
		log.Fatal(err)
H
Helin Wang 已提交
68 69
	}
}