func newRadosConn(t *testing.T, user string, cfgPath string) (c *rados.Conn) { var err error c, err = rados.NewConn(user) if err != nil { t.Fatalf("Failed to open keyring: %v", err) } err = c.ReadConfigFile(cfgPath) if err != nil { t.Fatalf("Failed to read: %s", cfgPath) } err = c.Connect() if err != nil { t.Fatalf("Failed to connect to remote cluster: %v", err) } return }
func main() { /* pid */ if err := CreatePidfile(PIDFILE); err != nil { fmt.Printf("can not create pid file %s\n", PIDFILE) return } defer RemovePidfile(PIDFILE) /* log */ f, err := os.OpenFile(LOGPATH, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { fmt.Println("failed to open log\n") return } defer f.Close() m := martini.Classic() m.Use(AuthMe(SECRET)) slog = log.New(f, "[wuzei]", log.LstdFlags) m.Map(slog) cfg, err = getGcCfg() if err != nil { slog.Println(err.Error()) return } wugui.InitCachePool(cfg.MyIPAddr, cfg.Peers, cfg.Port) slog.Printf("Config of group cache: %+v\n", cfg) var cacheSize int64 cacheSize = int64(cfg.CacheSizeMBytes) * 1024 * 1024 wugui.InitRadosCache(cfg.Name, cacheSize, cfg.CacheChunkSizeKBytes*1024) conn, err = rados.NewConn("admin") if err != nil { slog.Println("failed to open keyring") return } conn.SetConfigOption("rados_mon_op_timeout", MONTIMEOUT) conn.SetConfigOption("rados_osd_op_timeout", OSDTIMEOUT) err = conn.ReadConfigFile("/etc/ceph/ceph.conf") if err != nil { slog.Println("failed to open ceph.conf") return } err = conn.Connect() if err != nil { slog.Println("failed to connect to remote cluster") return } defer conn.Shutdown() ReqQueue.Init() m.Get("/whoareyou", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("I AM WUZEI")) }) m.Get("/cachestats", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, fmt.Sprintf("%+v\n", wugui.GetRadosCacheStats())) }) /* resume upload protocal is based on http://www.grid.net.ru/nginx/resumable_uploads.en.html */ m.Put("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", PutHandler) m.Delete("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", DeleteHandler) m.Get("/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", GetHandler) m.Get("/info/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", InfoHandler) m.Get("/calcmd5/(?P<pool>[A-Za-z0-9]+)/(?P<soid>[^/]+)", Md5sumHandler) m.Get("/blocksize", BlockHandler) sl, _ := nettimeout.NewListener(cfg.ListenPort, time.Duration(cfg.SocketTimeout)*time.Second, time.Duration(cfg.SocketTimeout)*time.Second) server := http.Server{} http.HandleFunc("/", m.ServeHTTP) sigChan := make(chan os.Signal) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM) go func() { server.Serve(sl) }() slog.Printf("Serving HTTP\n") for { select { case signal := <-sigChan: slog.Printf("Got signal:%v\n", signal) switch signal { case syscall.SIGHUP: slog.Println("Reloading config file.") cfg, err = getGcCfg() if err != nil { slog.Printf("Failed to load config: %s\n", err.Error()) continue } slog.Printf("Updating Peers to: %v with Port: %d\n", cfg.Peers, cfg.Port) wugui.SetCachePoolPeers(cfg.Peers, cfg.Port) default: sl.Stop() slog.Printf("Waiting on server\n") wg.Wait() slog.Printf("Server shutdown\n") // NOTE(wenjianhn): deferred functions will not run if using os.Exit(0). return } } } }