|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.subscription.it.local; |
| 21 | + |
| 22 | +import org.apache.iotdb.isession.ISession; |
| 23 | +import org.apache.iotdb.it.env.EnvFactory; |
| 24 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 25 | +import org.apache.iotdb.itbase.category.LocalStandaloneIT; |
| 26 | +import org.apache.iotdb.rpc.subscription.config.TopicConstant; |
| 27 | +import org.apache.iotdb.rpc.subscription.exception.SubscriptionOwnerFencedException; |
| 28 | +import org.apache.iotdb.session.subscription.SubscriptionTreeSession; |
| 29 | +import org.apache.iotdb.session.subscription.consumer.tree.SubscriptionTreePullConsumer; |
| 30 | +import org.apache.iotdb.session.subscription.payload.SubscriptionMessage; |
| 31 | +import org.apache.iotdb.session.subscription.payload.SubscriptionRecordHandler; |
| 32 | +import org.apache.iotdb.subscription.it.IoTDBSubscriptionITConstant; |
| 33 | + |
| 34 | +import org.apache.tsfile.read.query.dataset.ResultSet; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Ignore; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.experimental.categories.Category; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | + |
| 42 | +import java.time.Duration; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Properties; |
| 46 | +import java.util.concurrent.atomic.AtomicReference; |
| 47 | + |
| 48 | +@RunWith(IoTDBTestRunner.class) |
| 49 | +@Category({LocalStandaloneIT.class}) |
| 50 | +public class IoTDBSubscriptionTopicOwnerIT extends AbstractSubscriptionLocalIT { |
| 51 | + |
| 52 | + @Override |
| 53 | + @Before |
| 54 | + public void setUp() throws Exception { |
| 55 | + super.setUp(); |
| 56 | + } |
| 57 | + |
| 58 | + @Ignore |
| 59 | + @Test |
| 60 | + public void testTopicOwnerFencingRejectsStaleOwnerAndAllowsCurrentOwner() throws Exception { |
| 61 | + final String host = EnvFactory.getEnv().getIP(); |
| 62 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 63 | + final String topicName = "topic_owner_fencing"; |
| 64 | + |
| 65 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 66 | + session.open(); |
| 67 | + final Properties properties = new Properties(); |
| 68 | + properties.put(TopicConstant.PATH_KEY, "root.topic_owner.**"); |
| 69 | + properties.put(TopicConstant.START_TIME_KEY, "0"); |
| 70 | + properties.put(TopicConstant.OWNER_ID_KEY, "sn2"); |
| 71 | + properties.put(TopicConstant.OWNER_EPOCH_KEY, "6"); |
| 72 | + session.createTopic(topicName, properties); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + try (final SubscriptionTreePullConsumer staleOwnerConsumer = |
| 77 | + new SubscriptionTreePullConsumer.Builder() |
| 78 | + .host(host) |
| 79 | + .port(port) |
| 80 | + .consumerId("stale_sn") |
| 81 | + .consumerGroupId("topic_owner_group") |
| 82 | + .ownerId("sn1") |
| 83 | + .ownerEpoch(5L) |
| 84 | + .autoCommit(false) |
| 85 | + .buildPullConsumer()) { |
| 86 | + staleOwnerConsumer.open(); |
| 87 | + Assert.assertThrows( |
| 88 | + SubscriptionOwnerFencedException.class, () -> staleOwnerConsumer.subscribe(topicName)); |
| 89 | + } |
| 90 | + |
| 91 | + try (final SubscriptionTreePullConsumer currentOwnerConsumer = |
| 92 | + new SubscriptionTreePullConsumer.Builder() |
| 93 | + .host(host) |
| 94 | + .port(port) |
| 95 | + .consumerId("current_sn") |
| 96 | + .consumerGroupId("topic_owner_group") |
| 97 | + .ownerId("sn2") |
| 98 | + .ownerEpoch(6L) |
| 99 | + .autoCommit(false) |
| 100 | + .buildPullConsumer()) { |
| 101 | + currentOwnerConsumer.open(); |
| 102 | + currentOwnerConsumer.subscribe(topicName); |
| 103 | + |
| 104 | + insertData(); |
| 105 | + |
| 106 | + final AtomicReference<List<SubscriptionMessage>> polledMessages = |
| 107 | + new AtomicReference<>(Collections.emptyList()); |
| 108 | + IoTDBSubscriptionITConstant.AWAIT.untilAsserted( |
| 109 | + () -> { |
| 110 | + final List<SubscriptionMessage> messages = |
| 111 | + currentOwnerConsumer.poll(Duration.ofMillis(1000)); |
| 112 | + polledMessages.set(messages); |
| 113 | + Assert.assertFalse(messages.isEmpty()); |
| 114 | + Assert.assertTrue(countRows(messages) > 0); |
| 115 | + }); |
| 116 | + |
| 117 | + currentOwnerConsumer.commitSync(polledMessages.get()); |
| 118 | + currentOwnerConsumer.unsubscribe(topicName); |
| 119 | + } |
| 120 | + } finally { |
| 121 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 122 | + session.open(); |
| 123 | + session.dropTopicIfExists(topicName); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void insertData() throws Exception { |
| 129 | + try (final ISession session = EnvFactory.getEnv().getSessionConnection()) { |
| 130 | + for (int i = 0; i < 10; i++) { |
| 131 | + session.executeNonQueryStatement( |
| 132 | + String.format("insert into root.topic_owner.d1(time, s1) values (%s, %s)", i, i)); |
| 133 | + } |
| 134 | + session.executeNonQueryStatement("flush"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static int countRows(final List<SubscriptionMessage> messages) throws Exception { |
| 139 | + int rowCount = 0; |
| 140 | + for (final SubscriptionMessage message : messages) { |
| 141 | + for (final ResultSet resultSet : message.getResultSets()) { |
| 142 | + while (((SubscriptionRecordHandler.SubscriptionResultSet) resultSet).hasNext()) { |
| 143 | + resultSet.next(); |
| 144 | + rowCount++; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return rowCount; |
| 149 | + } |
| 150 | +} |
0 commit comments