-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Trigger rollover when meeting maxLedgerRolloverTimeMinutes #7111
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
Changes from all commits
ee2fc25
b155894
33f0615
9d225e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ | |
| import java.util.function.Supplier; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.bookkeeper.client.AsyncCallback; | ||
| import org.apache.bookkeeper.client.AsyncCallback.CreateCallback; | ||
| import org.apache.bookkeeper.client.AsyncCallback.OpenCallback; | ||
| import org.apache.bookkeeper.client.BKException; | ||
|
|
@@ -1391,6 +1392,38 @@ synchronized void ledgerClosed(final LedgerHandle lh) { | |
| } | ||
| } | ||
|
|
||
| synchronized void createLedgerAfterClosed() { | ||
| STATE_UPDATER.set(this, State.CreatingLedger); | ||
| this.lastLedgerCreationInitiationTimestamp = System.nanoTime(); | ||
| mbean.startDataLedgerCreateOp(); | ||
| asyncCreateLedger(bookKeeper, config, digestType, this, Collections.emptyMap()); | ||
| } | ||
|
|
||
| @Override | ||
| public void rollCurrentLedgerIfFull() { | ||
| log.info("[{}] Start checking if current ledger is full", name); | ||
| if (currentLedgerEntries > 0 && currentLedgerIsFull()) { | ||
| STATE_UPDATER.set(this, State.ClosingLedger); | ||
| currentLedger.asyncClose(new AsyncCallback.CloseCallback() { | ||
| @Override | ||
| public void closeComplete(int rc, LedgerHandle lh, Object o) { | ||
| checkArgument(currentLedger.getId() == lh.getId(), "ledgerId %s doesn't match with acked ledgerId %s", | ||
| currentLedger.getId(), | ||
| lh.getId()); | ||
|
|
||
| if (rc == BKException.Code.OK) { | ||
| log.debug("Successfuly closed ledger {}", lh.getId()); | ||
| } else { | ||
| log.warn("Error when closing ledger {}. Status={}", lh.getId(), BKException.getMessage(rc)); | ||
| } | ||
|
|
||
| ledgerClosed(lh); | ||
| createLedgerAfterClosed(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need initiate the creation of a ledger at this point. We can stay in LedgerClosed state until a new write comes in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes in current code logic we can stay in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I think we can change the current logic. If the current ledger is closed, we can delete it. I'm not sure is there any problems with this change. @merlimat Could you please help check this? |
||
| } | ||
| }, System.nanoTime()); | ||
| } | ||
| } | ||
|
|
||
| void clearPendingAddEntries(ManagedLedgerException e) { | ||
| while (!pendingAddEntries.isEmpty()) { | ||
| OpAddEntry op = pendingAddEntries.poll(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,7 @@ public class BrokerService implements Closeable, ZooKeeperCacheListener<Policies | |
| private final ScheduledExecutorService compactionMonitor; | ||
| private final ScheduledExecutorService messagePublishBufferMonitor; | ||
| private final ScheduledExecutorService consumedLedgersMonitor; | ||
| private final ScheduledExecutorService ledgerFullMonitor; | ||
| private ScheduledExecutorService topicPublishRateLimiterMonitor; | ||
| private ScheduledExecutorService brokerPublishRateLimiterMonitor; | ||
| protected volatile PublishRateLimiter brokerPublishRateLimiter = PublishRateLimiter.DISABLED_RATE_LIMITER; | ||
|
|
@@ -285,6 +286,8 @@ public BrokerService(PulsarService pulsar) throws Exception { | |
| Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-publish-buffer-monitor")); | ||
| this.consumedLedgersMonitor = Executors | ||
| .newSingleThreadScheduledExecutor(new DefaultThreadFactory("consumed-Ledgers-monitor")); | ||
| this.ledgerFullMonitor = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This thread is not being stopped. Potentially we could also reuse an existing executor.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 @wuzhanpeng can we re-use an existing executor? Otherwise, we end up creating a lot of executors. |
||
| Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("ledger-full-monitor")); | ||
|
|
||
| this.backlogQuotaManager = new BacklogQuotaManager(pulsar); | ||
| this.backlogQuotaChecker = Executors | ||
|
|
@@ -416,6 +419,7 @@ public void start() throws Exception { | |
| this.startCompactionMonitor(); | ||
| this.startMessagePublishBufferMonitor(); | ||
| this.startConsumedLedgersMonitor(); | ||
| this.startLedgerFullMonitor(); | ||
| this.startBacklogQuotaChecker(); | ||
| this.updateBrokerPublisherThrottlingMaxRate(); | ||
| this.startCheckReplicationPolicies(); | ||
|
|
@@ -493,6 +497,12 @@ protected void startConsumedLedgersMonitor() { | |
| } | ||
| } | ||
|
|
||
| protected void startLedgerFullMonitor() { | ||
| int interval = pulsar().getConfiguration().getManagedLedgerMaxLedgerRolloverTimeMinutes(); | ||
| ledgerFullMonitor.scheduleAtFixedRate(safeRun(this::checkLedgerFull), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the recurring task needs to be cancelled when BrokerService is closed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I newly add a |
||
| interval, interval, TimeUnit.MINUTES); | ||
| } | ||
|
|
||
| protected void startBacklogQuotaChecker() { | ||
| if (pulsar().getConfiguration().isBacklogQuotaCheckEnabled()) { | ||
| final int interval = pulsar().getConfiguration().getBacklogQuotaCheckIntervalInSeconds(); | ||
|
|
@@ -625,6 +635,7 @@ public void close() throws IOException { | |
| inactivityMonitor.shutdown(); | ||
| messageExpiryMonitor.shutdown(); | ||
| compactionMonitor.shutdown(); | ||
| ledgerFullMonitor.shutdown(); | ||
| backlogQuotaChecker.shutdown(); | ||
| authenticationService.close(); | ||
| pulsarStats.close(); | ||
|
|
@@ -1256,6 +1267,18 @@ private void checkConsumedLedgers() { | |
| }); | ||
| } | ||
|
|
||
| private void checkLedgerFull() { | ||
| forEachTopic((t) -> { | ||
| if (t instanceof PersistentTopic) { | ||
| Optional.ofNullable(((PersistentTopic) t).getManagedLedger()).ifPresent( | ||
| managedLedger -> { | ||
| managedLedger.rollCurrentLedgerIfFull(); | ||
| } | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void checkMessageDeduplicationInfo() { | ||
| forEachTopic(Topic::checkMessageDeduplicationInfo); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * 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 lombok.Cleanup; | ||
| import org.apache.bookkeeper.mledger.ManagedLedgerConfig; | ||
| import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl; | ||
| import org.apache.bookkeeper.mledger.util.Futures; | ||
| import org.apache.pulsar.broker.service.persistent.PersistentTopic; | ||
| import org.apache.pulsar.client.api.Consumer; | ||
| import org.apache.pulsar.client.api.Message; | ||
| import org.apache.pulsar.client.api.Producer; | ||
| import org.junit.Test; | ||
| import org.testng.Assert; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class CurrentLedgerRolloverIfFullTest extends BrokerTestBase { | ||
| @Override | ||
| protected void setup() throws Exception { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| protected void cleanup() throws Exception { | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCurrentLedgerRolloverIfFull() throws Exception { | ||
| super.baseSetup(); | ||
| final String topicName = "persistent://prop/ns-abc/CurrentLedgerRolloverIfFullTest"; | ||
|
|
||
| @Cleanup | ||
| Producer<byte[]> producer = pulsarClient.newProducer() | ||
| .topic(topicName) | ||
| .producerName("CurrentLedgerRolloverIfFullTest-producer-name") | ||
| .create(); | ||
|
|
||
| @Cleanup | ||
| Consumer<byte[]> consumer = pulsarClient.newConsumer() | ||
| .topic(topicName) | ||
| .subscriptionName("CurrentLedgerRolloverIfFullTest-subscriber-name") | ||
| .subscribe(); | ||
|
|
||
| Topic topicRef = pulsar.getBrokerService().getTopicReference(topicName).get(); | ||
| Assert.assertNotNull(topicRef); | ||
| PersistentTopic persistentTopic = (PersistentTopic) pulsar.getBrokerService().getOrCreateTopic(topicName).get(); | ||
|
|
||
| ManagedLedgerConfig managedLedgerConfig = persistentTopic.getManagedLedger().getConfig(); | ||
| managedLedgerConfig.setRetentionTime(1, TimeUnit.SECONDS); | ||
| managedLedgerConfig.setMaxEntriesPerLedger(2); | ||
| managedLedgerConfig.setMinimumRolloverTime(1, TimeUnit.MILLISECONDS); | ||
| managedLedgerConfig.setMaximumRolloverTime(5, TimeUnit.MILLISECONDS); | ||
|
|
||
| int msgNum = 10; | ||
| for (int i = 0; i < msgNum; i++) { | ||
| producer.send(new byte[1024 * 1024]); | ||
| } | ||
|
|
||
| ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) persistentTopic.getManagedLedger(); | ||
| Assert.assertEquals(managedLedger.getLedgersInfoAsList().size(), msgNum / 2); | ||
|
|
||
| for (int i = 0; i < msgNum; i++) { | ||
| Message<byte[]> msg = consumer.receive(2, TimeUnit.SECONDS); | ||
| Assert.assertTrue(msg != null); | ||
| consumer.acknowledge(msg); | ||
| } | ||
|
|
||
| // all the messages have been acknowledged | ||
| // and all the ledgers have been removed except the the last ledger | ||
| Thread.sleep(500); | ||
| Assert.assertEquals(managedLedger.getLedgersInfoAsList().size(), 1); | ||
| Assert.assertNotEquals(managedLedger.getCurrentLedgerSize(), 0); | ||
|
|
||
| // trigger a ledger rollover | ||
| // and now we have two ledgers, one with expired data and one for empty | ||
| managedLedger.rollCurrentLedgerIfFull(); | ||
| Thread.sleep(1000); | ||
| Assert.assertEquals(managedLedger.getLedgersInfoAsList().size(), 2); | ||
|
|
||
| // trigger a ledger trimming | ||
| // and now we only have the empty ledger | ||
| managedLedger.trimConsumedLedgersInBackground(Futures.NULL_PROMISE); | ||
| Assert.assertEquals(managedLedger.getCurrentLedgerSize(), 0); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to expose this in the interface, it should be better to keep in implementation details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope to be able to consult you here for advice, because I am not sure whether doing type casting in the
BrokerServiceis a suitable implementation. Maybe I can modify with:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that this is a managedLedger internal task, for which the
BrokerServiceshouldn't be concerned. For that it would be better to handle in theManagedLedgerFactoryImpl, to go through all open managed ledger instances and check if a rollover has to be forced.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I feel a little confused. If we treat rollover as an internal task, should
consumedLedgersMonitororbacklogQuotaCheckerbe internal tasks as well? Or is my understanding biased?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wuzhanpeng I think you can add an issue to track the monitor that outside the managed ledger but should be maintained by managed ledger own.