-
Notifications
You must be signed in to change notification settings - Fork 82
Edit Zone API #603
Edit Zone API #603
Changes from 21 commits
3caf951
76324c9
fe15576
9d1d4bf
8ec64a7
9ab82ca
13c874d
7ea6120
3f6a1dd
1f94dd3
c57768e
47f90bd
84e8866
f11f68e
f2e2b8c
cfd8136
4e12707
ec3b9c0
1a6c001
8282ce4
d589c4d
7d905c7
552fbb4
034fa48
3ed7136
62273e4
8ad368d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"net" | ||
|
||
"github.com/gluster/glusterd2/glusterd2/gdctx" | ||
"github.com/gluster/glusterd2/pkg/errors" | ||
|
||
config "github.com/spf13/viper" | ||
) | ||
|
@@ -39,18 +40,27 @@ func normalizeAddrs() ([]string, error) { | |
|
||
// AddSelfDetails results in the peer adding its own details into etcd | ||
func AddSelfDetails() error { | ||
var err error | ||
|
||
var err error | ||
p := &Peer{ | ||
ID: gdctx.MyUUID, | ||
Name: gdctx.HostName, | ||
PeerAddresses: []string{config.GetString("peeraddress")}, | ||
} | ||
|
||
p.ClientAddresses, err = normalizeAddrs() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
peerInfo, err := GetPeer(gdctx.MyUUID.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why these changes required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case the glusterd2 service was restarted and the data (metadata ) already existed then it is important to go and get back the data . |
||
if err == errors.ErrPeerNotFound { | ||
p.Metadata = make(map[string]string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have check |
||
p.Metadata["_zone"] = p.ID.String() | ||
} else if err == nil && peerInfo != nil { | ||
p.Metadata = peerInfo.Metadata | ||
} else { | ||
return err | ||
} | ||
|
||
return AddOrUpdatePeer(p) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmdexec | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move only device commands to
Move other parts of code back to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there a need to move commands to device utils?, won't it be useful if a separate library is made for commands There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the discussion with you, I grouped the device related functions in separate file here 2adafaa#diff-388b0ce2a0b41316c76c470557c4cbd1 |
||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/gluster/glusterd2/glusterd2/transaction" | ||
) | ||
|
||
func createVgName(device string) string { | ||
vgName := strings.Replace("vg"+device, "/", "-", -1) | ||
return vgName | ||
} | ||
|
||
// DeviceSetup is used to prepare device before using devices. | ||
func DeviceSetup(c transaction.TxnCtx, device string) error { | ||
|
||
var err error | ||
defer func() { | ||
if err != nil { | ||
DeviceCleanup(c, device) | ||
} | ||
}() | ||
pvcreateCmd := exec.Command("pvcreate", "--metadatasize=128M", "--dataalignment=256K", device) | ||
if err := pvcreateCmd.Run(); err != nil { | ||
c.Logger().WithError(err).WithField("device", device).Error("pvcreate failed for device") | ||
return err | ||
} | ||
vgName := createVgName(device) | ||
vgcreateCmd := exec.Command("vgcreate", vgName, device) | ||
if err = vgcreateCmd.Run(); err != nil { | ||
c.Logger().WithError(err).WithField("device", device).Error("vgcreate failed for device") | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
// DeviceCleanup is used to clean up devices. | ||
func DeviceCleanup(c transaction.TxnCtx, device string) { | ||
vgName := createVgName(device) | ||
vgremoveCmd := exec.Command("vgremove", vgName) | ||
if err := vgremoveCmd.Run(); err != nil { | ||
c.Logger().WithError(err).WithField("device", device).Error("vgremove failed for device") | ||
} | ||
pvremoveCmd := exec.Command("pvremove", device) | ||
if err := pvremoveCmd.Run(); err != nil { | ||
c.Logger().WithError(err).WithField("device", device).Error("pvremove failed for device") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package device | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gluster/glusterd2/glusterd2/gdctx" | ||
|
@@ -18,18 +19,30 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { | |
|
||
ctx := r.Context() | ||
logger := gdctx.GetReqLogger(ctx) | ||
peerID := mux.Vars(r)["peerid"] | ||
if uuid.Parse(peerID) == nil { | ||
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "Invalid peer-id passed in url") | ||
return | ||
} | ||
|
||
req := new(deviceapi.AddDeviceReq) | ||
if err := restutils.UnmarshalRequest(r, req); err != nil { | ||
logger.WithError(err).Error("Failed to Unmarshal request") | ||
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, errors.ErrJSONParsingFailed) | ||
return | ||
} | ||
peerID := mux.Vars(r)["peerid"] | ||
if peerID == "" { | ||
restutils.SendHTTPError(ctx, w, http.StatusBadRequest, "peerid not present in request") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lock here. |
||
lock, unlock := transaction.CreateLockFuncs(peerID) | ||
if err := lock(ctx); err != nil { | ||
if err == transaction.ErrLockTimeout { | ||
restutils.SendHTTPError(ctx, w, http.StatusConflict, err) | ||
} else { | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
} | ||
return | ||
} | ||
defer unlock(ctx) | ||
|
||
peerInfo, err := peer.GetPeer(peerID) | ||
if err != nil { | ||
logger.WithError(err).WithField("peerid", peerID).Error("Peer ID not found in store") | ||
|
@@ -40,27 +53,39 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { | |
} | ||
return | ||
} | ||
|
||
var devices []deviceapi.Info | ||
err = json.Unmarshal([]byte(peerInfo.Metadata["_"]), &devices) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. peerInfo.Metadata["_devices"]? |
||
if err != nil { | ||
logger.WithError(err).WithField("peerid", peerID).Error(err) | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
if !CheckIfDeviceExist(req.Devices, devices) { | ||
logger.WithError(err).WithField("device", req.Devices).Error(" One or more devices already exists") | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, " One or more devices already exists") | ||
return | ||
} | ||
|
||
txn := transaction.NewTxn(ctx) | ||
defer txn.Cleanup() | ||
lock, unlock, err := transaction.CreateLockSteps(peerInfo.ID.String()) | ||
|
||
txn.Nodes = []uuid.UUID{peerInfo.ID} | ||
txn.Steps = []*transaction.Step{ | ||
lock, | ||
{ | ||
DoFunc: "prepare-device", | ||
Nodes: txn.Nodes, | ||
}, | ||
unlock, | ||
} | ||
err = txn.Ctx.Set("peerid", peerID) | ||
err = txn.Ctx.Set("peerid", &peerID) | ||
if err != nil { | ||
logger.WithError(err).Error("Failed to set data for transaction") | ||
logger.WithError(err).WithField("key", "peerid").WithField("value", peerID).Error("Failed to set key in transaction context") | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
err = txn.Ctx.Set("req", req) | ||
err = txn.Ctx.Set("", &req.Devices) | ||
if err != nil { | ||
logger.WithError(err).Error("Failed to set data for transaction") | ||
logger.WithError(err).WithField("key", "").Error("Failed to set key in transaction context") | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
@@ -72,7 +97,7 @@ func deviceAddHandler(w http.ResponseWriter, r *http.Request) { | |
} | ||
peerInfo, err = peer.GetPeer(peerID) | ||
if err != nil { | ||
logger.WithError(err).Error("Failed to get peer from store") | ||
logger.WithError(err).WithField("peerid", peerID).Error("Failed to get peer from store") | ||
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, "Failed to get peer from store") | ||
return | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,29 @@ func GetDevices(peerID string) ([]deviceapi.Info, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
if len(peerInfo.MetaData["devices"]) > 0 { | ||
if len(peerInfo.Metadata["_devices"]) > 0 { | ||
var deviceInfo []deviceapi.Info | ||
if err := json.Unmarshal([]byte(peerInfo.MetaData["devices"]), &deviceInfo); err != nil { | ||
if err := json.Unmarshal([]byte(peerInfo.Metadata["_devices"]), &deviceInfo); err != nil { | ||
return nil, err | ||
} | ||
return deviceInfo, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
//CheckIfDeviceExist returns error if all devices already exist or returns list of devices to be added | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect comment: This function returns true if any of the devices exist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, do not export this function until you have an external consumer out of this package. |
||
func CheckIfDeviceExist(reqDevices []string, devices []deviceapi.Info) bool { | ||
|
||
for _, key := range reqDevices { | ||
for _, reqKey := range devices { | ||
if key == reqKey.Name { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// AddDevices adds device to specific peer | ||
func AddDevices(devices []deviceapi.Info, peerID string) error { | ||
deviceDetails, err := GetDevices(peerID) | ||
|
@@ -40,7 +53,7 @@ func AddDevices(devices []deviceapi.Info, peerID string) error { | |
if err != nil { | ||
return err | ||
} | ||
peerInfo.MetaData["devices"] = string(deviceJSON) | ||
peerInfo.Metadata["_devices"] = string(deviceJSON) | ||
err = peer.AddOrUpdatePeer(peerInfo) | ||
if err != nil { | ||
return err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error can be added to generic errors list. Used this in multiple places and also Volume metadata will also requires this error msg.