Skip to content

[pulsar-broker] Support roll-over ledgers for inactive topics - #13073

Merged
michaeljmarshall merged 1 commit into
apache:masterfrom
rdhabalia:ledger_age
Feb 10, 2022
Merged

michaeljmarshall merged 1 commit into
apache:masterfrom
rdhabalia:ledger_age

Conversation

@rdhabalia

Copy link
Copy Markdown
Contributor

Motivation

we need the support of ledger roll-over for inactive topics to prevent data-loss. It happens when Inactive topics that do not have publish for a long time and the current ledger segment ensemble bookies are replaced by replicator when ensemble bookies went into maintenance mode. In this scenario, there is a chance of data loss and rolling over such ledgers prevents data loss.
For example:

  1. In the beginning topic is writing to ledger 1000 with bookie b1 and b2
get /ledgers/00/0000/L1000
BookieMetadataFormatVersion	2
quorumSize: 2
ensembleSize: 2
length: 0
lastEntryId: -1
state: OPEN
segment {
  ensembleMember: "b1:3181" -> prod5-bookie7.messaging.gq1.yahoo.com
  ensembleMember: "b2:3181" -> prod5-bookie3.messaging.gq1.yahoo.com
  firstEntryId: 0
} 
  1. Then both bookie b1 and b2 went down and replicator replaced them with bookie b3 and b4
get /ledgers/00/0000/L1000
quorumSize: 2
ensembleSize: 2
length: 104
lastEntryId: 1
state: CLOSED
segment {
  ensembleMember: "b3:3181"
  ensembleMember: "b4:3181"
  firstEntryId: 0
}
segment {
  ensembleMember: "b5:3181"
  ensembleMember: "b6:3181"
  firstEntryId: 1
}
  1. after that bookie b1 and b2 came back again and topic received a publish. broker was able to reconnect to bookie b1, and b2 and continued to write to bookie b1 and b2
Broker-DEBUG log:
22:45:24.000 [bookkeeper-ml-workers-OrderedExecutor-6-0] DEBUG org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl - [prop/us-west1/test/persistent/t1] Write into current ledger lh=291607 entries=14
# broker successfully connected to b1 and b2 and wrote new entry to b1 and b2

22:36:38.949 [bookie-io-1-6] INFO  o.a.b.proto.BookieRequestHandler     - Channel connected  [id: 0x43c62e1c, L:/10.10.10.10:3181 - R:/b1:60908]
22:36:38.950 [bookie-io-1-5] INFO  o.a.b.proto.BookieRequestHandler     - Channel connected  [id: 0x43c62e1c, L:/10.10.10.10:3181 - R:/b2:60908]
  1. So, according to zk ledger 1000 has one entry but the broker is kept writing to b1 and b2 bookies which can cause the data loss.

Bookie PR#2616 can be helpful in this scenario but rolling over inactive ledger helps to user who are doing bookie replacement/maintenance more frequent to avoid kind of data loss.

Modification

  • added config managedLedgerInActiveLedgerRolloverTimeMinutes to enable rollover of inactive ledgers (by default it will be 0)

Result

this feature helps in case of inactive topics which write to replaced bookies during any maintenance and helps to avoid data-loss in that situation.

@rdhabalia rdhabalia added this to the 2.10.0 milestone Dec 1, 2021
@rdhabalia rdhabalia self-assigned this Dec 1, 2021
@github-actions

github-actions Bot commented Dec 1, 2021

Copy link
Copy Markdown

@rdhabalia:Thanks for your contribution. For this PR, do we need to update docs?
(The PR template contains info about doc, which helps others know more about the changes. Can you provide doc-related info in this and future PR descriptions? Thanks)

@michaeljmarshall michaeljmarshall left a comment •

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.

@rdhabalia - can you help me understand how this feature is different from the one associated with managedLedgerMaxLedgerRolloverTimeMinutes? The managed ledger uses max rollover time to scheduled a task that will close the ledger once that much time has passed (assuming the rollover criteria for size or number of entries have not yet been met). I think lowering that max time achieves the behavior proposed in this PR.

EDIT: I took a second look and can see how inactivity is different from the max time for the ledger to be open. However, this feels like an issue that the bookkeeper client should solve. A timeout doesn't completely remove the risk and, if I understand the risk correctly, it seems like the inactivity timeout would have to be pretty low to be effective.

Comment thread conf/broker.conf Outdated

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

@michaeljmarshall michaeljmarshall Dec 1, 2021 •

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: inactive is used as a single word here. I think the casing should be Inactive here and in the rest of the PR.

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.

done

@rdhabalia

rdhabalia commented Dec 1, 2021 •

Copy link
Copy Markdown
Contributor Author

can you help me understand how this feature is different from the one associated with managedLedgerMaxLedgerRolloverTimeMinutes? The managed ledger uses max rollover time to scheduled a task that will close the ledger once that much time has passed (assuming the rollover criteria for size or number of entries have not yet been met). I think lowering that max time achieves the behavior proposed in this PR.

No, both these configs are made to solve two different problems.managedLedgerMaxLedgerRolloverTimeMinutes targets max time ledger can stay opened which can be 5 hours. and managedLedgerInActiveLedgerRolloverTimeMinutes max time ledger can stay opened without any activity which can be 30 mins.

However, this feels like an issue that the bookkeeper client should solve. A timeout doesn't completely remove the risk and, if I understand the risk correctly, it seems like the inactivity timeout would have to be pretty low to be effective.

Yes, that problem should be handled at bookkeeper client but broker can have this capability to not only solve this particular problem but allow user to control life span of inactive topic ledgers.

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

// Jump to specific thread to avoid contention from writers writing from different threads
executor.executeOrdered(name, safeRun(() -> {
OpAddEntry addOperation = OpAddEntry.createNoRetainBuffer(this, buffer, callback, ctx);
checkInactiveLedgerAndRollOver(lastAddEntryTimeMs);

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.

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. we can move it to avoid adding at the caller method.

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.

done

private void checkInactiveLedgerAndRollOver(final long lastAddEntryTimeMs) {
long currentTimeMs = System.currentTimeMillis();
if (inactiveLedgerRollOverTimeMs > 0 && currentTimeMs > (lastAddEntryTimeMs + inactiveLedgerRollOverTimeMs)) {
log.info("[{}| Closing inactive ledger, last-add entry {}", name, lastAddEntryTimeMs);

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.

typo, use ] instead of |

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.

done

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.

int totalAddEntries = 5;
for (int i = 0; i < totalAddEntries; i++) {
String content = "entry"; // 5 bytes
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.

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.

@eolivelli eolivelli left a comment

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.

@rdhabalia it seems to me that this is some kind of workaround to a BookKeeper bug, is this correct ?
Will upgrading to latest BK help ?
We are going to cut a release of BK soon.
I am not sure we need this change if it is only reducing the risk of data loss.

btw rolling inactive ledgers may have some benefit as they will be move to tier-2 and so I am fine with this change.

+1 (please fix the comments reported by @nicoloboschi, there is a discrepancy between Seconds vs Minutes)

@Anonymitaet Anonymitaet added doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. and removed doc-label-missing labels Dec 2, 2021
@michaeljmarshall

Copy link
Copy Markdown
Member

@rdhabalia - thank you for your explanation.

@michaeljmarshall michaeljmarshall left a comment

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.

I think it makes sense close out inactive ledgers. I wonder if we should check for inactivity in a different way though.

@FieldContext(
dynamic = true,
category = CATEGORY_STORAGE_ML,
doc = "Time to rollover ledger for inactive topic (duration without any publish on that topic)."

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.

Suggested change
doc = "Time to rollover ledger for inactive topic (duration without any publish on that topic)."
doc = "Time to rollover ledger for inactive topic (duration without any publish on that topic). "

return;
}
// check if ledger can be closed due to inactivity
checkInactiveLedgerAndRollOver(lastAddEntryTimeMs);

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.

The implementation relies on messages to trigger ledger closure due to inactivity. While it might be a slightly heavier solution, would it make more sense to have a single scheduled task that runs in the broker service to check for inactive ledgers? Otherwise, a ledger that stops receiving messages will not close until the managedLedgerMaxLedgerRolloverTimeMinutes has passed.

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.

hmm.. actually lazy check should be enough because eventually ledger will be closed if topic in unloaded, broker restarts or topic receives a new write and that will protect topic from the issue. However, adding scheduled task will not have much harm except it might repetitive close empty ledger but might give certainty to ledger age.
I have made the change and moved check to scheduled task.

@rdhabalia

Copy link
Copy Markdown
Contributor Author

/pulsarbot run-failure-checks

@rdhabalia
rdhabalia force-pushed the ledger_age branch 2 times, most recently from e4648b7 to 3c2b844 Compare December 21, 2021 22:56
@rdhabalia

Copy link
Copy Markdown
Contributor Author

@michaeljmarshall PTAL

@michaeljmarshall

Copy link
Copy Markdown
Member

I plan to review this later tonight. Sorry for the delay @rdhabalia

@michaeljmarshall michaeljmarshall left a comment

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.

@rdhabalia - this looks great to me. I have one question about where we schedule the call to check for inactive ledgers. Otherwise, I think we should be good to go.

// 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.

@codelipenghui

Copy link
Copy Markdown
Contributor

@rdhabalia Please check @michaeljmarshall's comment and resolve the conflicts

@codelipenghui codelipenghui modified the milestones: 2.10.0, 2.11.0 Jan 25, 2022
@rdhabalia

Copy link
Copy Markdown
Contributor Author

@michaeljmarshall please unblock the PR.

@michaeljmarshall

Copy link
Copy Markdown
Member

@rdhabalia - do you have a reply for my comment here #13073 (comment)?

@rdhabalia

Copy link
Copy Markdown
Contributor Author

@michaeljmarshall yes, I have replied it. unnecessary blocking PR causes delay in feature and releases.

change inactiveLedgerRollOverTimeMs config

address comments: timeunit + logging + moving marking at internal method

make inactive-ledger check task scheduled

@michaeljmarshall michaeljmarshall left a comment

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.

@rdhabalia - I did not intend to block your PR--I asked what I believe was a legitimate question.

@michaeljmarshall
michaeljmarshall merged commit 792e264 into apache:master Feb 10, 2022
@michaeljmarshall michaeljmarshall modified the milestones: 2.11.0, 2.10.0 Feb 10, 2022
@michaeljmarshall michaeljmarshall added the type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages label Feb 10, 2022
Nicklee007 pushed a commit to Nicklee007/pulsar that referenced this pull request Apr 20, 2022
…#13073)

change inactiveLedgerRollOverTimeMs config

address comments: timeunit + logging + moving marking at internal method

make inactive-ledger check task scheduled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/broker doc Your PR contains doc changes, no matter whether the changes are in markdown or code files. type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants