Search in sources :

Example 1 with BacklogQuotaImpl

use of org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl in project pulsar by apache.

the class PersistentTopicsBase method internalSetBacklogQuota.

protected CompletableFuture<Void> internalSetBacklogQuota(BacklogQuota.BacklogQuotaType backlogQuotaType, BacklogQuotaImpl backlogQuota, boolean isGlobal) {
    BacklogQuota.BacklogQuotaType finalBacklogQuotaType = backlogQuotaType == null ? BacklogQuota.BacklogQuotaType.destination_storage : backlogQuotaType;
    return validateTopicPolicyOperationAsync(topicName, PolicyName.BACKLOG, PolicyOperation.WRITE).thenAccept(__ -> validatePoliciesReadOnlyAccess()).thenCompose(__ -> getTopicPoliciesAsyncWithRetry(topicName, isGlobal)).thenCompose(op -> {
        TopicPolicies topicPolicies = op.orElseGet(TopicPolicies::new);
        return getRetentionPoliciesAsync(topicName, topicPolicies).thenCompose(retentionPolicies -> {
            if (!checkBacklogQuota(backlogQuota, retentionPolicies)) {
                log.warn("[{}] Failed to update backlog configuration for topic {}: conflicts with" + " retention quota", clientAppId(), topicName);
                return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED, "Backlog Quota exceeds configured retention quota for topic. " + "Please increase retention quota and retry"));
            }
            if (backlogQuota != null) {
                topicPolicies.getBackLogQuotaMap().put(finalBacklogQuotaType.name(), backlogQuota);
            } else {
                topicPolicies.getBackLogQuotaMap().remove(finalBacklogQuotaType.name());
            }
            Map<String, BacklogQuotaImpl> backLogQuotaMap = topicPolicies.getBackLogQuotaMap();
            topicPolicies.setIsGlobal(isGlobal);
            return pulsar().getTopicPoliciesService().updateTopicPoliciesAsync(topicName, topicPolicies).thenRun(() -> {
                try {
                    log.info("[{}] Successfully updated backlog quota map: namespace={}, " + "topic={}, map={}", clientAppId(), namespaceName, topicName.getLocalName(), jsonMapper().writeValueAsString(backLogQuotaMap));
                } catch (JsonProcessingException ignore) {
                }
            });
        });
    });
}
Also used : Version(com.github.zafarkhaja.semver.Version) SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) ManagedLedgerOfflineBacklog(org.apache.bookkeeper.mledger.impl.ManagedLedgerOfflineBacklog) Topic(org.apache.pulsar.broker.service.Topic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) LedgerOffloader(org.apache.bookkeeper.mledger.LedgerOffloader) DelayedDeliveryPolicies(org.apache.pulsar.common.policies.data.DelayedDeliveryPolicies) StringUtils(org.apache.commons.lang3.StringUtils) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) AlreadyRunningException(org.apache.pulsar.broker.service.BrokerServiceException.AlreadyRunningException) TopicOperation(org.apache.pulsar.common.policies.data.TopicOperation) PolicyOperation(org.apache.pulsar.common.policies.data.PolicyOperation) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamespaceOperation(org.apache.pulsar.common.policies.data.NamespaceOperation) DispatchRateImpl(org.apache.pulsar.common.policies.data.impl.DispatchRateImpl) PersistentOfflineTopicStats(org.apache.pulsar.common.policies.data.PersistentOfflineTopicStats) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) AsyncResponse(javax.ws.rs.container.AsyncResponse) EventsTopicNames.checkTopicIsTransactionCoordinatorAssign(org.apache.pulsar.common.events.EventsTopicNames.checkTopicIsTransactionCoordinatorAssign) Set(java.util.Set) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) OffloadProcessStatus(org.apache.pulsar.client.admin.OffloadProcessStatus) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) WebApplicationException(javax.ws.rs.WebApplicationException) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Subscription(org.apache.pulsar.broker.service.Subscription) TopicStats(org.apache.pulsar.common.policies.data.TopicStats) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) ArrayList(java.util.ArrayList) Commands(org.apache.pulsar.common.protocol.Commands) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) Lists(com.google.common.collect.Lists) DateFormatter(org.apache.pulsar.common.util.DateFormatter) BiConsumer(java.util.function.BiConsumer) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo) IOException(java.io.IOException) PulsarService(org.apache.pulsar.broker.PulsarService) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) ExecutionException(java.util.concurrent.ExecutionException) PulsarService.isTransactionInternalName(org.apache.pulsar.broker.PulsarService.isTransactionInternalName) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) DEFAULT_OPERATION_TIMEOUT_SEC(org.apache.pulsar.broker.resources.PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC) AdminResource(org.apache.pulsar.broker.admin.AdminResource) LoggerFactory(org.slf4j.LoggerFactory) KeyValue(org.apache.pulsar.common.api.proto.KeyValue) TopicStatsImpl(org.apache.pulsar.common.policies.data.stats.TopicStatsImpl) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) AuthPolicies(org.apache.pulsar.common.policies.data.AuthPolicies) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) BrokerEntryMetadata(org.apache.pulsar.common.api.proto.BrokerEntryMetadata) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) PartitionedTopicInternalStats(org.apache.pulsar.common.policies.data.PartitionedTopicInternalStats) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) OffloadPoliciesImpl(org.apache.pulsar.common.policies.data.OffloadPoliciesImpl) BitSetRecyclable(org.apache.pulsar.common.util.collections.BitSetRecyclable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) PulsarByteBufAllocator(org.apache.pulsar.common.allocator.PulsarByteBufAllocator) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) Base64(java.util.Base64) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) EncryptionKeys(org.apache.pulsar.common.api.proto.EncryptionKeys) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) PolicyName(org.apache.pulsar.common.policies.data.PolicyName) PartitionedTopicStatsImpl(org.apache.pulsar.common.policies.data.stats.PartitionedTopicStatsImpl) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) TopicName(org.apache.pulsar.common.naming.TopicName) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerInfoCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ManagedLedgerInfoCallback) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) LongRunningProcessStatus(org.apache.pulsar.client.admin.LongRunningProcessStatus) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) TopicDomain(org.apache.pulsar.common.naming.TopicDomain) Status(javax.ws.rs.core.Response.Status) PartitionedManagedLedgerInfo(org.apache.pulsar.common.naming.PartitionedManagedLedgerInfo) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) Logger(org.slf4j.Logger) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) MessageId(org.apache.pulsar.client.api.MessageId) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) RestException(org.apache.pulsar.broker.web.RestException) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with BacklogQuotaImpl

use of org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl in project pulsar by apache.

the class ReplicatorTopicPoliciesTest method testReplicateQuotaTopicPolicies.

@Test
public void testReplicateQuotaTopicPolicies() throws Exception {
    final String namespace = "pulsar/partitionedNs-" + UUID.randomUUID();
    final String topic = "persistent://" + namespace + "/topic" + UUID.randomUUID();
    init(namespace, topic);
    // set BacklogQuota
    BacklogQuotaImpl backlogQuota = new BacklogQuotaImpl();
    backlogQuota.setLimitSize(1);
    backlogQuota.setLimitTime(2);
    // local
    admin1.topicPolicies().setBacklogQuota(topic, backlogQuota);
    Awaitility.await().untilAsserted(() -> assertEquals(admin2.topicPolicies().getBacklogQuotaMap(topic).size(), 0));
    Awaitility.await().untilAsserted(() -> assertEquals(admin3.topicPolicies().getBacklogQuotaMap(topic).size(), 0));
    // global
    admin1.topicPolicies(true).setBacklogQuota(topic, backlogQuota);
    Awaitility.await().untilAsserted(() -> assertTrue(admin2.topicPolicies(true).getBacklogQuotaMap(topic).containsValue(backlogQuota)));
    Awaitility.await().untilAsserted(() -> assertTrue(admin3.topicPolicies(true).getBacklogQuotaMap(topic).containsValue(backlogQuota)));
    // remove BacklogQuota
    admin1.topicPolicies(true).removeBacklogQuota(topic);
    Awaitility.await().untilAsserted(() -> assertEquals(admin2.topicPolicies(true).getBacklogQuotaMap(topic).size(), 0));
    Awaitility.await().untilAsserted(() -> assertEquals(admin3.topicPolicies(true).getBacklogQuotaMap(topic).size(), 0));
}
Also used : BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) Test(org.testng.annotations.Test)

Example 3 with BacklogQuotaImpl

use of org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl in project pulsar by apache.

the class BacklogQuotaCompatibilityTest method testV27ClientSetV28BrokerRead.

@Test
public void testV27ClientSetV28BrokerRead() throws Exception {
    Policies writePolicy = new Policies();
    BacklogQuotaImpl writeBacklogQuota = new BacklogQuotaImpl();
    writeBacklogQuota.setLimit(1024);
    writeBacklogQuota.setLimitTime(60);
    writeBacklogQuota.setPolicy(testPolicy);
    HashMap<BacklogQuota.BacklogQuotaType, BacklogQuota> quotaHashMap = new HashMap<>();
    quotaHashMap.put(BacklogQuota.BacklogQuotaType.destination_storage, writeBacklogQuota);
    writePolicy.backlog_quota_map = quotaHashMap;
    byte[] serialize = simpleType.serialize("/path", writePolicy);
    Policies policies = simpleType.deserialize("/path", serialize, null);
    BacklogQuota readBacklogQuota = policies.backlog_quota_map.get(BacklogQuota.BacklogQuotaType.destination_storage);
    Assert.assertEquals(readBacklogQuota.getLimitSize(), 1024);
    Assert.assertEquals(readBacklogQuota.getLimitTime(), 60);
    Assert.assertEquals(readBacklogQuota.getPolicy(), testPolicy);
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) HashMap(java.util.HashMap) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) Test(org.testng.annotations.Test)

Example 4 with BacklogQuotaImpl

use of org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl in project pulsar by apache.

the class BacklogQuotaCompatibilityTest method testV28ClientSetV28BrokerRead.

@Test
public void testV28ClientSetV28BrokerRead() throws Exception {
    Policies writePolicy = new Policies();
    BacklogQuotaImpl writeBacklogQuota = new BacklogQuotaImpl();
    writeBacklogQuota.setLimitSize(1024);
    writeBacklogQuota.setLimitTime(60);
    writeBacklogQuota.setPolicy(testPolicy);
    HashMap<BacklogQuota.BacklogQuotaType, BacklogQuota> quotaHashMap = new HashMap<>();
    quotaHashMap.put(BacklogQuota.BacklogQuotaType.destination_storage, writeBacklogQuota);
    writePolicy.backlog_quota_map = quotaHashMap;
    byte[] serialize = simpleType.serialize("/path", writePolicy);
    Policies policies = simpleType.deserialize("/path", serialize, null);
    BacklogQuota readBacklogQuota = policies.backlog_quota_map.get(BacklogQuota.BacklogQuotaType.destination_storage);
    Assert.assertEquals(readBacklogQuota.getLimit(), 1024);
    Assert.assertEquals(readBacklogQuota.getLimitTime(), 60);
    Assert.assertEquals(readBacklogQuota.getPolicy(), testPolicy);
}
Also used : Policies(org.apache.pulsar.common.policies.data.Policies) BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) HashMap(java.util.HashMap) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) Test(org.testng.annotations.Test)

Example 5 with BacklogQuotaImpl

use of org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl in project pulsar by apache.

the class BacklogQuotaCompatibilityTest method testV28ClientSetV27BrokerRead.

@Test
public void testV28ClientSetV27BrokerRead() {
    BacklogQuotaImpl writeBacklogQuota = new BacklogQuotaImpl();
    writeBacklogQuota.setLimitSize(1024);
    Assert.assertEquals(1024, writeBacklogQuota.getLimit());
}
Also used : BacklogQuotaImpl(org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl) Test(org.testng.annotations.Test)

Aggregations

BacklogQuotaImpl (org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl)5 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)3 Policies (org.apache.pulsar.common.policies.data.Policies)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Version (com.github.zafarkhaja.semver.Version)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1