Ejemplo n.º 1
0
func (sc *ServerContext) InitCBGTManager() (base.CbgtContext, error) {

	base.LogTo("DIndex+", "Initializing CBGT")

	couchbaseUrl, err := base.CouchbaseUrlWithAuth(
		*sc.config.ClusterConfig.Server,
		sc.config.ClusterConfig.Username,
		sc.config.ClusterConfig.Password,
		*sc.config.ClusterConfig.Bucket,
	)
	if err != nil {
		return base.CbgtContext{}, err
	}

	uuid, err := cmd.MainUUID(base.IndexTypeSyncGateway, sc.config.ClusterConfig.DataDir)
	if err != nil {
		return base.CbgtContext{}, err
	}

	// this tells CBGT that we are brining a new CBGT node online
	register := "wanted"

	// use the uuid as the bindHttp so that we don't have to make the user
	// configure this, and since as far as the REST Api interface, we'll be using
	// whatever is configured in adminInterface anyway.
	// More info here:
	//   https://github.com/couchbaselabs/cbgt/issues/1
	//   https://github.com/couchbaselabs/cbgt/issues/25
	bindHttp := uuid

	options := map[string]interface{}{
		"keyPrefix": db.KSyncKeyPrefix,
	}

	cfgCb, err := cbgt.NewCfgCBEx(
		couchbaseUrl,
		*sc.config.ClusterConfig.Bucket,
		options,
	)
	if err != nil {
		return base.CbgtContext{}, err
	}

	tags := []string{"feed", "janitor", "pindex", "planner"}
	container := ""
	weight := 1                               // this would allow us to have more pindexes serviced by this node
	server := *sc.config.ClusterConfig.Server // or use "." (don't bother checking)
	extras := ""
	var managerEventHandlers cbgt.ManagerEventHandlers

	// refresh it so we have a fresh copy
	if err := cfgCb.Refresh(); err != nil {
		return base.CbgtContext{}, err
	}

	manager := cbgt.NewManager(
		cbgt.VERSION,
		cfgCb,
		uuid,
		tags,
		container,
		weight,
		extras,
		bindHttp,
		sc.config.ClusterConfig.DataDir,
		server,
		managerEventHandlers,
	)

	err = manager.Start(register)
	if err != nil {
		return base.CbgtContext{}, err
	}

	if err := sc.enableCBGTAutofailover(
		cbgt.VERSION,
		manager,
		cfgCb,
		uuid,
		couchbaseUrl,
		db.KSyncKeyPrefix,
	); err != nil {
		return base.CbgtContext{}, err
	}

	cbgtContext := base.CbgtContext{
		Manager: manager,
		Cfg:     cfgCb,
	}

	return cbgtContext, nil

}
Ejemplo n.º 2
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	go cmd.DumpOnSignalForPlatform()

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfg(cmdName, flags.CfgConnect,
		flags.BindHttp, flags.Register, flags.DataDir)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf(fmt.Sprintf("%v", err))
			return
		}
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	expvars.Set("indexes", bleveHttp.IndexStats())

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extra,
		flags.BindHttp, flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, flags.Options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	log.Printf("main: listening on: %s", flags.BindHttp)
	u := flags.BindHttp
	if u[0] == ':' {
		u = "localhost" + u
	}
	if strings.HasPrefix(u, "0.0.0.0:") {
		u = "localhost" + u[len("0.0.0.0"):]
	}
	log.Printf("------------------------------------------------------------")
	log.Printf("web UI / REST API is available: http://%s", u)
	log.Printf("------------------------------------------------------------")
	err = http.ListenAndServe(flags.BindHttp, nil)
	if err != nil {
		log.Fatalf("main: listen, err: %v\n"+
			"  Please check that your -bindHttp parameter (%q)\n"+
			"  is correct and available.", err, flags.BindHttp)
	}
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)
	log.SetLoggerCallback(LoggerFunc)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	go cmd.DumpOnSignalForPlatform()

	bleve.StoreDynamic = false
	bleve.MappingJSONStrict = true

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	cbft.SetAuthType(flags.AuthType)
	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	// User may supply a comma-separated list of HOST:PORT values for
	// http addresss/port listening, but only the first entry is used
	// for cbgt node and Cfg registration.
	bindHttps := strings.Split(flags.BindHttp, ",")

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfg(cmdName, flags.CfgConnect,
		bindHttps[0], flags.Register, flags.DataDir)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf(fmt.Sprintf("%v", err))
			return
		}
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extras,
		bindHttps[0], flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, flags.Options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	anyHostPorts := map[string]bool{}

	// Bind to 0.0.0.0's first.
	for _, bindHttp := range bindHttps {
		if strings.HasPrefix(bindHttp, "0.0.0.0:") {
			go MainServeHttp(bindHttp, nil)

			anyHostPorts[bindHttp] = true
		}
	}

	for i := len(bindHttps) - 1; i >= 1; i-- {
		go MainServeHttp(bindHttps[i], anyHostPorts)
	}

	MainServeHttp(bindHttps[0], anyHostPorts)

	<-(make(chan struct{})) // Block forever.
}
Ejemplo n.º 4
-1
func main() {
	platform.HideConsole(true)
	defer platform.HideConsole(false)

	flag.Parse()

	if flags.Help {
		flag.Usage()
		os.Exit(2)
	}

	if flags.Version {
		fmt.Printf("%s main: %s, data: %s\n", path.Base(os.Args[0]),
			VERSION, cbgt.VERSION)
		os.Exit(0)
	}

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	mr, err := cbgt.NewMsgRing(os.Stderr, 1000)
	if err != nil {
		log.Fatalf("main: could not create MsgRing, err: %v", err)
	}
	log.SetOutput(mr)
	log.SetLoggerCallback(LoggerFunc)

	log.Printf("main: %s started (%s/%s)",
		os.Args[0], VERSION, cbgt.VERSION)

	rand.Seed(time.Now().UTC().UnixNano())

	// disabling as speculative change for
	// https://issues.couchbase.com/browse/MB-20002
	// theory being that invoking signal.Notify()
	// interferes with forestdb signal handler
	// go cmd.DumpOnSignalForPlatform()

	MainWelcome(flagAliases)

	s, err := os.Stat(flags.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			if flags.DataDir == DEFAULT_DATA_DIR {
				log.Printf("main: creating data directory, dataDir: %s",
					flags.DataDir)
				err = os.Mkdir(flags.DataDir, 0700)
				if err != nil {
					log.Fatalf("main: could not create data directory,"+
						" dataDir: %s, err: %v", flags.DataDir, err)
				}
			} else {
				log.Fatalf("main: data directory does not exist,"+
					" dataDir: %s", flags.DataDir)
				return
			}
		} else {
			log.Fatalf("main: could not access data directory,"+
				" dataDir: %s, err: %v", flags.DataDir, err)
			return
		}
	} else {
		if !s.IsDir() {
			log.Fatalf("main: not a directory, dataDir: %s", flags.DataDir)
			return
		}
	}

	wd, err := os.Getwd()
	if err != nil {
		log.Fatalf("main: os.Getwd, err: %#v", err)
		return
	}
	log.Printf("main: curr dir: %q", wd)

	dataDirAbs, err := filepath.Abs(flags.DataDir)
	if err != nil {
		log.Fatalf("main: filepath.Abs, err: %#v", err)
		return
	}
	log.Printf("main: data dir: %q", dataDirAbs)

	uuid := flags.UUID
	if uuid != "" {
		uuidPath :=
			flags.DataDir + string(os.PathSeparator) + cmdName + ".uuid"

		err = ioutil.WriteFile(uuidPath, []byte(uuid), 0600)
		if err != nil {
			log.Fatalf("main: could not write uuidPath: %s\n"+
				"  Please check that your -data/-dataDir parameter (%q)\n"+
				"  is to a writable directory where %s can persist data.",
				uuidPath, flags.DataDir, cmdName)
			return
		}
	}

	if uuid == "" {
		uuid, err = cmd.MainUUID(cmdName, flags.DataDir)
		if err != nil {
			log.Fatalf("%v", err)
			return
		}
	}

	options := cmd.ParseOptions(flags.Options, "CBFT_ENV_OPTIONS",
		map[string]string{
			"managerLoadDataDir": "async",
			"authType":           flags.AuthType,
		})

	err = InitHttpOptions(options)
	if err != nil {
		log.Fatalf("main: InitHttpOptions, err: %v", err)
		return
	}

	// User may supply a comma-separated list of HOST:PORT values for
	// http addresss/port listening, but only the first http entry
	// is used for cbgt node and Cfg registration.
	bindHttpList := strings.Split(flags.BindHttp, ",")

	// If cfg is down, we error, leaving it to some user-supplied
	// outside watchdog to backoff and restart/retry.
	cfg, err := cmd.MainCfgEx(cmdName, flags.CfgConnect,
		bindHttpList[0], flags.Register, flags.DataDir, uuid, options)
	if err != nil {
		if err == cmd.ErrorBindHttp {
			log.Fatalf("%v", err)
			return
		}
		log.Fatalf("main: could not start cfg, cfgConnect: %s, err: %v\n"+
			"  Please check that your -cfg/-cfgConnect parameter (%q)\n"+
			"  is correct and/or that your configuration provider\n"+
			"  is available.",
			flags.CfgConnect, err, flags.CfgConnect)
		return
	}

	var tagsArr []string
	if flags.Tags != "" {
		tagsArr = strings.Split(flags.Tags, ",")
	}

	router, err := MainStart(cfg, uuid, tagsArr,
		flags.Container, flags.Weight, flags.Extras,
		bindHttpList[0], flags.DataDir,
		flags.StaticDir, flags.StaticETag,
		flags.Server, flags.Register, mr, options)
	if err != nil {
		log.Fatal(err)
	}

	if flags.Register == "unknown" {
		log.Printf("main: unregistered node; now exiting")
		os.Exit(0)
	}

	http.Handle("/", router)

	anyHostPorts := map[string]bool{}

	// Bind to 0.0.0.0's first for http listening.
	for _, bindHttp := range bindHttpList {
		if strings.HasPrefix(bindHttp, "0.0.0.0:") {
			go MainServeHttp("http", bindHttp, nil, "", "")

			anyHostPorts[bindHttp] = true
		}
	}

	for i := len(bindHttpList) - 1; i >= 1; i-- {
		go MainServeHttp("http", bindHttpList[i], anyHostPorts, "", "")
	}

	if flags.BindHttps != "" {
		bindHttpsList := strings.Split(flags.BindHttps, ",")

		// Bind to 0.0.0.0's first for https listening.
		for _, bindHttps := range bindHttpsList {
			if strings.HasPrefix(bindHttps, "0.0.0.0:") {
				go MainServeHttp("https", bindHttps, nil,
					flags.TlsCertFile, flags.TlsKeyFile)

				anyHostPorts[bindHttps] = true
			}
		}

		for _, bindHttps := range bindHttpsList {
			go MainServeHttp("https", bindHttps, anyHostPorts,
				flags.TlsCertFile, flags.TlsKeyFile)
		}
	}

	MainServeHttp("http", bindHttpList[0], anyHostPorts, "", "")

	<-(make(chan struct{})) // Block forever.
}