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.

545 lines
16 KiB

4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. package shell
  2. import (
  3. "bufio"
  4. "context"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "math"
  9. "os"
  10. "path/filepath"
  11. "sync"
  12. "github.com/chrislusf/seaweedfs/weed/filer"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/pb"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  17. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  18. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  19. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  20. "github.com/chrislusf/seaweedfs/weed/storage/types"
  21. "github.com/chrislusf/seaweedfs/weed/util"
  22. )
  23. func init() {
  24. Commands = append(Commands, &commandVolumeFsck{})
  25. }
  26. type commandVolumeFsck struct {
  27. env *CommandEnv
  28. }
  29. func (c *commandVolumeFsck) Name() string {
  30. return "volume.fsck"
  31. }
  32. func (c *commandVolumeFsck) Help() string {
  33. return `check all volumes to find entries not used by the filer
  34. Important assumption!!!
  35. the system is all used by one filer.
  36. This command works this way:
  37. 1. collect all file ids from all volumes, as set A
  38. 2. collect all file ids from the filer, as set B
  39. 3. find out the set A subtract B
  40. If -findMissingChunksInFiler is enabled, this works
  41. in a reverse way:
  42. 1. collect all file ids from all volumes, as set A
  43. 2. collect all file ids from the filer, as set B
  44. 3. find out the set B subtract A
  45. `
  46. }
  47. func (c *commandVolumeFsck) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  48. fsckCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  49. verbose := fsckCommand.Bool("v", false, "verbose mode")
  50. findMissingChunksInFiler := fsckCommand.Bool("findMissingChunksInFiler", false, "see \"help volume.fsck\"")
  51. findMissingChunksInFilerPath := fsckCommand.String("findMissingChunksInFilerPath", "/", "used together with findMissingChunksInFiler")
  52. applyPurging := fsckCommand.Bool("reallyDeleteFromVolume", false, "<expert only> delete data not referenced by the filer")
  53. if err = fsckCommand.Parse(args); err != nil {
  54. return nil
  55. }
  56. if err = commandEnv.confirmIsLocked(); err != nil {
  57. return
  58. }
  59. c.env = commandEnv
  60. // create a temp folder
  61. tempFolder, err := os.MkdirTemp("", "sw_fsck")
  62. if err != nil {
  63. return fmt.Errorf("failed to create temp folder: %v", err)
  64. }
  65. if *verbose {
  66. fmt.Fprintf(writer, "working directory: %s\n", tempFolder)
  67. }
  68. defer os.RemoveAll(tempFolder)
  69. // collect all volume id locations
  70. volumeIdToVInfo, err := c.collectVolumeIds(commandEnv, *verbose, writer)
  71. if err != nil {
  72. return fmt.Errorf("failed to collect all volume locations: %v", err)
  73. }
  74. // collect each volume file ids
  75. for volumeId, vinfo := range volumeIdToVInfo {
  76. err = c.collectOneVolumeFileIds(tempFolder, volumeId, vinfo, *verbose, writer)
  77. if err != nil {
  78. return fmt.Errorf("failed to collect file ids from volume %d on %s: %v", volumeId, vinfo.server, err)
  79. }
  80. }
  81. if *findMissingChunksInFiler {
  82. // collect all filer file ids and paths
  83. if err = c.collectFilerFileIdAndPaths(volumeIdToVInfo, tempFolder, writer, *findMissingChunksInFilerPath, *verbose, applyPurging); err != nil {
  84. return fmt.Errorf("collectFilerFileIdAndPaths: %v", err)
  85. }
  86. // for each volume, check filer file ids
  87. if err = c.findFilerChunksMissingInVolumeServers(volumeIdToVInfo, tempFolder, writer, *verbose, applyPurging); err != nil {
  88. return fmt.Errorf("findFilerChunksMissingInVolumeServers: %v", err)
  89. }
  90. } else {
  91. // collect all filer file ids
  92. if err = c.collectFilerFileIds(tempFolder, volumeIdToVInfo, *verbose, writer); err != nil {
  93. return fmt.Errorf("failed to collect file ids from filer: %v", err)
  94. }
  95. // volume file ids substract filer file ids
  96. if err = c.findExtraChunksInVolumeServers(volumeIdToVInfo, tempFolder, writer, *verbose, applyPurging); err != nil {
  97. return fmt.Errorf("findExtraChunksInVolumeServers: %v", err)
  98. }
  99. }
  100. return nil
  101. }
  102. func (c *commandVolumeFsck) collectFilerFileIdAndPaths(volumeIdToServer map[uint32]VInfo, tempFolder string, writer io.Writer, filerPath string, verbose bool, applyPurging *bool) error {
  103. if verbose {
  104. fmt.Fprintf(writer, "checking each file from filer ...\n")
  105. }
  106. files := make(map[uint32]*os.File)
  107. for vid := range volumeIdToServer {
  108. dst, openErr := os.OpenFile(getFilerFileIdFile(tempFolder, vid), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  109. if openErr != nil {
  110. return fmt.Errorf("failed to create file %s: %v", getFilerFileIdFile(tempFolder, vid), openErr)
  111. }
  112. files[vid] = dst
  113. }
  114. defer func() {
  115. for _, f := range files {
  116. f.Close()
  117. }
  118. }()
  119. type Item struct {
  120. vid uint32
  121. fileKey uint64
  122. cookie uint32
  123. path util.FullPath
  124. }
  125. return doTraverseBfsAndSaving(c.env, nil, filerPath, false, func(outputChan chan interface{}) {
  126. buffer := make([]byte, 16)
  127. for item := range outputChan {
  128. i := item.(*Item)
  129. if f, ok := files[i.vid]; ok {
  130. util.Uint64toBytes(buffer, i.fileKey)
  131. util.Uint32toBytes(buffer[8:], i.cookie)
  132. util.Uint32toBytes(buffer[12:], uint32(len(i.path)))
  133. f.Write(buffer)
  134. f.Write([]byte(i.path))
  135. // fmt.Fprintf(writer, "%d,%x%08x %d %s\n", i.vid, i.fileKey, i.cookie, len(i.path), i.path)
  136. } else {
  137. fmt.Fprintf(writer, "%d,%x%08x %s volume not found\n", i.vid, i.fileKey, i.cookie, i.path)
  138. }
  139. }
  140. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  141. if verbose && entry.Entry.IsDirectory {
  142. fmt.Fprintf(writer, "checking directory %s\n", util.NewFullPath(entry.Dir, entry.Entry.Name))
  143. }
  144. dChunks, mChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.Chunks, 0, math.MaxInt64)
  145. if resolveErr != nil {
  146. return nil
  147. }
  148. dChunks = append(dChunks, mChunks...)
  149. for _, chunk := range dChunks {
  150. outputChan <- &Item{
  151. vid: chunk.Fid.VolumeId,
  152. fileKey: chunk.Fid.FileKey,
  153. cookie: chunk.Fid.Cookie,
  154. path: util.NewFullPath(entry.Dir, entry.Entry.Name),
  155. }
  156. }
  157. return nil
  158. })
  159. }
  160. func (c *commandVolumeFsck) findFilerChunksMissingInVolumeServers(volumeIdToVInfo map[uint32]VInfo, tempFolder string, writer io.Writer, verbose bool, applyPurging *bool) error {
  161. for volumeId, vinfo := range volumeIdToVInfo {
  162. checkErr := c.oneVolumeFileIdsCheckOneVolume(tempFolder, volumeId, writer, verbose)
  163. if checkErr != nil {
  164. return fmt.Errorf("failed to collect file ids from volume %d on %s: %v", volumeId, vinfo.server, checkErr)
  165. }
  166. }
  167. return nil
  168. }
  169. func (c *commandVolumeFsck) findExtraChunksInVolumeServers(volumeIdToVInfo map[uint32]VInfo, tempFolder string, writer io.Writer, verbose bool, applyPurging *bool) error {
  170. var totalInUseCount, totalOrphanChunkCount, totalOrphanDataSize uint64
  171. for volumeId, vinfo := range volumeIdToVInfo {
  172. inUseCount, orphanFileIds, orphanDataSize, checkErr := c.oneVolumeFileIdsSubtractFilerFileIds(tempFolder, volumeId, writer, verbose)
  173. if checkErr != nil {
  174. return fmt.Errorf("failed to collect file ids from volume %d on %s: %v", volumeId, vinfo.server, checkErr)
  175. }
  176. totalInUseCount += inUseCount
  177. totalOrphanChunkCount += uint64(len(orphanFileIds))
  178. totalOrphanDataSize += orphanDataSize
  179. if verbose {
  180. for _, fid := range orphanFileIds {
  181. fmt.Fprintf(writer, "%s\n", fid)
  182. }
  183. }
  184. if *applyPurging && len(orphanFileIds) > 0 {
  185. if vinfo.isEcVolume {
  186. fmt.Fprintf(writer, "Skip purging for Erasure Coded volumes.\n")
  187. }
  188. if inUseCount == 0 {
  189. if err := deleteVolume(c.env.option.GrpcDialOption, needle.VolumeId(volumeId), vinfo.server); err != nil {
  190. return fmt.Errorf("delete volume %d: %v", volumeId, err)
  191. }
  192. } else {
  193. if err := c.purgeFileIdsForOneVolume(volumeId, orphanFileIds, writer); err != nil {
  194. return fmt.Errorf("purge for volume %d: %v", volumeId, err)
  195. }
  196. }
  197. }
  198. }
  199. if totalOrphanChunkCount == 0 {
  200. fmt.Fprintf(writer, "no orphan data\n")
  201. return nil
  202. }
  203. if !*applyPurging {
  204. pct := float64(totalOrphanChunkCount*100) / (float64(totalOrphanChunkCount + totalInUseCount))
  205. fmt.Fprintf(writer, "\nTotal\t\tentries:%d\torphan:%d\t%.2f%%\t%dB\n",
  206. totalOrphanChunkCount+totalInUseCount, totalOrphanChunkCount, pct, totalOrphanDataSize)
  207. fmt.Fprintf(writer, "This could be normal if multiple filers or no filers are used.\n")
  208. }
  209. return nil
  210. }
  211. func (c *commandVolumeFsck) collectOneVolumeFileIds(tempFolder string, volumeId uint32, vinfo VInfo, verbose bool, writer io.Writer) error {
  212. if verbose {
  213. fmt.Fprintf(writer, "collecting volume %d file ids from %s ...\n", volumeId, vinfo.server)
  214. }
  215. return operation.WithVolumeServerClient(vinfo.server, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  216. ext := ".idx"
  217. if vinfo.isEcVolume {
  218. ext = ".ecx"
  219. }
  220. copyFileClient, err := volumeServerClient.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  221. VolumeId: volumeId,
  222. Ext: ext,
  223. CompactionRevision: math.MaxUint32,
  224. StopOffset: math.MaxInt64,
  225. Collection: vinfo.collection,
  226. IsEcVolume: vinfo.isEcVolume,
  227. IgnoreSourceFileNotFound: false,
  228. })
  229. if err != nil {
  230. return fmt.Errorf("failed to start copying volume %d%s: %v", volumeId, ext, err)
  231. }
  232. err = writeToFile(copyFileClient, getVolumeFileIdFile(tempFolder, volumeId))
  233. if err != nil {
  234. return fmt.Errorf("failed to copy %d%s from %s: %v", volumeId, ext, vinfo.server, err)
  235. }
  236. return nil
  237. })
  238. }
  239. func (c *commandVolumeFsck) collectFilerFileIds(tempFolder string, volumeIdToServer map[uint32]VInfo, verbose bool, writer io.Writer) error {
  240. if verbose {
  241. fmt.Fprintf(writer, "collecting file ids from filer ...\n")
  242. }
  243. files := make(map[uint32]*os.File)
  244. for vid := range volumeIdToServer {
  245. dst, openErr := os.OpenFile(getFilerFileIdFile(tempFolder, vid), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  246. if openErr != nil {
  247. return fmt.Errorf("failed to create file %s: %v", getFilerFileIdFile(tempFolder, vid), openErr)
  248. }
  249. files[vid] = dst
  250. }
  251. defer func() {
  252. for _, f := range files {
  253. f.Close()
  254. }
  255. }()
  256. type Item struct {
  257. vid uint32
  258. fileKey uint64
  259. }
  260. return doTraverseBfsAndSaving(c.env, nil, "/", false, func(outputChan chan interface{}) {
  261. buffer := make([]byte, 8)
  262. for item := range outputChan {
  263. i := item.(*Item)
  264. util.Uint64toBytes(buffer, i.fileKey)
  265. files[i.vid].Write(buffer)
  266. }
  267. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  268. dChunks, mChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.Chunks, 0, math.MaxInt64)
  269. if resolveErr != nil {
  270. if verbose {
  271. fmt.Fprintf(writer, "resolving manifest chunks in %s: %v\n", util.NewFullPath(entry.Dir, entry.Entry.Name), resolveErr)
  272. }
  273. return nil
  274. }
  275. dChunks = append(dChunks, mChunks...)
  276. for _, chunk := range dChunks {
  277. outputChan <- &Item{
  278. vid: chunk.Fid.VolumeId,
  279. fileKey: chunk.Fid.FileKey,
  280. }
  281. }
  282. return nil
  283. })
  284. }
  285. func (c *commandVolumeFsck) oneVolumeFileIdsCheckOneVolume(tempFolder string, volumeId uint32, writer io.Writer, verbose bool) (err error) {
  286. if verbose {
  287. fmt.Fprintf(writer, "find missing file chuns in volume %d ...\n", volumeId)
  288. }
  289. db := needle_map.NewMemDb()
  290. defer db.Close()
  291. if err = db.LoadFromIdx(getVolumeFileIdFile(tempFolder, volumeId)); err != nil {
  292. return
  293. }
  294. file := getFilerFileIdFile(tempFolder, volumeId)
  295. fp, err := os.Open(file)
  296. if err != nil {
  297. return
  298. }
  299. defer fp.Close()
  300. type Item struct {
  301. fileKey uint64
  302. cookie uint32
  303. path util.FullPath
  304. }
  305. br := bufio.NewReader(fp)
  306. buffer := make([]byte, 16)
  307. item := &Item{}
  308. var readSize int
  309. for {
  310. readSize, err = io.ReadFull(br, buffer)
  311. if err != nil || readSize != 16 {
  312. if err == io.EOF {
  313. return nil
  314. } else {
  315. break
  316. }
  317. }
  318. item.fileKey = util.BytesToUint64(buffer[:8])
  319. item.cookie = util.BytesToUint32(buffer[8:12])
  320. pathSize := util.BytesToUint32(buffer[12:16])
  321. pathBytes := make([]byte, int(pathSize))
  322. n, err := io.ReadFull(br, pathBytes)
  323. if err != nil {
  324. fmt.Fprintf(writer, "%d,%x%08x in unexpected error: %v\n", volumeId, item.fileKey, item.cookie, err)
  325. }
  326. if n != int(pathSize) {
  327. fmt.Fprintf(writer, "%d,%x%08x %d unexpected file name size %d\n", volumeId, item.fileKey, item.cookie, pathSize, n)
  328. }
  329. item.path = util.FullPath(string(pathBytes))
  330. if _, found := db.Get(types.NeedleId(item.fileKey)); !found {
  331. fmt.Fprintf(writer, "%d,%x%08x in %s %d not found\n", volumeId, item.fileKey, item.cookie, item.path, pathSize)
  332. }
  333. }
  334. return
  335. }
  336. func (c *commandVolumeFsck) oneVolumeFileIdsSubtractFilerFileIds(tempFolder string, volumeId uint32, writer io.Writer, verbose bool) (inUseCount uint64, orphanFileIds []string, orphanDataSize uint64, err error) {
  337. db := needle_map.NewMemDb()
  338. defer db.Close()
  339. if err = db.LoadFromIdx(getVolumeFileIdFile(tempFolder, volumeId)); err != nil {
  340. return
  341. }
  342. filerFileIdsData, err := os.ReadFile(getFilerFileIdFile(tempFolder, volumeId))
  343. if err != nil {
  344. return
  345. }
  346. dataLen := len(filerFileIdsData)
  347. if dataLen%8 != 0 {
  348. return 0, nil, 0, fmt.Errorf("filer data is corrupted")
  349. }
  350. for i := 0; i < len(filerFileIdsData); i += 8 {
  351. fileKey := util.BytesToUint64(filerFileIdsData[i : i+8])
  352. db.Delete(types.NeedleId(fileKey))
  353. inUseCount++
  354. }
  355. var orphanFileCount uint64
  356. db.AscendingVisit(func(n needle_map.NeedleValue) error {
  357. // fmt.Printf("%d,%x\n", volumeId, n.Key)
  358. orphanFileIds = append(orphanFileIds, fmt.Sprintf("%d,%s00000000", volumeId, n.Key.String()))
  359. orphanFileCount++
  360. orphanDataSize += uint64(n.Size)
  361. return nil
  362. })
  363. if orphanFileCount > 0 {
  364. pct := float64(orphanFileCount*100) / (float64(orphanFileCount + inUseCount))
  365. fmt.Fprintf(writer, "volume:%d\tentries:%d\torphan:%d\t%.2f%%\t%dB\n",
  366. volumeId, orphanFileCount+inUseCount, orphanFileCount, pct, orphanDataSize)
  367. }
  368. return
  369. }
  370. type VInfo struct {
  371. server pb.ServerAddress
  372. collection string
  373. isEcVolume bool
  374. }
  375. func (c *commandVolumeFsck) collectVolumeIds(commandEnv *CommandEnv, verbose bool, writer io.Writer) (volumeIdToServer map[uint32]VInfo, err error) {
  376. if verbose {
  377. fmt.Fprintf(writer, "collecting volume id and locations from master ...\n")
  378. }
  379. volumeIdToServer = make(map[uint32]VInfo)
  380. // collect topology information
  381. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  382. if err != nil {
  383. return
  384. }
  385. eachDataNode(topologyInfo, func(dc string, rack RackId, t *master_pb.DataNodeInfo) {
  386. for _, diskInfo := range t.DiskInfos {
  387. for _, vi := range diskInfo.VolumeInfos {
  388. volumeIdToServer[vi.Id] = VInfo{
  389. server: pb.NewServerAddressFromDataNode(t),
  390. collection: vi.Collection,
  391. isEcVolume: false,
  392. }
  393. }
  394. for _, ecShardInfo := range diskInfo.EcShardInfos {
  395. volumeIdToServer[ecShardInfo.Id] = VInfo{
  396. server: pb.NewServerAddressFromDataNode(t),
  397. collection: ecShardInfo.Collection,
  398. isEcVolume: true,
  399. }
  400. }
  401. }
  402. })
  403. if verbose {
  404. fmt.Fprintf(writer, "collected %d volumes and locations.\n", len(volumeIdToServer))
  405. }
  406. return
  407. }
  408. func (c *commandVolumeFsck) purgeFileIdsForOneVolume(volumeId uint32, fileIds []string, writer io.Writer) (err error) {
  409. fmt.Fprintf(writer, "purging orphan data for volume %d...\n", volumeId)
  410. locations, found := c.env.MasterClient.GetLocations(volumeId)
  411. if !found {
  412. return fmt.Errorf("failed to find volume %d locations", volumeId)
  413. }
  414. resultChan := make(chan []*volume_server_pb.DeleteResult, len(locations))
  415. var wg sync.WaitGroup
  416. for _, location := range locations {
  417. wg.Add(1)
  418. go func(server pb.ServerAddress, fidList []string) {
  419. defer wg.Done()
  420. if deleteResults, deleteErr := operation.DeleteFilesAtOneVolumeServer(server, c.env.option.GrpcDialOption, fidList, false); deleteErr != nil {
  421. err = deleteErr
  422. } else if deleteResults != nil {
  423. resultChan <- deleteResults
  424. }
  425. }(location.ServerAddress(), fileIds)
  426. }
  427. wg.Wait()
  428. close(resultChan)
  429. for results := range resultChan {
  430. for _, result := range results {
  431. if result.Error != "" {
  432. fmt.Fprintf(writer, "purge error: %s\n", result.Error)
  433. }
  434. }
  435. }
  436. return
  437. }
  438. func getVolumeFileIdFile(tempFolder string, vid uint32) string {
  439. return filepath.Join(tempFolder, fmt.Sprintf("%d.idx", vid))
  440. }
  441. func getFilerFileIdFile(tempFolder string, vid uint32) string {
  442. return filepath.Join(tempFolder, fmt.Sprintf("%d.fid", vid))
  443. }
  444. func writeToFile(client volume_server_pb.VolumeServer_CopyFileClient, fileName string) error {
  445. flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  446. dst, err := os.OpenFile(fileName, flags, 0644)
  447. if err != nil {
  448. return nil
  449. }
  450. defer dst.Close()
  451. for {
  452. resp, receiveErr := client.Recv()
  453. if receiveErr == io.EOF {
  454. break
  455. }
  456. if receiveErr != nil {
  457. return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
  458. }
  459. dst.Write(resp.FileContent)
  460. }
  461. return nil
  462. }