You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2018/06/04 20:52:39 UTC
[bookkeeper] 06/06: Provide a util method to run functions with
metadata client driver
This is an automated email from the ASF dual-hosted git repository.
sijie pushed a commit to branch branch-4.7
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git
commit 5be2e90942536d58f50c449a729f0ba26096564f
Author: Sijie Guo <si...@apache.org>
AuthorDate: Mon May 21 22:39:40 2018 -0700
Provide a util method to run functions with metadata client driver
Descriptions of the changes in this PR:
*Motivation*
Currently `MetadataDrivers` provides util methods to run functions with metadata bookie driver.
It is convinient to provide a util method to run functions with metadata client driver as well.
*Solution*
Provide a util method `runFunctionWithMetadataClientDriver` to run functions with metadata client driver.
Author: Sijie Guo <si...@apache.org>
Reviewers: Enrico Olivelli <eo...@gmail.com>, Jia Zhai <None>
This closes #1421 from sijie/utils_to_run_client_driver
---
.../apache/bookkeeper/meta/MetadataDrivers.java | 35 ++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/meta/MetadataDrivers.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/meta/MetadataDrivers.java
index f278205..bab6d84 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/meta/MetadataDrivers.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/meta/MetadataDrivers.java
@@ -25,16 +25,19 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.net.URI;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.discover.RegistrationManager;
import org.apache.bookkeeper.meta.exceptions.Code;
@@ -319,6 +322,38 @@ public final class MetadataDrivers {
}
/**
+ * Process the provided <i>function</i> with metadata client driver resolved
+ * from the metadata service uri returned by {@link ClientConfiguration#getMetadataServiceUri()}.
+ *
+ * @param conf client configuration
+ * @param function function to apply with metadata client driver.
+ * @param executorService executor service used by the metadata client driver.
+ * @throws MetadataException when failed to access metadata store
+ * @throws ExecutionException exception thrown when processing <tt>function</tt>.
+ */
+ public static <T> T runFunctionWithMetadataClientDriver(ClientConfiguration conf,
+ Function<MetadataClientDriver, T> function,
+ ScheduledExecutorService executorService)
+ throws MetadataException, ExecutionException {
+ try (MetadataClientDriver driver = MetadataDrivers.getClientDriver(
+ URI.create(conf.getMetadataServiceUri())
+ )) {
+ driver.initialize(conf, executorService, NullStatsLogger.INSTANCE, Optional.empty());
+ try {
+ return function.apply(driver);
+ } catch (Exception uee) {
+ if (uee.getCause() instanceof MetadataException) {
+ throw (MetadataException) uee.getCause();
+ } else {
+ throw new ExecutionException(uee.getMessage(), uee.getCause());
+ }
+ }
+ } catch (ConfigurationException e) {
+ throw new MetadataException(Code.INVALID_METADATA_SERVICE_URI, e);
+ }
+ }
+
+ /**
* Process the provided <i>function</i> with metadata bookie driver resolved
* from the metadata service uri returned by {@link ServerConfiguration#getMetadataServiceUri()}.
*
--
To stop receiving notification emails like this one, please contact
sijie@apache.org.