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.

87 lines
2.8 KiB

  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/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, block, err := storage.ReadNeedleBlob(v.DataFile(), int64(offset)*storage.NeedlePaddingSize, size)
  49. defer storage.ReleaseBytes(block.Bytes)
  50. if err != nil {
  51. writeJsonError(w, r, http.StatusInternalServerError, err)
  52. return
  53. }
  54. id := util.ParseUint64(r.FormValue("id"), 0)
  55. n := new(storage.Needle)
  56. n.ParseNeedleHeader(content)
  57. if id != n.Id {
  58. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("Expected file entry id %d, but found %d", id, n.Id))
  59. return
  60. }
  61. w.Write(content)
  62. }
  63. func (vs *VolumeServer) getVolume(volumeParameterName string, r *http.Request) (*storage.Volume, error) {
  64. volumeIdString := r.FormValue(volumeParameterName)
  65. if volumeIdString == "" {
  66. err := fmt.Errorf("Empty Volume Id: Need to pass in %s=the_volume_id.", volumeParameterName)
  67. return nil, err
  68. }
  69. vid, err := storage.NewVolumeId(volumeIdString)
  70. if err != nil {
  71. err = fmt.Errorf("Volume Id %s is not a valid unsigned integer", volumeIdString)
  72. return nil, err
  73. }
  74. v := vs.store.GetVolume(vid)
  75. if v == nil {
  76. return nil, fmt.Errorf("Not Found Volume Id %s: %d", volumeIdString, vid)
  77. }
  78. return v, nil
  79. }