forked from buraksezer/olric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
100 lines (92 loc) · 2.63 KB
/
stats.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
// Copyright 2018-2020 Burak Sezer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package olric
import (
"os"
"runtime"
"github.com/buraksezer/olric/internal/protocol"
"github.com/buraksezer/olric/stats"
"github.com/vmihailenco/msgpack"
)
func (db *Olric) stats() stats.Stats {
mem := &runtime.MemStats{}
runtime.ReadMemStats(mem)
s := stats.Stats{
Cmdline: os.Args,
ReleaseVersion: ReleaseVersion,
ClusterCoordinator: db.discovery.GetCoordinator(),
Runtime: stats.Runtime{
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
Version: runtime.Version(),
NumCPU: runtime.NumCPU(),
NumGoroutine: runtime.NumGoroutine(),
MemStats: *mem,
},
Partitions: make(map[uint64]stats.Partition),
Backups: make(map[uint64]stats.Partition),
}
collect := func(partID uint64, part *partition) stats.Partition {
owners := part.loadOwners()
p := stats.Partition{
Backups: db.backups[partID].loadOwners(),
Length: part.length(),
DMaps: make(map[string]stats.DMap),
}
if !part.backup {
p.Owner = part.owner()
}
if len(owners) > 0 {
p.PreviousOwners = owners[:len(owners)-1]
}
part.m.Range(func(name, dm interface{}) bool {
dm.(*dmap).Lock()
tmp := stats.DMap{
Length: dm.(*dmap).storage.Len(),
NumTables: dm.(*dmap).storage.NumTables(),
SlabInfo: stats.SlabInfo(dm.(*dmap).storage.SlabInfo()),
}
p.DMaps[name.(string)] = tmp
dm.(*dmap).Unlock()
return true
})
return p
}
routingMtx.RLock()
for partID, part := range db.partitions {
s.Partitions[partID] = collect(partID, part)
}
for partID, part := range db.backups {
s.Backups[partID] = collect(partID, part)
}
routingMtx.RUnlock()
return s
}
func (db *Olric) statsOperation(w, _ protocol.EncodeDecoder) {
s := db.stats()
value, err := msgpack.Marshal(s)
if err != nil {
db.errorResponse(w, err)
return
}
w.SetStatus(protocol.StatusOK)
w.SetValue(value)
}
// Stats exposes some useful metrics to monitor an Olric node.
func (db *Olric) Stats() (stats.Stats, error) {
if err := db.isOperable(); err != nil {
return stats.Stats{}, err
}
return db.stats(), nil
}