|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package topic |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" |
| 22 | + "github.com/spf13/pflag" |
| 23 | + |
| 24 | + "github.com/streamnative/pulsarctl/pkg/cmdutils" |
| 25 | +) |
| 26 | + |
| 27 | +func SetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { |
| 28 | + desc := cmdutils.LongDescription{} |
| 29 | + desc.CommandUsedFor = "Set subscription message dispatch rate for a topic" |
| 30 | + desc.CommandPermission = "This command requires tenant admin permissions." |
| 31 | + |
| 32 | + var examples []cmdutils.Example |
| 33 | + msg := cmdutils.Example{ |
| 34 | + Desc: "Set subscription message dispatch rate for a topic", |
| 35 | + Command: "pulsarctl topics set-subscription-dispatch-rate topic " + |
| 36 | + "--msg-dispatch-rate 4 --byte-dispatch-rate 5 --dispatch-rate-period 6 --relative-to-publish-rate", |
| 37 | + } |
| 38 | + examples = append(examples, msg) |
| 39 | + desc.CommandExamples = examples |
| 40 | + |
| 41 | + var out []cmdutils.Output |
| 42 | + successOut := cmdutils.Output{ |
| 43 | + Desc: "normal output", |
| 44 | + Out: "Set subscription message dispatch rate successfully for [topic]", |
| 45 | + } |
| 46 | + out = append(out, successOut, ArgError) |
| 47 | + out = append(out, TopicNameErrors...) |
| 48 | + out = append(out, TopicLevelPolicyNotEnabledError) |
| 49 | + out = append(out, NamespaceErrors...) |
| 50 | + desc.CommandOutput = out |
| 51 | + |
| 52 | + vc.SetDescription( |
| 53 | + "set-subscription-dispatch-rate", |
| 54 | + "Set subscription message dispatch rate for a topic", |
| 55 | + desc.ToString(), |
| 56 | + desc.ExampleToString(), |
| 57 | + "set-subscription-dispatch-rate", |
| 58 | + ) |
| 59 | + dispatchRateData := &utils.DispatchRateData{} |
| 60 | + vc.SetRunFuncWithNameArg(func() error { |
| 61 | + return doSetSubscriptionDispatchRate(vc, dispatchRateData) |
| 62 | + }, "the topic name is not specified or the topic name is specified more than one") |
| 63 | + |
| 64 | + vc.FlagSetGroup.InFlagSet("SubscriptionDispatchRate", func(set *pflag.FlagSet) { |
| 65 | + set.Int64VarP( |
| 66 | + &dispatchRateData.DispatchThrottlingRateInMsg, |
| 67 | + "msg-dispatch-rate", |
| 68 | + "", |
| 69 | + -1, |
| 70 | + "message-dispatch-rate (defaults to -1 and overwrites the existing value when omitted)") |
| 71 | + set.Int64VarP( |
| 72 | + &dispatchRateData.DispatchThrottlingRateInByte, |
| 73 | + "byte-dispatch-rate", |
| 74 | + "", |
| 75 | + -1, |
| 76 | + "byte-dispatch-rate (defaults to -1 and overwrites the existing value when omitted)") |
| 77 | + set.Int64VarP( |
| 78 | + &dispatchRateData.RatePeriodInSecond, |
| 79 | + "dispatch-rate-period", |
| 80 | + "", |
| 81 | + 1, |
| 82 | + "dispatch-rate-period in second type (defaults to 1 second and overwrites the existing value when omitted)") |
| 83 | + set.BoolVarP( |
| 84 | + &dispatchRateData.RelativeToPublishRate, |
| 85 | + "relative-to-publish-rate", |
| 86 | + "", |
| 87 | + false, |
| 88 | + "dispatch rate relative to publish-rate (if publish-relative flag is enabled "+ |
| 89 | + "then broker will apply throttling value to (publish-rate + dispatch rate))") |
| 90 | + }) |
| 91 | + vc.EnableOutputFlagSet() |
| 92 | +} |
| 93 | + |
| 94 | +func doSetSubscriptionDispatchRate(vc *cmdutils.VerbCmd, dispatchRateData *utils.DispatchRateData) error { |
| 95 | + // for testing |
| 96 | + if vc.NameError != nil { |
| 97 | + return vc.NameError |
| 98 | + } |
| 99 | + |
| 100 | + topic, err := utils.GetTopicName(vc.NameArg) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + admin := cmdutils.NewPulsarClient() |
| 105 | + err = admin.Topics().SetSubscriptionDispatchRate(*topic, *dispatchRateData) |
| 106 | + if err == nil { |
| 107 | + vc.Command.Printf("Set subscription message dispatch rate successfully for [%s]\n", topic.String()) |
| 108 | + } |
| 109 | + return err |
| 110 | +} |
0 commit comments