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.

150 lines
4.1 KiB

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