2015-05-27 21:13:31 +00:00
|
|
|
package cluster_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/cluster"
|
|
|
|
"github.com/influxdb/influxdb/tsdb"
|
|
|
|
)
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Ensure the shard writer can successful write a single request.
|
|
|
|
func TestShardWriter_WriteShard_Success(t *testing.T) {
|
2015-05-30 20:00:46 +00:00
|
|
|
ts := newTestService(writeShardSuccess)
|
|
|
|
s := cluster.NewService(cluster.Config{BindAddress: "127.0.0.1:0"})
|
2015-06-02 20:45:52 +00:00
|
|
|
s.TSDBStore = ts
|
2015-05-30 16:11:23 +00:00
|
|
|
if err := s.Open(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
w := cluster.NewShardWriter(time.Minute)
|
|
|
|
w.MetaStore = &metaStore{host: s.Addr().String()}
|
2015-05-27 21:13:31 +00:00
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Build a single point.
|
2015-05-27 21:13:31 +00:00
|
|
|
now := time.Now()
|
|
|
|
var points []tsdb.Point
|
2015-05-30 16:11:23 +00:00
|
|
|
points = append(points, tsdb.NewPoint("cpu", tsdb.Tags{"host": "server01"}, map[string]interface{}{"value": int64(100)}, now))
|
2015-05-27 21:13:31 +00:00
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Write to shard and close.
|
2015-05-30 20:00:46 +00:00
|
|
|
if err := w.WriteShard(1, 2, points); err != nil {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatal(err)
|
2015-05-30 16:11:23 +00:00
|
|
|
} else if err := w.Close(); err != nil {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Validate response.
|
2015-05-27 21:13:31 +00:00
|
|
|
responses, err := ts.ResponseN(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-30 16:11:23 +00:00
|
|
|
} else if responses[0].shardID != 1 {
|
|
|
|
t.Fatalf("unexpected shard id: %d", responses[0].shardID)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Validate point.
|
|
|
|
if p := responses[0].points[0]; p.Name() != "cpu" {
|
|
|
|
t.Fatalf("unexpected name: %s", p.Name())
|
|
|
|
} else if p.Fields()["value"] != int64(100) {
|
|
|
|
t.Fatalf("unexpected 'value' field: %d", p.Fields()["value"])
|
|
|
|
} else if p.Tags()["host"] != "server01" {
|
|
|
|
t.Fatalf("unexpected 'host' tag: %s", p.Tags()["host"])
|
|
|
|
} else if p.Time().UnixNano() != now.UnixNano() {
|
|
|
|
t.Fatalf("unexpected time: %s", p.Time())
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
// Ensure the shard writer can successful write a multiple requests.
|
|
|
|
func TestShardWriter_WriteShard_Multiple(t *testing.T) {
|
|
|
|
ts := newTestService(writeShardSuccess)
|
|
|
|
s := cluster.NewService(cluster.Config{BindAddress: "127.0.0.1:0"})
|
2015-06-02 20:45:52 +00:00
|
|
|
s.TSDBStore = ts
|
2015-05-30 20:00:46 +00:00
|
|
|
if err := s.Open(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
w := cluster.NewShardWriter(time.Minute)
|
|
|
|
w.MetaStore = &metaStore{host: s.Addr().String()}
|
2015-05-27 21:13:31 +00:00
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
// Build a single point.
|
2015-05-27 21:13:31 +00:00
|
|
|
now := time.Now()
|
|
|
|
var points []tsdb.Point
|
2015-05-30 20:00:46 +00:00
|
|
|
points = append(points, tsdb.NewPoint("cpu", tsdb.Tags{"host": "server01"}, map[string]interface{}{"value": int64(100)}, now))
|
2015-05-27 21:13:31 +00:00
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
// Write to shard twice and close.
|
|
|
|
if err := w.WriteShard(1, 2, points); err != nil {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatal(err)
|
2015-05-30 20:00:46 +00:00
|
|
|
} else if err := w.WriteShard(1, 2, points); err != nil {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatal(err)
|
2015-05-30 20:00:46 +00:00
|
|
|
} else if err := w.Close(); err != nil {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
// Validate response.
|
2015-05-27 21:13:31 +00:00
|
|
|
responses, err := ts.ResponseN(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-30 20:00:46 +00:00
|
|
|
} else if responses[0].shardID != 1 {
|
|
|
|
t.Fatalf("unexpected shard id: %d", responses[0].shardID)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
// Validate point.
|
|
|
|
if p := responses[0].points[0]; p.Name() != "cpu" {
|
|
|
|
t.Fatalf("unexpected name: %s", p.Name())
|
|
|
|
} else if p.Fields()["value"] != int64(100) {
|
|
|
|
t.Fatalf("unexpected 'value' field: %d", p.Fields()["value"])
|
|
|
|
} else if p.Tags()["host"] != "server01" {
|
|
|
|
t.Fatalf("unexpected 'host' tag: %s", p.Tags()["host"])
|
|
|
|
} else if p.Time().UnixNano() != now.UnixNano() {
|
|
|
|
t.Fatalf("unexpected time: %s", p.Time())
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-30 16:11:23 +00:00
|
|
|
|
|
|
|
// Ensure the shard writer returns an error when the server fails to accept the write.
|
|
|
|
func TestShardWriter_WriteShard_Error(t *testing.T) {
|
2015-05-30 20:00:46 +00:00
|
|
|
ts := newTestService(writeShardFail)
|
|
|
|
s := cluster.NewService(cluster.Config{BindAddress: "127.0.0.1:0"})
|
2015-06-02 20:45:52 +00:00
|
|
|
s.TSDBStore = ts
|
2015-05-30 16:11:23 +00:00
|
|
|
if err := s.Open(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
w := cluster.NewShardWriter(time.Minute)
|
|
|
|
w.MetaStore = &metaStore{host: s.Addr().String()}
|
2015-05-27 21:13:31 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
shardID := uint64(1)
|
|
|
|
ownerID := uint64(2)
|
|
|
|
var points []tsdb.Point
|
|
|
|
points = append(points, tsdb.NewPoint(
|
|
|
|
"cpu", tsdb.Tags{"host": "server01"}, map[string]interface{}{"value": int64(100)}, now,
|
|
|
|
))
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
if err := w.WriteShard(shardID, ownerID, points); err == nil || err.Error() != "error code 1: write shard: failed to write" {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Ensure the shard writer returns an error when dialing times out.
|
|
|
|
func TestShardWriter_Write_ErrDialTimeout(t *testing.T) {
|
2015-05-30 20:00:46 +00:00
|
|
|
ts := newTestService(writeShardSuccess)
|
|
|
|
s := cluster.NewService(cluster.Config{BindAddress: "127.0.0.1:0"})
|
2015-06-02 20:45:52 +00:00
|
|
|
s.TSDBStore = ts
|
2015-05-30 16:11:23 +00:00
|
|
|
if err := s.Open(); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
w := cluster.NewShardWriter(time.Nanosecond)
|
|
|
|
w.MetaStore = &metaStore{host: s.Addr().String()}
|
2015-05-27 21:13:31 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
shardID := uint64(1)
|
|
|
|
ownerID := uint64(2)
|
|
|
|
var points []tsdb.Point
|
|
|
|
points = append(points, tsdb.NewPoint(
|
|
|
|
"cpu", tsdb.Tags{"host": "server01"}, map[string]interface{}{"value": int64(100)}, now,
|
|
|
|
))
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
if err, exp := w.WriteShard(shardID, ownerID, points), "i/o timeout"; err == nil || !strings.Contains(err.Error(), exp) {
|
2015-05-27 21:13:31 +00:00
|
|
|
t.Fatalf("expected error %v, to contain %s", err, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
// Ensure the shard writer returns an error when reading times out.
|
|
|
|
func TestShardWriter_Write_ErrReadTimeout(t *testing.T) {
|
2015-05-27 21:13:31 +00:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
w := cluster.NewShardWriter(time.Millisecond)
|
|
|
|
w.MetaStore = &metaStore{host: ln.Addr().String()}
|
2015-05-27 21:13:31 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
shardID := uint64(1)
|
|
|
|
ownerID := uint64(2)
|
|
|
|
var points []tsdb.Point
|
|
|
|
points = append(points, tsdb.NewPoint(
|
|
|
|
"cpu", tsdb.Tags{"host": "server01"}, map[string]interface{}{"value": int64(100)}, now,
|
|
|
|
))
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
if err := w.WriteShard(shardID, ownerID, points); err == nil || !strings.Contains(err.Error(), "i/o timeout") {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2015-05-27 21:13:31 +00:00
|
|
|
}
|
|
|
|
}
|