|
| 1 | +package importer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "fmt" |
| 8 | + "github.com/tinylib/msgp/msgp" |
| 9 | + "io" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/grafana/metrictank/conf" |
| 13 | + "github.com/kisielk/whisper-go/whisper" |
| 14 | + "github.com/raintank/schema" |
| 15 | +) |
| 16 | + |
| 17 | +//go:generate msgp |
| 18 | + |
| 19 | +// ArchiveRequest is a complete representation of a Metric together with some |
| 20 | +// chunk write requests containing data that shall be written into this metric |
| 21 | +type ArchiveRequest struct { |
| 22 | + MetricData schema.MetricData |
| 23 | + ChunkWriteRequests []ChunkWriteRequest |
| 24 | +} |
| 25 | + |
| 26 | +func NewArchiveRequest(w *whisper.Whisper, schemas conf.Schemas, file, name string, from, until uint32, writeUnfinishedChunks bool) (*ArchiveRequest, error) { |
| 27 | + if len(w.Header.Archives) == 0 { |
| 28 | + return nil, fmt.Errorf("Whisper file contains no archives: %q", file) |
| 29 | + } |
| 30 | + |
| 31 | + method, err := convertWhisperMethod(w.Header.Metadata.AggregationMethod) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + points := make(map[int][]whisper.Point) |
| 37 | + for i := range w.Header.Archives { |
| 38 | + p, err := w.DumpArchive(i) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("Failed to dump archive %d from whisper file %s", i, file) |
| 41 | + } |
| 42 | + points[i] = p |
| 43 | + } |
| 44 | + |
| 45 | + res := &ArchiveRequest{ |
| 46 | + MetricData: schema.MetricData{ |
| 47 | + Name: name, |
| 48 | + Value: 0, |
| 49 | + Interval: int(w.Header.Archives[0].SecondsPerPoint), |
| 50 | + Unit: "unknown", |
| 51 | + Time: 0, |
| 52 | + Mtype: "gauge", |
| 53 | + Tags: []string{}, |
| 54 | + }, |
| 55 | + } |
| 56 | + res.MetricData.SetId() |
| 57 | + |
| 58 | + _, selectedSchema := schemas.Match(res.MetricData.Name, int(w.Header.Archives[0].SecondsPerPoint)) |
| 59 | + converter := newConverter(w.Header.Archives, points, method, from, until) |
| 60 | + for retIdx, retention := range selectedSchema.Retentions { |
| 61 | + convertedPoints := converter.getPoints(retIdx, uint32(retention.SecondsPerPoint), uint32(retention.NumberOfPoints)) |
| 62 | + for m, p := range convertedPoints { |
| 63 | + if len(p) == 0 { |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + var archive schema.Archive |
| 68 | + if retIdx > 0 { |
| 69 | + archive = schema.NewArchive(m, retention.ChunkSpan) |
| 70 | + } |
| 71 | + |
| 72 | + encodedChunks := encodeChunksFromPoints(p, uint32(retention.SecondsPerPoint), retention.ChunkSpan, writeUnfinishedChunks) |
| 73 | + for _, chunk := range encodedChunks { |
| 74 | + res.ChunkWriteRequests = append(res.ChunkWriteRequests, NewChunkWriteRequest( |
| 75 | + archive, |
| 76 | + uint32(retention.MaxRetention()), |
| 77 | + chunk.Series.T0, |
| 78 | + chunk.Encode(retention.ChunkSpan), |
| 79 | + time.Now(), |
| 80 | + )) |
| 81 | + } |
| 82 | + |
| 83 | + if res.MetricData.Time < int64(p[len(p)-1].Timestamp) { |
| 84 | + res.MetricData.Time = int64(p[len(p)-1].Timestamp) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return res, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (a *ArchiveRequest) MarshalCompressed() (*bytes.Buffer, error) { |
| 93 | + var buf bytes.Buffer |
| 94 | + |
| 95 | + buf.WriteByte(byte(uint8(1))) |
| 96 | + |
| 97 | + g := gzip.NewWriter(&buf) |
| 98 | + err := msgp.Encode(g, a) |
| 99 | + if err != nil { |
| 100 | + return &buf, fmt.Errorf("ERROR: Encoding MGSP data: %q", err) |
| 101 | + } |
| 102 | + |
| 103 | + err = g.Close() |
| 104 | + if err != nil { |
| 105 | + return &buf, fmt.Errorf("ERROR: Compressing MSGP data: %q", err) |
| 106 | + } |
| 107 | + |
| 108 | + return &buf, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (a *ArchiveRequest) UnmarshalCompressed(b io.Reader) error { |
| 112 | + versionBuf := make([]byte, 1) |
| 113 | + readBytes, err := b.Read(versionBuf) |
| 114 | + if err != nil || readBytes != 1 { |
| 115 | + return fmt.Errorf("ERROR: Failed to read one byte: %s", err) |
| 116 | + } |
| 117 | + |
| 118 | + version := uint8(versionBuf[0]) |
| 119 | + if version != 1 { |
| 120 | + return fmt.Errorf("ERROR: Only version 1 is supported, received version %d", version) |
| 121 | + } |
| 122 | + |
| 123 | + gzipReader, err := gzip.NewReader(b) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("ERROR: Creating Gzip reader: %q", err) |
| 126 | + } |
| 127 | + |
| 128 | + err = msgp.Decode(bufio.NewReader(gzipReader), a) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("ERROR: Unmarshaling Raw: %q", err) |
| 131 | + } |
| 132 | + gzipReader.Close() |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
0 commit comments