change_superblock.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "path"
  7. "strconv"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  12. )
  13. var (
  14. fixVolumePath = flag.String("dir", "/tmp", "data directory to store files")
  15. fixVolumeCollection = flag.String("collection", "", "the volume collection name")
  16. fixVolumeId = flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  17. targetReplica = flag.String("replication", "", "If just empty, only print out current replication setting.")
  18. targetTTL = flag.String("ttl", "", "If just empty, only print out current ttl setting.")
  19. )
  20. /*
  21. This is to change replication factor in .dat file header. Need to shut down the volume servers
  22. that has those volumes.
  23. 1. fix the .dat file in place
  24. // just see the replication setting
  25. go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads
  26. Current Volume Replication: 000
  27. // fix the replication setting
  28. go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads -replication 001
  29. Current Volume Replication: 000
  30. Changing to: 001
  31. Done.
  32. 2. copy the fixed .dat and related .idx files to some remote server
  33. 3. restart volume servers or start new volume servers.
  34. */
  35. func main() {
  36. flag.Parse()
  37. fileName := strconv.Itoa(*fixVolumeId)
  38. if *fixVolumeCollection != "" {
  39. fileName = *fixVolumeCollection + "_" + fileName
  40. }
  41. datFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".dat"), os.O_RDWR, 0644)
  42. if err != nil {
  43. glog.Fatalf("Open Volume Data File [ERROR]: %v", err)
  44. }
  45. datBackend := backend.NewDiskFile(datFile)
  46. defer datBackend.Close()
  47. superBlock, err := super_block.ReadSuperBlock(datBackend)
  48. if err != nil {
  49. glog.Fatalf("cannot parse existing super block: %v", err)
  50. }
  51. fmt.Printf("Current Volume Replication: %s\n", superBlock.ReplicaPlacement)
  52. fmt.Printf("Current Volume TTL: %s\n", superBlock.Ttl.String())
  53. hasChange := false
  54. if *targetReplica != "" {
  55. replica, err := super_block.NewReplicaPlacementFromString(*targetReplica)
  56. if err != nil {
  57. glog.Fatalf("cannot parse target replica %s: %v", *targetReplica, err)
  58. }
  59. fmt.Printf("Changing replication to: %s\n", replica)
  60. superBlock.ReplicaPlacement = replica
  61. hasChange = true
  62. }
  63. if *targetTTL != "" {
  64. ttl, err := needle.ReadTTL(*targetTTL)
  65. if err != nil {
  66. glog.Fatalf("cannot parse target ttl %s: %v", *targetTTL, err)
  67. }
  68. fmt.Printf("Changing ttl to: %s\n", ttl)
  69. superBlock.Ttl = ttl
  70. hasChange = true
  71. }
  72. if hasChange {
  73. header := superBlock.Bytes()
  74. if n, e := datBackend.WriteAt(header, 0); n == 0 || e != nil {
  75. glog.Fatalf("cannot write super block: %v", e)
  76. }
  77. fmt.Println("Change Applied.")
  78. }
  79. }