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.

267 lines
8.9 KiB

  1. package weed_server
  2. import (
  3. "code.google.com/p/weed-fs/go/stats"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "code.google.com/p/weed-fs/go/topology"
  6. "code.google.com/p/weed-fs/go/util"
  7. "encoding/json"
  8. "errors"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. type LookupResultLocation struct {
  14. Url string `json:"url,omitempty"`
  15. PublicUrl string `json:"publicUrl,omitempty"`
  16. }
  17. type LookupResult struct {
  18. VolumeId string `json:"volumeId,omitempty"`
  19. Locations []LookupResultLocation `json:"locations,omitempty"`
  20. Error string `json:"error,omitempty"`
  21. }
  22. func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]LookupResult) {
  23. volumeLocations = make(map[string]LookupResult)
  24. for _, vid := range vids {
  25. commaSep := strings.Index(vid, ",")
  26. if commaSep > 0 {
  27. vid = vid[0:commaSep]
  28. }
  29. if _, ok := volumeLocations[vid]; ok {
  30. continue
  31. }
  32. volumeId, err := storage.NewVolumeId(vid)
  33. if err == nil {
  34. machines := ms.Topo.Lookup(collection, volumeId)
  35. if machines != nil {
  36. var ret []LookupResultLocation
  37. for _, dn := range machines {
  38. ret = append(ret, LookupResultLocation{Url: dn.Url(), PublicUrl: dn.PublicUrl})
  39. }
  40. volumeLocations[vid] = LookupResult{VolumeId: vid, Locations: ret}
  41. } else {
  42. volumeLocations[vid] = LookupResult{VolumeId: vid, Error: "volumeId not found."}
  43. }
  44. } else {
  45. volumeLocations[vid] = LookupResult{VolumeId: vid, Error: "Unknown volumeId format."}
  46. }
  47. }
  48. return
  49. }
  50. // Takes one volumeId only, can not do batch lookup
  51. func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
  52. vid := r.FormValue("volumeId")
  53. commaSep := strings.Index(vid, ",")
  54. if commaSep > 0 {
  55. vid = vid[0:commaSep]
  56. }
  57. vids := []string{vid}
  58. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  59. volumeLocations := ms.lookupVolumeId(vids, collection)
  60. location := volumeLocations[vid]
  61. if location.Error != "" {
  62. w.WriteHeader(http.StatusNotFound)
  63. }
  64. writeJsonQuiet(w, r, location)
  65. }
  66. // This can take batched volumeIds, &volumeId=x&volumeId=y&volumeId=z
  67. func (ms *MasterServer) volumeLookupHandler(w http.ResponseWriter, r *http.Request) {
  68. r.ParseForm()
  69. vids := r.Form["volumeId"]
  70. collection := r.FormValue("collection") //optional, but can be faster if too many collections
  71. volumeLocations := ms.lookupVolumeId(vids, collection)
  72. var ret []LookupResult
  73. for _, volumeLocation := range volumeLocations {
  74. ret = append(ret, volumeLocation)
  75. }
  76. writeJsonQuiet(w, r, ret)
  77. }
  78. func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
  79. stats.AssignRequest()
  80. requestedCount, e := strconv.Atoi(r.FormValue("count"))
  81. if e != nil {
  82. requestedCount = 1
  83. }
  84. option, err := ms.getVolumeGrowOption(r)
  85. if err != nil {
  86. w.WriteHeader(http.StatusNotAcceptable)
  87. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  88. return
  89. }
  90. if !ms.Topo.HasWriableVolume(option) {
  91. if ms.Topo.FreeSpace() <= 0 {
  92. w.WriteHeader(http.StatusNotFound)
  93. writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
  94. return
  95. } else {
  96. ms.vgLock.Lock()
  97. defer ms.vgLock.Unlock()
  98. if !ms.Topo.HasWriableVolume(option) {
  99. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  100. writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
  101. return
  102. }
  103. }
  104. }
  105. }
  106. fid, count, dn, err := ms.Topo.PickForWrite(requestedCount, option)
  107. if err == nil {
  108. writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
  109. } else {
  110. w.WriteHeader(http.StatusNotAcceptable)
  111. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  112. }
  113. }
  114. func (ms *MasterServer) collectionDeleteHandler(w http.ResponseWriter, r *http.Request) {
  115. collection, ok := ms.Topo.GetCollection(r.FormValue("collection"))
  116. if !ok {
  117. writeJsonQuiet(w, r, map[string]interface{}{"error": "collection " + r.FormValue("collection") + "does not exist!"})
  118. return
  119. }
  120. for _, server := range collection.ListVolumeServers() {
  121. _, err := util.Get("http://" + server.Ip + ":" + strconv.Itoa(server.Port) + "/admin/delete_collection?collection=" + r.FormValue("collection"))
  122. if err != nil {
  123. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  124. return
  125. }
  126. }
  127. ms.Topo.DeleteCollection(r.FormValue("collection"))
  128. }
  129. func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
  130. init := r.FormValue("init") == "true"
  131. ip := r.FormValue("ip")
  132. if ip == "" {
  133. ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
  134. }
  135. port, _ := strconv.Atoi(r.FormValue("port"))
  136. maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
  137. s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
  138. publicUrl := r.FormValue("publicUrl")
  139. volumes := new([]storage.VolumeInfo)
  140. if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
  141. writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
  142. return
  143. }
  144. debug(s, "volumes", r.FormValue("volumes"))
  145. ms.Topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
  146. m := make(map[string]interface{})
  147. m["VolumeSizeLimit"] = uint64(ms.volumeSizeLimitMB) * 1024 * 1024
  148. writeJsonQuiet(w, r, m)
  149. }
  150. func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
  151. m := make(map[string]interface{})
  152. m["Version"] = util.VERSION
  153. m["Topology"] = ms.Topo.ToMap()
  154. writeJsonQuiet(w, r, m)
  155. }
  156. func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
  157. gcThreshold := r.FormValue("garbageThreshold")
  158. if gcThreshold == "" {
  159. gcThreshold = ms.garbageThreshold
  160. }
  161. debug("garbageThreshold =", gcThreshold)
  162. ms.Topo.Vacuum(gcThreshold)
  163. ms.dirStatusHandler(w, r)
  164. }
  165. func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
  166. count := 0
  167. option, err := ms.getVolumeGrowOption(r)
  168. if err != nil {
  169. w.WriteHeader(http.StatusNotAcceptable)
  170. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  171. return
  172. }
  173. if err == nil {
  174. if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
  175. if ms.Topo.FreeSpace() < count*option.ReplicaPlacement.GetCopyCount() {
  176. err = errors.New("Only " + strconv.Itoa(ms.Topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*option.ReplicaPlacement.GetCopyCount()))
  177. } else {
  178. count, err = ms.vg.GrowByCountAndType(count, option, ms.Topo)
  179. }
  180. } else {
  181. err = errors.New("parameter count is not found")
  182. }
  183. }
  184. if err != nil {
  185. w.WriteHeader(http.StatusNotAcceptable)
  186. writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
  187. } else {
  188. w.WriteHeader(http.StatusNotAcceptable)
  189. writeJsonQuiet(w, r, map[string]interface{}{"count": count})
  190. }
  191. }
  192. func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
  193. m := make(map[string]interface{})
  194. m["Version"] = util.VERSION
  195. m["Volumes"] = ms.Topo.ToVolumeMap()
  196. writeJsonQuiet(w, r, m)
  197. }
  198. func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
  199. vid, _, _, _, _ := parseURLPath(r.URL.Path)
  200. volumeId, err := storage.NewVolumeId(vid)
  201. if err != nil {
  202. debug("parsing error:", err, r.URL.Path)
  203. return
  204. }
  205. machines := ms.Topo.Lookup("", volumeId)
  206. if machines != nil && len(machines) > 0 {
  207. http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
  208. } else {
  209. w.WriteHeader(http.StatusNotFound)
  210. writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
  211. }
  212. }
  213. func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  214. if ms.Topo.IsLeader() {
  215. submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  216. } else {
  217. submitForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  218. }
  219. }
  220. func (ms *MasterServer) deleteFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
  221. if ms.Topo.IsLeader() {
  222. deleteForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
  223. } else {
  224. deleteForClientHandler(w, r, ms.Topo.RaftServer.Leader())
  225. }
  226. }
  227. func (ms *MasterServer) hasWriableVolume(option *topology.VolumeGrowOption) bool {
  228. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement)
  229. return vl.GetActiveVolumeCount(option) > 0
  230. }
  231. func (ms *MasterServer) getVolumeGrowOption(r *http.Request) (*topology.VolumeGrowOption, error) {
  232. replicationString := r.FormValue("replication")
  233. if replicationString == "" {
  234. replicationString = ms.defaultReplicaPlacement
  235. }
  236. replicaPlacement, err := storage.NewReplicaPlacementFromString(replicationString)
  237. if err != nil {
  238. return nil, err
  239. }
  240. volumeGrowOption := &topology.VolumeGrowOption{
  241. Collection: r.FormValue("collection"),
  242. ReplicaPlacement: replicaPlacement,
  243. DataCenter: r.FormValue("dataCenter"),
  244. Rack: r.FormValue("rack"),
  245. DataNode: r.FormValue("dataNode"),
  246. }
  247. return volumeGrowOption, nil
  248. }