|
20 | 20 |
|
21 | 21 | import static org.mockito.ArgumentMatchers.any; |
22 | 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
23 | | -import static org.mockito.Mockito.spy; |
24 | 23 | import static org.mockito.Mockito.times; |
25 | 24 | import static org.mockito.Mockito.verify; |
26 | 25 | import static org.testng.AssertJUnit.assertEquals; |
|
29 | 28 | import static org.testng.AssertJUnit.assertNull; |
30 | 29 | import static org.testng.AssertJUnit.assertSame; |
31 | 30 | import static org.testng.AssertJUnit.assertTrue; |
| 31 | +import java.util.ArrayList; |
32 | 32 | import java.util.HashSet; |
33 | 33 | import java.util.List; |
34 | 34 | import java.util.Map; |
35 | 35 | import java.util.Optional; |
36 | 36 | import java.util.Set; |
37 | 37 | import java.util.UUID; |
38 | 38 | import java.util.concurrent.CompletableFuture; |
| 39 | +import java.util.concurrent.CompletionException; |
39 | 40 | import java.util.concurrent.ConcurrentHashMap; |
40 | 41 | import java.util.concurrent.CopyOnWriteArrayList; |
41 | 42 | import java.util.concurrent.ExecutionException; |
42 | 43 | import java.util.concurrent.ExecutorService; |
43 | 44 | import java.util.concurrent.Executors; |
44 | 45 | import java.util.concurrent.TimeUnit; |
45 | 46 | import java.util.concurrent.TimeoutException; |
| 47 | +import java.util.concurrent.atomic.AtomicBoolean; |
46 | 48 | import lombok.Cleanup; |
47 | 49 | import lombok.extern.slf4j.Slf4j; |
48 | 50 | import org.apache.commons.lang3.reflect.FieldUtils; |
@@ -353,69 +355,67 @@ public void testHandleNamespaceBeingDeleted() throws Exception { |
353 | 355 | service.deleteTopicPoliciesAsync(TOPIC1).get(); |
354 | 356 | } |
355 | 357 |
|
356 | | - @Test |
357 | | - public void testGetTopicPoliciesWithCleanCache() throws Exception { |
| 358 | + @Test(timeOut = 120_000) |
| 359 | + public void testGetTopicPoliciesWhileNamespaceIsUnloaded() throws Exception { |
| 360 | + // A namespace unload is what drops a namespace's cached topic policies in a broker: the policies are loaded |
| 361 | + // again by a new __change_events reader when the namespace is served again. Reads that run while the |
| 362 | + // namespace is unloaded over and over may fail with a retriable error while it is not served, but they must |
| 363 | + // never answer "no policies" (or other policies) for a topic that has a policy. The exact interleaving of a |
| 364 | + // read with the replacement of the cache is pinned by |
| 365 | + // testGetTopicPoliciesWhenBundleBounceReplacesPolicyCacheGeneration. |
358 | 366 | final String topic = "persistent://" + NAMESPACE1 + "/test" + UUID.randomUUID(); |
359 | | - pulsarClient.newProducer().topic(topic).create().close(); |
360 | | - |
361 | | - SystemTopicBasedTopicPoliciesService topicPoliciesService = |
362 | | - (SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService(); |
363 | | - |
364 | | - ConcurrentHashMap<TopicName, TopicPolicies> spyPoliciesCache = |
365 | | - spy(new ConcurrentHashMap<TopicName, TopicPolicies>()); |
366 | | - FieldUtils.writeDeclaredField(topicPoliciesService, "policiesCache", spyPoliciesCache, true); |
367 | | - |
368 | | - Awaitility.await().untilAsserted(() -> Assertions.assertThat( |
369 | | - TopicPolicyTestUtils.getTopicPolicies(topicPoliciesService, TopicName.get(topic))).isNull()); |
370 | | - |
| 367 | + final TopicName topicName = TopicName.get(topic); |
| 368 | + admin.topics().createNonPartitionedTopic(topic); |
371 | 369 | admin.topicPolicies().setMaxConsumersPerSubscription(topic, 1); |
372 | | - Awaitility.await().untilAsserted(() -> { |
373 | | - Assertions.assertThat(TopicPolicyTestUtils.getTopicPolicies(pulsar.getTopicPoliciesService(), |
374 | | - TopicName.get(topic))).isNotNull(); |
375 | | - }); |
376 | | - |
377 | | - Map<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> readers = |
378 | | - (Map<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>>) |
379 | | - FieldUtils.readDeclaredField(topicPoliciesService, "readerCaches", true); |
380 | | - |
381 | | - Mockito.doAnswer(invocation -> { |
382 | | - Thread.sleep(1000); |
383 | | - return invocation.callRealMethod(); |
384 | | - }).when(spyPoliciesCache).get(Mockito.any()); |
| 370 | + final TopicPoliciesService service = pulsar.getTopicPoliciesService(); |
| 371 | + Awaitility.await().untilAsserted(() -> Assertions.assertThat( |
| 372 | + TopicPolicyTestUtils.getTopicPolicies(service, topicName)).isNotNull()); |
385 | 373 |
|
386 | | - CompletableFuture<Void> result = new CompletableFuture<>(); |
387 | | - Thread thread = new Thread(() -> { |
| 374 | + final int unloads = 10; |
| 375 | + final AtomicBoolean unloading = new AtomicBoolean(true); |
| 376 | + final CompletableFuture<Void> unloader = CompletableFuture.runAsync(() -> { |
388 | 377 | try { |
389 | | - for (int i = 0; i < 10; i++) { |
390 | | - final var policies = TopicPolicyTestUtils.getTopicPolicies(topicPoliciesService, |
391 | | - TopicName.get(topic)); |
392 | | - if (policies == null) { |
393 | | - throw new Exception("null policies for " + i + "th get"); |
394 | | - } |
| 378 | + for (int i = 0; i < unloads; i++) { |
| 379 | + admin.namespaces().unload(NAMESPACE1); |
395 | 380 | } |
396 | | - result.complete(null); |
397 | | - } catch (Exception e) { |
398 | | - result.completeExceptionally(e); |
| 381 | + } catch (PulsarAdminException e) { |
| 382 | + throw new CompletionException(e); |
| 383 | + } finally { |
| 384 | + unloading.set(false); |
399 | 385 | } |
400 | 386 | }); |
401 | | - |
402 | | - Thread thread2 = new Thread(() -> { |
403 | | - for (int i = 0; i < 10; i++) { |
404 | | - CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = |
405 | | - readers.get(TopicName.get(topic).getNamespaceObject()); |
406 | | - if (readerCompletableFuture != null) { |
407 | | - readerCompletableFuture.join().closeAsync().join(); |
| 387 | + final List<String> wrongAnswers = new ArrayList<>(); |
| 388 | + final List<String> errors = new ArrayList<>(); |
| 389 | + int answered = 0; |
| 390 | + while (unloading.get()) { |
| 391 | + try { |
| 392 | + final Optional<TopicPolicies> policies = |
| 393 | + service.getTopicPoliciesAsync(topicName, TopicPoliciesService.GetType.LOCAL_ONLY) |
| 394 | + .get(30, TimeUnit.SECONDS); |
| 395 | + if (policies.isEmpty() |
| 396 | + || !Integer.valueOf(1).equals(policies.get().getMaxConsumersPerSubscription())) { |
| 397 | + wrongAnswers.add(String.valueOf(policies)); |
| 398 | + } else { |
| 399 | + answered++; |
408 | 400 | } |
| 401 | + } catch (ExecutionException e) { |
| 402 | + // Not served at this moment (the namespace is being unloaded); a caller retries. |
| 403 | + errors.add(String.valueOf(e.getCause())); |
409 | 404 | } |
410 | | - }); |
411 | | - |
412 | | - thread.start(); |
413 | | - thread2.start(); |
414 | | - |
415 | | - thread.join(); |
416 | | - thread2.join(); |
417 | | - |
418 | | - result.join(); |
| 405 | + } |
| 406 | + unloader.get(60, TimeUnit.SECONDS); |
| 407 | + |
| 408 | + Assertions.assertThat(wrongAnswers) |
| 409 | + .describedAs("reads answered without the topic's policy while its namespace was unloaded") |
| 410 | + .isEmpty(); |
| 411 | + Assertions.assertThat(answered) |
| 412 | + .describedAs("no read was answered while the namespace was unloaded, so the assertion above holds" |
| 413 | + + " vacuously; errors: %s", errors) |
| 414 | + .isPositive(); |
| 415 | + // Served again once the unloads are over. |
| 416 | + Awaitility.await().untilAsserted(() -> Assertions.assertThat( |
| 417 | + TopicPolicyTestUtils.getTopicPolicies(service, topicName).getMaxConsumersPerSubscription()) |
| 418 | + .isEqualTo(1)); |
419 | 419 | } |
420 | 420 |
|
421 | 421 | @Test |
|