master.go 2.5 KB
Newer Older
D
dongzhihong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

H
Helin Wang 已提交
15 16 17
package main

import (
18
	"fmt"
H
Helin Wang 已提交
19 20 21 22
	"net"
	"net/http"
	"net/rpc"
	"strconv"
23
	"strings"
H
Helin Wang 已提交
24 25
	"time"

H
Helin Wang 已提交
26
	"github.com/namsral/flag"
27
	log "github.com/sirupsen/logrus"
28
	"github.com/topicai/candy"
H
Helin Wang 已提交
29

30
	"github.com/PaddlePaddle/Paddle/go/master"
31
	"github.com/PaddlePaddle/Paddle/go/utils/networkhelper"
H
Helin Wang 已提交
32 33 34
)

func main() {
H
Helin Wang 已提交
35
	port := flag.Int("port", 8080, "port of the master server.")
36
	ttlSec := flag.Int("ttl", 60, "etcd lease TTL in seconds.")
37
	endpoints := flag.String("endpoints", "http://127.0.0.1:2379", "comma separated etcd endpoints. If empty, fault tolerance will not be enabled.")
38 39 40 41 42
	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 已提交
43 44
	flag.Parse()

45 46 47 48 49
	level, e := log.ParseLevel(*logLevel)
	candy.Must(e)

	log.SetLevel(level)

50 51 52 53 54 55 56
	if *endpoints == "" {
		log.Warningln("-endpoints not set, fault tolerance not be enabled.")
	}

	var store master.Store
	if *endpoints != "" {
		eps := strings.Split(*endpoints, ",")
57 58 59 60 61 62 63
		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)
64 65 66 67
		if err != nil {
			log.Fatal(err)
		}
	} else {
H
Helin Wang 已提交
68
		store = &master.InMemStore{}
69
	}
H
Helin Wang 已提交
70

71 72 73
	s, err := master.NewService(store, *chunkPerTask, *taskTimeoutDur, *taskTimeoutMax)
	if err != nil {
		log.Fatal(err)
H
Helin Wang 已提交
74 75
	}

76
	err = rpc.Register(s)
H
Helin Wang 已提交
77
	if err != nil {
78
		log.Fatal(err)
H
Helin Wang 已提交
79 80 81 82 83
	}

	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if err != nil {
84
		log.Fatal(err)
H
Helin Wang 已提交
85 86 87 88
	}

	err = http.Serve(l, nil)
	if err != nil {
89
		log.Fatal(err)
H
Helin Wang 已提交
90 91
	}
}