Skip to content

Commit 2c433d3

Browse files
committed
Report observer status in cluster replicas for streams and consumers
This is to support nats-io/nats-server#4582. Signed-off-by: Neil Twigg <[email protected]>
1 parent 2cb1910 commit 2c433d3

File tree

6 files changed

+59
-52
lines changed

6 files changed

+59
-52
lines changed

cli/consumer_command.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,16 +637,7 @@ func (c *consumerCmd) showInfo(config api.ConsumerConfig, state api.ConsumerInfo
637637
cols.AddRow("Name", state.Cluster.Name)
638638
cols.AddRow("Leader", state.Cluster.Leader)
639639
for _, r := range state.Cluster.Replicas {
640-
since := fmt.Sprintf("seen %s ago", f(r.Active))
641-
if r.Active == 0 || r.Active == math.MaxInt64 {
642-
since = "not seen"
643-
}
644-
645-
if r.Current {
646-
cols.AddRowf("Replica", "%s, current, %s", r.Name, since)
647-
} else {
648-
cols.AddRowf("Replica", "%s, outdated, %s", r.Name, since)
649-
}
640+
cols.AddRow("Replica", replicaInfoFor(r))
650641
}
651642
}
652643

cli/kv_command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ func renderNatsGoClusterInfo(cols *columns.Writer, info *nats.StreamInfo) {
838838
state = append(state, "outdated")
839839
}
840840

841+
if r.Observer {
842+
state = append(state, "observer")
843+
}
844+
841845
if r.Offline {
842846
state = append(state, "OFFLINE")
843847
}

cli/stream_command.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,33 +1933,7 @@ func (c *streamCmd) showStreamInfo(info *api.StreamInfo) {
19331933
cols.AddRow("Name", info.Cluster.Name)
19341934
cols.AddRow("Leader", info.Cluster.Leader)
19351935
for _, r := range info.Cluster.Replicas {
1936-
state := []string{r.Name}
1937-
1938-
if r.Current {
1939-
state = append(state, "current")
1940-
} else {
1941-
state = append(state, "outdated")
1942-
}
1943-
1944-
if r.Offline {
1945-
state = append(state, "OFFLINE")
1946-
}
1947-
1948-
if r.Active > 0 && r.Active < math.MaxInt64 {
1949-
state = append(state, fmt.Sprintf("seen %s ago", f(r.Active)))
1950-
} else {
1951-
state = append(state, "not seen")
1952-
}
1953-
1954-
switch {
1955-
case r.Lag > 1:
1956-
state = append(state, fmt.Sprintf("%s operations behind", f(r.Lag)))
1957-
case r.Lag == 1:
1958-
state = append(state, fmt.Sprintf("%s operation behind", f(r.Lag)))
1959-
}
1960-
1961-
cols.AddRow("Replica", state)
1962-
1936+
cols.AddRow("Replica", replicaInfoFor(r))
19631937
}
19641938
cols.Println()
19651939
}

cli/util.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,9 @@ func renderCluster(cluster *api.ClusterInfo) string {
789789
// first we figure out leader and downs based on the full names and build
790790
// peers array which is a list of all the full names
791791
leader := -1
792-
warn := []int{}
793-
var peers []string
792+
peers := make([]string, 0, len(cluster.Replicas))
793+
warn := make([]int, 0, len(cluster.Replicas))
794+
obs := make([]int, 0, len(cluster.Replicas))
794795

795796
if cluster.Leader != "" {
796797
peers = append(peers, cluster.Leader)
@@ -805,7 +806,9 @@ func renderCluster(cluster *api.ClusterInfo) string {
805806
} else {
806807
warn = append(warn, i)
807808
}
808-
809+
}
810+
if r.Observer {
811+
obs = append(obs, i)
809812
}
810813
peers = append(peers, name)
811814
}
@@ -815,6 +818,9 @@ func renderCluster(cluster *api.ClusterInfo) string {
815818
if leader != -1 {
816819
compact[0] = compact[0] + "*"
817820
}
821+
for _, i := range obs {
822+
compact[i] = compact[i] + "^"
823+
}
818824
for _, i := range warn {
819825
compact[i] = compact[i] + "!"
820826
}
@@ -1360,3 +1366,36 @@ func structWithoutOmitEmpty(s any) any {
13601366

13611367
return res
13621368
}
1369+
1370+
func replicaInfoFor(r *api.PeerInfo) []string {
1371+
state := []string{r.Name}
1372+
1373+
if r.Current {
1374+
state = append(state, "current")
1375+
} else {
1376+
state = append(state, "outdated")
1377+
}
1378+
1379+
if r.Observer {
1380+
state = append(state, "observer")
1381+
}
1382+
1383+
if r.Offline {
1384+
state = append(state, "OFFLINE")
1385+
}
1386+
1387+
if r.Active > 0 && r.Active < math.MaxInt64 {
1388+
state = append(state, fmt.Sprintf("seen %s ago", f(r.Active)))
1389+
} else {
1390+
state = append(state, "not seen")
1391+
}
1392+
1393+
switch {
1394+
case r.Lag > 1:
1395+
state = append(state, fmt.Sprintf("%s operations behind", f(r.Lag)))
1396+
case r.Lag == 1:
1397+
state = append(state, fmt.Sprintf("%s operation behind", f(r.Lag)))
1398+
}
1399+
1400+
return state
1401+
}

go.mod

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ module github.com/nats-io/natscli
22

33
go 1.20
44

5+
replace github.com/nats-io/jsm.go => github.com/nats-io/jsm.go v0.1.1-0.20230926090317-6fb84b8ef01d
6+
7+
replace github.com/nats-io/nats.go => github.com/nats-io/nats.go v1.30.2-0.20230926090553-22cd46b4d288
8+
59
require (
610
github.com/AlecAivazis/survey/v2 v2.3.7
711
github.com/HdrHistogram/hdrhistogram-go v1.1.2
12+
github.com/antonmedv/expr v1.15.3
813
github.com/choria-io/fisk v0.6.0
914
github.com/dustin/go-humanize v1.0.1
1015
github.com/emicklei/dot v1.6.0
@@ -19,8 +24,10 @@ require (
1924
github.com/klauspost/compress v1.17.0
2025
github.com/mattn/go-isatty v0.0.19
2126
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0
27+
github.com/nats-io/jwt/v2 v2.5.2
2228
github.com/nats-io/nats-server/v2 v2.10.1
2329
github.com/nats-io/nats.go v1.30.0
30+
github.com/nats-io/nkeys v0.4.5
2431
github.com/nats-io/nuid v1.0.1
2532
github.com/prometheus/client_golang v1.16.0
2633
github.com/prometheus/common v0.44.0
@@ -32,7 +39,6 @@ require (
3239
)
3340

3441
require (
35-
github.com/antonmedv/expr v1.15.3 // indirect
3642
github.com/beorn7/perks v1.0.1 // indirect
3743
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3844
github.com/golang/protobuf v1.5.3 // indirect
@@ -42,12 +48,9 @@ require (
4248
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4349
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
4450
github.com/minio/highwayhash v1.0.2 // indirect
45-
github.com/nats-io/jwt/v2 v2.5.2 // indirect
46-
github.com/nats-io/nkeys v0.4.5 // indirect
4751
github.com/prometheus/client_model v0.4.0 // indirect
4852
github.com/prometheus/procfs v0.11.1 // indirect
4953
github.com/rivo/uniseg v0.4.4 // indirect
50-
github.com/sevlyar/retag v0.0.0-20190429052747-c3f10e304082 // indirect
5154
golang.org/x/net v0.15.0 // indirect
5255
golang.org/x/sys v0.12.0 // indirect
5356
golang.org/x/text v0.13.0 // indirect

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,14 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQ
8080
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
8181
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
8282
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
83-
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8 h1:OKm9e1//rlcl4i9zXQ6QQxj7DJaeL+Oe8WBgAKO4cqI=
84-
github.com/nats-io/jsm.go v0.1.1-0.20230921074448-1bbb5650afc8/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
85-
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0 h1:YrGcddIEq3vsWFO6JKGqHF1NquZCCKXJBJmp61tDRJY=
86-
github.com/nats-io/jsm.go v0.1.1-0.20230922064108-bb09405bc2c0/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
83+
github.com/nats-io/jsm.go v0.1.1-0.20230926090317-6fb84b8ef01d h1:jHy988Qmc1KRJjnyMoAHqMfZYtFFGhV8mh0EsMrh+DE=
84+
github.com/nats-io/jsm.go v0.1.1-0.20230926090317-6fb84b8ef01d/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk=
8785
github.com/nats-io/jwt/v2 v2.5.2 h1:DhGH+nKt+wIkDxM6qnVSKjokq5t59AZV5HRcFW0zJwU=
8886
github.com/nats-io/jwt/v2 v2.5.2/go.mod h1:24BeQtRwxRV8ruvC4CojXlx/WQ/VjuwlYiH+vu/+ibI=
8987
github.com/nats-io/nats-server/v2 v2.10.1 h1:MIJ614dhOIdo71iSzY8ln78miXwrYvlvXHUyS+XdKZQ=
9088
github.com/nats-io/nats-server/v2 v2.10.1/go.mod h1:3PMvMSu2cuK0J9YInRLWdFpFsswKKGUS77zVSAudRto=
91-
github.com/nats-io/nats.go v1.30.0 h1:bj/rVsRCrFXxmm9mJiDhb74UKl2HhKpDwKRBtvCjZjc=
92-
github.com/nats-io/nats.go v1.30.0/go.mod h1:dcfhUgmQNN4GJEfIb2f9R7Fow+gzBF4emzDHrVBd5qM=
89+
github.com/nats-io/nats.go v1.30.2-0.20230926090553-22cd46b4d288 h1:ZF1eeVI+Tkmd9Ks9QB+OXt/NLuPv+mXKxiu7nixTmVU=
90+
github.com/nats-io/nats.go v1.30.2-0.20230926090553-22cd46b4d288/go.mod h1:dcfhUgmQNN4GJEfIb2f9R7Fow+gzBF4emzDHrVBd5qM=
9391
github.com/nats-io/nkeys v0.4.5 h1:Zdz2BUlFm4fJlierwvGK+yl20IAKUm7eV6AAZXEhkPk=
9492
github.com/nats-io/nkeys v0.4.5/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5sNvt64=
9593
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
@@ -112,8 +110,6 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
112110
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
113111
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
114112
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
115-
github.com/sevlyar/retag v0.0.0-20190429052747-c3f10e304082 h1:fj05fHX+p6w6xqPfvEjFtdu95JwguF0Kg1cz/sht8+U=
116-
github.com/sevlyar/retag v0.0.0-20190429052747-c3f10e304082/go.mod h1:mOWh3Kdot9kBKCLbKcJTzIBBEPKRJAq2lk03eVVDmco=
117113
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
118114
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
119115
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

0 commit comments

Comments
 (0)