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.

212 lines
5.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. package filesys
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. )
  14. type ContinuousDirtyPages struct {
  15. intervals *ContinuousIntervals
  16. f *File
  17. lock sync.Mutex
  18. }
  19. func newDirtyPages(file *File) *ContinuousDirtyPages {
  20. return &ContinuousDirtyPages{
  21. intervals: &ContinuousIntervals{},
  22. f: file,
  23. }
  24. }
  25. func (pages *ContinuousDirtyPages) releaseResource() {
  26. }
  27. var counter = int32(0)
  28. func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  29. pages.lock.Lock()
  30. defer pages.lock.Unlock()
  31. glog.V(3).Infof("%s AddPage [%d,%d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  32. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  33. // this is more than what buffer can hold.
  34. return pages.flushAndSave(ctx, offset, data)
  35. }
  36. hasOverlap := pages.intervals.AddInterval(data, offset)
  37. if hasOverlap {
  38. chunks, err = pages.saveExistingPagesToStorage(ctx)
  39. pages.intervals.AddInterval(data, offset)
  40. return
  41. }
  42. var chunk *filer_pb.FileChunk
  43. var hasSavedData bool
  44. if pages.intervals.TotalSize() > pages.f.wfs.option.ChunkSizeLimit {
  45. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage(ctx)
  46. if hasSavedData {
  47. chunks = append(chunks, chunk)
  48. }
  49. }
  50. return
  51. }
  52. func (pages *ContinuousDirtyPages) flushAndSave(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  53. var chunk *filer_pb.FileChunk
  54. var newChunks []*filer_pb.FileChunk
  55. // flush existing
  56. if newChunks, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  57. if newChunks != nil {
  58. chunks = append(chunks, newChunks...)
  59. }
  60. } else {
  61. return
  62. }
  63. // flush the new page
  64. if chunk, err = pages.saveToStorage(ctx, bytes.NewReader(data), offset, int64(len(data))); err == nil {
  65. if chunk != nil {
  66. glog.V(4).Infof("%s/%s flush big request [%d,%d) to %s", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.FileId)
  67. chunks = append(chunks, chunk)
  68. }
  69. } else {
  70. glog.V(0).Infof("%s/%s failed to flush2 [%d,%d): %v", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
  71. return
  72. }
  73. return
  74. }
  75. func (pages *ContinuousDirtyPages) FlushToStorage(ctx context.Context) (chunks []*filer_pb.FileChunk, err error) {
  76. pages.lock.Lock()
  77. defer pages.lock.Unlock()
  78. return pages.saveExistingPagesToStorage(ctx)
  79. }
  80. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage(ctx context.Context) (chunks []*filer_pb.FileChunk, err error) {
  81. var hasSavedData bool
  82. var chunk *filer_pb.FileChunk
  83. for {
  84. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage(ctx)
  85. if !hasSavedData {
  86. return chunks, err
  87. }
  88. if err == nil {
  89. chunks = append(chunks, chunk)
  90. } else {
  91. return
  92. }
  93. }
  94. }
  95. func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage(ctx context.Context) (chunk *filer_pb.FileChunk, hasSavedData bool, err error) {
  96. maxList := pages.intervals.RemoveLargestIntervalLinkedList()
  97. if maxList == nil {
  98. return nil, false, nil
  99. }
  100. chunk, err = pages.saveToStorage(ctx, maxList.ToReader(), maxList.Offset(), maxList.Size())
  101. if err == nil {
  102. hasSavedData = true
  103. glog.V(3).Infof("%s saveToStorage [%d,%d) %s", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), chunk.FileId)
  104. } else {
  105. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), err)
  106. return
  107. }
  108. return
  109. }
  110. func (pages *ContinuousDirtyPages) saveToStorage(ctx context.Context, reader io.Reader, offset int64, size int64) (*filer_pb.FileChunk, error) {
  111. var fileId, host string
  112. var auth security.EncodedJwt
  113. if err := pages.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  114. request := &filer_pb.AssignVolumeRequest{
  115. Count: 1,
  116. Replication: pages.f.wfs.option.Replication,
  117. Collection: pages.f.wfs.option.Collection,
  118. TtlSec: pages.f.wfs.option.TtlSec,
  119. DataCenter: pages.f.wfs.option.DataCenter,
  120. }
  121. resp, err := client.AssignVolume(ctx, request)
  122. if err != nil {
  123. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  124. return err
  125. }
  126. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  127. return nil
  128. }); err != nil {
  129. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  130. }
  131. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  132. uploadResult, err := operation.Upload(fileUrl, pages.f.Name, reader, false, "", nil, auth)
  133. if err != nil {
  134. glog.V(0).Infof("upload data %v to %s: %v", pages.f.Name, fileUrl, err)
  135. return nil, fmt.Errorf("upload data: %v", err)
  136. }
  137. if uploadResult.Error != "" {
  138. glog.V(0).Infof("upload failure %v to %s: %v", pages.f.Name, fileUrl, err)
  139. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  140. }
  141. return &filer_pb.FileChunk{
  142. FileId: fileId,
  143. Offset: offset,
  144. Size: uint64(size),
  145. Mtime: time.Now().UnixNano(),
  146. ETag: uploadResult.ETag,
  147. }, nil
  148. }
  149. func max(x, y int64) int64 {
  150. if x > y {
  151. return x
  152. }
  153. return y
  154. }
  155. func min(x, y int64) int64 {
  156. if x < y {
  157. return x
  158. }
  159. return y
  160. }
  161. func (pages *ContinuousDirtyPages) ReadDirtyData(ctx context.Context, data []byte, startOffset int64) (offset int64, size int) {
  162. pages.lock.Lock()
  163. defer pages.lock.Unlock()
  164. return pages.intervals.ReadData(data, startOffset)
  165. }