This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathserver_test.go
280 lines (258 loc) · 11.2 KB
/
server_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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package pilosa_test
import (
"context"
"fmt"
"reflect"
"sort"
"testing"
"time"
pilosa "github.com/featurebasedb/featurebase/v3"
"github.com/featurebasedb/featurebase/v3/test"
)
func TestViewsRemovalTTL(t *testing.T) {
cluster := test.MustRunCluster(t, 1)
node := cluster.GetNode(0)
defer cluster.Close()
// Create a client
client := node.Client()
indexName := cluster.Idx()
// Create indexes and field with ttl lasting 24 hours
if err := client.CreateIndex(context.Background(), indexName, pilosa.IndexOptions{TrackExistence: true}); err != nil && err != pilosa.ErrIndexExists {
t.Fatalf("creating index, err: %v", err)
}
dateNow := time.Now().UTC()
dateYesterday := dateNow.Add(-24 * time.Hour)
dateCurrentMonth := time.Date(dateNow.Year(), dateNow.Month(), 1, 0, 0, 0, 0, time.UTC)
dateLastDayOfMonth := dateCurrentMonth.AddDate(0, 1, 0).Add(-time.Nanosecond)
var tests = []struct {
name string
date string
expViews []string
}{
{
name: "date_old",
date: "2001-02-03T04:05",
expViews: []string{"standard"},
/* date_old (2001-02-03T04:05), this will create these views:
- standard
- standard_2001
- standard_200102
- standard_20010203
- standard_2001020304
Since the sample date here is over 20 years all views except "standard" should get deleted
*/
},
{
name: "date_now",
date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), dateNow.Hour(), dateNow.Minute()),
expViews: []string{
"standard",
"standard_" + fmt.Sprintf("%d", dateNow.Year()),
"standard_" + fmt.Sprintf("%d%02d", dateNow.Year(), dateNow.Month()),
"standard_" + fmt.Sprintf("%d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day()),
"standard_" + fmt.Sprintf("%d%02d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), dateNow.Hour()),
},
/* For example: current time is 2022-05-11T15:17 (also when the 24 hrs ttl countdown starts) will generate these views:
- standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
- standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_20220511 -> end date is 2022_05_12 T00:00, in future -> keep
- standard_2022051115 -> end date is 2022_05_11 T18:00, in future -> keep
*/
},
{
name: "date_yesterday",
date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateYesterday.Year(), dateYesterday.Month(), dateYesterday.Day(), dateYesterday.Hour(), dateYesterday.Minute()),
expViews: []string{
"standard",
"standard_" + fmt.Sprintf("%d", dateYesterday.Year()),
"standard_" + fmt.Sprintf("%d%02d", dateYesterday.Year(), dateYesterday.Month()),
"standard_" + fmt.Sprintf("%d%02d%02d", dateYesterday.Year(), dateYesterday.Month(), dateYesterday.Day()),
"standard_" + fmt.Sprintf("%d%02d%02d%02d", dateYesterday.Year(), dateYesterday.Month(), dateYesterday.Day(), dateYesterday.Hour()),
},
/* Example: current time is 2022-05-11T15:17, date_yesterday (2022-05-10T15:17) will generate these views:
- standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
- standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_20220510 -> end date is 2022_05_11 T00:00, within 24 hrs -> keep
- standard_2022051015 -> end date is 2022_05_10 T16:00, within 24 hrs -> keep
*/
},
// {
// name: "date_first_of_month",
// date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateCurrentMonth.Year(), dateCurrentMonth.Month(), 1, 0, 0),
// expViews: []string{
// "standard",
// "standard_" + fmt.Sprintf("%d", dateCurrentMonth.Year()),
// "standard_" + fmt.Sprintf("%d%02d", dateCurrentMonth.Year(), dateCurrentMonth.Month()),
// },
// /* Example: current time is 2022-05-11T15:17, date_first_of_month (2022-05-01T00:00) will generate these views:
// - standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
// - standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
// - standard_20220501 -> end date is 2022_05_02 T00:00, older than ttl -> delete
// - standard_2022050115 -> end date is 2022_05_01 T01:00, older than ttl -> delete
// !!! commenting this test out for now, there is a special case where if today's date is also same as date_first_of_month
// in that case, the expected views would have all 4, instead of 2 in the above example, since they are not older than the 24 hr ttl
// */
// },
{
name: "date_last_of_month",
date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateLastDayOfMonth.Year(), dateLastDayOfMonth.Month(), dateLastDayOfMonth.Day(), dateLastDayOfMonth.Hour(), dateLastDayOfMonth.Minute()),
expViews: []string{
"standard",
"standard_" + fmt.Sprintf("%d", dateLastDayOfMonth.Year()),
"standard_" + fmt.Sprintf("%d%02d", dateLastDayOfMonth.Year(), dateLastDayOfMonth.Month()),
"standard_" + fmt.Sprintf("%d%02d%02d", dateNow.Year(), dateNow.Month(), dateLastDayOfMonth.Day()),
"standard_" + fmt.Sprintf("%d%02d%02d%02d", dateNow.Year(), dateNow.Month(), dateLastDayOfMonth.Day(), dateLastDayOfMonth.Hour()),
},
/* Example: current time is 2022-05-11T15:17, date_last_of_month (2022-05-31T23:59) will generate these views:
- standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
- standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_20220531 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_2022053123 -> end date is 2022_06_01 T00:00, in future -> keep
*/
},
{
name: "date_first_hour_day",
date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), 0, 0),
expViews: []string{
"standard",
"standard_" + fmt.Sprintf("%d", dateNow.Year()),
"standard_" + fmt.Sprintf("%d%02d", dateNow.Year(), dateNow.Month()),
"standard_" + fmt.Sprintf("%d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day()),
"standard_" + fmt.Sprintf("%d%02d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), 0),
},
/* Example: current time is 2022-05-11T15:17, date_first_hour_day (2022-05-11T00:00) will generate these views:
- standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
- standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_20220511 -> end date is 2022_05_12 T00:00, in future -> keep
- standard_2022051100 -> end date is 2022_05_01 T01:00, within TTL -> keep
*/
},
{
name: "date_last_hour_day",
date: fmt.Sprintf("%d-%02d-%02dT%02d:%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), 23, 59),
expViews: []string{
"standard",
"standard_" + fmt.Sprintf("%d", dateNow.Year()),
"standard_" + fmt.Sprintf("%d%02d", dateNow.Year(), dateNow.Month()),
"standard_" + fmt.Sprintf("%d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day()),
"standard_" + fmt.Sprintf("%d%02d%02d%02d", dateNow.Year(), dateNow.Month(), dateNow.Day(), 23),
},
/* Example: current time is 2022-05-11T15:17, date_last_hour_day (2022-05-11T23:59) will generate these views:
- standard_2022 -> end date is 2023_01_01 T00:00, in future -> keep
- standard_202205 -> end date is 2022_06_01 T00:00, in future -> keep
- standard_20220511 -> end date is 2022_05_12 T00:00, in future -> keep
- standard_2022051123 -> end date is 2022_05_12 T00:00, in future -> keep
*/
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := client.CreateFieldWithOptions(context.Background(), indexName, test.name, pilosa.FieldOptions{TTL: time.Hour * 24, Type: pilosa.FieldTypeTime, TimeQuantum: "YMDH"}); err != nil {
t.Fatalf("creating field, err: %v", err)
}
// set data
_, err := client.Query(context.Background(), indexName, &pilosa.QueryRequest{Index: indexName, Query: "Set(1, " + test.name + "=1, " + test.date + ")"})
if err != nil {
t.Fatalf("setting sample data, err: %v", err)
}
// run ViewsRemoval
node.Server.ViewsRemoval(context.Background())
// Get all the views for given index + field
views, err := node.API.Views(context.Background(), indexName, test.name)
if err != nil {
t.Fatal(err)
}
var viewNames []string
for _, view := range views {
viewNames = append(viewNames, view.Name())
}
sort.Strings(viewNames)
if !reflect.DeepEqual(test.expViews, viewNames) {
t.Fatalf("after ttl removal, expected %v, but got %v", test.expViews, viewNames)
}
})
}
}
func TestViewsRemovalStandard(t *testing.T) {
cluster := test.MustRunCluster(t, 1)
node := cluster.GetNode(0)
defer cluster.Close()
// Create a client
client := node.Client()
indexName := cluster.Idx()
// Create indexes and field with ttl lasting 24 hours
if err := client.CreateIndex(context.Background(), indexName, pilosa.IndexOptions{TrackExistence: true}); err != nil && err != pilosa.ErrIndexExists {
t.Fatalf("creating index, err: %v", err)
}
var tests = []struct {
name string
date string
noStandardView string
expViews []string
}{
{
name: "t1_delete_standard",
date: "2001-02-03T04:05",
noStandardView: "true",
expViews: nil,
/* date 2001-02-03T04:05, this will create these views:
- standard
- standard_2001
- standard_200102
- standard_20010203
- standard_2001020304
Since the sample date here is over 20 years, all views with dates should get deleted
Since noStandardView is true, 'standard' view should get deleted
*/
},
{
name: "t2_keep_standard",
date: "2001-02-03T04:05",
noStandardView: "false",
expViews: []string{"standard"},
/* date 2001-02-03T04:05, this will create these views:
- standard
- standard_2001
- standard_200102
- standard_20010203
- standard_2001020304
Since the sample date here is over 20 years, all views with dates should get deleted
Since noStandardView is false, 'standard' view should NOT get deleted
*/
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := client.CreateFieldWithOptions(context.Background(), indexName, test.name, pilosa.FieldOptions{TTL: time.Hour * 24, Type: pilosa.FieldTypeTime, TimeQuantum: "YMDH"}); err != nil {
t.Fatalf("creating field, err: %v", err)
}
// set data
_, err := client.Query(context.Background(), indexName, &pilosa.QueryRequest{Index: indexName, Query: "Set(1, " + test.name + "=1, " + test.date + ")"})
if err != nil {
t.Fatalf("setting sample data, err: %v", err)
}
// update noStandardView value
err = node.API.UpdateField(context.Background(), indexName, test.name, pilosa.FieldUpdate{Option: "noStandardView", Value: test.noStandardView})
if err != nil {
t.Fatalf("updating noStandardView, err: %v", err)
}
// run ViewsRemoval
node.Server.ViewsRemoval(context.Background())
// get all the views for given index + field
views, err := node.API.Views(context.Background(), indexName, test.name)
if err != nil {
t.Fatal(err)
}
var viewNames []string
for _, view := range views {
viewNames = append(viewNames, view.Name())
}
sort.Strings(viewNames)
if !reflect.DeepEqual(test.expViews, viewNames) {
t.Fatalf("after ttl removal, expected %v, but got %v", test.expViews, viewNames)
}
})
}
}