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.

179 lines
4.7 KiB

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