TopicPoliciesTest.java 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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.admin;

21 22
import static org.testng.Assert.assertEquals;

23 24
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
25
import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
26 27
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.broker.service.BacklogQuotaManager;
28 29
import org.apache.pulsar.broker.service.Topic;
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
30
import org.apache.pulsar.client.admin.PulsarAdminException;
31
import org.apache.pulsar.client.api.Producer;
32 33 34
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.BacklogQuota;
import org.apache.pulsar.common.policies.data.ClusterData;
35
import org.apache.pulsar.common.policies.data.DispatchRate;
36
import org.apache.pulsar.common.policies.data.PersistencePolicies;
37 38 39 40 41 42 43 44
import org.apache.pulsar.common.policies.data.RetentionPolicies;
import org.apache.pulsar.common.policies.data.TenantInfo;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Slf4j
45
public class TopicPoliciesTest extends MockedPulsarServiceBaseTest {
46 47 48 49 50 51 52

    private final String testTenant = "my-tenant";

    private final String testNamespace = "my-namespace";

    private final String myNamespace = testTenant + "/" + testNamespace;

53
    private final String testTopic = "persistent://" + myNamespace + "/test-set-backlog-quota";
54

55 56
    private final String persistenceTopic = "persistent://" + myNamespace + "/test-set-persistence";

57 58 59 60 61 62 63 64 65 66 67
    @BeforeMethod
    @Override
    protected void setup() throws Exception {
        this.conf.setSystemTopicEnabled(true);
        this.conf.setTopicLevelPoliciesEnabled(true);
        super.internalSetup();

        admin.clusters().createCluster("test", new ClusterData(pulsar.getWebServiceAddress()));
        TenantInfo tenantInfo = new TenantInfo(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test"));
        admin.tenants().createTenant(this.testTenant, tenantInfo);
        admin.namespaces().createNamespace(testTenant + "/" + testNamespace, Sets.newHashSet("test"));
68 69
        admin.topics().createPartitionedTopic(testTopic, 2);
        Producer producer = pulsarClient.newProducer().topic(testTopic).create();
70
        producer.close();
71
        Thread.sleep(3000);
72 73 74 75 76 77 78 79 80 81 82 83
    }

    @AfterMethod
    @Override
    public void cleanup() throws Exception {
        super.internalCleanup();
    }

    @Test
    public void testSetBacklogQuota() throws Exception {

        BacklogQuota backlogQuota = new BacklogQuota(1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
84
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
85

86 87
        admin.topics().setBacklogQuota(testTopic, backlogQuota);
        log.info("Backlog quota set success on topic: {}", testTopic);
88 89

        Thread.sleep(3000);
90
        BacklogQuota getBacklogQuota = admin.topics().getBacklogQuotaMap(testTopic)
91
                .get(BacklogQuota.BacklogQuotaType.destination_storage);
92
        log.info("Backlog quota {} get on topic: {}", getBacklogQuota, testTopic);
93 94 95
        Assert.assertEquals(getBacklogQuota, backlogQuota);

        BacklogQuotaManager backlogQuotaManager = pulsar.getBrokerService().getBacklogQuotaManager();
96 97
        BacklogQuota backlogQuotaInManager = backlogQuotaManager.getBacklogQuota(TopicName.get(testTopic));
        log.info("Backlog quota {} in backlog quota manager on topic: {}", backlogQuotaInManager, testTopic);
98 99
        Assert.assertEquals(backlogQuotaInManager, backlogQuota);

100
        admin.topics().deletePartitionedTopic(testTopic, true);
101 102 103 104 105
    }

    @Test
    public void testRemoveBacklogQuota() throws Exception {
        BacklogQuota backlogQuota = new BacklogQuota(1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
106 107 108
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
        admin.topics().setBacklogQuota(testTopic, backlogQuota);
        log.info("Backlog quota set success on topic: {}", testTopic);
109 110

        Thread.sleep(3000);
111
        BacklogQuota getBacklogQuota = admin.topics().getBacklogQuotaMap(testTopic)
112
                .get(BacklogQuota.BacklogQuotaType.destination_storage);
113
        log.info("Backlog quota {} get on topic: {}", getBacklogQuota, testTopic);
114 115 116
        Assert.assertEquals(backlogQuota, getBacklogQuota);

        BacklogQuotaManager backlogQuotaManager = pulsar.getBrokerService().getBacklogQuotaManager();
117 118
        BacklogQuota backlogQuotaInManager = backlogQuotaManager.getBacklogQuota(TopicName.get(testTopic));
        log.info("Backlog quota {} in backlog quota manager on topic: {}", backlogQuotaInManager, testTopic);
119 120
        Assert.assertEquals(backlogQuota, backlogQuotaInManager);

121 122
        admin.topics().removeBacklogQuota(testTopic);
        getBacklogQuota = admin.topics().getBacklogQuotaMap(testTopic)
123
                .get(BacklogQuota.BacklogQuotaType.destination_storage);
124
        log.info("Backlog quota {} get on topic: {} after remove", getBacklogQuota, testTopic);
125 126
        Assert.assertNull(getBacklogQuota);

127
        backlogQuotaInManager = backlogQuotaManager.getBacklogQuota(TopicName.get(testTopic));
128
        log.info("Backlog quota {} in backlog quota manager on topic: {} after remove", backlogQuotaInManager,
129
                testTopic);
130 131
        Assert.assertEquals(backlogQuotaManager.getDefaultQuota(), backlogQuotaInManager);

132
        admin.topics().deletePartitionedTopic(testTopic, true);
133 134 135
    }

    @Test
136
    public void testCheckBacklogQuota() throws Exception {
137
        RetentionPolicies retentionPolicies = new RetentionPolicies(10, 10);
138
        String namespace = TopicName.get(testTopic).getNamespace();
139 140 141 142
        admin.namespaces().setRetention(namespace, retentionPolicies);

        BacklogQuota backlogQuota =
                new BacklogQuota(10 * 1024 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
143
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
144
        try {
145
            admin.topics().setBacklogQuota(testTopic, backlogQuota);
146 147 148 149 150 151 152
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 412);
        }
        Thread.sleep(3000);
        backlogQuota =
                new BacklogQuota(10 * 1024 * 1024 + 1, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
153
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
154
        try {
155
            admin.topics().setBacklogQuota(testTopic, backlogQuota);
156 157 158 159 160 161 162
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 412);
        }
        Thread.sleep(3000);
        backlogQuota =
                new BacklogQuota(10 * 1024 * 1024 - 1, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
163 164
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
        admin.topics().setBacklogQuota(testTopic, backlogQuota);
165
        Thread.sleep(3000);
166
        BacklogQuota getBacklogQuota = admin.topics().getBacklogQuotaMap(testTopic)
167
                .get(BacklogQuota.BacklogQuotaType.destination_storage);
168
        log.info("Backlog quota {} get on topic: {} after remove", getBacklogQuota, testTopic);
169 170
        Assert.assertEquals(getBacklogQuota, backlogQuota);

171
        admin.topics().deletePartitionedTopic(testTopic, true);
172 173 174
    }

    @Test
175 176 177 178 179
    public void testCheckRetention() throws Exception {
        BacklogQuota backlogQuota =
                new BacklogQuota(10 * 1024 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction);
        admin.topics().setBacklogQuota(testTopic, backlogQuota);
        Thread.sleep(3000);
180

181 182
        RetentionPolicies retention = new RetentionPolicies(10, 10);
        log.info("Retention: {} will set to the topic: {}", retention, testTopic);
183
        try {
184
            admin.topics().setRetention(testTopic, retention);
185 186
            Assert.fail();
        } catch (PulsarAdminException e) {
187
            Assert.assertEquals(e.getStatusCode(), 412);
188 189
        }

190 191
        retention = new RetentionPolicies(10, 9);
        log.info("Retention: {} will set to the topic: {}", retention, testTopic);
192
        try {
193
            admin.topics().setRetention(testTopic, retention);
194 195
            Assert.fail();
        } catch (PulsarAdminException e) {
196
            Assert.assertEquals(e.getStatusCode(), 412);
197 198
        }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        Thread.sleep(3000);
        retention = new RetentionPolicies(10, 12);
        log.info("Backlog quota: {} will set to the topic: {}", backlogQuota, testTopic);
        admin.topics().setRetention(testTopic, retention);
        Thread.sleep(3000);
        RetentionPolicies getRetention = admin.topics().getRetention(testTopic);
        log.info("Backlog quota {} get on topic: {}", getRetention, testTopic);
        Assert.assertEquals(getRetention, retention);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testSetRetention() throws Exception {
        RetentionPolicies retention = new RetentionPolicies(60, 1024);
        log.info("Retention: {} will set to the topic: {}", retention, testTopic);

        admin.topics().setRetention(testTopic, retention);
        log.info("Retention set success on topic: {}", testTopic);

        Thread.sleep(3000);
        RetentionPolicies getRetention = admin.topics().getRetention(testTopic);
        log.info("Retention {} get on topic: {}", getRetention, testTopic);
        Assert.assertEquals(getRetention, retention);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testRemoveRetention() throws Exception {

        RetentionPolicies retention = new RetentionPolicies(60, 1024);
        log.info("Retention: {} will set to the topic: {}", retention, testTopic);

        admin.topics().setRetention(testTopic, retention);
        log.info("Retention set success on topic: {}", testTopic);

        Thread.sleep(3000);
        RetentionPolicies getRetention = admin.topics().getRetention(testTopic);
        log.info("Retention {} get on topic: {}", getRetention, testTopic);
        Assert.assertEquals(getRetention, retention);

        admin.topics().removeRetention(testTopic);
        Thread.sleep(3000);
        log.info("Retention {} get on topic: {} after remove", getRetention, testTopic);
        getRetention = admin.topics().getRetention(testTopic);
        Assert.assertNull(getRetention);

        admin.topics().deletePartitionedTopic(testTopic, true);
248
    }
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

    @Test
    public void testCheckPersistence() throws Exception {
        PersistencePolicies persistencePolicies = new PersistencePolicies(6, 2, 2, 0.0);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, testTopic);
        try {
            admin.topics().setPersistence(testTopic, persistencePolicies);
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 400);
        }

        persistencePolicies = new PersistencePolicies(2, 6, 2, 0.0);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, testTopic);
        try {
            admin.topics().setPersistence(testTopic, persistencePolicies);
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 400);
        }

        persistencePolicies = new PersistencePolicies(2, 2, 6, 0.0);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, testTopic);
        try {
            admin.topics().setPersistence(testTopic, persistencePolicies);
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 400);
        }

        persistencePolicies = new PersistencePolicies(1, 2, 2, 0.0);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, testTopic);
        try {
            admin.topics().setPersistence(testTopic, persistencePolicies);
            Assert.fail();
        } catch (PulsarAdminException e) {
            Assert.assertEquals(e.getStatusCode(), 400);
        }

        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testSetPersistence() throws Exception {
        PersistencePolicies persistencePolicies = new PersistencePolicies(3, 3, 3, 0.1);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, persistenceTopic);

        admin.topics().setPersistence(persistenceTopic, persistencePolicies);
        Thread.sleep(3000);

        admin.topics().createPartitionedTopic(persistenceTopic, 2);
        Producer producer = pulsarClient.newProducer().topic(persistenceTopic).create();
        producer.close();

        admin.lookups().lookupTopic(persistenceTopic);
        Topic t = pulsar.getBrokerService().getOrCreateTopic(persistenceTopic).get();
        PersistentTopic persistentTopic = (PersistentTopic) t;
        ManagedLedgerConfig managedLedgerConfig = persistentTopic.getManagedLedger().getConfig();
        assertEquals(managedLedgerConfig.getEnsembleSize(), 3);
        assertEquals(managedLedgerConfig.getWriteQuorumSize(), 3);
        assertEquals(managedLedgerConfig.getAckQuorumSize(), 3);
        assertEquals(managedLedgerConfig.getThrottleMarkDelete(), 0.1);

        PersistencePolicies getPersistencePolicies = admin.topics().getPersistence(persistenceTopic);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, persistenceTopic);
        Assert.assertEquals(getPersistencePolicies, persistencePolicies);

        admin.topics().deletePartitionedTopic(persistenceTopic, true);
        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testRemovePersistence() throws Exception {

        PersistencePolicies persistencePolicies = new PersistencePolicies(2, 2, 2, 0.0);
        log.info("PersistencePolicies: {} will set to the topic: {}", persistencePolicies, testTopic);

        admin.topics().setPersistence(testTopic, persistencePolicies);
        Thread.sleep(3000);
        PersistencePolicies getPersistencePolicies = admin.topics().getPersistence(testTopic);

        log.info("PersistencePolicies {} get on topic: {}", getPersistencePolicies, testTopic);
        Assert.assertEquals(getPersistencePolicies, persistencePolicies);

        admin.topics().removePersistence(testTopic);
        Thread.sleep(3000);
        log.info("PersistencePolicies {} get on topic: {} after remove", getPersistencePolicies, testTopic);
        getPersistencePolicies = admin.topics().getPersistence(testTopic);
        Assert.assertNull(getPersistencePolicies);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379


    @Test
    public void testGetSetDispatchRate() throws Exception {
        DispatchRate dispatchRate = new DispatchRate(100, 10000, 1, true);
        log.info("Dispatch Rate: {} will set to the topic: {}", dispatchRate, testTopic);

        admin.topics().setDispatchRate(testTopic, dispatchRate);
        log.info("Dispatch Rate set success on topic: {}", testTopic);

        Thread.sleep(3000);
        DispatchRate getDispatchRate = admin.topics().getDispatchRate(testTopic);
        log.info("Dispatch Rate: {} get on topic: {}", getDispatchRate, testTopic);
        Assert.assertEquals(getDispatchRate, dispatchRate);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testRemoveDispatchRate() throws Exception {
        DispatchRate dispatchRate = new DispatchRate(100, 10000, 1, true);
        log.info("Dispatch Rate: {} will set to the topic: {}", dispatchRate, testTopic);

        admin.topics().setDispatchRate(testTopic, dispatchRate);
        log.info("Dispatch Rate set success on topic: {}", testTopic);

        Thread.sleep(3000);
        DispatchRate getDispatchRate = admin.topics().getDispatchRate(testTopic);
        log.info("Dispatch Rate: {} get on topic: {}", getDispatchRate, testTopic);
        Assert.assertEquals(getDispatchRate, dispatchRate);

        admin.topics().removeDispatchRate(testTopic);
        Thread.sleep(3000);
        log.info("Dispatch Rate get on topic: {} after remove", getDispatchRate, testTopic);
        getDispatchRate = admin.topics().getDispatchRate(testTopic);
        Assert.assertNull(getDispatchRate);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

    @Test
    public void testGetSetCompactionThreshold() throws Exception {
        long compactionThreshold = 100000;
        log.info("Compaction threshold: {} will set to the topic: {}", compactionThreshold, testTopic);

        admin.topics().setCompactionThreshold(testTopic, compactionThreshold);
        log.info("Compaction threshold set success on topic: {}", testTopic);

        Thread.sleep(3000);
        long getCompactionThreshold = admin.topics().getCompactionThreshold(testTopic);
        log.info("Compaction threshold: {} get on topic: {}", getCompactionThreshold, testTopic);
        Assert.assertEquals(getCompactionThreshold, compactionThreshold);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }

    @Test
    public void testRemoveCompactionThreshold() throws Exception {
        Long compactionThreshold = 100000L;
        log.info("Compaction threshold: {} will set to the topic: {}", compactionThreshold, testTopic);

        admin.topics().setCompactionThreshold(testTopic, compactionThreshold);
        log.info("Compaction threshold set success on topic: {}", testTopic);

        Thread.sleep(3000);
        Long getCompactionThreshold = admin.topics().getCompactionThreshold(testTopic);
        log.info("Compaction threshold: {} get on topic: {}", getCompactionThreshold, testTopic);
        Assert.assertEquals(getCompactionThreshold, compactionThreshold);

        admin.topics().removeCompactionThreshold(testTopic);
        Thread.sleep(3000);
        log.info("Compaction threshold get on topic: {} after remove", getCompactionThreshold, testTopic);
        getCompactionThreshold = admin.topics().getCompactionThreshold(testTopic);
        Assert.assertNull(getCompactionThreshold);

        admin.topics().deletePartitionedTopic(testTopic, true);
    }
418
}