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.

259 lines
6.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  12. "google.golang.org/grpc"
  13. )
  14. func (v *Volume) GetVolumeSyncStatus() *volume_server_pb.VolumeSyncStatusResponse {
  15. v.dataFileAccessLock.Lock()
  16. defer v.dataFileAccessLock.Unlock()
  17. var syncStatus = &volume_server_pb.VolumeSyncStatusResponse{}
  18. if stat, err := v.dataFile.Stat(); err == nil {
  19. syncStatus.TailOffset = uint64(stat.Size())
  20. }
  21. syncStatus.Collection = v.Collection
  22. syncStatus.IdxFileSize = v.nm.IndexFileSize()
  23. syncStatus.CompactRevision = uint32(v.SuperBlock.CompactionRevision)
  24. syncStatus.Ttl = v.SuperBlock.Ttl.String()
  25. syncStatus.Replication = v.SuperBlock.ReplicaPlacement.String()
  26. return syncStatus
  27. }
  28. // The volume sync with a master volume via 2 steps:
  29. // 1. The slave checks master side to find subscription checkpoint
  30. // to setup the replication.
  31. // 2. The slave receives the updates from master
  32. /*
  33. Assume the slave volume needs to follow the master volume.
  34. The master volume could be compacted, and could be many files ahead of
  35. slave volume.
  36. Step 0: // implemented in command/backup.go, to avoid dat file size overflow.
  37. 0.1 If slave compact version is less than the master, do a local compaction, and set
  38. local compact version the same as the master.
  39. 0.2 If the slave size is still bigger than the master, discard local copy and do a full copy.
  40. Step 1:
  41. The slave volume ask the master by the last modification time t.
  42. The master do a binary search in volume (use .idx as an array, and check the appendAtNs in .dat file),
  43. to find the first entry with appendAtNs > t.
  44. Step 2:
  45. The master send content bytes to the slave. The bytes are not chunked by needle.
  46. Step 3:
  47. The slave generate the needle map for the new bytes. (This may be optimized to incrementally
  48. update needle map when receiving new .dat bytes. But seems not necessary now.)
  49. */
  50. func (v *Volume) IncrementalBackup(volumeServer string, grpcDialOption grpc.DialOption) error {
  51. ctx := context.Background()
  52. startFromOffset, _, _ := v.FileStat()
  53. appendAtNs, err := v.findLastAppendAtNs()
  54. if err != nil {
  55. return err
  56. }
  57. err = operation.WithVolumeServerClient(volumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  58. stream, err := client.VolumeIncrementalCopy(ctx, &volume_server_pb.VolumeIncrementalCopyRequest{
  59. VolumeId: uint32(v.Id),
  60. SinceNs: appendAtNs,
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. v.dataFile.Seek(int64(startFromOffset), io.SeekStart)
  66. for {
  67. resp, recvErr := stream.Recv()
  68. if recvErr != nil {
  69. if recvErr == io.EOF {
  70. break
  71. } else {
  72. return recvErr
  73. }
  74. }
  75. _, writeErr := v.dataFile.Write(resp.FileContent)
  76. if writeErr != nil {
  77. return writeErr
  78. }
  79. }
  80. return nil
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. // add to needle map
  86. return ScanVolumeFileFrom(v.version, v.dataFile, int64(startFromOffset), &VolumeFileScanner4GenIdx{v: v})
  87. }
  88. func (v *Volume) findLastAppendAtNs() (uint64, error) {
  89. offset, err := v.locateLastAppendEntry()
  90. if err != nil {
  91. return 0, err
  92. }
  93. if offset.IsZero() {
  94. return 0, nil
  95. }
  96. return v.readAppendAtNs(offset)
  97. }
  98. func (v *Volume) locateLastAppendEntry() (Offset, error) {
  99. indexFile, e := os.OpenFile(v.FileName()+".idx", os.O_RDONLY, 0644)
  100. if e != nil {
  101. return Offset{}, fmt.Errorf("cannot read %s.idx: %v", v.FileName(), e)
  102. }
  103. defer indexFile.Close()
  104. fi, err := indexFile.Stat()
  105. if err != nil {
  106. return Offset{}, fmt.Errorf("file %s stat error: %v", indexFile.Name(), err)
  107. }
  108. fileSize := fi.Size()
  109. if fileSize%NeedleMapEntrySize != 0 {
  110. return Offset{}, fmt.Errorf("unexpected file %s size: %d", indexFile.Name(), fileSize)
  111. }
  112. if fileSize == 0 {
  113. return Offset{}, nil
  114. }
  115. bytes := make([]byte, NeedleMapEntrySize)
  116. n, e := indexFile.ReadAt(bytes, fileSize-NeedleMapEntrySize)
  117. if n != NeedleMapEntrySize {
  118. return Offset{}, fmt.Errorf("file %s read error: %v", indexFile.Name(), e)
  119. }
  120. _, offset, _ := idx.IdxFileEntry(bytes)
  121. return offset, nil
  122. }
  123. func (v *Volume) readAppendAtNs(offset Offset) (uint64, error) {
  124. n, _, bodyLength, err := needle.ReadNeedleHeader(v.dataFile, v.SuperBlock.version, offset.ToAcutalOffset())
  125. if err != nil {
  126. return 0, fmt.Errorf("ReadNeedleHeader: %v", err)
  127. }
  128. _, err = n.ReadNeedleBody(v.dataFile, v.SuperBlock.version, offset.ToAcutalOffset()+int64(NeedleHeaderSize), bodyLength)
  129. if err != nil {
  130. return 0, fmt.Errorf("ReadNeedleBody offset %d, bodyLength %d: %v", offset.ToAcutalOffset(), bodyLength, err)
  131. }
  132. return n.AppendAtNs, nil
  133. }
  134. // on server side
  135. func (v *Volume) BinarySearchByAppendAtNs(sinceNs uint64) (offset Offset, isLast bool, err error) {
  136. indexFile, openErr := os.OpenFile(v.FileName()+".idx", os.O_RDONLY, 0644)
  137. if openErr != nil {
  138. err = fmt.Errorf("cannot read %s.idx: %v", v.FileName(), openErr)
  139. return
  140. }
  141. defer indexFile.Close()
  142. fi, statErr := indexFile.Stat()
  143. if statErr != nil {
  144. err = fmt.Errorf("file %s stat error: %v", indexFile.Name(), statErr)
  145. return
  146. }
  147. fileSize := fi.Size()
  148. if fileSize%NeedleMapEntrySize != 0 {
  149. err = fmt.Errorf("unexpected file %s size: %d", indexFile.Name(), fileSize)
  150. return
  151. }
  152. bytes := make([]byte, NeedleMapEntrySize)
  153. entryCount := fileSize / NeedleMapEntrySize
  154. l := int64(0)
  155. h := entryCount
  156. for l < h {
  157. m := (l + h) / 2
  158. if m == entryCount {
  159. return Offset{}, true, nil
  160. }
  161. // read the appendAtNs for entry m
  162. offset, err = v.readAppendAtNsForIndexEntry(indexFile, bytes, m)
  163. if err != nil {
  164. return
  165. }
  166. mNs, nsReadErr := v.readAppendAtNs(offset)
  167. if nsReadErr != nil {
  168. err = nsReadErr
  169. return
  170. }
  171. // move the boundary
  172. if mNs <= sinceNs {
  173. l = m + 1
  174. } else {
  175. h = m
  176. }
  177. }
  178. if l == entryCount {
  179. return Offset{}, true, nil
  180. }
  181. offset, err = v.readAppendAtNsForIndexEntry(indexFile, bytes, l)
  182. return offset, false, err
  183. }
  184. // bytes is of size NeedleMapEntrySize
  185. func (v *Volume) readAppendAtNsForIndexEntry(indexFile *os.File, bytes []byte, m int64) (Offset, error) {
  186. if _, readErr := indexFile.ReadAt(bytes, m*NeedleMapEntrySize); readErr != nil && readErr != io.EOF {
  187. return Offset{}, readErr
  188. }
  189. _, offset, _ := idx.IdxFileEntry(bytes)
  190. return offset, nil
  191. }
  192. // generate the volume idx
  193. type VolumeFileScanner4GenIdx struct {
  194. v *Volume
  195. }
  196. func (scanner *VolumeFileScanner4GenIdx) VisitSuperBlock(superBlock SuperBlock) error {
  197. return nil
  198. }
  199. func (scanner *VolumeFileScanner4GenIdx) ReadNeedleBody() bool {
  200. return false
  201. }
  202. func (scanner *VolumeFileScanner4GenIdx) VisitNeedle(n *needle.Needle, offset int64) error {
  203. if n.Size > 0 && n.Size != TombstoneFileSize {
  204. return scanner.v.nm.Put(n.Id, ToOffset(offset), n.Size)
  205. }
  206. return scanner.v.nm.Delete(n.Id, ToOffset(offset))
  207. }