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.

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