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.

218 lines
6.3 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
avoid slice out of bounds avoid this problem 2018/09/04 16:27:14 fuse: panic in handler for Write [ID=0x27c0d Node=0x2 Uid=0 Gid=0 Pid=0] 0x1 131072 @10607788032 fl=WriteCache lock=0 ffl=OpenReadOnly: runtime error: slice bounds out of range goroutine 211141 [running]: bazil.org/fuse/fs.(*Server).serve.func2(0x10d3e60, 0xc00014be30, 0xc00052fef8, 0xc00052fe77) /home/travis/gopath/src/bazil.org/fuse/fs/serve.go:857 +0x1ac panic(0xe2d080, 0x17f62b0) /home/travis/.gimme/versions/go/src/runtime/panic.go:513 +0x1b9 github.com/chrislusf/seaweedfs/weed/filesys.(*ContinuousDirtyPages).saveToStorage(0xc0000aca80, 0x10d7ba0, 0xc0003fcc00, 0xc0005dc000, 0x20000, 0x1000000, 0x276720000, 0xc0003feaa0, 0x0, 0x0) /home/travis/gopath/src/github.com/chrislusf/seaweedfs/weed/filesys/dirty_page.go:142 +0x8ec github.com/chrislusf/seaweedfs/weed/filesys.(*ContinuousDirtyPages).saveExistingPagesToStorage(0xc0000aca80, 0x10d7ba0, 0xc0003fcc00, 0x0, 0x0, 0x0) /home/travis/gopath/src/github.com/chrislusf/seaweedfs/weed/filesys/dirty_page.go:107 +0x6c github.com/chrislusf/seaweedfs/weed/filesys.(*ContinuousDirtyPages).AddPage(0xc0000aca80, 0x10d7ba0, 0xc0003fcc00, 0x278460000, 0xc011966050, 0x20000, 0x20fb0, 0x6fc23ac00, 0x4a817c800, 0x0, ...) /home/travis/gopath/src/github.com/chrislusf/seaweedfs/weed/filesys/dirty_page.go:70 +0x8f github.com/chrislusf/seaweedfs/weed/filesys.(*FileHandle).Write(0xc000548410, 0x10d7ba0, 0xc0003fcc00, 0xc00014be30, 0xc011946af8, 0x47fa01, 0x0) /home/travis/gopath/src/github.com/chrislusf/seaweedfs/weed/filesys/filehandle.go:141 +0x245 bazil.org/fuse/fs.(*Server).handleRequest(0xc0002cc0c0, 0x10d7ba0, 0xc0003fcc00, 0x10cb020, 0xc000394140, 0xc0000acac0, 0x10d3e60, 0xc00014be30, 0xc00052fef8, 0x10ca6a0, ...) /home/travis/gopath/src/bazil.org/fuse/fs/serve.go:1265 +0x1599 bazil.org/fuse/fs.(*Server).serve(0xc0002cc0c0, 0x10d3e60, 0xc00014be30) /home/travis/gopath/src/bazil.org/fuse/fs/serve.go:878 +0x410 bazil.org/fuse/fs.(*Server).Serve.func1(0xc0002cc0c0, 0x10d3e60, 0xc00014be30) /home/travis/gopath/src/bazil.org/fuse/fs/serve.go:425 +0x6e created by bazil.org/fuse/fs.(*Server).Serve /home/travis/gopath/src/bazil.org/fuse/fs/serve.go:423 +0x321
6 years ago
  1. package filesys
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "sync/atomic"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "sync"
  12. )
  13. type ContinuousDirtyPages struct {
  14. hasData bool
  15. Offset int64
  16. Size int64
  17. Data []byte
  18. f *File
  19. lock sync.Mutex
  20. }
  21. func newDirtyPages(file *File) *ContinuousDirtyPages {
  22. return &ContinuousDirtyPages{
  23. Data: nil,
  24. f: file,
  25. }
  26. }
  27. func (pages *ContinuousDirtyPages) releaseResource() {
  28. if pages.Data != nil {
  29. pages.f.wfs.bufPool.Put(pages.Data)
  30. pages.Data = nil
  31. atomic.AddInt32(&counter, -1)
  32. glog.V(3).Infof("%s/%s releasing resource %d", pages.f.dir.Path, pages.f.Name, counter)
  33. }
  34. }
  35. var counter = int32(0)
  36. func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  37. pages.lock.Lock()
  38. defer pages.lock.Unlock()
  39. var chunk *filer_pb.FileChunk
  40. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  41. // this is more than what buffer can hold.
  42. return pages.flushAndSave(ctx, offset, data)
  43. }
  44. if pages.Data == nil {
  45. pages.Data = pages.f.wfs.bufPool.Get().([]byte)
  46. atomic.AddInt32(&counter, 1)
  47. glog.V(3).Infof("%s/%s acquire resource %d", pages.f.dir.Path, pages.f.Name, counter)
  48. }
  49. if offset < pages.Offset || offset >= pages.Offset+int64(len(pages.Data)) ||
  50. pages.Offset+int64(len(pages.Data)) < offset+int64(len(data)) {
  51. // if the data is out of range,
  52. // or buffer is full if adding new data,
  53. // flush current buffer and add new data
  54. // println("offset", offset, "size", len(data), "existing offset", pages.Offset, "size", pages.Size)
  55. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  56. if chunk != nil {
  57. glog.V(4).Infof("%s/%s add save [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  58. chunks = append(chunks, chunk)
  59. }
  60. } else {
  61. glog.V(0).Infof("%s/%s add save [%d,%d): %v", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
  62. return
  63. }
  64. pages.Offset = offset
  65. copy(pages.Data, data)
  66. pages.Size = int64(len(data))
  67. return
  68. }
  69. if offset != pages.Offset+pages.Size {
  70. // when this happens, debug shows the data overlapping with existing data is empty
  71. // the data is not just append
  72. if offset == pages.Offset && int(pages.Size) < len(data) {
  73. // glog.V(2).Infof("pages[%d,%d) pages.Data len=%v, data len=%d, pages.Size=%d", pages.Offset, pages.Offset+pages.Size, len(pages.Data), len(data), pages.Size)
  74. copy(pages.Data[pages.Size:], data[pages.Size:])
  75. } else {
  76. if pages.Size != 0 {
  77. glog.V(1).Infof("%s/%s add page: pages [%d, %d) write [%d, %d)", pages.f.dir.Path, pages.f.Name, pages.Offset, pages.Offset+pages.Size, offset, offset+int64(len(data)))
  78. }
  79. return pages.flushAndSave(ctx, offset, data)
  80. }
  81. } else {
  82. copy(pages.Data[offset-pages.Offset:], data)
  83. }
  84. pages.Size = max(pages.Size, offset+int64(len(data))-pages.Offset)
  85. return
  86. }
  87. func (pages *ContinuousDirtyPages) flushAndSave(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  88. var chunk *filer_pb.FileChunk
  89. // flush existing
  90. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  91. if chunk != nil {
  92. glog.V(4).Infof("%s/%s flush existing [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  93. chunks = append(chunks, chunk)
  94. }
  95. } else {
  96. glog.V(0).Infof("%s/%s failed to flush1 [%d,%d): %v", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
  97. return
  98. }
  99. pages.Size = 0
  100. pages.Offset = 0
  101. // flush the new page
  102. if chunk, err = pages.saveToStorage(ctx, data, offset); err == nil {
  103. if chunk != nil {
  104. glog.V(4).Infof("%s/%s flush big request [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  105. chunks = append(chunks, chunk)
  106. }
  107. } else {
  108. 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)
  109. return
  110. }
  111. return
  112. }
  113. func (pages *ContinuousDirtyPages) FlushToStorage(ctx context.Context) (chunk *filer_pb.FileChunk, err error) {
  114. pages.lock.Lock()
  115. defer pages.lock.Unlock()
  116. if pages.Size == 0 {
  117. return nil, nil
  118. }
  119. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  120. pages.Size = 0
  121. pages.Offset = 0
  122. if chunk != nil {
  123. glog.V(4).Infof("%s/%s flush [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  124. }
  125. }
  126. return
  127. }
  128. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage(ctx context.Context) (*filer_pb.FileChunk, error) {
  129. if pages.Size == 0 {
  130. return nil, nil
  131. }
  132. return pages.saveToStorage(ctx, pages.Data[:pages.Size], pages.Offset)
  133. }
  134. func (pages *ContinuousDirtyPages) saveToStorage(ctx context.Context, buf []byte, offset int64) (*filer_pb.FileChunk, error) {
  135. var fileId, host string
  136. if err := pages.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  137. request := &filer_pb.AssignVolumeRequest{
  138. Count: 1,
  139. Replication: pages.f.wfs.option.Replication,
  140. Collection: pages.f.wfs.option.Collection,
  141. TtlSec: pages.f.wfs.option.TtlSec,
  142. DataCenter: pages.f.wfs.option.DataCenter,
  143. }
  144. resp, err := client.AssignVolume(ctx, request)
  145. if err != nil {
  146. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  147. return err
  148. }
  149. fileId, host = resp.FileId, resp.Url
  150. return nil
  151. }); err != nil {
  152. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  153. }
  154. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  155. bufReader := bytes.NewReader(buf)
  156. uploadResult, err := operation.Upload(fileUrl, pages.f.Name, bufReader, false, "application/octet-stream", nil, "")
  157. if err != nil {
  158. glog.V(0).Infof("upload data %v to %s: %v", pages.f.Name, fileUrl, err)
  159. return nil, fmt.Errorf("upload data: %v", err)
  160. }
  161. if uploadResult.Error != "" {
  162. glog.V(0).Infof("upload failure %v to %s: %v", pages.f.Name, fileUrl, err)
  163. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  164. }
  165. return &filer_pb.FileChunk{
  166. FileId: fileId,
  167. Offset: offset,
  168. Size: uint64(len(buf)),
  169. Mtime: time.Now().UnixNano(),
  170. ETag: uploadResult.ETag,
  171. }, nil
  172. }
  173. func max(x, y int64) int64 {
  174. if x > y {
  175. return x
  176. }
  177. return y
  178. }