command_volume_fsck.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package shell
  2. import (
  3. "bufio"
  4. "context"
  5. "flag"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "io"
  9. "io/ioutil"
  10. "math"
  11. "os"
  12. "path/filepath"
  13. "sync"
  14. "github.com/chrislusf/seaweedfs/weed/filer"
  15. "github.com/chrislusf/seaweedfs/weed/operation"
  16. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  17. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  18. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  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. if err = commandEnv.confirmIsLocked(); err != nil {
  49. return
  50. }
  51. fsckCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  52. verbose := fsckCommand.Bool("v", false, "verbose mode")
  53. findMissingChunksInFiler := fsckCommand.Bool("findMissingChunksInFiler", false, "see \"help volume.fsck\"")
  54. findMissingChunksInFilerPath := fsckCommand.String("findMissingChunksInFilerPath", "/", "used together with findMissingChunksInFiler")
  55. applyPurging := fsckCommand.Bool("reallyDeleteFromVolume", false, "<expert only> delete data not referenced by the filer")
  56. if err = fsckCommand.Parse(args); err != nil {
  57. return nil
  58. }
  59. c.env = commandEnv
  60. // create a temp folder
  61. tempFolder, err := ioutil.TempDir("", "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. return nil
  160. }
  161. func (c *commandVolumeFsck) findFilerChunksMissingInVolumeServers(volumeIdToVInfo map[uint32]VInfo, tempFolder string, writer io.Writer, verbose bool, applyPurging *bool) error {
  162. for volumeId, vinfo := range volumeIdToVInfo {
  163. checkErr := c.oneVolumeFileIdsCheckOneVolume(tempFolder, volumeId, writer, verbose)
  164. if checkErr != nil {
  165. return fmt.Errorf("failed to collect file ids from volume %d on %s: %v", volumeId, vinfo.server, checkErr)
  166. }
  167. }
  168. return nil
  169. }
  170. func (c *commandVolumeFsck) findExtraChunksInVolumeServers(volumeIdToVInfo map[uint32]VInfo, tempFolder string, writer io.Writer, verbose bool, applyPurging *bool) error {
  171. var totalInUseCount, totalOrphanChunkCount, totalOrphanDataSize uint64
  172. for volumeId, vinfo := range volumeIdToVInfo {
  173. inUseCount, orphanFileIds, orphanDataSize, checkErr := c.oneVolumeFileIdsSubtractFilerFileIds(tempFolder, volumeId, writer, verbose)
  174. if checkErr != nil {
  175. return fmt.Errorf("failed to collect file ids from volume %d on %s: %v", volumeId, vinfo.server, checkErr)
  176. }
  177. totalInUseCount += inUseCount
  178. totalOrphanChunkCount += uint64(len(orphanFileIds))
  179. totalOrphanDataSize += orphanDataSize
  180. if verbose {
  181. for _, fid := range orphanFileIds {
  182. fmt.Fprintf(writer, "%s\n", fid)
  183. }
  184. }
  185. if *applyPurging && len(orphanFileIds) > 0 {
  186. if vinfo.isEcVolume {
  187. fmt.Fprintf(writer, "Skip purging for Erasure Coded volumes.\n")
  188. }
  189. if inUseCount == 0 {
  190. if err := deleteVolume(c.env.option.GrpcDialOption, needle.VolumeId(volumeId), vinfo.server); err != nil {
  191. return fmt.Errorf("delete volume %d: %v\n", volumeId, err)
  192. }
  193. } else {
  194. if err := c.purgeFileIdsForOneVolume(volumeId, orphanFileIds, writer); err != nil {
  195. return fmt.Errorf("purge for volume %d: %v\n", volumeId, err)
  196. }
  197. }
  198. }
  199. }
  200. if totalOrphanChunkCount == 0 {
  201. fmt.Fprintf(writer, "no orphan data\n")
  202. return nil
  203. }
  204. if !*applyPurging {
  205. pct := float64(totalOrphanChunkCount*100) / (float64(totalOrphanChunkCount + totalInUseCount))
  206. fmt.Fprintf(writer, "\nTotal\t\tentries:%d\torphan:%d\t%.2f%%\t%dB\n",
  207. totalOrphanChunkCount+totalInUseCount, totalOrphanChunkCount, pct, totalOrphanDataSize)
  208. fmt.Fprintf(writer, "This could be normal if multiple filers or no filers are used.\n")
  209. }
  210. return nil
  211. }
  212. func (c *commandVolumeFsck) collectOneVolumeFileIds(tempFolder string, volumeId uint32, vinfo VInfo, verbose bool, writer io.Writer) error {
  213. if verbose {
  214. fmt.Fprintf(writer, "collecting volume %d file ids from %s ...\n", volumeId, vinfo.server)
  215. }
  216. return operation.WithVolumeServerClient(vinfo.server, c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  217. ext := ".idx"
  218. if vinfo.isEcVolume {
  219. ext = ".ecx"
  220. }
  221. copyFileClient, err := volumeServerClient.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  222. VolumeId: volumeId,
  223. Ext: ext,
  224. CompactionRevision: math.MaxUint32,
  225. StopOffset: math.MaxInt64,
  226. Collection: vinfo.collection,
  227. IsEcVolume: vinfo.isEcVolume,
  228. IgnoreSourceFileNotFound: false,
  229. })
  230. if err != nil {
  231. return fmt.Errorf("failed to start copying volume %d%s: %v", volumeId, ext, err)
  232. }
  233. err = writeToFile(copyFileClient, getVolumeFileIdFile(tempFolder, volumeId))
  234. if err != nil {
  235. return fmt.Errorf("failed to copy %d%s from %s: %v", volumeId, ext, vinfo.server, err)
  236. }
  237. return nil
  238. })
  239. }
  240. func (c *commandVolumeFsck) collectFilerFileIds(tempFolder string, volumeIdToServer map[uint32]VInfo, verbose bool, writer io.Writer) error {
  241. if verbose {
  242. fmt.Fprintf(writer, "collecting file ids from filer ...\n")
  243. }
  244. files := make(map[uint32]*os.File)
  245. for vid := range volumeIdToServer {
  246. dst, openErr := os.OpenFile(getFilerFileIdFile(tempFolder, vid), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  247. if openErr != nil {
  248. return fmt.Errorf("failed to create file %s: %v", getFilerFileIdFile(tempFolder, vid), openErr)
  249. }
  250. files[vid] = dst
  251. }
  252. defer func() {
  253. for _, f := range files {
  254. f.Close()
  255. }
  256. }()
  257. type Item struct {
  258. vid uint32
  259. fileKey uint64
  260. }
  261. return doTraverseBfsAndSaving(c.env, nil, "/", false, func(outputChan chan interface{}) {
  262. buffer := make([]byte, 8)
  263. for item := range outputChan {
  264. i := item.(*Item)
  265. util.Uint64toBytes(buffer, i.fileKey)
  266. files[i.vid].Write(buffer)
  267. }
  268. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  269. dChunks, mChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.Chunks, 0, math.MaxInt64)
  270. if resolveErr != nil {
  271. return nil
  272. }
  273. dChunks = append(dChunks, mChunks...)
  274. for _, chunk := range dChunks {
  275. outputChan <- &Item{
  276. vid: chunk.Fid.VolumeId,
  277. fileKey: chunk.Fid.FileKey,
  278. }
  279. }
  280. return nil
  281. })
  282. }
  283. func (c *commandVolumeFsck) oneVolumeFileIdsCheckOneVolume(tempFolder string, volumeId uint32, writer io.Writer, verbose bool) (err error) {
  284. if verbose {
  285. fmt.Fprintf(writer, "find missing file chuns in volume %d ...\n", volumeId)
  286. }
  287. db := needle_map.NewMemDb()
  288. defer db.Close()
  289. if err = db.LoadFromIdx(getVolumeFileIdFile(tempFolder, volumeId)); err != nil {
  290. return
  291. }
  292. file := getFilerFileIdFile(tempFolder, volumeId)
  293. fp, err := os.Open(file)
  294. if err != nil {
  295. return
  296. }
  297. defer fp.Close()
  298. type Item struct {
  299. fileKey uint64
  300. cookie uint32
  301. path util.FullPath
  302. }
  303. br := bufio.NewReader(fp)
  304. buffer := make([]byte, 16)
  305. item := &Item{}
  306. var readSize int
  307. for {
  308. readSize, err = io.ReadFull(br, buffer)
  309. if err != nil || readSize != 16 {
  310. if err == io.EOF {
  311. return nil
  312. } else {
  313. break
  314. }
  315. }
  316. item.fileKey = util.BytesToUint64(buffer[:8])
  317. item.cookie = util.BytesToUint32(buffer[8:12])
  318. pathSize := util.BytesToUint32(buffer[12:16])
  319. pathBytes := make([]byte, int(pathSize))
  320. n, err := io.ReadFull(br, pathBytes)
  321. if err != nil {
  322. fmt.Fprintf(writer, "%d,%x%08x in unexpected error: %v\n", volumeId, item.fileKey, item.cookie, err)
  323. }
  324. if n != int(pathSize) {
  325. fmt.Fprintf(writer, "%d,%x%08x %d unexpected file name size %d\n", volumeId, item.fileKey, item.cookie, pathSize, n)
  326. }
  327. item.path = util.FullPath(string(pathBytes))
  328. if _, found := db.Get(types.NeedleId(item.fileKey)); !found {
  329. fmt.Fprintf(writer, "%d,%x%08x in %s %d not found\n", volumeId, item.fileKey, item.cookie, item.path, pathSize)
  330. }
  331. }
  332. return
  333. }
  334. func (c *commandVolumeFsck) oneVolumeFileIdsSubtractFilerFileIds(tempFolder string, volumeId uint32, writer io.Writer, verbose bool) (inUseCount uint64, orphanFileIds []string, orphanDataSize uint64, err error) {
  335. db := needle_map.NewMemDb()
  336. defer db.Close()
  337. if err = db.LoadFromIdx(getVolumeFileIdFile(tempFolder, volumeId)); err != nil {
  338. return
  339. }
  340. filerFileIdsData, err := ioutil.ReadFile(getFilerFileIdFile(tempFolder, volumeId))
  341. if err != nil {
  342. return
  343. }
  344. dataLen := len(filerFileIdsData)
  345. if dataLen%8 != 0 {
  346. return 0, nil, 0, fmt.Errorf("filer data is corrupted")
  347. }
  348. for i := 0; i < len(filerFileIdsData); i += 8 {
  349. fileKey := util.BytesToUint64(filerFileIdsData[i : i+8])
  350. db.Delete(types.NeedleId(fileKey))
  351. inUseCount++
  352. }
  353. var orphanFileCount uint64
  354. db.AscendingVisit(func(n needle_map.NeedleValue) error {
  355. // fmt.Printf("%d,%x\n", volumeId, n.Key)
  356. orphanFileIds = append(orphanFileIds, fmt.Sprintf("%d,%s00000000", volumeId, n.Key.String()))
  357. orphanFileCount++
  358. orphanDataSize += uint64(n.Size)
  359. return nil
  360. })
  361. if orphanFileCount > 0 {
  362. pct := float64(orphanFileCount*100) / (float64(orphanFileCount + inUseCount))
  363. fmt.Fprintf(writer, "volume:%d\tentries:%d\torphan:%d\t%.2f%%\t%dB\n",
  364. volumeId, orphanFileCount+inUseCount, orphanFileCount, pct, orphanDataSize)
  365. }
  366. return
  367. }
  368. type VInfo struct {
  369. server string
  370. collection string
  371. isEcVolume bool
  372. }
  373. func (c *commandVolumeFsck) collectVolumeIds(commandEnv *CommandEnv, verbose bool, writer io.Writer) (volumeIdToServer map[uint32]VInfo, err error) {
  374. if verbose {
  375. fmt.Fprintf(writer, "collecting volume id and locations from master ...\n")
  376. }
  377. volumeIdToServer = make(map[uint32]VInfo)
  378. // collect topology information
  379. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  380. if err != nil {
  381. return
  382. }
  383. eachDataNode(topologyInfo, func(dc string, rack RackId, t *master_pb.DataNodeInfo) {
  384. for _, diskInfo := range t.DiskInfos {
  385. for _, vi := range diskInfo.VolumeInfos {
  386. volumeIdToServer[vi.Id] = VInfo{
  387. server: t.Id,
  388. collection: vi.Collection,
  389. isEcVolume: false,
  390. }
  391. }
  392. for _, ecShardInfo := range diskInfo.EcShardInfos {
  393. volumeIdToServer[ecShardInfo.Id] = VInfo{
  394. server: t.Id,
  395. collection: ecShardInfo.Collection,
  396. isEcVolume: true,
  397. }
  398. }
  399. }
  400. })
  401. if verbose {
  402. fmt.Fprintf(writer, "collected %d volumes and locations.\n", len(volumeIdToServer))
  403. }
  404. return
  405. }
  406. func (c *commandVolumeFsck) purgeFileIdsForOneVolume(volumeId uint32, fileIds []string, writer io.Writer) (err error) {
  407. fmt.Fprintf(writer, "purging orphan data for volume %d...\n", volumeId)
  408. locations, found := c.env.MasterClient.GetLocations(volumeId)
  409. if !found {
  410. return fmt.Errorf("failed to find volume %d locations", volumeId)
  411. }
  412. resultChan := make(chan []*volume_server_pb.DeleteResult, len(locations))
  413. var wg sync.WaitGroup
  414. for _, location := range locations {
  415. wg.Add(1)
  416. go func(server string, fidList []string) {
  417. defer wg.Done()
  418. if deleteResults, deleteErr := operation.DeleteFilesAtOneVolumeServer(server, c.env.option.GrpcDialOption, fidList, false); deleteErr != nil {
  419. err = deleteErr
  420. } else if deleteResults != nil {
  421. resultChan <- deleteResults
  422. }
  423. }(location.Url, fileIds)
  424. }
  425. wg.Wait()
  426. close(resultChan)
  427. for results := range resultChan {
  428. for _, result := range results {
  429. if result.Error != "" {
  430. fmt.Fprintf(writer, "purge error: %s\n", result.Error)
  431. }
  432. }
  433. }
  434. return
  435. }
  436. func getVolumeFileIdFile(tempFolder string, vid uint32) string {
  437. return filepath.Join(tempFolder, fmt.Sprintf("%d.idx", vid))
  438. }
  439. func getFilerFileIdFile(tempFolder string, vid uint32) string {
  440. return filepath.Join(tempFolder, fmt.Sprintf("%d.fid", vid))
  441. }
  442. func writeToFile(client volume_server_pb.VolumeServer_CopyFileClient, fileName string) error {
  443. flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  444. dst, err := os.OpenFile(fileName, flags, 0644)
  445. if err != nil {
  446. return nil
  447. }
  448. defer dst.Close()
  449. for {
  450. resp, receiveErr := client.Recv()
  451. if receiveErr == io.EOF {
  452. break
  453. }
  454. if receiveErr != nil {
  455. return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
  456. }
  457. dst.Write(resp.FileContent)
  458. }
  459. return nil
  460. }