|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hedwig.server.topics; |
| 19 | + |
| 20 | +import java.util.concurrent.LinkedBlockingQueue; |
| 21 | +import java.util.concurrent.SynchronousQueue; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | +import java.util.concurrent.atomic.AtomicInteger; |
| 24 | + |
| 25 | +import org.apache.hedwig.client.conf.ClientConfiguration; |
| 26 | +import org.apache.hedwig.client.HedwigClient; |
| 27 | +import org.apache.hedwig.client.api.Publisher; |
| 28 | +import org.apache.hedwig.client.api.Subscriber; |
| 29 | +import org.apache.hedwig.exceptions.PubSubException; |
| 30 | +import org.apache.hedwig.protocol.PubSubProtocol.SubscribeRequest.CreateOrAttach; |
| 31 | +import org.apache.hedwig.server.HedwigHubTestBase; |
| 32 | +import org.apache.hedwig.util.Callback; |
| 33 | +import org.apache.hedwig.util.ConcurrencyUtils; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import com.google.protobuf.ByteString; |
| 37 | + |
| 38 | +public class TestConcurrentTopicAcquisition extends HedwigHubTestBase { |
| 39 | + |
| 40 | + // Client variables |
| 41 | + protected HedwigClient client; |
| 42 | + protected Publisher publisher; |
| 43 | + protected Subscriber subscriber; |
| 44 | + |
| 45 | + final LinkedBlockingQueue<ByteString> subscribers = |
| 46 | + new LinkedBlockingQueue<ByteString>(); |
| 47 | + final ByteString topic = ByteString.copyFromUtf8("concurrent-topic"); |
| 48 | + final int numSubscribers = 300; |
| 49 | + final AtomicInteger numDone = new AtomicInteger(0); |
| 50 | + |
| 51 | + // SynchronousQueues to verify async calls |
| 52 | + private final SynchronousQueue<Boolean> queue = new SynchronousQueue<Boolean>(); |
| 53 | + |
| 54 | + class SubCallback implements Callback<Void> { |
| 55 | + |
| 56 | + ByteString subId; |
| 57 | + |
| 58 | + public SubCallback(ByteString subId) { |
| 59 | + this.subId = subId; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void operationFinished(Object ctx, |
| 64 | + Void resultOfOperation) { |
| 65 | + if (logger.isDebugEnabled()) { |
| 66 | + logger.debug("subscriber " + subId.toStringUtf8() + " succeed."); |
| 67 | + } |
| 68 | + int done = numDone.incrementAndGet(); |
| 69 | + if (done == numSubscribers) { |
| 70 | + ConcurrencyUtils.put(queue, false); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void operationFailed(Object ctx, |
| 76 | + PubSubException exception) { |
| 77 | + if (logger.isDebugEnabled()) { |
| 78 | + logger.debug("subscriber " + subId.toStringUtf8() + " failed : ", exception); |
| 79 | + } |
| 80 | + ConcurrencyUtils.put(subscribers, subId); |
| 81 | + // ConcurrencyUtils.put(queue, false); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void setUp() throws Exception { |
| 87 | + super.setUp(); |
| 88 | + client = new HedwigClient(new ClientConfiguration()); |
| 89 | + |
| 90 | + publisher = client.getPublisher(); |
| 91 | + subscriber = client.getSubscriber(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void tearDown() throws Exception { |
| 96 | + // sub.interrupt(); |
| 97 | + // sub.join(); |
| 98 | + |
| 99 | + client.close(); |
| 100 | + super.tearDown(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testTopicAcquistion() throws Exception { |
| 105 | + logger.info("Start concurrent topic acquistion test."); |
| 106 | + |
| 107 | + // let one bookie down to cause not enough bookie exception |
| 108 | + logger.info("Tear down one bookie server."); |
| 109 | + bktb.tearDownOneBookieServer(); |
| 110 | + |
| 111 | + // In current implementation, the first several subscriptions will succeed to put topic in topic manager set, |
| 112 | + // because the tear down bookie server's zk node need time to disappear |
| 113 | + // some subscriptions will create ledger successfully, then other subscriptions will fail. |
| 114 | + // the race condition will be: topic manager own topic but persistence manager doesn't |
| 115 | + |
| 116 | + // 300 subscribers subscribe to a same topic |
| 117 | + final AtomicBoolean inRedirectLoop = new AtomicBoolean(false); |
| 118 | + numDone.set(0); |
| 119 | + for (int i=0; i<numSubscribers; i++) { |
| 120 | + ByteString subId = ByteString.copyFromUtf8("sub-" + i); |
| 121 | + if (logger.isDebugEnabled()) { |
| 122 | + logger.debug("subscriber " + subId.toStringUtf8() + " subscribes topic " + topic.toStringUtf8()); |
| 123 | + } |
| 124 | + subscriber.asyncSubscribe(topic, subId, CreateOrAttach.CREATE_OR_ATTACH, |
| 125 | + new Callback<Void>() { |
| 126 | + |
| 127 | + private void tick() { |
| 128 | + if (numDone.incrementAndGet() == numSubscribers) { |
| 129 | + ConcurrencyUtils.put(queue, true); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void operationFinished(Object ctx, |
| 135 | + Void resultOfOperation) { |
| 136 | + tick(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void operationFailed(Object ctx, |
| 141 | + PubSubException exception) { |
| 142 | + if (exception instanceof PubSubException.ServiceDownException) { |
| 143 | + String msg = exception.getMessage(); |
| 144 | + if (msg.indexOf("ServerRedirectLoopException") > 0) { |
| 145 | + inRedirectLoop.set(true); |
| 146 | + } |
| 147 | + if (logger.isDebugEnabled()) { |
| 148 | + logger.debug("Operation failed : ", exception); |
| 149 | + } |
| 150 | + } |
| 151 | + tick(); |
| 152 | + } |
| 153 | + |
| 154 | + }, |
| 155 | + null); |
| 156 | + } |
| 157 | + |
| 158 | + queue.take(); |
| 159 | + |
| 160 | + // TODO: remove comment after we fix the issue |
| 161 | + // Assert.assertEquals(false, inRedirectLoop.get()); |
| 162 | + |
| 163 | + // start a thread to send subscriptions |
| 164 | + numDone.set(0); |
| 165 | + Thread sub = new Thread(new Runnable() { |
| 166 | + |
| 167 | + @Override |
| 168 | + public void run() { |
| 169 | + logger.info("sub thread started"); |
| 170 | + try { |
| 171 | + // 100 subscribers subscribe to a same topic |
| 172 | + for (int i=0; i<numSubscribers; i++) { |
| 173 | + ByteString subscriberId = ByteString.copyFromUtf8("sub-" + i); |
| 174 | + subscribers.put(subscriberId); |
| 175 | + } |
| 176 | + |
| 177 | + ByteString subId; |
| 178 | + while (true) { |
| 179 | + subId = subscribers.take(); |
| 180 | + |
| 181 | + if (logger.isDebugEnabled()) { |
| 182 | + logger.debug("subscriber " + subId.toStringUtf8() + " subscribes topic " + topic.toStringUtf8()); |
| 183 | + } |
| 184 | + subscriber.asyncSubscribe(topic, subId, CreateOrAttach.CREATE_OR_ATTACH, |
| 185 | + new SubCallback(subId), null); |
| 186 | + } |
| 187 | + // subscriber.asyncSubscribe(topic, subscriberId, mode, callback, context) |
| 188 | + } catch (InterruptedException ie) { |
| 189 | + // break |
| 190 | + logger.warn("Interrupted : ", ie); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + }); |
| 195 | + sub.start(); |
| 196 | + Thread.sleep(2000); |
| 197 | + |
| 198 | + // start a new bookie server |
| 199 | + logger.info("start new bookie server"); |
| 200 | + bktb.startUpNewBookieServer(); |
| 201 | + |
| 202 | + // hope that all the subscriptions will be OK |
| 203 | + queue.take(); |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments