Skip to content

Commit 983eab6

Browse files
author
Tim Middleton
committed
Add roles and run report command
1 parent e13cdd1 commit 983eab6

File tree

10 files changed

+146
-6
lines changed

10 files changed

+146
-6
lines changed

docs/reference/66_reporters.adoc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///////////////////////////////////////////////////////////////////////////////
22

3-
Copyright (c) 2021, 2023 Oracle and/or its affiliates.
3+
Copyright (c) 2021, 2024 Oracle and/or its affiliates.
44
Licensed under the Universal Permissive License v 1.0 as shown at
55
https://oss.oracle.com/licenses/upl.
66

@@ -18,6 +18,7 @@ There are various commands that allow you to work with and manage Reporters.
1818
* <<start-reporter, `cohctl start reporter`>> - starts a reporter on a specific node
1919
* <<stop-reporter, `cohctl stop reporter`>> - stops a reporter on a specific node
2020
* <<set-reporter, `cohctl set reporter`>> - sets a reporter attribute for one or more members
21+
* <<run-report, `cohctl run report`>> - runs a report on a specific node and returns the report output in JSON
2122
2223
[#get-reporters]
2324
==== Get Reporters
@@ -168,9 +169,34 @@ NODE ID STATE CONFIG FILE OUTPUT PATH BATCH# LAST REPORT LA
168169
2 Stopped reports/report-group.xml /tmp 0 0ms 0.0000ms 60 false
169170
----
170171
172+
[#run-report]
173+
==== Run
174+
175+
include::../../build/_output/docs-gen/run_report.adoc[tag=text]
176+
177+
NOTE: The otuput will always be JSON. You can use `-o jsonpath=...` to use jsonpath expression or pipe through to a utility such as `jq`.
178+
179+
This REST endpoint that this command uses is only available in the most recent Coherence releases.
180+
You will receive a HTTP 400 error if it is not supported in your Coherence version.
181+
182+
[source,bash]
183+
----
184+
cohctl run report report-node -c local -n 1
185+
----
186+
Output:
187+
[source,bash]
188+
----
189+
{"items":[{"RefreshTime":"Tue Oct 15 09:07:55 AWST 2024","ReportTime":"Tue Oct 15 09:07:55 AWST 2024",
190+
...
191+
"RoleName":"CoherenceServer","Addres,"BatchCounter":"0","rowID":3}]}
192+
----
193+
194+
NOTE: The output above is truncated for readability.
195+
171196
=== See Also
172197
173198
* <<docs/reference/20_services.adoc,Services>>
199+
* <<docs/examples/10_jsonpath.adoc,Using JSONPath>>
174200
175201
176202

pkg/cmd/cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ var (
735735
machineParam string
736736
rackParam string
737737
siteParam string
738+
roleParam string
738739
skipMavenDepsParam bool
739740
backupLogFilesParam bool
740741
validPersistenceModes = []string{"on-demand", "active", "active-backup", "active-async"}
@@ -1456,6 +1457,7 @@ func init() {
14561457
createClusterCmd.Flags().StringVarP(&machineParam, machineArg, "", "", machineMessage)
14571458
createClusterCmd.Flags().StringVarP(&rackParam, rackArg, "", "", rackMessage)
14581459
createClusterCmd.Flags().StringVarP(&siteParam, siteArg, "", "", siteMessage)
1460+
createClusterCmd.Flags().StringVarP(&roleParam, roleArg, "", "", roleMessage)
14591461

14601462
stopClusterCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
14611463

@@ -1474,6 +1476,7 @@ func init() {
14741476
startClusterCmd.Flags().StringVarP(&machineParam, machineArg, "", "", machineMessage)
14751477
startClusterCmd.Flags().StringVarP(&rackParam, rackArg, "", "", rackMessage)
14761478
startClusterCmd.Flags().StringVarP(&siteParam, siteArg, "", "", siteMessage)
1479+
startClusterCmd.Flags().StringVarP(&roleParam, roleArg, "", "", roleMessage)
14771480

14781481
startConsoleCmd.Flags().StringVarP(&heapMemoryParam, heapMemoryArg, "M", defaultHeap, heapMemoryMessage)
14791482
startConsoleCmd.Flags().Int32VarP(&logLevelParam, logLevelArg, "l", 5, logLevelMessage)
@@ -1505,6 +1508,7 @@ func init() {
15051508
scaleClusterCmd.Flags().StringVarP(&machineParam, machineArg, "", "", machineMessage)
15061509
scaleClusterCmd.Flags().StringVarP(&rackParam, rackArg, "", "", rackMessage)
15071510
scaleClusterCmd.Flags().StringVarP(&siteParam, siteArg, "", "", siteMessage)
1511+
scaleClusterCmd.Flags().StringVarP(&roleParam, roleArg, "", "", roleMessage)
15081512
}
15091513

15101514
// sanitizeConnectionName sanitizes a cluster connection

pkg/cmd/cluster_utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ func getCacheServerArgs(connection ClusterConnection, member string, httpPort in
420420
baseArgs = append(baseArgs, fmt.Sprintf("-Dcoherence.site=%s", siteParam))
421421
}
422422

423+
if roleParam != "" {
424+
baseArgs = append(baseArgs, fmt.Sprintf("-Dcoherence.role=%s", roleParam))
425+
}
426+
423427
// if default heap is overridden, then use this
424428
if heapMemoryParam != defaultHeap {
425429
heap = heapMemoryParam
@@ -661,7 +665,7 @@ func runCommandBase(command, logFileName string, arguments []string) (string, er
661665
Logger.Info("Run command", fields...)
662666
}
663667

664-
start := time.Now()
668+
startTime := time.Now()
665669
process := exec.Command(command, arguments...)
666670
if len(logFileName) > 0 {
667671
// a log file was supplied, so we are assuming this command will be async and
@@ -684,7 +688,7 @@ func runCommandBase(command, logFileName string, arguments []string) (string, er
684688

685689
if Config.Debug {
686690
fields := []zapcore.Field{
687-
zap.String("time", fmt.Sprintf("%v", time.Since(start))),
691+
zap.String("time", fmt.Sprintf("%v", time.Since(startTime))),
688692
}
689693
Logger.Info("Duration", fields...)
690694
}

pkg/cmd/reporter.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
validReporterAttributes = []string{reporterConfigFile, reporterCurrentBatch, reporterIntervalSeconds, reporterOutputPath}
3434
reporterAttributeName string
3535
reporterAttributeValue string
36+
reporterNodID int
3637
)
3738

3839
// getReportersCmd represents the get reporters command.
@@ -155,6 +156,73 @@ var describeReporterCmd = &cobra.Command{
155156
},
156157
}
157158

159+
// runReportCmd represents the run report command.
160+
var runReportCmd = &cobra.Command{
161+
Use: "report report-name",
162+
Short: "run a report and return the output",
163+
Long: `The 'run report' command runs a report on a specific node and returns the report output in JSON.
164+
The report name should not include the .xml extension and will have the 'report' prefix added. E.g.
165+
'report-node' will expand to 'reports/report-node.xml'. A HTTP 400 will be returned if the report name is not valid.`,
166+
Args: func(cmd *cobra.Command, args []string) error {
167+
if len(args) != 1 {
168+
displayErrorAndExit(cmd, "you must provide a report name")
169+
}
170+
return nil
171+
},
172+
RunE: func(cmd *cobra.Command, args []string) error {
173+
var (
174+
jsonData []byte
175+
err error
176+
dataFetcher fetcher.Fetcher
177+
found = false
178+
)
179+
180+
// retrieve the current context or the value from "-c"
181+
_, dataFetcher, err = GetConnectionAndDataFetcher()
182+
if err != nil {
183+
return err
184+
}
185+
186+
// validate the nodeID
187+
nodeIDArray, err := GetClusterNodeIDs(dataFetcher)
188+
if err != nil {
189+
return err
190+
}
191+
for _, v := range nodeIDArray {
192+
i, _ := strconv.Atoi(v)
193+
if i == reporterNodID {
194+
found = true
195+
}
196+
}
197+
if !found {
198+
return fmt.Errorf("unable to find node id %v", reporterNodID)
199+
}
200+
201+
jsonData, err = dataFetcher.RunReportJSON(args[0], reporterNodID)
202+
if err != nil {
203+
return err
204+
}
205+
206+
// output format cannot be table
207+
if OutputFormat == constants.TABLE {
208+
OutputFormat = constants.JSON
209+
}
210+
211+
if strings.Contains(OutputFormat, constants.JSONPATH) {
212+
jsonPathResult, err := utils.GetJSONPathResults(jsonData, OutputFormat)
213+
if err != nil {
214+
return err
215+
}
216+
cmd.Println(jsonPathResult)
217+
return nil
218+
} else if OutputFormat == constants.JSON {
219+
cmd.Println(string(jsonData))
220+
}
221+
222+
return nil
223+
},
224+
}
225+
158226
// startReporterCmd represents the start reporter command.
159227
var startReporterCmd = &cobra.Command{
160228
Use: reporterUse,
@@ -309,4 +377,7 @@ func init() {
309377
_ = setReporterCmd.MarkFlagRequired("attribute")
310378
setReporterCmd.Flags().StringVarP(&reporterAttributeValue, "value", "v", "", "attribute value to set")
311379
_ = setReporterCmd.MarkFlagRequired("value")
380+
381+
runReportCmd.Flags().IntVarP(&reporterNodID, "node", "n", 0, "node to run report on")
382+
_ = runReportCmd.MarkFlagRequired("node")
312383
}

pkg/cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const (
9191
machineMessage = "the machine name to use"
9292
rackMessage = "the rack name to use"
9393
siteMessage = "the site name to use"
94+
roleMessage = "the role name to use"
9495
cacheConfigMessage = "cache configuration file"
9596
operationalConfigMessage = "override override file"
9697
cacheConfigArg = "cache-config"
@@ -103,6 +104,7 @@ const (
103104
machineArg = "machine"
104105
rackArg = "rack"
105106
siteArg = "site"
107+
roleArg = "role"
106108
logLevelMessage = "coherence log level"
107109
profileMessage = "profile to add to cluster startup command line"
108110
backupLogFilesMessage = "backup old cache server log files"
@@ -570,6 +572,10 @@ func Initialize(command *cobra.Command) *cobra.Command {
570572
setCmd.AddCommand(setFederationCmd)
571573
setCmd.AddCommand(setColorCmd)
572574

575+
// run command
576+
command.AddCommand(runCmd)
577+
runCmd.AddCommand(runReportCmd)
578+
573579
// clear
574580
command.AddCommand(clearCmd)
575581
clearCmd.AddCommand(clearContextCmd)

pkg/cmd/run.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at
4+
* https://oss.oracle.com/licenses/upl.
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// runCmd represents the run.
14+
var runCmd = &cobra.Command{
15+
Use: "run",
16+
Short: "run a report",
17+
Long: `The 'run' command runs reports`,
18+
}

pkg/fetcher/fetcher.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ type Fetcher interface {
223223
// GetReporterJSON returns reporter for a node in raw json.
224224
GetReporterJSON(nodeID string) ([]byte, error)
225225

226+
// RunReportJSON runs a specified report.
227+
RunReportJSON(report string, nodeID int) ([]byte, error)
228+
226229
// StartReporter starts the reporter on a member.
227230
StartReporter(nodeID string) error
228231

pkg/fetcher/http_fetcher.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,15 @@ func (h HTTPFetcher) GetReporterJSON(nodeID string) ([]byte, error) {
764764
return result, nil
765765
}
766766

767+
// RunReportJSON runs a specified report.
768+
func (h HTTPFetcher) RunReportJSON(report string, nodeID int) ([]byte, error) {
769+
result, err := httpGetRequest(h, fmt.Sprintf("%s%d/runReport/%s%s", reportersPath, nodeID, report, links))
770+
if err != nil {
771+
return constants.EmptyByte, utils.GetError(fmt.Sprintf("cannot run report %v on node %v", report, nodeID), err)
772+
}
773+
return result, nil
774+
}
775+
767776
// StartReporter starts the reporter on a member.
768777
func (h HTTPFetcher) StartReporter(nodeID string) error {
769778
_, err := issueReporterCommand(h, nodeID, "start")

pkg/utils/utils.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ func IsValidInt(value string) bool {
162162

163163
// SanitizeSnapshotName sanitizes a snapshot name by replacing any unwanted characters with '-'.
164164
func SanitizeSnapshotName(snapshotName string) string {
165-
var (
166-
sb = strings.Builder{}
167-
)
165+
var sb = strings.Builder{}
168166

169167
for _, c := range []byte(snapshotName) {
170168
r := rune(c)

scripts/generate-doc-snippets.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ create_doc $DOCS_DIR/describe_reporter "${COHCTL} describe reporter --help"
170170
create_doc $DOCS_DIR/start_reporter "${COHCTL} start reporter --help"
171171
create_doc $DOCS_DIR/stop_reporter "${COHCTL} stop reporter --help"
172172
create_doc $DOCS_DIR/set_reporter "${COHCTL} set reporter --help"
173+
create_doc $DOCS_DIR/run_report "${COHCTL} run report --help"
173174

174175
# JFRs
175176
create_doc $DOCS_DIR/get_jfrs "${COHCTL} get jfrs --help"

0 commit comments

Comments
 (0)