|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.service; |
| 20 | + |
| 21 | +import com.github.benmanes.caffeine.cache.AsyncCacheLoader; |
| 22 | +import com.github.benmanes.caffeine.cache.AsyncLoadingCache; |
| 23 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 24 | +import com.google.common.annotations.VisibleForTesting; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.concurrent.Executor; |
| 29 | +import java.util.function.Consumer; |
| 30 | +import lombok.CustomLog; |
| 31 | +import org.apache.pulsar.broker.PulsarService; |
| 32 | +import org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory; |
| 33 | +import org.apache.pulsar.common.events.EventType; |
| 34 | +import org.apache.pulsar.common.naming.NamespaceName; |
| 35 | +import org.apache.pulsar.common.naming.TopicName; |
| 36 | +import org.apache.pulsar.common.policies.data.TopicPolicies; |
| 37 | +import org.jspecify.annotations.NonNull; |
| 38 | + |
| 39 | +/** |
| 40 | + * Routes topic policy operations to the legacy system-topic backend when a namespace already has |
| 41 | + * a topic-policy {@code __change_events} system topic, and otherwise to the configured backend. |
| 42 | + */ |
| 43 | +@CustomLog |
| 44 | +public class LegacyAwareTopicPoliciesService implements TopicPoliciesService { |
| 45 | + |
| 46 | + private final AsyncLoadingCache<NamespaceName, Boolean> isLegacyNamespace; |
| 47 | + @VisibleForTesting |
| 48 | + final SystemTopicBasedTopicPoliciesService systemTopicService; |
| 49 | + private final TopicPoliciesService configuredService; |
| 50 | + |
| 51 | + public LegacyAwareTopicPoliciesService(PulsarService pulsar, |
| 52 | + SystemTopicBasedTopicPoliciesService systemTopicService, |
| 53 | + TopicPoliciesService configuredService) { |
| 54 | + // Generally, we only need to check if the __change_events topic exists once because the __change_events topic |
| 55 | + // should only be created by broker before the upgrade, where `SystemTopicBasedTopicPoliciesService` is |
| 56 | + // configured as the topic policies service. |
| 57 | + this.isLegacyNamespace = Caffeine.newBuilder().expireAfterWrite(Duration.ofHours(1)) |
| 58 | + .buildAsync(new AsyncCacheLoader<>() { |
| 59 | + @NonNull |
| 60 | + @Override |
| 61 | + public CompletableFuture<? extends Boolean> asyncLoad(NamespaceName key, |
| 62 | + @NonNull Executor executor) { |
| 63 | + return NamespaceEventsSystemTopicFactory.checkSystemTopicExists(key, EventType.TOPIC_POLICY, |
| 64 | + pulsar); |
| 65 | + } |
| 66 | + }); |
| 67 | + this.systemTopicService = systemTopicService; |
| 68 | + this.configuredService = configuredService; |
| 69 | + if (configuredService instanceof SystemTopicBasedTopicPoliciesService) { |
| 70 | + throw new IllegalArgumentException( |
| 71 | + "configuredService should not be an instance of SystemTopicBasedTopicPoliciesService"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void start(PulsarService pulsarService) { |
| 77 | + // We should not call `systemTopicService.start()`, which just registers a namespace bundle listener to create |
| 78 | + // a reader on `<namespace>/__change_events` when the namespace's bundle is loaded firstly. It's just an |
| 79 | + // optimization to create the reader before loading any topic. However, it could create a reader on a namespace |
| 80 | + // that does not even have the __change_events topic. |
| 81 | + configuredService.start(pulsarService); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void close() throws Exception { |
| 86 | + try { |
| 87 | + configuredService.close(); |
| 88 | + } finally { |
| 89 | + systemTopicService.close(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public CompletableFuture<Optional<TopicPolicies>> getTopicPoliciesAsync(TopicName topicName, GetType type) { |
| 95 | + return resolveService(topicName.getNamespaceObject()) |
| 96 | + .thenCompose(service -> service.getTopicPoliciesAsync(topicName, type)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public CompletableFuture<Void> updateTopicPoliciesAsync(TopicName topicName, boolean isGlobalPolicy, |
| 101 | + boolean skipUpdateWhenTopicPolicyDoesntExist, |
| 102 | + Consumer<TopicPolicies> policyUpdater) { |
| 103 | + return resolveService(topicName.getNamespaceObject()) |
| 104 | + .thenCompose(service -> service.updateTopicPoliciesAsync(topicName, isGlobalPolicy, |
| 105 | + skipUpdateWhenTopicPolicyDoesntExist, policyUpdater)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public CompletableFuture<Void> deleteTopicPoliciesAsync(TopicName topicName) { |
| 110 | + return resolveService(topicName.getNamespaceObject()) |
| 111 | + .thenCompose(service -> service.deleteTopicPoliciesAsync(topicName)); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public CompletableFuture<Void> deleteTopicPoliciesAsync(TopicName topicName, |
| 116 | + boolean keepGlobalPoliciesAfterDeleting) { |
| 117 | + return resolveService(topicName.getNamespaceObject()) |
| 118 | + .thenCompose(service -> service.deleteTopicPoliciesAsync(topicName, |
| 119 | + keepGlobalPoliciesAfterDeleting)); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public CompletableFuture<Boolean> registerListenerAsync(TopicName topicName, TopicPolicyListener listener) { |
| 124 | + return resolveService(topicName.getNamespaceObject()) |
| 125 | + .thenCompose(service -> service.registerListenerAsync(topicName, listener)); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean registerListener(TopicName topicName, TopicPolicyListener listener) { |
| 130 | + throw new RuntimeException("should not be called"); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void unregisterListener(TopicName topicName, TopicPolicyListener listener) { |
| 135 | + configuredService.unregisterListener(topicName, listener); |
| 136 | + systemTopicService.unregisterListener(topicName, listener); |
| 137 | + } |
| 138 | + |
| 139 | + @VisibleForTesting |
| 140 | + CompletableFuture<TopicPoliciesService> resolveService(NamespaceName namespace) { |
| 141 | + return isLegacyNamespace.get(namespace) |
| 142 | + .thenApply(isLegacy -> isLegacy ? systemTopicService : configuredService); |
| 143 | + } |
| 144 | +} |
0 commit comments