Skip to content

Commit

Permalink
enhancement: when creating vol, you can specify the number of replica…
Browse files Browse the repository at this point in the history
… of the data partition

Signed-off-by: zhuhyc <[email protected]>
  • Loading branch information
zhuhyc committed Feb 17, 2020
1 parent 72be732 commit 3c78bc6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
19 changes: 16 additions & 3 deletions master/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,23 @@ func (m *Server) createVol(w http.ResponseWriter, r *http.Request) {
msg string
size int
mpCount int
dpReplicaNum int
capacity int
vol *Vol
followerRead bool
authenticate bool
)

if name, owner, mpCount, size, capacity, followerRead, authenticate, err = parseRequestToCreateVol(r); err != nil {
if name, owner, mpCount, dpReplicaNum, size, capacity, followerRead, authenticate, err = parseRequestToCreateVol(r); err != nil {
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeParamError, Msg: err.Error()})
return
}
if vol, err = m.cluster.createVol(name, owner, mpCount, size, capacity, followerRead, authenticate); err != nil {
if dpReplicaNum != 0 && dpReplicaNum < 2 {
err = fmt.Errorf("replicaNum can't be less than 2,replicaNum[%v]", dpReplicaNum)
sendErrReply(w, r, &proto.HTTPReply{Code: proto.ErrCodeParamError, Msg: err.Error()})
return
}
if vol, err = m.cluster.createVol(name, owner, mpCount, dpReplicaNum, size, capacity, followerRead, authenticate); err != nil {
sendErrReply(w, r, newErrHTTPReply(err))
return
}
Expand Down Expand Up @@ -1028,7 +1034,7 @@ func parseBoolFieldToUpdateVol(r *http.Request, vol *Vol) (followerRead, authent
return
}

func parseRequestToCreateVol(r *http.Request) (name, owner string, mpCount, size, capacity int, followerRead, authenticate bool, err error) {
func parseRequestToCreateVol(r *http.Request) (name, owner string, mpCount, dpReplicaNum, size, capacity int, followerRead, authenticate bool, err error) {
if err = r.ParseForm(); err != nil {
return
}
Expand All @@ -1045,6 +1051,13 @@ func parseRequestToCreateVol(r *http.Request) (name, owner string, mpCount, size
}
}

if replicaStr := r.FormValue(replicaNumKey); replicaStr == "" {
dpReplicaNum = defaultReplicaNum
} else if dpReplicaNum, err = strconv.Atoi(replicaStr); err != nil {
err = unmatchedKey(replicaNumKey)
return
}

if sizeStr := r.FormValue(dataPartitionSizeKey); sizeStr != "" {
if size, err = strconv.Atoi(sizeStr); err != nil {
err = unmatchedKey(dataPartitionSizeKey)
Expand Down
2 changes: 1 addition & 1 deletion master/api_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func createDefaultMasterServerForTest() *Server {
time.Sleep(5 * time.Second)
testServer.cluster.scheduleToUpdateStatInfo()
fmt.Printf("nodeSet len[%v]\n", len(testServer.cluster.t.nodeSetMap))
testServer.cluster.createVol(commonVolName, "cfs", 3, 3, 100, false, false)
testServer.cluster.createVol(commonVolName, "cfs", 3, 3, 3, 100, false, false)
vol, err := testServer.cluster.getVol(commonVolName)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions master/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ errHandler:

// Create a new volume.
// By default we create 3 meta partitions and 10 data partitions during initialization.
func (c *Cluster) createVol(name, owner string, mpCount, size, capacity int, followerRead, authenticate bool) (vol *Vol, err error) {
func (c *Cluster) createVol(name, owner string, mpCount, dpReplicaNum, size, capacity int, followerRead, authenticate bool) (vol *Vol, err error) {
var (
dataPartitionSize uint64
readWriteDataPartitions int
Expand All @@ -1146,7 +1146,7 @@ func (c *Cluster) createVol(name, owner string, mpCount, size, capacity int, fol
} else {
dataPartitionSize = uint64(size) * util.GB
}
if vol, err = c.doCreateVol(name, owner, dataPartitionSize, uint64(capacity), defaultReplicaNum, followerRead, authenticate); err != nil {
if vol, err = c.doCreateVol(name, owner, dataPartitionSize, uint64(capacity), dpReplicaNum, followerRead, authenticate); err != nil {
goto errHandler
}
if err = vol.initMetaPartitions(c, mpCount); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion master/vol.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Vol struct {
func newVol(id uint64, name, owner string, dpSize, capacity uint64, dpReplicaNum, mpReplicaNum uint8, followerRead, authenticate bool) (vol *Vol) {
vol = &Vol{ID: id, Name: name, MetaPartitions: make(map[uint64]*MetaPartition, 0)}
vol.dataPartitions = newDataPartitionMap(name)
if dpReplicaNum < 1 {
if dpReplicaNum <= 1 {
dpReplicaNum = defaultReplicaNum
}
vol.dpReplicaNum = dpReplicaNum
Expand Down
2 changes: 1 addition & 1 deletion master/vol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func markDeleteVol(name string, t *testing.T) {

func TestVolReduceReplicaNum(t *testing.T) {
volName := "reduce-replica-num"
vol, err := server.cluster.createVol(volName, volName, 3, util.DefaultDataPartitionSize, 100, false, false)
vol, err := server.cluster.createVol(volName, volName, 3, 3, util.DefaultDataPartitionSize, 100, false, false)
if err != nil {
t.Error(err)
return
Expand Down

0 comments on commit 3c78bc6

Please sign in to comment.