lib/storage: code cleanup after 5bfd4e6218

This commit is contained in:
Aliaksandr Valialkin 2020-10-20 16:10:46 +03:00
parent 0db7c2b500
commit 9c5cd5a6c5
2 changed files with 15 additions and 14 deletions

View File

@ -1047,13 +1047,14 @@ func checkTimeRange(s *storage.Storage, tr storage.TimeRange) error {
if !*denyQueriesOutsideRetention { if !*denyQueriesOutsideRetention {
return nil return nil
} }
retentionPeriod := s.RetentionMonths() retentionMsecs := s.RetentionMsecs()
minAllowedTimestamp := (int64(fasttime.UnixTimestamp()) - int64(retentionPeriod)*3600*24*30) * 1000 minAllowedTimestamp := int64(fasttime.UnixTimestamp()*1000) - retentionMsecs
if tr.MinTimestamp > minAllowedTimestamp { if tr.MinTimestamp > minAllowedTimestamp {
return nil return nil
} }
return &httpserver.ErrorWithStatusCode{ return &httpserver.ErrorWithStatusCode{
Err: fmt.Errorf("the given time range %s is outside the allowed retention of %d months according to -denyQueriesOutsideRetention", &tr, retentionPeriod), Err: fmt.Errorf("the given time range %s is outside the allowed retention %.3f days according to -denyQueriesOutsideRetention",
&tr, float64(retentionMsecs)/(24*3600*1000)),
StatusCode: http.StatusServiceUnavailable, StatusCode: http.StatusServiceUnavailable,
} }
} }

View File

@ -50,9 +50,9 @@ type Storage struct {
slowPerDayIndexInserts uint64 slowPerDayIndexInserts uint64
slowMetricNameLoads uint64 slowMetricNameLoads uint64
path string path string
cachePath string cachePath string
retentionMonths int retentionMsecs int64
// lock file for exclusive access to the storage on the given path. // lock file for exclusive access to the storage on the given path.
flockF *os.File flockF *os.File
@ -129,11 +129,10 @@ func OpenStorage(path string, retentionMsecs int64) (*Storage, error) {
if retentionMsecs <= 0 { if retentionMsecs <= 0 {
retentionMsecs = maxRetentionMsecs retentionMsecs = maxRetentionMsecs
} }
retentionMonths := (retentionMsecs + (msecsPerMonth - 1)) / msecsPerMonth
s := &Storage{ s := &Storage{
path: path, path: path,
cachePath: path + "/cache", cachePath: path + "/cache",
retentionMonths: int(retentionMonths), retentionMsecs: retentionMsecs,
stop: make(chan struct{}), stop: make(chan struct{}),
} }
@ -202,9 +201,9 @@ func OpenStorage(path string, retentionMsecs int64) (*Storage, error) {
return s, nil return s, nil
} }
// RetentionMonths returns retention months for s. // RetentionMsecs returns retentionMsecs for s.
func (s *Storage) RetentionMonths() int { func (s *Storage) RetentionMsecs() int64 {
return s.retentionMonths return s.retentionMsecs
} }
// debugFlush flushes recently added storage data, so it becomes visible to search. // debugFlush flushes recently added storage data, so it becomes visible to search.
@ -488,8 +487,9 @@ func (s *Storage) startRetentionWatcher() {
} }
func (s *Storage) retentionWatcher() { func (s *Storage) retentionWatcher() {
retentionMonths := int((s.retentionMsecs + (msecsPerMonth - 1)) / msecsPerMonth)
for { for {
d := nextRetentionDuration(s.retentionMonths) d := nextRetentionDuration(retentionMonths)
select { select {
case <-s.stop: case <-s.stop:
return return