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.

243 lines
7.1 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. verbose := fsckCommand.Bool("v", false, "verbose mode")
  39. applyChanges := fsckCommand.Bool("force", false, "apply the fix")
  40. if err = fsckCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. c.env = commandEnv
  44. // collect topology information
  45. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  46. if err != nil {
  47. return err
  48. }
  49. volumeReplicas, _ := collectVolumeReplicaLocations(topologyInfo)
  50. // pick 1 pairs of volume replica
  51. fileCount := func(replica *VolumeReplica) uint64 {
  52. return replica.info.FileCount - replica.info.DeleteCount
  53. }
  54. aDB, bDB := needle_map.NewMemDb(), needle_map.NewMemDb()
  55. defer aDB.Close()
  56. defer bDB.Close()
  57. for _, replicas := range volumeReplicas {
  58. sort.Slice(replicas, func(i, j int) bool {
  59. return fileCount(replicas[i]) > fileCount(replicas[j])
  60. })
  61. for len(replicas) >= 2 {
  62. a, b := replicas[0], replicas[1]
  63. if fileCount(a) == fileCount(b) {
  64. replicas = replicas[1:]
  65. continue
  66. }
  67. if a.info.ReadOnly || b.info.ReadOnly {
  68. fmt.Fprintf(writer, "skipping readonly volume %d on %s and %s\n", a.info.Id, a.location.dataNode.Id, b.location.dataNode.Id)
  69. continue
  70. }
  71. // reset index db
  72. aDB.Close()
  73. bDB.Close()
  74. aDB, bDB = needle_map.NewMemDb(), needle_map.NewMemDb()
  75. // read index db
  76. if err := c.readIndexDatabase(aDB, a.info.Collection, a.info.Id, a.location.dataNode.Id, *verbose, writer); err != nil {
  77. return err
  78. }
  79. if err := c.readIndexDatabase(bDB, b.info.Collection, b.info.Id, b.location.dataNode.Id, *verbose, writer); err != nil {
  80. return err
  81. }
  82. // find and make up the differnces
  83. if err := c.doVolumeCheckDisk(aDB, bDB, a, b, *verbose, writer, *applyChanges); err != nil {
  84. return err
  85. }
  86. if err := c.doVolumeCheckDisk(bDB, aDB, b, a, *verbose, writer, *applyChanges); err != nil {
  87. return err
  88. }
  89. replicas = replicas[1:]
  90. }
  91. }
  92. return nil
  93. }
  94. func (c *commandVolumeCheckDisk) doVolumeCheckDisk(subtrahend, minuend *needle_map.MemDb, source, target *VolumeReplica, verbose bool, writer io.Writer, applyChanges bool) error {
  95. // find missing keys
  96. // hash join, can be more efficient
  97. var missingNeedles []needle_map.NeedleValue
  98. var counter int
  99. subtrahend.AscendingVisit(func(value needle_map.NeedleValue) error {
  100. counter++
  101. if _, found := minuend.Get(value.Key); !found {
  102. missingNeedles = append(missingNeedles, value)
  103. }
  104. return nil
  105. })
  106. fmt.Fprintf(writer, "%s has %d entries, %s missed %d entries\n", source.location.dataNode.Id, counter, target.location.dataNode.Id, len(missingNeedles))
  107. for _, needleValue := range missingNeedles {
  108. needleBlob, err := c.readSourceNeedleBlob(source.location.dataNode.Id, source.info.Id, needleValue)
  109. if err != nil {
  110. return err
  111. }
  112. if !applyChanges {
  113. continue
  114. }
  115. if verbose {
  116. fmt.Fprintf(writer, "read %d,%x %s => %s \n", source.info.Id, needleValue.Key, source.location.dataNode.Id, target.location.dataNode.Id)
  117. }
  118. if err := c.writeNeedleBlobToTarget(target.location.dataNode.Id, source.info.Id, needleValue, needleBlob); err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. func (c *commandVolumeCheckDisk) readSourceNeedleBlob(sourceVolumeServer string, volumeId uint32, needleValue needle_map.NeedleValue) (needleBlob []byte, err error) {
  125. err = operation.WithVolumeServerClient(sourceVolumeServer, c.env.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  126. resp, err := client.ReadNeedleBlob(context.Background(), &volume_server_pb.ReadNeedleBlobRequest{
  127. VolumeId: volumeId,
  128. NeedleId: uint64(needleValue.Key),
  129. Offset: needleValue.Offset.ToActualOffset(),
  130. Size: int32(needleValue.Size),
  131. })
  132. if err != nil {
  133. return err
  134. }
  135. needleBlob = resp.NeedleBlob
  136. return nil
  137. })
  138. return
  139. }
  140. func (c *commandVolumeCheckDisk) writeNeedleBlobToTarget(targetVolumeServer string, volumeId uint32, needleValue needle_map.NeedleValue, needleBlob []byte) error {
  141. return operation.WithVolumeServerClient(targetVolumeServer, c.env.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  142. _, err := client.WriteNeedleBlob(context.Background(), &volume_server_pb.WriteNeedleBlobRequest{
  143. VolumeId: volumeId,
  144. NeedleId: uint64(needleValue.Key),
  145. Size: int32(needleValue.Size),
  146. NeedleBlob: needleBlob,
  147. })
  148. return err
  149. })
  150. }
  151. func (c *commandVolumeCheckDisk) readIndexDatabase(db *needle_map.MemDb, collection string, volumeId uint32, volumeServer string, verbose bool, writer io.Writer) error {
  152. var buf bytes.Buffer
  153. if err := c.copyVolumeIndexFile(collection, volumeId, volumeServer, &buf, verbose, writer); err != nil {
  154. return err
  155. }
  156. if verbose {
  157. fmt.Fprintf(writer, "load collection %s volume %d index size %d from %s ...\n", collection, volumeId, buf.Len(), volumeServer)
  158. }
  159. return db.LoadFromReaderAt(bytes.NewReader(buf.Bytes()))
  160. }
  161. func (c *commandVolumeCheckDisk) copyVolumeIndexFile(collection string, volumeId uint32, volumeServer string, buf *bytes.Buffer, verbose bool, writer io.Writer) error {
  162. return operation.WithVolumeServerClient(volumeServer, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  163. ext := ".idx"
  164. copyFileClient, err := volumeServerClient.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  165. VolumeId: volumeId,
  166. Ext: ".idx",
  167. CompactionRevision: math.MaxUint32,
  168. StopOffset: math.MaxInt64,
  169. Collection: collection,
  170. IsEcVolume: false,
  171. IgnoreSourceFileNotFound: false,
  172. })
  173. if err != nil {
  174. return fmt.Errorf("failed to start copying volume %d%s: %v", volumeId, ext, err)
  175. }
  176. err = writeToBuffer(copyFileClient, buf)
  177. if err != nil {
  178. return fmt.Errorf("failed to copy %d%s from %s: %v", volumeId, ext, volumeServer, err)
  179. }
  180. return nil
  181. })
  182. }
  183. func writeToBuffer(client volume_server_pb.VolumeServer_CopyFileClient, buf *bytes.Buffer) error {
  184. for {
  185. resp, receiveErr := client.Recv()
  186. if receiveErr == io.EOF {
  187. break
  188. }
  189. if receiveErr != nil {
  190. return fmt.Errorf("receiving: %v", receiveErr)
  191. }
  192. buf.Write(resp.FileContent)
  193. }
  194. return nil
  195. }