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 2022/11/11 08:37:11 UTC
[GitHub] [pulsar] poorbarcode commented on a diff in pull request #17677: [feat][broker][PIP-195] Implement BookkeeperBucketSnapshotStorage - part3
poorbarcode commented on code in PR #17677:
URL: https://github.com/apache/pulsar/pull/17677#discussion_r1019978133
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BookkeeperBucketSnapshotStorage.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.broker.delayed.bucket;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import javax.validation.constraints.NotNull;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.LedgerEntry;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotMetadata;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BookkeeperBucketSnapshotStorage implements BucketSnapshotStorage {
+
+ private static final Logger log = LoggerFactory.getLogger(BookkeeperBucketSnapshotStorage.class);
+ private static final byte[] LedgerPassword = "".getBytes();
+
+ private final PulsarService pulsar;
+ private final ServiceConfiguration config;
+ private BookKeeper bookKeeper;
+
+ public BookkeeperBucketSnapshotStorage(PulsarService pulsar) {
+ this.pulsar = pulsar;
+ this.config = pulsar.getConfig();
+ }
+
+ @Override
+ public CompletableFuture<Long> createBucketSnapshot(SnapshotMetadata snapshotMetadata,
+ List<SnapshotSegment> bucketSnapshotSegments) {
+ return createLedger()
+ .thenCompose(ledgerHandle -> addEntry(ledgerHandle, snapshotMetadata.toByteArray())
+ .thenApply(__ -> ledgerHandle))
+ .thenCompose(ledgerHandle -> addSnapshotSegments(ledgerHandle, bucketSnapshotSegments)
+ .thenApply(__ -> ledgerHandle))
+ .thenCompose(ledgerHandle -> closeLedger(ledgerHandle)
+ .thenApply(__ -> ledgerHandle.getId()));
+ }
+
+ @Override
+ public CompletableFuture<SnapshotMetadata> getBucketSnapshotMetadata(long bucketId) {
+ return openLedger(bucketId).thenCompose(
+ ledgerHandle -> getLedgerEntry(ledgerHandle, 0, 0).
+ thenCompose(
+ entryEnumeration -> parseSnapshotMetadataEntry(entryEnumeration.nextElement())));
+ }
+
+ @Override
+ public CompletableFuture<List<SnapshotSegment>> getBucketSnapshotSegment(long bucketId, long firstSegmentEntryId,
+ long lastSegmentEntryId) {
+ return openLedger(bucketId).thenCompose(
+ ledgerHandle -> getLedgerEntry(ledgerHandle, firstSegmentEntryId, lastSegmentEntryId).thenCompose(
+ this::parseSnapshotSegmentEntries));
+ }
+
+ @Override
+ public CompletableFuture<Long> getBucketSnapshotLength(long bucketId) {
+ return openLedger(bucketId).thenApply(ledgerHandle -> {
+ long length = ledgerHandle.getLength();
+ closeLedger(ledgerHandle);
+ return length;
+ });
+ }
+
+ @Override
+ public CompletableFuture<Void> deleteBucketSnapshot(long bucketId) {
+ return deleteLedger(bucketId);
+ }
+
+ @Override
+ public void start() throws Exception {
+ this.bookKeeper = pulsar.getBookKeeperClientFactory().create(
Review Comment:
Do we really need a separate BK client?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BookkeeperBucketSnapshotStorage.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.broker.delayed.bucket;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import javax.validation.constraints.NotNull;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.LedgerEntry;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotMetadata;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BookkeeperBucketSnapshotStorage implements BucketSnapshotStorage {
+
+ private static final Logger log = LoggerFactory.getLogger(BookkeeperBucketSnapshotStorage.class);
+ private static final byte[] LedgerPassword = "".getBytes();
+
+ private final PulsarService pulsar;
+ private final ServiceConfiguration config;
+ private BookKeeper bookKeeper;
+
+ public BookkeeperBucketSnapshotStorage(PulsarService pulsar) {
+ this.pulsar = pulsar;
+ this.config = pulsar.getConfig();
+ }
+
+ @Override
+ public CompletableFuture<Long> createBucketSnapshot(SnapshotMetadata snapshotMetadata,
Review Comment:
If the ledger was created successfully, but cursor properties persistence failed. What should we do with these orphaned ledgers?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BookkeeperBucketSnapshotStorage.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.broker.delayed.bucket;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import javax.validation.constraints.NotNull;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.LedgerEntry;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotMetadata;
+import org.apache.pulsar.broker.delayed.proto.DelayedMessageIndexBucketSnapshotFormat.SnapshotSegment;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BookkeeperBucketSnapshotStorage implements BucketSnapshotStorage {
+
+ private static final Logger log = LoggerFactory.getLogger(BookkeeperBucketSnapshotStorage.class);
+ private static final byte[] LedgerPassword = "".getBytes();
+
+ private final PulsarService pulsar;
+ private final ServiceConfiguration config;
+ private BookKeeper bookKeeper;
+
+ public BookkeeperBucketSnapshotStorage(PulsarService pulsar) {
+ this.pulsar = pulsar;
+ this.config = pulsar.getConfig();
+ }
+
+ @Override
+ public CompletableFuture<Long> createBucketSnapshot(SnapshotMetadata snapshotMetadata,
+ List<SnapshotSegment> bucketSnapshotSegments) {
+ return createLedger()
+ .thenCompose(ledgerHandle -> addEntry(ledgerHandle, snapshotMetadata.toByteArray())
+ .thenApply(__ -> ledgerHandle))
+ .thenCompose(ledgerHandle -> addSnapshotSegments(ledgerHandle, bucketSnapshotSegments)
+ .thenApply(__ -> ledgerHandle))
+ .thenCompose(ledgerHandle -> closeLedger(ledgerHandle)
+ .thenApply(__ -> ledgerHandle.getId()));
+ }
+
+ @Override
+ public CompletableFuture<SnapshotMetadata> getBucketSnapshotMetadata(long bucketId) {
+ return openLedger(bucketId).thenCompose(
Review Comment:
Should we close this `LedgerHandle`?
Same for methods `getBucketSnapshotSegment` and `getBucketSnapshotLength`
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/bucket/BucketSnapshotException.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.broker.delayed.bucket;
+
+import org.apache.pulsar.broker.service.BrokerServiceException;
+
+public class BucketSnapshotException extends BrokerServiceException {
Review Comment:
Suggest making `BucketSnapshotException` extend `PersistenceException` instead of `BrokerServiceException `. Because if you add a new exception, later exception handling needs to be adapted as well.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org