This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
/
file_table_persister_test.go
231 lines (196 loc) · 6.11 KB
/
file_table_persister_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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package nbs
import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFSTableCacheOnOpen(t *testing.T) {
assert := assert.New(t)
dir := makeTempDir(t)
defer os.RemoveAll(dir)
names := []addr{}
cacheSize := 2
fc := newFDCache(cacheSize)
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
// Create some tables manually, load them into the cache, and then blow them away
func() {
for i := 0; i < cacheSize; i++ {
name, err := writeTableData(dir, []byte{byte(i)})
assert.NoError(err)
names = append(names, name)
}
for _, name := range names {
fts.Open(name, 1, nil)
}
removeTables(dir, names...)
}()
// Tables should still be cached, even though they're gone from disk
for i, name := range names {
src := fts.Open(name, 1, nil)
h := computeAddr([]byte{byte(i)})
assert.True(src.has(h))
}
// Kick a table out of the cache
name, err := writeTableData(dir, []byte{0xff})
assert.NoError(err)
fts.Open(name, 1, nil)
present := fc.reportEntries()
// Since 0 refcount entries are evicted randomly, the only thing we can validate is that fc remains at its target size
assert.Len(present, cacheSize)
}
func makeTempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
return dir
}
func writeTableData(dir string, chunx ...[]byte) (name addr, err error) {
var tableData []byte
tableData, name = buildTable(chunx)
err = ioutil.WriteFile(filepath.Join(dir, name.String()), tableData, 0666)
return
}
func removeTables(dir string, names ...addr) error {
for _, name := range names {
if err := os.Remove(filepath.Join(dir, name.String())); err != nil {
return err
}
}
return nil
}
func contains(s sort.StringSlice, e string) bool {
for _, c := range s {
if c == e {
return true
}
}
return false
}
func TestFSTablePersisterPersist(t *testing.T) {
assert := assert.New(t)
dir := makeTempDir(t)
defer os.RemoveAll(dir)
fc := newFDCache(defaultMaxTables)
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
src, err := persistTableData(fts, testChunks...)
assert.NoError(err)
if assert.True(src.count() > 0) {
buff, err := ioutil.ReadFile(filepath.Join(dir, src.hash().String()))
assert.NoError(err)
tr := newTableReader(parseTableIndex(buff), tableReaderAtFromBytes(buff), fileBlockSize)
assertChunksInReader(testChunks, tr, assert)
}
}
func persistTableData(p tablePersister, chunx ...[]byte) (src chunkSource, err error) {
mt := newMemTable(testMemTableSize)
for _, c := range chunx {
if !mt.addChunk(computeAddr(c), c) {
return nil, fmt.Errorf("memTable too full to add %s", computeAddr(c))
}
}
return p.Persist(mt, nil, &Stats{}), nil
}
func TestFSTablePersisterPersistNoData(t *testing.T) {
assert := assert.New(t)
mt := newMemTable(testMemTableSize)
existingTable := newMemTable(testMemTableSize)
for _, c := range testChunks {
assert.True(mt.addChunk(computeAddr(c), c))
assert.True(existingTable.addChunk(computeAddr(c), c))
}
dir := makeTempDir(t)
defer os.RemoveAll(dir)
fc := newFDCache(defaultMaxTables)
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
src := fts.Persist(mt, existingTable, &Stats{})
assert.True(src.count() == 0)
_, err := os.Stat(filepath.Join(dir, src.hash().String()))
assert.True(os.IsNotExist(err), "%v", err)
}
func TestFSTablePersisterCacheOnPersist(t *testing.T) {
assert := assert.New(t)
dir := makeTempDir(t)
fc := newFDCache(1)
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
defer os.RemoveAll(dir)
var name addr
func() {
src, err := persistTableData(fts, testChunks...)
assert.NoError(err)
name = src.hash()
removeTables(dir, name)
}()
// Table should still be cached, even though it's gone from disk
src := fts.Open(name, uint32(len(testChunks)), nil)
assertChunksInReader(testChunks, src, assert)
// Evict |name| from cache
src, err := persistTableData(fts, []byte{0xff})
assert.NoError(err)
present := fc.reportEntries()
// Since 0 refcount entries are evicted randomly, the only thing we can validate is that fc remains at its target size
assert.Len(present, 1)
}
func TestFSTablePersisterConjoinAll(t *testing.T) {
assert := assert.New(t)
assert.True(len(testChunks) > 1, "Whoops, this test isn't meaningful")
sources := make(chunkSources, len(testChunks))
dir := makeTempDir(t)
defer os.RemoveAll(dir)
fc := newFDCache(len(sources))
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
for i, c := range testChunks {
randChunk := make([]byte, (i+1)*13)
_, err := rand.Read(randChunk)
assert.NoError(err)
name, err := writeTableData(dir, c, randChunk)
assert.NoError(err)
sources[i] = fts.Open(name, 2, nil)
}
src := fts.ConjoinAll(sources, &Stats{})
if assert.True(src.count() > 0) {
buff, err := ioutil.ReadFile(filepath.Join(dir, src.hash().String()))
assert.NoError(err)
tr := newTableReader(parseTableIndex(buff), tableReaderAtFromBytes(buff), fileBlockSize)
assertChunksInReader(testChunks, tr, assert)
}
present := fc.reportEntries()
// Since 0 refcount entries are evicted randomly, the only thing we can validate is that fc remains at its target size
assert.Len(present, len(sources))
}
func TestFSTablePersisterConjoinAllDups(t *testing.T) {
assert := assert.New(t)
dir := makeTempDir(t)
defer os.RemoveAll(dir)
fc := newFDCache(defaultMaxTables)
defer fc.Drop()
fts := newFSTablePersister(dir, fc, nil)
reps := 3
sources := make(chunkSources, reps)
for i := 0; i < reps; i++ {
mt := newMemTable(1 << 10)
for _, c := range testChunks {
mt.addChunk(computeAddr(c), c)
}
sources[i] = fts.Persist(mt, nil, &Stats{})
}
src := fts.ConjoinAll(sources, &Stats{})
if assert.True(src.count() > 0) {
buff, err := ioutil.ReadFile(filepath.Join(dir, src.hash().String()))
assert.NoError(err)
tr := newTableReader(parseTableIndex(buff), tableReaderAtFromBytes(buff), fileBlockSize)
assertChunksInReader(testChunks, tr, assert)
assert.EqualValues(reps*len(testChunks), tr.count())
}
}