store_replicate.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package topology
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/storage"
  14. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func ReplicatedWrite(masterNode string, s *storage.Store, volumeId needle.VolumeId, n *needle.Needle, r *http.Request) (isUnchanged bool, err error) {
  18. //check JWT
  19. jwt := security.GetJwt(r)
  20. // check whether this is a replicated write request
  21. var remoteLocations []operation.Location
  22. if r.FormValue("type") != "replicate" {
  23. // this is the initial request
  24. remoteLocations, err = getWritableRemoteReplications(s, volumeId, masterNode)
  25. if err != nil {
  26. glog.V(0).Infoln(err)
  27. return
  28. }
  29. }
  30. // read fsync value
  31. fsync := false
  32. if r.FormValue("fsync") == "true" {
  33. fsync = true
  34. }
  35. if s.GetVolume(volumeId) != nil {
  36. isUnchanged, err = s.WriteVolumeNeedle(volumeId, n, fsync)
  37. if err != nil {
  38. err = fmt.Errorf("failed to write to local disk: %v", err)
  39. glog.V(0).Infoln(err)
  40. return
  41. }
  42. }
  43. if len(remoteLocations) > 0 { //send to other replica locations
  44. if err = distributedOperation(remoteLocations, s, func(location operation.Location) error {
  45. u := url.URL{
  46. Scheme: "http",
  47. Host: location.Url,
  48. Path: r.URL.Path,
  49. }
  50. q := url.Values{
  51. "type": {"replicate"},
  52. "ttl": {n.Ttl.String()},
  53. }
  54. if n.LastModified > 0 {
  55. q.Set("ts", strconv.FormatUint(n.LastModified, 10))
  56. }
  57. if n.IsChunkedManifest() {
  58. q.Set("cm", "true")
  59. }
  60. u.RawQuery = q.Encode()
  61. pairMap := make(map[string]string)
  62. if n.HasPairs() {
  63. tmpMap := make(map[string]string)
  64. err := json.Unmarshal(n.Pairs, &tmpMap)
  65. if err != nil {
  66. glog.V(0).Infoln("Unmarshal pairs error:", err)
  67. }
  68. for k, v := range tmpMap {
  69. pairMap[needle.PairNamePrefix+k] = v
  70. }
  71. }
  72. // volume server do not know about encryption
  73. _, err := operation.UploadData(u.String(), string(n.Name), false, n.Data, n.IsCompressed(), string(n.Mime), pairMap, jwt)
  74. return err
  75. }); err != nil {
  76. err = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, err)
  77. glog.V(0).Infoln(err)
  78. }
  79. }
  80. return
  81. }
  82. func ReplicatedDelete(masterNode string, store *storage.Store,
  83. volumeId needle.VolumeId, n *needle.Needle,
  84. r *http.Request) (size uint32, err error) {
  85. //check JWT
  86. jwt := security.GetJwt(r)
  87. var remoteLocations []operation.Location
  88. if r.FormValue("type") != "replicate" {
  89. remoteLocations, err = getWritableRemoteReplications(store, volumeId, masterNode)
  90. if err != nil {
  91. glog.V(0).Infoln(err)
  92. return
  93. }
  94. }
  95. size, err = store.DeleteVolumeNeedle(volumeId, n)
  96. if err != nil {
  97. glog.V(0).Infoln("delete error:", err)
  98. return
  99. }
  100. if len(remoteLocations) > 0 { //send to other replica locations
  101. if err = distributedOperation(remoteLocations, store, func(location operation.Location) error {
  102. return util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", string(jwt))
  103. }); err != nil {
  104. size = 0
  105. }
  106. }
  107. return
  108. }
  109. type DistributedOperationResult map[string]error
  110. func (dr DistributedOperationResult) Error() error {
  111. var errs []string
  112. for k, v := range dr {
  113. if v != nil {
  114. errs = append(errs, fmt.Sprintf("[%s]: %v", k, v))
  115. }
  116. }
  117. if len(errs) == 0 {
  118. return nil
  119. }
  120. return errors.New(strings.Join(errs, "\n"))
  121. }
  122. type RemoteResult struct {
  123. Host string
  124. Error error
  125. }
  126. func distributedOperation(locations []operation.Location, store *storage.Store, op func(location operation.Location) error) error {
  127. length := len(locations)
  128. results := make(chan RemoteResult)
  129. for _, location := range locations {
  130. go func(location operation.Location, results chan RemoteResult) {
  131. results <- RemoteResult{location.Url, op(location)}
  132. }(location, results)
  133. }
  134. ret := DistributedOperationResult(make(map[string]error))
  135. for i := 0; i < length; i++ {
  136. result := <-results
  137. ret[result.Host] = result.Error
  138. }
  139. return ret.Error()
  140. }
  141. func getWritableRemoteReplications(s *storage.Store, volumeId needle.VolumeId, masterNode string) (
  142. remoteLocations []operation.Location, err error) {
  143. v := s.GetVolume(volumeId)
  144. if v != nil && v.ReplicaPlacement.GetCopyCount() == 1 {
  145. return
  146. }
  147. // not on local store, or has replications
  148. lookupResult, lookupErr := operation.Lookup(masterNode, volumeId.String())
  149. if lookupErr == nil {
  150. selfUrl := s.Ip + ":" + strconv.Itoa(s.Port)
  151. for _, location := range lookupResult.Locations {
  152. if location.Url != selfUrl {
  153. remoteLocations = append(remoteLocations, location)
  154. }
  155. }
  156. } else {
  157. err = fmt.Errorf("failed to lookup for %d: %v", volumeId, lookupErr)
  158. return
  159. }
  160. if v != nil {
  161. // has one local and has remote replications
  162. copyCount := v.ReplicaPlacement.GetCopyCount()
  163. if len(lookupResult.Locations) < copyCount {
  164. err = fmt.Errorf("replicating opetations [%d] is less than volume %d replication copy count [%d]",
  165. len(lookupResult.Locations), volumeId, copyCount)
  166. }
  167. }
  168. return
  169. }