// Ensures the points writer maps a single point to a single shard. func TestPointsWriter_MapShards_One(t *testing.T) { ms := PointsWriterMetaClient{} rp := NewRetentionPolicy("myp", time.Hour, 3) ms.NodeIDFn = func() uint64 { return 1 } ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { return rp, nil } ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { return &rp.ShardGroups[0], nil } c := cluster.PointsWriter{MetaClient: ms} pr := &cluster.WritePointsRequest{ Database: "mydb", RetentionPolicy: "myrp", } pr.AddPoint("cpu", 1.0, time.Now(), nil) var ( shardMappings *cluster.ShardMapping err error ) if shardMappings, err = c.MapShards(pr); err != nil { t.Fatalf("unexpected an error: %v", err) } if exp := 1; len(shardMappings.Points) != exp { t.Errorf("MapShards() len mismatch. got %v, exp %v", len(shardMappings.Points), exp) } }
// Ensures the points writer maps a multiple points across shard group boundaries. func TestPointsWriter_MapShards_Multiple(t *testing.T) { ms := PointsWriterMetaClient{} rp := NewRetentionPolicy("myp", time.Hour, 3) AttachShardGroupInfo(rp, []meta.ShardOwner{ {NodeID: 1}, {NodeID: 2}, {NodeID: 3}, }) AttachShardGroupInfo(rp, []meta.ShardOwner{ {NodeID: 1}, {NodeID: 2}, {NodeID: 3}, }) ms.NodeIDFn = func() uint64 { return 1 } ms.RetentionPolicyFn = func(db, retentionPolicy string) (*meta.RetentionPolicyInfo, error) { return rp, nil } ms.CreateShardGroupIfNotExistsFn = func(database, policy string, timestamp time.Time) (*meta.ShardGroupInfo, error) { for i, sg := range rp.ShardGroups { if timestamp.Equal(sg.StartTime) || timestamp.After(sg.StartTime) && timestamp.Before(sg.EndTime) { return &rp.ShardGroups[i], nil } } panic("should not get here") } c := cluster.PointsWriter{MetaClient: ms} pr := &cluster.WritePointsRequest{ Database: "mydb", RetentionPolicy: "myrp", } // Three points that range over the shardGroup duration (1h) and should map to two // distinct shards pr.AddPoint("cpu", 1.0, time.Unix(0, 0), nil) pr.AddPoint("cpu", 2.0, time.Unix(0, 0).Add(time.Hour), nil) pr.AddPoint("cpu", 3.0, time.Unix(0, 0).Add(time.Hour+time.Second), nil) var ( shardMappings *cluster.ShardMapping err error ) if shardMappings, err = c.MapShards(pr); err != nil { t.Fatalf("unexpected an error: %v", err) } if exp := 2; len(shardMappings.Points) != exp { t.Errorf("MapShards() len mismatch. got %v, exp %v", len(shardMappings.Points), exp) } for _, points := range shardMappings.Points { // First shard shoud have 1 point w/ first point added if len(points) == 1 && points[0].Time() != pr.Points[0].Time() { t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[0].Time(), pr.Points[0].Time()) } // Second shard shoud have the last two points added if len(points) == 2 && points[0].Time() != pr.Points[1].Time() { t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[0].Time(), pr.Points[1].Time()) } if len(points) == 2 && points[1].Time() != pr.Points[2].Time() { t.Fatalf("MapShards() value mismatch. got %v, exp %v", points[1].Time(), pr.Points[2].Time()) } } }