Example #1
0
func NewTokenMapStream() *TokenMapStream {
	cancel := make(chan struct{})
	ch := make(chan *TokenMap)

	cb := func(path string, value []byte, rev interface{}) error {
		if path != TokensKey {
			return nil
		}

		tokens := &TokenMap{}
		err := json.Unmarshal(value, tokens)
		if err != nil {
			log.Fatalf("Failed to unmarshal token map: %s\n%s",
				err.Error(), string(value))
		}

		select {
		case ch <- tokens:
		case <-cancel:
		}

		return nil
	}

	go metakv.RunObserveChildren(ServiceDir, cb, cancel)

	return &TokenMapStream{
		C:      ch,
		cancel: cancel,
	}
}
Example #2
0
// NewCfgMetaKv returns a CfgMetaKv that reads and stores its single
// configuration file in the metakv.
func NewCfgMetaKv(nodeUUID string) (*CfgMetaKv, error) {
	cfg := &CfgMetaKv{
		prefix:       CfgMetaKvPrefix,
		nodeUUID:     nodeUUID,
		cfgMem:       NewCfgMem(),
		cancelCh:     make(chan struct{}),
		splitEntries: map[string]CfgMetaKvEntry{},
	}

	backoffStartSleepMS := 200
	backoffFactor := float32(1.5)
	backoffMaxSleepMS := 5000

	go ExponentialBackoffLoop("cfg_metakv.RunObserveChildren",
		func() int {
			err := metakv.RunObserveChildren(cfg.prefix, cfg.metaKVCallback,
				cfg.cancelCh)
			if err == nil {
				return -1 // Success, so stop the loop.
			}

			log.Printf("cfg_metakv: RunObserveChildren, err: %v", err)

			return 0 // No progress, so exponential backoff.
		},
		backoffStartSleepMS, backoffFactor, backoffMaxSleepMS)

	return cfg, nil
}
Example #3
0
// NewCfgMetaKv returns a CfgMetaKv that reads and stores its single
// configuration file in the metakv.
func NewCfgMetaKv() (*CfgMetaKv, error) {
	cfg := &CfgMetaKv{
		path:     BASE_CFG_PATH,
		cancelCh: make(chan struct{}),
		cfgMem:   NewCfgMem(),
        uuid:     NewUUID(),
	}
	go func() {
		for {
			err := metakv.RunObserveChildren(cfg.path, cfg.metaKVCallback,
				cfg.cancelCh)
			if err == nil {
				return
			} else {
				log.Printf("metakv notifier failed (%v)", err)
			}
		}
	}()
	return cfg, nil
}
Example #4
0
func NewSettingsManager(supvCmdch MsgChannel,
	supvMsgch MsgChannel, config common.Config) (settingsManager, common.Config, Message) {
	s := settingsManager{
		supvCmdch: supvCmdch,
		supvMsgch: supvMsgch,
		config:    config,
		cancelCh:  make(chan struct{}),
	}

	config, err := common.GetSettingsConfig(config)
	if err != nil {
		return s, nil, &MsgError{
			err: Error{
				category: INDEXER,
				cause:    err,
				severity: FATAL,
			}}
	}

	ncpu := common.SetNumCPUs(config["indexer.settings.max_cpu_percent"].Int())
	logging.Infof("Setting maxcpus = %d", ncpu)

	setBlockPoolSize(nil, config)
	setLogger(config)

	http.HandleFunc("/settings", s.handleSettingsReq)
	http.HandleFunc("/triggerCompaction", s.handleCompactionTrigger)
	go func() {
		for {
			err := metakv.RunObserveChildren("/", s.metaKVCallback, s.cancelCh)
			if err == nil {
				return
			} else {
				logging.Errorf("IndexerSettingsManager: metakv notifier failed (%v)..Restarting", err)
			}
		}
	}()

	indexerConfig := config.SectionConfig("indexer.", true)
	return s, indexerConfig, &MsgSuccess{}
}
Example #5
0
func SetupSettingsNotifier(callb func(Config), cancelCh chan struct{}) {
	metaKvCb := func(path string, val []byte, rev interface{}) error {
		if path == IndexingSettingsMetaPath {
			logging.Infof("New settings received: \n%s", string(val))
			config := SystemConfig.FilterConfig(".settings.")
			config.Update(val)
			callb(config)
		}
		return nil
	}

	go func() {
		for {
			err := metakv.RunObserveChildren(IndexingSettingsMetaDir, metaKvCb, cancelCh)
			if err == nil {
				return
			} else {
				logging.Errorf("Settings metakv notifier failed (%v)..Restarting", err)
			}
		}
	}()

	return
}