This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
SidelineSpoutHandlerTest.java
767 lines (611 loc) · 32 KB
/
SidelineSpoutHandlerTest.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
* Copyright (c) 2017, 2018, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* * Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.storm.spout.sideline.handler;
import com.salesforce.storm.spout.dynamic.ConsumerPartition;
import com.salesforce.storm.spout.dynamic.DelegateSpout;
import com.salesforce.storm.spout.dynamic.DynamicSpout;
import com.salesforce.storm.spout.dynamic.FactoryManager;
import com.salesforce.storm.spout.dynamic.VirtualSpoutFactory;
import com.salesforce.storm.spout.dynamic.consumer.ConsumerPeerContext;
import com.salesforce.storm.spout.dynamic.filter.FilterChainStep;
import com.salesforce.storm.spout.dynamic.metrics.LogRecorder;
import com.salesforce.storm.spout.sideline.SidelineVirtualSpoutIdentifier;
import com.salesforce.storm.spout.dynamic.VirtualSpout;
import com.salesforce.storm.spout.dynamic.VirtualSpoutIdentifier;
import com.salesforce.storm.spout.dynamic.kafka.KafkaConsumerConfig;
import com.salesforce.storm.spout.dynamic.config.SpoutConfig;
import com.salesforce.storm.spout.dynamic.mocks.MockConsumer;
import com.salesforce.storm.spout.sideline.config.SidelineConfig;
import com.salesforce.storm.spout.dynamic.filter.NegatingFilterChainStep;
import com.salesforce.storm.spout.dynamic.filter.StaticMessageFilter;
import com.salesforce.storm.spout.dynamic.mocks.MockTopologyContext;
import com.salesforce.storm.spout.sideline.persistence.InMemoryPersistenceAdapter;
import com.salesforce.storm.spout.sideline.persistence.PersistenceAdapter;
import com.salesforce.storm.spout.sideline.persistence.SidelinePayload;
import com.salesforce.storm.spout.sideline.trigger.SidelineRequest;
import com.salesforce.storm.spout.sideline.trigger.SidelineRequestIdentifier;
import com.salesforce.storm.spout.sideline.trigger.SidelineType;
import com.salesforce.storm.spout.sideline.trigger.StaticTrigger;
import org.apache.storm.task.TopologyContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test that {@link SidelineSpoutHandler} opens, closes and resumes sidelines correctly.
*/
public class SidelineSpoutHandlerTest {
private static String CONSUMER_ID_PREFIX = "VirtualSpoutPrefix";
/**
* Test the open method properly stores the spout's config.
*/
@Test
public void testOpen() {
final Map<String, Object> config = getConfig();
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
assertEquals(
config,
sidelineSpoutHandler.getSpoutConfig()
);
// close things out.
sidelineSpoutHandler.close();
}
/**
* Test upon spout opening that we have a firehose virtual spout instance.
*/
@Test
public void testOnSpoutOpenCreatesFirehose() {
final Map<String, Object> config = getConfig();
final PersistenceAdapter persistenceAdapter = new InMemoryPersistenceAdapter();
persistenceAdapter.open(config);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
assertNotNull(sidelineSpoutHandler.getFireHoseSpout());
// close things out.
sidelineSpoutHandler.close();
spout.close();
}
/**
* Test upon spout opening sidelines, both starts and stops are resumed properly.
*
* This test has one started sideline and one stopped sideline, both of which should resume correctly.
*/
@Test
public void testOnSpoutOpenResumesSidelines() {
final Map<String, Object> config = getConfig();
final String namespace = "Test";
final SidelineRequestIdentifier startRequestId = new SidelineRequestIdentifier("StartRequest");
final StaticMessageFilter startFilter = new StaticMessageFilter();
final SidelineRequest startRequest = new SidelineRequest(startRequestId, startFilter);
final SidelineRequestIdentifier stopRequestId = new SidelineRequestIdentifier("StopRequest");
final StaticMessageFilter stopFilter = new StaticMessageFilter();
final SidelineRequest stopRequest = new SidelineRequest(stopRequestId, stopFilter);
final VirtualSpoutIdentifier virtualSpoutIdentifier2 = new SidelineVirtualSpoutIdentifier(
CONSUMER_ID_PREFIX,
stopRequestId
);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
final PersistenceAdapter persistenceAdapter = sidelineSpoutHandler.getPersistenceAdapter();
// Make a starting request that we expect to resume
persistenceAdapter.persistSidelineRequestState(
SidelineType.START,
startRequestId,
startRequest,
new ConsumerPartition(namespace, 0),
1L,
2L
);
// Make a stopping request that we expect to resume
persistenceAdapter.persistSidelineRequestState(
SidelineType.RESOLVE,
stopRequestId,
stopRequest,
new ConsumerPartition(namespace, 1),
3L,
4L
);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
// Make sure we have a firehose
assertNotNull(sidelineSpoutHandler.getFireHoseSpout(), "Firehose should not be null");
// Our firehose should have gotten a filter chain step on resume
assertEquals(
1,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size(),
"FilterChain should have one step for the start request"
);
// When we fetch that step by startRequestId it should be our start filter
assertEquals(
startFilter,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().get(startRequestId),
"FilterChain should have the start filter on it"
);
assertTrue(
spout.hasVirtualSpout(virtualSpoutIdentifier2),
"The stop request should have created a new VirtualSpout instance"
);
// close things out.
sidelineSpoutHandler.close();
spout.close();
}
/**
* Test that when a sideline is started the firehose gets a new filter from the sideline request.
*/
@Test
public void testStartSidelining() {
final Map<String, Object> config = getConfig();
final String namespace = MockConsumer.topic;
final SidelineRequestIdentifier startRequestId = new SidelineRequestIdentifier("StartRequest");
final StaticMessageFilter startFilter = new StaticMessageFilter();
final SidelineRequest startRequest = new SidelineRequest(startRequestId, startFilter);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
final PersistenceAdapter persistenceAdapter = sidelineSpoutHandler.getPersistenceAdapter();
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
// Tell our mock consumer that these are the partitions we're working with
MockConsumer.partitions = Arrays.asList(0, 5);
// Finally, start a sideline with out given request
sidelineSpoutHandler.start(startRequest);
assertTrue(
sidelineSpoutHandler.isStarted(startRequest),
"Sideline should be started"
);
// Make sure we have a firehose
assertNotNull(sidelineSpoutHandler.getFireHoseSpout());
// Our firehose should have gotten a filter chain step on resume
assertEquals(1, sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size());
// When we fetch that step by startRequestId it should be our start filter
assertEquals(
startFilter,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().get(startRequestId)
);
final SidelinePayload partition0 = persistenceAdapter.retrieveSidelineRequest(startRequestId, new ConsumerPartition(namespace, 0));
assertEquals(SidelineType.START, partition0.type);
assertEquals(startRequestId, partition0.id);
assertEquals(startRequest, partition0.request);
assertEquals(Long.valueOf(1L), partition0.startingOffset);
assertNull(partition0.endingOffset);
final SidelinePayload partition5 = persistenceAdapter.retrieveSidelineRequest(startRequestId, new ConsumerPartition(namespace, 5));
assertEquals(SidelineType.START, partition5.type);
assertEquals(startRequestId, partition5.id);
assertEquals(startRequest, partition5.request);
assertEquals(Long.valueOf(1L), partition5.startingOffset);
assertNull(partition5.endingOffset);
// close things out.
sidelineSpoutHandler.close();
spout.close();
// Put this back to our default
MockConsumer.partitions = Arrays.asList(1);
}
/**
* Test resuming a sideline.
*/
@Test
public void testResumeSidelining() {
final Map<String, Object> config = getConfig();
final String namespace = MockConsumer.topic;
// Wondering why 1? Checkout the MockConsumer and the partitions that it starts with.
final int partitionId = 1;
final StaticMessageFilter filter = new StaticMessageFilter();
final SidelineRequestIdentifier requestId = new SidelineRequestIdentifier("SidelineRequest");
final SidelineRequest request = new SidelineRequest(requestId, filter);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
final PersistenceAdapter persistenceAdapter = sidelineSpoutHandler.getPersistenceAdapter();
// Note that we are doing this AFTER the spout has opened because we are NOT testing the resume logic
persistenceAdapter.persistSidelineRequestState(
SidelineType.START,
requestId,
request,
new ConsumerPartition(namespace, partitionId),
1L, // starting offset
null // ending offset
);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
// In case stuff opens up really fast, we want to wait until the VirtualSpout has a current state before
// attempting to resolve a sideline.
await()
.atMost(5, TimeUnit.SECONDS)
.until(() -> sidelineSpoutHandler.getFireHoseSpout().getCurrentState(), notNullValue());
sidelineSpoutHandler.resume(request);
assertTrue(
sidelineSpoutHandler.isResumed(request),
"Sideline should be in the resume state"
);
assertEquals(
2,
spout.getTotalVirtualSpouts(),
"We should have two virtual spouts, the fire hose and the sideline"
);
assertEquals(
1,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size(),
"Firehose should still have it's filter for the tenant being sidelined"
);
final DelegateSpout sidelineVirtualSpout = spout.getVirtualSpout(new SidelineVirtualSpoutIdentifier(CONSUMER_ID_PREFIX, requestId));
assertNotNull(
sidelineVirtualSpout,
"We should have a virtual spout for the sideline"
);
assertEquals(
1,
sidelineVirtualSpout.getFilterChain().getSteps().size(),
"Sideline spout should have one filter"
);
assertEquals(
new NegatingFilterChainStep(sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getStep(requestId)),
sidelineVirtualSpout.getFilterChain().getStep(requestId),
"The negation of the firehose's filter should be the filter on the sideline spout"
);
assertNull(sidelineVirtualSpout.getEndingState(), "Ending state should be null");
final SidelinePayload sidelinePayload = persistenceAdapter.retrieveSidelineRequest(
requestId,
new ConsumerPartition(namespace, partitionId)
);
assertEquals(SidelineType.RESUME, sidelinePayload.type, "Sideline type should match");
assertEquals(request.id, sidelinePayload.id, "Sideline request id should match");
assertEquals(
request.step,
sidelinePayload.request.step,
"Sideline payload should be the same as on the fire hose, it's negated at runtime by the handler"
);
assertEquals(Long.valueOf(1L), sidelinePayload.startingOffset, "Sideline starting offset should be at 1");
assertNull(sidelinePayload.endingOffset, "Sideline ending offset should not be set");
// Close everything up
sidelineSpoutHandler.close();
spout.close();
}
/**
* Test that when a sideline is resolved, the filter is removed from the firehose and the virtual spout is provided with an
* ending offset.
*/
@Test
public void testResolveSidelining() {
final Map<String, Object> config = getConfig();
final String namespace = MockConsumer.topic;
final SidelineRequestIdentifier stopRequestId = new SidelineRequestIdentifier("StopRequest");
final FilterChainStep stopFilter = new NegatingFilterChainStep(new StaticMessageFilter());
final SidelineRequest stopRequest = new SidelineRequest(stopRequestId, stopFilter);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
final PersistenceAdapter persistenceAdapter = sidelineSpoutHandler.getPersistenceAdapter();
// Persist our stop request as a start, so that when we stop it, it can be found
// Note that we are doing this AFTER the spout has opened because we are NOT testing the resume logic
persistenceAdapter.persistSidelineRequestState(
SidelineType.RESUME,
stopRequestId,
stopRequest,
new ConsumerPartition(namespace, 0),
1L, // starting offset
null // ending offset
);
persistenceAdapter.persistSidelineRequestState(
SidelineType.RESUME,
stopRequestId,
stopRequest,
new ConsumerPartition(namespace, 5),
3L, // starting offset
null // ending offset
);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
// In case stuff opens up really fast, we want to wait until the VirtualSpout has a current state before
// attempting to resolve a sideline.
await()
.atMost(5, TimeUnit.SECONDS)
.until(() -> sidelineSpoutHandler.getFireHoseSpout().getCurrentState(), notNullValue());
// Tell our mock consumer that these are the partitions we're working with
MockConsumer.partitions = Arrays.asList(0, 5);
// Finally, start a sideline with out given request
sidelineSpoutHandler.resolve(stopRequest);
assertTrue(
sidelineSpoutHandler.isResolving(stopRequest),
"Sideline should be stopped"
);
// Firehose no longer has any filters
assertEquals(
0,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size()
);
final SidelinePayload partition0 = persistenceAdapter.retrieveSidelineRequest(stopRequestId, new ConsumerPartition(namespace, 0));
assertEquals(SidelineType.RESOLVE, partition0.type);
assertEquals(stopRequestId, partition0.id);
assertEquals(stopRequest.step, partition0.request.step);
assertEquals(Long.valueOf(1L), partition0.startingOffset);
assertEquals(Long.valueOf(1L), partition0.endingOffset);
final SidelinePayload partition5 = persistenceAdapter.retrieveSidelineRequest(stopRequestId, new ConsumerPartition(namespace, 5));
assertEquals(SidelineType.RESOLVE, partition5.type);
assertEquals(stopRequestId, partition5.id);
assertEquals(stopRequest.step, partition5.request.step);
assertEquals(Long.valueOf(3L), partition5.startingOffset);
assertEquals(Long.valueOf(1L), partition5.endingOffset);
// close things out.
sidelineSpoutHandler.close();
spout.close();
// Put this back to normal
MockConsumer.partitions = Arrays.asList(1);
}
/**
* Test upon spout closing the triggers are cleaned up.
*/
@Test
public void testOnSpoutClose() {
final Map<String, Object> config = getConfig();
final PersistenceAdapter persistenceAdapter = new InMemoryPersistenceAdapter();
persistenceAdapter.open(config);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
assertNotNull(sidelineSpoutHandler.getSidelineTriggers());
assertEquals(1, sidelineSpoutHandler.getSidelineTriggers().size());
assertTrue(sidelineSpoutHandler.getSidelineTriggers().get(0) instanceof StaticTrigger);
sidelineSpoutHandler.onSpoutClose(spout);
assertEquals(0, sidelineSpoutHandler.getSidelineTriggers().size());
// close things out.
sidelineSpoutHandler.close();
spout.close();
}
/**
* Test that we get a runtime exception when we configure a class that doesn't utilize our interfaces.
*/
@Test
public void testMisconfiguredCreateSidelineTriggers() {
final Map<String, Object> config = getConfig();
// Override our trigger class with one that does not actually exist.
config.put(SidelineConfig.TRIGGER_CLASS, "FooBar" + System.currentTimeMillis());
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
Assertions.assertThrows(RuntimeException.class, () ->
sidelineSpoutHandler.createSidelineTriggers()
);
// close things out.
sidelineSpoutHandler.close();
}
/**
* Test that the handler creates proper virtual spout identifiers.
*/
@Test
public void testGenerateVirtualSpoutId() {
final SidelineRequestIdentifier expectedSidelineRequestIdentifier = new SidelineRequestIdentifier("SidelineRequestIdentifier");
// Create our config, specify the consumer id because it will be used as a prefix
final Map<String, Object> config = getConfig();
// Create a persistence adapter, this is called in the handler onSpoutOpen() method, we're just trying to avoid a NullPointer here
final PersistenceAdapter persistenceAdapter = new InMemoryPersistenceAdapter();
persistenceAdapter.open(config);
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
// Create our handler
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
final VirtualSpoutIdentifier virtualSpoutIdentifier = sidelineSpoutHandler.generateSidelineVirtualSpoutId(
expectedSidelineRequestIdentifier
);
assertTrue(virtualSpoutIdentifier instanceof SidelineVirtualSpoutIdentifier);
final SidelineVirtualSpoutIdentifier sidelineVirtualSpoutIdentifier = (SidelineVirtualSpoutIdentifier) virtualSpoutIdentifier;
assertEquals(CONSUMER_ID_PREFIX, sidelineVirtualSpoutIdentifier.getConsumerId());
assertEquals(expectedSidelineRequestIdentifier, sidelineVirtualSpoutIdentifier.getSidelineRequestIdentifier());
// close things out.
sidelineSpoutHandler.close();
spout.close();
}
/**
* Test that periodically calling {@link SidelineSpoutHandler#loadSidelines()} will restore {@link VirtualSpout}'s and
* {@link com.salesforce.storm.spout.dynamic.filter.FilterChainStep}'s after calling
* {@link SidelineSpoutHandler#onSpoutOpen(DynamicSpout, Map, TopologyContext)}, and that when spouts and filters are removed
* they are properly restored back onto the spout.
*/
@Test
public void testLoadSidelines() {
final Map<String, Object> config = getConfig();
final String namespace = "Test";
final DynamicSpout spout = new DynamicSpout(config);
spout.open(null, new MockTopologyContext(), null);
final VirtualSpoutFactory virtualSpoutFactory = getVirtualSpoutFactory(config);
final SidelineSpoutHandler sidelineSpoutHandler = new SidelineSpoutHandler();
sidelineSpoutHandler.open(config, virtualSpoutFactory);
sidelineSpoutHandler.onSpoutOpen(spout, new HashMap(), new MockTopologyContext());
assertTrue(
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().isEmpty(),
"There should not be any filters on the firehose"
);
// The firehose has to move from the queue to a SpoutRunner, and this happens in a separate thread so we wait a bit.
await()
.until(spout::getTotalVirtualSpouts, equalTo(1));
assertEquals(1, spout.getTotalVirtualSpouts(), "Only the firehose should be on the spout");
final SidelineRequestIdentifier startRequestId = new SidelineRequestIdentifier("StartRequest");
final StaticMessageFilter startFilter = new StaticMessageFilter();
final SidelineRequest startRequest = new SidelineRequest(startRequestId, startFilter);
final SidelineRequestIdentifier stopRequestId = new SidelineRequestIdentifier("StopRequest");
final StaticMessageFilter stopFilter = new StaticMessageFilter();
final SidelineRequest stopRequest = new SidelineRequest(stopRequestId, stopFilter);
final PersistenceAdapter persistenceAdapter = sidelineSpoutHandler.getPersistenceAdapter();
// Make a starting request that we expect to be loaded
persistenceAdapter.persistSidelineRequestState(
SidelineType.START,
startRequestId,
startRequest,
new ConsumerPartition(namespace, 0),
1L,
2L
);
// Make a stopping request that we expect to be loaded
persistenceAdapter.persistSidelineRequestState(
SidelineType.RESOLVE,
stopRequestId,
stopRequest,
new ConsumerPartition(namespace, 1),
3L,
4L
);
final VirtualSpoutIdentifier startRequestVirtualSpoutIdentifier = new SidelineVirtualSpoutIdentifier(
CONSUMER_ID_PREFIX,
startRequestId
);
final VirtualSpoutIdentifier stopRequestVirtualSpoutIdentifier = new SidelineVirtualSpoutIdentifier(
CONSUMER_ID_PREFIX,
stopRequestId
);
// Reload the sidelines, this is normally done via a thread on an interval - we're going to validate it behaves correctly now
sidelineSpoutHandler.loadSidelines();
assertEquals(
1,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size(),
"There should be a filter on the firehose for our start request"
);
assertTrue(
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().hasStep(startRequestId),
"Start request should be on the filter chain"
);
assertFalse(
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().hasStep(stopRequestId),
"Stop request should not be on the filter chain"
);
// A new sideline spout should be added for our stopped request
await()
.until(spout::getTotalVirtualSpouts, equalTo(2));
assertTrue(
spout.hasVirtualSpout(stopRequestVirtualSpoutIdentifier),
"Spout should have a VirtualSpout for the stop request"
);
assertFalse(
spout.hasVirtualSpout(startRequestVirtualSpoutIdentifier),
"Spout should not have a VirtualSpout for the start request"
);
// Call this again, basically we want to be able to call this without messing with state and nothing else should change,
// so we'll validate that number of vspouts stays the same and the filter chain stays the same too.
sidelineSpoutHandler.loadSidelines();
assertEquals(
2,
spout.getTotalVirtualSpouts(),
"Spout should have two virtual spouts"
);
assertTrue(
spout.hasVirtualSpout(sidelineSpoutHandler.getFireHoseSpoutIdentifier()),
"Spout should have the firehose VirtualSpout"
);
assertTrue(
spout.hasVirtualSpout(stopRequestVirtualSpoutIdentifier),
"Spout should have a VirtualSpout for the stop request"
);
assertFalse(
spout.hasVirtualSpout(startRequestVirtualSpoutIdentifier),
"Spout should not have a VirtualSpout for the start request"
);
assertEquals(
1,
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().getSteps().size(),
"Firehose only has 1 filter"
);
// Now we're going to mess with the state of the spout and filter chain and see if things reload properly
spout.removeVirtualSpout(stopRequestVirtualSpoutIdentifier);
spout.removeVirtualSpout(sidelineSpoutHandler.getFireHoseSpoutIdentifier());
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().removeStep(startRequestId);
assertFalse(
spout.hasVirtualSpout(stopRequestVirtualSpoutIdentifier),
"Spout should not have a VirtualSpout for the stop request"
);
assertFalse(
spout.hasVirtualSpout(sidelineSpoutHandler.getFireHoseSpoutIdentifier()),
"Spout should not have a VirtualSpout for the firehose"
);
assertFalse(
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().hasStep(startRequestId),
"Start request should not be on the filter chain"
);
// The missing spout and filter chain step should be restored after this
sidelineSpoutHandler.loadSidelines();
assertTrue(
// Note that this will be in the starting queue, not in the runners at this point and that's OK for this test
spout.hasVirtualSpout(stopRequestVirtualSpoutIdentifier),
"Spout should have a VirtualSpout for the stop request"
);
assertTrue(
spout.hasVirtualSpout(sidelineSpoutHandler.getFireHoseSpoutIdentifier()),
"Spout should have a VirtualSpout for the firehose"
);
assertTrue(
sidelineSpoutHandler.getFireHoseSpout().getFilterChain().hasStep(startRequestId),
"Start request should be on the filter chain"
);
// Note that I would love to mess with the filter chain on an existing VirtualSpout that isn't the FireHose, but there's no real way
// to get to the other spouts right now. This may not even be relevant either, because we never manipulate the sideline's filter
// chains after they've been opened (whereas we do with the firehose). In fact, it's not even possible to do because of the
// aforementioned lack of accessibility to non-firehose spouts.
sidelineSpoutHandler.close();
spout.close();
}
private VirtualSpoutFactory getVirtualSpoutFactory(final Map<String,Object> config) {
return new VirtualSpoutFactory(config, new ConsumerPeerContext(0, 1), new FactoryManager(config), new LogRecorder());
}
private Map<String, Object> getConfig() {
final Map<String, Object> config = SpoutConfig.setDefaults(SidelineConfig.setDefaults(new HashMap<>()));
config.put(KafkaConsumerConfig.CONSUMER_ID_PREFIX, CONSUMER_ID_PREFIX);
config.put(KafkaConsumerConfig.KAFKA_TOPIC, "KafkaTopic");
config.put(
SpoutConfig.PERSISTENCE_ADAPTER_CLASS,
com.salesforce.storm.spout.dynamic.persistence.InMemoryPersistenceAdapter.class.getName()
);
config.put(
SidelineConfig.PERSISTENCE_ADAPTER_CLASS,
InMemoryPersistenceAdapter.class.getName()
);
config.put(SidelineConfig.TRIGGER_CLASS, StaticTrigger.class.getName());
config.put(SpoutConfig.CONSUMER_CLASS, MockConsumer.class.getName());
return config;
}
}