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

[Improve][broker] Support clear old bookie data for BKCluster #16744

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ private void startBookieWithRocksDB() throws Exception {
.bkPort(bkPort)
.numBookies(numOfBk)
.dataDir(bkDir)
.clearOldData(wipeData)
.build();
config.setBookkeeperNumberOfChannelsPerBookie(1);
config.setMetadataStoreUrl(metadataStoreUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.TenantInfo;
import org.apache.pulsar.metadata.bookkeeper.BKCluster;
import org.junit.platform.commons.util.StringUtils;


public class EmbeddedPulsarCluster implements AutoCloseable {
Expand All @@ -52,13 +53,16 @@ public class EmbeddedPulsarCluster implements AutoCloseable {
private final PulsarAdmin admin;

@Builder
private EmbeddedPulsarCluster(int numBrokers, int numBookies, String metadataStoreUrl) throws Exception {
private EmbeddedPulsarCluster(int numBrokers, int numBookies, String metadataStoreUrl,
String dataDir, boolean clearOldData) throws Exception {
this.numBrokers = numBrokers;
this.numBookies = numBookies;
this.metadataStoreUrl = metadataStoreUrl;
this.bkCluster = BKCluster.builder()
.metadataServiceUri(metadataStoreUrl)
.numBookies(numBookies)
.dataDir(StringUtils.isNotBlank(dataDir) ? dataDir : null)
.clearOldData(clearOldData)
.build();

for (int i = 0; i < numBrokers; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,51 @@
package org.apache.pulsar.broker;

import static org.testng.Assert.assertEquals;
import java.io.File;
import java.util.function.Supplier;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.util.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.metadata.BaseMetadataStoreTest;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Slf4j
public class EndToEndMetadataTest extends BaseMetadataStoreTest {

private File tempDir;

@BeforeClass(alwaysRun = true)
@Override
public void setup() throws Exception {
super.setup();
tempDir = IOUtils.createTempDir("bookies", "test");
}

@AfterClass(alwaysRun = true)
@Override
public void cleanup() throws Exception {
super.cleanup();
FileUtils.deleteDirectory(tempDir);
}

@Test(dataProvider = "impl")
public void testPublishConsume(String provider, Supplier<String> urlSupplier) throws Exception {

@Cleanup
EmbeddedPulsarCluster epc = EmbeddedPulsarCluster.builder()
.numBrokers(1)
.numBookies(1)
.metadataStoreUrl(urlSupplier.get())
.dataDir(tempDir.getAbsolutePath())
.clearOldData(true)
.build();

@Cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.metadata.bookkeeper;

import static org.apache.commons.io.FileUtils.cleanDirectory;
import java.io.File;
import java.io.IOException;
import java.net.NetworkInterface;
Expand Down Expand Up @@ -75,6 +76,8 @@ public static class BKClusterConf {
private String dataDir;
private int bkPort = 0;

private boolean clearOldData;

public BKClusterConf metadataServiceUri(String metadataServiceUri) {
this.metadataServiceUri = metadataServiceUri;
return this;
Expand All @@ -95,6 +98,11 @@ public BKClusterConf bkPort(int bkPort) {
return this;
}

public BKClusterConf clearOldData(Boolean clearOldData) {
coderzc marked this conversation as resolved.
Show resolved Hide resolved
this.clearOldData = clearOldData;
return this;
}

public BKCluster build() throws Exception {
return new BKCluster(this);
}
Expand Down Expand Up @@ -198,6 +206,10 @@ private ServerConfiguration newServerConfiguration() throws Exception {
dataDir = createTempDir("bookie", "test");
}

if (clusterConf.clearOldData) {
cleanDirectory(dataDir);
}

int port;
if (baseConf.isEnableLocalTransport() || !baseConf.getAllowEphemeralPorts() || clusterConf.bkPort == 0) {
port = PortManager.nextFreePort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public abstract class BaseMetadataStoreTest extends TestRetrySupport {

@BeforeClass(alwaysRun = true)
@Override
public final void setup() throws Exception {
public void setup() throws Exception {
incrementSetupNumber();
zks = new TestZKServer();
}

@AfterClass(alwaysRun = true)
@Override
public final void cleanup() throws Exception {
public void cleanup() throws Exception {
markCurrentSetupNumberCleaned();
if (zks != null) {
zks.close();
Expand Down