change_superblock.go 2.9 KB

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