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.

220 lines
6.4 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 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"
  7. "sync/atomic"
  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. hasData bool
  16. Offset int64
  17. Size int64
  18. Data []byte
  19. f *File
  20. lock sync.Mutex
  21. }
  22. func newDirtyPages(file *File) *ContinuousDirtyPages {
  23. return &ContinuousDirtyPages{
  24. Data: nil,
  25. f: file,
  26. }
  27. }
  28. func (pages *ContinuousDirtyPages) releaseResource() {
  29. if pages.Data != nil {
  30. pages.f.wfs.bufPool.Put(pages.Data)
  31. pages.Data = nil
  32. atomic.AddInt32(&counter, -1)
  33. glog.V(3).Infof("%s/%s releasing resource %d", pages.f.dir.Path, pages.f.Name, counter)
  34. }
  35. }
  36. var counter = int32(0)
  37. func (pages *ContinuousDirtyPages) AddPage(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  38. pages.lock.Lock()
  39. defer pages.lock.Unlock()
  40. var chunk *filer_pb.FileChunk
  41. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  42. // this is more than what buffer can hold.
  43. return pages.flushAndSave(ctx, offset, data)
  44. }
  45. if pages.Data == nil {
  46. pages.Data = pages.f.wfs.bufPool.Get().([]byte)
  47. atomic.AddInt32(&counter, 1)
  48. glog.V(3).Infof("%s/%s acquire resource %d", pages.f.dir.Path, pages.f.Name, counter)
  49. }
  50. if offset < pages.Offset || offset >= pages.Offset+int64(len(pages.Data)) ||
  51. pages.Offset+int64(len(pages.Data)) < offset+int64(len(data)) {
  52. // if the data is out of range,
  53. // or buffer is full if adding new data,
  54. // flush current buffer and add new data
  55. // println("offset", offset, "size", len(data), "existing offset", pages.Offset, "size", pages.Size)
  56. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  57. if chunk != nil {
  58. glog.V(4).Infof("%s/%s add save [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  59. chunks = append(chunks, chunk)
  60. }
  61. } else {
  62. 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)
  63. return
  64. }
  65. pages.Offset = offset
  66. copy(pages.Data, data)
  67. pages.Size = int64(len(data))
  68. return
  69. }
  70. if offset != pages.Offset+pages.Size {
  71. // when this happens, debug shows the data overlapping with existing data is empty
  72. // the data is not just append
  73. if offset == pages.Offset && int(pages.Size) < len(data) {
  74. // 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)
  75. copy(pages.Data[pages.Size:], data[pages.Size:])
  76. } else {
  77. if pages.Size != 0 {
  78. 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)))
  79. }
  80. return pages.flushAndSave(ctx, offset, data)
  81. }
  82. } else {
  83. copy(pages.Data[offset-pages.Offset:], data)
  84. }
  85. pages.Size = max(pages.Size, offset+int64(len(data))-pages.Offset)
  86. return
  87. }
  88. func (pages *ContinuousDirtyPages) flushAndSave(ctx context.Context, offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  89. var chunk *filer_pb.FileChunk
  90. // flush existing
  91. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  92. if chunk != nil {
  93. glog.V(4).Infof("%s/%s flush existing [%d,%d) to %s", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.FileId)
  94. chunks = append(chunks, chunk)
  95. }
  96. } else {
  97. 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)
  98. return
  99. }
  100. pages.Size = 0
  101. pages.Offset = 0
  102. // flush the new page
  103. if chunk, err = pages.saveToStorage(ctx, data, offset); err == nil {
  104. if chunk != nil {
  105. 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)
  106. chunks = append(chunks, chunk)
  107. }
  108. } else {
  109. 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)
  110. return
  111. }
  112. return
  113. }
  114. func (pages *ContinuousDirtyPages) FlushToStorage(ctx context.Context) (chunk *filer_pb.FileChunk, err error) {
  115. pages.lock.Lock()
  116. defer pages.lock.Unlock()
  117. if pages.Size == 0 {
  118. return nil, nil
  119. }
  120. if chunk, err = pages.saveExistingPagesToStorage(ctx); err == nil {
  121. pages.Size = 0
  122. pages.Offset = 0
  123. if chunk != nil {
  124. glog.V(4).Infof("%s/%s flush [%d,%d)", pages.f.dir.Path, pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size))
  125. }
  126. }
  127. return
  128. }
  129. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage(ctx context.Context) (*filer_pb.FileChunk, error) {
  130. if pages.Size == 0 {
  131. return nil, nil
  132. }
  133. return pages.saveToStorage(ctx, pages.Data[:pages.Size], pages.Offset)
  134. }
  135. func (pages *ContinuousDirtyPages) saveToStorage(ctx context.Context, buf []byte, offset int64) (*filer_pb.FileChunk, error) {
  136. var fileId, host string
  137. var auth security.EncodedJwt
  138. if err := pages.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  139. request := &filer_pb.AssignVolumeRequest{
  140. Count: 1,
  141. Replication: pages.f.wfs.option.Replication,
  142. Collection: pages.f.wfs.option.Collection,
  143. TtlSec: pages.f.wfs.option.TtlSec,
  144. DataCenter: pages.f.wfs.option.DataCenter,
  145. }
  146. resp, err := client.AssignVolume(ctx, request)
  147. if err != nil {
  148. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  149. return err
  150. }
  151. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  152. return nil
  153. }); err != nil {
  154. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  155. }
  156. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  157. bufReader := bytes.NewReader(buf)
  158. uploadResult, err := operation.Upload(fileUrl, pages.f.Name, bufReader, false, "application/octet-stream", nil, auth)
  159. if err != nil {
  160. glog.V(0).Infof("upload data %v to %s: %v", pages.f.Name, fileUrl, err)
  161. return nil, fmt.Errorf("upload data: %v", err)
  162. }
  163. if uploadResult.Error != "" {
  164. glog.V(0).Infof("upload failure %v to %s: %v", pages.f.Name, fileUrl, err)
  165. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  166. }
  167. return &filer_pb.FileChunk{
  168. FileId: fileId,
  169. Offset: offset,
  170. Size: uint64(len(buf)),
  171. Mtime: time.Now().UnixNano(),
  172. ETag: uploadResult.ETag,
  173. }, nil
  174. }
  175. func max(x, y int64) int64 {
  176. if x > y {
  177. return x
  178. }
  179. return y
  180. }