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.

86 lines
2.7 KiB

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