volume_server_handlers_sync.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, 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) getVolumeId(volumeParameterName string, r *http.Request) (storage.VolumeId, 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 0, 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 0, err
  72. }
  73. return vid, err
  74. }