forked from apache/falcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityUtilTest.java
482 lines (406 loc) · 20.4 KB
/
EntityUtilTest.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.falcon.entity;
import org.apache.falcon.Pair;
import org.apache.falcon.FalconException;
import org.apache.falcon.entity.parser.ClusterEntityParser;
import org.apache.falcon.entity.parser.EntityParserFactory;
import org.apache.falcon.entity.parser.ProcessEntityParser;
import org.apache.falcon.entity.v0.EntityType;
import org.apache.falcon.entity.v0.Frequency;
import org.apache.falcon.entity.v0.SchemaHelper;
import org.apache.falcon.entity.v0.feed.Feed;
import org.apache.falcon.entity.v0.feed.LateArrival;
import org.apache.falcon.entity.v0.feed.Property;
import org.apache.falcon.entity.v0.process.Cluster;
import org.apache.falcon.entity.v0.process.Process;
import org.apache.falcon.hadoop.HadoopClientFactory;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
/**
* Test for validating Entity util helper methods.
*/
public class EntityUtilTest extends AbstractTestBase {
private static TimeZone tz = TimeZone.getTimeZone("UTC");
@Test
public void testProcessView() throws Exception {
Process process = (Process) EntityType.PROCESS.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(PROCESS_XML));
Cluster cluster = new Cluster();
cluster.setName("newCluster");
cluster.setValidity(process.getClusters().getClusters().get(0).getValidity());
process.getClusters().getClusters().add(cluster);
Assert.assertEquals(process.getClusters().getClusters().size(), 2);
String currentCluster = process.getClusters().getClusters().get(0).getName();
Process newProcess = EntityUtil.getClusterView(process, currentCluster);
Assert.assertFalse(EntityUtil.equals(process, newProcess));
Assert.assertEquals(newProcess.getClusters().getClusters().size(), 1);
Assert.assertEquals(newProcess.getClusters().getClusters().get(0).getName(), currentCluster);
}
@Test
public void testFeedView() throws Exception {
Feed feed = (Feed) EntityType.FEED.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(FEED_XML));
Feed view = EntityUtil.getClusterView(feed, "testCluster");
Assert.assertEquals(view.getClusters().getClusters().size(), 1);
Assert.assertEquals(view.getClusters().getClusters().get(0).getName(), "testCluster");
view = EntityUtil.getClusterView(feed, "backupCluster");
Assert.assertEquals(view.getClusters().getClusters().size(), 2);
}
@Test
public void testEquals() throws Exception {
Process process1 = (Process) EntityType.PROCESS.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(PROCESS_XML));
Process process2 = (Process) EntityType.PROCESS.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(PROCESS_XML));
Assert.assertTrue(EntityUtil.equals(process1, process2));
Assert.assertTrue(EntityUtil.md5(process1).equals(EntityUtil.md5(process2)));
process2.getClusters().getClusters().get(0).getValidity().setEnd(
SchemaHelper.parseDateUTC("2013-04-21T00:00Z"));
Assert.assertFalse(EntityUtil.equals(process1, process2));
Assert.assertFalse(EntityUtil.md5(process1).equals(EntityUtil.md5(process2)));
Assert.assertTrue(EntityUtil.equals(process1, process2, new String[]{"clusters.clusters[\\d+].validity.end"}));
}
private static Date getDate(String date) throws Exception {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm Z");
return format.parse(date);
}
@Test
public void testGetNextStartTime() throws Exception {
Date now = getDate("2012-04-03 02:45 UTC");
Date start = getDate("2012-04-02 03:00 UTC");
Date newStart = getDate("2012-04-03 03:00 UTC");
Frequency frequency = new Frequency("hours(1)");
Assert.assertEquals(newStart, EntityUtil.getNextStartTime(start,
frequency, tz, now));
}
@Test
public void testgetNextStartTimeOld() throws Exception {
Date now = getDate("2012-05-02 02:45 UTC");
Date start = getDate("2012-02-01 03:00 UTC");
Date newStart = getDate("2012-05-02 03:00 UTC");
Frequency frequency = new Frequency("days(7)");
Assert.assertEquals(newStart, EntityUtil.getNextStartTime(start,
frequency, tz, now));
}
@Test
public void testGetNextStartTime2() throws Exception {
Date now = getDate("2010-05-02 04:45 UTC");
Date start = getDate("2010-02-01 03:00 UTC");
Date newStart = getDate("2010-05-03 03:00 UTC");
Frequency frequency = new Frequency("days(7)");
Assert.assertEquals(newStart, EntityUtil.getNextStartTime(start,
frequency, tz, now));
}
@Test
public void testGetNextStartTime3() throws Exception {
Date now = getDate("2010-05-02 04:45 UTC");
Date start = getDate("1980-02-01 03:00 UTC");
Date newStart = getDate("2010-05-07 03:00 UTC");
Frequency frequency = new Frequency("days(7)");
Assert.assertEquals(newStart, EntityUtil.getNextStartTime(start,
frequency, tz, now));
}
@Test
public void testGetInstanceSequence() throws Exception {
Date instance = getDate("2012-05-22 13:40 UTC");
Date start = getDate("2012-05-14 07:40 UTC");
Frequency frequency = new Frequency("hours(1)");
Assert.assertEquals(199, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence1() throws Exception {
Date instance = getDate("2012-05-22 12:40 UTC");
Date start = getDate("2012-05-14 07:40 UTC");
Frequency frequency = Frequency.fromString("hours(1)");
Assert.assertEquals(198, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence2() throws Exception {
Date instance = getDate("2012-05-22 12:41 UTC");
Date start = getDate("2012-05-14 07:40 UTC");
Frequency frequency = Frequency.fromString("hours(1)");
Assert.assertEquals(199, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence3() throws Exception {
Date instance = getDate("2010-01-02 01:01 UTC");
Date start = getDate("2010-01-02 01:00 UTC");
Frequency frequency = Frequency.fromString("minutes(1)");
Assert.assertEquals(2, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence4() throws Exception {
Date instance = getDate("2010-01-01 01:03 UTC");
Date start = getDate("2010-01-01 01:01 UTC");
Frequency frequency = Frequency.fromString("minutes(2)");
Assert.assertEquals(2, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence5() throws Exception {
Date instance = getDate("2010-01-01 02:01 UTC");
Date start = getDate("2010-01-01 01:01 UTC");
Frequency frequency = Frequency.fromString("hours(1)");
Assert.assertEquals(2, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence6() throws Exception {
Date instance = getDate("2010-01-01 01:04 UTC");
Date start = getDate("2010-01-01 01:01 UTC");
Frequency frequency = Frequency.fromString("minutes(3)");
Assert.assertEquals(2, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetInstanceSequence7() throws Exception {
Date instance = getDate("2010-01-01 01:03 UTC");
Date start = getDate("2010-01-01 01:01 UTC");
Frequency frequency = Frequency.fromString("minutes(1)");
Assert.assertEquals(3, EntityUtil.getInstanceSequence(start,
frequency, tz, instance));
}
@Test
public void testGetNextStartTimeMonthly() throws Exception {
Date startDate = getDate("2012-06-02 10:00 UTC");
Date nextAfter = getDate("2136-06-02 10:00 UTC");
Frequency frequency = Frequency.fromString("months(1)");
Date expectedResult = nextAfter;
Date result = EntityUtil.getNextStartTime(startDate, frequency, tz, nextAfter);
Assert.assertEquals(result, expectedResult);
}
@Test
public void testGetEntityStartEndDates() throws Exception {
Process process = (Process) EntityType.PROCESS.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(PROCESS_XML));
Cluster cluster = new Cluster();
cluster.setName("testCluster");
cluster.setValidity(process.getClusters().getClusters().get(0).getValidity());
process.getClusters().getClusters().add(cluster);
Date expectedStartDate = new SimpleDateFormat("yyyy-MM-dd z").parse("2011-11-02 UTC");
Date expectedEndDate = new SimpleDateFormat("yyyy-MM-dd z").parse("2091-12-30 UTC");
Pair<Date, Date> startEndDates = EntityUtil.getEntityStartEndDates(process);
Assert.assertEquals(startEndDates.first, expectedStartDate);
Assert.assertEquals(startEndDates.second, expectedEndDate);
}
@Test
public void testGetFeedProperties() {
Feed feed = new Feed();
org.apache.falcon.entity.v0.feed.Properties props = new org.apache.falcon.entity.v0.feed.Properties();
Property queue = new Property();
String name = "Q";
String value = "head of Q division!";
queue.setName(name);
queue.setValue(value);
props.getProperties().add(queue);
feed.setProperties(props);
Properties actual = EntityUtil.getEntityProperties(feed);
Assert.assertEquals(actual.size(), 1);
Assert.assertEquals(actual.getProperty(name), value);
}
@Test
public void testGetProcessProperties() {
org.apache.falcon.entity.v0.cluster.Cluster cluster = new org.apache.falcon.entity.v0.cluster.Cluster();
org.apache.falcon.entity.v0.cluster.Properties props = new org.apache.falcon.entity.v0.cluster.Properties();
org.apache.falcon.entity.v0.cluster.Property priority = new org.apache.falcon.entity.v0.cluster.Property();
String name = "priority";
String value = "Sister of Moriarity!";
priority.setName(name);
priority.setValue(value);
props.getProperties().add(priority);
cluster.setProperties(props);
Properties actual = EntityUtil.getEntityProperties(cluster);
Assert.assertEquals(actual.size(), 1);
Assert.assertEquals(actual.getProperty(name), value);
}
@Test
public void testGetClusterProperties() {
Process process = new Process();
org.apache.falcon.entity.v0.process.Properties props = new org.apache.falcon.entity.v0.process.Properties();
org.apache.falcon.entity.v0.process.Property priority = new org.apache.falcon.entity.v0.process.Property();
String name = "M";
String value = "Minions!";
priority.setName(name);
priority.setValue(value);
props.getProperties().add(priority);
process.setProperties(props);
Properties actual = EntityUtil.getEntityProperties(process);
Assert.assertEquals(actual.size(), 1);
Assert.assertEquals(actual.getProperty(name), value);
}
@Test
public void testGetLateProcessFeed() throws FalconException {
Feed feed = new Feed();
Assert.assertNull(EntityUtil.getLateProcess(feed));
LateArrival lateArrival = new LateArrival();
lateArrival.setCutOff(Frequency.fromString("days(1)"));
feed.setLateArrival(lateArrival);
Assert.assertNotNull(EntityUtil.getLateProcess(feed));
}
@Test(dataProvider = "NextInstanceExpressions")
public void testGetNextInstances(String instanceTimeStr, String frequencyStr, int instanceIncrementCount,
String expectedInstanceTimeStr) throws Exception {
Date instanceTime = getDate(instanceTimeStr);
Frequency frequency = Frequency.fromString(frequencyStr);
Date nextInstanceTime = EntityUtil.getNextInstanceTime(instanceTime, frequency, tz, instanceIncrementCount);
Assert.assertEquals(nextInstanceTime, getDate(expectedInstanceTimeStr));
}
@DataProvider(name = "NextInstanceExpressions")
public Object[][] nextInstanceExpressions() throws ParseException {
String instanceTimeStr = "2014-01-01 00:00 UTC";
return new Object[][] {
{instanceTimeStr, "minutes(1)", 1, "2014-01-01 00:01 UTC"},
{instanceTimeStr, "minutes(1)", 25, "2014-01-01 00:25 UTC"},
{instanceTimeStr, "hours(1)", 1, "2014-01-01 01:00 UTC"},
{instanceTimeStr, "hours(1)", 5, "2014-01-01 05:00 UTC"},
{instanceTimeStr, "days(1)", 1, "2014-01-02 00:00 UTC"},
{instanceTimeStr, "days(1)", 10, "2014-01-11 00:00 UTC"},
{instanceTimeStr, "months(1)", 1, "2014-02-01 00:00 UTC"},
{instanceTimeStr, "months(1)", 7, "2014-08-01 00:00 UTC"},
};
}
@Test(dataProvider = "bundlePaths")
public void testIsStagingPath(Path path, boolean createPath, boolean expected) throws Exception {
ClusterEntityParser parser = (ClusterEntityParser) EntityParserFactory.getParser(EntityType.CLUSTER);
InputStream stream = this.getClass().getResourceAsStream(CLUSTER_XML);
org.apache.falcon.entity.v0.cluster.Cluster cluster = parser.parse(stream);
ProcessEntityParser processParser = (ProcessEntityParser) EntityParserFactory.getParser(EntityType.PROCESS);
stream = this.getClass().getResourceAsStream(PROCESS_XML);
Process process = processParser.parse(stream);
FileSystem fs = HadoopClientFactory.get().
createFalconFileSystem(ClusterHelper.getConfiguration(cluster));
if (createPath && !fs.exists(path)) {
fs.create(path);
}
Assert.assertEquals(EntityUtil.isStagingPath(cluster, process, path), expected);
}
@DataProvider(name = "bundlePaths")
public Object[][] getBundlePaths() {
return new Object[][] {
{new Path("/projects/falcon/staging/ivory/workflows/process/sample/"), true, true},
{new Path("/projects/falcon/staging/falcon/workflows/process/sample/"), true, true},
{new Path("/projects/abc/falcon/workflows/process/sample/"), true, false},
{new Path("/projects/falcon/staging/falcon/workflows/process/test-process/"), false, false},
{new Path("/projects/falcon/staging/falcon/workflows/process/test-process/"), true, false},
};
}
@Test
public void testStringToProps() {
String testPropsString = "key1:value1,key2 : value2 , key3: value3, key4:value4:test";
Map<String, String> props = EntityUtil.getPropertyMap(testPropsString);
Assert.assertEquals(props.size(), 4);
for (int i = 1; i <= 3; i++) {
Assert.assertEquals(props.get("key" + i), "value" + i);
}
Assert.assertEquals(props.get("key4"), "value4:test");
}
@Test (expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = "Found invalid property .*",
dataProvider = "InvalidProps")
public void testInvalidStringToProps(String propString) {
String[] invalidProps = {"key1", "key1=value1", "key1:value1,key2=value2, :value"};
EntityUtil.getPropertyMap(propString);
}
@DataProvider(name = "InvalidProps")
public Object[][] getInvalidProps() {
return new Object[][]{
{"key1"},
{"key1=value1"},
{"key1:value1,key2=value2"},
{":value"},
};
}
@Test
public void testGetLatestStagingPath() throws FalconException, IOException {
ClusterEntityParser parser = (ClusterEntityParser) EntityParserFactory.getParser(EntityType.CLUSTER);
InputStream stream = this.getClass().getResourceAsStream(CLUSTER_XML);
org.apache.falcon.entity.v0.cluster.Cluster cluster = parser.parse(stream);
ProcessEntityParser processParser = (ProcessEntityParser) EntityParserFactory.getParser(EntityType.PROCESS);
stream = this.getClass().getResourceAsStream(PROCESS_XML);
Process process = processParser.parse(stream);
process.setName("staging-test");
String md5 = EntityUtil.md5(EntityUtil.getClusterView(process, "testCluster"));
FileSystem fs = HadoopClientFactory.get().
createFalconFileSystem(ClusterHelper.getConfiguration(cluster));
String basePath = "/projects/falcon/staging/falcon/workflows/process/staging-test/";
Path[] paths = {
new Path(basePath + "5a8100dc460b44db2e7bfab84b24cb92_1436441045003"),
new Path(basePath + "6b3a1b6c7cf9de62c78b125415ffb70c_1436504488677"),
new Path(basePath + md5 + "_1436344303117"),
new Path(basePath + md5 + "_1436347924846"),
new Path(basePath + md5 + "_1436357052992"),
new Path(basePath + "logs"),
new Path(basePath + "random_dir"),
};
// Ensure exception is thrown when there are no staging dirs.
fs.delete(new Path(basePath), true);
try {
EntityUtil.getLatestStagingPath(cluster, process);
Assert.fail("Exception expected");
} catch (FalconException e) {
// Do nothing
}
// Now create paths
for (Path path : paths) {
fs.create(path);
}
// Ensure latest is returned.
Assert.assertEquals(EntityUtil.getLatestStagingPath(cluster, process).getName(), md5 + "_1436357052992");
}
@Test
public void testIsClusterUsedByEntity() throws Exception {
Process process = (Process) EntityType.PROCESS.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(PROCESS_XML));
Feed feed = (Feed) EntityType.FEED.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(FEED_XML));
org.apache.falcon.entity.v0.cluster.Cluster cluster =
(org.apache.falcon.entity.v0.cluster.Cluster) EntityType.CLUSTER.getUnmarshaller().unmarshal(
getClass().getResourceAsStream(CLUSTER_XML));
Assert.assertTrue(EntityUtil.isEntityDependentOnCluster(cluster, "testCluster"));
Assert.assertTrue(EntityUtil.isEntityDependentOnCluster(feed, "testCluster"));
Assert.assertTrue(EntityUtil.isEntityDependentOnCluster(feed, "backupCluster"));
Assert.assertTrue(EntityUtil.isEntityDependentOnCluster(process, "testCluster"));
Assert.assertFalse(EntityUtil.isEntityDependentOnCluster(cluster, "fakeCluster"));
Assert.assertFalse(EntityUtil.isEntityDependentOnCluster(feed, "fakeCluster"));
Assert.assertFalse(EntityUtil.isEntityDependentOnCluster(process, "fakeCluster"));
}
@Test
public void testGetNextInstanceTimeWithDelay() throws Exception {
Date date = getDate("2016-08-10 03:00 UTC");
Frequency delay = new Frequency("hours(2)");
Date nextInstanceWithDelay = EntityUtil.getNextInstanceTimeWithDelay(date, delay, TimeZone.getTimeZone("UTC"));
Assert.assertEquals(nextInstanceWithDelay, getDate("2016-08-10 05:00 UTC"));
}
}