Skip to content

Commit 4fe680c

Browse files
author
Tim Middleton
authored
Add roles and run report command (#237)
* Add roles and run report command * minor updates
1 parent e13cdd1 commit 4fe680c

File tree

13 files changed

+157
-12
lines changed

13 files changed

+157
-12
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/cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
back = "back"
4343
all = "all"
4444
partitionDisplayType = "partition"
45+
access = "access"
46+
storage = "storage"
4547
)
4648

4749
// getCachesCmd represents the get caches command.
@@ -452,7 +454,7 @@ You can specify '-o wide' to display addition information.`,
452454
return nil
453455
},
454456
RunE: func(cmd *cobra.Command, args []string) error {
455-
return getCacheDetails(cmd, args, "access")
457+
return getCacheDetails(cmd, args, access)
456458
},
457459
}
458460

@@ -470,7 +472,7 @@ You can specify '-o wide' to display addition information.`,
470472
return nil
471473
},
472474
RunE: func(cmd *cobra.Command, args []string) error {
473-
return getCacheDetails(cmd, args, "storage")
475+
return getCacheDetails(cmd, args, storage)
474476
},
475477
}
476478

@@ -660,11 +662,11 @@ func getCacheDetails(cmd *cobra.Command, args []string, displayType string) erro
660662
cmd.Printf("Cache: %s\n\n", args[0])
661663
}
662664

663-
if displayType == "access" {
665+
if displayType == access {
664666
cmd.Println(FormatCacheDetailsSizeAndAccess(cacheDetails.Details))
665667
} else if displayType == "index" {
666668
cmd.Println(FormatCacheIndexDetails(cacheDetails.Details))
667-
} else if displayType == "storage" {
669+
} else if displayType == storage {
668670
cmd.Println(FormatCacheDetailsStorage(cacheDetails.Details))
669671
} else if displayType == partitionDisplayType {
670672
cmd.Printf("Cache: %s\n", args[0])

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/formatting.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,9 @@ func FormatCacheDetailsStorage(cacheDetails []config.CacheDetail) string {
10171017
}
10181018

10191019
for _, value := range cacheDetails {
1020+
if value.Tier != "back" {
1021+
continue
1022+
}
10201023
var nodeID, _ = strconv.Atoi(value.NodeID)
10211024

10221025
table.AddRow(formatSmallInteger(int32(nodeID)), value.Tier,

pkg/cmd/monitor_cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,11 +959,11 @@ func getCacheContent(dataFetcher fetcher.Fetcher, displayType string) ([]string,
959959
return emptyStringArray, err
960960
}
961961

962-
if displayType == "access" {
962+
if displayType == access {
963963
sb.WriteString(FormatCacheDetailsSizeAndAccess(cacheDetails.Details))
964964
} else if displayType == "index" {
965965
sb.WriteString(FormatCacheIndexDetails(cacheDetails.Details))
966-
} else if displayType == "storage" {
966+
} else if displayType == storage {
967967
sb.WriteString(FormatCacheDetailsStorage(cacheDetails.Details))
968968
} else if displayType == partitionDisplayType {
969969
sb.WriteString(FormatCachePartitions(cachePartitionDetails.Details, cacheSummary))

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

0 commit comments

Comments
 (0)