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.

126 lines
3.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package topology
  2. import (
  3. "bytes"
  4. "net/http"
  5. "strconv"
  6. "net/url"
  7. "github.com/chrislusf/seaweedfs/go/glog"
  8. "github.com/chrislusf/seaweedfs/go/operation"
  9. "github.com/chrislusf/seaweedfs/go/security"
  10. "github.com/chrislusf/seaweedfs/go/storage"
  11. "github.com/chrislusf/seaweedfs/go/util"
  12. )
  13. func ReplicatedWrite(masterNode string, s *storage.Store,
  14. volumeId storage.VolumeId, needle *storage.Needle,
  15. r *http.Request) (size uint32, errorStatus string) {
  16. //check JWT
  17. jwt := security.GetJwt(r)
  18. ret, err := s.Write(volumeId, needle)
  19. needToReplicate := !s.HasVolume(volumeId)
  20. if err != nil {
  21. errorStatus = "Failed to write to local disk (" + err.Error() + ")"
  22. } else if ret > 0 {
  23. needToReplicate = needToReplicate || s.GetVolume(volumeId).NeedToReplicate()
  24. } else {
  25. errorStatus = "Failed to write to local disk"
  26. }
  27. if !needToReplicate && ret > 0 {
  28. needToReplicate = s.GetVolume(volumeId).NeedToReplicate()
  29. }
  30. if needToReplicate { //send to other replica locations
  31. if r.FormValue("type") != "replicate" {
  32. if !distributedOperation(masterNode, s, volumeId, func(location operation.Location) bool {
  33. u := url.URL{
  34. Scheme: "http",
  35. Host: location.Url,
  36. Path: r.URL.Path,
  37. }
  38. q := url.Values{
  39. "type": {"replicate"},
  40. }
  41. if needle.LastModified > 0 {
  42. q.Set("ts", strconv.FormatUint(needle.LastModified, 10))
  43. }
  44. if needle.IsChunkedManifest() {
  45. q.Set("cm", "true")
  46. }
  47. u.RawQuery = q.Encode()
  48. _, err := operation.Upload(u.String(),
  49. string(needle.Name), bytes.NewReader(needle.Data), needle.IsGzipped(), string(needle.Mime),
  50. jwt)
  51. return err == nil
  52. }) {
  53. ret = 0
  54. errorStatus = "Failed to write to replicas for volume " + volumeId.String()
  55. }
  56. }
  57. }
  58. size = ret
  59. return
  60. }
  61. func ReplicatedDelete(masterNode string, store *storage.Store,
  62. volumeId storage.VolumeId, n *storage.Needle,
  63. r *http.Request) (ret uint32) {
  64. //check JWT
  65. jwt := security.GetJwt(r)
  66. ret, err := store.Delete(volumeId, n)
  67. if err != nil {
  68. glog.V(0).Infoln("delete error:", err)
  69. return
  70. }
  71. needToReplicate := !store.HasVolume(volumeId)
  72. if !needToReplicate && ret > 0 {
  73. needToReplicate = store.GetVolume(volumeId).NeedToReplicate()
  74. }
  75. if needToReplicate { //send to other replica locations
  76. if r.FormValue("type") != "replicate" {
  77. if !distributedOperation(masterNode, store, volumeId, func(location operation.Location) bool {
  78. return nil == util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", jwt)
  79. }) {
  80. ret = 0
  81. }
  82. }
  83. }
  84. return
  85. }
  86. func distributedOperation(masterNode string, store *storage.Store, volumeId storage.VolumeId, op func(location operation.Location) bool) bool {
  87. if lookupResult, lookupErr := operation.Lookup(masterNode, volumeId.String()); lookupErr == nil {
  88. length := 0
  89. selfUrl := (store.Ip + ":" + strconv.Itoa(store.Port))
  90. results := make(chan bool)
  91. for _, location := range lookupResult.Locations {
  92. if location.Url != selfUrl {
  93. length++
  94. go func(location operation.Location, results chan bool) {
  95. results <- op(location)
  96. }(location, results)
  97. }
  98. }
  99. ret := true
  100. for i := 0; i < length; i++ {
  101. ret = ret && <-results
  102. }
  103. if volume := store.GetVolume(volumeId); volume != nil {
  104. if length+1 < volume.ReplicaPlacement.GetCopyCount() {
  105. glog.V(0).Infof("replicating opetations [%d] is less than volume's replication copy count [%d]", length+1, volume.ReplicaPlacement.GetCopyCount())
  106. ret = false
  107. }
  108. }
  109. return ret
  110. } else {
  111. glog.V(0).Infoln("Failed to lookup for", volumeId, lookupErr.Error())
  112. }
  113. return false
  114. }