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

Donot create transaction components for function work topic #11543

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.persistent;

import org.apache.logging.log4j.core.util.Assert;
import org.apache.pulsar.functions.worker.WorkerConfig;

public class CheckTopicIsSpecial {
public static boolean checkTopicIsFunctionWorkerService(PersistentTopic topic) {
if (!Assert.isNonEmpty(topic)){
throw new IllegalArgumentException("topic can`t be null");
}
WorkerConfig workerConfig = topic.getBrokerService().getPulsar().getWorkerConfig().get();
String topicName = topic.getName();
return workerConfig.getClusterCoordinationTopic().equals(topicName)
|| workerConfig.getFunctionAssignmentTopic().equals(topicName)
|| workerConfig.getFunctionMetadataTopic().equals(topicName);
Comment on lines +29 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

This can only work for the broker run with the function worker? If the function working running independently, here will get an incorrect worker config.

I think we already have a PR to support transaction buffer and pending ack lazy creation, if new transactions happens on a topic, the transaction buffer and the pending ack will not be created?

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.broker.service.persistent;

import static org.apache.pulsar.broker.service.persistent.CheckTopicIsSpecial.checkTopicIsFunctionWorkerService;
import static org.apache.pulsar.common.events.EventsTopicNames.checkTopicIsEventsNames;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -149,7 +150,8 @@ public PersistentSubscription(PersistentTopic topic, String subscriptionName, Ma
this.expiryMonitor = new PersistentMessageExpiryMonitor(topicName, subscriptionName, cursor, this);
this.setReplicated(replicated);
if (topic.getBrokerService().getPulsar().getConfig().isTransactionCoordinatorEnabled()
&& !checkTopicIsEventsNames(TopicName.get(topicName))) {
&& !checkTopicIsEventsNames(TopicName.get(topicName))
&& !checkTopicIsFunctionWorkerService(topic)) {
this.pendingAckHandle = new PendingAckHandleImpl(this);
} else {
this.pendingAckHandle = new PendingAckHandleDisabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES;
import static org.apache.pulsar.broker.service.persistent.CheckTopicIsSpecial.checkTopicIsFunctionWorkerService;
import static org.apache.pulsar.common.events.EventsTopicNames.checkTopicIsEventsNames;
import static org.apache.pulsar.compaction.Compactor.COMPACTION_SUBSCRIPTION;
import com.carrotsearch.hppc.ObjectObjectHashMap;
Expand Down Expand Up @@ -241,7 +242,8 @@ public void reset() {
}
}

public PersistentTopic(String topic, ManagedLedger ledger, BrokerService brokerService) throws NamingException {
public PersistentTopic(String topic, ManagedLedger ledger, BrokerService brokerService)
throws NamingException {
super(topic, brokerService);
this.ledger = ledger;
this.subscriptions = new ConcurrentOpenHashMap<>(16, 1);
Expand Down Expand Up @@ -291,7 +293,8 @@ public PersistentTopic(String topic, ManagedLedger ledger, BrokerService brokerS
checkReplicatedSubscriptionControllerState();
TopicName topicName = TopicName.get(topic);
if (brokerService.getPulsar().getConfiguration().isTransactionCoordinatorEnabled()
&& !checkTopicIsEventsNames(topicName)) {
&& !checkTopicIsEventsNames(topicName)
&& !checkTopicIsFunctionWorkerService(this)) {
this.transactionBuffer = brokerService.getPulsar()
.getTransactionBufferProvider().newTransactionBuffer(this, transactionCompletableFuture);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
package org.apache.pulsar.broker.transaction;

import static org.apache.pulsar.transaction.coordinator.impl.MLTransactionLogImpl.TRANSACTION_LOG_PREFIX;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.service.Subscription;
import org.apache.pulsar.broker.service.Topic;
import org.apache.pulsar.broker.service.persistent.PersistentSubscription;
import org.apache.pulsar.broker.transaction.pendingack.PendingAckHandle;
import org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore;
import org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleDisabled;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.ConsumerBuilder;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.SubscriptionType;
Expand All @@ -41,11 +48,9 @@
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.apache.pulsar.common.policies.data.TransactionBufferStats;
import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap;
import org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID;
import org.apache.pulsar.transaction.coordinator.TransactionMetadataStore;
import org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreState;
import org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore;
import org.apache.pulsar.functions.worker.WorkerConfig;
import org.awaitility.Awaitility;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
Expand Down Expand Up @@ -167,4 +172,29 @@ public void testGetTxnID() throws Exception {
Assert.assertEquals(txnID.getLeastSigBits(), 1);
Assert.assertEquals(txnID.getMostSigBits(), 0);
}

@Test
public void testFilterFunctionTopicForTransactionComponent()
throws IOException, PulsarAdminException, ExecutionException, InterruptedException, NoSuchFieldException,
IllegalAccessException {
WorkerConfig workerConfig = getPulsarServiceList().get(0).getWorkerConfig().get();
String [] functionTopics = {workerConfig.getFunctionMetadataTopic(),
workerConfig.getFunctionAssignmentTopic(),
workerConfig.getClusterCoordinationTopic()
};
for (int i = 0; i < 3; i++) {
admin.topics().createNonPartitionedTopic(functionTopics[i]);
TransactionBufferStats transactionBufferStats = admin.transactions().getTransactionBufferStats(functionTopics[i]);
Assert.assertNull(transactionBufferStats);
pulsarClient.newConsumer().topic(functionTopics[i]).subscriptionName("myTest-subscription").subscribe();
PulsarService pulsarService = getPulsarServiceList().get(0);
List<String> subscriptions = admin.topics().getSubscriptions(functionTopics[i]);
Topic topic = pulsarService.getBrokerService().getTopicIfExists(functionTopics[i]).get().get();
Subscription subscription = topic.getSubscription(subscriptions.get(0));
Field field = PersistentSubscription.class.getDeclaredField("pendingAckHandle");
field.setAccessible(true);
PendingAckHandle pendingAckHandle = (PendingAckHandle) field.get(subscription);
Assert.assertTrue(pendingAckHandle instanceof PendingAckHandleDisabled);
}
}
}