-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test #205
Open
Feng-ATM
wants to merge
4
commits into
opencurve:master
Choose a base branch
from
Feng-ATM:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
test #205
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"github.com/opencurve/curveadm/cli/command/client" | ||
"github.com/opencurve/curveadm/cli/command/cluster" | ||
"github.com/opencurve/curveadm/cli/command/config" | ||
"github.com/opencurve/curveadm/cli/command/disks" | ||
"github.com/opencurve/curveadm/cli/command/hosts" | ||
"github.com/opencurve/curveadm/cli/command/pfs" | ||
"github.com/opencurve/curveadm/cli/command/playground" | ||
|
@@ -61,6 +62,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) { | |
cluster.NewClusterCommand(curveadm), // curveadm cluster ... | ||
config.NewConfigCommand(curveadm), // curveadm config ... | ||
hosts.NewHostsCommand(curveadm), // curveadm hosts ... | ||
disks.NewDisksCommand(curveadm), // curveadm disks ... | ||
playground.NewPlaygroundCommand(curveadm), // curveadm playground ... | ||
target.NewTargetCommand(curveadm), // curveadm target ... | ||
pfs.NewPFSCommand(curveadm), // curveadm pfs ... | ||
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. with a basic code review
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2023-02-24 | ||
* Author: Lijin Xiong ([email protected]) | ||
*/ | ||
|
||
package disks | ||
|
||
import ( | ||
"github.com/opencurve/curveadm/cli/cli" | ||
cliutil "github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewDisksCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "disks", | ||
Short: "Manage disks", | ||
Args: cliutil.NoArgs, | ||
RunE: cliutil.ShowHelp(curveadm.Err()), | ||
} | ||
|
||
cmd.AddCommand( | ||
NewCommitCommand(curveadm), | ||
NewShowCommand(curveadm), | ||
NewListCommand(curveadm), | ||
) | ||
return cmd | ||
} | ||
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. the code review.
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveAdm | ||
* Created Date: 2023-02-24 | ||
* Author: Lijin Xiong ([email protected]) | ||
*/ | ||
|
||
package disks | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/opencurve/curveadm/cli/cli" | ||
"github.com/opencurve/curveadm/internal/common" | ||
comm "github.com/opencurve/curveadm/internal/common" | ||
"github.com/opencurve/curveadm/internal/configure/disks" | ||
"github.com/opencurve/curveadm/internal/configure/topology" | ||
"github.com/opencurve/curveadm/internal/errno" | ||
"github.com/opencurve/curveadm/internal/storage" | ||
"github.com/opencurve/curveadm/internal/tui" | ||
tuicomm "github.com/opencurve/curveadm/internal/tui/common" | ||
"github.com/opencurve/curveadm/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
COMMIT_EXAMPLE = `Examples: | ||
$ curveadm disks commit /path/to/disks.yaml # Commit disks` | ||
) | ||
|
||
type commitOptions struct { | ||
filename string | ||
slient bool | ||
} | ||
|
||
func NewCommitCommand(curveadm *cli.CurveAdm) *cobra.Command { | ||
var options commitOptions | ||
cmd := &cobra.Command{ | ||
Use: "commit DISKS [OPTIONS]", | ||
Short: "Commit disks", | ||
Args: utils.ExactArgs(1), | ||
Example: COMMIT_EXAMPLE, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
options.filename = args[0] | ||
return runCommit(curveadm, options) | ||
}, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.BoolVarP(&options.slient, "slient", "s", false, "Slient output for disks commit") | ||
|
||
return cmd | ||
} | ||
|
||
func readAndCheckDisks(curveadm *cli.CurveAdm, options commitOptions) (string, []*disks.DiskConfig, error) { | ||
var dcs []*disks.DiskConfig | ||
// 1) read disks from file | ||
if !utils.PathExist(options.filename) { | ||
return "", dcs, errno.ERR_DISKS_FILE_NOT_FOUND. | ||
F("%s: no such file", utils.AbsPath(options.filename)) | ||
} | ||
data, err := utils.ReadFile(options.filename) | ||
if err != nil { | ||
return data, dcs, errno.ERR_READ_DISKS_FILE_FAILED.E(err) | ||
} | ||
|
||
// 2) display disks difference | ||
oldData := curveadm.Disks() | ||
if !options.slient { | ||
diff := utils.Diff(oldData, data) | ||
curveadm.WriteOutln(diff) | ||
} | ||
|
||
// 3) check disks data | ||
dcs, err = disks.ParseDisks(data, curveadm) | ||
return data, dcs, err | ||
} | ||
|
||
func assambleNewDiskRecords(dcs []*disks.DiskConfig, | ||
oldDiskRecords []storage.Disk) ([]storage.Disk, []storage.Disk) { | ||
keySep := ":" | ||
newDiskMap := make(map[string]bool) | ||
|
||
var newDiskRecords, diskRecordDeleteList []storage.Disk | ||
for _, dc := range dcs { | ||
for _, host := range dc.GetHost() { | ||
key := strings.Join([]string{host, dc.GetDevice()}, keySep) | ||
newDiskMap[key] = true | ||
newDiskRecords = append( | ||
newDiskRecords, storage.Disk{ | ||
Host: host, | ||
Device: dc.GetDevice(), | ||
Size: comm.DISK_DEFAULT_NULL_SIZE, | ||
URI: comm.DISK_DEFAULT_NULL_URI, | ||
MountPoint: dc.GetMountPoint(), | ||
FormatPercent: dc.GetFormatPercent(), | ||
ChunkServerID: comm.DISK_DEFAULT_NULL_CHUNKSERVER_ID, | ||
}) | ||
} | ||
} | ||
|
||
for _, dr := range oldDiskRecords { | ||
key := strings.Join([]string{dr.Host, dr.Device}, keySep) | ||
if _, ok := newDiskMap[key]; !ok { | ||
diskRecordDeleteList = append(diskRecordDeleteList, dr) | ||
} | ||
} | ||
|
||
return newDiskRecords, diskRecordDeleteList | ||
} | ||
|
||
func writeDiskRecord(dr storage.Disk, curveadm *cli.CurveAdm) error { | ||
if diskRecords, err := curveadm.Storage().GetDisk( | ||
common.DISK_FILTER_DEVICE, dr.Host, dr.Device); err != nil { | ||
return err | ||
} else if len(diskRecords) == 0 { | ||
if err := curveadm.Storage().SetDisk( | ||
dr.Host, | ||
dr.Device, | ||
dr.MountPoint, | ||
dr.ContainerImage, | ||
dr.FormatPercent); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func syncDiskRecords(data string, dcs []*disks.DiskConfig, | ||
curveadm *cli.CurveAdm, options commitOptions) error { | ||
oldDiskRecords := curveadm.DiskRecords() | ||
tui.SortDiskRecords(oldDiskRecords) | ||
|
||
newDiskRecords, diskRecordDeleteList := assambleNewDiskRecords(dcs, oldDiskRecords) | ||
tui.SortDiskRecords(newDiskRecords) | ||
oldDiskRecordsString := tui.FormatDisks(oldDiskRecords) | ||
newDiskRecordsString := tui.FormatDisks(newDiskRecords) | ||
|
||
if !options.slient { | ||
diff := utils.Diff(oldDiskRecordsString, newDiskRecordsString) | ||
curveadm.WriteOutln(diff) | ||
} | ||
|
||
pass := tuicomm.ConfirmYes("Disk changes are showing above. Do you want to continue?") | ||
if !pass { | ||
curveadm.WriteOut(tuicomm.PromptCancelOpetation("commit disk table")) | ||
return errno.ERR_CANCEL_OPERATION | ||
} | ||
|
||
// write new disk records | ||
for _, dr := range newDiskRecords { | ||
if err := writeDiskRecord(dr, curveadm); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// delete obsolete disk records | ||
for _, dr := range diskRecordDeleteList { | ||
if dr.ChunkServerID != comm.DISK_DEFAULT_NULL_CHUNKSERVER_ID { | ||
return errno.ERR_DELETE_SERVICE_BINDING_DISK. | ||
F("The disk[%s:%s] is used by service[%s:%s]", | ||
dr.Host, dr.Device, topology.ROLE_CHUNKSERVER, dr.ChunkServerID) | ||
} | ||
|
||
if err := curveadm.Storage().DeleteDisk(dr.Host, dr.Device); err != nil { | ||
return errno.ERR_UPDATE_DISK_FAILED.E(err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runCommit(curveadm *cli.CurveAdm, options commitOptions) error { | ||
// 1) read and check disks | ||
data, dcs, err := readAndCheckDisks(curveadm, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2) confirm by user | ||
pass := tuicomm.ConfirmYes("Do you want to continue?") | ||
if !pass { | ||
curveadm.WriteOut(tuicomm.PromptCancelOpetation("commit disks")) | ||
return errno.ERR_CANCEL_OPERATION | ||
} | ||
|
||
// 3) add disk records | ||
err = syncDiskRecords(data, dcs, curveadm, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 4) add disks data | ||
err = curveadm.Storage().SetDisks(data) | ||
if err != nil { | ||
return errno.ERR_UPDATE_DISKS_FAILED. | ||
F("commit disks failed") | ||
} | ||
|
||
// 5) print success prompt | ||
curveadm.WriteOutln(color.GreenString("Disks updated")) | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
.
The code patch adds two new variables (disks and diskRecords). It also added two functions to get the disks and disk records.
The code looks good and appears to be properly formatted. The only issue I see is that the variables disks and diskRecords are not declared as constants, which might lead to confusion when a user is modifying the code. To make the code easier to read, I would suggest declaring the variables as constants. Additionally, it would be helpful to include comments for each function to explain what it does.