Skip to content

Commit 76f2a88

Browse files
committed
Subscription: add topic owner epoch fencing
1 parent 05a816d commit 76f2a88

22 files changed

Lines changed: 630 additions & 2 deletions

File tree

‎iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ public enum TSStatusCode {
316316
SHOW_SUBSCRIPTION_ERROR(1910),
317317
SUBSCRIPTION_PIPE_TIMEOUT_ERROR(1911),
318318
SUBSCRIPTION_NOT_ENABLED_ERROR(1912),
319+
SUBSCRIPTION_OWNER_FENCED(1913),
320+
SUBSCRIPTION_OWNER_REQUIRED(1914),
321+
SUBSCRIPTION_OWNER_EPOCH_REQUIRED(1915),
322+
SUBSCRIPTION_OWNER_LEASE_EXPIRED(1916),
319323

320324
// Topic
321325
CREATE_TOPIC_ERROR(2000),

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/rpc/subscription/config/ConsumerConfig.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public String getConsumerGroupId() {
6868
return getString(ConsumerConstant.CONSUMER_GROUP_ID_KEY);
6969
}
7070

71+
public String getOwnerId() {
72+
return getString(ConsumerConstant.OWNER_ID_KEY);
73+
}
74+
75+
public Long getOwnerEpoch() {
76+
return hasAttribute(ConsumerConstant.OWNER_EPOCH_KEY)
77+
? getLong(ConsumerConstant.OWNER_EPOCH_KEY)
78+
: null;
79+
}
80+
7181
public String getUsername() {
7282
return getString(ConsumerConstant.USERNAME_KEY);
7383
}

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/rpc/subscription/config/ConsumerConstant.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class ConsumerConstant {
4040

4141
public static final String CONSUMER_ID_KEY = "consumer-id";
4242
public static final String CONSUMER_GROUP_ID_KEY = "group-id";
43+
public static final String OWNER_ID_KEY = "owner-id";
44+
public static final String OWNER_EPOCH_KEY = "owner-epoch";
4345

4446
public static final String HEARTBEAT_INTERVAL_MS_KEY = "heartbeat-interval-ms";
4547
public static final long HEARTBEAT_INTERVAL_MS_DEFAULT_VALUE = 30_000L;

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/rpc/subscription/config/TopicConstant.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public class TopicConstant {
6161
public static final String STRICT_KEY = "strict";
6262
public static final String STRICT_DEFAULT_VALUE = "true";
6363

64+
public static final String OWNER_ID_KEY = "owner-id";
65+
public static final String OWNER_EPOCH_KEY = "owner-epoch";
66+
public static final String OWNER_LEASE_EXPIRE_TIME_MS_KEY = "owner-lease-expire-time-ms";
67+
6468
private TopicConstant() {
6569
throw new IllegalStateException(SubscriptionMessages.UTILITY_CLASS);
6670
}

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/base/AbstractSubscriptionConsumer.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ abstract class AbstractSubscriptionConsumer implements AutoCloseable {
105105

106106
protected String consumerId;
107107
protected String consumerGroupId;
108+
protected String ownerId;
109+
protected Long ownerEpoch;
108110

109111
private final long heartbeatIntervalMs;
110112
private final long endpointsSyncIntervalMs;
@@ -154,6 +156,14 @@ public String getConsumerGroupId() {
154156
return consumerGroupId;
155157
}
156158

159+
public String getOwnerId() {
160+
return ownerId;
161+
}
162+
163+
public Long getOwnerEpoch() {
164+
return ownerEpoch;
165+
}
166+
157167
/////////////////////////////// ctor ///////////////////////////////
158168

159169
protected AbstractSubscriptionConsumer(final AbstractSubscriptionConsumerBuilder builder) {
@@ -183,6 +193,8 @@ protected AbstractSubscriptionConsumer(final AbstractSubscriptionConsumerBuilder
183193

184194
this.consumerId = builder.consumerId;
185195
this.consumerGroupId = builder.consumerGroupId;
196+
this.ownerId = builder.ownerId;
197+
this.ownerEpoch = builder.ownerEpoch;
186198

187199
this.heartbeatIntervalMs = builder.heartbeatIntervalMs;
188200
this.endpointsSyncIntervalMs = builder.endpointsSyncIntervalMs;
@@ -213,6 +225,8 @@ protected AbstractSubscriptionConsumer(
213225
.encryptedPassword((String) properties.get(ConsumerConstant.ENCRYPTED_PASSWORD_KEY))
214226
.consumerId((String) properties.get(ConsumerConstant.CONSUMER_ID_KEY))
215227
.consumerGroupId((String) properties.get(ConsumerConstant.CONSUMER_GROUP_ID_KEY))
228+
.ownerId((String) properties.get(ConsumerConstant.OWNER_ID_KEY))
229+
.ownerEpoch((Long) properties.get(ConsumerConstant.OWNER_EPOCH_KEY))
216230
.heartbeatIntervalMs(
217231
(Long)
218232
properties.getOrDefault(
@@ -394,6 +408,8 @@ protected abstract AbstractSubscriptionProvider constructSubscriptionProvider(
394408
final String encryptedPassword,
395409
final String consumerId,
396410
final String consumerGroupId,
411+
final String ownerId,
412+
final Long ownerEpoch,
397413
final int thriftMaxFrameSize,
398414
final long heartbeatIntervalMs,
399415
final int connectionTimeoutInMs);
@@ -408,6 +424,8 @@ AbstractSubscriptionProvider constructProviderAndHandshake(final TEndPoint endPo
408424
this.encryptedPassword,
409425
this.consumerId,
410426
this.consumerGroupId,
427+
this.ownerId,
428+
this.ownerEpoch,
411429
this.thriftMaxFrameSize,
412430
this.heartbeatIntervalMs,
413431
this.connectionTimeoutInMs);
@@ -1429,6 +1447,8 @@ protected Map<String, String> coreReportMessage() {
14291447
final Map<String, String> result = new HashMap<>();
14301448
result.put("consumerId", consumerId);
14311449
result.put("consumerGroupId", consumerGroupId);
1450+
result.put("ownerId", ownerId);
1451+
result.put("ownerEpoch", String.valueOf(ownerEpoch));
14321452
result.put("isClosed", isClosed.toString());
14331453
result.put("fileSaveDir", fileSaveDir);
14341454
result.put(
@@ -1443,6 +1463,8 @@ protected Map<String, String> allReportMessage() {
14431463
final Map<String, String> result = new HashMap<>();
14441464
result.put("consumerId", consumerId);
14451465
result.put("consumerGroupId", consumerGroupId);
1466+
result.put("ownerId", ownerId);
1467+
result.put("ownerEpoch", String.valueOf(ownerEpoch));
14461468
result.put("heartbeatIntervalMs", String.valueOf(heartbeatIntervalMs));
14471469
result.put("endpointsSyncIntervalMs", String.valueOf(endpointsSyncIntervalMs));
14481470
result.put("providers", providers.toString());

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/base/AbstractSubscriptionConsumerBuilder.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class AbstractSubscriptionConsumerBuilder {
4040

4141
protected String consumerId;
4242
protected String consumerGroupId;
43+
protected String ownerId;
44+
protected Long ownerEpoch;
4345

4446
protected long heartbeatIntervalMs = ConsumerConstant.HEARTBEAT_INTERVAL_MS_DEFAULT_VALUE;
4547
protected long endpointsSyncIntervalMs =
@@ -111,6 +113,27 @@ public AbstractSubscriptionConsumerBuilder consumerGroupId(
111113
return this;
112114
}
113115

116+
public AbstractSubscriptionConsumerBuilder ownerId(@Nullable final String ownerId) {
117+
if (Objects.isNull(ownerId)) {
118+
return this;
119+
}
120+
this.ownerId = ownerId;
121+
return this;
122+
}
123+
124+
public AbstractSubscriptionConsumerBuilder ownerEpoch(final long ownerEpoch) {
125+
this.ownerEpoch = ownerEpoch;
126+
return this;
127+
}
128+
129+
public AbstractSubscriptionConsumerBuilder ownerEpoch(@Nullable final Long ownerEpoch) {
130+
if (Objects.isNull(ownerEpoch)) {
131+
return this;
132+
}
133+
this.ownerEpoch = ownerEpoch;
134+
return this;
135+
}
136+
114137
public AbstractSubscriptionConsumerBuilder heartbeatIntervalMs(final long heartbeatIntervalMs) {
115138
this.heartbeatIntervalMs =
116139
Math.max(heartbeatIntervalMs, ConsumerConstant.HEARTBEAT_INTERVAL_MS_MIN_VALUE);

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/base/AbstractSubscriptionProvider.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public abstract class AbstractSubscriptionProvider {
8181

8282
private String consumerId;
8383
private String consumerGroupId;
84+
private final String ownerId;
85+
private final Long ownerEpoch;
8486

8587
private final AtomicBoolean isClosed = new AtomicBoolean(true);
8688
private final AtomicBoolean isAvailable = new AtomicBoolean(false);
@@ -109,6 +111,8 @@ protected AbstractSubscriptionProvider(
109111
final String encryptedPassword,
110112
final String consumerId,
111113
final String consumerGroupId,
114+
final String ownerId,
115+
final Long ownerEpoch,
112116
final int thriftMaxFrameSize,
113117
final long heartbeatIntervalMs,
114118
final int connectionTimeoutInMs) {
@@ -124,6 +128,8 @@ protected AbstractSubscriptionProvider(
124128
this.endPoint = endPoint;
125129
this.consumerId = consumerId;
126130
this.consumerGroupId = consumerGroupId;
131+
this.ownerId = ownerId;
132+
this.ownerEpoch = ownerEpoch;
127133
this.username = username;
128134
this.password = password;
129135
this.encryptedPassword = encryptedPassword;
@@ -176,6 +182,12 @@ synchronized void handshake() throws SubscriptionException, IoTDBConnectionExcep
176182
final Map<String, String> consumerAttributes = new HashMap<>();
177183
consumerAttributes.put(ConsumerConstant.CONSUMER_GROUP_ID_KEY, consumerGroupId);
178184
consumerAttributes.put(ConsumerConstant.CONSUMER_ID_KEY, consumerId);
185+
if (ownerId != null) {
186+
consumerAttributes.put(ConsumerConstant.OWNER_ID_KEY, ownerId);
187+
}
188+
if (ownerEpoch != null) {
189+
consumerAttributes.put(ConsumerConstant.OWNER_EPOCH_KEY, String.valueOf(ownerEpoch));
190+
}
179191
consumerAttributes.put(ConsumerConstant.USERNAME_KEY, username);
180192
consumerAttributes.put(ConsumerConstant.PASSWORD_KEY, password);
181193
if (encryptedPassword != null) {

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/table/SubscriptionTableProvider.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class SubscriptionTableProvider extends AbstractSubscriptionProvider {
3333
final String encryptedPassword,
3434
final String consumerId,
3535
final String consumerGroupId,
36+
final String ownerId,
37+
final Long ownerEpoch,
3638
final int thriftMaxFrameSize,
3739
final long heartbeatIntervalMs,
3840
final int connectionTimeoutInMs) {
@@ -43,6 +45,8 @@ final class SubscriptionTableProvider extends AbstractSubscriptionProvider {
4345
encryptedPassword,
4446
consumerId,
4547
consumerGroupId,
48+
ownerId,
49+
ownerEpoch,
4650
thriftMaxFrameSize,
4751
heartbeatIntervalMs,
4852
connectionTimeoutInMs);

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/table/SubscriptionTablePullConsumer.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected AbstractSubscriptionProvider constructSubscriptionProvider(
4545
final String encryptedPassword,
4646
final String consumerId,
4747
final String consumerGroupId,
48+
final String ownerId,
49+
final Long ownerEpoch,
4850
final int thriftMaxFrameSize,
4951
final long heartbeatIntervalMs,
5052
final int connectionTimeoutInMs) {
@@ -55,6 +57,8 @@ protected AbstractSubscriptionProvider constructSubscriptionProvider(
5557
encryptedPassword,
5658
consumerId,
5759
consumerGroupId,
60+
ownerId,
61+
ownerEpoch,
5862
thriftMaxFrameSize,
5963
heartbeatIntervalMs,
6064
connectionTimeoutInMs);

‎iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/table/SubscriptionTablePullConsumerBuilder.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ public SubscriptionTablePullConsumerBuilder consumerGroupId(final String consume
7474
return this;
7575
}
7676

77+
@Override
78+
public SubscriptionTablePullConsumerBuilder ownerId(final String ownerId) {
79+
super.ownerId(ownerId);
80+
return this;
81+
}
82+
83+
@Override
84+
public SubscriptionTablePullConsumerBuilder ownerEpoch(final long ownerEpoch) {
85+
super.ownerEpoch(ownerEpoch);
86+
return this;
87+
}
88+
89+
@Override
90+
public SubscriptionTablePullConsumerBuilder ownerEpoch(final Long ownerEpoch) {
91+
super.ownerEpoch(ownerEpoch);
92+
return this;
93+
}
94+
7795
@Override
7896
public SubscriptionTablePullConsumerBuilder heartbeatIntervalMs(final long heartbeatIntervalMs) {
7997
super.heartbeatIntervalMs(heartbeatIntervalMs);

0 commit comments

Comments
 (0)