-
Notifications
You must be signed in to change notification settings - Fork 49
Policy probe util #3790
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
Merged
Merged
Policy probe util #3790
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| package policy | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/nspcc-dev/neofs-node/cmd/internal/cmdprinter" | ||
| internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" | ||
| "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" | ||
| "github.com/nspcc-dev/neofs-sdk-go/client" | ||
| cid "github.com/nspcc-dev/neofs-sdk-go/container/id" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| policyFlag string | ||
| short bool | ||
| ) | ||
|
|
||
| var checkCmd = &cobra.Command{ | ||
| Use: "check", | ||
| Short: "Check placement policy", | ||
| Long: `Check placement policy parsing and validation. | ||
| Policy can be provided as QL-encoded string, JSON-encoded string or path to file with it. | ||
| Shows nodes that will be used for container placement based on current network map snapshot. | ||
| Note: this command uses a zero container ID for placement; when there are many nodes and | ||
| only a subset must be chosen, results may differ from a real container.`, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| placementPolicy, err := ParseContainerPolicy(cmd, policyFlag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ctx, cancel := commonflags.GetCommandContext(cmd) | ||
| defer cancel() | ||
|
|
||
| cli, err := internalclient.GetSDKClientByFlag(ctx, commonflags.RPC) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer cli.Close() | ||
|
|
||
| nm, err := cli.NetMapSnapshot(ctx, client.PrmNetMapSnapshot{}) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to get netmap snapshot to validate container placement: %w", err) | ||
| } | ||
|
|
||
| placementNodes, err := nm.ContainerNodes(*placementPolicy, cid.ID{}) | ||
|
carpawell marked this conversation as resolved.
|
||
| if err != nil { | ||
| return fmt.Errorf("could not build container nodes based on given placement policy: %w", err) | ||
| } | ||
|
|
||
| repRuleNum := placementPolicy.NumberOfReplicas() | ||
| for i := range repRuleNum { | ||
| if placementPolicy.ReplicaNumberByIndex(i) > uint32(len(placementNodes[i])) { | ||
| return fmt.Errorf( | ||
| "the number of nodes '%d' in selector is not enough for the number of replicas '%d'", | ||
| len(placementNodes[i]), | ||
| placementPolicy.ReplicaNumberByIndex(i), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ecRules := placementPolicy.ECRules() | ||
| for i := range ecRules { | ||
| d := ecRules[i].DataPartNum() | ||
| p := ecRules[i].ParityPartNum() | ||
| n := uint32(len(placementNodes[repRuleNum+i])) | ||
| if d > n || p > n || d+p > n { | ||
| return fmt.Errorf( | ||
| "the number of nodes '%d' in selector is not enough for EC rule '%d/%d'", n, d, p) | ||
| } | ||
| } | ||
|
|
||
| cmdprinter.PrettyPrintPlacementPolicyNodes(cmd, placementNodes, *placementPolicy, short) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func initCheckCmd() { | ||
| flags := checkCmd.Flags() | ||
|
|
||
| flags.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage) | ||
| flags.DurationP(commonflags.Timeout, commonflags.TimeoutShorthand, commonflags.TimeoutDefault, commonflags.TimeoutUsage) | ||
| flags.StringVarP(&policyFlag, "policy", "p", "", "QL-encoded or JSON-encoded placement policy or path to file with it") | ||
| flags.BoolVar(&short, "short", false, "Shortens output of node info") | ||
|
|
||
| _ = checkCmd.MarkFlagRequired(commonflags.RPC) | ||
| _ = checkCmd.MarkFlagRequired("policy") | ||
| } | ||
This file contains hidden or 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,22 @@ | ||
| package policy | ||
|
|
||
| import ( | ||
| "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // Cmd represents the policy command. | ||
| var Cmd = &cobra.Command{ | ||
| Use: "policy", | ||
| Short: "Operations with container placement policy", | ||
| Long: "Operations with container placement policy", | ||
| PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
| commonflags.Bind(cmd) | ||
| commonflags.BindAPI(cmd) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.AddCommand(checkCmd) | ||
| initCheckCmd() | ||
| } |
This file contains hidden or 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,44 @@ | ||
| package policy | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" | ||
| "github.com/nspcc-dev/neofs-sdk-go/netmap" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // ParseContainerPolicy tries to parse the provided string as a path to file with placement policy, | ||
| // then as QL and JSON encoded policies. Returns an error if all attempts fail. | ||
| func ParseContainerPolicy(cmd *cobra.Command, policyString string) (*netmap.PlacementPolicy, error) { | ||
| _, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy | ||
| if err == nil { | ||
| common.PrintVerbose(cmd, "Reading placement policy from file: %s", policyString) | ||
|
|
||
| data, err := os.ReadFile(policyString) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("can't read file with placement policy: %w", err) | ||
| } | ||
|
|
||
| policyString = string(data) | ||
| } | ||
|
|
||
| var result netmap.PlacementPolicy | ||
|
|
||
| err = result.DecodeString(policyString) | ||
| if err == nil { | ||
| common.PrintVerbose(cmd, "Parsed QL encoded policy") | ||
| return &result, nil | ||
| } | ||
| common.PrintVerbose(cmd, "Can't parse policy as QL: %v", err) | ||
|
|
||
| if err = result.UnmarshalJSON([]byte(policyString)); err == nil { | ||
| common.PrintVerbose(cmd, "Parsed JSON encoded policy") | ||
| return &result, nil | ||
| } | ||
| common.PrintVerbose(cmd, "Can't parse policy as JSON: %v", err) | ||
|
|
||
| return nil, errors.New("can't parse placement policy") | ||
|
carpawell marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| ## neofs-cli container policy | ||
|
|
||
| Operations with container placement policy | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Operations with container placement policy | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for policy | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -c, --config string Config file (default is $HOME/.config/neofs-cli/config.yaml) | ||
| -v, --verbose Verbose output | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [neofs-cli container](neofs-cli_container.md) - Operations with containers | ||
| * [neofs-cli container policy check](neofs-cli_container_policy_check.md) - Check placement policy | ||
|
|
Oops, something went wrong.
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.
policy can naturally be an arg (not a flag) imo, but do not insist
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.
I am in favor of using flags. I made the flag required for clarity.