| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class RootReference is an immutable structure to represent state of the MVMap as a whole |
| 10 |
* (not related to a particular B-Tree node). |
| 11 |
* Single structure would allow for non-blocking atomic state change. |
| 12 |
* The most important part of it is a reference to the root node. |
| 13 |
* |
| 14 |
* @author <a href='mailto:[email protected]'>Andrei Tokar</a> |
| 15 |
*/ |
| 16 |
public final class RootReference<K,V> { |
| 17 |
|
| 18 |
/** |
| 19 |
* The root page. |
| 20 |
*/ |
| 21 |
public final Page<K,V> root; |
| 22 |
/** |
| 23 |
* The version used for writing. |
| 24 |
*/ |
| 25 |
public final long version; |
| 26 |
/** |
| 27 |
* Counter of reentrant locks. |
| 28 |
*/ |
| 29 |
private final byte holdCount; |
| 30 |
/** |
| 31 |
* Lock owner thread id. |
| 32 |
*/ |
| 33 |
private final long ownerId; |
| 34 |
/** |
| 35 |
* Reference to the previous root in the chain. |
| 36 |
* That is the last root of the previous version, which had any data changes. |
| 37 |
* Versions without any data changes are dropped from the chain, as it built. |
| 38 |
*/ |
| 39 |
volatile RootReference<K,V> previous; |
| 40 |
/** |
| 41 |
* Counter for successful root updates. |
| 42 |
*/ |
| 43 |
final long updateCounter; |
| 44 |
/** |
| 45 |
* Counter for attempted root updates. |
| 46 |
*/ |
| 47 |
final long updateAttemptCounter; |
| 48 |
/** |
| 49 |
* Size of the occupied part of the append buffer. |
| 50 |
*/ |
| 51 |
private final byte appendCounter; |
| 52 |
|
| 53 |
|
| 54 |
// This one is used to set root initially and for r/o snapshots |
| 55 |
RootReference(Page<K,V> root, long version) { |
| 56 |
this.root = root; |
| 57 |
this.version = version; |
| 58 |
this.previous = null; |
| 59 |
this.updateCounter = 1; |
| 60 |
this.updateAttemptCounter = 1; |
| 61 |
this.holdCount = 0; |
| 62 |
this.ownerId = 0; |
| 63 |
this.appendCounter = 0; |
| 64 |
} |
| 65 |
|
| 66 |
private RootReference(RootReference<K,V> r, Page<K,V> root, long updateAttemptCounter) { |
| 67 |
this.root = root; |
| 68 |
this.version = r.version; |
| 69 |
this.previous = r.previous; |
| 70 |
this.updateCounter = r.updateCounter + 1; |
| 71 |
this.updateAttemptCounter = r.updateAttemptCounter + updateAttemptCounter; |
| 72 |
this.holdCount = 0; |
| 73 |
this.ownerId = 0; |
| 74 |
this.appendCounter = r.appendCounter; |
| 75 |
} |
| 76 |
|
| 77 |
// This one is used for locking |
| 78 |
private RootReference(RootReference<K,V> r, int attempt) { |
| 79 |
this.root = r.root; |
| 80 |
this.version = r.version; |
| 81 |
this.previous = r.previous; |
| 82 |
this.updateCounter = r.updateCounter + 1; |
| 83 |
this.updateAttemptCounter = r.updateAttemptCounter + attempt; |
| 84 |
assert r.holdCount == 0 || r.ownerId == Thread.currentThread().getId() // |
| 85 |
: Thread.currentThread().getId() + " " + r; |
| 86 |
this.holdCount = (byte)(r.holdCount + 1); |
| 87 |
this.ownerId = Thread.currentThread().getId(); |
| 88 |
this.appendCounter = r.appendCounter; |
| 89 |
} |
| 90 |
|
| 91 |
// This one is used for unlocking |
| 92 |
private RootReference(RootReference<K,V> r, Page<K,V> root, boolean keepLocked, int appendCounter) { |
| 93 |
this.root = root; |
| 94 |
this.version = r.version; |
| 95 |
this.previous = r.previous; |
| 96 |
this.updateCounter = r.updateCounter; |
| 97 |
this.updateAttemptCounter = r.updateAttemptCounter; |
| 98 |
assert r.holdCount > 0 && r.ownerId == Thread.currentThread().getId() // |
| 99 |
: Thread.currentThread().getId() + " " + r; |
| 100 |
this.holdCount = (byte)(r.holdCount - (keepLocked ? 0 : 1)); |
| 101 |
this.ownerId = this.holdCount == 0 ? 0 : Thread.currentThread().getId(); |
| 102 |
this.appendCounter = (byte) appendCounter; |
| 103 |
} |
| 104 |
|
| 105 |
// This one is used for version change |
| 106 |
private RootReference(RootReference<K,V> r, long version, int attempt) { |
| 107 |
RootReference<K,V> previous = r; |
| 108 |
RootReference<K,V> tmp; |
| 109 |
while ((tmp = previous.previous) != null && tmp.root == r.root) { |
| 110 |
previous = tmp; |
| 111 |
} |
| 112 |
this.root = r.root; |
| 113 |
this.version = version; |
| 114 |
this.previous = previous; |
| 115 |
this.updateCounter = r.updateCounter + 1; |
| 116 |
this.updateAttemptCounter = r.updateAttemptCounter + attempt; |
| 117 |
this.holdCount = r.holdCount == 0 ? 0 : (byte)(r.holdCount - 1); |
| 118 |
this.ownerId = this.holdCount == 0 ? 0 : r.ownerId; |
| 119 |
assert r.appendCounter == 0; |
| 120 |
this.appendCounter = 0; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Try to unlock. |
| 125 |
* |
| 126 |
* @param newRootPage the new root page |
| 127 |
* @param attemptCounter the number of attempts so far |
| 128 |
* @return the new, unlocked, root reference, or null if not successful |
| 129 |
*/ |
| 130 |
RootReference<K,V> updateRootPage(Page<K,V> newRootPage, long attemptCounter) { |
| 131 |
return isFree() ? tryUpdate(new RootReference<>(this, newRootPage, attemptCounter)) : null; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Try to lock. |
| 136 |
* |
| 137 |
* @param attemptCounter the number of attempts so far |
| 138 |
* @return the new, locked, root reference, or null if not successful |
| 139 |
*/ |
| 140 |
RootReference<K,V> tryLock(int attemptCounter) { |
| 141 |
return canUpdate() ? tryUpdate(new RootReference<>(this, attemptCounter)) : null; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Try to unlock, and if successful update the version |
| 146 |
* |
| 147 |
* @param version the version |
| 148 |
* @param attempt the number of attempts so far |
| 149 |
* @return the new, unlocked and updated, root reference, or null if not successful |
| 150 |
*/ |
| 151 |
RootReference<K,V> tryUnlockAndUpdateVersion(long version, int attempt) { |
| 152 |
return canUpdate() ? tryUpdate(new RootReference<>(this, version, attempt)) : null; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Update the page, possibly keeping it locked. |
| 157 |
* |
| 158 |
* @param page the page |
| 159 |
* @param keepLocked whether to keep it locked |
| 160 |
* @param appendCounter number of items in append buffer |
| 161 |
* @return the new root reference, or null if not successful |
| 162 |
*/ |
| 163 |
RootReference<K,V> updatePageAndLockedStatus(Page<K,V> page, boolean keepLocked, int appendCounter) { |
| 164 |
return canUpdate() ? tryUpdate(new RootReference<>(this, page, keepLocked, appendCounter)) : null; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Removed old versions that are not longer used. |
| 169 |
* |
| 170 |
* @param oldestVersionToKeep the oldest version that needs to be retained |
| 171 |
*/ |
| 172 |
void removeUnusedOldVersions(long oldestVersionToKeep) { |
| 173 |
// We need to keep at least one previous version (if any) here, |
| 174 |
// because in order to retain whole history of some version |
| 175 |
// we really need last root of the previous version. |
| 176 |
// Root labeled with version "X" is the LAST known root for that version |
| 177 |
// and therefore the FIRST known root for the version "X+1" |
| 178 |
for(RootReference<K,V> rootRef = this; rootRef != null; rootRef = rootRef.previous) { |
| 179 |
if (rootRef.version < oldestVersionToKeep) { |
| 180 |
RootReference<K,V> previous; |
| 181 |
assert (previous = rootRef.previous) == null || previous.getAppendCounter() == 0 // |
| 182 |
: oldestVersionToKeep + " " + rootRef.previous; |
| 183 |
rootRef.previous = null; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
boolean isLocked() { |
| 189 |
return holdCount != 0; |
| 190 |
} |
| 191 |
|
| 192 |
private boolean isFree() { |
| 193 |
return holdCount == 0; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
private boolean canUpdate() { |
| 198 |
return isFree() || ownerId == Thread.currentThread().getId(); |
| 199 |
} |
| 200 |
|
| 201 |
public boolean isLockedByCurrentThread() { |
| 202 |
return holdCount != 0 && ownerId == Thread.currentThread().getId(); |
| 203 |
} |
| 204 |
|
| 205 |
private RootReference<K,V> tryUpdate(RootReference<K,V> updatedRootReference) { |
| 206 |
assert canUpdate(); |
| 207 |
return root.map.compareAndSetRoot(this, updatedRootReference) ? updatedRootReference : null; |
| 208 |
} |
| 209 |
|
| 210 |
long getVersion() { |
| 211 |
RootReference<K,V> prev = previous; |
| 212 |
return prev == null || prev.root != root || |
| 213 |
prev.appendCounter != appendCounter ? |
| 214 |
version : prev.getVersion(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Does the root have changes since the specified version? |
| 219 |
* |
| 220 |
* @param version to check against |
| 221 |
* @param persistent whether map is backed by persistent storage |
| 222 |
* @return true if this root has unsaved changes |
| 223 |
*/ |
| 224 |
boolean hasChangesSince(long version, boolean persistent) { |
| 225 |
return persistent && (root.isSaved() ? getAppendCounter() > 0 : getTotalCount() > 0) |
| 226 |
|| getVersion() > version; |
| 227 |
} |
| 228 |
|
| 229 |
int getAppendCounter() { |
| 230 |
return appendCounter & 0xff; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Whether flushing is needed. |
| 235 |
* |
| 236 |
* @return true if yes |
| 237 |
*/ |
| 238 |
public boolean needFlush() { |
| 239 |
return appendCounter != 0; |
| 240 |
} |
| 241 |
|
| 242 |
public long getTotalCount() { |
| 243 |
return root.getTotalCount() + getAppendCounter(); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public String toString() { |
| 248 |
return "RootReference(" + System.identityHashCode(root) + |
| 249 |
", v=" + version + |
| 250 |
", owner=" + ownerId + (ownerId == Thread.currentThread().getId() ? "(current)" : "") + |
| 251 |
", holdCnt=" + holdCount + |
| 252 |
", keys=" + root.getTotalCount() + |
| 253 |
", append=" + getAppendCounter() + |
| 254 |
")"; |
| 255 |
} |
| 256 |
} |