change_superblock.go 2.8 KB

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