-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: master
Are you sure you want to change the base?
Policy probe util #3790
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| 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.`, | ||
| 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{}) | ||
|
Member
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. @cthulhu-rider, is it possible to get a different result in some corner cases if we intentionally put |
||
| 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") | ||
| } | ||
| 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() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 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 | ||
| } | ||
|
|
||
| if err = result.UnmarshalJSON([]byte(policyString)); err == nil { | ||
| common.PrintVerbose(cmd, "Parsed JSON encoded policy") | ||
| return &result, nil | ||
| } | ||
|
|
||
| return nil, errors.New("can't parse placement policy") | ||
|
Member
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 no details at all? can be hard to debug for a user, even in
Member
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. oh, that is a copy from another file, ok |
||
| } | ||
| 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 | ||
|
|
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