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.

164 lines
4.4 KiB

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