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

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. size = ret
  27. return
  28. }
  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. }
  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. pairMap := make(map[string]string)
  52. if needle.HasPairs() {
  53. tmpMap := make(map[string]string)
  54. err := json.Unmarshal(needle.Pairs, &tmpMap)
  55. if err != nil {
  56. glog.V(0).Infoln("Unmarshal pairs error:", err)
  57. }
  58. for k, v := range tmpMap {
  59. pairMap[storage.PairNamePrefix+k] = v
  60. }
  61. }
  62. _, err := operation.Upload(u.String(),
  63. string(needle.Name), bytes.NewReader(needle.Data), needle.IsGzipped(), string(needle.Mime),
  64. pairMap, jwt)
  65. return err
  66. }); err != nil {
  67. ret = 0
  68. errorStatus = fmt.Sprintf("Failed to write to replicas for volume %d: %v", volumeId, err)
  69. }
  70. }
  71. }
  72. size = ret
  73. return
  74. }
  75. func ReplicatedDelete(masterNode string, store *storage.Store,
  76. volumeId storage.VolumeId, n *storage.Needle,
  77. r *http.Request) (uint32, error) {
  78. //check JWT
  79. jwt := security.GetJwt(r)
  80. ret, err := store.Delete(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 storage.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. }