Fix panic in ForEachMeasurementTagKey

If a shard was closed, ForEachMeasurementTagKey and TagKeyCardinality
would panic because the engine was nil.
pull/8487/head
Jason Wilder 2017-06-13 12:04:32 -06:00
parent e2c1da05e4
commit 77afe50f7e
2 changed files with 41 additions and 4 deletions

View File

@ -973,14 +973,18 @@ func (s *Shard) CreateSnapshot() (string, error) {
} }
func (s *Shard) ForEachMeasurementTagKey(name []byte, fn func(key []byte) error) error { func (s *Shard) ForEachMeasurementTagKey(name []byte, fn func(key []byte) error) error {
s.mu.RLock() if err := s.ready(); err != nil {
defer s.mu.RUnlock() return nil
}
return s.engine.ForEachMeasurementTagKey(name, fn) return s.engine.ForEachMeasurementTagKey(name, fn)
} }
func (s *Shard) TagKeyCardinality(name, key []byte) int { func (s *Shard) TagKeyCardinality(name, key []byte) int {
s.mu.RLock() if err := s.ready(); err != nil {
defer s.mu.RUnlock() return 0
}
return s.engine.TagKeyCardinality(name, key) return s.engine.TagKeyCardinality(name, key)
} }

View File

@ -789,6 +789,39 @@ func TestShard_Disabled_WriteQuery(t *testing.T) {
} }
} }
func TestShard_Closed_Functions(t *testing.T) {
sh := NewShard()
if err := sh.Open(); err != nil {
t.Fatal(err)
}
defer sh.Close()
pt := models.MustNewPoint(
"cpu",
models.NewTags(map[string]string{"host": "server"}),
map[string]interface{}{"value": 1.0},
time.Unix(1, 2),
)
if err := sh.WritePoints([]models.Point{pt}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
sh.Close()
// Should not panic, just a no-op when shard is closed
if err := sh.ForEachMeasurementTagKey([]byte("cpu"), func(k []byte) error {
return nil
}); err != nil {
t.Fatalf("expected nil: got %v", err)
}
// Should not panic, just a no-op when shard is closed
if exp, got := 0, sh.TagKeyCardinality([]byte("cpu"), []byte("host")); exp != got {
t.Fatalf("expected nil: exp %v, got %v", exp, got)
}
}
func TestShard_FieldDimensions(t *testing.T) { func TestShard_FieldDimensions(t *testing.T) {
sh := NewShard() sh := NewShard()