2015-05-26 21:42:44 +00:00
|
|
|
package cluster
|
2015-05-08 20:27:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2015-05-20 22:46:27 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2015-05-26 19:56:54 +00:00
|
|
|
"github.com/influxdb/influxdb/cluster/internal"
|
2015-05-22 21:00:51 +00:00
|
|
|
"github.com/influxdb/influxdb/tsdb"
|
2015-05-08 20:27:31 +00:00
|
|
|
)
|
|
|
|
|
2015-05-21 17:12:58 +00:00
|
|
|
//go:generate protoc --gogo_out=. internal/data.proto
|
|
|
|
|
2015-07-16 21:27:29 +00:00
|
|
|
// MapShardRequest represents the request to map a remote shard for a query.
|
|
|
|
type MapShardRequest struct {
|
|
|
|
pb internal.MapShardRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MapShardRequest) ShardID() uint64 { return m.pb.GetShardID() }
|
|
|
|
func (m *MapShardRequest) Query() string { return m.pb.GetQuery() }
|
|
|
|
func (m *MapShardRequest) ChunkSize() int32 { return m.pb.GetChunkSize() }
|
|
|
|
|
|
|
|
func (m *MapShardRequest) SetShardID(id uint64) { m.pb.ShardID = &id }
|
|
|
|
func (m *MapShardRequest) SetQuery(query string) { m.pb.Query = &query }
|
|
|
|
func (m *MapShardRequest) SetChunkSize(chunkSize int32) { m.pb.ChunkSize = &chunkSize }
|
|
|
|
|
|
|
|
// MarshalBinary encodes the object to a binary format.
|
|
|
|
func (m *MapShardRequest) MarshalBinary() ([]byte, error) {
|
|
|
|
return proto.Marshal(&m.pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary populates MapShardRequest from a binary format.
|
|
|
|
func (m *MapShardRequest) UnmarshalBinary(buf []byte) error {
|
|
|
|
if err := proto.Unmarshal(buf, &m.pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapShardResponse represents the response returned from a remote MapShardRequest call
|
|
|
|
type MapShardResponse struct {
|
|
|
|
pb internal.MapShardResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMapShardResponse(code int, message string) *MapShardResponse {
|
|
|
|
m := &MapShardResponse{}
|
|
|
|
m.SetCode(code)
|
|
|
|
m.SetMessage(message)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MapShardResponse) Code() int { return int(r.pb.GetCode()) }
|
|
|
|
func (r *MapShardResponse) Message() string { return r.pb.GetMessage() }
|
|
|
|
func (r *MapShardResponse) TagSets() []string { return r.pb.GetTagSets() }
|
2015-07-28 22:49:18 +00:00
|
|
|
func (r *MapShardResponse) Fields() []string { return r.pb.GetFields() }
|
2015-07-16 21:27:29 +00:00
|
|
|
func (r *MapShardResponse) Data() []byte { return r.pb.GetData() }
|
|
|
|
|
|
|
|
func (r *MapShardResponse) SetCode(code int) { r.pb.Code = proto.Int32(int32(code)) }
|
|
|
|
func (r *MapShardResponse) SetMessage(message string) { r.pb.Message = &message }
|
|
|
|
func (r *MapShardResponse) SetTagSets(tagsets []string) { r.pb.TagSets = tagsets }
|
2015-07-28 22:49:18 +00:00
|
|
|
func (r *MapShardResponse) SetFields(fields []string) { r.pb.Fields = fields }
|
2015-07-16 21:27:29 +00:00
|
|
|
func (r *MapShardResponse) SetData(data []byte) { r.pb.Data = data }
|
|
|
|
|
|
|
|
// MarshalBinary encodes the object to a binary format.
|
|
|
|
func (r *MapShardResponse) MarshalBinary() ([]byte, error) {
|
|
|
|
return proto.Marshal(&r.pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary populates WritePointRequest from a binary format.
|
|
|
|
func (r *MapShardResponse) UnmarshalBinary(buf []byte) error {
|
|
|
|
if err := proto.Unmarshal(buf, &r.pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:52:11 +00:00
|
|
|
// WritePointsRequest represents a request to write point data to the cluster
|
|
|
|
type WritePointsRequest struct {
|
|
|
|
Database string
|
|
|
|
RetentionPolicy string
|
|
|
|
ConsistencyLevel ConsistencyLevel
|
2015-05-22 21:00:51 +00:00
|
|
|
Points []tsdb.Point
|
2015-05-20 18:52:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 22:46:27 +00:00
|
|
|
// AddPoint adds a point to the WritePointRequest with field name 'value'
|
|
|
|
func (w *WritePointsRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
|
2015-05-22 21:00:51 +00:00
|
|
|
w.Points = append(w.Points, tsdb.NewPoint(
|
2015-05-22 20:13:06 +00:00
|
|
|
name, tags, map[string]interface{}{"value": value}, timestamp,
|
|
|
|
))
|
2015-05-20 18:52:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 22:46:27 +00:00
|
|
|
// WriteShardRequest represents the a request to write a slice of points to a shard
|
|
|
|
type WriteShardRequest struct {
|
|
|
|
pb internal.WriteShardRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteShardResponse represents the response returned from a remote WriteShardRequest call
|
|
|
|
type WriteShardResponse struct {
|
|
|
|
pb internal.WriteShardResponse
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
func (w *WriteShardRequest) SetShardID(id uint64) { w.pb.ShardID = &id }
|
|
|
|
func (w *WriteShardRequest) ShardID() uint64 { return w.pb.GetShardID() }
|
2015-05-20 22:46:27 +00:00
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
func (w *WriteShardRequest) Points() []tsdb.Point { return w.unmarshalPoints() }
|
2015-05-20 22:46:27 +00:00
|
|
|
|
|
|
|
func (w *WriteShardRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
|
2015-05-22 21:00:51 +00:00
|
|
|
w.AddPoints([]tsdb.Point{tsdb.NewPoint(
|
2015-05-22 20:13:06 +00:00
|
|
|
name, tags, map[string]interface{}{"value": value}, timestamp,
|
|
|
|
)})
|
2015-05-20 22:46:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 21:00:51 +00:00
|
|
|
func (w *WriteShardRequest) AddPoints(points []tsdb.Point) {
|
2015-05-20 22:46:27 +00:00
|
|
|
w.pb.Points = append(w.pb.Points, w.marshalPoints(points)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalBinary encodes the object to a binary format.
|
|
|
|
func (w *WriteShardRequest) MarshalBinary() ([]byte, error) {
|
|
|
|
return proto.Marshal(&w.pb)
|
|
|
|
}
|
|
|
|
|
2015-05-22 21:00:51 +00:00
|
|
|
func (w *WriteShardRequest) marshalPoints(points []tsdb.Point) []*internal.Point {
|
2015-05-20 22:46:27 +00:00
|
|
|
pts := make([]*internal.Point, len(points))
|
|
|
|
for i, p := range points {
|
|
|
|
fields := []*internal.Field{}
|
2015-05-22 21:22:03 +00:00
|
|
|
for k, v := range p.Fields() {
|
2015-05-20 22:46:27 +00:00
|
|
|
name := k
|
|
|
|
f := &internal.Field{
|
|
|
|
Name: &name,
|
|
|
|
}
|
|
|
|
switch t := v.(type) {
|
|
|
|
case int:
|
|
|
|
f.Int64 = proto.Int64(int64(t))
|
|
|
|
case int32:
|
|
|
|
f.Int32 = proto.Int32(t)
|
|
|
|
case int64:
|
|
|
|
f.Int64 = proto.Int64(t)
|
|
|
|
case float64:
|
|
|
|
f.Float64 = proto.Float64(t)
|
|
|
|
case bool:
|
|
|
|
f.Bool = proto.Bool(t)
|
|
|
|
case string:
|
|
|
|
f.String_ = proto.String(t)
|
|
|
|
case []byte:
|
|
|
|
f.Bytes = t
|
|
|
|
}
|
|
|
|
fields = append(fields, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := []*internal.Tag{}
|
2015-05-22 21:12:34 +00:00
|
|
|
for k, v := range p.Tags() {
|
2015-05-20 22:46:27 +00:00
|
|
|
key := k
|
|
|
|
value := v
|
|
|
|
tags = append(tags, &internal.Tag{
|
|
|
|
Key: &key,
|
|
|
|
Value: &value,
|
|
|
|
})
|
|
|
|
}
|
2015-05-22 20:37:37 +00:00
|
|
|
name := p.Name()
|
2015-05-20 22:46:27 +00:00
|
|
|
pts[i] = &internal.Point{
|
|
|
|
Name: &name,
|
2015-05-22 20:13:06 +00:00
|
|
|
Time: proto.Int64(p.Time().UnixNano()),
|
2015-05-20 22:46:27 +00:00
|
|
|
Fields: fields,
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return pts
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary populates WritePointRequest from a binary format.
|
|
|
|
func (w *WriteShardRequest) UnmarshalBinary(buf []byte) error {
|
|
|
|
if err := proto.Unmarshal(buf, &w.pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
func (w *WriteShardRequest) unmarshalPoints() []tsdb.Point {
|
2015-05-22 21:00:51 +00:00
|
|
|
points := make([]tsdb.Point, len(w.pb.GetPoints()))
|
2015-05-20 22:46:27 +00:00
|
|
|
for i, p := range w.pb.GetPoints() {
|
2015-05-22 21:00:51 +00:00
|
|
|
pt := tsdb.NewPoint(
|
2015-05-22 20:13:06 +00:00
|
|
|
p.GetName(), map[string]string{},
|
|
|
|
map[string]interface{}{}, time.Unix(0, p.GetTime()))
|
2015-05-20 22:46:27 +00:00
|
|
|
|
|
|
|
for _, f := range p.GetFields() {
|
|
|
|
n := f.GetName()
|
|
|
|
if f.Int32 != nil {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetInt32())
|
2015-05-20 22:46:27 +00:00
|
|
|
} else if f.Int64 != nil {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetInt64())
|
2015-05-20 22:46:27 +00:00
|
|
|
} else if f.Float64 != nil {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetFloat64())
|
2015-05-20 22:46:27 +00:00
|
|
|
} else if f.Bool != nil {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetBool())
|
2015-05-20 22:46:27 +00:00
|
|
|
} else if f.String_ != nil {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetString_())
|
2015-05-20 22:46:27 +00:00
|
|
|
} else {
|
2015-05-22 21:22:03 +00:00
|
|
|
pt.AddField(n, f.GetBytes())
|
2015-05-20 22:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 21:12:34 +00:00
|
|
|
tags := tsdb.Tags{}
|
2015-05-20 22:46:27 +00:00
|
|
|
for _, t := range p.GetTags() {
|
2015-05-22 21:12:34 +00:00
|
|
|
tags[t.GetKey()] = t.GetValue()
|
2015-05-20 22:46:27 +00:00
|
|
|
}
|
2015-05-22 21:12:34 +00:00
|
|
|
pt.SetTags(tags)
|
2015-05-20 22:46:27 +00:00
|
|
|
points[i] = pt
|
|
|
|
}
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
func (w *WriteShardResponse) SetCode(code int) { w.pb.Code = proto.Int32(int32(code)) }
|
|
|
|
func (w *WriteShardResponse) SetMessage(message string) { w.pb.Message = &message }
|
2015-05-20 22:46:27 +00:00
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
func (w *WriteShardResponse) Code() int { return int(w.pb.GetCode()) }
|
|
|
|
func (w *WriteShardResponse) Message() string { return w.pb.GetMessage() }
|
2015-05-20 22:46:27 +00:00
|
|
|
|
|
|
|
// MarshalBinary encodes the object to a binary format.
|
|
|
|
func (w *WriteShardResponse) MarshalBinary() ([]byte, error) {
|
|
|
|
return proto.Marshal(&w.pb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary populates WritePointRequest from a binary format.
|
|
|
|
func (w *WriteShardResponse) UnmarshalBinary(buf []byte) error {
|
|
|
|
if err := proto.Unmarshal(buf, &w.pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|