|
| 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.exception.SubscriptionRuntimeException; |
| 27 | +import org.apache.iotdb.rpc.subscription.payload.poll.SubscriptionCommitContext; |
| 28 | +import org.apache.iotdb.session.subscription.SubscriptionSession; |
| 29 | +import org.apache.iotdb.session.subscription.consumer.SubscriptionPullConsumer; |
| 30 | +import org.apache.iotdb.session.subscription.payload.SubscriptionMessage; |
| 31 | +import org.apache.iotdb.subscription.it.IoTDBSubscriptionITConstant; |
| 32 | + |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Ignore; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.experimental.categories.Category; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | + |
| 40 | +import java.lang.reflect.Field; |
| 41 | +import java.time.Duration; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Set; |
| 44 | +import java.util.SortedMap; |
| 45 | +import java.util.concurrent.locks.LockSupport; |
| 46 | + |
| 47 | +import static org.junit.Assert.fail; |
| 48 | + |
| 49 | +@RunWith(IoTDBTestRunner.class) |
| 50 | +@Category({LocalStandaloneIT.class}) |
| 51 | +public class IoTDBSubscriptionMessageIT extends AbstractSubscriptionLocalIT { |
| 52 | + |
| 53 | + @Override |
| 54 | + @Before |
| 55 | + public void setUp() throws Exception { |
| 56 | + super.setUp(); |
| 57 | + } |
| 58 | + |
| 59 | + @Ignore |
| 60 | + @Test |
| 61 | + public void testPullConsumerCommitAfterRemoveUserData() throws Exception { |
| 62 | + final String topicName = "topic_remove_user_data"; |
| 63 | + insertHistoricalData(0, 100); |
| 64 | + createTopic(topicName); |
| 65 | + |
| 66 | + final String host = EnvFactory.getEnv().getIP(); |
| 67 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 68 | + try (final SubscriptionPullConsumer consumer = |
| 69 | + new SubscriptionPullConsumer.Builder() |
| 70 | + .host(host) |
| 71 | + .port(port) |
| 72 | + .consumerId("c_remove_user_data") |
| 73 | + .consumerGroupId("cg_remove_user_data") |
| 74 | + .autoCommit(false) |
| 75 | + .buildPullConsumer()) { |
| 76 | + consumer.open(); |
| 77 | + consumer.subscribe(topicName); |
| 78 | + |
| 79 | + final List<SubscriptionMessage> messages = pollMessages(consumer); |
| 80 | + Assert.assertFalse(messages.isEmpty()); |
| 81 | + |
| 82 | + for (final SubscriptionMessage message : messages) { |
| 83 | + Assert.assertNotNull(message.getCommitContext()); |
| 84 | + Assert.assertFalse(message.getResultSets().isEmpty()); |
| 85 | + |
| 86 | + message.removeUserData(); |
| 87 | + |
| 88 | + Assert.assertNotNull(message.getCommitContext()); |
| 89 | + Assert.assertThrows(SubscriptionRuntimeException.class, message::getResultSets); |
| 90 | + Assert.assertThrows(SubscriptionRuntimeException.class, message::getRecordTabletIterator); |
| 91 | + message.removeUserData(); |
| 92 | + } |
| 93 | + |
| 94 | + consumer.commitSync(messages); |
| 95 | + consumer.unsubscribe(topicName); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Ignore |
| 100 | + @Test |
| 101 | + public void testPullConsumerAutoCommitStoresCommitContextsOnly() throws Exception { |
| 102 | + final String topicName = "topic_auto_commit_context_only"; |
| 103 | + insertHistoricalData(100, 200); |
| 104 | + createTopic(topicName); |
| 105 | + |
| 106 | + final String host = EnvFactory.getEnv().getIP(); |
| 107 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 108 | + try (final SubscriptionPullConsumer consumer = |
| 109 | + new SubscriptionPullConsumer.Builder() |
| 110 | + .host(host) |
| 111 | + .port(port) |
| 112 | + .consumerId("c_auto_commit") |
| 113 | + .consumerGroupId("cg_auto_commit") |
| 114 | + .autoCommit(true) |
| 115 | + .autoCommitIntervalMs(60_000L) |
| 116 | + .buildPullConsumer()) { |
| 117 | + consumer.open(); |
| 118 | + consumer.subscribe(topicName); |
| 119 | + |
| 120 | + final List<SubscriptionMessage> messages = pollMessages(consumer); |
| 121 | + Assert.assertFalse(messages.isEmpty()); |
| 122 | + messages.forEach(SubscriptionMessage::removeUserData); |
| 123 | + |
| 124 | + final SortedMap<Long, Set<SubscriptionCommitContext>> uncommittedCommitContexts = |
| 125 | + getUncommittedCommitContexts(consumer); |
| 126 | + Assert.assertFalse(uncommittedCommitContexts.isEmpty()); |
| 127 | + |
| 128 | + final Object storedObject = |
| 129 | + uncommittedCommitContexts.values().iterator().next().iterator().next(); |
| 130 | + Assert.assertTrue(storedObject instanceof SubscriptionCommitContext); |
| 131 | + Assert.assertFalse(storedObject instanceof SubscriptionMessage); |
| 132 | + |
| 133 | + consumer.unsubscribe(topicName); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void insertHistoricalData(final int start, final int end) { |
| 138 | + try (final ISession session = EnvFactory.getEnv().getSessionConnection()) { |
| 139 | + for (int i = start; i < end; ++i) { |
| 140 | + session.executeNonQueryStatement( |
| 141 | + String.format("insert into root.db.d1(time, s1) values (%s, 1)", i)); |
| 142 | + } |
| 143 | + session.executeNonQueryStatement("flush"); |
| 144 | + } catch (final Exception e) { |
| 145 | + e.printStackTrace(); |
| 146 | + fail(e.getMessage()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void createTopic(final String topicName) { |
| 151 | + final String host = EnvFactory.getEnv().getIP(); |
| 152 | + final int port = Integer.parseInt(EnvFactory.getEnv().getPort()); |
| 153 | + try (final SubscriptionSession session = new SubscriptionSession(host, port)) { |
| 154 | + session.open(); |
| 155 | + session.createTopic(topicName); |
| 156 | + } catch (final Exception e) { |
| 157 | + e.printStackTrace(); |
| 158 | + fail(e.getMessage()); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private List<SubscriptionMessage> pollMessages(final SubscriptionPullConsumer consumer) |
| 163 | + throws Exception { |
| 164 | + for (int i = 0; i < 10; ++i) { |
| 165 | + final List<SubscriptionMessage> messages = |
| 166 | + consumer.poll(Duration.ofMillis(IoTDBSubscriptionITConstant.POLL_TIMEOUT_MS)); |
| 167 | + if (!messages.isEmpty()) { |
| 168 | + return messages; |
| 169 | + } |
| 170 | + LockSupport.parkNanos(IoTDBSubscriptionITConstant.SLEEP_NS); |
| 171 | + } |
| 172 | + fail("Failed to poll subscription messages within the expected timeout."); |
| 173 | + throw new IllegalStateException("unreachable"); |
| 174 | + } |
| 175 | + |
| 176 | + @SuppressWarnings("unchecked") |
| 177 | + private SortedMap<Long, Set<SubscriptionCommitContext>> getUncommittedCommitContexts( |
| 178 | + final SubscriptionPullConsumer consumer) throws Exception { |
| 179 | + final Field field = |
| 180 | + SubscriptionPullConsumer.class.getDeclaredField("uncommittedCommitContexts"); |
| 181 | + field.setAccessible(true); |
| 182 | + return (SortedMap<Long, Set<SubscriptionCommitContext>>) field.get(consumer); |
| 183 | + } |
| 184 | +} |
0 commit comments