store_replicate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. _, err := operation.Upload(u.String(),
  67. string(n.Name), bytes.NewReader(n.Data), n.IsGzipped(), string(n.Mime),
  68. pairMap, jwt)
  69. return err
  70. }); err != nil {
  71. size = 0
  72. err = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, err)
  73. glog.V(0).Infoln(err)
  74. }
  75. }
  76. return
  77. }
  78. func ReplicatedDelete(masterNode string, store *storage.Store,
  79. volumeId needle.VolumeId, n *needle.Needle,
  80. r *http.Request) (size uint32, err error) {
  81. //check JWT
  82. jwt := security.GetJwt(r)
  83. var remoteLocations []operation.Location
  84. if r.FormValue("type") != "replicate" {
  85. remoteLocations, err = getWritableRemoteReplications(store, volumeId, masterNode)
  86. if err != nil {
  87. glog.V(0).Infoln(err)
  88. return
  89. }
  90. }
  91. size, err = store.DeleteVolumeNeedle(volumeId, n)
  92. if err != nil {
  93. glog.V(0).Infoln("delete error:", err)
  94. return
  95. }
  96. if len(remoteLocations) > 0 { //send to other replica locations
  97. if err = distributedOperation(remoteLocations, store, func(location operation.Location) error {
  98. return util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", string(jwt))
  99. }); err != nil {
  100. size = 0
  101. }
  102. }
  103. return
  104. }
  105. type DistributedOperationResult map[string]error
  106. func (dr DistributedOperationResult) Error() error {
  107. var errs []string
  108. for k, v := range dr {
  109. if v != nil {
  110. errs = append(errs, fmt.Sprintf("[%s]: %v", k, v))
  111. }
  112. }
  113. if len(errs) == 0 {
  114. return nil
  115. }
  116. return errors.New(strings.Join(errs, "\n"))
  117. }
  118. type RemoteResult struct {
  119. Host string
  120. Error error
  121. }
  122. func distributedOperation(locations []operation.Location, store *storage.Store, op func(location operation.Location) error) error {
  123. length := len(locations)
  124. results := make(chan RemoteResult)
  125. for _, location := range locations {
  126. go func(location operation.Location, results chan RemoteResult) {
  127. results <- RemoteResult{location.Url, op(location)}
  128. }(location, results)
  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. return ret.Error()
  136. }
  137. func getWritableRemoteReplications(s *storage.Store, volumeId needle.VolumeId, masterNode string) (
  138. remoteLocations []operation.Location, err error) {
  139. copyCount := s.GetVolume(volumeId).ReplicaPlacement.GetCopyCount()
  140. if copyCount > 1 {
  141. if lookupResult, lookupErr := operation.Lookup(masterNode, volumeId.String()); lookupErr == nil {
  142. if len(lookupResult.Locations) < copyCount {
  143. err = fmt.Errorf("replicating opetations [%d] is less than volume's replication copy count [%d]",
  144. len(lookupResult.Locations), copyCount)
  145. return
  146. }
  147. selfUrl := s.Ip + ":" + strconv.Itoa(s.Port)
  148. for _, location := range lookupResult.Locations {
  149. if location.Url != selfUrl {
  150. remoteLocations = append(remoteLocations, location)
  151. }
  152. }
  153. } else {
  154. err = fmt.Errorf("failed to lookup for %d: %v", volumeId, lookupErr)
  155. return
  156. }
  157. }
  158. return
  159. }