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.

99 lines
2.6 KiB

  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. superBlock, err := storage.ReadSuperBlock(datFile)
  45. if err != nil {
  46. glog.Fatalf("cannot parse existing super block: %v", err)
  47. }
  48. fmt.Printf("Current Volume Replication: %s\n", superBlock.ReplicaPlacement)
  49. fmt.Printf("Current Volume TTL: %s\n", superBlock.Ttl.String())
  50. hasChange := false
  51. if *targetReplica != "" {
  52. replica, err := storage.NewReplicaPlacementFromString(*targetReplica)
  53. if err != nil {
  54. glog.Fatalf("cannot parse target replica %s: %v", *targetReplica, err)
  55. }
  56. fmt.Printf("Changing replication to: %s\n", replica)
  57. superBlock.ReplicaPlacement = replica
  58. hasChange = true
  59. }
  60. if *targetTTL != "" {
  61. ttl, err := storage.ReadTTL(*targetTTL)
  62. if err != nil {
  63. glog.Fatalf("cannot parse target ttl %s: %v", *targetTTL, err)
  64. }
  65. fmt.Printf("Changing ttl to: %s\n", ttl)
  66. superBlock.Ttl = ttl
  67. hasChange = true
  68. }
  69. if hasChange {
  70. header = superBlock.Bytes()
  71. if n, e := datFile.WriteAt(header, 0); n == 0 || e != nil {
  72. glog.Fatalf("cannot write super block: %v", e)
  73. }
  74. fmt.Println("Change Applied.")
  75. }
  76. }