|
| 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.cluster; |
| 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.ClusterIT; |
| 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.model.Topic; |
| 31 | +import org.apache.iotdb.subscription.it.AbstractSubscriptionIT; |
| 32 | +import org.apache.iotdb.subscription.it.IoTDBSubscriptionITConstant; |
| 33 | + |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.experimental.categories.Category; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | + |
| 41 | +import java.util.Properties; |
| 42 | + |
| 43 | +/** |
| 44 | + * Owner-fencing correctness under owner/DataNode/ConfigNode partition topologies, simulated at the |
| 45 | + * node-process level via the IT framework (shutdown/start of ConfigNodes and DataNodes; ConfigNode |
| 46 | + * failover via leader shutdown). Runs on a 3 ConfigNode + 3 DataNode cluster. |
| 47 | + * |
| 48 | + * <p>Covers: |
| 49 | + * |
| 50 | + * <ul> |
| 51 | + * <li>multi-DataNode: a stale owner is fenced for a topic whose data spans several DataNodes; |
| 52 | + * <li>ConfigNode failover: the owner epoch is persisted via consensus, so the new leader keeps |
| 53 | + * fencing the stale owner after the previous leader is shut down; |
| 54 | + * <li>DataNode restart: after a DataNode is shut down and restarted, the owner metadata is |
| 55 | + * re-synced and the stale owner stays fenced. |
| 56 | + * </ul> |
| 57 | + */ |
| 58 | +@RunWith(IoTDBTestRunner.class) |
| 59 | +@Category({ClusterIT.class}) |
| 60 | +public class IoTDBSubscriptionTopicOwnerPartitionIT extends AbstractSubscriptionIT { |
| 61 | + |
| 62 | + private static final String HEAD_PATH = "root.topic_owner_partition"; |
| 63 | + |
| 64 | + @Override |
| 65 | + @Before |
| 66 | + public void setUp() throws Exception { |
| 67 | + super.setUp(); |
| 68 | + EnvFactory.getEnv() |
| 69 | + .getConfig() |
| 70 | + .getCommonConfig() |
| 71 | + .setSubscriptionEnabled(true) |
| 72 | + .setPipeMemoryManagementEnabled(false) |
| 73 | + .setIsPipeEnableMemoryCheck(false) |
| 74 | + // Lower the owner-lease floor so the test can use a short lease and stay fast. |
| 75 | + .setSubscriptionOwnerLeaseDurationMsMin(1000); |
| 76 | + // 3 ConfigNodes (for failover) + 3 DataNodes (for multi-DataNode data placement). |
| 77 | + EnvFactory.getEnv().initClusterEnvironment(3, 3); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + @After |
| 82 | + public void tearDown() throws Exception { |
| 83 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 84 | + super.tearDown(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testStaleOwnerFencedOnMultiDataNodeTopic() throws Exception { |
| 89 | + final String host = EnvFactory.getEnv().getIP(); |
| 90 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 91 | + final String topicName = "topic_owner_multi_dn"; |
| 92 | + |
| 93 | + createOwnedTopic(host, port, topicName, "owner2", 6L); |
| 94 | + try { |
| 95 | + // Spread data across multiple devices (and thus, with multiple data regions, multiple |
| 96 | + // DataNodes) before consuming. |
| 97 | + insertDataAcrossDevices(); |
| 98 | + |
| 99 | + // A stale owner (epoch 5 < current 6) must be fenced regardless of which DataNode serves the |
| 100 | + // request. |
| 101 | + assertSubscribeFenced(host, port, topicName, "stale_multi_dn", "owner1", 5L); |
| 102 | + |
| 103 | + // The current owner can subscribe. |
| 104 | + try (final SubscriptionTreePullConsumer currentOwnerConsumer = |
| 105 | + ownerConsumer( |
| 106 | + host, port, "current_multi_dn", "topic_owner_multi_dn_group", "owner2", 6L)) { |
| 107 | + currentOwnerConsumer.open(); |
| 108 | + currentOwnerConsumer.subscribe(topicName); |
| 109 | + currentOwnerConsumer.unsubscribe(topicName); |
| 110 | + } |
| 111 | + } finally { |
| 112 | + dropTopic(host, port, topicName); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testConfigNodeFailoverPersistsOwnerEpochAndKeepsFencing() throws Exception { |
| 118 | + final String host = EnvFactory.getEnv().getIP(); |
| 119 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 120 | + final String topicName = "topic_owner_cn_failover"; |
| 121 | + final long ownerLeaseDurationMs = 2000L; |
| 122 | + |
| 123 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 124 | + session.open(); |
| 125 | + final Properties properties = new Properties(); |
| 126 | + properties.put(TopicConstant.PATH_KEY, HEAD_PATH + ".**"); |
| 127 | + properties.put(TopicConstant.START_TIME_KEY, "0"); |
| 128 | + properties.put(TopicConstant.OWNER_ID_KEY, "owner1"); |
| 129 | + properties.put(TopicConstant.OWNER_EPOCH_KEY, "5"); |
| 130 | + properties.put( |
| 131 | + TopicConstant.OWNER_LEASE_DURATION_MS_KEY, String.valueOf(ownerLeaseDurationMs)); |
| 132 | + session.createTopic(topicName, properties); |
| 133 | + |
| 134 | + // Transfer to owner2/epoch 6 (this persists owner + epoch via ConfigNode consensus). |
| 135 | + session.alterTopicOwner(topicName, "owner2", 6L); |
| 136 | + Assert.assertTrue(getTopicAttributes(session, topicName).contains("owner-epoch=6")); |
| 137 | + } |
| 138 | + |
| 139 | + // Fail over: shut down the current leader ConfigNode. |
| 140 | + final int leaderIndex = EnvFactory.getEnv().getLeaderConfigNodeIndex(); |
| 141 | + EnvFactory.getEnv().shutdownConfigNode(leaderIndex); |
| 142 | + |
| 143 | + try { |
| 144 | + // After failover, the new leader must still report the persisted owner/epoch (consensus), and |
| 145 | + // a stale owner (epoch 5) must remain fenced. |
| 146 | + IoTDBSubscriptionITConstant.AWAIT.untilAsserted( |
| 147 | + () -> { |
| 148 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 149 | + session.open(); |
| 150 | + final String attributes = getTopicAttributes(session, topicName); |
| 151 | + Assert.assertTrue(attributes.contains("owner-id=owner2")); |
| 152 | + Assert.assertTrue(attributes.contains("owner-epoch=6")); |
| 153 | + } |
| 154 | + }); |
| 155 | + assertSubscribeFenced(host, port, topicName, "stale_after_failover", "owner1", 5L); |
| 156 | + } finally { |
| 157 | + EnvFactory.getEnv().startConfigNode(leaderIndex); |
| 158 | + dropTopic(host, port, topicName); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testDataNodeRestartReSyncsOwnerFencing() throws Exception { |
| 164 | + final String host = EnvFactory.getEnv().getIP(); |
| 165 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 166 | + final String topicName = "topic_owner_dn_restart"; |
| 167 | + |
| 168 | + createOwnedTopic(host, port, topicName, "owner2", 6L); |
| 169 | + try { |
| 170 | + // Restart a DataNode: it loses in-memory owner metadata and must re-sync it on restart. |
| 171 | + EnvFactory.getEnv().shutdownDataNode(1); |
| 172 | + EnvFactory.getEnv().startDataNode(1); |
| 173 | + |
| 174 | + // After the DataNode comes back and re-syncs owner metadata, the stale owner stays fenced. |
| 175 | + IoTDBSubscriptionITConstant.AWAIT.untilAsserted( |
| 176 | + () -> |
| 177 | + assertSubscribeFenced(host, port, topicName, "stale_after_dn_restart", "owner1", 5L)); |
| 178 | + } finally { |
| 179 | + dropTopic(host, port, topicName); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static void createOwnedTopic( |
| 184 | + final String host, |
| 185 | + final int port, |
| 186 | + final String topicName, |
| 187 | + final String ownerId, |
| 188 | + final long ownerEpoch) |
| 189 | + throws Exception { |
| 190 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 191 | + session.open(); |
| 192 | + final Properties properties = new Properties(); |
| 193 | + properties.put(TopicConstant.PATH_KEY, HEAD_PATH + ".**"); |
| 194 | + properties.put(TopicConstant.START_TIME_KEY, "0"); |
| 195 | + properties.put(TopicConstant.OWNER_ID_KEY, ownerId); |
| 196 | + properties.put(TopicConstant.OWNER_EPOCH_KEY, String.valueOf(ownerEpoch)); |
| 197 | + session.createTopic(topicName, properties); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static void dropTopic(final String host, final int port, final String topicName) { |
| 202 | + try (final SubscriptionTreeSession session = new SubscriptionTreeSession(host, port)) { |
| 203 | + session.open(); |
| 204 | + session.dropTopicIfExists(topicName); |
| 205 | + } catch (final Exception e) { |
| 206 | + // best-effort cleanup |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private static SubscriptionTreePullConsumer ownerConsumer( |
| 211 | + final String host, |
| 212 | + final int port, |
| 213 | + final String consumerId, |
| 214 | + final String consumerGroupId, |
| 215 | + final String ownerId, |
| 216 | + final long ownerEpoch) { |
| 217 | + return new SubscriptionTreePullConsumer.Builder() |
| 218 | + .host(host) |
| 219 | + .port(port) |
| 220 | + .consumerId(consumerId) |
| 221 | + .consumerGroupId(consumerGroupId) |
| 222 | + .ownerId(ownerId) |
| 223 | + .ownerEpoch(ownerEpoch) |
| 224 | + .autoCommit(false) |
| 225 | + .buildPullConsumer(); |
| 226 | + } |
| 227 | + |
| 228 | + private static void assertSubscribeFenced( |
| 229 | + final String host, |
| 230 | + final int port, |
| 231 | + final String topicName, |
| 232 | + final String consumerId, |
| 233 | + final String ownerId, |
| 234 | + final long ownerEpoch) |
| 235 | + throws Exception { |
| 236 | + try (final SubscriptionTreePullConsumer staleOwnerConsumer = |
| 237 | + ownerConsumer(host, port, consumerId, topicName + "_stale_group", ownerId, ownerEpoch)) { |
| 238 | + staleOwnerConsumer.open(); |
| 239 | + Assert.assertThrows( |
| 240 | + SubscriptionOwnerFencedException.class, () -> staleOwnerConsumer.subscribe(topicName)); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private void insertDataAcrossDevices() throws Exception { |
| 245 | + try (final ISession session = EnvFactory.getEnv().getSessionConnection()) { |
| 246 | + for (int device = 0; device < 6; device++) { |
| 247 | + for (int i = 0; i < 10; i++) { |
| 248 | + session.executeNonQueryStatement( |
| 249 | + String.format( |
| 250 | + "insert into %s.d%s(time, s1) values (%s, %s)", HEAD_PATH, device, i, i)); |
| 251 | + } |
| 252 | + } |
| 253 | + session.executeNonQueryStatement("flush"); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + private static String getTopicAttributes( |
| 258 | + final SubscriptionTreeSession session, final String topicName) throws Exception { |
| 259 | + for (final Topic topic : session.getTopics()) { |
| 260 | + if (topicName.equals(topic.getTopicName())) { |
| 261 | + return topic.getTopicAttributes(); |
| 262 | + } |
| 263 | + } |
| 264 | + Assert.fail("Topic " + topicName + " should exist."); |
| 265 | + return ""; |
| 266 | + } |
| 267 | +} |
0 commit comments