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]Enable custom metadata stores #19208

Merged
merged 7 commits into from
Jan 19, 2023
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
@@ -0,0 +1,37 @@
/*
* 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.metadata.api;

public interface MetadataStoreProvider {


/**
* Scheme of the urls that MetadataSores provided by this object can handle.
*/
String urlScheme();

/**
* Creates a new MetadataStore.
* @throws MetadataStoreException if any exception happens while creating the metadata store.
*/
MetadataStore create(
String metadataURL,
MetadataStoreConfig metadataStoreConfig,
boolean enableSessionWatcher) throws MetadataStoreException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
*/
package org.apache.pulsar.metadata.impl;

import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.api.MetadataStoreProvider;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;

@Slf4j
public class MetadataStoreFactoryImpl {

public static final String METADATASTORE_PROVIDERS_PROPERTY = "pulsar.metadatastore.providers";

private static Map<String, MetadataStoreProvider> providers;

static {
loadProviders();
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
}

public static MetadataStore create(String metadataURL, MetadataStoreConfig metadataStoreConfig) throws
MetadataStoreException {
return newInstance(metadataURL, metadataStoreConfig, false);
Expand All @@ -45,7 +58,7 @@ public static MetadataStoreExtended createExtended(String metadataURL, MetadataS
private static MetadataStore newInstance(String metadataURL, MetadataStoreConfig metadataStoreConfig,
boolean enableSessionWatcher)
throws MetadataStoreException {

MetadataStoreProvider provider;
if (metadataURL.startsWith(LocalMemoryMetadataStore.MEMORY_SCHEME_IDENTIFIER)) {
return new LocalMemoryMetadataStore(metadataURL, metadataStoreConfig);
} else if (metadataURL.startsWith(RocksdbMetadataStore.ROCKSDB_SCHEME_IDENTIFIER)) {
Expand All @@ -55,11 +68,42 @@ private static MetadataStore newInstance(String metadataURL, MetadataStoreConfig
} else if (metadataURL.startsWith(ZKMetadataStore.ZK_SCHEME_IDENTIFIER)) {
return new ZKMetadataStore(metadataURL.substring(ZKMetadataStore.ZK_SCHEME_IDENTIFIER.length()),
metadataStoreConfig, enableSessionWatcher);
} else if ((provider = findProvider(metadataURL)) != null){
return provider.create(metadataURL, metadataStoreConfig, enableSessionWatcher);
} else {
return new ZKMetadataStore(metadataURL, metadataStoreConfig, enableSessionWatcher);
}
}

static void loadProviders() {
String factoryClasses = System.getProperty(METADATASTORE_PROVIDERS_PROPERTY);
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
providers = new HashMap<>();
if (factoryClasses == null) {
return;
}
String[] classNames = factoryClasses.split(",");
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
for (String className : classNames) {
try {
Class<? extends MetadataStoreProvider> clazz =
(Class<? extends MetadataStoreProvider>) Class.forName(className);
MetadataStoreProvider provider = clazz.getConstructor().newInstance();
String scheme = provider.urlScheme();
providers.put(scheme, provider);
} catch (Exception e) {
log.warn("Failed to load metadata store provider class for name '{}'", className, e);
}
}
}

private static MetadataStoreProvider findProvider(String metadataURL) {
for (Map.Entry<String, MetadataStoreProvider> entry : providers.entrySet()) {
if (metadataURL.startsWith(entry.getKey())) {
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
return entry.getValue();
}
}
return null;
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Removes the identifier from the full metadata url.
*
Expand All @@ -70,6 +114,7 @@ private static MetadataStore newInstance(String metadataURL, MetadataStoreConfig
* @return
*/
public static String removeIdentifierFromMetadataURL(String metadataURL) {
MetadataStoreProvider provider;
if (metadataURL.startsWith(LocalMemoryMetadataStore.MEMORY_SCHEME_IDENTIFIER)) {
return metadataURL.substring(LocalMemoryMetadataStore.MEMORY_SCHEME_IDENTIFIER.length());
} else if (metadataURL.startsWith(RocksdbMetadataStore.ROCKSDB_SCHEME_IDENTIFIER)) {
Expand All @@ -78,6 +123,8 @@ public static String removeIdentifierFromMetadataURL(String metadataURL) {
return metadataURL.substring(EtcdMetadataStore.ETCD_SCHEME_IDENTIFIER.length());
} else if (metadataURL.startsWith(ZKMetadataStore.ZK_SCHEME_IDENTIFIER)) {
return metadataURL.substring(ZKMetadataStore.ZK_SCHEME_IDENTIFIER.length());
} else if ((provider = findProvider(metadataURL)) != null) {
andrasbeni marked this conversation as resolved.
Show resolved Hide resolved
return metadataURL.substring(provider.urlScheme().length());
}
return metadataURL;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.metadata.impl;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.apache.pulsar.metadata.api.GetResult;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.api.MetadataStoreProvider;
import org.apache.pulsar.metadata.api.Stat;
import org.apache.pulsar.metadata.api.extended.CreateOption;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class MetadataStoreFactoryImplTest {

private static Object originalProperty;

@BeforeClass
public void setMetadataStoreProperty() {
originalProperty = System.getProperties().get(MetadataStoreFactoryImpl.METADATASTORE_PROVIDERS_PROPERTY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It certainly does. But I think it is a good practice to leave the static state as it was before the test, regardless of the probability of it being non-null.
On the other hand, I think it's possible for someone using this very feature to run tests with their own provider configured in system properties.

System.setProperty(MetadataStoreFactoryImpl.METADATASTORE_PROVIDERS_PROPERTY,
MyMetadataStoreProvider.class.getName());
MetadataStoreFactoryImpl.loadProviders();
}

@AfterClass
public void resetMetadataStoreProperty() {
if (originalProperty != null) {
System.getProperties().put(MetadataStoreFactoryImpl.METADATASTORE_PROVIDERS_PROPERTY, originalProperty);
}
MetadataStoreFactoryImpl.loadProviders();
}


@Test
public void testCreateWithProvider() throws MetadataStoreException{
MetadataStore instance = MetadataStoreFactoryImpl.create(
"custom://localhost",
MetadataStoreConfig.builder().build());
assertTrue(instance instanceof MyMetadataStore);
}


@Test
public void testRemoveIdentifierFromMetadataURL() {
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("zk:host:port"), "host:port");
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("rocksdb:/data/dir"), "/data/dir");
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("etcd:host:port"), "host:port");
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("memory:name"), "name");
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("http://unknown/url/scheme"), "http://unknown/url/scheme");
assertEquals(MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL("custom:suffix"), "suffix");
}

public static class MyMetadataStoreProvider implements MetadataStoreProvider {

@Override
public String urlScheme() {
return "custom:";
}

@Override
public MetadataStore create(String metadataURL, MetadataStoreConfig metadataStoreConfig,
boolean enableSessionWatcher) throws MetadataStoreException {
return new MyMetadataStore();
}
}

public static class MyMetadataStore extends AbstractMetadataStore {
protected MyMetadataStore() {
super("custom");
}

@Override
protected CompletableFuture<List<String>> getChildrenFromStore(String path) {
return null;
}

@Override
protected CompletableFuture<Boolean> existsFromStore(String path) {
return null;
}

@Override
protected CompletableFuture<Optional<GetResult>> storeGet(String path) {
return null;
}

@Override
protected CompletableFuture<Void> storeDelete(String path, Optional<Long> expectedVersion) {
return null;
}

@Override
protected CompletableFuture<Stat> storePut(String path, byte[] data, Optional<Long> optExpectedVersion,
EnumSet<CreateOption> options) {
return null;
}
}


}