forked from voldemort/voldemort
/
DataCleanupJobTest.java
280 lines (243 loc) · 11.8 KB
/
DataCleanupJobTest.java
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 2008-2009 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package voldemort.scheduled;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;
import org.apache.commons.io.FileDeleteStrategy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import voldemort.MockTime;
import voldemort.TestUtils;
import voldemort.common.service.SchedulerService;
import voldemort.server.VoldemortConfig;
import voldemort.server.scheduler.DataCleanupJob;
import voldemort.server.storage.ScanPermitWrapper;
import voldemort.store.StorageEngine;
import voldemort.store.StoreDefinition;
import voldemort.store.bdb.BdbStorageConfiguration;
import voldemort.utils.ByteArray;
import voldemort.utils.EventThrottler;
import voldemort.utils.Props;
import voldemort.utils.SystemTime;
import voldemort.utils.Time;
import voldemort.utils.Utils;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Versioned;
@RunWith(Parameterized.class)
public class DataCleanupJobTest {
private MockTime time;
private StorageEngine<ByteArray, byte[], byte[]> engine;
private File storeDir;
private BdbStorageConfiguration bdbStorage;
private boolean prefixPartitionId;
public DataCleanupJobTest(boolean prefixPartitionId) {
this.prefixPartitionId = prefixPartitionId;
}
@Parameters
public static Collection<Object[]> modes() {
Object[][] data = new Object[][] { { true }, { false } };
return Arrays.asList(data);
}
@Before
public void setUp() throws Exception {
time = new MockTime();
storeDir = TestUtils.createTempDir();
FileDeleteStrategy.FORCE.delete(storeDir);
// lets use all the default values.
Props props = new Props();
props.put("node.id", 1);
props.put("voldemort.home", "test/common/voldemort/config");
VoldemortConfig voldemortConfig = new VoldemortConfig(props);
voldemortConfig.setBdbCacheSize(1024 * 1024);
voldemortConfig.setBdbOneEnvPerStore(true);
voldemortConfig.setBdbDataDirectory(storeDir.toURI().getPath());
voldemortConfig.setBdbPrefixKeysWithPartitionId(prefixPartitionId);
bdbStorage = new BdbStorageConfiguration(voldemortConfig);
StoreDefinition defA = TestUtils.makeStoreDefinition("cleanupTestStore");
engine = bdbStorage.getStore(defA, TestUtils.makeSingleNodeRoutingStrategy());
}
@After
public void tearDown() throws Exception {
try {
if(engine != null)
engine.close();
if(bdbStorage != null)
bdbStorage.close();
} finally {
FileDeleteStrategy.FORCE.delete(storeDir);
}
}
@Test
public void testCleanupFrequency() {
SchedulerService scheduler = new SchedulerService(1, time);
try {
Date now = new Date();
// clean up will purge everything older than last 2 seconds
Runnable cleanupJob = new DataCleanupJob<ByteArray, byte[], byte[]>(engine,
new ScanPermitWrapper(1),
2 * Time.MS_PER_SECOND,
SystemTime.INSTANCE,
new EventThrottler(1));
// and will run every 5 seconds starting now
scheduler.schedule("cleanup-freq-test", cleanupJob, now, 5 * Time.MS_PER_SECOND);
// load some data
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
engine.put(b, new Versioned<byte[]>(b.get()), null);
}
// sleep for 2 seconds
Thread.sleep(2 * Time.MS_PER_SECOND);
// None of the keys should have been deleted, i.e data cleanup
// should n't have run.
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Did not find key '" + i + "' in store!", found.size() > 0);
}
// wait till 4 seconds from start
Thread.sleep(System.currentTimeMillis() - (now.getTime() + 4 * Time.MS_PER_SECOND));
// load some more data
for(int i = 10; i < 20; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
engine.put(b, new Versioned<byte[]>(b.get()), null);
}
// give time for data cleanup to finally run
Thread.sleep(System.currentTimeMillis() - (now.getTime() + 6 * Time.MS_PER_SECOND));
// first batch of writes should have been deleted
for(int i = 0; i < 10; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Expected key '" + i + "' to be deleted!", found.size() == 0);
}
// and later ones retained.
for(int i = 10; i < 20; i++) {
ByteArray b = new ByteArray(Integer.toString(i).getBytes());
List<Versioned<byte[]>> found = engine.get(b, null);
assertTrue("Expected key '" + i + "' to be retained!", found.size() > 0);
}
} catch(Exception e) {
} finally {
scheduler.stop();
}
}
@Test
public void testCleanupCleansUp() {
time.setTime(123);
put("a", "b", "c");
time.setTime(123 + Time.MS_PER_DAY + 1);
put("d", "e", "f");
assertContains("a", "b", "c", "d", "e", "f");
// update a single item to bump its vector clock time
put("a");
// now run cleanup
new DataCleanupJob<ByteArray, byte[], byte[]>(engine,
new ScanPermitWrapper(1),
Time.MS_PER_DAY,
time,
new EventThrottler(1)).run();
// Check that all the later keys are there AND the key updated later
assertContains("a", "d", "e", "f");
}
public void testCleanupStartTime() {
// Make sure the default is always the next day.
GregorianCalendar cal = new GregorianCalendar();
assertEquals("Default is not tomorrow",
Utils.getDayOfTheWeekFromNow(1),
(cal.get(Calendar.DAY_OF_WEEK) + 1) % 7);
// When starting the server any day in the week from SUN to FRI and
// targeting a saturday, should always start on the next saturday
GregorianCalendar expectedStart = TestUtils.getCalendar(2012,
Calendar.SEPTEMBER,
29,
0,
0,
0);
Random rand = new Random();
for(int day = Calendar.SUNDAY; day <= Calendar.FRIDAY; day++) {
GregorianCalendar serverStartTime = TestUtils.getCalendar(2012,
Calendar.SEPTEMBER,
22 + day,
rand.nextInt(24),
rand.nextInt(60),
rand.nextInt(60));
GregorianCalendar computedStart = Utils.getCalendarForNextRun(serverStartTime,
Calendar.SATURDAY,
0);
assertEquals("Expected :" + expectedStart.getTimeInMillis() + " Computed: "
+ computedStart.getTimeInMillis(),
expectedStart.getTimeInMillis(),
computedStart.getTimeInMillis());
}
// Targeting saturday, 00:00 and starting on a friday 23:59:59 should
// start the next saturday
GregorianCalendar serverStartTime = TestUtils.getCalendar(2012,
Calendar.SEPTEMBER,
28,
23,
59,
59);
GregorianCalendar computedStart = Utils.getCalendarForNextRun(serverStartTime,
Calendar.SATURDAY,
0);
assertEquals("Expected :" + expectedStart.getTimeInMillis() + " Computed: "
+ computedStart.getTimeInMillis(),
expectedStart.getTimeInMillis(),
computedStart.getTimeInMillis());
// If we start past the start hour on the target day, it should start
// the next week
serverStartTime = TestUtils.getCalendar(2012, Calendar.SEPTEMBER, 29, 1, 0, 1);
computedStart = Utils.getCalendarForNextRun(serverStartTime, Calendar.SATURDAY, 0);
assertEquals(Calendar.SATURDAY, computedStart.get(Calendar.DAY_OF_WEEK));
assertEquals(serverStartTime.get(Calendar.DAY_OF_YEAR) + 7,
computedStart.get(Calendar.DAY_OF_YEAR));
}
private void put(String... items) {
for(String item: items) {
VectorClock clock = null;
List<Versioned<byte[]>> found = engine.get(new ByteArray(item.getBytes()), null);
if(found.size() == 0) {
clock = new VectorClock(time.getMilliseconds());
} else if(found.size() == 1) {
VectorClock oldClock = (VectorClock) found.get(0).getVersion();
clock = oldClock.incremented(0, time.getMilliseconds());
} else {
fail("Found multiple versions.");
}
engine.put(new ByteArray(item.getBytes()),
new Versioned<byte[]>(item.getBytes(), clock),
null);
}
}
private void assertContains(String... keys) {
for(String key: keys) {
List<Versioned<byte[]>> found = engine.get(new ByteArray(key.getBytes()), null);
assertTrue("Did not find key '" + key + "' in store!", found.size() > 0);
}
}
}