diff options
| author | Chris Lu <chris.lu@gmail.com> | 2014-03-02 22:16:54 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2014-03-02 22:16:54 -0800 |
| commit | 27c74a7e66558a4f9ce0d10621606dfed98a3abb (patch) | |
| tree | f16eef19480fd51ccbef54c05d39c2eacf309e56 /go/storage/replica_placement.go | |
| parent | edae676913363bdd1e5a50bf0778fdcc3c6d6051 (diff) | |
| download | seaweedfs-27c74a7e66558a4f9ce0d10621606dfed98a3abb.tar.xz seaweedfs-27c74a7e66558a4f9ce0d10621606dfed98a3abb.zip | |
Major:
change replication_type to ReplicaPlacement, hopefully cleaner code
works for 9 possible ReplicaPlacement
xyz
x : number of copies on other data centers
y : number of copies on other racks
z : number of copies on current rack
x y z each can be 0,1,2
Minor:
weed server "-mdir" default to "-dir" if empty
Diffstat (limited to 'go/storage/replica_placement.go')
| -rw-r--r-- | go/storage/replica_placement.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/go/storage/replica_placement.go b/go/storage/replica_placement.go new file mode 100644 index 000000000..55428749b --- /dev/null +++ b/go/storage/replica_placement.go @@ -0,0 +1,61 @@ +package storage + +import ( + "errors" + "fmt" +) + +const ( + ReplicaPlacementCount = 9 +) + +type ReplicaPlacement struct { + SameRackCount int + DiffRackCount int + DiffDataCenterCount int +} + +func NewReplicaPlacementFromString(t string) (*ReplicaPlacement, error) { + rp := &ReplicaPlacement{} + for i, c := range t { + count := int(c - '0') + if 0 <= count && count <= 2 { + switch i { + case 0: + rp.DiffDataCenterCount = count + case 1: + rp.DiffRackCount = count + case 2: + rp.SameRackCount = count + } + } else { + return rp, errors.New("Unknown Replication Type:" + t) + } + } + return rp, nil +} + +func NewReplicaPlacementFromByte(b byte) (*ReplicaPlacement, error) { + return NewReplicaPlacementFromString(fmt.Sprintf("%d", b)) +} + +func (rp *ReplicaPlacement) Byte() byte { + ret := rp.DiffDataCenterCount*100 + rp.DiffRackCount*10 + rp.SameRackCount + return byte(ret) +} + +func (rp *ReplicaPlacement) String() string { + b := make([]byte, 3) + b[0] = byte(rp.DiffDataCenterCount + '0') + b[1] = byte(rp.DiffRackCount + '0') + b[2] = byte(rp.SameRackCount + '0') + return string(b) +} + +func (rp *ReplicaPlacement) GetCopyCount() int { + return rp.DiffDataCenterCount + rp.DiffRackCount + rp.SameRackCount + 1 +} + +func (rp *ReplicaPlacement) GetReplicationLevelIndex() int { + return rp.DiffDataCenterCount*3 + rp.DiffRackCount*3 + rp.SameRackCount +} |
