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.

159 lines
4.3 KiB

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