Merge branch 'master' of ops:nuevo-pilosa

This commit is contained in:
Todd Gruben 2013-12-18 20:15:06 -06:00
commit 637c758858
4 changed files with 87 additions and 19 deletions

View file

@ -11,23 +11,16 @@ import (
)
type Config struct {
config map[string]interface{}
lock sync.RWMutex
loaded bool
config map[string]interface{}
lock sync.RWMutex
filename string
loaded bool
}
var config *Config
func init() {
config_file := os.Getenv("PILOSA_CONFIG")
if config_file == "" {
config_file = "pilosa.yaml"
}
var err error
config, err = NewConfig(config_file)
if err != nil {
log.Fatal(err)
}
config = NewConfig("")
}
func GetSafe(key string) (interface{}, bool) {
@ -46,18 +39,38 @@ func GetString(key string) string {
return config.GetString(key)
}
func NewConfig(filename string) (*Config, error) {
func NewConfig(filename string) *Config {
self := Config{}
self.config = make(map[string]interface{})
data, err := ioutil.ReadFile(filename)
self.filename = filename
return &self
}
func (self *Config) load() error {
self.lock.Lock()
defer self.lock.Unlock()
if self.loaded {
return nil
}
config_file := self.filename
if config_file == "" {
config_file = os.Getenv("PILOSA_CONFIG")
if config_file == "" {
log.Println("PILOSA_CONFIG not set, defaulting to pilosa.yaml")
config_file = "pilosa.yaml"
}
}
data, err := ioutil.ReadFile(config_file)
if err != nil {
return nil, errors.New("Problem with config file: " + err.Error())
return errors.New("Problem with config file: " + err.Error())
}
goyaml.Unmarshal(data, self.config)
return &self, nil
self.loaded = true
return nil
}
func (self *Config) GetSafe(key string) (interface{}, bool) {
self.load()
self.lock.RLock()
defer self.lock.RUnlock()
value, ok := self.config[key]
@ -65,6 +78,7 @@ func (self *Config) GetSafe(key string) (interface{}, bool) {
}
func (self *Config) Get(key string) interface{} {
self.load()
self.lock.RLock()
defer self.lock.RUnlock()
return self.config[key]

View file

@ -34,3 +34,33 @@ func TestConfig(t *testing.T) {
So(GetString("temp"), ShouldEqual, "/tmp")
})
}
func TestConfigObject(t *testing.T) {
err := os.Setenv("PILOSA_CONFIG", "")
if err != nil {
t.Fatal("Error setting PILOSA_CONFIG")
}
conf := NewConfig("test.yaml")
Convey("config.Get()", t, func() {
So(conf.Get("port_tcp"), ShouldEqual, 12000)
So(conf.Get("port_http"), ShouldEqual, 15000)
So(conf.Get("temp"), ShouldEqual, "/tmp")
So(conf.Get("notfound"), ShouldBeNil)
})
Convey("config.GetSafe()", t, func() {
val, ok := conf.GetSafe("port_tcp")
So(ok, ShouldBeTrue)
So(val, ShouldEqual, 12000)
val, ok = conf.GetSafe("derp")
So(ok, ShouldBeFalse)
So(val, ShouldBeNil)
})
Convey("config.GetInt()", t, func() {
So(conf.GetInt("port_tcp"), ShouldEqual, 12000)
So(conf.GetInt("port_http"), ShouldEqual, 15000)
So(conf.GetInt("notfound"), ShouldEqual, 0)
})
Convey("config.GetString()", t, func() {
So(conf.GetString("temp"), ShouldEqual, "/tmp")
})
}

View file

@ -1,17 +1,21 @@
package core
import (
"github.com/coreos/go-etcd/etcd"
"log"
"os"
"os/signal"
"pilosa/config"
"pilosa/db"
"pilosa/interfaces"
"syscall"
"github.com/nu7hatch/gouuid"
"github.com/coreos/go-etcd/etcd"
)
type Service struct {
Stopper
Id *uuid.UUID
Etcd *etcd.Client
Cluster *db.Cluster
TopologyMapper *TopologyMapper
@ -23,6 +27,7 @@ type Service struct {
func NewService() *Service {
service := new(Service)
service.init_id()
service.Etcd = etcd.NewClient(nil)
service.Cluster = db.NewCluster()
service.TopologyMapper = &TopologyMapper{service, "/pilosa/0"}
@ -30,6 +35,25 @@ func NewService() *Service {
return service
}
func (service *Service) init_id() {
var id *uuid.UUID
var err error
id_string := config.GetString("id")
if id_string == "" {
log.Println("Service id not configured, generating...")
id, err = uuid.NewV4()
if err != nil {
log.Fatal("problem generating uuid")
}
} else {
id, err = uuid.ParseHex(id_string)
if err != nil {
log.Fatalf("Service id '%s' not valid", id_string)
}
}
service.Id = id
}
func (service *Service) GetSignals() (chan os.Signal, chan os.Signal) {
hupChan := make(chan os.Signal, 1)
termChan := make(chan os.Signal, 1)

View file

@ -9,7 +9,7 @@ import (
)
type Cruncher struct {
core.Service
*core.Service
close_chan chan bool
api *index.FragmentContainer
}
@ -33,7 +33,7 @@ func (cruncher *Cruncher) Run(port int) {
func NewCruncher() *Cruncher {
service := core.NewService()
fragment_container := index.NewFragmentContainer()
cruncher := Cruncher{*service, make(chan bool), fragment_container}
cruncher := Cruncher{service, make(chan bool), fragment_container}
cruncher.Transport = transport.NewTcpTransport(service)
cruncher.Dispatch = dispatch.NewCruncherDispatch(service)
return &cruncher