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 {
return nil
}
retentionPeriod := s.RetentionMonths()
minAllowedTimestamp := (int64(fasttime.UnixTimestamp()) - int64(retentionPeriod)*3600*24*30) * 1000
retentionMsecs := s.RetentionMsecs()
minAllowedTimestamp := int64(fasttime.UnixTimestamp()*1000) - retentionMsecs
if tr.MinTimestamp > minAllowedTimestamp {
return nil
}
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,
}
}

View File

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