-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delayed message delivery implementation (#4062)
* Delayed message delivery implementation * Fixed compilation * Allow to configure the delayed tracker implementation * Use int64 for timestamp * Address comments * More tests for TripleLongPriorityQueue * Removing useless sync block that causes deadlock with consumer close * Fixed merge conflict * Avoid new list when passing entries to consumer * Fixed test. Since entries are evicted from cache, they might be resent in diff order * Fixed context message builder * Fixed triggering writePromise when last entry was nullified * Moved entries filtering from consumer to dispatcher * Added Javadocs * Reduced synchronized scope to minimum
- Loading branch information
Showing
32 changed files
with
1,471 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 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.delayed; | ||
|
||
import com.google.common.annotations.Beta; | ||
|
||
import java.util.Set; | ||
|
||
import org.apache.bookkeeper.mledger.impl.PositionImpl; | ||
|
||
/** | ||
* Represent the tracker for the delayed delivery of messages for a particular subscription. | ||
* | ||
* Note: this interface is still being refined and some breaking changes might be introduced. | ||
*/ | ||
@Beta | ||
public interface DelayedDeliveryTracker extends AutoCloseable { | ||
|
||
/** | ||
* Add a message to the tracker | ||
* | ||
* @param ledgerId | ||
* the ledgerId | ||
* @param entryId | ||
* the entryId | ||
* @param deliveryAt | ||
* the absolute timestamp at which the message should be tracked | ||
* @return true if the message was added to the tracker or false if it should be delivered immediately | ||
*/ | ||
boolean addMessage(long ledgerId, long entryId, long deliveryAt); | ||
|
||
/** | ||
* Return true if there's at least a message that is scheduled to be delivered already | ||
*/ | ||
boolean hasMessageAvailable(); | ||
|
||
/** | ||
* @return the number of delayed messages being tracked | ||
*/ | ||
long getNumberOfDelayedMessages(); | ||
|
||
/** | ||
* Get a set of position of messages that have already reached the delivery time | ||
*/ | ||
Set<PositionImpl> getScheduledMessages(int maxMessages); | ||
|
||
/** | ||
* Close the subscription tracker and release all resources. | ||
*/ | ||
void close(); | ||
} |
55 changes: 55 additions & 0 deletions
55
...-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* 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.delayed; | ||
|
||
import com.google.common.annotations.Beta; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers; | ||
|
||
/** | ||
* Factory of InMemoryDelayedDeliveryTracker objects. This is the entry point for implementations. | ||
* | ||
* Note: this interface is still being refined and some breaking changes might be introduced. | ||
*/ | ||
@Beta | ||
public interface DelayedDeliveryTrackerFactory extends AutoCloseable { | ||
/** | ||
* Initialize the factory implementation from the broker service configuration | ||
* | ||
* @param config | ||
* the broker service config object | ||
*/ | ||
void initialize(ServiceConfiguration config) throws IOException; | ||
|
||
/** | ||
* Create a new tracker instance. | ||
* | ||
* @param dispatcher | ||
* a multi-consumer dispatcher instance | ||
*/ | ||
DelayedDeliveryTracker newTracker(PersistentDispatcherMultipleConsumers dispatcher); | ||
|
||
/** | ||
* Close the factory and release all the resources | ||
*/ | ||
void close() throws IOException; | ||
} |
47 changes: 47 additions & 0 deletions
47
...r-broker/src/main/java/org/apache/pulsar/broker/delayed/DelayedDeliveryTrackerLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* 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.delayed; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import java.io.IOException; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
|
||
@UtilityClass | ||
public class DelayedDeliveryTrackerLoader { | ||
public static DelayedDeliveryTrackerFactory loadDelayedDeliveryTrackerFactory(ServiceConfiguration conf) | ||
throws IOException { | ||
Class<?> factoryClass; | ||
try { | ||
factoryClass = Class.forName(conf.getDelayedDeliveryTrackerFactoryClassName()); | ||
Object obj = factoryClass.newInstance(); | ||
checkArgument(obj instanceof DelayedDeliveryTrackerFactory, | ||
"The factory has to be an instance of " + DelayedDeliveryTrackerFactory.class.getName()); | ||
|
||
DelayedDeliveryTrackerFactory factory = (DelayedDeliveryTrackerFactory) obj; | ||
factory.initialize(conf); | ||
return factory; | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.