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.

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