volume_server_handlers_admin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "path/filepath"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/stats"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. )
  12. func (vs *VolumeServer) statusHandler(w http.ResponseWriter, r *http.Request) {
  13. m := make(map[string]interface{})
  14. m["Version"] = util.VERSION
  15. m["Volumes"] = vs.store.Status()
  16. writeJsonQuiet(w, r, http.StatusOK, m)
  17. }
  18. func (vs *VolumeServer) assignVolumeHandler(w http.ResponseWriter, r *http.Request) {
  19. var err error
  20. preallocate := int64(0)
  21. if r.FormValue("preallocate") != "" {
  22. preallocate, err = strconv.ParseInt(r.FormValue("preallocate"), 10, 64)
  23. if err != nil {
  24. glog.V(0).Infof("ignoring invalid int64 value for preallocate = %v", r.FormValue("preallocate"))
  25. }
  26. }
  27. err = vs.store.AddVolume(
  28. r.FormValue("volume"),
  29. r.FormValue("collection"),
  30. vs.needleMapKind,
  31. r.FormValue("replication"),
  32. r.FormValue("ttl"),
  33. preallocate,
  34. )
  35. if err == nil {
  36. writeJsonQuiet(w, r, http.StatusAccepted, map[string]string{"error": ""})
  37. } else {
  38. writeJsonError(w, r, http.StatusNotAcceptable, err)
  39. }
  40. glog.V(2).Infof("assign volume = %s, collection = %s , replication = %s, error = %v",
  41. r.FormValue("volume"), r.FormValue("collection"), r.FormValue("replication"), err)
  42. }
  43. func (vs *VolumeServer) deleteCollectionHandler(w http.ResponseWriter, r *http.Request) {
  44. err := vs.store.DeleteCollection(r.FormValue("collection"))
  45. if err == nil {
  46. writeJsonQuiet(w, r, http.StatusOK, map[string]string{"error": ""})
  47. } else {
  48. writeJsonError(w, r, http.StatusInternalServerError, err)
  49. }
  50. glog.V(2).Infof("deleting collection = %s, error = %v", r.FormValue("collection"), err)
  51. }
  52. func (vs *VolumeServer) statsDiskHandler(w http.ResponseWriter, r *http.Request) {
  53. m := make(map[string]interface{})
  54. m["Version"] = util.VERSION
  55. var ds []*stats.DiskStatus
  56. for _, loc := range vs.store.Locations {
  57. if dir, e := filepath.Abs(loc.Directory); e == nil {
  58. ds = append(ds, stats.NewDiskStatus(dir))
  59. }
  60. }
  61. m["DiskStatuses"] = ds
  62. writeJsonQuiet(w, r, http.StatusOK, m)
  63. }
  64. func (vs *VolumeServer) getVolume(volumeParameterName string, r *http.Request) (*storage.Volume, error) {
  65. vid, err := vs.getVolumeId(volumeParameterName, r)
  66. if err != nil {
  67. return nil, err
  68. }
  69. v := vs.store.GetVolume(vid)
  70. if v == nil {
  71. return nil, fmt.Errorf("Not Found Volume Id %d", vid)
  72. }
  73. return v, nil
  74. }
  75. func (vs *VolumeServer) getVolumeMountHandler(w http.ResponseWriter, r *http.Request) {
  76. vid, err := vs.getVolumeId("volume", r)
  77. if err != nil {
  78. writeJsonError(w, r, http.StatusNotFound, err)
  79. return
  80. }
  81. vs.store.MountVolume(vid)
  82. writeJsonQuiet(w, r, http.StatusOK, "Volume mounted")
  83. }
  84. func (vs *VolumeServer) getVolumeUnmountHandler(w http.ResponseWriter, r *http.Request) {
  85. vid, err := vs.getVolumeId("volume", r)
  86. if err != nil {
  87. writeJsonError(w, r, http.StatusNotFound, err)
  88. return
  89. }
  90. vs.store.UnmountVolume(vid)
  91. writeJsonQuiet(w, r, http.StatusOK, "Volume unmounted")
  92. }
  93. func (vs *VolumeServer) getVolumeDeleteHandler(w http.ResponseWriter, r *http.Request) {
  94. vid, err := vs.getVolumeId("volume", r)
  95. if err != nil {
  96. writeJsonError(w, r, http.StatusNotFound, err)
  97. return
  98. }
  99. vs.store.DeleteVolume(vid)
  100. writeJsonQuiet(w, r, http.StatusOK, "Volume deleted")
  101. }