|
| 1 | +/* |
| 2 | + (c) Copyright [2023-2024] Open Text. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + You may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package commands |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/vertica/vcluster/vclusterops" |
| 25 | + "github.com/vertica/vcluster/vclusterops/vlog" |
| 26 | +) |
| 27 | + |
| 28 | +/* CmdClusterHealth |
| 29 | + * |
| 30 | + * Implements ClusterCommand interface |
| 31 | + */ |
| 32 | +type CmdClusterHealth struct { |
| 33 | + clusterHealthOptions *vclusterops.VClusterHealthOptions |
| 34 | + |
| 35 | + CmdBase |
| 36 | +} |
| 37 | + |
| 38 | +func makeCmdClusterHealth() *cobra.Command { |
| 39 | + // CmdClusterHealth |
| 40 | + newCmd := &CmdClusterHealth{} |
| 41 | + opt := vclusterops.VClusterHealthFactory() |
| 42 | + newCmd.clusterHealthOptions = &opt |
| 43 | + |
| 44 | + cmd := makeBasicCobraCmd( |
| 45 | + newCmd, |
| 46 | + clusterHealth, |
| 47 | + "Checks the database cluster health. This is used for testing and debugging only.", |
| 48 | + `Checks the database cluster health. |
| 49 | + |
| 50 | +This is used for testing and debugging only. |
| 51 | +
|
| 52 | +Examples: |
| 53 | + # Check the cluster health |
| 54 | + vcluster cluster_health |
| 55 | +`, |
| 56 | + // TODO: modify this |
| 57 | + []string{dbNameFlag, configFlag, hostsFlag, ipv6Flag, passwordFlag, outputFileFlag}, |
| 58 | + ) |
| 59 | + |
| 60 | + // local flags |
| 61 | + newCmd.setLocalFlags(cmd) |
| 62 | + |
| 63 | + return cmd |
| 64 | +} |
| 65 | + |
| 66 | +// setLocalFlags will set the local flags the command has |
| 67 | +func (c *CmdClusterHealth) setLocalFlags(cmd *cobra.Command) { |
| 68 | + // TODO: add some code here |
| 69 | + // local flag of CmdClusterHealth, |
| 70 | + // --operation : the operation type, including "check", "get_slow_events", "get_transaction_starts", "get_session_starts" |
| 71 | + // --txn-id : the transaction id (for slow event and trsanction start) |
| 72 | + // --node-name : the node name (for all operations) |
| 73 | + // --start-time : the start time (for all operations) |
| 74 | + // --end-time : the end time (for all operations) |
| 75 | + // --session-id : the session id (for session start and slow event) |
| 76 | + // --debug : debug mode (for all operations) |
| 77 | + // --threadhold : the threadhold of seconds for slow events (for get_slow_events) |
| 78 | + // --thread-id : the thread id (for get_slow_events) |
| 79 | + // --phase-duration-desc : the phase duration description (for get_slow_events) |
| 80 | + // --event-desc : the event description (for get_slow_events) |
| 81 | + // --user-name : the user name (for get_slow_events) |
| 82 | + |
| 83 | + cmd.Flags().StringVar( |
| 84 | + &c.clusterHealthOptions.Operation, |
| 85 | + "operation", |
| 86 | + "", |
| 87 | + "The operation type, including 'cascade', 'get_slow_events', 'get_transaction_starts', 'get_session_starts'.", |
| 88 | + ) |
| 89 | + cmd.Flags().StringVar( |
| 90 | + &c.clusterHealthOptions.TxnID, |
| 91 | + "txn-id", |
| 92 | + "", |
| 93 | + "The transaction id (for slow event and transaction start).", |
| 94 | + ) |
| 95 | + cmd.Flags().StringVar( |
| 96 | + &c.clusterHealthOptions.NodeName, |
| 97 | + "node-name", |
| 98 | + "", |
| 99 | + "The node name (for all operations).", |
| 100 | + ) |
| 101 | + cmd.Flags().StringVar( |
| 102 | + &c.clusterHealthOptions.StartTime, |
| 103 | + "start-time", |
| 104 | + "", |
| 105 | + "The start time (for all operations).", |
| 106 | + ) |
| 107 | + cmd.Flags().StringVar( |
| 108 | + &c.clusterHealthOptions.EndTime, |
| 109 | + "end-time", |
| 110 | + "", |
| 111 | + "The end time (for all operations).", |
| 112 | + ) |
| 113 | + cmd.Flags().StringVar( |
| 114 | + &c.clusterHealthOptions.SessionID, |
| 115 | + "session-id", |
| 116 | + "", |
| 117 | + "The session id (for session start and slow event).", |
| 118 | + ) |
| 119 | + cmd.Flags().BoolVar( |
| 120 | + &c.clusterHealthOptions.Debug, |
| 121 | + "debug", |
| 122 | + false, |
| 123 | + "Debug mode (for all operations).", |
| 124 | + ) |
| 125 | + cmd.Flags().StringVar( |
| 126 | + &c.clusterHealthOptions.Threadhold, |
| 127 | + "threadhold", |
| 128 | + "", |
| 129 | + "The threadhold of seconds for slow events (for get_slow_events).", |
| 130 | + ) |
| 131 | + cmd.Flags().StringVar( |
| 132 | + &c.clusterHealthOptions.ThreadID, |
| 133 | + "thread-id", |
| 134 | + "", |
| 135 | + "The thread id (for get_slow_events).", |
| 136 | + ) |
| 137 | + cmd.Flags().StringVar( |
| 138 | + &c.clusterHealthOptions.PhaseDurationDesc, |
| 139 | + "phase-duration-desc", |
| 140 | + "", |
| 141 | + "The phase duration description (for get_slow_events).", |
| 142 | + ) |
| 143 | + cmd.Flags().StringVar( |
| 144 | + &c.clusterHealthOptions.EventDesc, |
| 145 | + "event-desc", |
| 146 | + "", |
| 147 | + "The event description (for get_slow_events).", |
| 148 | + ) |
| 149 | +} |
| 150 | + |
| 151 | +func (c *CmdClusterHealth) Parse(inputArgv []string, logger vlog.Printer) error { |
| 152 | + c.argv = inputArgv |
| 153 | + logger.LogMaskedArgParse(c.argv) |
| 154 | + |
| 155 | + // for some options, we do not want to use their default values, |
| 156 | + // if they are not provided in cli, |
| 157 | + // reset the value of those options to nil |
| 158 | + c.ResetUserInputOptions(&c.clusterHealthOptions.DatabaseOptions) |
| 159 | + return c.validateParse(logger) |
| 160 | +} |
| 161 | + |
| 162 | +func (c *CmdClusterHealth) validateParse(logger vlog.Printer) error { |
| 163 | + logger.Info("Called validateParse()") |
| 164 | + |
| 165 | + if !c.usePassword() { |
| 166 | + err := c.getCertFilesFromCertPaths(&c.clusterHealthOptions.DatabaseOptions) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // validate txn id is integer |
| 173 | + if c.clusterHealthOptions.TxnID != "" { |
| 174 | + _, err := c.validateInt(c.clusterHealthOptions.TxnID, "txn-id") |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + } |
| 179 | + err := c.ValidateParseBaseOptions(&c.clusterHealthOptions.DatabaseOptions) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + return c.setDBPassword(&c.clusterHealthOptions.DatabaseOptions) |
| 184 | +} |
| 185 | + |
| 186 | +// validateInt checks if the given string is a valid integer |
| 187 | +func (c *CmdClusterHealth) validateInt(value, fieldName string) (int, error) { |
| 188 | + intValue, err := strconv.Atoi(value) |
| 189 | + if err != nil { |
| 190 | + return 0, fmt.Errorf("%s must be an integer", fieldName) |
| 191 | + } |
| 192 | + return intValue, nil |
| 193 | +} |
| 194 | + |
| 195 | +func (c *CmdClusterHealth) Run(vcc vclusterops.ClusterCommands) error { |
| 196 | + vcc.LogInfo("Called method Run()") |
| 197 | + |
| 198 | + options := c.clusterHealthOptions |
| 199 | + |
| 200 | + err := vcc.VClusterHealth(options) |
| 201 | + if err != nil { |
| 202 | + vcc.LogError(err, "failed to check cluster health.") |
| 203 | + return err |
| 204 | + } |
| 205 | + |
| 206 | + bytes, err := json.MarshalIndent(options.CascadeStack, "", " ") |
| 207 | + if err != nil { |
| 208 | + return fmt.Errorf("failed to marshal the traceback result, details: %w", err) |
| 209 | + } |
| 210 | + |
| 211 | + c.writeCmdOutputToFile(globals.file, bytes, vcc.GetLog()) |
| 212 | + vcc.LogInfo("Slow event traceback: ", "slow events", string(bytes)) |
| 213 | + |
| 214 | + return nil |
| 215 | +} |
| 216 | + |
| 217 | +// SetDatabaseOptions will assign a vclusterops.DatabaseOptions instance to the one in CmdClusterHealth |
| 218 | +func (c *CmdClusterHealth) SetDatabaseOptions(opt *vclusterops.DatabaseOptions) { |
| 219 | + c.clusterHealthOptions.DatabaseOptions = *opt |
| 220 | +} |
0 commit comments