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.

185 lines
5.1 KiB

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