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.

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