-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConsistencyChecker.java
327 lines (292 loc) · 12.5 KB
/
ConsistencyChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.index.gbptree;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.neo4j.io.pagecache.PageCursor;
import static java.lang.String.format;
import static org.neo4j.index.gbptree.GenSafePointerPair.pointer;
/**
* <ul>
* Checks:
* <li>order of keys in isolated nodes
* <li>keys fit inside range given by parent node
* <li>sibling pointers match
* <li>GSPP
* </ul>
*/
class ConsistencyChecker<KEY>
{
private final TreeNode<KEY,?> node;
private final KEY readKey;
private final Comparator<KEY> comparator;
private final Layout<KEY,?> layout;
private final List<RightmostInChain<KEY>> rightmostPerLevel = new ArrayList<>();
private final long stableGeneration;
private final long unstableGeneration;
ConsistencyChecker( TreeNode<KEY,?> node, Layout<KEY,?> layout, long stableGeneration, long unstableGeneration )
{
this.node = node;
this.readKey = layout.newKey();
this.comparator = node.keyComparator();
this.layout = layout;
this.stableGeneration = stableGeneration;
this.unstableGeneration = unstableGeneration;
}
public boolean check( PageCursor cursor, long expectedGen ) throws IOException
{
assertOnTreeNode( cursor );
KeyRange<KEY> openRange = new KeyRange<>( comparator, null, null, layout, null );
boolean result = checkSubtree( cursor, openRange, expectedGen, 0 );
// Assert that rightmost node on each level has empty right sibling.
rightmostPerLevel.forEach( RightmostInChain::assertLast );
return result;
}
private void assertOnTreeNode( PageCursor cursor )
{
// TODO also check node type when available
if ( !node.isInternal( cursor ) && !node.isLeaf( cursor ) )
{
throw new IllegalArgumentException( "Cursor is not pinned to a page containing a tree node." );
}
}
private boolean checkSubtree( PageCursor cursor, KeyRange<KEY> range, long expectedGen, int level )
throws IOException
{
// check header pointers
assertNoCrashOrBrokenPointerInGSPP(
cursor, stableGeneration, unstableGeneration, "LeftSibling", TreeNode.BYTE_POS_LEFTSIBLING, node );
assertNoCrashOrBrokenPointerInGSPP(
cursor, stableGeneration, unstableGeneration, "RightSibling", TreeNode.BYTE_POS_RIGHTSIBLING, node );
assertNoCrashOrBrokenPointerInGSPP(
cursor, stableGeneration, unstableGeneration, "NewGen", TreeNode.BYTE_POS_NEWGEN, node );
long pageId = assertSiblings( cursor, level );
checkNewGenPointerGen( cursor );
assertPointerGenMatchesGen( cursor, expectedGen );
if ( node.isInternal( cursor ) )
{
assertKeyOrderAndSubtrees( cursor, pageId, range, expectedGen, level );
}
else if ( node.isLeaf( cursor ) )
{
assertKeyOrder( cursor, range );
}
else
{
throw new TreeInconsistencyException( "Page:" + cursor.getCurrentPageId() + " at level:" + level +
" isn't a tree node, parent expected range " + range );
}
return true;
}
private void assertPointerGenMatchesGen( PageCursor cursor, long expectedGen )
{
long nodeGen = node.gen( cursor );
assert nodeGen <= expectedGen : "Expected node:" + cursor.getCurrentPageId() + " gen:" + nodeGen +
" to be ≤ pointer gen:" + expectedGen;
}
private void checkNewGenPointerGen( PageCursor cursor ) throws IOException
{
long newGen = node.newGen( cursor, stableGeneration, unstableGeneration );
if ( TreeNode.isNode( newGen ) )
{
System.err.println( "WARNING: we ended up on an old generation " + cursor.getCurrentPageId() +
" which had newGen:" + pointer( newGen ) );
long newGenGen = node.pointerGen( cursor, newGen );
long origin = cursor.getCurrentPageId();
node.goTo( cursor, "newGen", newGen );
try
{
assertPointerGenMatchesGen( cursor, newGenGen );
}
finally
{
node.goTo( cursor, "back", origin );
}
}
}
// Assumption: We traverse the tree from left to right on every level
private long assertSiblings( PageCursor cursor, int level )
{
// If this is the first time on this level, we will add a new entry
for ( int i = rightmostPerLevel.size(); i <= level; i++ )
{
RightmostInChain<KEY> first = new RightmostInChain<>( node, stableGeneration, unstableGeneration );
rightmostPerLevel.add( i, first );
}
RightmostInChain<KEY> rightmost = rightmostPerLevel.get( level );
return rightmost.assertNext( cursor );
}
private void assertKeyOrderAndSubtrees( PageCursor cursor, long pageId, KeyRange<KEY> range, long expectedGen,
int level )
throws IOException
{
int keyCount = node.keyCount( cursor );
KEY prev = layout.newKey();
KeyRange<KEY> childRange;
int pos = 0;
while ( pos < keyCount )
{
node.keyAt( cursor, readKey, pos );
assert range.inRange( readKey ) :
readKey + " (pos " + pos + "/" + (keyCount-1) + ")" + " isn't in range " + range;
if ( pos > 0 )
{
assert comparator.compare( prev, readKey ) < 0; // Assume unique keys
}
long child = childAt( cursor, pos );
long childGen = node.pointerGen( cursor, child );
node.goTo( cursor, "child at pos " + pos, child );
if ( pos == 0 )
{
childRange = range.restrictRight( readKey );
}
else
{
childRange = range.restrictLeft( prev ).restrictRight( readKey );
}
checkSubtree( cursor, childRange, childGen, level + 1 );
node.goTo( cursor, "parent", pageId );
layout.copyKey( readKey, prev );
pos++;
}
// Check last child
long child = childAt( cursor, pos );
long childGen = node.pointerGen( cursor, child );
node.goTo( cursor, "child at pos " + pos, child );
childRange = range.restrictLeft( prev );
checkSubtree( cursor, childRange, childGen, level + 1 );
node.goTo( cursor, "parent", pageId );
}
private long childAt( PageCursor cursor, int pos )
{
assertNoCrashOrBrokenPointerInGSPP(
cursor, stableGeneration, unstableGeneration, "Child", node.childOffset( pos ), node );
return node.childAt( cursor, pos, stableGeneration, unstableGeneration );
}
private void assertKeyOrder( PageCursor cursor, KeyRange<KEY> range )
{
int keyCount = node.keyCount( cursor );
KEY prev = layout.newKey();
boolean first = true;
for ( int pos = 0; pos < keyCount; pos++ )
{
node.keyAt( cursor, readKey, pos );
assert range.inRange( readKey ) : "Expected " + readKey + " to be in range " + range;
if ( !first )
{
assert comparator.compare( prev, readKey ) < 0; // Assume unique keys
}
else
{
first = false;
}
layout.copyKey( readKey, prev );
}
}
static void assertNoCrashOrBrokenPointerInGSPP( PageCursor cursor, long stableGeneration, long unstableGeneration,
String pointerFieldName, int offset, TreeNode<?,?> treeNode )
{
cursor.setOffset( offset );
long currentNodeId = cursor.getCurrentPageId();
// A
long generationA = GenSafePointer.readGeneration( cursor );
long pointerA = GenSafePointer.readPointer( cursor );
short checksumA = GenSafePointer.readChecksum( cursor );
boolean correctChecksumA = GenSafePointer.checksumOf( generationA, pointerA ) == checksumA;
byte stateA = GenSafePointerPair.pointerState(
stableGeneration, unstableGeneration, generationA, pointerA, correctChecksumA );
boolean okA = stateA != GenSafePointerPair.BROKEN && stateA != GenSafePointerPair.CRASH;
// B
long generationB = GenSafePointer.readGeneration( cursor );
long pointerB = GenSafePointer.readPointer( cursor );
short checksumB = GenSafePointer.readChecksum( cursor );
boolean correctChecksumB = GenSafePointer.checksumOf( generationB, pointerB ) == checksumB;
byte stateB = GenSafePointerPair.pointerState(
stableGeneration, unstableGeneration, generationB, pointerB, correctChecksumB );
boolean okB = stateB != GenSafePointerPair.BROKEN && stateB != GenSafePointerPair.CRASH;
if ( !(okA && okB) )
{
boolean isInternal = treeNode.isInternal( cursor );
String type = isInternal ? "internal" : "leaf";
throw new TreeInconsistencyException(
"GSPP state found that was not ok in %s field in %s node with id %d%n slotA[%s]%n slotB[%s]",
pointerFieldName, type, currentNodeId,
stateToString( generationA, pointerA, stateA ),
stateToString( generationB, pointerB, stateB ) );
}
}
private static String stateToString( long generationA, long pointerA, byte stateA )
{
return format( "generation=%d, pointer=%d, state=%s",
generationA, pointerA, GenSafePointerPair.pointerStateName( stateA ) );
}
private static class KeyRange<KEY>
{
private final Comparator<KEY> comparator;
private final KEY fromInclusive;
private final KEY toExclusive;
private final Layout<KEY,?> layout;
private final KeyRange<KEY> superRange;
private KeyRange( Comparator<KEY> comparator, KEY fromInclusive, KEY toExclusive, Layout<KEY,?> layout,
KeyRange<KEY> superRange )
{
this.comparator = comparator;
this.superRange = superRange;
this.fromInclusive = fromInclusive == null ? null : layout.copyKey( fromInclusive, layout.newKey() );
this.toExclusive = toExclusive == null ? null : layout.copyKey( toExclusive, layout.newKey() );
this.layout = layout;
}
boolean inRange( KEY key )
{
if ( fromInclusive != null )
{
if ( toExclusive != null )
{
return comparator.compare( key, fromInclusive ) >= 0 && comparator.compare( key, toExclusive ) < 0;
}
return comparator.compare( key, fromInclusive ) >= 0;
}
return toExclusive == null || comparator.compare( key, toExclusive ) < 0;
}
KeyRange<KEY> restrictLeft( KEY left )
{
if ( fromInclusive == null || comparator.compare( fromInclusive, left ) < 0 )
{
return new KeyRange<>( comparator, left, toExclusive, layout, this );
}
return new KeyRange<>( comparator, fromInclusive, toExclusive, layout, this );
}
KeyRange<KEY> restrictRight( KEY right )
{
if ( toExclusive == null || comparator.compare( toExclusive, right ) > 0 )
{
return new KeyRange<>( comparator, fromInclusive, right, layout, this );
}
return new KeyRange<>( comparator, fromInclusive, toExclusive, layout, this );
}
@Override
public String toString()
{
return fromInclusive + " ≤ key < " + toExclusive + (superRange != null ? format( "%n%s", superRange ) : "");
}
}
}