Refactor shard group time bound checking

This allows it to be tested.
pull/1781/head
Philip O'Toole 2015-02-27 13:34:35 -08:00
parent db91f0caeb
commit 05d630bfb8
4 changed files with 51 additions and 1 deletions

View File

@ -1,3 +1,9 @@
## v0.9.0-rc6 [Unrelease]
### Bugfixes
- [#1744](https://github.com/influxdb/influxdb/pull/1744): Select shard groups which completely encompass time range. Thanks @kylezh.
## v0.9.0-rc5 [2015-02-27] ## v0.9.0-rc5 [2015-02-27]
### Bugfixes ### Bugfixes

View File

@ -5,6 +5,7 @@ package influxdb
import ( import (
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/influxdb/influxdb/influxql" "github.com/influxdb/influxdb/influxql"
) )
@ -259,6 +260,42 @@ func Test_seriesIDs_reject(t *testing.T) {
} }
} }
// Test shard group selection.
func TestShardGroup_Contains(t *testing.T) {
// Make a shard group 1 hour in duration
g := newShardGroup()
g.StartTime, _ = time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
g.EndTime = g.StartTime.Add(time.Hour)
if !g.Contains(g.StartTime.Add(-time.Minute), g.EndTime) {
t.Fatal("shard group not selected when min before start time")
}
if !g.Contains(g.StartTime, g.EndTime.Add(time.Minute)) {
t.Fatal("shard group not selected when max after after end time")
}
if !g.Contains(g.StartTime.Add(-time.Minute), g.EndTime.Add(time.Minute)) {
t.Fatal("shard group not selected when min before start time and when max after end time")
}
if !g.Contains(g.StartTime.Add(time.Minute), g.EndTime.Add(-time.Minute)) {
t.Fatal("shard group not selected when min after start time and when max before end time")
}
if !g.Contains(g.StartTime, g.EndTime) {
t.Fatal("shard group not selected when min at start time and when max at end time")
}
if g.Contains(g.StartTime.Add(-10*time.Hour), g.EndTime.Add(-9*time.Hour)) {
t.Fatal("shard group selected when both min and max before shard times")
}
if g.Contains(g.StartTime.Add(24*time.Hour), g.EndTime.Add(25*time.Hour)) {
t.Fatal("shard group selected when both min and max after shard times")
}
}
// MustParseExpr parses an expression string and returns its AST representation. // MustParseExpr parses an expression string and returns its AST representation.
func MustParseExpr(s string) influxql.Expr { func MustParseExpr(s string) influxql.Expr {
expr, err := influxql.ParseExpr(s) expr, err := influxql.ParseExpr(s)

View File

@ -45,6 +45,13 @@ func newShardGroup() *ShardGroup { return &ShardGroup{} }
// Duration returns the duration between the shard group's start and end time. // Duration returns the duration between the shard group's start and end time.
func (g *ShardGroup) Duration() time.Duration { return g.EndTime.Sub(g.StartTime) } func (g *ShardGroup) Duration() time.Duration { return g.EndTime.Sub(g.StartTime) }
// Contains return whether the shard group contains data for the time between min and max
func (g *ShardGroup) Contains(min, max time.Time) bool {
return timeBetweenInclusive(g.StartTime, min, max) ||
timeBetweenInclusive(g.EndTime, min, max) ||
(g.StartTime.Before(min) && g.EndTime.After(max))
}
// dropSeries will delete all data with the seriesID // dropSeries will delete all data with the seriesID
func (g *ShardGroup) dropSeries(seriesID uint32) error { func (g *ShardGroup) dropSeries(seriesID uint32) error {
for _, s := range g.Shards { for _, s := range g.Shards {

2
tx.go
View File

@ -114,7 +114,7 @@ func (tx *tx) CreateIterators(stmt *influxql.SelectStatement) ([]influxql.Iterat
// Find shard groups within time range. // Find shard groups within time range.
var shardGroups []*ShardGroup var shardGroups []*ShardGroup
for _, group := range rp.shardGroups { for _, group := range rp.shardGroups {
if timeBetweenInclusive(group.StartTime, tmin, tmax) || timeBetweenInclusive(group.EndTime, tmin, tmax) { if group.Contains(tmin, tmax) {
shardGroups = append(shardGroups, group) shardGroups = append(shardGroups, group)
} }
} }