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.

84 lines
2.3 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. )
  17. /*
  18. This is to change replication factor in .dat file header. Need to shut down the volume servers
  19. that has those volumes.
  20. 1. fix the .dat file in place
  21. // just see the replication setting
  22. go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads
  23. Current Volume Replication: 000
  24. // fix the replication setting
  25. go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads -replication 001
  26. Current Volume Replication: 000
  27. Changing to: 001
  28. Done.
  29. 2. copy the fixed .dat and related .idx files to some remote server
  30. 3. restart volume servers or start new volume servers.
  31. */
  32. func main() {
  33. flag.Parse()
  34. fileName := strconv.Itoa(*fixVolumeId)
  35. if *fixVolumeCollection != "" {
  36. fileName = *fixVolumeCollection + "_" + fileName
  37. }
  38. datFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".dat"), os.O_RDWR, 0644)
  39. if err != nil {
  40. glog.Fatalf("Open Volume Data File [ERROR]: %v", err)
  41. }
  42. defer datFile.Close()
  43. header := make([]byte, storage.SuperBlockSize)
  44. if _, e := datFile.Read(header); e != nil {
  45. glog.Fatalf("cannot read volume %s super block: %v", fileName+".dat", e)
  46. }
  47. superBlock, err := storage.ParseSuperBlock(header)
  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. if *targetReplica == "" {
  53. return
  54. }
  55. replica, err := storage.NewReplicaPlacementFromString(*targetReplica)
  56. if err != nil {
  57. glog.Fatalf("cannot parse target replica %s: %v", *targetReplica, err)
  58. }
  59. fmt.Printf("Changing to: %s\n", replica)
  60. superBlock.ReplicaPlacement = replica
  61. header = superBlock.Bytes()
  62. if n, e := datFile.WriteAt(header, 0); n == 0 || e != nil {
  63. glog.Fatalf("cannot write super block: %v", e)
  64. }
  65. fmt.Println("Done.")
  66. }