func (back *zkConfig) reregisterWatch(path string, logger log.Logger) error { logger = log.NewContext(logger).With(logkey.ZkPath, path) logger.Log("Reregistering watch") _, _, _, err := back.conn.ExistsW(path) if err != nil { logger.Log(log.Err, err, "Unable to reregistering watch") return errors.Annotatef(err, "unable to reregister watch for node %s", path) } return nil }
func (back *zkConfig) Watch(key string, callback backingCallbackFunction) error { back.rootLogger.Log(logkey.ZkMethod, "watch", logkey.ZkPath, key) path := back.configPath(key) _, _, _, err := back.conn.ExistsW(path) if err != nil { return errors.Annotatef(err, "cannot watch path %s", path) } back.callbacks.add(path, callback) return nil }
// Ini creates a backing config reader that reads properties from an Ini file func Ini(filename string) (Reader, error) { file, err := ini.LoadFile(filename) if err != nil { return nil, errors.Annotatef(err, "Unable to open file %s", filename) } return &propertyFileDisco{ filename: filename, file: file, }, nil }
// Get returns the config value from zookeeper func (back *zkConfig) Get(key string) ([]byte, error) { back.rootLogger.Log(logkey.ZkMethod, "get", logkey.ZkPath, key) pathToFetch := back.configPath(key) bytes, _, _, err := back.conn.GetW(pathToFetch) if err != nil { if err == zk.ErrNoNode { return nil, nil } return nil, errors.Annotatef(err, "cannot load zk node %s", pathToFetch) } return bytes, nil }
func (back *zkConfig) Write(key string, value []byte) error { back.rootLogger.Log(logkey.ZkMethod, "write", logkey.ZkPath, key) path := back.configPath(key) exists, stat, err := back.conn.Exists(path) if err != nil { return err } if !exists { if value == nil { return nil } _, err := back.conn.Create(path, value, 0, zk.WorldACL(zk.PermAll)) return errors.Annotatef(err, "cannot create path %s", path) } if value == nil { err = back.conn.Delete(path, stat.Version) } else { stat, err = back.conn.Set(path, value, stat.Version) } return errors.Annotatef(err, "cannot change path %s", path) }
// Update the content of this config variable to newValue. func (c *intConf) Update(newValue []byte) error { c.mutex.Lock() defer c.mutex.Unlock() oldValue := c.Get() if newValue == nil { atomic.StoreInt64(&c.currentVal, c.defaultVal) } else { newValueInt, err := strconv.ParseInt(string(newValue), 10, 64) if err != nil { return errors.Annotatef(err, "Unparsable float %s", string(newValue)) } atomic.StoreInt64(&c.currentVal, newValueInt) } if oldValue != c.Get() { for _, w := range c.watches { w(&c.Int, oldValue) } } return nil }
// Update the content of this config variable to newValue. func (c *floatConf) Update(newValue []byte) error { c.mutex.Lock() defer c.mutex.Unlock() oldValue := c.Get() if newValue == nil { atomic.StoreUint64(&c.currentVal, math.Float64bits(c.defaultVal)) } else { newValueFloat, err := strconv.ParseFloat(string(newValue), 64) if err != nil { return errors.Annotatef(err, "unable to parse float %s", newValue) } atomic.StoreUint64(&c.currentVal, math.Float64bits(newValueFloat)) } if oldValue != c.Get() { for _, w := range c.watches { w(&c.Float, oldValue) } } return nil }