store_replicate.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/valyala/fasthttp"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/security"
  15. "github.com/chrislusf/seaweedfs/weed/storage"
  16. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. )
  19. func OldReplicatedWrite(masterNode string, s *storage.Store,
  20. volumeId needle.VolumeId, n *needle.Needle,
  21. r *http.Request) (size uint32, isUnchanged bool, err error) {
  22. //check JWT
  23. jwt := security.OldGetJwt(r)
  24. var remoteLocations []operation.Location
  25. if r.FormValue("type") != "replicate" {
  26. remoteLocations, err = getWritableRemoteReplications(s, volumeId, masterNode)
  27. if err != nil {
  28. glog.V(0).Infoln(err)
  29. return
  30. }
  31. }
  32. size, isUnchanged, err = s.WriteVolumeNeedle(volumeId, n)
  33. if err != nil {
  34. err = fmt.Errorf("failed to write to local disk: %v", err)
  35. glog.V(0).Infoln(err)
  36. return
  37. }
  38. if len(remoteLocations) > 0 { //send to other replica locations
  39. if err = distributedOperation(remoteLocations, s, func(location operation.Location) error {
  40. u := url.URL{
  41. Scheme: "http",
  42. Host: location.Url,
  43. Path: r.URL.Path,
  44. }
  45. q := url.Values{
  46. "type": {"replicate"},
  47. "ttl": {n.Ttl.String()},
  48. }
  49. if n.LastModified > 0 {
  50. q.Set("ts", strconv.FormatUint(n.LastModified, 10))
  51. }
  52. if n.IsChunkedManifest() {
  53. q.Set("cm", "true")
  54. }
  55. u.RawQuery = q.Encode()
  56. pairMap := make(map[string]string)
  57. if n.HasPairs() {
  58. tmpMap := make(map[string]string)
  59. err := json.Unmarshal(n.Pairs, &tmpMap)
  60. if err != nil {
  61. glog.V(0).Infoln("Unmarshal pairs error:", err)
  62. }
  63. for k, v := range tmpMap {
  64. pairMap[needle.PairNamePrefix+k] = v
  65. }
  66. }
  67. _, err := operation.Upload(u.String(),
  68. string(n.Name), bytes.NewReader(n.Data), n.IsGzipped(), string(n.Mime),
  69. pairMap, jwt)
  70. return err
  71. }); err != nil {
  72. size = 0
  73. err = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, err)
  74. glog.V(0).Infoln(err)
  75. }
  76. }
  77. return
  78. }
  79. func ReplicatedWrite(masterNode string, s *storage.Store,
  80. volumeId needle.VolumeId, n *needle.Needle,
  81. ctx *fasthttp.RequestCtx) (size uint32, isUnchanged bool, err error) {
  82. //check JWT
  83. jwt := security.GetJwt(ctx)
  84. var remoteLocations []operation.Location
  85. if string(ctx.FormValue("type")) != "replicate" {
  86. remoteLocations, err = getWritableRemoteReplications(s, volumeId, masterNode)
  87. if err != nil {
  88. glog.V(0).Infoln(err)
  89. return
  90. }
  91. }
  92. size, isUnchanged, err = s.WriteVolumeNeedle(volumeId, n)
  93. if err != nil {
  94. err = fmt.Errorf("failed to write to local disk: %v", err)
  95. glog.V(0).Infoln(err)
  96. return
  97. }
  98. if len(remoteLocations) > 0 { //send to other replica locations
  99. if err = distributedOperation(remoteLocations, s, func(location operation.Location) error {
  100. u := url.URL{
  101. Scheme: "http",
  102. Host: location.Url,
  103. Path: string(ctx.Path()),
  104. }
  105. q := url.Values{
  106. "type": {"replicate"},
  107. "ttl": {n.Ttl.String()},
  108. }
  109. if n.LastModified > 0 {
  110. q.Set("ts", strconv.FormatUint(n.LastModified, 10))
  111. }
  112. if n.IsChunkedManifest() {
  113. q.Set("cm", "true")
  114. }
  115. u.RawQuery = q.Encode()
  116. pairMap := make(map[string]string)
  117. if n.HasPairs() {
  118. tmpMap := make(map[string]string)
  119. err := json.Unmarshal(n.Pairs, &tmpMap)
  120. if err != nil {
  121. glog.V(0).Infoln("Unmarshal pairs error:", err)
  122. }
  123. for k, v := range tmpMap {
  124. pairMap[needle.PairNamePrefix+k] = v
  125. }
  126. }
  127. _, err := operation.Upload(u.String(),
  128. string(n.Name), bytes.NewReader(n.Data), n.IsGzipped(), string(n.Mime),
  129. pairMap, jwt)
  130. return err
  131. }); err != nil {
  132. size = 0
  133. err = fmt.Errorf("failed to write to replicas for volume %d: %v", volumeId, err)
  134. glog.V(0).Infoln(err)
  135. }
  136. }
  137. return
  138. }
  139. func OldReplicatedDelete(masterNode string, store *storage.Store,
  140. volumeId needle.VolumeId, n *needle.Needle,
  141. r *http.Request) (size uint32, err error) {
  142. //check JWT
  143. jwt := security.OldGetJwt(r)
  144. var remoteLocations []operation.Location
  145. if r.FormValue("type") != "replicate" {
  146. remoteLocations, err = getWritableRemoteReplications(store, volumeId, masterNode)
  147. if err != nil {
  148. glog.V(0).Infoln(err)
  149. return
  150. }
  151. }
  152. size, err = store.DeleteVolumeNeedle(volumeId, n)
  153. if err != nil {
  154. glog.V(0).Infoln("delete error:", err)
  155. return
  156. }
  157. if len(remoteLocations) > 0 { //send to other replica locations
  158. if err = distributedOperation(remoteLocations, store, func(location operation.Location) error {
  159. return util.Delete("http://"+location.Url+r.URL.Path+"?type=replicate", string(jwt))
  160. }); err != nil {
  161. size = 0
  162. }
  163. }
  164. return
  165. }
  166. func ReplicatedDelete(masterNode string, store *storage.Store,
  167. volumeId needle.VolumeId, n *needle.Needle,
  168. ctx *fasthttp.RequestCtx) (size uint32, err error) {
  169. //check JWT
  170. jwt := security.GetJwt(ctx)
  171. var remoteLocations []operation.Location
  172. if string(ctx.FormValue("type")) != "replicate" {
  173. remoteLocations, err = getWritableRemoteReplications(store, volumeId, masterNode)
  174. if err != nil {
  175. glog.V(0).Infoln(err)
  176. return
  177. }
  178. }
  179. size, err = store.DeleteVolumeNeedle(volumeId, n)
  180. if err != nil {
  181. glog.V(0).Infoln("delete error:", err)
  182. return
  183. }
  184. if len(remoteLocations) > 0 { //send to other replica locations
  185. if err = distributedOperation(remoteLocations, store, func(location operation.Location) error {
  186. return util.Delete("http://"+location.Url+string(ctx.Path())+"?type=replicate", string(jwt))
  187. }); err != nil {
  188. size = 0
  189. }
  190. }
  191. return
  192. }
  193. type DistributedOperationResult map[string]error
  194. func (dr DistributedOperationResult) Error() error {
  195. var errs []string
  196. for k, v := range dr {
  197. if v != nil {
  198. errs = append(errs, fmt.Sprintf("[%s]: %v", k, v))
  199. }
  200. }
  201. if len(errs) == 0 {
  202. return nil
  203. }
  204. return errors.New(strings.Join(errs, "\n"))
  205. }
  206. type RemoteResult struct {
  207. Host string
  208. Error error
  209. }
  210. func distributedOperation(locations []operation.Location, store *storage.Store, op func(location operation.Location) error) error {
  211. length := len(locations)
  212. results := make(chan RemoteResult)
  213. for _, location := range locations {
  214. go func(location operation.Location, results chan RemoteResult) {
  215. results <- RemoteResult{location.Url, op(location)}
  216. }(location, results)
  217. }
  218. ret := DistributedOperationResult(make(map[string]error))
  219. for i := 0; i < length; i++ {
  220. result := <-results
  221. ret[result.Host] = result.Error
  222. }
  223. return ret.Error()
  224. }
  225. func getWritableRemoteReplications(s *storage.Store, volumeId needle.VolumeId, masterNode string) (
  226. remoteLocations []operation.Location, err error) {
  227. copyCount := s.GetVolume(volumeId).ReplicaPlacement.GetCopyCount()
  228. if copyCount > 1 {
  229. if lookupResult, lookupErr := operation.Lookup(masterNode, volumeId.String()); lookupErr == nil {
  230. if len(lookupResult.Locations) < copyCount {
  231. err = fmt.Errorf("replicating opetations [%d] is less than volume's replication copy count [%d]",
  232. len(lookupResult.Locations), copyCount)
  233. return
  234. }
  235. selfUrl := s.Ip + ":" + strconv.Itoa(s.Port)
  236. for _, location := range lookupResult.Locations {
  237. if location.Url != selfUrl {
  238. remoteLocations = append(remoteLocations, location)
  239. }
  240. }
  241. } else {
  242. err = fmt.Errorf("failed to lookup for %d: %v", volumeId, lookupErr)
  243. return
  244. }
  245. }
  246. return
  247. }