| 1 |
/* |
| 2 |
* Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (http://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore.tx; |
| 7 |
|
| 8 |
import org.h2.mvstore.MVMap; |
| 9 |
import org.h2.value.VersionedValue; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class TxDecisionMaker is a base implementation of MVMap.DecisionMaker |
| 13 |
* to be used for TransactionMap modification. |
| 14 |
* |
| 15 |
* @author <a href='mailto:[email protected]'>Andrei Tokar</a> |
| 16 |
*/ |
| 17 |
abstract class TxDecisionMaker extends MVMap.DecisionMaker<VersionedValue> { |
| 18 |
/** |
| 19 |
* Map to decide upon |
| 20 |
*/ |
| 21 |
private final int mapId; |
| 22 |
|
| 23 |
/** |
| 24 |
* Key for the map entry to decide upon |
| 25 |
*/ |
| 26 |
private final Object key; |
| 27 |
|
| 28 |
/** |
| 29 |
* Value for the map entry |
| 30 |
*/ |
| 31 |
final Object value; |
| 32 |
|
| 33 |
/** |
| 34 |
* Transaction we are operating within |
| 35 |
*/ |
| 36 |
private final Transaction transaction; |
| 37 |
|
| 38 |
/** |
| 39 |
* Id for the undo log entry created for this modification |
| 40 |
*/ |
| 41 |
long undoKey; |
| 42 |
|
| 43 |
/** |
| 44 |
* Id of the last operation, we decided to {@link MVMap.Decision.REPEAT}. |
| 45 |
*/ |
| 46 |
private long lastOperationId; |
| 47 |
|
| 48 |
private Transaction blockingTransaction; |
| 49 |
private MVMap.Decision decision; |
| 50 |
|
| 51 |
TxDecisionMaker(int mapId, Object key, Object value, Transaction transaction) { |
| 52 |
this.mapId = mapId; |
| 53 |
this.key = key; |
| 54 |
this.value = value; |
| 55 |
this.transaction = transaction; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public MVMap.Decision decide(VersionedValue existingValue, VersionedValue providedValue) { |
| 60 |
assert decision == null; |
| 61 |
long id; |
| 62 |
int blockingId; |
| 63 |
// if map does not have that entry yet |
| 64 |
if (existingValue == null || |
| 65 |
// or entry is a committed one |
| 66 |
(id = existingValue.getOperationId()) == 0 || |
| 67 |
// or it came from the same transaction |
| 68 |
isThisTransaction(blockingId = TransactionStore.getTransactionId(id))) { |
| 69 |
logIt(existingValue); |
| 70 |
decision = MVMap.Decision.PUT; |
| 71 |
} else if (isCommitted(blockingId)) { |
| 72 |
// Condition above means that entry belongs to a committing transaction. |
| 73 |
// We assume that we are looking at the final value for this transaction, |
| 74 |
// and if it's not the case, then it will fail later, |
| 75 |
// because a tree root has definitely been changed. |
| 76 |
logIt(existingValue.getCurrentValue() == null ? null |
| 77 |
: VersionedValueCommitted.getInstance(existingValue.getCurrentValue())); |
| 78 |
decision = MVMap.Decision.PUT; |
| 79 |
} else if (getBlockingTransaction() != null) { |
| 80 |
// this entry comes from a different transaction, and this |
| 81 |
// transaction is not committed yet |
| 82 |
// should wait on blockingTransaction that was determined earlier |
| 83 |
decision = MVMap.Decision.ABORT; |
| 84 |
} else if (isRepeatedOperation(id)) { |
| 85 |
// There is no transaction with that id, and we've tried it just |
| 86 |
// before, but map root has not changed (which must be the case if |
| 87 |
// we just missed a closed transaction), therefore we came back here |
| 88 |
// again. |
| 89 |
// Now we assume it's a leftover after unclean shutdown (map update |
| 90 |
// was written but not undo log), and will effectively roll it back |
| 91 |
// (just assume committed value and overwrite). |
| 92 |
Object committedValue = existingValue.getCommittedValue(); |
| 93 |
logIt(committedValue == null ? null : VersionedValueCommitted.getInstance(committedValue)); |
| 94 |
decision = MVMap.Decision.PUT; |
| 95 |
} else { |
| 96 |
// transaction has been committed/rolled back and is closed by now, so |
| 97 |
// we can retry immediately and either that entry become committed |
| 98 |
// or we'll hit case above |
| 99 |
decision = MVMap.Decision.REPEAT; |
| 100 |
} |
| 101 |
return decision; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public final void reset() { |
| 106 |
if (decision != MVMap.Decision.REPEAT) { |
| 107 |
lastOperationId = 0; |
| 108 |
if (decision == MVMap.Decision.PUT) { |
| 109 |
// positive decision has been made already and undo record created, |
| 110 |
// but map was updated afterwards and undo record deletion required |
| 111 |
transaction.logUndo(); |
| 112 |
} |
| 113 |
} |
| 114 |
blockingTransaction = null; |
| 115 |
decision = null; |
| 116 |
} |
| 117 |
|
| 118 |
public final MVMap.Decision getDecision() { |
| 119 |
return decision; |
| 120 |
} |
| 121 |
|
| 122 |
final Transaction getBlockingTransaction() { |
| 123 |
return blockingTransaction; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Create undo log entry |
| 128 |
* @param value previous value to be logged |
| 129 |
*/ |
| 130 |
final void logIt(VersionedValue value) { |
| 131 |
undoKey = transaction.log(mapId, key, value); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Check whether specified transaction id belongs to "current" transaction |
| 136 |
* (transaction we are acting within). |
| 137 |
* |
| 138 |
* @param transactionId to check |
| 139 |
* @return true it it is "current" transaction's id, false otherwise |
| 140 |
*/ |
| 141 |
final boolean isThisTransaction(int transactionId) { |
| 142 |
return transactionId == transaction.transactionId; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Determine whether specified id corresponds to a logically committed transaction. |
| 147 |
* In case of pending transaction, reference to actual Transaction object (if any) |
| 148 |
* is preserved for future use. |
| 149 |
* |
| 150 |
* @param transactionId to use |
| 151 |
* @return true if transaction should be considered as committed, false otherwise |
| 152 |
*/ |
| 153 |
final boolean isCommitted(int transactionId) { |
| 154 |
Transaction blockingTx; |
| 155 |
boolean result; |
| 156 |
do { |
| 157 |
blockingTx = transaction.store.getTransaction(transactionId); |
| 158 |
result = transaction.store.committingTransactions.get().get(transactionId); |
| 159 |
} while (blockingTx != transaction.store.getTransaction(transactionId)); |
| 160 |
|
| 161 |
if (!result) { |
| 162 |
blockingTransaction = blockingTx; |
| 163 |
} |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Store operation id provided, but before that, compare it against last stored one. |
| 169 |
* This is to prevent an infinite loop in case of uncommitted "leftover" entry |
| 170 |
* (one without a corresponding undo log entry, most likely as a result of unclean shutdown). |
| 171 |
* |
| 172 |
* @param id for the operation we decided to {@link MVMap.Decision.REPEAT} |
| 173 |
* @return true if the same as last operation id, false otherwise |
| 174 |
*/ |
| 175 |
final boolean isRepeatedOperation(long id) { |
| 176 |
if (id == lastOperationId) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
lastOperationId = id; |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Record for future references specified value as a decision that has been made. |
| 185 |
* |
| 186 |
* @param decision made |
| 187 |
* @return argument provided |
| 188 |
*/ |
| 189 |
final MVMap.Decision setDecision(MVMap.Decision decision) { |
| 190 |
return this.decision = decision; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public final String toString() { |
| 195 |
return "txdm " + transaction.transactionId; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
public static class PutDecisionMaker extends TxDecisionMaker |
| 200 |
{ |
| 201 |
PutDecisionMaker(int mapId, Object key, Object value, Transaction transaction) { |
| 202 |
super(mapId, key, value, transaction); |
| 203 |
} |
| 204 |
|
| 205 |
@SuppressWarnings("unchecked") |
| 206 |
@Override |
| 207 |
public final VersionedValue selectValue(VersionedValue existingValue, VersionedValue providedValue) { |
| 208 |
return VersionedValueUncommitted.getInstance(undoKey, value, |
| 209 |
existingValue == null ? null : existingValue.getCommittedValue()); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
public static final class PutIfAbsentDecisionMaker extends PutDecisionMaker |
| 215 |
{ |
| 216 |
PutIfAbsentDecisionMaker(int mapId, Object key, Object value, Transaction transaction) { |
| 217 |
super(mapId, key, value, transaction); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public MVMap.Decision decide(VersionedValue existingValue, VersionedValue providedValue) { |
| 222 |
assert getDecision() == null; |
| 223 |
int blockingId; |
| 224 |
// if map does not have that entry yet |
| 225 |
if (existingValue == null) { |
| 226 |
logIt(null); |
| 227 |
return setDecision(MVMap.Decision.PUT); |
| 228 |
} else { |
| 229 |
long id = existingValue.getOperationId(); |
| 230 |
if (id == 0 // entry is a committed one |
| 231 |
// or it came from the same transaction |
| 232 |
|| isThisTransaction(blockingId = TransactionStore.getTransactionId(id))) { |
| 233 |
if(existingValue.getCurrentValue() != null) { |
| 234 |
return setDecision(MVMap.Decision.ABORT); |
| 235 |
} |
| 236 |
logIt(existingValue); |
| 237 |
return setDecision(MVMap.Decision.PUT); |
| 238 |
} else if (isCommitted(blockingId)) { |
| 239 |
// entry belongs to a committing transaction |
| 240 |
// and therefore will be committed soon |
| 241 |
if(existingValue.getCurrentValue() != null) { |
| 242 |
return setDecision(MVMap.Decision.ABORT); |
| 243 |
} |
| 244 |
logIt(null); |
| 245 |
return setDecision(MVMap.Decision.PUT); |
| 246 |
} else if (getBlockingTransaction() != null) { |
| 247 |
// this entry comes from a different transaction, and this |
| 248 |
// transaction is not committed yet |
| 249 |
// should wait on blockingTransaction that was determined |
| 250 |
// earlier and then try again |
| 251 |
return setDecision(MVMap.Decision.ABORT); |
| 252 |
} else if (isRepeatedOperation(id)) { |
| 253 |
// There is no transaction with that id, and we've tried it |
| 254 |
// just before, but map root has not changed (which must be |
| 255 |
// the case if we just missed a closed transaction), |
| 256 |
// therefore we came back here again. |
| 257 |
// Now we assume it's a leftover after unclean shutdown (map |
| 258 |
// update was written but not undo log), and will |
| 259 |
// effectively roll it back (just assume committed value and |
| 260 |
// overwrite). |
| 261 |
Object committedValue = existingValue.getCommittedValue(); |
| 262 |
if (committedValue != null) { |
| 263 |
return setDecision(MVMap.Decision.ABORT); |
| 264 |
} |
| 265 |
logIt(null); |
| 266 |
return setDecision(MVMap.Decision.PUT); |
| 267 |
} else { |
| 268 |
// transaction has been committed/rolled back and is closed |
| 269 |
// by now, so we can retry immediately and either that entry |
| 270 |
// become committed or we'll hit case above |
| 271 |
return setDecision(MVMap.Decision.REPEAT); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
public static final class LockDecisionMaker extends TxDecisionMaker |
| 279 |
{ |
| 280 |
LockDecisionMaker(int mapId, Object key, Transaction transaction) { |
| 281 |
super(mapId, key, null, transaction); |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public MVMap.Decision decide(VersionedValue existingValue, VersionedValue providedValue) { |
| 286 |
MVMap.Decision decision = super.decide(existingValue, providedValue); |
| 287 |
if (existingValue == null) { |
| 288 |
assert decision == MVMap.Decision.PUT; |
| 289 |
decision = setDecision(MVMap.Decision.REMOVE); |
| 290 |
} |
| 291 |
return decision; |
| 292 |
} |
| 293 |
|
| 294 |
@SuppressWarnings("unchecked") |
| 295 |
@Override |
| 296 |
public VersionedValue selectValue(VersionedValue existingValue, VersionedValue providedValue) { |
| 297 |
return VersionedValueUncommitted.getInstance(undoKey, |
| 298 |
existingValue == null ? null : existingValue.getCurrentValue(), |
| 299 |
existingValue == null ? null : existingValue.getCommittedValue()); |
| 300 |
} |
| 301 |
} |
| 302 |
} |