forked from streamnative/pulsar-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topic.java
380 lines (285 loc) · 12.3 KB
/
Topic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* 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.pulsar.broker.service;
import io.netty.buffer.ByteBuf;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.mledger.Position;
import org.apache.pulsar.broker.service.persistent.DispatchRateLimiter;
import org.apache.pulsar.broker.service.persistent.SubscribeRateLimiter;
import org.apache.pulsar.broker.service.plugin.EntryFilter;
import org.apache.pulsar.broker.stats.ClusterReplicationMetrics;
import org.apache.pulsar.broker.stats.NamespaceStats;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.transaction.TxnID;
import org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition;
import org.apache.pulsar.common.api.proto.CommandSubscribe.SubType;
import org.apache.pulsar.common.api.proto.KeySharedMeta;
import org.apache.pulsar.common.policies.data.BacklogQuota;
import org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType;
import org.apache.pulsar.common.policies.data.EntryFilters;
import org.apache.pulsar.common.policies.data.HierarchyTopicPolicies;
import org.apache.pulsar.common.policies.data.PersistentTopicInternalStats;
import org.apache.pulsar.common.policies.data.Policies;
import org.apache.pulsar.common.policies.data.stats.TopicStatsImpl;
import org.apache.pulsar.common.protocol.schema.SchemaData;
import org.apache.pulsar.common.protocol.schema.SchemaVersion;
import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap;
import org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats;
import org.apache.pulsar.utils.StatsOutputStream;
public interface Topic {
interface PublishContext {
default String getProducerName() {
return null;
}
default long getSequenceId() {
return -1L;
}
default void setOriginalProducerName(String originalProducerName) {
}
default void setOriginalSequenceId(long originalSequenceId) {
}
/**
* Return the producer name for the original producer.
* <p>
* For messages published locally, this will return the same local producer name, though in case of replicated
* messages, the original producer name will differ
*/
default String getOriginalProducerName() {
return null;
}
default long getOriginalSequenceId() {
return -1L;
}
void completed(Exception e, long ledgerId, long entryId);
default void setMetadataFromEntryData(ByteBuf entryData) {
}
default long getHighestSequenceId() {
return -1L;
}
default void setOriginalHighestSequenceId(long originalHighestSequenceId) {
}
default long getOriginalHighestSequenceId() {
return -1L;
}
default long getNumberOfMessages() {
return 1L;
}
default long getMsgSize() {
return -1L;
}
default boolean isMarkerMessage() {
return false;
}
default void setProperty(String propertyName, Object value) {
}
default Object getProperty(String propertyName) {
return null;
}
default boolean isChunked() {
return false;
}
default long getEntryTimestamp() {
return -1L;
}
default void setEntryTimestamp(long entryTimestamp) {
}
}
CompletableFuture<Void> initialize();
void publishMessage(ByteBuf headersAndPayload, PublishContext callback);
/**
* Tries to add a producer to the topic. Several validations will be performed.
*
* @param producer Producer to add
* @param producerQueuedFuture
* a future that will be triggered if the producer is being queued up prior of getting established
* @return the "topic epoch" if there is one or empty
*/
CompletableFuture<Optional<Long>> addProducer(Producer producer, CompletableFuture<Void> producerQueuedFuture);
void removeProducer(Producer producer);
/**
* Wait TransactionBuffer Recovers completely.
* Take snapshot after TB Recovers completely.
* @param isTxnEnabled isTxnEnabled
* @return a future which has completely if isTxn = false. Or a future return by takeSnapshot.
*/
CompletableFuture<Void> checkIfTransactionBufferRecoverCompletely(boolean isTxnEnabled);
/**
* record add-latency.
*/
void recordAddLatency(long latency, TimeUnit unit);
/**
* increase the publishing limited times.
*/
long increasePublishLimitedTimes();
@Deprecated
CompletableFuture<Consumer> subscribe(TransportCnx cnx, String subscriptionName, long consumerId, SubType subType,
int priorityLevel, String consumerName, boolean isDurable,
MessageId startMessageId,
Map<String, String> metadata, boolean readCompacted,
InitialPosition initialPosition,
long startMessageRollbackDurationSec, boolean replicateSubscriptionState,
KeySharedMeta keySharedMeta);
/**
* Subscribe a topic.
* @param option
* @return
*/
CompletableFuture<Consumer> subscribe(SubscriptionOption option);
CompletableFuture<Subscription> createSubscription(String subscriptionName, InitialPosition initialPosition,
boolean replicateSubscriptionState, Map<String, String> properties);
CompletableFuture<Void> unsubscribe(String subName);
ConcurrentOpenHashMap<String, ? extends Subscription> getSubscriptions();
CompletableFuture<Void> delete();
Map<String, Producer> getProducers();
String getName();
CompletableFuture<Void> checkReplication();
CompletableFuture<Void> close(boolean closeWithoutWaitingClientDisconnect);
CompletableFuture<Void> close(
boolean disconnectClients, boolean closeWithoutWaitingClientDisconnect);
void checkGC();
CompletableFuture<Void> checkClusterMigration();
void checkInactiveSubscriptions();
/**
* Activate cursors those caught up backlog-threshold entries and deactivate slow cursors which are creating
* backlog.
*/
void checkBackloggedCursors();
void checkCursorsToCacheEntries();
/**
* Indicate if the current topic enabled server side deduplication.
* This is a dynamic configuration, user may update it by namespace/topic policies.
*
* @return whether enabled server side deduplication
*/
default boolean isDeduplicationEnabled() {
return false;
}
void checkDeduplicationSnapshot();
void checkMessageExpiry();
void checkMessageDeduplicationInfo();
void incrementPublishCount(Producer producer, int numOfMessages, long msgSizeInBytes);
boolean shouldProducerMigrate();
boolean isReplicationBacklogExist();
CompletableFuture<Void> onPoliciesUpdate(Policies data);
CompletableFuture<Void> checkBacklogQuotaExceeded(String producerName, BacklogQuotaType backlogQuotaType);
boolean isEncryptionRequired();
boolean getSchemaValidationEnforced();
boolean isReplicated();
boolean isShadowReplicated();
EntryFilters getEntryFiltersPolicy();
List<EntryFilter> getEntryFilters();
BacklogQuota getBacklogQuota(BacklogQuotaType backlogQuotaType);
/**
* Uses the best-effort (not necessarily up-to-date) information available to return the age.
* @return The oldest unacknowledged message age in seconds, or -1 if not available
*/
long getBestEffortOldestUnacknowledgedMessageAgeSeconds();
void updateRates(NamespaceStats nsStats, NamespaceBundleStats currentBundleStats,
StatsOutputStream topicStatsStream, ClusterReplicationMetrics clusterReplicationMetrics,
String namespaceName, boolean hydratePublishers);
Subscription getSubscription(String subscription);
ConcurrentOpenHashMap<String, ? extends Replicator> getReplicators();
ConcurrentOpenHashMap<String, ? extends Replicator> getShadowReplicators();
TopicStatsImpl getStats(boolean getPreciseBacklog, boolean subscriptionBacklogSize,
boolean getEarliestTimeInBacklog);
TopicStatsImpl getStats(GetStatsOptions getStatsOptions);
CompletableFuture<? extends TopicStatsImpl> asyncGetStats(boolean getPreciseBacklog,
boolean subscriptionBacklogSize,
boolean getEarliestTimeInBacklog);
CompletableFuture<? extends TopicStatsImpl> asyncGetStats(GetStatsOptions getStatsOptions);
CompletableFuture<PersistentTopicInternalStats> getInternalStats(boolean includeLedgerMetadata);
Position getLastPosition();
CompletableFuture<MessageId> getLastMessageId();
/**
* Whether a topic has had a schema defined for it.
*/
CompletableFuture<Boolean> hasSchema();
/**
* Add a schema to the topic. This will fail if the new schema is incompatible with the current
* schema.
*/
CompletableFuture<SchemaVersion> addSchema(SchemaData schema);
/**
* Delete the schema if this topic has a schema defined for it.
*/
CompletableFuture<SchemaVersion> deleteSchema();
/**
* Check if schema is compatible with current topic schema.
*/
CompletableFuture<Void> checkSchemaCompatibleForConsumer(SchemaData schema);
/**
* If the topic is idle (no producers, no entries, no subscribers and no existing schema),
* add the passed schema to the topic. Otherwise, check that the passed schema is compatible
* with what the topic already has.
*/
CompletableFuture<Void> addSchemaIfIdleOrCheckCompatible(SchemaData schema);
CompletableFuture<Void> deleteForcefully();
default Optional<DispatchRateLimiter> getDispatchRateLimiter() {
return Optional.empty();
}
default Optional<SubscribeRateLimiter> getSubscribeRateLimiter() {
return Optional.empty();
}
default Optional<DispatchRateLimiter> getBrokerDispatchRateLimiter() {
return Optional.empty();
}
default boolean isSystemTopic() {
return false;
}
boolean isPersistent();
boolean isTransferring();
/* ------ Transaction related ------ */
/**
* Publish Transaction message to this Topic's TransactionBuffer.
*
* @param txnID Transaction Id
* @param headersAndPayload Message data
* @param publishContext Publish context
*/
void publishTxnMessage(TxnID txnID, ByteBuf headersAndPayload, PublishContext publishContext);
/**
* End the transaction in this topic.
*
* @param txnID Transaction id
* @param txnAction Transaction action.
* @param lowWaterMark low water mark of this tc
* @return
*/
CompletableFuture<Void> endTxn(TxnID txnID, int txnAction, long lowWaterMark);
/**
* Truncate a topic.
* The truncate operation will move all cursors to the end of the topic and delete all inactive ledgers.
* @return
*/
CompletableFuture<Void> truncate();
/**
* Get BrokerService.
* @return
*/
BrokerService getBrokerService();
/**
* Get HierarchyTopicPolicies.
* @return
*/
HierarchyTopicPolicies getHierarchyTopicPolicies();
}