|
| 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.tests.integration.upgrade; |
| 20 | + |
| 21 | +import com.github.dockerjava.api.model.Bind; |
| 22 | +import lombok.Cleanup; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.pulsar.client.api.Consumer; |
| 25 | +import org.apache.pulsar.client.api.Message; |
| 26 | +import org.apache.pulsar.client.api.MessageId; |
| 27 | +import org.apache.pulsar.client.api.Producer; |
| 28 | +import org.apache.pulsar.client.api.PulsarClient; |
| 29 | +import org.apache.pulsar.client.api.Schema; |
| 30 | +import org.apache.pulsar.client.api.SubscriptionInitialPosition; |
| 31 | +import org.apache.pulsar.tests.integration.containers.PulsarContainer; |
| 32 | +import org.apache.pulsar.tests.integration.topologies.PulsarCluster; |
| 33 | +import org.apache.pulsar.tests.integration.topologies.PulsarClusterSpec; |
| 34 | +import org.apache.pulsar.tests.integration.topologies.PulsarClusterTestBase; |
| 35 | +import org.testcontainers.containers.GenericContainer; |
| 36 | +import org.testcontainers.containers.Network; |
| 37 | +import org.testng.annotations.Test; |
| 38 | +import java.util.stream.Stream; |
| 39 | +import static java.util.stream.Collectors.joining; |
| 40 | +import static org.testng.Assert.assertEquals; |
| 41 | + |
| 42 | +/** |
| 43 | + * Test upgrading/downgrading Pulsar cluster from major releases. |
| 44 | + */ |
| 45 | +@Slf4j |
| 46 | +public class PulsarUpgradeDowngradeTest extends PulsarClusterTestBase { |
| 47 | + |
| 48 | + @Test(timeOut=600_000) |
| 49 | + public void upgradeFrom_2_10_6() throws Exception { |
| 50 | + testUpgradeDowngrade("apachepulsar/pulsar:2.10.6", PulsarContainer.DEFAULT_IMAGE_NAME); |
| 51 | + } |
| 52 | + |
| 53 | + @Test(timeOut=600_000) |
| 54 | + public void upgradeFrom_3_0_5() throws Exception { |
| 55 | + testUpgradeDowngrade("apachepulsar/pulsar:3.0.5", PulsarContainer.DEFAULT_IMAGE_NAME); |
| 56 | + } |
| 57 | + |
| 58 | + private void testUpgradeDowngrade(String imageOld, String imageNew) throws Exception { |
| 59 | + final String clusterName = Stream.of(this.getClass().getSimpleName(), randomName(5)) |
| 60 | + .filter(s -> !s.isEmpty()) |
| 61 | + .collect(joining("-")); |
| 62 | + String topicName = generateTopicName("testupdown", true); |
| 63 | + |
| 64 | + @Cleanup |
| 65 | + Network network = Network.newNetwork(); |
| 66 | + @Cleanup |
| 67 | + GenericContainer<?> alpine = new GenericContainer<>("alpine:3.20.1") |
| 68 | + .withExposedPorts(80) |
| 69 | + .withNetwork(network) |
| 70 | + .withNetworkAliases("shared-storage") |
| 71 | + .withEnv("MAGIC_NUMBER", "42") |
| 72 | + .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd |
| 73 | + .getHostConfig() |
| 74 | + .withBinds(Bind.parse("/pulsar/data:/pulsar/data"))) |
| 75 | + .withCommand("/bin/sh", "-c", |
| 76 | + "mkdir -p /pulsar/data && " |
| 77 | + + "chmod -R ug+rwx /pulsar/data && " |
| 78 | + + "chown -R 10000:0 /pulsar/data && " |
| 79 | + + "rm -rf /pulsar/data/* && " |
| 80 | + + "while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"); |
| 81 | + alpine.start(); |
| 82 | + |
| 83 | + PulsarClusterSpec specOld = PulsarClusterSpec.builder() |
| 84 | + .numBookies(2) |
| 85 | + .numBrokers(1) |
| 86 | + .clusterName(clusterName) |
| 87 | + .dataContainer(alpine) |
| 88 | + .pulsarTestImage(imageOld) |
| 89 | + .build(); |
| 90 | + |
| 91 | + PulsarClusterSpec specNew = PulsarClusterSpec.builder() |
| 92 | + .numBookies(2) |
| 93 | + .numBrokers(1) |
| 94 | + .clusterName(clusterName) |
| 95 | + .dataContainer(alpine) |
| 96 | + .pulsarTestImage(imageNew) |
| 97 | + .build(); |
| 98 | + |
| 99 | + log.info("Setting up OLD cluster {} with {} bookies, {} brokers using {}", |
| 100 | + specOld.clusterName(), specOld.numBookies(), specOld.numBrokers(), imageOld); |
| 101 | + |
| 102 | + pulsarCluster = PulsarCluster.forSpec(specNew, network); |
| 103 | + pulsarCluster.closeNetworkOnExit = false; |
| 104 | + pulsarCluster.start(true); |
| 105 | + |
| 106 | + try { |
| 107 | + log.info("setting retention"); |
| 108 | + pulsarCluster.runAdminCommandOnAnyBroker("namespaces", |
| 109 | + "set-retention", "--size", "100M", "--time", "100m", "public/default"); |
| 110 | + |
| 111 | + publishAndConsume(topicName, pulsarCluster.getPlainTextServiceUrl(), 10, 10); |
| 112 | + } finally { |
| 113 | + pulsarCluster.stop(); |
| 114 | + } |
| 115 | + |
| 116 | + log.info("Upgrading to NEW cluster {} with {} bookies, {} brokers using {}", |
| 117 | + specNew.clusterName(), specNew.numBookies(), specNew.numBrokers(), imageNew); |
| 118 | + |
| 119 | + pulsarCluster = PulsarCluster.forSpec(specNew, network); |
| 120 | + pulsarCluster.closeNetworkOnExit = false; |
| 121 | + pulsarCluster.start(false); |
| 122 | + |
| 123 | + try { |
| 124 | + publishAndConsume(topicName, pulsarCluster.getPlainTextServiceUrl(), 10, 20); |
| 125 | + } finally { |
| 126 | + pulsarCluster.stop(); |
| 127 | + } |
| 128 | + |
| 129 | + log.info("Downgrading to OLD cluster {} with {} bookies, {} brokers using {}", |
| 130 | + specOld.clusterName(), specOld.numBookies(), specOld.numBrokers(), imageOld); |
| 131 | + |
| 132 | + pulsarCluster = PulsarCluster.forSpec(specOld, network); |
| 133 | + pulsarCluster.closeNetworkOnExit = false; |
| 134 | + pulsarCluster.start(false); |
| 135 | + |
| 136 | + try { |
| 137 | + publishAndConsume(topicName, pulsarCluster.getPlainTextServiceUrl(), 10, 30); |
| 138 | + } finally { |
| 139 | + pulsarCluster.stop(); |
| 140 | + alpine.stop(); |
| 141 | + network.close(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void publishAndConsume(String topicName, String serviceUrl, int numProduce, int numConsume) throws Exception { |
| 146 | + log.info("publishAndConsume: topic name: {}", topicName); |
| 147 | + |
| 148 | + @Cleanup |
| 149 | + PulsarClient client = PulsarClient.builder() |
| 150 | + .serviceUrl(serviceUrl) |
| 151 | + .build(); |
| 152 | + |
| 153 | + @Cleanup |
| 154 | + Producer<String> producer = client.newProducer(Schema.STRING) |
| 155 | + .topic(topicName) |
| 156 | + .create(); |
| 157 | + |
| 158 | + log.info("Publishing {} messages", numProduce); |
| 159 | + for (int i = numConsume - numProduce; i < numConsume; i++) { |
| 160 | + log.info("Publishing message: {}", "smoke-message-" + i); |
| 161 | + producer.send("smoke-message-" + i); |
| 162 | + } |
| 163 | + |
| 164 | + @Cleanup |
| 165 | + Consumer<String> consumer = client.newConsumer(Schema.STRING) |
| 166 | + .topic(topicName) |
| 167 | + .subscriptionName("my-sub") |
| 168 | + .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) |
| 169 | + .subscribe(); |
| 170 | + consumer.seek(MessageId.earliest); |
| 171 | + |
| 172 | + log.info("Consuming {} messages", numConsume); |
| 173 | + for (int i = 0; i < numConsume; i++) { |
| 174 | + log.info("Waiting for message: {}", i); |
| 175 | + Message<String> m = consumer.receive(); |
| 176 | + log.info("Received message: {}", m.getValue()); |
| 177 | + assertEquals("smoke-message-" + i, m.getValue()); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments