github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/tidb-server/main.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  	"os"
    20  	"os/signal"
    21  	"runtime"
    22  	"syscall"
    23  	"time"
    24  
    25  	"github.com/insionng/yougam/libraries/juju/errors"
    26  	"github.com/insionng/yougam/libraries/ngaut/log"
    27  	"github.com/insionng/yougam/libraries/pingcap/tidb"
    28  	"github.com/insionng/yougam/libraries/pingcap/tidb/metric"
    29  	"github.com/insionng/yougam/libraries/pingcap/tidb/server"
    30  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/hbase"
    31  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/boltdb"
    32  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/tikv"
    33  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/printer"
    34  )
    35  
    36  var (
    37  	store      = flag.String("store", "goleveldb", "registered store name, [memory, goleveldb, hbase, boltdb, tikv]")
    38  	storePath  = flag.String("path", "/tmp/tidb", "tidb storage path")
    39  	logLevel   = flag.String("L", "debug", "log level: info, debug, warn, error, fatal")
    40  	port       = flag.String("P", "4000", "mp server port")
    41  	statusPort = flag.String("status", "10080", "tidb server status port")
    42  	lease      = flag.Int("lease", 1, "schema lease seconds, very dangerous to change only if you know what you do")
    43  	socket     = flag.String("socket", "", "The socket file to use for connection.")
    44  )
    45  
    46  func main() {
    47  	tidb.RegisterStore("hbase", hbasekv.Driver{})
    48  	tidb.RegisterLocalStore("boltdb", boltdb.Driver{})
    49  	tidb.RegisterStore("tikv", tikv.Driver{})
    50  
    51  	metric.RunMetric(3 * time.Second)
    52  	printer.PrintTiDBInfo()
    53  	runtime.GOMAXPROCS(runtime.NumCPU())
    54  
    55  	flag.Parse()
    56  
    57  	if *lease < 0 {
    58  		log.Fatalf("invalid lease seconds %d", *lease)
    59  	}
    60  
    61  	tidb.SetSchemaLease(time.Duration(*lease) * time.Second)
    62  
    63  	cfg := &server.Config{
    64  		Addr:       fmt.Sprintf(":%s", *port),
    65  		LogLevel:   *logLevel,
    66  		StatusAddr: fmt.Sprintf(":%s", *statusPort),
    67  		Socket:     *socket,
    68  	}
    69  
    70  	log.SetLevelByString(cfg.LogLevel)
    71  	store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *store, *storePath))
    72  	if err != nil {
    73  		log.Fatal(errors.ErrorStack(err))
    74  	}
    75  	// Create a session to load information schema.
    76  	se, err := tidb.CreateSession(store)
    77  	if err != nil {
    78  		log.Fatal(errors.ErrorStack(err))
    79  	}
    80  	se.Close()
    81  
    82  	var driver server.IDriver
    83  	driver = server.NewTiDBDriver(store)
    84  	var svr *server.Server
    85  	svr, err = server.NewServer(cfg, driver)
    86  	if err != nil {
    87  		log.Fatal(errors.ErrorStack(err))
    88  	}
    89  
    90  	sc := make(chan os.Signal, 1)
    91  	signal.Notify(sc,
    92  		syscall.SIGHUP,
    93  		syscall.SIGINT,
    94  		syscall.SIGTERM,
    95  		syscall.SIGQUIT)
    96  
    97  	go func() {
    98  		sig := <-sc
    99  		log.Infof("Got signal [%d] to exit.", sig)
   100  		svr.Close()
   101  		os.Exit(0)
   102  	}()
   103  
   104  	log.Error(svr.Run())
   105  }