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.

90 lines
2.8 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage"
  7. "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func (vs *VolumeServer) getVolumeSyncStatusHandler(w http.ResponseWriter, r *http.Request) {
  11. v, err := vs.getVolume("volume", r)
  12. if v == nil {
  13. writeJsonError(w, r, http.StatusBadRequest, err)
  14. return
  15. }
  16. syncStat := v.GetVolumeSyncStatus()
  17. if syncStat.Error != "" {
  18. writeJsonError(w, r, http.StatusInternalServerError, fmt.Errorf("Get Volume %d status error: %s", v.Id, syncStat.Error))
  19. glog.V(2).Infoln("getVolumeSyncStatusHandler volume =", r.FormValue("volume"), ", error =", err)
  20. } else {
  21. writeJsonQuiet(w, r, http.StatusOK, syncStat)
  22. }
  23. }
  24. func (vs *VolumeServer) getVolumeIndexContentHandler(w http.ResponseWriter, r *http.Request) {
  25. v, err := vs.getVolume("volume", r)
  26. if v == nil {
  27. writeJsonError(w, r, http.StatusBadRequest, err)
  28. return
  29. }
  30. content, err := v.IndexFileContent()
  31. if err != nil {
  32. writeJsonError(w, r, http.StatusInternalServerError, err)
  33. return
  34. }
  35. w.Write(content)
  36. }
  37. func (vs *VolumeServer) getVolumeDataContentHandler(w http.ResponseWriter, r *http.Request) {
  38. v, err := vs.getVolume("volume", r)
  39. if v == nil {
  40. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("Not Found volume: %v", err))
  41. return
  42. }
  43. if int(v.SuperBlock.CompactRevision) != util.ParseInt(r.FormValue("revision"), 0) {
  44. writeJsonError(w, r, http.StatusExpectationFailed, fmt.Errorf("Requested Volume Revision is %s, but current revision is %d", r.FormValue("revision"), v.SuperBlock.CompactRevision))
  45. return
  46. }
  47. offset := uint32(util.ParseUint64(r.FormValue("offset"), 0))
  48. size := uint32(util.ParseUint64(r.FormValue("size"), 0))
  49. content, err := storage.ReadNeedleBlob(v.DataFile(), int64(offset)*types.NeedlePaddingSize, size)
  50. if err != nil {
  51. writeJsonError(w, r, http.StatusInternalServerError, err)
  52. return
  53. }
  54. id, err := types.ParseNeedleId(r.FormValue("id"))
  55. if err != nil {
  56. writeJsonError(w, r, http.StatusBadRequest, err)
  57. return
  58. }
  59. n := new(storage.Needle)
  60. n.ParseNeedleHeader(content)
  61. if id != n.Id {
  62. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("Expected file entry id %d, but found %d", id, n.Id))
  63. return
  64. }
  65. w.Write(content)
  66. }
  67. func (vs *VolumeServer) getVolumeId(volumeParameterName string, r *http.Request) (storage.VolumeId, error) {
  68. volumeIdString := r.FormValue(volumeParameterName)
  69. if volumeIdString == "" {
  70. err := fmt.Errorf("Empty Volume Id: Need to pass in %s=the_volume_id.", volumeParameterName)
  71. return 0, err
  72. }
  73. vid, err := storage.NewVolumeId(volumeIdString)
  74. if err != nil {
  75. err = fmt.Errorf("Volume Id %s is not a valid unsigned integer", volumeIdString)
  76. return 0, err
  77. }
  78. return vid, err
  79. }