2016-06-06 19:27:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
2016-06-06 19:27:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fixVolumePath = flag.String("dir", "/tmp", "data directory to store files")
|
|
|
|
fixVolumeCollection = flag.String("collection", "", "the volume collection name")
|
|
|
|
fixVolumeId = flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
|
|
|
|
targetReplica = flag.String("replication", "", "If just empty, only print out current replication setting.")
|
2016-11-05 03:42:28 +00:00
|
|
|
targetTTL = flag.String("ttl", "", "If just empty, only print out current ttl setting.")
|
2016-06-06 19:27:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is to change replication factor in .dat file header. Need to shut down the volume servers
|
|
|
|
that has those volumes.
|
|
|
|
|
|
|
|
1. fix the .dat file in place
|
|
|
|
// just see the replication setting
|
|
|
|
go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads
|
|
|
|
Current Volume Replication: 000
|
|
|
|
// fix the replication setting
|
|
|
|
go run change_replication.go -volumeId=9 -dir=/Users/chrislu/Downloads -replication 001
|
|
|
|
Current Volume Replication: 000
|
|
|
|
Changing to: 001
|
|
|
|
Done.
|
|
|
|
|
|
|
|
2. copy the fixed .dat and related .idx files to some remote server
|
|
|
|
3. restart volume servers or start new volume servers.
|
|
|
|
*/
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
fileName := strconv.Itoa(*fixVolumeId)
|
|
|
|
if *fixVolumeCollection != "" {
|
|
|
|
fileName = *fixVolumeCollection + "_" + fileName
|
|
|
|
}
|
|
|
|
datFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".dat"), os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Open Volume Data File [ERROR]: %v", err)
|
|
|
|
}
|
2019-10-30 05:37:36 +00:00
|
|
|
datBackend := backend.NewDiskFile(datFile)
|
2019-10-29 07:35:16 +00:00
|
|
|
defer datBackend.Close()
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2019-12-23 20:48:20 +00:00
|
|
|
superBlock, err := super_block.ReadSuperBlock(datBackend)
|
2016-06-06 19:27:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("cannot parse existing super block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Current Volume Replication: %s\n", superBlock.ReplicaPlacement)
|
2016-11-05 03:42:28 +00:00
|
|
|
fmt.Printf("Current Volume TTL: %s\n", superBlock.Ttl.String())
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
hasChange := false
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
if *targetReplica != "" {
|
2019-12-23 20:48:20 +00:00
|
|
|
replica, err := super_block.NewReplicaPlacementFromString(*targetReplica)
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("cannot parse target replica %s: %v", *targetReplica, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Changing replication to: %s\n", replica)
|
|
|
|
|
|
|
|
superBlock.ReplicaPlacement = replica
|
|
|
|
hasChange = true
|
2016-06-06 19:27:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
if *targetTTL != "" {
|
2019-04-19 04:43:36 +00:00
|
|
|
ttl, err := needle.ReadTTL(*targetTTL)
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("cannot parse target ttl %s: %v", *targetTTL, err)
|
|
|
|
}
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
fmt.Printf("Changing ttl to: %s\n", ttl)
|
2016-06-06 19:27:28 +00:00
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
superBlock.Ttl = ttl
|
|
|
|
hasChange = true
|
2016-06-06 19:27:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 03:42:28 +00:00
|
|
|
if hasChange {
|
|
|
|
|
2018-07-09 12:35:04 +00:00
|
|
|
header := superBlock.Bytes()
|
2016-11-05 03:42:28 +00:00
|
|
|
|
2021-02-21 04:06:06 +00:00
|
|
|
if n, e := datBackend.WriteAt(header, 0); n == 0 || e != nil {
|
2016-11-05 03:42:28 +00:00
|
|
|
glog.Fatalf("cannot write super block: %v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Change Applied.")
|
|
|
|
}
|
2016-06-06 19:27:28 +00:00
|
|
|
|
|
|
|
}
|