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.

246 lines
7.2 KiB

4 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "context"
  5. "flag"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  10. "io"
  11. "math"
  12. "sort"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandVolumeCheckDisk{})
  16. }
  17. type commandVolumeCheckDisk struct {
  18. env *CommandEnv
  19. }
  20. func (c *commandVolumeCheckDisk) Name() string {
  21. return "volume.check.disk"
  22. }
  23. func (c *commandVolumeCheckDisk) Help() string {
  24. return `check all replicated volumes to find and fix inconsistencies
  25. How it works:
  26. find all volumes that are replicated
  27. for each volume id, if there are more than 2 replicas, find one pair with the largest 2 in file count.
  28. for the pair volume A and B
  29. append entries in A and not in B to B
  30. append entries in B and not in A to A
  31. `
  32. }
  33. func (c *commandVolumeCheckDisk) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  34. if err = commandEnv.confirmIsLocked(); err != nil {
  35. return
  36. }
  37. fsckCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  38. slowMode := fsckCommand.Bool("slow", false, "slow mode checks all replicas even file counts are the same")
  39. verbose := fsckCommand.Bool("v", false, "verbose mode")
  40. applyChanges := fsckCommand.Bool("force", false, "apply the fix")
  41. if err = fsckCommand.Parse(args); err != nil {
  42. return nil
  43. }
  44. c.env = commandEnv
  45. // collect topology information
  46. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  47. if err != nil {
  48. return err
  49. }
  50. volumeReplicas, _ := collectVolumeReplicaLocations(topologyInfo)
  51. // pick 1 pairs of volume replica
  52. fileCount := func(replica *VolumeReplica) uint64 {
  53. return replica.info.FileCount - replica.info.DeleteCount
  54. }
  55. aDB, bDB := needle_map.NewMemDb(), needle_map.NewMemDb()
  56. defer aDB.Close()
  57. defer bDB.Close()
  58. for _, replicas := range volumeReplicas {
  59. sort.Slice(replicas, func(i, j int) bool {
  60. return fileCount(replicas[i]) > fileCount(replicas[j])
  61. })
  62. for len(replicas) >= 2 {
  63. a, b := replicas[0], replicas[1]
  64. if !*slowMode {
  65. if fileCount(a) == fileCount(b) {
  66. replicas = replicas[1:]
  67. continue
  68. }
  69. }
  70. if a.info.ReadOnly || b.info.ReadOnly {
  71. fmt.Fprintf(writer, "skipping readonly volume %d on %s and %s\n", a.info.Id, a.location.dataNode.Id, b.location.dataNode.Id)
  72. continue
  73. }
  74. // reset index db
  75. aDB.Close()
  76. bDB.Close()
  77. aDB, bDB = needle_map.NewMemDb(), needle_map.NewMemDb()
  78. // read index db
  79. if err := c.readIndexDatabase(aDB, a.info.Collection, a.info.Id, a.location.dataNode.Id, *verbose, writer); err != nil {
  80. return err
  81. }
  82. if err := c.readIndexDatabase(bDB, b.info.Collection, b.info.Id, b.location.dataNode.Id, *verbose, writer); err != nil {
  83. return err
  84. }
  85. // find and make up the differnces
  86. if err := c.doVolumeCheckDisk(aDB, bDB, a, b, *verbose, writer, *applyChanges); err != nil {
  87. return err
  88. }
  89. if err := c.doVolumeCheckDisk(bDB, aDB, b, a, *verbose, writer, *applyChanges); err != nil {
  90. return err
  91. }
  92. replicas = replicas[1:]
  93. }
  94. }
  95. return nil
  96. }
  97. func (c *commandVolumeCheckDisk) doVolumeCheckDisk(subtrahend, minuend *needle_map.MemDb, source, target *VolumeReplica, verbose bool, writer io.Writer, applyChanges bool) error {
  98. // find missing keys
  99. // hash join, can be more efficient
  100. var missingNeedles []needle_map.NeedleValue
  101. var counter int
  102. subtrahend.AscendingVisit(func(value needle_map.NeedleValue) error {
  103. counter++
  104. if _, found := minuend.Get(value.Key); !found {
  105. missingNeedles = append(missingNeedles, value)
  106. }
  107. return nil
  108. })
  109. fmt.Fprintf(writer, "volume %d %s has %d entries, %s missed %d entries\n", source.info.Id, source.location.dataNode.Id, counter, target.location.dataNode.Id, len(missingNeedles))
  110. for _, needleValue := range missingNeedles {
  111. needleBlob, err := c.readSourceNeedleBlob(source.location.dataNode.Id, source.info.Id, needleValue)
  112. if err != nil {
  113. return err
  114. }
  115. if !applyChanges {
  116. continue
  117. }
  118. if verbose {
  119. fmt.Fprintf(writer, "read %d,%x %s => %s \n", source.info.Id, needleValue.Key, source.location.dataNode.Id, target.location.dataNode.Id)
  120. }
  121. if err := c.writeNeedleBlobToTarget(target.location.dataNode.Id, source.info.Id, needleValue, needleBlob); err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }
  127. func (c *commandVolumeCheckDisk) readSourceNeedleBlob(sourceVolumeServer string, volumeId uint32, needleValue needle_map.NeedleValue) (needleBlob []byte, err error) {
  128. err = operation.WithVolumeServerClient(sourceVolumeServer, c.env.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  129. resp, err := client.ReadNeedleBlob(context.Background(), &volume_server_pb.ReadNeedleBlobRequest{
  130. VolumeId: volumeId,
  131. NeedleId: uint64(needleValue.Key),
  132. Offset: needleValue.Offset.ToActualOffset(),
  133. Size: int32(needleValue.Size),
  134. })
  135. if err != nil {
  136. return err
  137. }
  138. needleBlob = resp.NeedleBlob
  139. return nil
  140. })
  141. return
  142. }
  143. func (c *commandVolumeCheckDisk) writeNeedleBlobToTarget(targetVolumeServer string, volumeId uint32, needleValue needle_map.NeedleValue, needleBlob []byte) error {
  144. return operation.WithVolumeServerClient(targetVolumeServer, c.env.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  145. _, err := client.WriteNeedleBlob(context.Background(), &volume_server_pb.WriteNeedleBlobRequest{
  146. VolumeId: volumeId,
  147. NeedleId: uint64(needleValue.Key),
  148. Size: int32(needleValue.Size),
  149. NeedleBlob: needleBlob,
  150. })
  151. return err
  152. })
  153. }
  154. func (c *commandVolumeCheckDisk) readIndexDatabase(db *needle_map.MemDb, collection string, volumeId uint32, volumeServer string, verbose bool, writer io.Writer) error {
  155. var buf bytes.Buffer
  156. if err := c.copyVolumeIndexFile(collection, volumeId, volumeServer, &buf, verbose, writer); err != nil {
  157. return err
  158. }
  159. if verbose {
  160. fmt.Fprintf(writer, "load collection %s volume %d index size %d from %s ...\n", collection, volumeId, buf.Len(), volumeServer)
  161. }
  162. return db.LoadFromReaderAt(bytes.NewReader(buf.Bytes()))
  163. }
  164. func (c *commandVolumeCheckDisk) copyVolumeIndexFile(collection string, volumeId uint32, volumeServer string, buf *bytes.Buffer, verbose bool, writer io.Writer) error {
  165. return operation.WithVolumeServerClient(volumeServer, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  166. ext := ".idx"
  167. copyFileClient, err := volumeServerClient.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  168. VolumeId: volumeId,
  169. Ext: ".idx",
  170. CompactionRevision: math.MaxUint32,
  171. StopOffset: math.MaxInt64,
  172. Collection: collection,
  173. IsEcVolume: false,
  174. IgnoreSourceFileNotFound: false,
  175. })
  176. if err != nil {
  177. return fmt.Errorf("failed to start copying volume %d%s: %v", volumeId, ext, err)
  178. }
  179. err = writeToBuffer(copyFileClient, buf)
  180. if err != nil {
  181. return fmt.Errorf("failed to copy %d%s from %s: %v", volumeId, ext, volumeServer, err)
  182. }
  183. return nil
  184. })
  185. }
  186. func writeToBuffer(client volume_server_pb.VolumeServer_CopyFileClient, buf *bytes.Buffer) error {
  187. for {
  188. resp, receiveErr := client.Recv()
  189. if receiveErr == io.EOF {
  190. break
  191. }
  192. if receiveErr != nil {
  193. return fmt.Errorf("receiving: %v", receiveErr)
  194. }
  195. buf.Write(resp.FileContent)
  196. }
  197. return nil
  198. }