-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JMS Selectors: apply selectors on the write path
- Loading branch information
Showing
7 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...r-jms-filters/src/main/java/com/datastax/oss/pulsar/jms/selectors/WritePathSelectors.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,142 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed 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 com.datastax.oss.pulsar.jms.selectors; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.bookkeeper.mledger.Entry; | ||
import org.apache.bookkeeper.mledger.impl.PositionImpl; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.intercept.BrokerInterceptor; | ||
import org.apache.pulsar.broker.service.Producer; | ||
import org.apache.pulsar.broker.service.ServerCnx; | ||
import org.apache.pulsar.broker.service.Topic; | ||
import org.apache.pulsar.broker.service.plugin.EntryFilter; | ||
import org.apache.pulsar.broker.service.plugin.FilterContext; | ||
import org.apache.pulsar.common.api.proto.BaseCommand; | ||
import org.apache.pulsar.common.api.proto.CommandAck; | ||
import org.apache.pulsar.common.api.proto.MarkerType; | ||
import org.apache.pulsar.common.api.proto.MessageMetadata; | ||
import org.apache.pulsar.common.intercept.InterceptException; | ||
import org.apache.pulsar.common.protocol.Commands; | ||
|
||
@Slf4j | ||
public class WritePathSelectors implements BrokerInterceptor { | ||
|
||
private final JMSFilter filter = new JMSFilter(); | ||
|
||
@Override | ||
public void initialize(PulsarService pulsarService) throws Exception { | ||
log.info("initialize"); | ||
} | ||
|
||
@Override | ||
public void onMessagePublish( | ||
Producer producer, ByteBuf headersAndPayload, Topic.PublishContext publishContext) { | ||
log.info("onMessagePublish {} {}", producer.getProducerName(), producer.getTopic().getName()); | ||
MessageMetadata messageMetadata = | ||
Commands.peekMessageMetadata(headersAndPayload, "jms-filter-on-publish", -1); | ||
if (messageMetadata.hasNumMessagesInBatch() && messageMetadata.getNumMessagesInBatch() > 0) { | ||
log.info("Skip batch message"); | ||
return; | ||
} | ||
if (messageMetadata.hasMarkerType() | ||
&& messageMetadata.getMarkerType() != MarkerType.UNKNOWN_MARKER_VALUE) { | ||
log.info("Skip marker message"); | ||
return; | ||
} | ||
|
||
producer | ||
.getTopic() | ||
.getSubscriptions() | ||
.forEach( | ||
(name, subscription) -> { | ||
Map<String, String> subscriptionProperties = subscription.getSubscriptionProperties(); | ||
FilterContext filterContext = new FilterContext(); | ||
filterContext.setSubscription(subscription); | ||
filterContext.setMsgMetadata(messageMetadata); | ||
filterContext.setConsumer(null); | ||
Entry entry = null; // we would need the Entry only in case of batch messages | ||
EntryFilter.FilterResult filterResult = filter.filterEntry(entry, filterContext); | ||
log.info("Filter result for subscription {} is {}", name, filterResult); | ||
String property = "filter-result-" + name + "@" + subscription.getTopicName(); | ||
publishContext.setProperty(property, filterResult); | ||
}); | ||
} | ||
|
||
@Override | ||
public void messageProduced( | ||
ServerCnx cnx, | ||
Producer producer, | ||
long startTimeNs, | ||
long ledgerId, | ||
long entryId, | ||
Topic.PublishContext publishContext) { | ||
log.info( | ||
"messageProduced {} {}: {}:{}", | ||
producer.getProducerName(), | ||
producer.getTopic().getName(), | ||
ledgerId, | ||
entryId); | ||
producer | ||
.getTopic() | ||
.getSubscriptions() | ||
.forEach( | ||
(name, subscription) -> { | ||
String property = "filter-result-" + name + "@" + subscription.getTopicName(); | ||
EntryFilter.FilterResult filterResult = | ||
(EntryFilter.FilterResult) publishContext.getProperty(property); | ||
log.info( | ||
"Filter result for subscription {} is {} for entry {}:{}", | ||
name, | ||
filterResult, | ||
ledgerId, | ||
entryId); | ||
if (filterResult == EntryFilter.FilterResult.REJECT) { | ||
log.info("Reject message {}:{} for subscription {}", ledgerId, entryId, name); | ||
subscription.acknowledgeMessage( | ||
Arrays.asList(new PositionImpl(ledgerId, entryId)), | ||
CommandAck.AckType.Individual, | ||
null); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
filter.close(); | ||
} | ||
|
||
@Override | ||
public void onPulsarCommand(BaseCommand command, ServerCnx cnx) throws InterceptException {} | ||
|
||
@Override | ||
public void onConnectionClosed(ServerCnx cnx) {} | ||
|
||
@Override | ||
public void onWebserviceRequest(ServletRequest request) | ||
throws IOException, ServletException, InterceptException {} | ||
|
||
@Override | ||
public void onWebserviceResponse(ServletRequest request, ServletResponse response) | ||
throws IOException, ServletException {} | ||
} |
3 changes: 3 additions & 0 deletions
3
pulsar-jms-filters/src/main/resources/META-INF/services/broker_interceptor.yml
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,3 @@ | ||
interceptorClass: com.datastax.oss.pulsar.jms.selectors.WritePathSelectors | ||
name: jms-write-path-selectors | ||
description: Starlight for JMS - support for server side filters on the write path |
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
115 changes: 115 additions & 0 deletions
115
pulsar-jms/src/test/java/com/datastax/oss/pulsar/jms/WritePathSelectorsTest.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,115 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed 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 com.datastax.oss.pulsar.jms; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import com.datastax.oss.pulsar.jms.utils.PulsarContainerExtension; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import javax.jms.MessageProducer; | ||
import javax.jms.Queue; | ||
import javax.jms.TextMessage; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.client.api.SubscriptionType; | ||
import org.apache.pulsar.common.policies.data.SubscriptionStats; | ||
import org.apache.pulsar.common.policies.data.TopicStats; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
@Slf4j | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class WritePathSelectorsTest { | ||
|
||
@RegisterExtension | ||
static PulsarContainerExtension pulsarContainer = | ||
new PulsarContainerExtension() | ||
.withEnv("PULSAR_PREFIX_transactionCoordinatorEnabled", "false") | ||
.withEnv("PULSAR_PREFIX_brokerInterceptorsDirectory", "/pulsar/interceptors") | ||
.withEnv("PULSAR_PREFIX_brokerInterceptors", "jms-write-path-selectors") | ||
.withEnv("PULSAR_LOG_LEVEL", "info"); | ||
|
||
private Map<String, Object> buildProperties() { | ||
Map<String, Object> properties = pulsarContainer.buildJMSConnectionProperties(); | ||
properties.put("jms.useServerSideFiltering", true); | ||
properties.put("jms.enableClientSideEmulation", false); | ||
|
||
Map<String, Object> producerConfig = new HashMap<>(); | ||
producerConfig.put("batchingEnabled", false); | ||
properties.put("producerConfig", producerConfig); | ||
return properties; | ||
} | ||
|
||
@Test | ||
public void sendMessageReceiveFromQueue() throws Exception { | ||
Map<String, Object> properties = buildProperties(); | ||
|
||
String topicName = "persistent://public/default/test-" + UUID.randomUUID(); | ||
try (PulsarConnectionFactory factory = new PulsarConnectionFactory(properties); ) { | ||
try (PulsarConnection connection = factory.createConnection()) { | ||
connection.start(); | ||
try (PulsarSession session = connection.createSession(); ) { | ||
Queue destination = session.createQueue(topicName); | ||
|
||
try (PulsarMessageConsumer consumer1 = session.createConsumer(destination); ) { | ||
assertEquals( | ||
SubscriptionType.Shared, ((PulsarMessageConsumer) consumer1).getSubscriptionType()); | ||
|
||
String newSelector = "lastMessage=TRUE"; | ||
Map<String, String> subscriptionProperties = new HashMap<>(); | ||
subscriptionProperties.put("jms.selector", newSelector); | ||
subscriptionProperties.put("jms.filtering", "true"); | ||
|
||
pulsarContainer | ||
.getAdmin() | ||
.topics() | ||
.updateSubscriptionProperties(topicName, "jms-queue", subscriptionProperties); | ||
|
||
try (MessageProducer producer = session.createProducer(destination); ) { | ||
for (int i = 0; i < 10; i++) { | ||
TextMessage textMessage = session.createTextMessage("foo-" + i); | ||
if (i == 9) { | ||
textMessage.setBooleanProperty("lastMessage", true); | ||
} | ||
producer.send(textMessage); | ||
} | ||
} | ||
|
||
TextMessage textMessage = (TextMessage) consumer1.receive(); | ||
assertEquals("foo-9", textMessage.getText()); | ||
|
||
assertEquals(1, consumer1.getReceivedMessages()); | ||
assertEquals(0, consumer1.getSkippedMessages()); | ||
|
||
// no more messages | ||
assertNull(consumer1.receiveNoWait()); | ||
|
||
// ensure that the filter didn't reject any message while dispatching to the consumer | ||
// because the filter has been already applied on the write path | ||
TopicStats stats = pulsarContainer.getAdmin().topics().getStats(topicName); | ||
SubscriptionStats subscriptionStats = stats.getSubscriptions().get("jms-queue"); | ||
assertEquals(subscriptionStats.getFilterProcessedMsgCount(), 1); | ||
assertEquals(subscriptionStats.getFilterRejectedMsgCount(), 0); | ||
assertEquals(subscriptionStats.getFilterAcceptedMsgCount(), 1); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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