Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription: support TsFile message format & improve exception handling logic #12326

Merged
merged 114 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
ea1b05f
setup topic config
VGalaxies Apr 8, 2024
2f33383
improve SubscriptionSessionExample
VGalaxies Apr 9, 2024
d3e9976
intro req and resp for poll tsfile
VGalaxies Apr 9, 2024
dcda904
Merge branch 'master' into sub-file
VGalaxies Apr 10, 2024
cf8f4ba
backup
VGalaxies Apr 10, 2024
04d4b69
backup
VGalaxies Apr 10, 2024
4eefc00
backup
VGalaxies Apr 11, 2024
f424311
backup
VGalaxies Apr 11, 2024
3de85c9
backup
VGalaxies Apr 11, 2024
9fc7894
backup
VGalaxies Apr 12, 2024
2bd171a
fix
VGalaxies Apr 12, 2024
22aced3
fix
VGalaxies Apr 12, 2024
0cc8425
Merge branch 'master' into sub-file
VGalaxies Apr 12, 2024
4f85fb6
refact
VGalaxies Apr 13, 2024
1113eae
fix
VGalaxies Apr 13, 2024
7b86711
Merge branch 'master' into sub-file
VGalaxies Apr 16, 2024
7d5bc5a
backup
VGalaxies Apr 16, 2024
1cf8d99
backup
VGalaxies Apr 16, 2024
5cb1762
backup
VGalaxies Apr 16, 2024
bae11d1
improve example
VGalaxies Apr 16, 2024
ab1f04c
fix
VGalaxies Apr 16, 2024
1681b89
improve IT
VGalaxies Apr 17, 2024
d6382f6
fix
VGalaxies Apr 17, 2024
105ff60
Merge branch 'master' into sub-file
VGalaxies Apr 17, 2024
ddec406
more ITs
VGalaxies Apr 17, 2024
5db570f
fix
VGalaxies Apr 17, 2024
a90ecd3
fix
VGalaxies Apr 17, 2024
b377e7c
fix
VGalaxies Apr 17, 2024
db15723
backup
VGalaxies Apr 17, 2024
b73fb77
refact
VGalaxies Apr 17, 2024
6f5d742
improve
VGalaxies Apr 17, 2024
43fe24c
improve
VGalaxies Apr 17, 2024
dece6e1
fix
VGalaxies Apr 17, 2024
f3abdd0
prefetch tsfile
VGalaxies Apr 18, 2024
376510e
fix
VGalaxies Apr 18, 2024
2326d2b
Merge branch 'master' into sub-file
VGalaxies Apr 18, 2024
0a8992d
fix
VGalaxies Apr 18, 2024
92e705f
improve
VGalaxies Apr 18, 2024
1775bb6
fix
VGalaxies Apr 18, 2024
18b8189
Merge branch 'master' into sub-file
VGalaxies Apr 19, 2024
0322247
improve
VGalaxies Apr 19, 2024
c972417
improve
VGalaxies Apr 19, 2024
1614c9b
spotless
VGalaxies Apr 19, 2024
7fd091a
improve
VGalaxies Apr 19, 2024
974c64c
spotless
VGalaxies Apr 19, 2024
0c3b6d3
rename
VGalaxies Apr 19, 2024
d82af57
enhance logs
VGalaxies Apr 19, 2024
c2cb55d
improve
VGalaxies Apr 19, 2024
e494522
improve exception handling
VGalaxies Apr 19, 2024
33a9a3d
fix
VGalaxies Apr 19, 2024
d7f7b05
fix
VGalaxies Apr 19, 2024
cc14e6f
Merge branch 'master' into sub-file
VGalaxies Apr 19, 2024
b73aa9a
fix
VGalaxies Apr 19, 2024
d4f2e7e
fix
VGalaxies Apr 19, 2024
8809eda
spotless
VGalaxies Apr 19, 2024
82b2d93
fix
VGalaxies Apr 19, 2024
a39e52a
basic tsfile poll timer
VGalaxies Apr 20, 2024
e797f67
improve exception handling cont
VGalaxies Apr 20, 2024
b45bee0
Merge branch 'master' into sub-file
VGalaxies Apr 22, 2024
1e9c3b7
improve
VGalaxies Apr 22, 2024
844decd
simplify on the fly tsfile & consider duplication
VGalaxies Apr 23, 2024
72b2684
timer control on client side
VGalaxies Apr 23, 2024
62c4a2c
minor improve
VGalaxies Apr 23, 2024
1f60902
Merge branch 'master' into sub-file
VGalaxies Apr 23, 2024
0b9b459
add comment
VGalaxies Apr 23, 2024
c78f834
Merge branch 'master' into sub-file
VGalaxies Apr 24, 2024
8f81d50
improve
VGalaxies Apr 24, 2024
eb450d1
rename
VGalaxies Apr 24, 2024
8cffcf4
Merge branch 'master' into sub-file
VGalaxies Apr 26, 2024
7a2149e
Merge branch 'master' into sub-file
VGalaxies May 6, 2024
9da4e33
spotless
VGalaxies May 6, 2024
9de9b81
fix
VGalaxies May 6, 2024
89c7e3b
Merge branch 'master' into sub-file
VGalaxies May 6, 2024
f4fa0af
fixup
VGalaxies May 6, 2024
3f5cbc8
minor improve
VGalaxies May 8, 2024
d204b2a
intergrate with metrics framework
VGalaxies May 10, 2024
989bdd8
Merge branch 'master' into sub-file
VGalaxies May 13, 2024
75ad4a1
improve ex msg for sub client
VGalaxies May 13, 2024
5bbcbe3
round-robin poll endpoints
VGalaxies May 13, 2024
1d97637
Merge branch 'master' into sub-file
VGalaxies May 14, 2024
b830ec7
reset byte buffer after polling msg
VGalaxies May 14, 2024
fc7555f
refactor subscription providers
VGalaxies May 14, 2024
c2e52a6
spotless
VGalaxies May 14, 2024
0ef11fa
minor improve
VGalaxies May 14, 2024
7eac7ba
improve push consumer
VGalaxies May 15, 2024
239f115
minor improve
VGalaxies May 15, 2024
48e6e01
Merge branch 'master' into sub-file
VGalaxies May 15, 2024
e57c636
intro nack
VGalaxies May 15, 2024
087c5d5
add more tests for sub tsfile
VGalaxies May 15, 2024
bb0bde7
minor improve
VGalaxies May 16, 2024
4a8b42c
intro basic memory control
VGalaxies May 16, 2024
2ac75db
improve prefetch and cache
VGalaxies May 16, 2024
01b75f8
random initial delay & prefetch tsfile recursively & close consumer w…
VGalaxies May 16, 2024
e38f567
fix CI
VGalaxies May 17, 2024
46181c2
improve restart CI
VGalaxies May 17, 2024
61f8c33
fix CI
VGalaxies May 17, 2024
4aaa152
improve poll auto retry
VGalaxies May 19, 2024
9b2b684
refactor
VGalaxies May 20, 2024
19c95d0
Merge branch 'master' into sub-file
VGalaxies May 20, 2024
62a1739
close SubscriptionProvider when constructProviderAndHandshake throw e…
VGalaxies May 20, 2024
c424b19
add file ops for SubscriptionFileHandler
VGalaxies May 20, 2024
b48c809
intro future style commit async API
VGalaxies May 20, 2024
bfed7f3
fix AtomicMoveNotSupportedException
VGalaxies May 21, 2024
871c3c3
Merge branch 'master' into sub-file
VGalaxies May 21, 2024
79014bf
fixup
VGalaxies May 21, 2024
d852019
rename format in topic constant
VGalaxies May 21, 2024
18c8c67
downgrade consumer close ex to log
VGalaxies May 23, 2024
13795c1
remove sub prefix
VGalaxies May 23, 2024
e9efcbb
fix review
VGalaxies May 23, 2024
715005c
Merge branch 'master' into sub-file
VGalaxies May 23, 2024
b7c3d8b
Merge branch 'master' into sub-file
VGalaxies May 24, 2024
73905bf
notify the client when the topic does not meet the restrictions
VGalaxies May 24, 2024
44a9e8e
Merge branch 'master' into sub-file
VGalaxies May 27, 2024
4bcb9ea
fix IT
VGalaxies May 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,51 @@
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.isession.util.Version;
import org.apache.iotdb.rpc.subscription.config.ConsumerConstant;
import org.apache.iotdb.rpc.subscription.config.TopicConstant;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.subscription.SubscriptionMessage;
import org.apache.iotdb.session.subscription.SubscriptionPullConsumer;
import org.apache.iotdb.session.subscription.SubscriptionSession;
import org.apache.iotdb.session.subscription.SubscriptionSessionDataSet;
import org.apache.iotdb.session.subscription.SubscriptionSessionDataSets;
import org.apache.iotdb.session.subscription.payload.SubscriptionMessage;
import org.apache.iotdb.session.subscription.payload.SubscriptionSessionDataSet;

import java.time.Duration;
import org.apache.tsfile.read.TsFileReader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.locks.LockSupport;

public class SubscriptionSessionExample {

private static Session session;

private static final String LOCAL_HOST = "127.0.0.1";
private static final String HOST = "127.0.0.1";
private static final int PORT = 6667;

private static final String TOPIC_1 = "topic1";
private static final String TOPIC_2 = "`topic2`";

public static final long SLEEP_NS = 1_000_000_000L;
public static final long POLL_TIMEOUT_MS = 10_000L;
private static final int MAX_RETRY_TIMES = 3;

public static void main(String[] args) throws Exception {
private static void prepareData() throws Exception {
// Open session
session =
new Session.Builder()
.host(LOCAL_HOST)
.port(6667)
.host(HOST)
.port(PORT)
.username("root")
.password("root")
.version(Version.V_1_0)
.build();
session.open(false);

// Insert some historical data
long currentTime = System.currentTimeMillis();
for (int i = 0; i < 100; ++i) {
final long currentTime = System.currentTimeMillis();
for (int i = 0; i < 10000; ++i) {
session.executeNonQueryStatement(
String.format("insert into root.db.d1(time, s1, s2) values (%s, 1, 2)", i));
session.executeNonQueryStatement(
Expand All @@ -63,31 +76,60 @@ public static void main(String[] args) throws Exception {
}
session.executeNonQueryStatement("flush");

// Create topic
final String topic1 = "topic1";
final String topic2 = "`topic2`";
try (SubscriptionSession subscriptionSession = new SubscriptionSession(LOCAL_HOST, 6667)) {
// Close session
session.close();
session = null;
}

private static void dataQuery() throws Exception {
// Open session
session =
new Session.Builder()
.host(HOST)
.port(PORT)
.username("root")
.password("root")
.version(Version.V_1_0)
.build();
session.open(false);

// Query
final SessionDataSet dataSet = session.executeQueryStatement("select ** from root.**");
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
}

// Close session
session.close();
session = null;
}

private static void subscriptionExample1() throws Exception {
// Create topics
try (final SubscriptionSession subscriptionSession = new SubscriptionSession(HOST, PORT)) {
subscriptionSession.open();
subscriptionSession.createTopic(topic1);
subscriptionSession.createTopic(topic2);
subscriptionSession.createTopic(TOPIC_1);
}

int retryCount = 0;
// Subscription: property-style ctor
Properties config = new Properties();
final Properties config = new Properties();
config.put(ConsumerConstant.CONSUMER_ID_KEY, "c1");
config.put(ConsumerConstant.CONSUMER_GROUP_ID_KEY, "cg1");
SubscriptionPullConsumer consumer1 = new SubscriptionPullConsumer(config);
final SubscriptionPullConsumer consumer1 = new SubscriptionPullConsumer(config);
consumer1.open();
consumer1.subscribe(topic1);
consumer1.subscribe(TOPIC_1);
while (true) {
Thread.sleep(1000); // Wait for some time
List<SubscriptionMessage> messages = consumer1.poll(Duration.ofMillis(10000));
LockSupport.parkNanos(SLEEP_NS); // wait some time
final List<SubscriptionMessage> messages = consumer1.poll(POLL_TIMEOUT_MS);
if (messages.isEmpty()) {
break;
retryCount++;
if (retryCount >= MAX_RETRY_TIMES) {
break;
}
}
for (SubscriptionMessage message : messages) {
SubscriptionSessionDataSets payload = (SubscriptionSessionDataSets) message.getPayload();
for (SubscriptionSessionDataSet dataSet : payload) {
for (final SubscriptionMessage message : messages) {
for (final SubscriptionSessionDataSet dataSet : message.getSessionDataSetsHandler()) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
Expand All @@ -99,51 +141,75 @@ public static void main(String[] args) throws Exception {
}

// Show topics and subscriptions
try (SubscriptionSession subscriptionSession = new SubscriptionSession(LOCAL_HOST, 6667)) {
try (final SubscriptionSession subscriptionSession = new SubscriptionSession(HOST, PORT)) {
subscriptionSession.open();
subscriptionSession.getTopics().forEach((System.out::println));
subscriptionSession.getSubscriptions().forEach((System.out::println));
}

consumer1.unsubscribe(topic1);
consumer1.unsubscribe(TOPIC_1);
consumer1.close();
}

// Subscription: builder-style ctor
try (SubscriptionPullConsumer consumer2 =
new SubscriptionPullConsumer.Builder()
.consumerId("c2")
.consumerGroupId("cg2")
.autoCommit(false)
.buildPullConsumer()) {
consumer2.open();
consumer2.subscribe(topic2);
while (true) {
Thread.sleep(1000); // wait some time
List<SubscriptionMessage> messages =
consumer2.poll(Collections.singleton(topic2), Duration.ofMillis(10000));
if (messages.isEmpty()) {
break;
}
for (SubscriptionMessage message : messages) {
SubscriptionSessionDataSets payload = (SubscriptionSessionDataSets) message.getPayload();
for (SubscriptionSessionDataSet dataSet : payload) {
System.out.println(dataSet.getColumnNames());
System.out.println(dataSet.getColumnTypes());
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
}
}
}
consumer2.commitSync(messages);
}
consumer2.unsubscribe(topic2);
private static void subscriptionExample2() throws Exception {
try (final SubscriptionSession subscriptionSession = new SubscriptionSession(HOST, PORT)) {
subscriptionSession.open();
final Properties config = new Properties();
config.put(TopicConstant.FORMAT_KEY, TopicConstant.FORMAT_TS_FILE_HANDLER_VALUE);
subscriptionSession.createTopic(TOPIC_2, config);
}

// Query
SessionDataSet dataSet = session.executeQueryStatement("select ** from root.**");
while (dataSet.hasNext()) {
System.out.println(dataSet.next());
final List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 8; ++i) {
final int idx = i;
final Thread thread =
new Thread(
() -> {
int retryCount = 0;
// Subscription: builder-style ctor
try (final SubscriptionPullConsumer consumer2 =
new SubscriptionPullConsumer.Builder()
.consumerId("c" + idx)
.consumerGroupId("cg2")
.autoCommit(false)
.buildPullConsumer()) {
consumer2.open();
consumer2.subscribe(TOPIC_2);
while (true) {
LockSupport.parkNanos(SLEEP_NS); // wait some time
final List<SubscriptionMessage> messages =
consumer2.poll(Collections.singleton(TOPIC_2), POLL_TIMEOUT_MS);
if (messages.isEmpty()) {
retryCount++;
if (retryCount >= MAX_RETRY_TIMES) {
break;
}
}
for (final SubscriptionMessage message : messages) {
try (final TsFileReader reader = message.getTsFileHandler().openReader()) {
// do something...
}
}
consumer2.commitSync(messages);
}
consumer2.unsubscribe(TOPIC_2);
} catch (final IOException e) {
throw new RuntimeException(e);
}
});
thread.start();
threads.add(thread);
}
session.close();

for (final Thread thread : threads) {
thread.join();
}
}

public static void main(final String[] args) throws Exception {
prepareData();
dataQuery();
subscriptionExample1();
subscriptionExample2();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.subscription.it;

public class IoTDBSubscriptionITConstant {

public static final long AWAITILITY_POLL_DELAY_SECOND = 1L;
public static final long AWAITILITY_POLL_INTERVAL_SECOND = 2L;
public static final long AWAITILITY_AT_MOST_SECOND = 240L;

public static final long SLEEP_NS = 1_000_000_000L;
public static final long POLL_TIMEOUT_MS = 10_000L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public void setUp() {
senderEnv = MultiEnvFactory.getEnv(0);
receiverEnv = MultiEnvFactory.getEnv(1);

// enable auto create schema
senderEnv.getConfig().getCommonConfig().setAutoCreateSchemaEnabled(true);
receiverEnv.getConfig().getCommonConfig().setAutoCreateSchemaEnabled(true);

// for IoTDBSubscriptionConsumerGroupIT
receiverEnv.getConfig().getCommonConfig().setPipeAirGapReceiverEnabled(true);

// 10 min, assert that the operations will not time out
Expand Down
Loading
Loading