store_replicate.go 5.5 KB

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