/
service.go
1216 lines (1043 loc) · 34.6 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/constants"
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"strconv"
"strings"
"sync"
"time"
)
var (
serviceType string
statusHATimeout int32
statusHAType string
validServiceTypes = []string{constants.DistributedService, constants.FederatedService, constants.PagedTopic,
"Invocation", "Proxy", "RemoteCache", "ReplicatedCache", "OptimisticCache", "LocalCache", "RemoteGrpcCache", all}
validStatusHA = []string{"NODE-SAFE", "MACHINE-SAFE", "RACK-SAFE", "SITE-SAFE"}
allStatusHA = []string{"ENDANGERED", "NODE-SAFE", "MACHINE-SAFE", "RACK-SAFE", "SITE-SAFE"}
attributeNameService string
attributeValueService string
validAttributesService = []string{"threadCount", "threadCountMin", "threadCountMax",
"taskHungThresholdMillis", "requestTimeoutMillis"}
nodeIDService string
nodeIDServiceOperation string
excludeStorageDisabled bool
includeBacklogOnly bool
)
const (
serviceUse = "service service-name"
provideServiceName = "you must provide a service name"
unableToFindService = "unable to find service with service name '%s'"
noDistributionsData = "No distributions data is available"
)
// getServicesCmd represents the get services command.
var getServicesCmd = &cobra.Command{
Use: "services",
Short: "display services for a cluster",
Long: `The 'get services' command displays services for a cluster using various options.
You may specify the service type as well a status-ha value to wait for. You
can also specify '-o wide' to display addition information.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
statusHAValues []string
dataFetcher fetcher.Fetcher
connection string
)
if statusHAType != "none" {
if !isWatchEnabled() {
return errors.New("if you have specified a status-ha value then you must enable watch option")
}
if !utils.SliceContains(validStatusHA, statusHAType) {
return fmt.Errorf("the list of Status-HA values must be one of %v", validStatusHA)
}
}
// check service type
if serviceType != "" && !utils.SliceContains(validServiceTypes, serviceType) {
return fmt.Errorf("invalid service type of '%s' specified. \nValid types are %v", serviceType, validServiceTypes)
}
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
startTime := time.Now()
for {
var servicesSummary = config.ServicesSummaries{}
servicesResult, err := dataFetcher.GetServiceDetailsJSON()
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(servicesResult, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
} else if OutputFormat == constants.JSON {
cmd.Println(string(servicesResult))
} else {
err = json.Unmarshal(servicesResult, &servicesSummary)
if err != nil {
return utils.GetError("unable to unmarshall service result", err)
}
deDuplicatedServices := DeduplicateServices(servicesSummary, serviceType)
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(FormatServices(deDuplicatedServices))
// collect all the statusHA values
statusHAValues = make([]string, 0)
for _, value := range deDuplicatedServices {
// ignore n/a
if value.StatusHA != "n/a" && !utils.SliceContains(statusHAValues, value.StatusHA) {
statusHAValues = append(statusHAValues, value.StatusHA)
}
}
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// if we have specified a statusHA value to wait for then process this
if statusHAType != "none" {
elapsedSeconds := int32(time.Since(startTime).Seconds())
if len(statusHAValues) == 1 && isStatusHASaferThan(statusHAValues[0], statusHAType) {
cmd.Printf("Status HA value of %s or better reached in %d seconds for all service types of %s\n",
statusHAType, elapsedSeconds, serviceType)
return nil
}
if elapsedSeconds > statusHATimeout {
return fmt.Errorf("status HA value of %s or better NOT reached in %d seconds for all service types of %s",
statusHAType, elapsedSeconds, serviceType)
}
cmd.Printf("Waiting for Status HA value %s or better for all service type %s within %d seconds",
statusHAType, serviceType, statusHATimeout)
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// getServiceStorageCmd represents the get service-storage command.
var getServiceStorageCmd = &cobra.Command{
Use: "service-storage",
Short: "display partitioned services storage information for a cluster",
Long: `The 'get service-storage' command displays partitioned services storage for a cluster including
information regarding partition sizes.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
jsonData []byte
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
for {
storageSummary, err := getServiceStorageDetails(dataFetcher)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
// serialize the data to JSON
jsonData, err = json.Marshal(storageSummary)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(jsonData, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
} else {
cmd.Println(string(jsonData))
}
} else {
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(FormatServicesStorage(storageSummary))
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
func getServiceStorageDetails(dataFetcher fetcher.Fetcher) ([]config.ServiceStorageSummary, error) {
var (
servicesSummary = config.ServicesSummaries{}
serviceList = make([]string, 0)
errorSink = createErrorSink()
wg sync.WaitGroup
m = sync.RWMutex{}
)
// get the list of services
servicesResult, err := dataFetcher.GetServiceDetailsJSON()
if err != nil {
return nil, err
}
err = json.Unmarshal(servicesResult, &servicesSummary)
if err != nil {
return nil, err
}
// get the list of partitioned services
for _, v := range servicesSummary.Services {
if utils.IsDistributedCache(v.ServiceType) {
if !utils.SliceContains(serviceList, v.ServiceName) {
serviceList = append(serviceList, v.ServiceName)
}
}
}
if len(serviceList) == 0 {
return nil, errors.New("unable to find any partitioned services")
}
// now we have partitioned services, retrieve the data for each service
wg.Add(len(serviceList))
storageSummary := make([]config.ServiceStorageSummary, 0)
for _, service := range serviceList {
go func(serviceName string) {
var data = config.ServiceStorageSummary{}
defer wg.Done()
partitionsData, err1 := dataFetcher.GetServicePartitionsJSON(serviceName)
if err1 != nil {
errorSink.AppendError(err1)
}
err1 = json.Unmarshal(partitionsData, &data)
if err1 != nil {
errorSink.AppendError(utils.GetError("unable to unmarshall storage data", err1))
return
}
// protect the slice for update
m.Lock()
defer m.Unlock()
storageSummary = append(storageSummary, data)
}(service)
}
wg.Wait()
errorList := errorSink.GetErrors()
if len(errorList) != 0 {
return nil, utils.GetErrors(errorList)
}
return storageSummary, nil
}
// getServiceDistributionsCmd represents the get service-distributions command.
var getServiceDistributionsCmd = &cobra.Command{
Use: "service-distributions service-name",
Short: "display partition distributions information for a service",
Long: `The 'get service-distributions' command displays partition distributions for a service.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
ValidArgsFunction: completionDistributedService,
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
servicesResult, err := GetDistributedServices(dataFetcher)
if err != nil {
return err
}
if !utils.SliceContains(servicesResult, args[0]) {
return fmt.Errorf(unableToFindService, args[0])
}
for {
var (
distributionsData []byte
distributions config.Distributions
)
distributionsData, err = dataFetcher.GetScheduledDistributionsJSON(args[0])
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(distributionsData, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
} else {
cmd.Println(string(distributionsData))
}
} else {
printWatchHeader(cmd)
if len(distributionsData) != 0 {
err = json.Unmarshal(distributionsData, &distributions)
if err != nil {
return err
}
} else {
distributions.ScheduledDistributions = noDistributionsData
}
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(distributions.ScheduledDistributions)
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// getServiceDescriptionCmd represents the get service-description command.
var getServiceDescriptionCmd = &cobra.Command{
Use: "service-description service-name",
Short: "display description for a service",
Long: `The 'get service-description' command displays information regarding a service and it's members.
Only available in most recent Coherence versions.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
ValidArgsFunction: completionService,
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
servicesSummary = config.ServicesSummaries{}
serviceResult []byte
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
serviceResult, err = dataFetcher.GetServiceDetailsJSON()
if err != nil {
return err
}
err = json.Unmarshal(serviceResult, &servicesSummary)
if err != nil {
return err
}
if found := serviceExists(args[0], servicesSummary); !found {
return fmt.Errorf(unableToFindService, args[0])
}
for {
var (
descriptionData []byte
description config.Description
)
descriptionData, err = dataFetcher.GetServiceDescriptionJSON(args[0])
if err != nil {
return err
}
if len(descriptionData) != 0 {
err = json.Unmarshal(descriptionData, &description)
if err != nil {
return err
}
} else {
return nil
}
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(description.Description)
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// getServiceMembersCmd represents the get service-members command.
var getServiceMembersCmd = &cobra.Command{
Use: "service-members service-name",
Short: "display service members for a cluster",
Long: `The 'get service-members' command displays service members for a cluster.`,
ValidArgsFunction: completionService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
serviceResult []byte
connection string
servicesSummary = config.ServicesSummaries{}
)
serviceName := args[0]
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
serviceResult, err = dataFetcher.GetServiceDetailsJSON()
if err != nil {
return err
}
err = json.Unmarshal(serviceResult, &servicesSummary)
if err != nil {
return err
}
found := serviceExists(serviceName, servicesSummary)
if !found {
return fmt.Errorf(unableToFindService, serviceName)
}
for {
var (
membersResult []byte
membersDetails = config.ServiceMemberDetails{}
)
membersResult, err = dataFetcher.GetServiceMembersDetailsJSON(serviceName)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(membersResult, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
return nil
}
cmd.Println(string(membersResult))
} else {
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
err = json.Unmarshal(membersResult, &membersDetails)
if err != nil {
return utils.GetError("unable to unmarshall members result", err)
}
var finalDetails []config.ServiceMemberDetail
if excludeStorageDisabled {
// remove any entries where idle threads == -1 which indicates client
var newMemberDetails = make([]config.ServiceMemberDetail, 0)
for _, v := range membersDetails.Services {
if v.OwnedPartitionsPrimary > 0 {
newMemberDetails = append(newMemberDetails, v)
}
}
finalDetails = newMemberDetails
} else if includeBacklogOnly {
// Only include members with backlog > 0
var newMemberDetails = make([]config.ServiceMemberDetail, 0)
for _, v := range membersDetails.Services {
if v.TaskBacklog > 0 {
newMemberDetails = append(newMemberDetails, v)
}
}
finalDetails = newMemberDetails
} else {
finalDetails = membersDetails.Services
}
cmd.Println("Service: " + serviceName + "\n")
cmd.Println(FormatServiceMembers(finalDetails))
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching services so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// describeService represents the describe service command.
var describeServiceCmd = &cobra.Command{
Use: serviceUse,
Short: "describe a service",
Long: `The 'describe service' command shows information related to services. This
includes information about each service member as well as Persistence information if the
service is a cache service.`,
ValidArgsFunction: completionService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
servicesSummary = config.ServicesSummaries{}
membersDetails = config.ServiceMemberDetails{}
persistenceDetails = config.ServicesSummaries{}
proxiesSummary = config.ProxiesSummary{}
coordinator = config.PersistenceCoordinator{}
serviceResult []byte
membersResult []byte
proxyResults []byte
coordData []byte
distributionsData []byte
partitionsData []byte
err error
dataFetcher fetcher.Fetcher
connection string
errorSink = createErrorSink()
wg sync.WaitGroup
)
serviceName := args[0]
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
serviceResult, err = dataFetcher.GetServiceDetailsJSON()
if err != nil {
return err
}
err = json.Unmarshal(serviceResult, &servicesSummary)
if err != nil {
return err
}
if found := serviceExists(serviceName, servicesSummary); !found {
return fmt.Errorf(unableToFindService, serviceName)
}
// we have valid service name so issue queries in parallel
wg.Add(5)
go func() {
defer wg.Done()
var err1 error
serviceResult, err1 = dataFetcher.GetSingleServiceDetailsJSON(serviceName)
if err1 != nil {
errorSink.AppendError(err1)
}
}()
go func() {
defer wg.Done()
var err1 error
membersResult, err1 = dataFetcher.GetServiceMembersDetailsJSON(serviceName)
if err1 != nil {
errorSink.AppendError(err1)
}
}()
go func() {
defer wg.Done()
var err1 error
proxyResults, err1 = dataFetcher.GetProxySummaryJSON()
if err1 != nil {
errorSink.AppendError(err1)
}
}()
go func() {
defer wg.Done()
var err1 error
partitionsData, err = dataFetcher.GetServicePartitionsJSON(serviceName)
if err1 != nil {
errorSink.AppendError(err1)
}
}()
go func() {
defer wg.Done()
var err1 error
distributionsData, err1 = dataFetcher.GetScheduledDistributionsJSON(serviceName)
if err1 != nil {
errorSink.AppendError(err1)
}
}()
// wait for all data fetchers requests to complete
wg.Wait()
errorList := errorSink.GetErrors()
// check if any of the requests returned errors
if len(errorList) > 0 {
return utils.GetErrors(errorList)
}
if isJSONPathOrJSON() {
finalResult, err := utils.CombineByteArraysForJSON([][]byte{serviceResult, proxyResults,
membersResult, proxyResults, distributionsData},
[]string{"services", "proxies", "members", "partitions", "distribution"})
if err != nil {
return err
}
if err = processJSONOutput(cmd, finalResult); err != nil {
return err
}
} else {
var sb strings.Builder
sb.WriteString(FormatCurrentCluster(connection))
sb.WriteString("\nSERVICE DETAILS\n")
sb.WriteString("---------------\n")
value, err := FormatJSONForDescribe(serviceResult, true, "Name", "Type")
if err != nil {
return err
}
sb.WriteString(value)
sb.WriteString("\nSERVICE MEMBERS\n")
sb.WriteString("---------------\n")
err = json.Unmarshal(membersResult, &membersDetails)
if err != nil {
return utils.GetError("unable to unmarshall members result", err)
}
err = json.Unmarshal(membersResult, &persistenceDetails)
if err != nil {
return utils.GetError("unable to unmarshall members persistence result", err)
}
value = FormatServiceMembers(membersDetails.Services)
sb.WriteString(value)
if len(proxyResults) != 0 {
err = json.Unmarshal(proxyResults, &proxiesSummary)
if err != nil {
return utils.GetError("unable to unmarshall proxy result", err)
}
}
// filter out any proxy servers that are not for this service
hasProxies := false
hasHTTPServers := false
filteredProxies := make([]config.ProxySummary, 0)
for _, value := range proxiesSummary.Proxies {
if value.ServiceName == serviceName {
if value.Protocol == "tcp" {
hasProxies = true
}
if value.Protocol == "http" {
hasHTTPServers = true
}
filteredProxies = append(filteredProxies, value)
}
}
if !hasProxies && !hasHTTPServers {
sb.WriteString("\nSERVICE CACHES\n")
sb.WriteString("--------------\n")
serviceList := make([]string, 1)
serviceList[0] = serviceName
value, err = formatCachesSummary(serviceList, dataFetcher)
if err != nil {
return err
}
sb.WriteString(value)
sb.WriteString("\nPERSISTENCE FOR SERVICE\n")
sb.WriteString("-----------------------\n")
value = FormatPersistenceServices(persistenceDetails.Services, false)
sb.WriteString(value)
coordData, err = dataFetcher.GetPersistenceCoordinator(serviceName)
if err != nil {
return err
}
if len(coordData) > 0 {
err = json.Unmarshal(coordData, &coordinator)
if err != nil {
return err
}
}
value, err = FormatJSONForDescribe(coordData, false,
"Coordinator Id", "Idle", "Operation Status", "Snapshots")
if err != nil {
return err
}
sb.WriteString("\nPERSISTENCE COORDINATOR\n")
sb.WriteString("-----------------------\n")
sb.WriteString(value)
if len(distributionsData) != 0 {
value, err = FormatJSONForDescribe(distributionsData, true)
if err != nil {
return err
}
} else {
value = noDistributionsData
}
if value != "" {
sb.WriteString("\nDISTRIBUTION INFORMATION\n")
sb.WriteString("------------------------\n")
sb.WriteString(value)
}
if string(partitionsData) != "" {
value, err = FormatJSONForDescribe(partitionsData, true,
"Service", "Strategy Name")
if err != nil {
return err
}
} else {
value = ""
}
if value != "" {
sb.WriteString("\nPARTITION INFORMATION\n")
sb.WriteString("---------------------\n")
sb.WriteString(value)
}
}
if hasProxies {
sb.WriteString("\nSERVICE PROXY SERVERS\n")
sb.WriteString("---------------------\n")
sb.WriteString(FormatProxyServers(filteredProxies, "tcp"))
}
if hasHTTPServers {
sb.WriteString("\nSERVICE HTTP SERVERS\n")
sb.WriteString("--------------------\n")
sb.WriteString(FormatProxyServers(filteredProxies, "http"))
}
cmd.Println(sb.String())
}
return nil
},
}
// setServiceCmd represents the set service command.
var setServiceCmd = &cobra.Command{
Use: "service service-name",
Short: "set a service attribute across one or more members",
Long: `The 'set service' command sets an attribute for a service across one or member nodes.
The following attribute names are allowed: threadCount, threadCountMin, threadCountMax or
taskHungThresholdMillis or requestTimeoutMillis.`,
ValidArgsFunction: completionService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
servicesSummary = config.ServicesSummaries{}
serviceResult []byte
err error
dataFetcher fetcher.Fetcher
connection string
nodeIDArray []string
nodeIds []string
confirmMessage string
intValue int
errorSink = createErrorSink()
wg sync.WaitGroup
)
serviceName := args[0]
// validate the attribute name
if !utils.SliceContains(validAttributesService, attributeNameService) {
return fmt.Errorf("attribute name %s is invalid. Please choose one of\n%v",
attributeNameService, validAttributesService)
}
// validate the attribute value
intValue, err = strconv.Atoi(attributeValueService)
if err != nil {
return fmt.Errorf("invalid integer value of %s for attribute %s", attributeValueService, attributeNameService)
}
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
serviceResult, err = dataFetcher.GetServiceDetailsJSON()
if err != nil {
return err
}
err = json.Unmarshal(serviceResult, &servicesSummary)
if err != nil {
return err
}
found := false
for _, v := range servicesSummary.Services {
if v.ServiceName == serviceName {
found = true
break
}
}
if !found {
return fmt.Errorf(unableToFindService, serviceName)
}
// validate the nodes
nodeIDArray, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
if nodeIDService == all {
nodeIds = append(nodeIds, nodeIDArray...)
confirmMessage = fmt.Sprintf("all %d nodes", len(nodeIds))
} else {
if nodeIds, err = getNodeIDs(nodeIDService, nodeIDArray); err != nil {
return err
}
confirmMessage = fmt.Sprintf("%d node(s)", len(nodeIds))
}
cmd.Println(FormatCurrentCluster(connection))
cmd.Printf("Selected service: %s\n", serviceName)
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to set the value of attribute %s to %s for %s? (y/n) ",
attributeNameService, attributeValueService, confirmMessage)) {
return nil
}
wg.Add(len(nodeIds))
for _, value := range nodeIds {
go func(nodeId string) {
var err1 error
defer wg.Done()
_, err1 = dataFetcher.SetServiceAttribute(nodeId, serviceName, attributeNameService, intValue)
if err1 != nil {
errorSink.AppendError(err1)
}
}(value)
}
wg.Wait()
errorList := errorSink.GetErrors()
if len(errorList) > 0 {
return utils.GetErrors(errorList)
}
cmd.Println(OperationCompleted)
return nil
},
}
// suspendServiceCmd represents the suspend service command.
var suspendServiceCmd = &cobra.Command{
Use: serviceUse,
Short: "suspend a service",
Long: `The 'suspend service' command suspends a specific service in all the members of a cluster.`,
ValidArgsFunction: completionPersistenceService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return issueServiceCommand(cmd, args[0], fetcher.SuspendService)
},
}
// resumeServiceCmd represents the resume service command.
var resumeServiceCmd = &cobra.Command{
Use: serviceUse,
Short: "resume a service",
Long: `The 'resume service' command resumes a specific service in all the members of a cluster.`,
ValidArgsFunction: completionPersistenceService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return issueServiceCommand(cmd, args[0], fetcher.ResumeService)
},
}
// stopServiceCmd represents the stop service command.
var stopServiceCmd = &cobra.Command{
Use: serviceUse,
Short: "stop a service",
Long: `The 'stop service' command forces a specific service to stop on a cluster member.
Use the shutdown service command for normal service termination.`,
ValidArgsFunction: completionPersistenceService,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideServiceName)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return issueServiceNodeCommand(cmd, args[0], fetcher.StopService)
},
}
// startServiceCmd represents the start service command.
var startServiceCmd = &cobra.Command{