Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions conf/broker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,10 @@ managedLedgerMinLedgerRolloverTimeMinutes=10
# Maximum time before forcing a ledger rollover for a topic
managedLedgerMaxLedgerRolloverTimeMinutes=240

# Time to rollover ledger for inactive topic (duration without any publish on that topic)
# Disable rollover with value 0 (Default value 0)
managedLedgerInactiveLedgerRolloverTimeSeconds=0

# Maximum ledger size before triggering a rollover for a topic (MB)
managedLedgerMaxSizePerLedgerMbytes=2048

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,10 @@ void asyncSetProperties(Map<String, String> properties, AsyncCallbacks.UpdatePro
* @return the future of managed ledger internal stats
*/
CompletableFuture<ManagedLedgerInternalStats> getManagedLedgerInternalStats(boolean includeLedgerMetadata);

/**
* Check current inactive ledger (based on {@link ManagedLedgerConfig#getInactiveLedgerRollOverTimeMs()} and
* roll over that ledger if inactive.
*/
void checkInactiveLedgerAndRollOver();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ManagedLedgerConfig {
private Clock clock = Clock.systemUTC();
private ManagedLedgerInterceptor managedLedgerInterceptor;
private Map<String, String> properties;
private int inactiveLedgerRollOverTimeMs = 0;

public boolean isCreateIfMissing() {
return createIfMissing;
Expand Down Expand Up @@ -653,4 +654,19 @@ public ManagedLedgerInterceptor getManagedLedgerInterceptor() {
public void setManagedLedgerInterceptor(ManagedLedgerInterceptor managedLedgerInterceptor) {
this.managedLedgerInterceptor = managedLedgerInterceptor;
}

public int getInactiveLedgerRollOverTimeMs() {
return inactiveLedgerRollOverTimeMs;
}

/**
* Set rollOver time for inactive ledgers.
*
* @param inactiveLedgerRollOverTimeMs
* @param unit
*/
public void setInactiveLedgerRollOverTime(int inactiveLedgerRollOverTimeMs, TimeUnit unit) {
this.inactiveLedgerRollOverTimeMs = (int) unit.toMillis(inactiveLedgerRollOverTimeMs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {

private ManagedLedgerInterceptor managedLedgerInterceptor;

private volatile long lastAddEntryTimeMs = 0;
private long inactiveLedgerRollOverTimeMs = 0;

protected static final int DEFAULT_LEDGER_DELETE_RETRIES = 3;
protected static final int DEFAULT_LEDGER_DELETE_BACKOFF_TIME_SEC = 60;

Expand Down Expand Up @@ -323,6 +326,10 @@ public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper
this.maximumRolloverTimeMs = getMaximumRolloverTimeMs(config);
this.mlOwnershipChecker = mlOwnershipChecker;
this.propertiesMap = Maps.newHashMap();
if (config.getManagedLedgerInterceptor() != null) {
this.managedLedgerInterceptor = config.getManagedLedgerInterceptor();
}
this.inactiveLedgerRollOverTimeMs = config.getInactiveLedgerRollOverTimeMs();
}

synchronized void initialize(final ManagedLedgerInitializeLedgerCallback callback, final Object ctx) {
Expand Down Expand Up @@ -797,6 +804,8 @@ private synchronized void internalAsyncAddEntry(OpAddEntry addOperation) {
}
addOperation.initiate();
}
// mark add entry activity
lastAddEntryTimeMs = System.currentTimeMillis();
}

private boolean beforeAddEntry(OpAddEntry addOperation) {
Expand Down Expand Up @@ -4076,4 +4085,13 @@ private void cancelScheduledTasks() {
}
}

@Override
public void checkInactiveLedgerAndRollOver() {
long currentTimeMs = System.currentTimeMillis();
if (inactiveLedgerRollOverTimeMs > 0 && currentTimeMs > (lastAddEntryTimeMs + inactiveLedgerRollOverTimeMs)) {
log.info("[{}] Closing inactive ledger, last-add entry {}", name, lastAddEntryTimeMs);
ledgerClosed(currentLedger);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call rollCurrentLedgerIfFull method which actually close the ledger and it's the same rollover procedure used for topic when they reach the max quota

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rollCurrentLedgerIfFull closes ledger if its full and that won't be helpful in this case because ledger is not full but ledger inactivity criteria has been triggered.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to extract the code block inside the if

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3424,4 +3424,29 @@ public void testCancellationOfScheduledTasks() throws Exception {
assertTrue(timeoutTask2.isCancelled());
assertTrue(checkLedgerRollTask2.isCancelled());
}

@Test
public void testInactiveLedgerRollOver() throws Exception {
int inactiveLedgerRollOverTimeMs = 5;
ManagedLedgerFactoryConfig factoryConf = new ManagedLedgerFactoryConfig();
ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc);
ManagedLedgerConfig config = new ManagedLedgerConfig();
config.setInactiveLedgerRollOverTime(inactiveLedgerRollOverTimeMs, TimeUnit.MILLISECONDS);
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("rollover_inactive", config);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should cleanup resources here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoloboschi
This is the "style" of this file, it would be better to fix the whole file, but probably in a follow up patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. fixed it.

ManagedCursor cursor = ledger.openCursor("c1");

int totalAddEntries = 5;
for (int i = 0; i < totalAddEntries; i++) {
String content = "entry"; // 5 bytes
ledger.checkInactiveLedgerAndRollOver();
ledger.addEntry(content.getBytes());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be interesting to see what happens in a failure scenario, when the addEntry is rejected.
From your implementation we consider the topic active even if a write fails. it makes sense to me. just asking to add a unit test for that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this is an important aspect, worth to add a test case.

why is it so important to you ?
if a write fails the ledger will be rolled over in any case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it marks activity if it receives a write without depending on failures or introducing any complexity about the failures. so, I don't see any specific need for that test.

Thread.sleep(inactiveLedgerRollOverTimeMs * 5);
}

List<LedgerInfo> ledgers = ledger.getLedgersInfoAsList();
assertEquals(ledgers.size(), totalAddEntries);
ledger.close();
factory.shutdown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,14 @@ public class ServiceConfiguration implements PulsarConfiguration {
)
private int managedLedgerOffloadPrefetchRounds = 1;

@FieldContext(
dynamic = true,
category = CATEGORY_STORAGE_ML,
doc = "Time to rollover ledger for inactive topic (duration without any publish on that topic). "
+ "Disable rollover with value 0 (Default value 0)"
)
private int managedLedgerInactiveLedgerRolloverTimeSeconds = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minutes or seconds ?
in conf/broker.conf and the doc you set minutes instead of seconds

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. I missed to update doc after changing timeunit in code. I have fixed it now.


/**** --- Transaction config variables. --- ****/
@FieldContext(
category = CATEGORY_TRANSACTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,8 @@ public CompletableFuture<ManagedLedgerConfig> getManagedLedgerConfig(TopicName t
managedLedgerConfig.setRetentionSizeInMB(retentionPolicies.getRetentionSizeInMB());
managedLedgerConfig.setAutoSkipNonRecoverableData(serviceConfig.isAutoSkipNonRecoverableData());
managedLedgerConfig.setLazyCursorRecovery(serviceConfig.isLazyCursorRecovery());
managedLedgerConfig.setInactiveLedgerRollOverTime(
serviceConfig.getManagedLedgerInactiveLedgerRolloverTimeSeconds(), TimeUnit.SECONDS);

OffloadPoliciesImpl nsLevelOffloadPolicies =
(OffloadPoliciesImpl) policies.map(p -> p.offload_policies).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public synchronized void updateStats(
// this task: helps to activate inactive-backlog-cursors which have caught up and
// connected, also deactivate active-backlog-cursors which has backlog
topic.checkBackloggedCursors();
// check if topic is inactive and require ledger rollover
((PersistentTopic) topic).checkInactiveLedgers();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I can see that topic.checkBackloaggedCursors() here too, but I wonder if this implementation tightly couples two distinct features. I doubt that someone would turn off the stats feature, but if they did, they'd lose this feature.

We already have an event loop used to delete inactive topics. The broker service initializes several inactivity related loops here:

protected void startInactivityMonitor() {
if (pulsar().getConfiguration().isBrokerDeleteInactiveTopicsEnabled()) {
int interval = pulsar().getConfiguration().getBrokerDeleteInactiveTopicsFrequencySeconds();
inactivityMonitor.scheduleAtFixedRate(safeRun(() -> checkGC()), interval, interval,
TimeUnit.SECONDS);
}
// Deduplication info checker
long duplicationCheckerIntervalInSeconds = TimeUnit.MINUTES
.toSeconds(pulsar().getConfiguration().getBrokerDeduplicationProducerInactivityTimeoutMinutes()) / 3;
inactivityMonitor.scheduleAtFixedRate(safeRun(this::checkMessageDeduplicationInfo),
duplicationCheckerIntervalInSeconds,
duplicationCheckerIntervalInSeconds, TimeUnit.SECONDS);
// Inactive subscriber checker
if (pulsar().getConfiguration().getSubscriptionExpiryCheckIntervalInMinutes() > 0) {
long subscriptionExpiryCheckIntervalInSeconds =
TimeUnit.MINUTES.toSeconds(pulsar().getConfiguration()
.getSubscriptionExpiryCheckIntervalInMinutes());
inactivityMonitor.scheduleAtFixedRate(safeRun(this::checkInactiveSubscriptions),
subscriptionExpiryCheckIntervalInSeconds,
subscriptionExpiryCheckIntervalInSeconds, TimeUnit.SECONDS);
}
}

What is your perspective on adding this check to that loop? If we don't move the loop, I think we should document somewhere that the frequency of this method is governed by statsUpdateFrequencyInSecs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it doesn't depend on any feature. this background thread is being used for multiple simple topic checks such as backlogged-cursor or inactive-ledger checks. and it's internal implementation and we don't have to document it explicitly because it's just a safeguard check at the broker.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine if we want to leave it as is, but as it stands, the frequency of this method is determined by the broker config for statsUpdateFrequencyInSecs. In my view, that is unexpected coupling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as I explained earlier, this background thread performs topic's check including stats, backlogged-cursor-check and this will be an additional one. stats is something an essential part and its frequency should be reasonable enough to handle this kind of check. I don't see any usecase for why anyone disables the stats. however, if it's a real concern then will probably create another PR to separate all topic's checks out of this dedicated thread. but that can be addressed separately and definitely can not be a blocker for this PR.

} else if (topic instanceof NonPersistentTopic) {
tempNonPersistentTopics.add((NonPersistentTopic) topic);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,10 @@ public void checkBackloggedCursors() {
});
}

public void checkInactiveLedgers() {
ledger.checkInactiveLedgerAndRollOver();
}

@Override
public void checkDeduplicationSnapshot() {
messageDeduplication.takeSnapshot();
Expand Down
1 change: 1 addition & 0 deletions site2/docs/reference-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ brokerServiceCompactionThresholdInBytes|If the estimated backlog size is greater
|managedLedgerMaxEntriesPerLedger| The max number of entries to append to a ledger before triggering a rollover. A ledger rollover is triggered after the min rollover time has passed and one of the following conditions is true: <ul><li>The max rollover time has been reached</li><li>The max entries have been written to the ledger</li><li>The max ledger size has been written to the ledger</li></ul>|50000|
|managedLedgerMinLedgerRolloverTimeMinutes| Minimum time between ledger rollover for a topic |10|
|managedLedgerMaxLedgerRolloverTimeMinutes| Maximum time before forcing a ledger rollover for a topic |240|
|managedLedgerInactiveLedgerRolloverTimeSeconds| Time to rollover ledger for inactive topic |0|
|managedLedgerCursorMaxEntriesPerLedger| Max number of entries to append to a cursor ledger |50000|
|managedLedgerCursorRolloverTimeInSeconds| Max time before triggering a rollover on a cursor ledger |14400|
|managedLedgerMaxUnackedRangesToPersist| Max number of “acknowledgment holes” that are going to be persistently stored. When acknowledging out of order, a consumer will leave holes that are supposed to be quickly filled by acking all the messages. The information of which messages are acknowledged is persisted by compressing in “ranges” of messages that were acknowledged. After the max number of ranges is reached, the information will only be tracked in memory and messages will be redelivered in case of crashes. |1000|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,9 @@ public CompletableFuture<Void> asyncTruncate() {
public CompletableFuture<ManagedLedgerInternalStats> getManagedLedgerInternalStats(boolean includeLedgerMetadata) {
return null;
}

@Override
public void checkInactiveLedgerAndRollOver() {

}
}