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.

100 lines
2.7 KiB

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