2012-08-07 16:29:22 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-13 12:27:47 +08:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-02-27 14:54:22 +08:00
|
|
|
"code.google.com/p/weed-fs/go/operation"
|
2013-06-20 09:10:38 +08:00
|
|
|
"code.google.com/p/weed-fs/go/replication"
|
2013-02-27 14:54:22 +08:00
|
|
|
"code.google.com/p/weed-fs/go/storage"
|
2012-08-24 13:46:54 +08:00
|
|
|
"math/rand"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2012-09-17 08:31:15 +08:00
|
|
|
"os"
|
2013-07-14 06:57:44 +08:00
|
|
|
"path/filepath"
|
2012-10-07 01:50:52 +08:00
|
|
|
"runtime"
|
2012-08-24 13:46:54 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2012-08-24 13:46:54 +08:00
|
|
|
cmdVolume.Run = runVolume // break init cycle
|
2013-01-20 11:49:57 +08:00
|
|
|
cmdVolume.IsDebug = cmdVolume.Flag.Bool("debug", false, "enable debug mode")
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdVolume = &Command{
|
2012-09-26 16:55:56 +08:00
|
|
|
UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
|
2012-08-24 13:46:54 +08:00
|
|
|
Short: "start a volume server",
|
|
|
|
Long: `start a volume server to provide storage spaces
|
2012-08-07 16:29:22 +08:00
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2013-08-14 00:22:06 +08:00
|
|
|
vport = cmdVolume.Flag.Int("port", 8080, "http listen port")
|
2013-08-29 01:39:15 +08:00
|
|
|
volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
|
2013-08-14 00:22:06 +08:00
|
|
|
maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
|
|
|
|
ip = cmdVolume.Flag.String("ip", "localhost", "ip or server name")
|
|
|
|
publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible <ip|server_name>:<port>")
|
|
|
|
masterNode = cmdVolume.Flag.String("mserver", "localhost:9333", "master server location")
|
|
|
|
vpulse = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than the master's setting")
|
|
|
|
vReadTimeout = cmdVolume.Flag.Int("readTimeout", 3, "connection read timeout in seconds. Increase this if uploading large files.")
|
|
|
|
vMaxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
|
|
|
|
dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
|
|
|
|
rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
|
|
|
|
volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
|
2012-08-07 16:29:22 +08:00
|
|
|
|
2013-08-14 00:22:06 +08:00
|
|
|
store *storage.Store
|
|
|
|
volumeWhiteList []string
|
2012-08-07 16:29:22 +08:00
|
|
|
)
|
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
|
2013-01-03 05:40:00 +08:00
|
|
|
|
2012-08-07 16:29:22 +08:00
|
|
|
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
2012-10-07 01:50:52 +08:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = VERSION
|
|
|
|
m["Volumes"] = store.Status()
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
2012-09-11 08:08:52 +08:00
|
|
|
func assignVolumeHandler(w http.ResponseWriter, r *http.Request) {
|
2013-11-12 18:21:22 +08:00
|
|
|
err := store.AddVolume(r.FormValue("volume"), r.FormValue("collection"), r.FormValue("replicationType"))
|
2012-09-11 08:08:52 +08:00
|
|
|
if err == nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": ""})
|
2012-09-11 08:08:52 +08:00
|
|
|
} else {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
2012-09-11 08:08:52 +08:00
|
|
|
}
|
2013-11-12 18:21:22 +08:00
|
|
|
debug("assign volume =", r.FormValue("volume"), ", collection =", r.FormValue("collection"), ", replicationType =", r.FormValue("replicationType"), ", error =", err)
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
2012-11-24 09:03:27 +08:00
|
|
|
func vacuumVolumeCheckHandler(w http.ResponseWriter, r *http.Request) {
|
2012-11-27 05:12:21 +08:00
|
|
|
err, ret := store.CheckCompactVolume(r.FormValue("volume"), r.FormValue("garbageThreshold"))
|
|
|
|
if err == nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"error": "", "result": ret})
|
2012-11-27 05:12:21 +08:00
|
|
|
} else {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"error": err.Error(), "result": false})
|
2012-11-27 05:12:21 +08:00
|
|
|
}
|
|
|
|
debug("checked compacting volume =", r.FormValue("volume"), "garbageThreshold =", r.FormValue("garbageThreshold"), "vacuum =", ret)
|
2012-11-24 09:03:27 +08:00
|
|
|
}
|
2012-11-07 17:51:43 +08:00
|
|
|
func vacuumVolumeCompactHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := store.CompactVolume(r.FormValue("volume"))
|
|
|
|
if err == nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": ""})
|
2012-11-07 17:51:43 +08:00
|
|
|
} else {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
2012-11-07 17:51:43 +08:00
|
|
|
}
|
|
|
|
debug("compacted volume =", r.FormValue("volume"), ", error =", err)
|
|
|
|
}
|
|
|
|
func vacuumVolumeCommitHandler(w http.ResponseWriter, r *http.Request) {
|
2012-11-24 09:03:27 +08:00
|
|
|
err := store.CommitCompactVolume(r.FormValue("volume"))
|
2012-11-12 17:26:18 +08:00
|
|
|
if err == nil {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"error": ""})
|
2012-11-12 17:26:18 +08:00
|
|
|
} else {
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
2012-11-12 17:26:18 +08:00
|
|
|
}
|
|
|
|
debug("commit compact volume =", r.FormValue("volume"), ", error =", err)
|
2012-09-11 08:08:52 +08:00
|
|
|
}
|
2013-06-20 09:10:38 +08:00
|
|
|
func freezeVolumeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//TODO: notify master that this volume will be read-only
|
|
|
|
err := store.FreezeVolume(r.FormValue("volume"))
|
|
|
|
if err == nil {
|
|
|
|
writeJsonQuiet(w, r, map[string]interface{}{"error": ""})
|
|
|
|
} else {
|
|
|
|
writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
|
|
|
|
}
|
|
|
|
debug("freeze volume =", r.FormValue("volume"), ", error =", err)
|
|
|
|
}
|
2013-08-07 04:23:10 +08:00
|
|
|
func submitFromVolumeServerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
submitForClientHandler(w, r, *masterNode)
|
2013-08-07 03:13:10 +08:00
|
|
|
}
|
2012-08-07 16:29:22 +08:00
|
|
|
func storeHandler(w http.ResponseWriter, r *http.Request) {
|
2012-08-24 13:46:54 +08:00
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2013-03-21 13:57:41 +08:00
|
|
|
GetOrHeadHandler(w, r, true)
|
|
|
|
case "HEAD":
|
|
|
|
GetOrHeadHandler(w, r, false)
|
2012-08-24 13:46:54 +08:00
|
|
|
case "DELETE":
|
2013-08-14 00:22:06 +08:00
|
|
|
secure(volumeWhiteList, DeleteHandler)(w, r)
|
2013-11-12 18:21:22 +08:00
|
|
|
case "PUT":
|
|
|
|
secure(volumeWhiteList, PostHandler)(w, r)
|
2012-08-24 13:46:54 +08:00
|
|
|
case "POST":
|
2013-08-14 00:22:06 +08:00
|
|
|
secure(volumeWhiteList, PostHandler)(w, r)
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
2013-03-21 13:57:41 +08:00
|
|
|
func GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
|
2012-08-24 13:46:54 +08:00
|
|
|
n := new(storage.Needle)
|
2013-09-02 14:58:21 +08:00
|
|
|
vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
|
2012-09-26 07:05:31 +08:00
|
|
|
volumeId, err := storage.NewVolumeId(vid)
|
|
|
|
if err != nil {
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("parsing error:", err, r.URL.Path)
|
2012-09-26 07:05:31 +08:00
|
|
|
return
|
|
|
|
}
|
2012-08-24 13:46:54 +08:00
|
|
|
n.ParsePath(fid)
|
|
|
|
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("volume", volumeId, "reading", n)
|
2012-09-20 17:47:32 +08:00
|
|
|
if !store.HasVolume(volumeId) {
|
2012-09-26 07:05:31 +08:00
|
|
|
lookupResult, err := operation.Lookup(*masterNode, volumeId)
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("volume", volumeId, "found on", lookupResult, "error", err)
|
2012-09-20 17:47:32 +08:00
|
|
|
if err == nil {
|
|
|
|
http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
|
|
|
|
} else {
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("lookup error:", err, r.URL.Path)
|
2012-09-20 17:47:32 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2012-08-24 13:46:54 +08:00
|
|
|
cookie := n.Cookie
|
|
|
|
count, e := store.Read(volumeId, n)
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("read bytes", count, "error", e)
|
2012-09-20 17:11:08 +08:00
|
|
|
if e != nil || count <= 0 {
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("read error:", e, r.URL.Path)
|
2012-09-24 06:45:26 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2012-09-20 17:11:08 +08:00
|
|
|
return
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
if n.Cookie != cookie {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
|
2012-09-24 06:45:26 +08:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2012-08-24 13:46:54 +08:00
|
|
|
return
|
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
if n.LastModified != 0 {
|
|
|
|
w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
|
|
|
|
if r.Header.Get("If-Modified-Since") != "" {
|
|
|
|
if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
|
2013-07-10 15:25:14 +08:00
|
|
|
if t.Unix() >= int64(n.LastModified) {
|
2013-07-09 14:38:38 +08:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-13 05:19:11 +08:00
|
|
|
if n.NameSize > 0 && filename == "" {
|
2013-11-19 15:16:21 +08:00
|
|
|
filename = string(n.Name)
|
2013-07-13 05:19:11 +08:00
|
|
|
dotIndex := strings.LastIndex(filename, ".")
|
2012-12-22 18:10:45 +08:00
|
|
|
if dotIndex > 0 {
|
2013-07-13 05:19:11 +08:00
|
|
|
ext = filename[dotIndex:]
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mtype := ""
|
2012-08-24 13:46:54 +08:00
|
|
|
if ext != "" {
|
2012-12-22 18:10:45 +08:00
|
|
|
mtype = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
if n.MimeSize > 0 {
|
|
|
|
mtype = string(n.Mime)
|
|
|
|
}
|
|
|
|
if mtype != "" {
|
2012-08-24 13:46:54 +08:00
|
|
|
w.Header().Set("Content-Type", mtype)
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
2013-07-13 05:19:11 +08:00
|
|
|
if filename != "" {
|
|
|
|
w.Header().Set("Content-Disposition", "filename="+fileNameEscaper.Replace(filename))
|
2012-12-22 18:10:45 +08:00
|
|
|
}
|
|
|
|
if ext != ".gz" {
|
|
|
|
if n.IsGzipped() {
|
2012-08-24 13:46:54 +08:00
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
} else {
|
2013-01-17 16:56:56 +08:00
|
|
|
if n.Data, err = storage.UnGzipData(n.Data); err != nil {
|
|
|
|
debug("lookup error:", err, r.URL.Path)
|
|
|
|
}
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 18:10:45 +08:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
|
2013-03-21 13:57:41 +08:00
|
|
|
if isGetMethod {
|
|
|
|
if _, e = w.Write(n.Data); e != nil {
|
|
|
|
debug("response write error:", e)
|
|
|
|
}
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
func PostHandler(w http.ResponseWriter, r *http.Request) {
|
2013-08-06 04:37:41 +08:00
|
|
|
m := make(map[string]interface{})
|
2013-02-27 14:54:22 +08:00
|
|
|
if e := r.ParseForm(); e != nil {
|
|
|
|
debug("form parse error:", e)
|
2013-08-07 03:13:10 +08:00
|
|
|
writeJsonError(w, r, e)
|
2013-02-27 14:54:22 +08:00
|
|
|
return
|
|
|
|
}
|
2013-09-02 14:58:21 +08:00
|
|
|
vid, _, _, _, _ := parseURLPath(r.URL.Path)
|
2013-08-06 04:37:41 +08:00
|
|
|
volumeId, ve := storage.NewVolumeId(vid)
|
|
|
|
if ve != nil {
|
|
|
|
debug("NewVolumeId error:", ve)
|
2013-08-07 03:13:10 +08:00
|
|
|
writeJsonError(w, r, ve)
|
2013-08-06 04:37:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
needle, ne := storage.NewNeedle(r)
|
|
|
|
if ne != nil {
|
2013-08-07 03:13:10 +08:00
|
|
|
writeJsonError(w, r, ne)
|
2013-02-27 14:54:22 +08:00
|
|
|
return
|
|
|
|
}
|
2013-08-06 04:37:41 +08:00
|
|
|
ret, errorStatus := replication.ReplicatedWrite(*masterNode, store, volumeId, needle, r)
|
|
|
|
if errorStatus == "" {
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2012-08-24 13:46:54 +08:00
|
|
|
} else {
|
2013-08-06 04:37:41 +08:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
m["error"] = errorStatus
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
2013-08-06 04:37:41 +08:00
|
|
|
m["size"] = ret
|
|
|
|
writeJsonQuiet(w, r, m)
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2012-08-24 13:46:54 +08:00
|
|
|
n := new(storage.Needle)
|
2013-09-02 14:58:21 +08:00
|
|
|
vid, fid, _, _, _ := parseURLPath(r.URL.Path)
|
2012-09-04 11:40:38 +08:00
|
|
|
volumeId, _ := storage.NewVolumeId(vid)
|
2012-08-24 13:46:54 +08:00
|
|
|
n.ParsePath(fid)
|
|
|
|
|
2012-09-28 03:17:27 +08:00
|
|
|
debug("deleting", n)
|
2012-08-24 13:46:54 +08:00
|
|
|
|
|
|
|
cookie := n.Cookie
|
|
|
|
count, ok := store.Read(volumeId, n)
|
|
|
|
|
|
|
|
if ok != nil {
|
|
|
|
m := make(map[string]uint32)
|
|
|
|
m["size"] = 0
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-08-24 13:46:54 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Cookie != cookie {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("delete with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
|
2012-08-24 13:46:54 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Size = 0
|
2013-04-16 15:10:21 +08:00
|
|
|
ret := replication.ReplicatedDelete(*masterNode, store, volumeId, n, r)
|
2012-09-27 05:57:21 +08:00
|
|
|
|
|
|
|
if ret != 0 {
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2012-09-27 04:38:45 +08:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2012-09-26 18:27:10 +08:00
|
|
|
}
|
|
|
|
|
2012-08-24 13:46:54 +08:00
|
|
|
m := make(map[string]uint32)
|
|
|
|
m["size"] = uint32(count)
|
2013-02-27 14:54:22 +08:00
|
|
|
writeJsonQuiet(w, r, m)
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
2012-09-28 03:17:27 +08:00
|
|
|
|
2013-09-02 14:58:21 +08:00
|
|
|
func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
|
2013-07-14 14:46:12 +08:00
|
|
|
switch strings.Count(path, "/") {
|
|
|
|
case 3:
|
2013-07-13 05:19:11 +08:00
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid, filename = parts[1], parts[2], parts[3]
|
2013-07-14 06:57:44 +08:00
|
|
|
ext = filepath.Ext(filename)
|
2013-07-14 14:46:12 +08:00
|
|
|
case 2:
|
2013-07-16 08:26:00 +08:00
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid = parts[1], parts[2]
|
|
|
|
dotIndex := strings.LastIndex(fid, ".")
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = fid[dotIndex:]
|
|
|
|
fid = fid[0:dotIndex]
|
|
|
|
}
|
2013-07-14 14:46:12 +08:00
|
|
|
default:
|
2013-07-13 05:19:11 +08:00
|
|
|
sepIndex := strings.LastIndex(path, "/")
|
|
|
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
|
|
if commaIndex <= 0 {
|
2013-09-02 14:58:21 +08:00
|
|
|
vid, isVolumeIdOnly = path[sepIndex+1:], true
|
2013-07-13 05:19:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
|
|
vid = path[sepIndex+1 : commaIndex]
|
|
|
|
fid = path[commaIndex+1:]
|
|
|
|
ext = ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
fid = path[commaIndex+1 : dotIndex]
|
|
|
|
ext = path[dotIndex:]
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runVolume(cmd *Command, args []string) bool {
|
2012-11-07 17:51:43 +08:00
|
|
|
if *vMaxCpu < 1 {
|
|
|
|
*vMaxCpu = runtime.NumCPU()
|
|
|
|
}
|
2012-10-07 01:50:52 +08:00
|
|
|
runtime.GOMAXPROCS(*vMaxCpu)
|
2013-07-14 02:38:01 +08:00
|
|
|
folders := strings.Split(*volumeFolders, ",")
|
|
|
|
maxCountStrings := strings.Split(*maxVolumeCounts, ",")
|
|
|
|
maxCounts := make([]int, 0)
|
|
|
|
for _, maxString := range maxCountStrings {
|
|
|
|
if max, e := strconv.Atoi(maxString); e == nil {
|
|
|
|
maxCounts = append(maxCounts, max)
|
|
|
|
} else {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("The max specified in -max not a valid number %s", max)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
2013-07-14 02:38:01 +08:00
|
|
|
if len(folders) != len(maxCounts) {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
for _, folder := range folders {
|
|
|
|
fileInfo, err := os.Stat(folder)
|
|
|
|
if err != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("No Existing Folder:%s", folder)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
if !fileInfo.IsDir() {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("Volume Folder should not be a file:%s", folder)
|
2013-07-14 02:38:01 +08:00
|
|
|
}
|
|
|
|
perm := fileInfo.Mode().Perm()
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Volume Folder", folder)
|
|
|
|
glog.V(0).Infoln("Permission:", perm)
|
2012-09-17 08:31:15 +08:00
|
|
|
}
|
2012-09-26 18:27:10 +08:00
|
|
|
|
2012-09-26 17:29:16 +08:00
|
|
|
if *publicUrl == "" {
|
2012-09-26 18:27:10 +08:00
|
|
|
*publicUrl = *ip + ":" + strconv.Itoa(*vport)
|
2012-09-26 17:29:16 +08:00
|
|
|
}
|
2013-08-14 00:22:06 +08:00
|
|
|
if *volumeWhiteListOption != "" {
|
|
|
|
volumeWhiteList = strings.Split(*volumeWhiteListOption, ",")
|
2013-08-13 14:48:10 +08:00
|
|
|
}
|
2012-09-20 17:11:08 +08:00
|
|
|
|
2013-07-14 02:38:01 +08:00
|
|
|
store = storage.NewStore(*vport, *ip, *publicUrl, folders, maxCounts)
|
2012-08-24 13:46:54 +08:00
|
|
|
defer store.Close()
|
|
|
|
http.HandleFunc("/", storeHandler)
|
2013-08-14 00:22:06 +08:00
|
|
|
http.HandleFunc("/submit", secure(volumeWhiteList, submitFromVolumeServerHandler))
|
|
|
|
http.HandleFunc("/status", secure(volumeWhiteList, statusHandler))
|
|
|
|
http.HandleFunc("/admin/assign_volume", secure(volumeWhiteList, assignVolumeHandler))
|
|
|
|
http.HandleFunc("/admin/vacuum_volume_check", secure(volumeWhiteList, vacuumVolumeCheckHandler))
|
|
|
|
http.HandleFunc("/admin/vacuum_volume_compact", secure(volumeWhiteList, vacuumVolumeCompactHandler))
|
|
|
|
http.HandleFunc("/admin/vacuum_volume_commit", secure(volumeWhiteList, vacuumVolumeCommitHandler))
|
|
|
|
http.HandleFunc("/admin/freeze_volume", secure(volumeWhiteList, freezeVolumeHandler))
|
2012-08-24 13:46:54 +08:00
|
|
|
|
|
|
|
go func() {
|
2012-11-27 05:12:21 +08:00
|
|
|
connected := true
|
2012-12-04 14:54:08 +08:00
|
|
|
store.SetMaster(*masterNode)
|
2013-06-20 09:10:38 +08:00
|
|
|
store.SetDataCenter(*dataCenter)
|
|
|
|
store.SetRack(*rack)
|
2012-08-24 13:46:54 +08:00
|
|
|
for {
|
2012-12-04 14:54:08 +08:00
|
|
|
err := store.Join()
|
2012-11-27 05:12:21 +08:00
|
|
|
if err == nil {
|
|
|
|
if !connected {
|
|
|
|
connected = true
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Reconnected with master")
|
2012-11-27 05:12:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if connected {
|
|
|
|
connected = false
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 11:40:38 +08:00
|
|
|
time.Sleep(time.Duration(float32(*vpulse*1e3)*(1+rand.Float32())) * time.Millisecond)
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
}()
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("store joined at", *masterNode)
|
2012-08-24 13:46:54 +08:00
|
|
|
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("Start Weed volume server", VERSION, "at http://"+*ip+":"+strconv.Itoa(*vport))
|
2012-09-29 00:13:17 +08:00
|
|
|
srv := &http.Server{
|
2012-09-29 01:21:06 +08:00
|
|
|
Addr: ":" + strconv.Itoa(*vport),
|
|
|
|
Handler: http.DefaultServeMux,
|
|
|
|
ReadTimeout: (time.Duration(*vReadTimeout) * time.Second),
|
|
|
|
}
|
2012-09-29 00:13:17 +08:00
|
|
|
e := srv.ListenAndServe()
|
2012-08-24 13:46:54 +08:00
|
|
|
if e != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.Fatalf("Fail to start:%s", e.Error())
|
2012-08-24 13:46:54 +08:00
|
|
|
}
|
|
|
|
return true
|
2012-08-07 16:29:22 +08:00
|
|
|
}
|