-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added facility for tracking and exposing basic database-level stats #58
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package metrics | ||
|
||
import ( | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
"github.com/getlantern/wal" | ||
) | ||
|
||
var ( | ||
leaderStats *LeaderStats | ||
followerStats map[int]*FollowerStats | ||
partitionStats map[int]*PartitionStats | ||
|
||
mx sync.RWMutex | ||
) | ||
|
||
func init() { | ||
reset() | ||
} | ||
|
||
func reset() { | ||
leaderStats = &LeaderStats{} | ||
followerStats = make(map[int]*FollowerStats, 0) | ||
partitionStats = make(map[int]*PartitionStats, 0) | ||
} | ||
|
||
// Stats are the overall stats | ||
type Stats struct { | ||
Leader *LeaderStats | ||
Followers sortedFollowerStats | ||
Partitions sortedPartitionStats | ||
} | ||
|
||
// LeaderStats provides stats for the cluster leader | ||
type LeaderStats struct { | ||
ConnectedFollowers int | ||
CurrentlyReadingWAL time.Time | ||
} | ||
|
||
// FollowerStats provides stats for a single follower | ||
type FollowerStats struct { | ||
followerId int | ||
Partition int | ||
Queued int | ||
} | ||
|
||
// PartitionStats provides stats for a single partition | ||
type PartitionStats struct { | ||
Partition int | ||
NumFollowers int | ||
} | ||
|
||
type sortedFollowerStats []*FollowerStats | ||
|
||
func (s sortedFollowerStats) Len() int { return len(s) } | ||
func (s sortedFollowerStats) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s sortedFollowerStats) Less(i, j int) bool { | ||
if s[i].Partition < s[j].Partition { | ||
return true | ||
} | ||
return s[i].followerId < s[j].followerId | ||
} | ||
|
||
type sortedPartitionStats []*PartitionStats | ||
|
||
func (s sortedPartitionStats) Len() int { return len(s) } | ||
func (s sortedPartitionStats) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s sortedPartitionStats) Less(i, j int) bool { | ||
return s[i].Partition < s[j].Partition | ||
} | ||
|
||
func CurrentlyReadingWAL(offset wal.Offset) { | ||
ts := offset.TS() | ||
mx.Lock() | ||
leaderStats.CurrentlyReadingWAL = ts | ||
mx.Unlock() | ||
} | ||
|
||
// FollowerJoined records the fact that a follower joined the leader | ||
func FollowerJoined(followerID int, partition int) { | ||
mx.Lock() | ||
defer mx.Unlock() | ||
leaderStats.ConnectedFollowers++ | ||
followerStats[followerID] = &FollowerStats{ | ||
followerId: followerID, | ||
Partition: partition, | ||
Queued: 0, | ||
} | ||
ps := partitionStats[partition] | ||
if ps == nil { | ||
ps = &PartitionStats{Partition: partition} | ||
partitionStats[partition] = ps | ||
} | ||
ps.NumFollowers++ | ||
} | ||
|
||
// FollowerFailed records the fact that a follower failed (which is analogous to leaving) | ||
func FollowerFailed(followerID int) { | ||
mx.Lock() | ||
defer mx.Unlock() | ||
// Only delete once | ||
fs, found := followerStats[followerID] | ||
if found { | ||
leaderStats.ConnectedFollowers-- | ||
delete(followerStats, followerID) | ||
partitionStats[fs.Partition].NumFollowers-- | ||
} | ||
} | ||
|
||
// QueuedForFollower records how many measurements are queued for a given Follower | ||
func QueuedForFollower(followerID int, queued int) { | ||
mx.Lock() | ||
defer mx.Unlock() | ||
followerStats[followerID].Queued = queued | ||
} | ||
|
||
func GetStats() *Stats { | ||
mx.RLock() | ||
s := &Stats{ | ||
Leader: leaderStats, | ||
Followers: make(sortedFollowerStats, 0, len(followerStats)), | ||
Partitions: make(sortedPartitionStats, 0, len(partitionStats)), | ||
} | ||
|
||
for _, fs := range followerStats { | ||
s.Followers = append(s.Followers, fs) | ||
} | ||
for _, ps := range partitionStats { | ||
s.Partitions = append(s.Partitions, ps) | ||
} | ||
mx.RUnlock() | ||
|
||
sort.Sort(s.Followers) | ||
sort.Sort(s.Partitions) | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/getlantern/wal" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMetrics(t *testing.T) { | ||
reset() | ||
|
||
ts := time.Now() | ||
FollowerJoined(1, 1) | ||
FollowerJoined(2, 1) | ||
FollowerJoined(3, 2) | ||
FollowerJoined(4, 2) | ||
CurrentlyReadingWAL(wal.NewOffsetForTS(ts)) | ||
QueuedForFollower(1, 11) | ||
QueuedForFollower(2, 22) | ||
QueuedForFollower(3, 33) | ||
QueuedForFollower(4, 44) | ||
|
||
s := GetStats() | ||
assert.Equal(t, 4, s.Leader.ConnectedFollowers) | ||
assert.Equal(t, ts.Format(time.RFC3339), s.Leader.CurrentlyReadingWAL.Format(time.RFC3339)) | ||
|
||
assert.Equal(t, 1, s.Followers[0].Partition) | ||
assert.Equal(t, 11, s.Followers[0].Queued) | ||
assert.Equal(t, 1, s.Followers[1].Partition) | ||
assert.Equal(t, 22, s.Followers[1].Queued) | ||
assert.Equal(t, 2, s.Followers[2].Partition) | ||
assert.Equal(t, 33, s.Followers[2].Queued) | ||
assert.Equal(t, 2, s.Followers[3].Partition) | ||
assert.Equal(t, 44, s.Followers[3].Queued) | ||
|
||
assert.Equal(t, 1, s.Partitions[0].Partition) | ||
assert.Equal(t, 2, s.Partitions[0].NumFollowers) | ||
assert.Equal(t, 2, s.Partitions[1].Partition) | ||
assert.Equal(t, 2, s.Partitions[1].NumFollowers) | ||
|
||
// Fail a couple of followers. Fail each twice to make sure we don't double- | ||
// subtract. | ||
FollowerFailed(2) | ||
FollowerFailed(2) | ||
FollowerFailed(3) | ||
FollowerFailed(3) | ||
|
||
s = GetStats() | ||
assert.Equal(t, 2, s.Leader.ConnectedFollowers) | ||
assert.Equal(t, 1, s.Followers[0].Partition) | ||
assert.Equal(t, 11, s.Followers[0].Queued) | ||
assert.Equal(t, 2, s.Followers[1].Partition) | ||
assert.Equal(t, 44, s.Followers[1].Queued) | ||
|
||
assert.Equal(t, 1, s.Partitions[0].Partition) | ||
assert.Equal(t, 1, s.Partitions[0].NumFollowers) | ||
assert.Equal(t, 2, s.Partitions[1].Partition) | ||
assert.Equal(t, 1, s.Partitions[1].NumFollowers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package web | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/getlantern/zenodb/metrics" | ||
) | ||
|
||
func (h *handler) metrics(resp http.ResponseWriter, req *http.Request) { | ||
if !h.authenticate(resp, req) { | ||
resp.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
json.NewEncoder(resp).Encode(metrics.GetStats()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks okay by eyeball -- but could
followerStats[followerID]
benil
here if it was in a race for the lock withFollowerFailed
andFollowerFailed
wins?It looks like
markFailed
can only be triggered from a read that starts strictly after processing that follower, but it's a bit subtle, so just wanted to check if that was the expectation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is the expectation. Great question!