|
20 | 20 | import static org.apache.hadoop.ozone.OzoneConsts.TRANSACTION_INFO_KEY; |
21 | 21 |
|
22 | 22 | import com.google.common.base.Preconditions; |
| 23 | +import java.util.concurrent.atomic.AtomicInteger; |
23 | 24 | import java.util.concurrent.atomic.AtomicLong; |
24 | 25 | import java.util.concurrent.atomic.AtomicReference; |
25 | 26 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
@@ -53,6 +54,7 @@ public class SCMHADBTransactionBufferImpl implements SCMHADBTransactionBuffer { |
53 | 54 | private final AtomicReference<SnapshotInfo> latestSnapshot |
54 | 55 | = new AtomicReference<>(); |
55 | 56 | private final AtomicLong txFlushPending = new AtomicLong(0); |
| 57 | + private final AtomicInteger applyingTransactions = new AtomicInteger(0); |
56 | 58 | private long lastSnapshotTimeMs = 0; |
57 | 59 | private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); |
58 | 60 |
|
@@ -124,30 +126,64 @@ public AtomicReference<SnapshotInfo> getLatestSnapshotRef() { |
124 | 126 | public void flush() throws RocksDatabaseException, CodecException { |
125 | 127 | rwLock.writeLock().lock(); |
126 | 128 | try { |
127 | | - // write latest trx info into trx table in the same batch |
128 | | - Table<String, TransactionInfo> transactionInfoTable |
129 | | - = metadataStore.getTransactionInfoTable(); |
130 | | - transactionInfoTable.putWithBatch(currentBatchOperation, |
131 | | - TRANSACTION_INFO_KEY, latestTrxInfo); |
| 129 | + flushUnderWriteLock(); |
| 130 | + } finally { |
| 131 | + rwLock.writeLock().unlock(); |
| 132 | + } |
| 133 | + } |
132 | 134 |
|
133 | | - metadataStore.getStore().commitBatchOperation(currentBatchOperation); |
134 | | - currentBatchOperation.close(); |
135 | | - this.latestSnapshot.set(latestTrxInfo.toSnapshotInfo()); |
136 | | - // reset batch operation |
137 | | - currentBatchOperation = metadataStore.getStore().initBatchOperation(); |
138 | | - |
139 | | - DeletedBlockLog deletedBlockLog = scm.getScmBlockManager() |
140 | | - .getDeletedBlockLog(); |
141 | | - Preconditions.checkArgument( |
142 | | - deletedBlockLog instanceof DeletedBlockLogImpl); |
143 | | - ((DeletedBlockLogImpl) deletedBlockLog).onFlush(); |
| 135 | + @Override |
| 136 | + public void flushIfNeeded(long snapshotWaitTime) |
| 137 | + throws RocksDatabaseException, CodecException { |
| 138 | + rwLock.writeLock().lock(); |
| 139 | + try { |
| 140 | + if (applyingTransactions.get() > 0) { |
| 141 | + return; |
| 142 | + } |
| 143 | + long timeDiff = scm.getSystemClock().millis() - lastSnapshotTimeMs; |
| 144 | + if (txFlushPending.get() > 0 && timeDiff > snapshotWaitTime) { |
| 145 | + LOG.debug("Running TransactionFlushTask"); |
| 146 | + flushUnderWriteLock(); |
| 147 | + } |
144 | 148 | } finally { |
145 | | - txFlushPending.set(0); |
146 | | - lastSnapshotTimeMs = scm.getSystemClock().millis(); |
147 | 149 | rwLock.writeLock().unlock(); |
148 | 150 | } |
149 | 151 | } |
150 | 152 |
|
| 153 | + private void flushUnderWriteLock() |
| 154 | + throws RocksDatabaseException, CodecException { |
| 155 | + // write latest trx info into trx table in the same batch |
| 156 | + Table<String, TransactionInfo> transactionInfoTable |
| 157 | + = metadataStore.getTransactionInfoTable(); |
| 158 | + transactionInfoTable.putWithBatch(currentBatchOperation, |
| 159 | + TRANSACTION_INFO_KEY, latestTrxInfo); |
| 160 | + |
| 161 | + metadataStore.getStore().commitBatchOperation(currentBatchOperation); |
| 162 | + currentBatchOperation.close(); |
| 163 | + this.latestSnapshot.set(latestTrxInfo.toSnapshotInfo()); |
| 164 | + // reset batch operation |
| 165 | + currentBatchOperation = metadataStore.getStore().initBatchOperation(); |
| 166 | + |
| 167 | + DeletedBlockLog deletedBlockLog = scm.getScmBlockManager() |
| 168 | + .getDeletedBlockLog(); |
| 169 | + Preconditions.checkArgument( |
| 170 | + deletedBlockLog instanceof DeletedBlockLogImpl); |
| 171 | + ((DeletedBlockLogImpl) deletedBlockLog).onFlush(); |
| 172 | + |
| 173 | + txFlushPending.set(0); |
| 174 | + lastSnapshotTimeMs = scm.getSystemClock().millis(); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void beginApplyingTransaction() { |
| 179 | + applyingTransactions.incrementAndGet(); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void endApplyingTransaction() { |
| 184 | + applyingTransactions.decrementAndGet(); |
| 185 | + } |
| 186 | + |
151 | 187 | @Override |
152 | 188 | public void init() throws RocksDatabaseException, CodecException { |
153 | 189 | metadataStore = scm.getScmMetadataStore(); |
|
0 commit comments