~tsileo/blobsfile

ref: 76a89107299fb76b273d92e13602ab1822aa4f76 blobsfile/blobsfile_test.go -rw-r--r-- 11.2 KiB
76a89107Thomas Sileo Bugfixes 2 years ago
                                                                                
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package blobsfile

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"io/ioutil"
	mrand "math/rand"
	"os"
	"reflect"
	"sort"
	"testing"

	"a4.io/blobstash/pkg/hashutil"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func TestScan(t *testing.T) {
	// blobs, err := ScanBlobsFile("/home/thomas/Proj/blobstash/blobstash_yzadat1111111/blobs/blobs-00000")
	// check(err)
	// t.Logf("blobs=%q", blobs)
}

func BenchmarkBlobsFilePut512B(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 512, b)
}

func BenchmarkBlobsFilePut512KB(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 512000, b)
}

func BenchmarkBlobsFilePut2MB(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 2000000, b)
}

func BenchmarkBlobsFilePut512BCompressed(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 512, b)
}

func BenchmarkBlobsFilePut512KBCompressed(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 512000, b)
}

func BenchmarkBlobsFilePut2MBCompressed(b *testing.B) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	benchmarkBlobsFilePut(back, 2000000, b)
}

func benchmarkBlobsFilePut(back *BlobsFiles, blobSize int, b *testing.B) {
	// b.ResetTimer()
	// b.StopTimer()
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		h, blob := randBlob(blobSize)
		b.StartTimer()
		if err := back.Put(h, blob); err != nil {
			panic(err)
		}
		b.StopTimer()
	}
	b.SetBytes(int64(blobSize))
}

func TestBlobsFileReedSolomon(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test", BlobsFileSize: 16000000})
	check(err)
	defer os.RemoveAll("./tmp_blobsfile_test")
	testParity(t, b, true, nil)
	fname := b.filename(0)
	b.Close()
	// // Corrupt the file

	// f, err := os.OpenFile(fname, os.O_RDWR, 0755)
	// if err != nil {
	// 	panic(err)
	// }
	// FIXME(tsileo): test this
	// if _, err := f.Seek(defaultMaxBlobsFileSize/10*3, os.SEEK_SET); err != nil {
	// if _, err := f.Seek(defaultMaxBlobsFileSize/10, os.SEEK_SET); err != nil {
	// if _, err := f.Seek(16000000/10*2, os.SEEK_SET); err != nil {
	data, err := ioutil.ReadFile(fname)
	if err != nil {
		panic(err)
	}
	punchOffset := int64(16000000/10*5) - 10
	t.Logf("punch at %d\n", punchOffset)
	fmt.Printf("punch at %d/%d\n", punchOffset, 16000000)
	ndata := []byte("blobsfilelol")
	copy(data[punchOffset:punchOffset+int64(len(ndata))], ndata)
	if err := ioutil.WriteFile(fname, []byte(data), 0644); err != nil {
		panic(err)
	}
	// Reopen the db
	b, err = New(&Opts{Directory: "./tmp_blobsfile_test", BlobsFileSize: 16000000})
	check(err)
	defer b.Close()
	// Ensure we can recover from this corruption
	cb := func(err error) error {
		if err != nil {
			if err := b.scan(nil); err != nil {
				return b.checkBlobsFile(err.(*corruptedError))
			}
			panic("should not happen")
		}
		return nil
	}
	testParity(t, b, false, cb)
	packs := b.SealedPacks()
	t.Logf("packs=%+v", packs)
}

func TestBlobsFileReedSolomonReindex(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test", BlobsFileSize: 16000000})
	check(err)
	defer os.RemoveAll("./tmp_blobsfile_test")
	testParity(t, b, true, nil)
	fname := b.filename(0)
	b.Close()
	// // Corrupt the file

	// f, err := os.OpenFile(fname, os.O_RDWR, 0755)
	// if err != nil {
	// 	panic(err)
	// }
	// FIXME(tsileo): test this
	// if _, err := f.Seek(defaultMaxBlobsFileSize/10*3, os.SEEK_SET); err != nil {
	// if _, err := f.Seek(defaultMaxBlobsFileSize/10, os.SEEK_SET); err != nil {
	// if _, err := f.Seek(16000000/10*2, os.SEEK_SET); err != nil {
	data, err := ioutil.ReadFile(fname)
	if err != nil {
		panic(err)
	}
	punchOffset := int64(16000000/10*5) - 10
	t.Logf("punch at %d\n", punchOffset)
	fmt.Printf("punch at %d/%d\n", punchOffset, 16000000)
	ndata := []byte("blobsfilelol")
	copy(data[punchOffset:punchOffset+int64(len(ndata))], ndata)
	if err := ioutil.WriteFile(fname, []byte(data), 0644); err != nil {
		panic(err)
	}
	// Reopen the db
	b, err = New(&Opts{Directory: "./tmp_blobsfile_test", BlobsFileSize: 16000000})
	check(err)
	defer b.Close()
	if err := b.RebuildIndex(); err != nil {
		t.Errorf("failed to rebuild index: %v", err)
	}
}

func TestBlobsFileReedSolomonWithCompression(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test", BlobsFileSize: 16000000})
	check(err)
	defer b.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	testParity(t, b, true, nil)
}

func testParity(t *testing.T, b *BlobsFiles, insert bool, cb func(error) error) ([]string, [][]byte) {
	hashes := []string{}
	blobs := [][]byte{}
	if insert {
		for i := 0; i < 31+10; i++ {
			h, blob := randBlob(512000)
			hashes = append(hashes, h)
			blobs = append(blobs, blob)
			if err := b.Put(h, blob); err != nil {
				panic(err)
			}
		}
	}
	if err := b.checkParityBlobs(0); err != nil {
		if cb == nil {
			panic(err)
		}
		if err := cb(err); err != nil {
			panic(err)
		}
	}
	return hashes, blobs
}

func randBlob(size int) (string, []byte) {
	blob := make([]byte, size)
	if _, err := rand.Read(blob); err != nil {
		panic(err)
	}
	return hashutil.Compute(blob), blob
}

func TestBlobsFilePutIdempotent(t *testing.T) {
	back, err := New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	defer back.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	h, blob := randBlob(512)
	for i := 0; i < 10; i++ {
		if err := back.Put(h, blob); err != nil {
			panic(err)
		}
	}
	stats, err := back.Stats()
	if err != nil {
		panic(err)
	}
	if stats.BlobsCount != 1 || stats.BlobsSize != 512 {
		t.Errorf("bad stats: %+v", stats)
	}
}

func TestBlobsFileBlobPutGetEnumerate(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	defer os.RemoveAll("./tmp_blobsfile_test")
	hashes, blobs := testBackendPutGetEnumerateReindexGetEnumerate(t, b, 100)
	b.Close()
	// Test we can still read everything when closing/reopening the blobsfile
	b, err = New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	prefixes := map[string][]string{}
	for _, h := range hashes {
		if _, ok := prefixes[h[0:2]]; !ok {
			prefixes[h[0:2]] = []string{}
		}
		prefixes[h[0:2]] = append(prefixes[h[0:2]], h)
	}
	testBackendEnumerate(t, b, hashes, "", "\xfe")
	for prefix, phashes := range prefixes {
		testBackendEnumerate2(t, b, phashes, prefix, "")
	}
	testBackendGet(t, b, hashes, blobs)
	if err := b.Close(); err != nil {
		panic(err)
	}
	// Try with the index and removed and test re-indexing
	b, err = New(&Opts{Directory: "./tmp_blobsfile_test", Compression: Snappy})
	check(err)
	if err := b.RebuildIndex(); err != nil {
		panic(err)
	}
	testBackendEnumerate(t, b, hashes, "", "\xfe")
	testBackendGet(t, b, hashes, blobs)
}

func backendPut(t *testing.T, b *BlobsFiles, blobsCount int) ([]string, [][]byte) {
	blobs := [][]byte{}
	hashes := []string{}
	// TODO(tsileo): 50 blobs if in short mode
	for i := 0; i < blobsCount; i++ {
		h, blob := randBlob(mrand.Intn(4000000-32) + 32)
		hashes = append(hashes, h)
		blobs = append(blobs, blob)
		if err := b.Put(h, blob); err != nil {
			panic(err)
		}
	}

	stats, err := b.Stats()
	if err != nil {
		panic(err)
	}
	fmt.Printf("stats=%+v\n", stats)

	return hashes, blobs
}

func testBackendPutGetEnumerate(t *testing.T, b *BlobsFiles, blobsCount int) ([]string, [][]byte) {
	hashes, blobs := backendPut(t, b, blobsCount)
	testBackendGet(t, b, hashes, blobs)
	testBackendEnumerate(t, b, hashes, "", "\xfe")
	return hashes, blobs
}

func testBackendPutGetEnumerateReindexGetEnumerate(t *testing.T, b *BlobsFiles, blobsCount int) ([]string, [][]byte) {
	hashes, blobs := backendPut(t, b, blobsCount)
	testBackendGet(t, b, hashes, blobs)
	testBackendEnumerate(t, b, hashes, "", "\xfe")
	if err := b.RebuildIndex(); err != nil {
		panic(err)
	}
	testBackendGet(t, b, hashes, blobs)
	testBackendEnumerate(t, b, hashes, "", "\xfe")
	return hashes, blobs
}

func testBackendGet(t *testing.T, b *BlobsFiles, hashes []string, blobs [][]byte) {
	blobsIndex := map[string]bool{}
	for _, blob := range blobs {
		blobsIndex[hashutil.Compute(blob)] = true
	}
	for _, h := range hashes {
		if _, err := b.Get(h); err != nil {
			panic(err)
		}
		_, ok := blobsIndex[h]
		if !ok {
			t.Errorf("blob %s should be index", h)
		}
		delete(blobsIndex, h)
	}
	if len(blobsIndex) > 0 {
		t.Errorf("index should have been emptied, got len %d", len(blobsIndex))
	}
}

func testBackendEnumerate2(t *testing.T, b *BlobsFiles, hashes []string, start, end string) []string {
	sort.Strings(hashes)
	bchan := make(chan *Blob)
	errc := make(chan error, 1)
	go func() {
		errc <- b.EnumeratePrefix(bchan, start, 0)
	}()
	enumHashes := []string{}
	for ref := range bchan {
		enumHashes = append(enumHashes, ref.Hash)
	}
	if err := <-errc; err != nil {
		panic(err)
	}
	if !sort.StringsAreSorted(enumHashes) {
		t.Errorf("enum hashes should already be sorted")
	}
	if !reflect.DeepEqual(hashes, enumHashes) {
		t.Errorf("bad enumerate results %q %q", hashes, enumHashes)
	}
	return enumHashes
}

func testBackendEnumerate(t *testing.T, b *BlobsFiles, hashes []string, start, end string) []string {
	sort.Strings(hashes)
	bchan := make(chan *Blob)
	errc := make(chan error, 1)
	go func() {
		errc <- b.Enumerate(bchan, start, end, 0)
	}()
	enumHashes := []string{}
	for ref := range bchan {
		enumHashes = append(enumHashes, ref.Hash)
	}
	if err := <-errc; err != nil {
		panic(err)
	}
	if !sort.StringsAreSorted(enumHashes) {
		t.Errorf("enum hashes should already be sorted")
	}
	if !reflect.DeepEqual(hashes, enumHashes) {
		t.Errorf("bad enumerate results %q %q", hashes, enumHashes)
	}
	return enumHashes
}

func TestBlobsFileBlobEncodingNoCompression(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	defer b.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	_, blob := randBlob(512)
	_, data := b.encodeBlob(blob, flagBlob)
	size, blob2, f := b.decodeBlob(data)
	if f != flagBlob {
		t.Errorf("bad flag, got %v, expected %v", f, flagBlob)
	}
	if size != 512 || !bytes.Equal(blob, blob2) {
		t.Errorf("Error blob encoding, got size:%v, expected:512, got blob:%v, expected:%v", size, blob2[:10], blob[:10])
	}
}

func TestBlobsFileBlobEncoding(t *testing.T) {
	b, err := New(&Opts{Directory: "./tmp_blobsfile_test"})
	check(err)
	defer b.Close()
	defer os.RemoveAll("./tmp_blobsfile_test")
	_, blob := randBlob(512)
	_, data := b.encodeBlob(blob, flagBlob)
	size, blob2, f := b.decodeBlob(data)
	if f != flagBlob {
		t.Errorf("bad flag, got %v, expected %v", f, flagBlob)
	}
	// Don't check the size are as the returned size is the size of the compressed blob
	if !bytes.Equal(blob, blob2) {
		t.Errorf("Error blob encoding, got size:%v, expected:512, got blob:%v, expected:%v", size, blob2[:10], blob[:10])
	}
	packs := b.SealedPacks()
	t.Logf("packs=%+v", packs)
}