Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
le-an-vertica committed Jul 27, 2023
1 parent 68a27df commit 38622e8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions commands/cmd_re_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ func (c *CmdReIP) Run() error {
vlog.LogPrintInfo("Re-ip is successfully completed")
return nil
}

44 changes: 23 additions & 21 deletions vclusterops/nma_re_ip_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ type NMAReIPOp struct {
quorumCount int // quorumCount = (1/2 * number of primary nodes) + 1
primaryNodeCount int
hostRequestBodyMap map[string]string
forCLI bool
mapHostToNodeName map[string]string
}

func makeNMAReIPOp(name string,
catalogPathMap map[string]string,
reIPList []ReIPInfo,
forCLI bool) NMAReIPOp {
mapHostToNodeName map[string]string) NMAReIPOp {
op := NMAReIPOp{}
op.name = name
op.catalogPathMap = catalogPathMap
op.reIPList = reIPList
op.forCLI = forCLI
op.mapHostToNodeName = mapHostToNodeName

return op
}
Expand Down Expand Up @@ -114,20 +114,24 @@ func (op *NMAReIPOp) updateReIPList(execContext *OpEngineExecContext) error {
}

func (op *NMAReIPOp) Prepare(execContext *OpEngineExecContext) error {
// get the primary node names
// this step is needed as the new host addresses
// are not in the catalog
primaryNodes := make(map[string]struct{})
nodeList := execContext.nmaVDatabase.Nodes
for i := 0; i < len(nodeList); i++ {
vnode := nodeList[i]
if vnode.IsPrimary {
primaryNodes[vnode.Name] = struct{}{}
}
}

// calculate quorum and update the hosts
hostNodeMap := execContext.nmaVDatabase.HostNodeMap
if op.forCLI {
for _, host := range execContext.hostsWithLatestCatalog {
vnode, ok := hostNodeMap[host]
if !ok {
return fmt.Errorf("[%s] cannot find %s from the catalog", op.name, host)
}
if vnode.IsPrimary {
op.hosts = append(op.hosts, host)
}
for _, host := range execContext.hostsWithLatestCatalog {
nodeName := op.mapHostToNodeName[host]
if _, ok := primaryNodes[nodeName]; ok {
op.hosts = append(op.hosts, host)
}
} else {
op.hosts = execContext.hostsWithLatestCatalog
}

// get the quorum count
Expand All @@ -139,15 +143,13 @@ func (op *NMAReIPOp) Prepare(execContext *OpEngineExecContext) error {
}

// update re-ip list
if op.forCLI {
err := op.updateReIPList(execContext)
if err != nil {
return fmt.Errorf("[%s] error udating reIP list: %w", op.name, err)
}
err := op.updateReIPList(execContext)
if err != nil {
return fmt.Errorf("[%s] error udating reIP list: %w", op.name, err)
}

// build request body for hosts
err := op.updateRequestBody(op.hosts, execContext)
err = op.updateRequestBody(op.hosts, execContext)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions vclusterops/re_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type VReIPOptions struct {
DatabaseOptions

ReIPList []ReIPInfo
ForCLI bool // whether called by the CLI
}

func VReIPFactory() VReIPOptions {
Expand Down Expand Up @@ -145,6 +144,7 @@ func produceReIPInstructions(options *VReIPOptions) ([]ClusterOp, error) {
// VER-88084 call getCatalogPath endpoint
// to get mapHostToCatalogPath and hostNodeMap
mapHostToCatalogPath := util.GetHostCatalogPath(hosts, *options.Name, *options.CatalogPrefix)
mapHostToNodeName := util.GetHostNodeName(hosts, *options.Name)

// read catalog editor to get hosts with latest catalog
nmaReadCatEdOp, err := MakeNMAReadCatalogEditorOp(mapHostToCatalogPath, []string{})
Expand All @@ -155,7 +155,7 @@ func produceReIPInstructions(options *VReIPOptions) ([]ClusterOp, error) {
// re-ip
// at this stage the re-ip info should either by provided by
// the re-ip file (for vcluster CLI) or the Kubernetes operator
nmaReIPOP := makeNMAReIPOp("NMAReIPOp", mapHostToCatalogPath, options.ReIPList, options.ForCLI)
nmaReIPOP := makeNMAReIPOp("NMAReIPOp", mapHostToCatalogPath, options.ReIPList, mapHostToNodeName)

instructions = append(instructions,
&nmaHealthOp,
Expand Down
16 changes: 15 additions & 1 deletion vclusterops/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func ParamNotSetErrorMsg(param string) error {
}

// only works for the happy path and is temporary
// will be remove after VER-88084 is completed
// will be removed after VER-88084 is completed
func GetHostCatalogPath(hosts []string, dbName, catalogPrefix string) map[string]string {
dbNameLowerCase := strings.ToLower(dbName)
hostCatalogPath := make(map[string]string)
Expand All @@ -428,6 +428,19 @@ func GetHostCatalogPath(hosts []string, dbName, catalogPrefix string) map[string
return hostCatalogPath
}

// GetHostNodeName is temporary, which
// will be removed after VER-88084 is completed
func GetHostNodeName(hosts []string, dbName string) map[string]string {
dbNameLowerCase := strings.ToLower(dbName)
mapHostToNodeName := make(map[string]string)
for i, h := range hosts {
nodeNameSuffix := i + 1
mapHostToNodeName[h] = fmt.Sprintf("v_%s_node%04d",
dbNameLowerCase, nodeNameSuffix)
}
return mapHostToNodeName
}

// ParseConfigParams builds and returns a map from a comma-separated list of params.
func ParseConfigParams(configParamListStr string) (map[string]string, error) {
return ParseKeyValueListStr(configParamListStr, "config-param")
Expand Down Expand Up @@ -476,3 +489,4 @@ func GenVNodeName(vnodes map[string]string, dbName string, hostCount int) (strin
}
return "", false
}

0 comments on commit 38622e8

Please sign in to comment.