-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
integrity_test.go
289 lines (231 loc) · 8.26 KB
/
integrity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/minio/minio-go"
"github.com/stretchr/testify/require"
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/reader"
)
type Flog struct {
Host string `json:"host"`
UserId string `json:"user-identifier"`
Timestamp string `json:"datetime"`
Method string `json:"method"`
Request string `json:"request"`
Protocol string `json:"protocol"`
Status uint16 `json:"status"`
ByteCount uint64 `json:"bytes"`
Referer string `json:"referer"`
}
// Same as `Flog`, but all fields are pointers, because `parquet-go` is only
// working when fields are pointers.
type ParquetFlog struct {
Host *string `parquet:"name=host, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
UserId *string `parquet:"name=user-identifier, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
Timestamp *string `parquet:"name=datetime, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
Method *string `parquet:"name=method, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
Request *string `parquet:"name=request, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
Protocol *string `parquet:"name=protocol, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
Status *uint16 `parquet:"name=status, type=INT32, encoding=PLAIN"`
ByteCount *uint64 `parquet:"name=bytes, type=INT32, encoding=PLAIN"`
Referer *string `parquet:"name=referer, type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"`
}
func (flog *ParquetFlog) Deref() Flog {
return Flog{
Host: *flog.Host,
UserId: *flog.UserId,
Timestamp: *flog.Timestamp,
Method: *flog.Method,
Request: *flog.Request,
Protocol: *flog.Protocol,
Status: *flog.Status,
ByteCount: *flog.ByteCount,
Referer: *flog.Referer,
}
}
// - Send logs to Parseable
// - Wait for sync
// - Download parquet files from the store created by Parseable for the minute
// - Compare the sent logs with the ones loaded from the downloaded parquet
func TestIntegrity(t *testing.T) {
CreateStream(t, NewGlob.QueryClient, NewGlob.Stream)
iterations := 2
flogsPerIteration := 100
parseableSyncWait := 1 * time.Minute // NOTE: This needs to be in sync with Parseable's.
// - Generate log files using `flog`
// - Load them into `Flog` structs
// - Ingest them into Parseable
flogs := make([]Flog, 0, iterations*flogsPerIteration)
for i := 0; i < iterations; i++ {
flogsFile := fmt.Sprintf("%d.log", i)
err := exec.Command("flog",
"--number", strconv.Itoa(flogsPerIteration),
"--format", "json",
"--type", "log",
"--overwrite",
"--output", flogsFile).Run()
if err != nil {
slog.Error("couldn't generate flogs", "error", err)
}
loadedFlogs := loadFlogsFromFile(flogsFile)
err = ingestFlogs(loadedFlogs, NewGlob.Stream)
if err != nil {
t.Fatal("error ingesting flogs", err)
}
flogs = append(flogs, loadedFlogs...)
slog.Info("ingested logs, sleeping...",
"iteration", i+1,
"log_count", len(loadedFlogs))
// Wait for the events to be sync'd.
time.Sleep(parseableSyncWait)
// XXX: We don't need to sleep for the entire minute, just until the next minute boundary.
}
parquetFiles := downloadParquetFiles(NewGlob.Stream, NewGlob.MinIoConfig)
actualFlogs := loadFlogsFromParquetFiles(parquetFiles)
rowCount := len(actualFlogs)
for i, expectedFlog := range flogs {
// The rows in parquet written by Parseable will be latest first, so we
// compare the first of ours with the last of what we got from Parseable's
// store.
actualFlog := actualFlogs[rowCount-i-1].Deref()
require.Equal(t, actualFlog, expectedFlog)
}
DeleteStream(t, NewGlob.QueryClient, NewGlob.Stream)
}
func ingestFlogs(flogs []Flog, stream string) error {
payload, _ := json.Marshal(flogs)
if NewGlob.IngestorUrl.String() == "" {
req, _ := NewGlob.QueryClient.NewRequest(http.MethodPost, "ingest", bytes.NewBuffer(payload))
req.Header.Add("X-P-Stream", stream)
response, err := NewGlob.QueryClient.Do(req)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("couldn't ingest logs, status code = %d", response.StatusCode)
}
} else {
req, _ := NewGlob.IngestorClient.NewRequest(http.MethodPost, "ingest", bytes.NewBuffer(payload))
req.Header.Add("X-P-Stream", stream)
response, err := NewGlob.QueryClient.Do(req)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("couldn't ingest logs, status code = %d", response.StatusCode)
}
}
return nil
}
func downloadParquetFiles(stream string, config MinIoConfig) []string {
client, err := minio.New(config.Url, config.User, config.Pass, false)
if err != nil {
slog.Error("couldn't create MinIO client", "error", err)
}
downloadedFileNames := make([]string, 0, 10)
slog.Info("downloading parquet files from MinIO",
"bucket", config.Bucket,
"stream", stream)
for objectInfo := range client.ListObjectsV2(config.Bucket, stream, true, nil) {
key := objectInfo.Key
if !isParquetFile(key) {
slog.Info("skipping path, not a parquet file", "key", key)
continue
}
parquetObject, err := client.GetObject(config.Bucket, key, minio.GetObjectOptions{})
if err != nil {
slog.Error("couldn't get object", "key", key, "error", err)
}
// Write the MinIO Object we got, into `downloadPath`.
fileName := strings.ReplaceAll(key, "/", ".")
f, _ := os.Create(fileName)
_, err = io.Copy(f, parquetObject)
if err != nil {
slog.Error("couldn't copy", "fileName", fileName, "error", err)
}
downloadedFileNames = append(downloadedFileNames, fileName)
f.Close()
}
// Reverse the filenames, because we want latest files first.
for i, j := 0, len(downloadedFileNames)-1; i < j; i, j = i+1, j-1 {
downloadedFileNames[i], downloadedFileNames[j] = downloadedFileNames[j], downloadedFileNames[i]
}
slog.Info("downloaded files", "paths", downloadedFileNames)
return downloadedFileNames
}
func loadFlogsFromParquetFile(path string) []ParquetFlog {
fr, err := local.NewLocalFileReader(path)
slog.Info("reading parquet file", "path", path)
if err != nil {
slog.Error("can't create local file reader", "error", err)
}
defer fr.Close()
pr, err := reader.NewParquetReader(fr, new(ParquetFlog), 4)
if err != nil {
slog.Error("can't create parquet reader", "error", err)
}
defer pr.ReadStop()
flogs := make([]ParquetFlog, pr.GetNumRows())
if err = pr.Read(&flogs); err != nil {
slog.Error("can't read parquet file", "error", err)
}
return flogs
}
func loadFlogsFromParquetFiles(parquetFiles []string) []ParquetFlog {
slog.Info("loading flogs from parquet files", "paths", parquetFiles, "count", len(parquetFiles))
flogs := make([]ParquetFlog, 0, len(parquetFiles)*10)
for _, parquetFile := range parquetFiles {
flogs = append(flogs, loadFlogsFromParquetFile(parquetFile)...)
}
return flogs
}
func isParquetFile(path string) bool {
return filepath.Ext(path) == ".parquet"
}
func loadFlogsFromFile(path string) []Flog {
f, err := os.Open(path)
if err != nil {
slog.Error("couldn't open file", "path", path, "error", err)
}
lines := bufio.NewScanner(f)
lines.Split(bufio.ScanLines)
flogs := make([]Flog, 0, 10)
for lines.Scan() {
line := lines.Bytes()
flog := Flog{}
err := json.Unmarshal(line, &flog)
if err != nil {
slog.Error("couldn't unmarshal line", "line", string(line), "error", err)
}
flogs = append(flogs, flog)
}
return flogs
}