This repository was archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
clean-up/rename storageMonitor -> spaceMon #330
Open
kirg
wants to merge
20
commits into
master
Choose a base branch
from
spaceMon
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c1fe33d
More logs for OpenAppendStream, OpenReadStream failures
e363e7d
rename/clean-up storageMonitor -> spaceMon
ad50a73
~
d864041
fix potential race around disableWriteC
87269bc
WriteDisableNotify
a21c672
fix lint
10cf0d2
Merge branch 'master' into spaceMon
3b8ca33
fix names
2954dd3
Merge branch 'spaceMon' of ssh://github.com/uber/cherami-server into …
6b01dc9
glide up
c22fe7e
glide up, fix build
446ec91
~
7c5cc0f
remove circular dependency
28ba21a
remove modes
245e701
binary prefixes
885c08c
~
751a0ec
Increase thresholds
6eece02
module tag
ba3c40a
ttt
1c60db7
ttt
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
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,179 @@ | ||
| // Copyright (c) 2016 Uber Technologies, Inc. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
|
|
||
| package storehost | ||
|
|
||
| import ( | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/uber-common/bark" | ||
| "github.com/uber/cherami-server/common" | ||
| "github.com/uber/cherami-server/common/metrics" | ||
| "github.com/uber/cherami-server/services/storehost/load" | ||
| "go.uber.org/atomic" | ||
| ) | ||
|
|
||
| var ( | ||
| // TODO: make these dynamically configurable! | ||
|
|
||
| // SpaceMonThresholdWarn if free-space below this, emit warning logs | ||
| SpaceMonThresholdWarn uint64 = 250 * GiB | ||
|
|
||
| // SpaceMonThresholdReadOnly if free-space below this, switch store to read-only | ||
| SpaceMonThresholdReadOnly uint64 = 200 * GiB | ||
|
|
||
| // SpaceMonThresholdResumeWrites if read-only and free-space exceeds this, then make read-write | ||
| SpaceMonThresholdResumeWrites uint64 = 275 * GiB | ||
|
|
||
| // SpaceMonInterval interval at which to monitor free-space and take action | ||
| SpaceMonInterval = 2 * time.Minute | ||
| ) | ||
|
|
||
| const ( | ||
| // KiB kibibytes | ||
| KiB = 1024 | ||
| // MiB mebibytes | ||
| MiB = 1024 * KiB | ||
| // GiB gibibytes | ||
| GiB = 1024 * MiB | ||
| // TiB tebibytes | ||
| TiB = 1024 * GiB | ||
| ) | ||
|
|
||
| type ( | ||
| // SpaceMon monitors free-space and switches stores to read-only on low-space | ||
| SpaceMon struct { | ||
| storeHost *StoreHost | ||
| logger bark.Logger | ||
| m3Client metrics.Client | ||
| hostMetrics *load.HostMetrics | ||
|
|
||
| stopCh chan struct{} | ||
| path string | ||
| readonly *atomic.Bool // read-only | ||
| } | ||
| ) | ||
|
|
||
| // NewSpaceMon returns an instance of SpaceMon. | ||
| func NewSpaceMon(store *StoreHost, m3Client metrics.Client, hostMetrics *load.HostMetrics, logger bark.Logger, path string) *SpaceMon { | ||
|
|
||
| return &SpaceMon{ | ||
| storeHost: store, | ||
| logger: logger, | ||
| m3Client: m3Client, | ||
| hostMetrics: hostMetrics, | ||
| path: path, | ||
| readonly: atomic.NewBool(false), // default: read-write | ||
| } | ||
| } | ||
|
|
||
| // Start starts the monitoring | ||
| func (s *SpaceMon) Start() { | ||
|
|
||
| s.stopCh = make(chan struct{}) | ||
| go s.pump() | ||
| s.storeHost.DisableReadonly() | ||
|
|
||
| s.logger.Info("SpaceMon: started") | ||
| } | ||
|
|
||
| // Stop stops the monitoring | ||
| func (s *SpaceMon) Stop() { | ||
|
|
||
| close(s.stopCh) | ||
| s.logger.Info("SpaceMon: stopped") | ||
| } | ||
|
|
||
| func (s *SpaceMon) pump() { | ||
|
|
||
| log := s.logger.WithField(`path`, s.path) | ||
|
|
||
| ticker := time.NewTicker(SpaceMonInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-s.stopCh: | ||
| return // done | ||
| case <-ticker.C: | ||
| // continue below to check free-space, etc | ||
| } | ||
|
|
||
| // query available/total space | ||
| var stat syscall.Statfs_t | ||
| err := syscall.Statfs(s.path, &stat) | ||
| if err != nil { | ||
| log.WithField(common.TagErr, err).Error("SpaceMon: syscall.Statfs failed") | ||
| continue | ||
| } | ||
|
|
||
| avail := stat.Bavail * uint64(stat.Bsize) | ||
| total := stat.Blocks * uint64(stat.Bsize) | ||
|
|
||
| if total <= 0 { | ||
| log.Error(`SpaceMon: total space unavailable`) | ||
| continue | ||
| } | ||
|
|
||
| availMiBs, totalMiBs := avail/MiB, total/MiB | ||
| availPercent := 100.0 * float64(avail) / float64(total) | ||
|
|
||
| s.hostMetrics.Set(load.HostMetricFreeDiskSpaceBytes, int64(avail)) | ||
| s.m3Client.UpdateGauge(metrics.SystemResourceScope, metrics.StorageDiskAvailableSpaceMB, int64(availMiBs)) | ||
|
|
||
| xlog := log.WithFields(bark.Fields{ | ||
| `avail`: availMiBs, | ||
| `total`: totalMiBs, | ||
| `percent`: availPercent, | ||
| }) | ||
|
|
||
| switch { | ||
| case s.readonly.Load(): // disable readonly, if above resume-writes threshold | ||
|
|
||
| // if below resume-writes threshold, do nothing | ||
| if avail < SpaceMonThresholdResumeWrites { | ||
| xlog.Warn(`SpaceMon: continuing in read-only`) | ||
| continue | ||
| } | ||
|
|
||
| // disable readonly, if we have recovered free-space | ||
| if s.readonly.CAS(true, false) { | ||
|
|
||
| xlog.Info("SpaceMon: disabling read-only") | ||
| s.storeHost.EnableReadonly() | ||
| } | ||
|
|
||
| case avail < SpaceMonThresholdReadOnly: // enable read-only, if below alert-threshold | ||
|
|
||
| if s.readonly.CAS(false, true) { | ||
|
|
||
| xlog.Error("SpaceMon: switching to read-only") | ||
| s.storeHost.DisableReadonly() | ||
| } | ||
|
|
||
| case avail < SpaceMonThresholdWarn: // warn, if below warn-threshold | ||
| xlog.Warn("SpaceMon: warning: running low on space") | ||
|
|
||
| default: | ||
| xlog.Debug("SpaceMon: monitoring") | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
@kirg Curious as to why do the thresholds need to be so high? Is this a uber use case or does cherami need the diskspace somehow?