예제 #1
0
파일: manager.go 프로젝트: couchbase/cbgt
func (m *CtlMgr) GetCurrentTopology(haveTopologyRev service.Revision,
	cancelCh service.Cancel) (*service.Topology, error) {
	ctlTopology, err :=
		m.ctl.WaitGetTopology(string(haveTopologyRev), cancelCh)
	if err != nil {
		if err != service.ErrCanceled {
			log.Printf("ctl/manager, GetCurrenTopology, haveTopologyRev: %s,"+
				" err: %v", haveTopologyRev, err)
		}

		return nil, err
	}

	rv := &service.Topology{
		Rev:   service.Revision([]byte(ctlTopology.Rev)),
		Nodes: []service.NodeID{},
	}

	for _, ctlNode := range ctlTopology.MemberNodes {
		rv.Nodes = append(rv.Nodes, service.NodeID(ctlNode.UUID))
	}

	// TODO: Need a proper IsBalanced computation.
	rv.IsBalanced =
		len(ctlTopology.PrevWarnings) <= 0 && len(ctlTopology.PrevErrs) <= 0

	for resourceName, resourceWarnings := range ctlTopology.PrevWarnings {
		aggregate := map[string]bool{}
		for _, resourceWarning := range resourceWarnings {
			if strings.HasPrefix(resourceWarning, "could not meet constraints") {
				aggregate["could not meet replication constraints"] = true
			} else {
				aggregate[resourceWarning] = true
			}
		}

		for resourceWarning := range aggregate {
			rv.Messages = append(rv.Messages,
				fmt.Sprintf("warning: resource: %q -- %s",
					resourceName, resourceWarning))
		}
	}

	for _, err := range ctlTopology.PrevErrs {
		rv.Messages = append(rv.Messages, fmt.Sprintf("error: %v", err))
	}

	m.mu.Lock()
	m.lastTopology.Rev = rv.Rev
	same := reflect.DeepEqual(&m.lastTopology, rv)
	m.lastTopology = *rv
	m.mu.Unlock()

	if !same {
		log.Printf("ctl/manager, GetCurrenTopology, haveTopologyRev: %s,"+
			" changed, rv: %+v", haveTopologyRev, rv)
	}

	return rv, nil
}
예제 #2
0
파일: main.go 프로젝트: couchbase/cbft
func MainStart(cfg cbgt.Cfg, uuid string, tags []string, container string,
	weight int, extras, bindHttp, dataDir, staticDir, staticETag, server string,
	register string, mr *cbgt.MsgRing, options map[string]string) (
	*mux.Router, error) {
	if server == "" {
		return nil, fmt.Errorf("error: server URL required (-server)")
	}

	err := InitOptions(options)
	if err != nil {
		return nil, err
	}

	err = InitMossOptions(options)
	if err != nil {
		return nil, err
	}

	err = InitBleveOptions(options)
	if err != nil {
		return nil, err
	}

	if options["logStatsEvery"] != "" {
		logStatsEvery, err := strconv.Atoi(options["logStatsEvery"])
		if err != nil {
			return nil, err
		}
		if logStatsEvery >= 0 {
			cbft.LogEveryNStats = logStatsEvery
		}
	}

	err = cbft.InitResultCacheOptions(options)
	if err != nil {
		return nil, err
	}

	err = cbft.InitBleveResultCacheOptions(options)
	if err != nil {
		return nil, err
	}

	exitCode := MainTool(cfg, uuid, tags, flags, options)
	if exitCode >= 0 {
		os.Exit(exitCode)
	}

	if server != "." && options["startCheckServer"] != "skip" {
		_, err := couchbase.Connect(server)
		if err != nil {
			if !strings.HasPrefix(server, "http://") &&
				!strings.HasPrefix(server, "https://") {
				return nil, fmt.Errorf("error: not a URL, server: %q\n"+
					"  Please check that your -server parameter"+
					" is a valid URL\n"+
					"  (http://HOST:PORT),"+
					" such as \"http://*****:*****@HOST:PORT)",
				server, err, server)
		}
	}

	meh := &MainHandlers{}
	mgr := cbgt.NewManagerEx(cbgt.VERSION, cfg,
		uuid, tags, container, weight,
		extras, bindHttp, dataDir, server, meh, options)
	meh.mgr = mgr

	err = mgr.Start(register)
	if err != nil {
		return nil, err
	}

	var adtSvc *audit.AuditSvc
	if options["cbaudit"] == "true" {
		adtSvc, _ = audit.NewAuditSvc(server)
	}

	router, _, err :=
		cbft.NewRESTRouter(VERSION, mgr, staticDir, staticETag, mr,
			adtSvc)

	// register handlers needed by ns_server
	prefix := mgr.Options()["urlPrefix"]

	router.Handle(prefix+"/api/nsstats", cbft.NewNsStatsHandler(mgr))

	nsStatusHandler, err := cbft.NewNsStatusHandler(mgr, server)
	if err != nil {
		return nil, err
	}
	router.Handle(prefix+"/api/nsstatus", nsStatusHandler)

	nsSearchResultRedirectHandler, err := cbft.NsSearchResultRedirctHandler(mgr)
	if err != nil {
		return nil, err
	}
	router.Handle(prefix+"/api/nsSearchResultRedirect/{pIndexName}/{docID}", nsSearchResultRedirectHandler)

	cbAuthBasicLoginHadler, err := cbft.CBAuthBasicLoginHandler(mgr)
	if err != nil {
		return nil, err
	}
	router.Handle(prefix+"/login", cbAuthBasicLoginHadler)

	// ------------------------------------------------

	tagsMap := mgr.TagsMap()
	if tagsMap != nil && tagsMap["cbauth_service"] {
		dryRun := false
		dryRunV := mgr.Options()["cbauth_service.dryRun"]
		if dryRunV != "" {
			dryRun, err = strconv.ParseBool(dryRunV)
			if err != nil {
				return nil, err
			}
		}

		waitForMemberNodes := 30 // In seconds.
		waitForMemberNodesV := mgr.Options()["cbauth_service.waitForMemberNodes"]
		if waitForMemberNodesV != "" {
			waitForMemberNodes, err = strconv.Atoi(waitForMemberNodesV)
			if err != nil {
				return nil, err
			}
		}

		verbose := DefaultCtlVerbose
		verboseV := mgr.Options()["cbauth_service.verbose"]
		if verboseV != "" {
			verbose, err = strconv.Atoi(verboseV)
			if err != nil {
				return nil, err
			}
		}

		log.Printf("main: cbauth_service ctl starting,"+
			" dryRun: %v, waitForMemberNodes: %d, verbose: %d",
			dryRun, waitForMemberNodes, verbose)

		c, err := ctl.StartCtl(cfg, server, mgr.Options(), ctl.CtlOptions{
			DryRun:             dryRun,
			Verbose:            verbose,
			FavorMinNodes:      false,
			WaitForMemberNodes: waitForMemberNodes,
		})
		if err != nil {
			return nil, fmt.Errorf("main: ctl.StartCtl, err: %v", err)
		}

		nodeInfo := &service.NodeInfo{
			NodeID: service.NodeID(uuid),
		}

		err = cfg.Refresh()
		if err != nil {
			return nil, err
		}

		ctlMgr := ctl.NewCtlMgr(nodeInfo, c)
		if ctlMgr != nil {
			go func() {
				log.Printf("main: cbauth_service registering...")

				err := service.RegisterManager(ctlMgr, nil)
				if err != nil {
					log.Printf("main: cbauth_service register err: %v", err)
					return
				}

				log.Printf("main: cbauth_service registering... done")
			}()
		}
	}

	// ------------------------------------------------

	return router, err
}