Skip to content
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

Delayed message delivery implementation #4062

Merged
merged 24 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3290126
Delayed message delivery implementation
merlimat Apr 14, 2019
b54c438
Fixed compilation
merlimat Apr 17, 2019
d5e1ebc
Allow to configure the delayed tracker implementation
merlimat Apr 18, 2019
52832fe
Use int64 for timestamp
merlimat Apr 18, 2019
e249fcc
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat Apr 26, 2019
31db950
Address comments
merlimat Apr 26, 2019
ab584c1
More tests for TripleLongPriorityQueue
merlimat Apr 30, 2019
ed16288
Removing useless sync block that causes deadlock with consumer close
merlimat May 1, 2019
d0752ec
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 1, 2019
88a787b
Fixed merge conflict
merlimat May 1, 2019
c4e4fa8
Avoid new list when passing entries to consumer
merlimat May 3, 2019
1bbb032
Fixed test. Since entries are evicted from cache, they might be resen…
merlimat May 3, 2019
af47030
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 7, 2019
8180c83
Fixed context message builder
merlimat May 7, 2019
e99e199
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 15, 2019
640b4bd
Fixed triggering writePromise when last entry was nullified
merlimat May 20, 2019
3f35fb2
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 20, 2019
dcfa102
Moved entries filtering from consumer to dispatcher
merlimat May 21, 2019
7159782
Merge branch 'refactor-filtering' into delayed-delivery
merlimat May 21, 2019
b3d99b7
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 22, 2019
e36a44c
Added Javadocs
merlimat May 22, 2019
7cad297
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 23, 2019
17578a4
Merge remote-tracking branch 'apache/master' into delayed-delivery
merlimat May 28, 2019
b10712a
Reduced synchronized scope to minimum
merlimat May 28, 2019
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
10 changes: 10 additions & 0 deletions conf/broker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ maxConsumersPerSubscription=0
# Interval between checks to see if topics with compaction policies need to be compacted
brokerServiceCompactionMonitorIntervalInSeconds=60

# Whether to enable the delayed delivery for messages.
# If disabled, messages will be immediately delivered and there will
# be no tracking overhead.
delayedDeliveryEnabled=true

# Control the tick time for when retrying on delayed delivery,
# affecting the accuracy of the delivery time compared to the scheduled time.
# Default is 1 second.
delayedDeliveryTickTimeMillis=1000

### --- Authentication --- ###
# Role names that are treated as "proxy roles". If the broker sees a request with
#role as proxyRoles - it will demand to see a valid original principal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ public class ServiceConfiguration implements PulsarConfiguration {
// waiting for another HTTP call to complete in same thread.
private int numHttpServerThreads = Math.max(8, 2 * Runtime.getRuntime().availableProcessors());

@FieldContext(category = CATEGORY_SERVER, doc = "Whether to enable the delayed delivery for messages.")
private boolean delayedDeliveryEnabled = true;

@FieldContext(category = CATEGORY_SERVER, doc = "Class name of the factory that implements the delayed deliver tracker")
private String delayedDeliveryTrackerFactoryClassName = "org.apache.pulsar.broker.delayed.InMemoryDelayedDeliveryTrackerFactory";

@FieldContext(category = CATEGORY_SERVER, doc = "Control the tick time for when retrying on delayed delivery, "
+ " affecting the accuracy of the delivery time compared to the scheduled time. Default is 1 second.")
private long delayedDeliveryTickTimeMillis = 1000;

@FieldContext(
category = CATEGORY_WEBSOCKET,
doc = "Enable the WebSocket API service in broker"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

import java.io.IOException;
import java.net.URI;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -95,9 +98,9 @@
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.compaction.Compactor;
import org.apache.pulsar.compaction.TwoPhaseCompactor;
import org.apache.pulsar.functions.worker.WorkerUtils;
import org.apache.pulsar.functions.worker.WorkerConfig;
import org.apache.pulsar.functions.worker.WorkerService;
import org.apache.pulsar.functions.worker.WorkerUtils;
import org.apache.pulsar.websocket.WebSocketConsumerServlet;
import org.apache.pulsar.websocket.WebSocketProducerServlet;
import org.apache.pulsar.websocket.WebSocketReaderServlet;
Expand Down Expand Up @@ -543,7 +546,7 @@ private void startZkCacheService() throws PulsarServerException {
this.localZkCache = new LocalZooKeeperCache(getZkClient(), config.getZooKeeperOperationTimeoutSeconds(),
getOrderedExecutor());
this.globalZkCache = new GlobalZooKeeperCache(getZooKeeperClientFactory(),
(int) config.getZooKeeperSessionTimeoutMillis(),
(int) config.getZooKeeperSessionTimeoutMillis(),
config.getZooKeeperOperationTimeoutSeconds(), config.getConfigurationStoreServers(),
getOrderedExecutor(), this.cacheExecutor);
try {
Expand Down Expand Up @@ -883,7 +886,7 @@ public static String brokerUrlTls(String host, int port) {
}

public static String webAddress(ServiceConfiguration config) {
if (config.getWebServicePort().isPresent()) {
if (config.getWebServicePort().isPresent()) {
return webAddress(advertisedAddress(config), config.getWebServicePort().get());
} else {
return null;
Expand Down
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();
}
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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 {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think since we are adding a new interface in this PR. It might be worth considering using NarClassLoader to load the class. So people can implement the DelayedDeliveryTracker in a Nar package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using Nar will bring more "overhead" in the sense that we now need to have new maven module, place the nar somewhere in the distribution and have duplication of dependencies (eg: if the tracker wants to use BK).

I'd defer this until we really need it. My expectation would be here to have multiple implementation of tracker, all living within the same codebase. If that proves not true, we can always change at later time.

public static DelayedDeliveryTrackerFactory loadDelayedDeliveryTrackerFactory(ServiceConfiguration conf)
throws IOException {
// try {
merlimat marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
Loading