-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[feat][txn] Implement the AbortedTxnProcessor for TransactionBuffer #17847
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
Merged
congbobo184
merged 33 commits into
apache:master
from
liangyepianzhou:xiangying/pip196/aborted_txn_processor
Oct 31, 2022
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
eaf5d25
Add Processor for snapshotSegment
liangyepianzhou b097c21
optimize Processor for snapshotSegment
liangyepianzhou ce6d1ef
Add Processor for singleSnapshot
liangyepianzhou 4a22a6a
modify the TransactionBuffer to use AbortedTxnProcessor
liangyepianzhou 28380bb
delete TODO
liangyepianzhou 15f721d
add License
liangyepianzhou a4cb112
add description for interface
liangyepianzhou 280f0d3
optimize and fix some test.
liangyepianzhou 5b2699c
optimize and fix some test.
liangyepianzhou 0474172
chenge semaphore to state and some optimize
liangyepianzhou 393bd0c
Add persistent Worker
liangyepianzhou 98ad078
make updateSnapshotIndex have a fixed index snapshot
liangyepianzhou 9904eb8
add test and fix bug for check aborted transactiion id
liangyepianzhou 8c53f1a
fix some comments
liangyepianzhou b951329
optimization
liangyepianzhou 7b9fdad
change to TxnID
liangyepianzhou da05668
Move the task implementation to PersistentWorker
liangyepianzhou 721c096
clear logic
liangyepianzhou ae9af72
fix some comments
liangyepianzhou 0e4a73e
change synchronized `(SingleSnapshotAbortedTxnProcessorImpl.this)` to…
liangyepianzhou 45da1e6
move maxReadPosition back to TB
liangyepianzhou d3c99c1
fix some comments
liangyepianzhou c2425b5
fix some comments
liangyepianzhou 5191c3d
fix some comments
liangyepianzhou ba9c374
topicTransactionBuffer changed
765795c
fix
liangyepianzhou 2ad6811
delete SnapshotSegmentAbortedTxnProcessorImpl.java
liangyepianzhou 1a070cb
fix some comments
liangyepianzhou 99abe3f
checkstyle
liangyepianzhou fb68b81
Merge remote-tracking branch 'apache/master' into xiangying/pip196/ab…
liangyepianzhou 5725b34
license header
liangyepianzhou 8f73634
fix test
liangyepianzhou 7b1b817
unused imported
liangyepianzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/AbortedTxnProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.transaction.buffer; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import org.apache.bookkeeper.mledger.Position; | ||
| import org.apache.bookkeeper.mledger.impl.PositionImpl; | ||
| import org.apache.pulsar.client.api.transaction.TxnID; | ||
|
|
||
|
|
||
| public interface AbortedTxnProcessor { | ||
|
|
||
| /** | ||
| * After the transaction buffer writes a transaction aborted marker to the topic, | ||
| * the transaction buffer will put the aborted txnID and the aborted marker position to AbortedTxnProcessor. | ||
| * @param txnID aborted transaction ID. | ||
| * @param position the position of the abort txnID | ||
| */ | ||
| void putAbortedTxnAndPosition(TxnID txnID, PositionImpl position); | ||
|
|
||
| /** | ||
| * Clean up invalid aborted transactions. | ||
| */ | ||
| void trimExpiredAbortedTxns(); | ||
|
|
||
| /** | ||
| * Check whether the transaction ID is an aborted transaction ID. | ||
| * @param txnID the transaction ID that needs to be checked. | ||
| * @param readPosition the read position of the transaction message, can be used to find the segment. | ||
| * @return a boolean, whether the transaction ID is an aborted transaction ID. | ||
| */ | ||
| boolean checkAbortedTransaction(TxnID txnID, Position readPosition); | ||
|
|
||
| /** | ||
| * Recover transaction buffer by transaction buffer snapshot. | ||
| * @return a Position (startReadCursorPosition) determiner where to start to recover in the original topic. | ||
| */ | ||
|
|
||
| CompletableFuture<PositionImpl> recoverFromSnapshot(); | ||
|
|
||
| /** | ||
| * Delete the transaction buffer aborted transaction snapshot. | ||
| * @return a completableFuture. | ||
| */ | ||
| CompletableFuture<Void> deleteAbortedTxnSnapshot(); | ||
|
|
||
| /** | ||
| * Take aborted transactions snapshot. | ||
| * @return a completableFuture. | ||
| */ | ||
| CompletableFuture<Void> takeAbortedTxnsSnapshot(PositionImpl maxReadPosition); | ||
|
|
||
| /** | ||
| * Get the lastSnapshotTimestamps. | ||
| * @return the lastSnapshotTimestamps. | ||
| */ | ||
| long getLastSnapshotTimestamps(); | ||
|
|
||
| CompletableFuture<Void> closeAsync(); | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
...g/apache/pulsar/broker/transaction/buffer/impl/SingleSnapshotAbortedTxnProcessorImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| /* | ||
| * 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.transaction.buffer.impl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.bookkeeper.mledger.Position; | ||
| import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl; | ||
| import org.apache.bookkeeper.mledger.impl.PositionImpl; | ||
| import org.apache.commons.collections4.map.LinkedMap; | ||
| import org.apache.pulsar.broker.service.persistent.PersistentTopic; | ||
| import org.apache.pulsar.broker.systopic.SystemTopicClient; | ||
| import org.apache.pulsar.broker.transaction.buffer.AbortedTxnProcessor; | ||
| import org.apache.pulsar.broker.transaction.buffer.metadata.AbortTxnMetadata; | ||
| import org.apache.pulsar.broker.transaction.buffer.metadata.TransactionBufferSnapshot; | ||
| import org.apache.pulsar.client.api.Message; | ||
| import org.apache.pulsar.client.api.transaction.TxnID; | ||
| import org.apache.pulsar.common.naming.TopicName; | ||
| import org.apache.pulsar.common.util.FutureUtil; | ||
|
|
||
| @Slf4j | ||
| public class SingleSnapshotAbortedTxnProcessorImpl implements AbortedTxnProcessor { | ||
| private final PersistentTopic topic; | ||
| private final CompletableFuture<SystemTopicClient.Writer<TransactionBufferSnapshot>> takeSnapshotWriter; | ||
| /** | ||
| * Aborts, map for jude message is aborted, linked for remove abort txn in memory when this | ||
| * position have been deleted. | ||
| */ | ||
| private final LinkedMap<TxnID, PositionImpl> aborts = new LinkedMap<>(); | ||
|
|
||
| private volatile long lastSnapshotTimestamps; | ||
|
|
||
| public SingleSnapshotAbortedTxnProcessorImpl(PersistentTopic topic) { | ||
| this.topic = topic; | ||
| this.takeSnapshotWriter = this.topic.getBrokerService().getPulsar() | ||
| .getTransactionBufferSnapshotServiceFactory() | ||
| .getTxnBufferSnapshotService().createWriter(TopicName.get(topic.getName())); | ||
| } | ||
|
|
||
| @Override | ||
| public void putAbortedTxnAndPosition(TxnID abortedTxnId, PositionImpl position) { | ||
| aborts.put(abortedTxnId, position); | ||
| } | ||
|
|
||
| //In this implementation we clear the invalid aborted txn ID one by one. | ||
| @Override | ||
| public void trimExpiredAbortedTxns() { | ||
| while (!aborts.isEmpty() && !((ManagedLedgerImpl) topic.getManagedLedger()) | ||
| .ledgerExists(aborts.get(aborts.firstKey()).getLedgerId())) { | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("[{}] Topic transaction buffer clear aborted transaction, TxnId : {}, Position : {}", | ||
| topic.getName(), aborts.firstKey(), aborts.get(aborts.firstKey())); | ||
| } | ||
| aborts.remove(aborts.firstKey()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean checkAbortedTransaction(TxnID txnID, Position readPosition) { | ||
| return aborts.containsKey(txnID); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public CompletableFuture<PositionImpl> recoverFromSnapshot() { | ||
| return topic.getBrokerService().getPulsar().getTransactionBufferSnapshotServiceFactory() | ||
| .getTxnBufferSnapshotService() | ||
| .createReader(TopicName.get(topic.getName())).thenComposeAsync(reader -> { | ||
| PositionImpl startReadCursorPosition = null; | ||
| try { | ||
| while (reader.hasMoreEvents()) { | ||
| Message<TransactionBufferSnapshot> message = reader.readNext(); | ||
| if (topic.getName().equals(message.getKey())) { | ||
| TransactionBufferSnapshot transactionBufferSnapshot = message.getValue(); | ||
| if (transactionBufferSnapshot != null) { | ||
| handleSnapshot(transactionBufferSnapshot); | ||
| startReadCursorPosition = PositionImpl.get( | ||
| transactionBufferSnapshot.getMaxReadPositionLedgerId(), | ||
| transactionBufferSnapshot.getMaxReadPositionEntryId()); | ||
| } | ||
| } | ||
| } | ||
| closeReader(reader); | ||
| return CompletableFuture.completedFuture(startReadCursorPosition); | ||
| } catch (Exception ex) { | ||
| log.error("[{}] Transaction buffer recover fail when read " | ||
| + "transactionBufferSnapshot!", topic.getName(), ex); | ||
| closeReader(reader); | ||
| return FutureUtil.failedFuture(ex); | ||
| } | ||
|
|
||
| }, topic.getBrokerService().getPulsar().getTransactionExecutorProvider() | ||
| .getExecutor(this)); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> deleteAbortedTxnSnapshot() { | ||
| return this.takeSnapshotWriter.thenCompose(writer -> { | ||
| TransactionBufferSnapshot snapshot = new TransactionBufferSnapshot(); | ||
| snapshot.setTopicName(topic.getName()); | ||
| return writer.deleteAsync(snapshot.getTopicName(), snapshot); | ||
| }).thenRun(() -> { | ||
| log.info("[{}] Successes to delete the aborted transaction snapshot", this.topic); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> takeAbortedTxnsSnapshot(PositionImpl maxReadPosition) { | ||
| return takeSnapshotWriter.thenCompose(writer -> { | ||
| TransactionBufferSnapshot snapshot = new TransactionBufferSnapshot(); | ||
| snapshot.setTopicName(topic.getName()); | ||
| snapshot.setMaxReadPositionLedgerId(maxReadPosition.getLedgerId()); | ||
| snapshot.setMaxReadPositionEntryId(maxReadPosition.getEntryId()); | ||
| List<AbortTxnMetadata> list = new ArrayList<>(); | ||
| aborts.forEach((k, v) -> { | ||
| AbortTxnMetadata abortTxnMetadata = new AbortTxnMetadata(); | ||
| abortTxnMetadata.setTxnIdMostBits(k.getMostSigBits()); | ||
| abortTxnMetadata.setTxnIdLeastBits(k.getLeastSigBits()); | ||
| abortTxnMetadata.setLedgerId(v.getLedgerId()); | ||
| abortTxnMetadata.setEntryId(v.getEntryId()); | ||
| list.add(abortTxnMetadata); | ||
| }); | ||
| snapshot.setAborts(list); | ||
| return writer.writeAsync(snapshot.getTopicName(), snapshot).thenAccept(messageId -> { | ||
| this.lastSnapshotTimestamps = System.currentTimeMillis(); | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("[{}]Transaction buffer take snapshot success! " | ||
| + "messageId : {}", topic.getName(), messageId); | ||
| } | ||
| }).exceptionally(e -> { | ||
| log.warn("[{}]Transaction buffer take snapshot fail! ", topic.getName(), e.getCause()); | ||
| return null; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public long getLastSnapshotTimestamps() { | ||
| return this.lastSnapshotTimestamps; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> closeAsync() { | ||
| return takeSnapshotWriter.thenCompose(SystemTopicClient.Writer::closeAsync); | ||
| } | ||
|
|
||
| private void closeReader(SystemTopicClient.Reader<TransactionBufferSnapshot> reader) { | ||
| reader.closeAsync().exceptionally(e -> { | ||
| log.error("[{}]Transaction buffer reader close error!", topic.getName(), e); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| private void handleSnapshot(TransactionBufferSnapshot snapshot) { | ||
| if (snapshot.getAborts() != null) { | ||
| snapshot.getAborts().forEach(abortTxnMetadata -> | ||
| aborts.put(new TxnID(abortTxnMetadata.getTxnIdMostBits(), | ||
| abortTxnMetadata.getTxnIdLeastBits()), | ||
| PositionImpl.get(abortTxnMetadata.getLedgerId(), | ||
| abortTxnMetadata.getEntryId()))); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.