You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/11/30 09:56:14 UTC

[GitHub] [pulsar] zymap opened a new pull request #8744: (WIP) Package management bookkeeper storage

zymap opened a new pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744


   <!--
   ### Contribution Checklist
     
     - Name the pull request in the form "[Issue XYZ][component] Title of the pull request", where *XYZ* should be replaced by the actual issue number.
       Skip *Issue XYZ* if there is no associated github issue for this pull request.
       Skip *component* if you are unsure about which is the best component. E.g. `[docs] Fix typo in produce method`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   *(If this PR fixes a github issue, please add `Fixes #<xyz>`.)*
   
   Fixes #<xyz>
   
   *(or if this PR is one task of a github issue, please add `Master Issue: #<xyz>` to link to the master issue.)*
   
   Master Issue: #<xyz>
   
   ### Motivation
   
   
   *Explain here the context, and why you're making that change. What is the problem you're trying to solve.*
   
   ### Modifications
   
   *Describe the modifications you've done.*
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   *(Please pick either of the following options)*
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   *(or)*
   
   This change is already covered by existing tests, such as *(please describe tests)*.
   
   *(or)*
   
   This change added tests and can be verified as follows:
   
   *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Does this pull request potentially affect one of the following parts:
   
   *If `yes` was chosen, please highlight the changes*
   
     - Dependencies (does it add or upgrade a dependency): (yes / no)
     - The public API: (yes / no)
     - The schema: (yes / no / don't know)
     - The default values of configurations: (yes / no)
     - The wire protocol: (yes / no)
     - The rest endpoints: (yes / no)
     - The admin cli options: (yes / no)
     - Anything that affects deployment: (yes / no / don't know)
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a followup issue for adding the documentation
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] zymap commented on pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
zymap commented on pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#issuecomment-736255025


   @sijie @jiazhai @codelipenghui Please take a look when you have time. Thanks.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] zymap commented on pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
zymap commented on pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#issuecomment-736906736


   ping 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] codelipenghui merged pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
codelipenghui merged pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] codelipenghui commented on a change in pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
codelipenghui commented on a change in pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#discussion_r534626820



##########
File path: pulsar-package-management/bookkeeper-storage/src/main/java/org/apache/pulsar/packages/management/storage/bookkeeper/BookKeeperPackagesStorage.java
##########
@@ -0,0 +1,176 @@
+/**
+ * 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.packages.management.storage.bookkeeper;
+
+import com.google.common.base.Strings;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.distributedlog.DistributedLogConfiguration;
+import org.apache.distributedlog.DistributedLogConstants;
+import org.apache.distributedlog.api.DistributedLogManager;
+import org.apache.distributedlog.api.namespace.Namespace;
+import org.apache.distributedlog.api.namespace.NamespaceBuilder;
+import org.apache.distributedlog.exceptions.ZKException;
+import org.apache.distributedlog.impl.metadata.BKDLConfig;
+import org.apache.distributedlog.metadata.DLMetadata;
+import org.apache.distributedlog.namespace.NamespaceDriver;
+import org.apache.pulsar.packages.management.core.PackagesStorage;
+import org.apache.pulsar.packages.management.core.PackagesStorageConfiguration;
+import org.apache.zookeeper.KeeperException;
+
+
+/**
+ * Packages management storage implementation with bookkeeper.
+ */
+@Slf4j
+public class BookKeeperPackagesStorage implements PackagesStorage {
+
+    private final static String NS_CLIENT_ID = "packages-management";
+    final BookKeeperPackagesStorageConfiguration configuration;
+    private Namespace namespace;
+
+    BookKeeperPackagesStorage(PackagesStorageConfiguration configuration) {
+        this.configuration = new BookKeeperPackagesStorageConfiguration(configuration);
+    }
+
+    @Override
+    public void initialize() {
+        DistributedLogConfiguration conf = new DistributedLogConfiguration()
+            .setImmediateFlushEnabled(true)
+            .setOutputBufferSize(0)
+            .setWriteQuorumSize(configuration.getNumReplicas())
+            .setEnsembleSize(configuration.getNumReplicas())
+            .setAckQuorumSize(configuration.getNumReplicas())
+            .setLockTimeout(DistributedLogConstants.LOCK_IMMEDIATE);
+        if (!Strings.isNullOrEmpty(configuration.getBookkeeperClientAuthenticationPlugin())) {
+            conf.setProperty("bkc.clientAuthProviderFactoryClass",
+                configuration.getBookkeeperClientAuthenticationPlugin());
+            if (!Strings.isNullOrEmpty(configuration.getBookkeeperClientAuthenticationParametersName())) {
+                conf.setProperty("bkc." + configuration.getBookkeeperClientAuthenticationParametersName(),
+                    configuration.getBookkeeperClientAuthenticationParameters());
+            }
+        }
+        try {
+            this.namespace = NamespaceBuilder.newBuilder()
+                .conf(conf).clientId(NS_CLIENT_ID).uri(initializeDlogNamespace()).build();
+        } catch (IOException e) {
+            throw new RuntimeException("Initialize distributed log for packages management service failed.", e);
+        }
+        log.info("Packages management bookKeeper storage initialized successfully");
+    }
+
+    private URI initializeDlogNamespace() throws IOException {
+        BKDLConfig bkdlConfig = new BKDLConfig(configuration.getZkServers(), configuration.getLedgersRootPath());
+        DLMetadata dlMetadata = DLMetadata.create(bkdlConfig);
+        URI dlogURI = URI.create(String.format("distributedlog://%s/pulsar/packages", configuration.getZkServers()));
+        try {
+            dlMetadata.create(dlogURI);
+        } catch (ZKException e) {
+            if (e.getKeeperExceptionCode() == KeeperException.Code.NODEEXISTS) {
+                return dlogURI;
+            }
+        }
+        return dlogURI;
+    }
+
+    private CompletableFuture<DistributedLogManager> openLogManagerAsync(String path) {
+        CompletableFuture<DistributedLogManager> logFuture = new CompletableFuture<>();
+        CompletableFuture.runAsync(() -> {
+            try {
+                logFuture.complete(namespace.openLog(path));
+            } catch (IOException e) {
+                logFuture.completeExceptionally(e);
+            }
+        });
+        return logFuture;
+    }
+
+    @Override
+    public CompletableFuture<Void> writeAsync(String path, InputStream inputStream) {
+        return openLogManagerAsync(path)
+            .thenCompose(DLOutputStream::openWriterAsync)
+            .thenCompose(dlOutputStream -> dlOutputStream.writeAsync(inputStream))
+            .thenCompose(DLOutputStream::closeAsync);
+    }
+
+    @Override
+    public CompletableFuture<Void> readAsync(String path, OutputStream outputStream) {
+        return openLogManagerAsync(path)
+            .thenCompose(DLInputStream::openReaderAsync)
+            .thenCompose(dlInputStream -> dlInputStream.readAsync(outputStream))
+            .thenCompose(DLInputStream::closeAsync);
+    }
+
+    @Override
+    public CompletableFuture<Void> deleteAsync(String path) {
+        return namespace.getNamespaceDriver().getLogMetadataStore().getLogLocation(path)
+            .thenCompose(uri -> uri.map(value -> namespace.getNamespaceDriver()
+                .getLogStreamMetadataStore(NamespaceDriver.Role.WRITER).deleteLog(value, path))
+                .orElse(null));
+    }
+
+
+    @Override
+    public CompletableFuture<List<String>> listAsync(String path) {
+        return namespace.getNamespaceDriver().getLogMetadataStore().getLogs(path)
+            .thenApply(logs -> {
+                ArrayList<String> packages = new ArrayList<>();
+                logs.forEachRemaining(packages::add);
+                return packages;
+            });
+    }
+
+    @Override
+    public CompletableFuture<Boolean> existAsync(String path) {
+        CompletableFuture<Boolean> result = new CompletableFuture<>();
+        namespace.getNamespaceDriver().getLogMetadataStore().getLogLocation(path)
+            .whenComplete((uriOptional, throwable) -> {
+                if (throwable != null) {
+                    result.complete(false);
+                    return;
+                }
+
+                if (uriOptional.isPresent()) {
+                    namespace.getNamespaceDriver()
+                        .getLogStreamMetadataStore(NamespaceDriver.Role.WRITER)
+                        .logExists(uriOptional.get(), path)
+                        .whenComplete((ignore, e) -> {
+                            if (e != null) {
+                                result.complete(false);
+                            } else {
+                                result.complete(true);
+                            }
+                        });
+                } else {
+                    result.complete(false);
+                }
+            });
+        return result;    }
+
+    @Override
+    public CompletableFuture<Void> closeAsync() {
+        return CompletableFuture.runAsync(() -> this.namespace.close());

Review comment:
       We should also close the `DistributedLogManager`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] zymap commented on a change in pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
zymap commented on a change in pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#discussion_r534629505



##########
File path: pulsar-package-management/bookkeeper-storage/src/main/java/org/apache/pulsar/packages/management/storage/bookkeeper/BookKeeperPackagesStorage.java
##########
@@ -0,0 +1,176 @@
+/**
+ * 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.packages.management.storage.bookkeeper;
+
+import com.google.common.base.Strings;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.distributedlog.DistributedLogConfiguration;
+import org.apache.distributedlog.DistributedLogConstants;
+import org.apache.distributedlog.api.DistributedLogManager;
+import org.apache.distributedlog.api.namespace.Namespace;
+import org.apache.distributedlog.api.namespace.NamespaceBuilder;
+import org.apache.distributedlog.exceptions.ZKException;
+import org.apache.distributedlog.impl.metadata.BKDLConfig;
+import org.apache.distributedlog.metadata.DLMetadata;
+import org.apache.distributedlog.namespace.NamespaceDriver;
+import org.apache.pulsar.packages.management.core.PackagesStorage;
+import org.apache.pulsar.packages.management.core.PackagesStorageConfiguration;
+import org.apache.zookeeper.KeeperException;
+
+
+/**
+ * Packages management storage implementation with bookkeeper.
+ */
+@Slf4j
+public class BookKeeperPackagesStorage implements PackagesStorage {
+
+    private final static String NS_CLIENT_ID = "packages-management";
+    final BookKeeperPackagesStorageConfiguration configuration;
+    private Namespace namespace;
+
+    BookKeeperPackagesStorage(PackagesStorageConfiguration configuration) {
+        this.configuration = new BookKeeperPackagesStorageConfiguration(configuration);
+    }
+
+    @Override
+    public void initialize() {
+        DistributedLogConfiguration conf = new DistributedLogConfiguration()
+            .setImmediateFlushEnabled(true)
+            .setOutputBufferSize(0)
+            .setWriteQuorumSize(configuration.getNumReplicas())
+            .setEnsembleSize(configuration.getNumReplicas())
+            .setAckQuorumSize(configuration.getNumReplicas())
+            .setLockTimeout(DistributedLogConstants.LOCK_IMMEDIATE);
+        if (!Strings.isNullOrEmpty(configuration.getBookkeeperClientAuthenticationPlugin())) {
+            conf.setProperty("bkc.clientAuthProviderFactoryClass",
+                configuration.getBookkeeperClientAuthenticationPlugin());
+            if (!Strings.isNullOrEmpty(configuration.getBookkeeperClientAuthenticationParametersName())) {
+                conf.setProperty("bkc." + configuration.getBookkeeperClientAuthenticationParametersName(),
+                    configuration.getBookkeeperClientAuthenticationParameters());
+            }
+        }
+        try {
+            this.namespace = NamespaceBuilder.newBuilder()
+                .conf(conf).clientId(NS_CLIENT_ID).uri(initializeDlogNamespace()).build();
+        } catch (IOException e) {
+            throw new RuntimeException("Initialize distributed log for packages management service failed.", e);
+        }
+        log.info("Packages management bookKeeper storage initialized successfully");
+    }
+
+    private URI initializeDlogNamespace() throws IOException {
+        BKDLConfig bkdlConfig = new BKDLConfig(configuration.getZkServers(), configuration.getLedgersRootPath());
+        DLMetadata dlMetadata = DLMetadata.create(bkdlConfig);
+        URI dlogURI = URI.create(String.format("distributedlog://%s/pulsar/packages", configuration.getZkServers()));
+        try {
+            dlMetadata.create(dlogURI);
+        } catch (ZKException e) {
+            if (e.getKeeperExceptionCode() == KeeperException.Code.NODEEXISTS) {
+                return dlogURI;
+            }
+        }
+        return dlogURI;
+    }
+
+    private CompletableFuture<DistributedLogManager> openLogManagerAsync(String path) {
+        CompletableFuture<DistributedLogManager> logFuture = new CompletableFuture<>();
+        CompletableFuture.runAsync(() -> {
+            try {
+                logFuture.complete(namespace.openLog(path));
+            } catch (IOException e) {
+                logFuture.completeExceptionally(e);
+            }
+        });
+        return logFuture;
+    }
+
+    @Override
+    public CompletableFuture<Void> writeAsync(String path, InputStream inputStream) {
+        return openLogManagerAsync(path)
+            .thenCompose(DLOutputStream::openWriterAsync)
+            .thenCompose(dlOutputStream -> dlOutputStream.writeAsync(inputStream))
+            .thenCompose(DLOutputStream::closeAsync);
+    }
+
+    @Override
+    public CompletableFuture<Void> readAsync(String path, OutputStream outputStream) {
+        return openLogManagerAsync(path)
+            .thenCompose(DLInputStream::openReaderAsync)
+            .thenCompose(dlInputStream -> dlInputStream.readAsync(outputStream))
+            .thenCompose(DLInputStream::closeAsync);
+    }
+
+    @Override
+    public CompletableFuture<Void> deleteAsync(String path) {
+        return namespace.getNamespaceDriver().getLogMetadataStore().getLogLocation(path)
+            .thenCompose(uri -> uri.map(value -> namespace.getNamespaceDriver()
+                .getLogStreamMetadataStore(NamespaceDriver.Role.WRITER).deleteLog(value, path))
+                .orElse(null));
+    }
+
+
+    @Override
+    public CompletableFuture<List<String>> listAsync(String path) {
+        return namespace.getNamespaceDriver().getLogMetadataStore().getLogs(path)
+            .thenApply(logs -> {
+                ArrayList<String> packages = new ArrayList<>();
+                logs.forEachRemaining(packages::add);
+                return packages;
+            });
+    }
+
+    @Override
+    public CompletableFuture<Boolean> existAsync(String path) {
+        CompletableFuture<Boolean> result = new CompletableFuture<>();
+        namespace.getNamespaceDriver().getLogMetadataStore().getLogLocation(path)
+            .whenComplete((uriOptional, throwable) -> {
+                if (throwable != null) {
+                    result.complete(false);
+                    return;
+                }
+
+                if (uriOptional.isPresent()) {
+                    namespace.getNamespaceDriver()
+                        .getLogStreamMetadataStore(NamespaceDriver.Role.WRITER)
+                        .logExists(uriOptional.get(), path)
+                        .whenComplete((ignore, e) -> {
+                            if (e != null) {
+                                result.complete(false);
+                            } else {
+                                result.complete(true);
+                            }
+                        });
+                } else {
+                    result.complete(false);
+                }
+            });
+        return result;    }
+
+    @Override
+    public CompletableFuture<Void> closeAsync() {
+        return CompletableFuture.runAsync(() -> this.namespace.close());

Review comment:
       It closed at here https://github.com/apache/pulsar/pull/8744/files#diff-eb374311109d385162c647fec67fb23820f28aa8fc11c58dbdff2c859a51639dR118




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] zymap commented on pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
zymap commented on pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#issuecomment-737579539


   ping


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] zymap commented on pull request #8744: Package management bookkeeper storage implementation

Posted by GitBox <gi...@apache.org>.
zymap commented on pull request #8744:
URL: https://github.com/apache/pulsar/pull/8744#issuecomment-737096705


   ping @codelipenghui @jiazhai @sijie 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org