|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.streamnative.pulsar.handlers.kop.coordinator.transaction; |
| 15 | + |
| 16 | +import static org.mockito.ArgumentMatchers.any; |
| 17 | +import static org.mockito.ArgumentMatchers.anyString; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import org.apache.pulsar.metadata.api.GetResult; |
| 27 | +import org.apache.pulsar.metadata.api.MetadataStoreConfig; |
| 28 | +import org.apache.pulsar.metadata.api.Stat; |
| 29 | +import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; |
| 30 | +import org.apache.pulsar.metadata.impl.LocalMemoryMetadataStore; |
| 31 | +import org.testng.Assert; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +public class ProducerIdManagerImplTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void verifyThreadSafetyForTwoConcurrentNewProducerIdBlockCalls() throws Exception { |
| 38 | + // Initialize a fake metadata store such that the futures are not completed. This will allow |
| 39 | + // for low level control during this test. |
| 40 | + CompletableFuture<Optional<GetResult>> getFuture = new CompletableFuture<>(); |
| 41 | + CompletableFuture<Stat> completedPutFuture = new CompletableFuture<>(); |
| 42 | + // The value is not used, so mock with all "zero" values |
| 43 | + completedPutFuture.complete(new Stat("", 0, 0, 0, false, false)); |
| 44 | + |
| 45 | + MetadataStoreExtended mockedMetadataStore = mock(MetadataStoreExtended.class); |
| 46 | + when(mockedMetadataStore.get(anyString())).thenReturn(getFuture); |
| 47 | + when(mockedMetadataStore.put(anyString(), any(), any())).thenReturn(completedPutFuture); |
| 48 | + |
| 49 | + ProducerIdManagerImpl producerIdManager = new ProducerIdManagerImpl(1, mockedMetadataStore); |
| 50 | + // Trigger two calls to increase the producer id block. |
| 51 | + CompletableFuture<Void> firstNewBlock = producerIdManager.getNewProducerIdBlock(); |
| 52 | + CompletableFuture<Void> secondNewBlock = producerIdManager.getNewProducerIdBlock(); |
| 53 | + |
| 54 | + Assert.assertFalse(firstNewBlock.isDone()); |
| 55 | + Assert.assertFalse(secondNewBlock.isDone()); |
| 56 | + |
| 57 | + // Relies on the fact that completing the future also triggers the callbacks to run in same thread |
| 58 | + getFuture.complete(Optional.empty()); |
| 59 | + |
| 60 | + // Ensure that both calls completed |
| 61 | + Assert.assertTrue(firstNewBlock.isDone()); |
| 62 | + Assert.assertTrue(secondNewBlock.isDone()); |
| 63 | + Assert.assertFalse(firstNewBlock.isCompletedExceptionally()); |
| 64 | + Assert.assertFalse(secondNewBlock.isCompletedExceptionally()); |
| 65 | + |
| 66 | + // Ensure that the next producer id is the first value |
| 67 | + Assert.assertEquals(producerIdManager.generateProducerId().get().intValue(), 0, "The first id should be 0."); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void verifyProducerIdManagerForManyBrokersAndManyNewProducers() throws Exception { |
| 72 | + int expectedNumIds = 1000000; |
| 73 | + int numBrokers = 10; |
| 74 | + LocalMemoryMetadataStore metadataStore = |
| 75 | + new LocalMemoryMetadataStore("memory:localhost", MetadataStoreConfig.builder().build()); |
| 76 | + List<ProducerIdManagerImpl> producerIdManagers = new ArrayList<>(numBrokers); |
| 77 | + for (int i = 0; i < numBrokers; i++) { |
| 78 | + ProducerIdManagerImpl producerIdManager = new ProducerIdManagerImpl(i, metadataStore); |
| 79 | + producerIdManagers.add(producerIdManager); |
| 80 | + producerIdManager.initialize(); |
| 81 | + } |
| 82 | + |
| 83 | + List<CompletableFuture<Long>> futureIds = new ArrayList<>(expectedNumIds); |
| 84 | + |
| 85 | + for (int i = 0; i < expectedNumIds; i++) { |
| 86 | + for (ProducerIdManagerImpl producerIdManager : producerIdManagers) { |
| 87 | + futureIds.add(producerIdManager.generateProducerId()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + CompletableFuture.allOf(futureIds.toArray(new CompletableFuture[0])).get(); |
| 92 | + |
| 93 | + HashSet<Long> ids = new HashSet<>(); |
| 94 | + for (CompletableFuture<Long> futureId : futureIds) { |
| 95 | + Assert.assertTrue(ids.add(futureId.get()), String.format("Expected %d to be a unique id", futureId.get())); |
| 96 | + } |
| 97 | + Assert.assertEquals(ids.size(), expectedNumIds * numBrokers); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void tooManyConcurrentNewProducersShouldFail() throws Exception { |
| 102 | + long blockSize = ProducerIdManagerImpl.PID_BLOCK_SIZE; |
| 103 | + int brokerId = 1; |
| 104 | + // Initialize a fake metadata store such that the futures are not completed. This will allow |
| 105 | + // for low level control during this test. |
| 106 | + CompletableFuture<Optional<GetResult>> firstGetFuture = new CompletableFuture<>(); |
| 107 | + CompletableFuture<Optional<GetResult>> secondGetFuture = new CompletableFuture<>(); |
| 108 | + CompletableFuture<Stat> firstPutFuture = new CompletableFuture<>(); |
| 109 | + // The value is not used, and we mock the get results, so the put is essentially ignored |
| 110 | + firstPutFuture.complete(new Stat("", 0, 0, 0, false, false)); |
| 111 | + |
| 112 | + MetadataStoreExtended mockedMetadataStore = mock(MetadataStoreExtended.class); |
| 113 | + when(mockedMetadataStore.get(anyString())).thenReturn(firstGetFuture).thenReturn(secondGetFuture); |
| 114 | + when(mockedMetadataStore.put(anyString(), any(), any())).thenReturn(firstPutFuture); |
| 115 | + |
| 116 | + ProducerIdManagerImpl producerIdManager = new ProducerIdManagerImpl(brokerId, mockedMetadataStore); |
| 117 | + producerIdManager.initialize(); |
| 118 | + // Relies on the fact that completing the future also triggers the callbacks to run |
| 119 | + firstGetFuture.complete(Optional.empty()); |
| 120 | + List<CompletableFuture<Long>> futureIds = new ArrayList<>((int) blockSize + 1); |
| 121 | + |
| 122 | + // Create one blockSize worth of producer ids |
| 123 | + for (int i = 0; i < blockSize; i++) { |
| 124 | + Assert.assertEquals(producerIdManager.generateProducerId().get().intValue(), i); |
| 125 | + } |
| 126 | + |
| 127 | + // Now create callbacks for blockSize + 1 producer ids. |
| 128 | + for (int i = 0; i < blockSize + 1; i++) { |
| 129 | + futureIds.add(producerIdManager.generateProducerId()); |
| 130 | + } |
| 131 | + |
| 132 | + // Relies on the fact that completing the future also triggers the callbacks to run |
| 133 | + ProducerIdManagerImpl.ProducerIdBlock zeroBlock = ProducerIdManagerImpl.ProducerIdBlock |
| 134 | + .builder() |
| 135 | + .brokerId(brokerId) |
| 136 | + .blockStartId(0L) |
| 137 | + .blockEndId(ProducerIdManagerImpl.PID_BLOCK_SIZE - 1) |
| 138 | + .build(); |
| 139 | + // This stat is not actually used |
| 140 | + Stat stat = new Stat("", 0, 0, 0, false, false); |
| 141 | + GetResult result = new GetResult(ProducerIdManagerImpl.generateProducerIdBlockJson(zeroBlock), stat); |
| 142 | + secondGetFuture.complete(Optional.of(result)); |
| 143 | + |
| 144 | + int countFailed = 0; |
| 145 | + HashSet<Long> set = new HashSet<>(); |
| 146 | + for (CompletableFuture<Long> id : futureIds) { |
| 147 | + if (id.isDone()) { |
| 148 | + if (id.isCompletedExceptionally()) { |
| 149 | + countFailed++; |
| 150 | + } else { |
| 151 | + set.add(id.get()); |
| 152 | + } |
| 153 | + } else { |
| 154 | + Assert.fail(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + Assert.assertEquals(countFailed, 1, "Only one producer id should have failed"); |
| 159 | + Assert.assertEquals(set.size(), blockSize, "Ensures all ids are unique and that no extra ids were created."); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments