-
Notifications
You must be signed in to change notification settings - Fork 197
/
count_set_objects_using_request_info.go
104 lines (84 loc) · 2.21 KB
/
count_set_objects_using_request_info.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
as "github.com/aerospike/aerospike-client-go"
shared "github.com/aerospike/aerospike-client-go/examples/shared"
)
func main() {
runExample(shared.Client)
log.Println("Example finished successfully.")
}
func runExample(client *as.Client) {
replFactor, err := replicationFactor(client, *shared.Namespace)
shared.PanicOnError(err)
log.Println("Replication Factor:", replFactor)
totalObjects, err := countSetObjects(client, *shared.Namespace, *shared.Set)
shared.PanicOnError(err)
log.Println("Total Objects:", totalObjects)
totalUniqueObjects := totalObjects / replFactor
log.Println("Total Unique Object Count:", totalUniqueObjects)
}
func countSetObjects(client *as.Client, ns, set string) (int, error) {
const statKey = "objects"
// get the list of cluster nodes
nodes := client.GetNodes()
infop := as.NewInfoPolicy()
objCount := 0
// iterate over nodes
N:
for _, n := range nodes {
cmd := fmt.Sprintf("sets/%s/%s", ns, set)
info, err := n.RequestInfo(infop, cmd)
if err != nil {
return -1, err
}
vals := strings.Split(info[cmd], ":")
for _, val := range vals {
if i := strings.Index(val, statKey); i > -1 {
cnt, err := strconv.Atoi(val[i+len(statKey)+1:])
if err != nil {
return -1, err
}
objCount += cnt
continue N
}
}
}
return objCount, nil
}
func replicationFactor(client *as.Client, ns string) (int, error) {
const statKey = "effective_replication_factor"
// get the list of cluster nodes
nodes := client.GetNodes()
infop := as.NewInfoPolicy()
replFactor := -1
// iterate over nodes
N:
for _, n := range nodes {
cmd := fmt.Sprintf("namespace/%s", ns)
info, err := n.RequestInfo(infop, cmd)
if err != nil {
return -1, err
}
vals := strings.Split(info[cmd], ";")
// fmt.Println(vals)
for _, val := range vals {
if i := strings.Index(val, statKey); i > -1 {
rf, err := strconv.Atoi(val[i+len(statKey)+1:])
if err != nil {
return -1, err
}
if replFactor == -1 {
replFactor = rf
} else if replFactor != rf {
return -1, fmt.Errorf("Inconsistent replication factor for namespace %s in cluster", ns)
}
continue N
}
}
}
return replFactor, nil
}