You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.7 KiB

11 years ago
11 years ago
11 years ago
  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/operation"
  5. "code.google.com/p/weed-fs/go/stats"
  6. "code.google.com/p/weed-fs/go/storage"
  7. "code.google.com/p/weed-fs/go/topology"
  8. "code.google.com/p/weed-fs/go/util"
  9. "mime"
  10. "net/http"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  17. func (vs *VolumeServer) statusHandler(w http.ResponseWriter, r *http.Request) {
  18. m := make(map[string]interface{})
  19. m["Version"] = util.VERSION
  20. m["Volumes"] = vs.store.Status()
  21. writeJsonQuiet(w, r, m)
  22. }
  23. func (vs *VolumeServer) assignVolumeHandler(w http.ResponseWriter, r *http.Request) {
  24. err := vs.store.AddVolume(r.FormValue("volume"), r.FormValue("collection"), r.FormValue("replication"))
  25. if err == nil {
  26. writeJsonQuiet(w, r, map[string]string{"error": ""})
  27. } else {
  28. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  29. }
  30. glog.V(2).Infoln("assign volume =", r.FormValue("volume"), ", collection =", r.FormValue("collection"), ", replication =", r.FormValue("replication"), ", error =", err)
  31. }
  32. func (vs *VolumeServer) deleteCollectionHandler(w http.ResponseWriter, r *http.Request) {
  33. if "benchmark" != r.FormValue("collection") {
  34. glog.V(0).Infoln("deleting collection =", r.FormValue("collection"), "!!!")
  35. return
  36. }
  37. err := vs.store.DeleteCollection(r.FormValue("collection"))
  38. if err == nil {
  39. writeJsonQuiet(w, r, map[string]string{"error": ""})
  40. } else {
  41. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  42. }
  43. glog.V(2).Infoln("deleting collection =", r.FormValue("collection"), ", error =", err)
  44. }
  45. func (vs *VolumeServer) vacuumVolumeCheckHandler(w http.ResponseWriter, r *http.Request) {
  46. err, ret := vs.store.CheckCompactVolume(r.FormValue("volume"), r.FormValue("garbageThreshold"))
  47. if err == nil {
  48. writeJsonQuiet(w, r, map[string]interface{}{"error": "", "result": ret})
  49. } else {
  50. writeJsonQuiet(w, r, map[string]interface{}{"error": err.Error(), "result": false})
  51. }
  52. glog.V(2).Infoln("checked compacting volume =", r.FormValue("volume"), "garbageThreshold =", r.FormValue("garbageThreshold"), "vacuum =", ret)
  53. }
  54. func (vs *VolumeServer) vacuumVolumeCompactHandler(w http.ResponseWriter, r *http.Request) {
  55. err := vs.store.CompactVolume(r.FormValue("volume"))
  56. if err == nil {
  57. writeJsonQuiet(w, r, map[string]string{"error": ""})
  58. } else {
  59. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  60. }
  61. glog.V(2).Infoln("compacted volume =", r.FormValue("volume"), ", error =", err)
  62. }
  63. func (vs *VolumeServer) vacuumVolumeCommitHandler(w http.ResponseWriter, r *http.Request) {
  64. err := vs.store.CommitCompactVolume(r.FormValue("volume"))
  65. if err == nil {
  66. writeJsonQuiet(w, r, map[string]interface{}{"error": ""})
  67. } else {
  68. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  69. }
  70. glog.V(2).Infoln("commit compact volume =", r.FormValue("volume"), ", error =", err)
  71. }
  72. func (vs *VolumeServer) freezeVolumeHandler(w http.ResponseWriter, r *http.Request) {
  73. //TODO: notify master that this volume will be read-only
  74. err := vs.store.FreezeVolume(r.FormValue("volume"))
  75. if err == nil {
  76. writeJsonQuiet(w, r, map[string]interface{}{"error": ""})
  77. } else {
  78. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  79. }
  80. glog.V(2).Infoln("freeze volume =", r.FormValue("volume"), ", error =", err)
  81. }
  82. func (vs *VolumeServer) submitFromVolumeServerHandler(w http.ResponseWriter, r *http.Request) {
  83. submitForClientHandler(w, r, vs.masterNode)
  84. }
  85. func (vs *VolumeServer) storeHandler(w http.ResponseWriter, r *http.Request) {
  86. switch r.Method {
  87. case "GET":
  88. stats.ReadRequest()
  89. vs.GetOrHeadHandler(w, r, true)
  90. case "HEAD":
  91. stats.ReadRequest()
  92. vs.GetOrHeadHandler(w, r, false)
  93. case "DELETE":
  94. stats.DeleteRequest()
  95. secure(vs.whiteList, vs.DeleteHandler)(w, r)
  96. case "PUT":
  97. stats.WriteRequest()
  98. secure(vs.whiteList, vs.PostHandler)(w, r)
  99. case "POST":
  100. stats.WriteRequest()
  101. secure(vs.whiteList, vs.PostHandler)(w, r)
  102. }
  103. }
  104. func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
  105. n := new(storage.Needle)
  106. vid, fid, filename, ext, _ := parseURLPath(r.URL.Path)
  107. volumeId, err := storage.NewVolumeId(vid)
  108. if err != nil {
  109. glog.V(2).Infoln("parsing error:", err, r.URL.Path)
  110. w.WriteHeader(http.StatusBadRequest)
  111. return
  112. }
  113. err = n.ParsePath(fid)
  114. if err != nil {
  115. glog.V(2).Infoln("parsing fid error:", err, r.URL.Path)
  116. w.WriteHeader(http.StatusBadRequest)
  117. return
  118. }
  119. glog.V(4).Infoln("volume", volumeId, "reading", n)
  120. if !vs.store.HasVolume(volumeId) {
  121. lookupResult, err := operation.Lookup(vs.masterNode, volumeId.String())
  122. glog.V(2).Infoln("volume", volumeId, "found on", lookupResult, "error", err)
  123. if err == nil && len(lookupResult.Locations) > 0 {
  124. http.Redirect(w, r, "http://"+lookupResult.Locations[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  125. } else {
  126. glog.V(2).Infoln("lookup error:", err, r.URL.Path)
  127. w.WriteHeader(http.StatusNotFound)
  128. }
  129. return
  130. }
  131. cookie := n.Cookie
  132. count, e := vs.store.Read(volumeId, n)
  133. glog.V(4).Infoln("read bytes", count, "error", e)
  134. if e != nil || count <= 0 {
  135. glog.V(0).Infoln("read error:", e, r.URL.Path)
  136. w.WriteHeader(http.StatusNotFound)
  137. return
  138. }
  139. if n.Cookie != cookie {
  140. glog.V(0).Infoln("request", r.URL.Path, "with unmaching cookie seen:", cookie, "expected:", n.Cookie, "from", r.RemoteAddr, "agent", r.UserAgent())
  141. w.WriteHeader(http.StatusNotFound)
  142. return
  143. }
  144. if n.LastModified != 0 {
  145. w.Header().Set("Last-Modified", time.Unix(int64(n.LastModified), 0).UTC().Format(http.TimeFormat))
  146. if r.Header.Get("If-Modified-Since") != "" {
  147. if t, parseError := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); parseError == nil {
  148. if t.Unix() >= int64(n.LastModified) {
  149. w.WriteHeader(http.StatusNotModified)
  150. return
  151. }
  152. }
  153. }
  154. }
  155. if n.NameSize > 0 && filename == "" {
  156. filename = string(n.Name)
  157. dotIndex := strings.LastIndex(filename, ".")
  158. if dotIndex > 0 {
  159. ext = filename[dotIndex:]
  160. }
  161. }
  162. mtype := ""
  163. if ext != "" {
  164. mtype = mime.TypeByExtension(ext)
  165. }
  166. if n.MimeSize > 0 {
  167. mtype = string(n.Mime)
  168. }
  169. if mtype != "" {
  170. w.Header().Set("Content-Type", mtype)
  171. }
  172. if filename != "" {
  173. w.Header().Set("Content-Disposition", "filename="+fileNameEscaper.Replace(filename))
  174. }
  175. if ext != ".gz" {
  176. if n.IsGzipped() {
  177. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  178. w.Header().Set("Content-Encoding", "gzip")
  179. } else {
  180. if n.Data, err = storage.UnGzipData(n.Data); err != nil {
  181. glog.V(0).Infoln("lookup error:", err, r.URL.Path)
  182. }
  183. }
  184. }
  185. }
  186. w.Header().Set("Content-Length", strconv.Itoa(len(n.Data)))
  187. if isGetMethod {
  188. if _, e = w.Write(n.Data); e != nil {
  189. glog.V(0).Infoln("response write error:", e)
  190. }
  191. }
  192. }
  193. func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  194. m := make(map[string]interface{})
  195. if e := r.ParseForm(); e != nil {
  196. glog.V(0).Infoln("form parse error:", e)
  197. writeJsonError(w, r, e)
  198. return
  199. }
  200. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  201. volumeId, ve := storage.NewVolumeId(vid)
  202. if ve != nil {
  203. glog.V(0).Infoln("NewVolumeId error:", ve)
  204. writeJsonError(w, r, ve)
  205. return
  206. }
  207. needle, ne := storage.NewNeedle(r)
  208. if ne != nil {
  209. writeJsonError(w, r, ne)
  210. return
  211. }
  212. ret, errorStatus := topology.ReplicatedWrite(vs.masterNode, vs.store, volumeId, needle, r)
  213. if errorStatus == "" {
  214. w.WriteHeader(http.StatusCreated)
  215. } else {
  216. w.WriteHeader(http.StatusInternalServerError)
  217. m["error"] = errorStatus
  218. }
  219. if needle.HasName() {
  220. m["name"] = string(needle.Name)
  221. }
  222. m["size"] = ret
  223. writeJsonQuiet(w, r, m)
  224. }
  225. func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  226. n := new(storage.Needle)
  227. vid, fid, _, _, _ := parseURLPath(r.URL.Path)
  228. volumeId, _ := storage.NewVolumeId(vid)
  229. n.ParsePath(fid)
  230. glog.V(2).Infoln("deleting", n)
  231. cookie := n.Cookie
  232. count, ok := vs.store.Read(volumeId, n)
  233. if ok != nil {
  234. m := make(map[string]uint32)
  235. m["size"] = 0
  236. writeJsonQuiet(w, r, m)
  237. return
  238. }
  239. if n.Cookie != cookie {
  240. glog.V(0).Infoln("delete", r.URL.Path, "with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
  241. return
  242. }
  243. n.Size = 0
  244. ret := topology.ReplicatedDelete(vs.masterNode, vs.store, volumeId, n, r)
  245. if ret != 0 {
  246. w.WriteHeader(http.StatusAccepted)
  247. } else {
  248. w.WriteHeader(http.StatusInternalServerError)
  249. }
  250. m := make(map[string]uint32)
  251. m["size"] = uint32(count)
  252. writeJsonQuiet(w, r, m)
  253. }
  254. func (vs *VolumeServer) statsDiskHandler(w http.ResponseWriter, r *http.Request) {
  255. m := make(map[string]interface{})
  256. m["Version"] = util.VERSION
  257. ds := make([]*stats.DiskStatus, 0)
  258. for _, loc := range vs.store.Locations {
  259. if dir, e := filepath.Abs(loc.Directory); e == nil {
  260. ds = append(ds, stats.NewDiskStatus(dir))
  261. }
  262. }
  263. m["DiskStatues"] = ds
  264. writeJsonQuiet(w, r, m)
  265. }