-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CrashGenerationCleaner.java
250 lines (223 loc) · 9.36 KB
/
CrashGenerationCleaner.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
/*
* Copyright (c) 2002-2018 "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.internal.gbptree;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.neo4j.index.internal.gbptree.GBPTree.Monitor;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.neo4j.helpers.Exceptions.launderedException;
/**
* Scans the entire tree and checks all GSPPs, replacing all CRASH gen GSPs with zeros.
*/
class CrashGenerationCleaner
{
private final PagedFile pagedFile;
private final TreeNode<?,?> treeNode;
private final long lowTreeNodeId;
private final long highTreeNodeId;
private final int availableProcessors;
private final long batchSize;
private final long stableGeneration;
private final long unstableGeneration;
private final Monitor monitor;
private final long internalMaxKeyCount;
CrashGenerationCleaner( PagedFile pagedFile, TreeNode<?,?> treeNode, long lowTreeNodeId, long highTreeNodeId,
long stableGeneration, long unstableGeneration, Monitor monitor )
{
this.pagedFile = pagedFile;
this.treeNode = treeNode;
this.lowTreeNodeId = lowTreeNodeId;
this.highTreeNodeId = highTreeNodeId;
this.availableProcessors = Runtime.getRuntime().availableProcessors();
this.batchSize = // Each processor will get roughly 100 batches each
min( 1000, max( 10, (highTreeNodeId - lowTreeNodeId) / (100 * availableProcessors) ) );
this.stableGeneration = stableGeneration;
this.unstableGeneration = unstableGeneration;
this.monitor = monitor;
this.internalMaxKeyCount = treeNode.internalMaxKeyCount();
}
// === Methods about the execution and threading ===
public void clean() throws IOException
{
monitor.cleanupStarted();
assert unstableGeneration > stableGeneration : unexpectedGenerations();
assert unstableGeneration - stableGeneration > 1 : unexpectedGenerations();
long startTime = currentTimeMillis();
int threads = availableProcessors;
ExecutorService executor = Executors.newFixedThreadPool( threads );
AtomicLong nextId = new AtomicLong( lowTreeNodeId );
AtomicReference<Throwable> error = new AtomicReference<>();
AtomicInteger cleanedPointers = new AtomicInteger();
for ( int i = 0; i < threads; i++ )
{
executor.submit( cleaner( nextId, error, cleanedPointers ) );
}
executor.shutdown();
try
{
long lastProgression = nextId.get();
// Have max no-progress-timeout quite high to be able to cope with huge
// I/O congestion spikes w/o failing in vain.
while ( !executor.awaitTermination( 30, SECONDS ) )
{
if ( lastProgression == nextId.get() )
{
// No progression at all, abort?
error.compareAndSet( null, new IOException( "No progress, so forcing abort" ) );
}
lastProgression = nextId.get();
}
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
}
Throwable finalError = error.get();
if ( finalError != null )
{
throw launderedException( IOException.class, finalError );
}
long endTime = currentTimeMillis();
monitor.cleanupFinished( highTreeNodeId - lowTreeNodeId, cleanedPointers.get(), endTime - startTime );
}
private Runnable cleaner( AtomicLong nextId, AtomicReference<Throwable> error, AtomicInteger cleanedPointers )
{
return () ->
{
try ( PageCursor cursor = pagedFile.io( 0, PagedFile.PF_SHARED_READ_LOCK );
PageCursor writeCursor = pagedFile.io( 0, PagedFile.PF_SHARED_WRITE_LOCK ) )
{
long localNextId;
while ( ( localNextId = nextId.getAndAdd( batchSize )) < highTreeNodeId )
{
for ( int i = 0; i < batchSize && localNextId < highTreeNodeId; i++, localNextId++ )
{
PageCursorUtil.goTo( cursor, "clean", localNextId );
if ( hasCrashedGSPP( treeNode, cursor ) )
{
writeCursor.next( cursor.getCurrentPageId() );
cleanTreeNode( treeNode, writeCursor, cleanedPointers );
}
}
// Check error status after a batch, to reduce volatility overhead.
// Is this over thinking things? Perhaps
if ( error.get() != null )
{
break;
}
}
}
catch ( Throwable e )
{
error.compareAndSet( null, e );
}
};
}
// === Methods about checking if a tree node has crashed pointers ===
private boolean hasCrashedGSPP( TreeNode<?,?> treeNode, PageCursor cursor ) throws IOException
{
boolean isTreeNode;
int keyCount;
do
{
isTreeNode = TreeNode.nodeType( cursor ) == TreeNode.NODE_TYPE_TREE_NODE;
keyCount = TreeNode.keyCount( cursor );
}
while ( cursor.shouldRetry() );
PageCursorUtil.checkOutOfBounds( cursor );
if ( !isTreeNode )
{
return false;
}
boolean hasCrashed;
do
{
hasCrashed =
hasCrashedGSPP( cursor, TreeNode.BYTE_POS_SUCCESSOR ) ||
hasCrashedGSPP( cursor, TreeNode.BYTE_POS_LEFTSIBLING ) ||
hasCrashedGSPP( cursor, TreeNode.BYTE_POS_RIGHTSIBLING );
if ( !hasCrashed && TreeNode.isInternal( cursor ) )
{
for ( int i = 0; i <= keyCount && i <= internalMaxKeyCount && !hasCrashed; i++ )
{
hasCrashed = hasCrashedGSPP( cursor, treeNode.childOffset( i ) );
}
}
}
while ( cursor.shouldRetry() );
PageCursorUtil.checkOutOfBounds( cursor );
return hasCrashed;
}
private boolean hasCrashedGSPP( PageCursor cursor, int gsppOffset )
{
return hasCrashedGSP( cursor, gsppOffset ) || hasCrashedGSP( cursor, gsppOffset + GenerationSafePointer.SIZE );
}
private boolean hasCrashedGSP( PageCursor cursor, int offset )
{
cursor.setOffset( offset );
long generation = GenerationSafePointer.readGeneration( cursor );
return generation > stableGeneration && generation < unstableGeneration;
}
// === Methods about actually cleaning a discovered crashed tree node ===
private void cleanTreeNode( TreeNode<?,?> treeNode, PageCursor cursor, AtomicInteger cleanedPointers )
{
cleanCrashedGSPP( cursor, TreeNode.BYTE_POS_SUCCESSOR, cleanedPointers );
cleanCrashedGSPP( cursor, TreeNode.BYTE_POS_LEFTSIBLING, cleanedPointers );
cleanCrashedGSPP( cursor, TreeNode.BYTE_POS_RIGHTSIBLING, cleanedPointers );
if ( TreeNode.isInternal( cursor ) )
{
int keyCount = TreeNode.keyCount( cursor );
for ( int i = 0; i <= keyCount && i <= internalMaxKeyCount; i++ )
{
cleanCrashedGSPP( cursor, treeNode.childOffset( i ), cleanedPointers );
}
}
}
private void cleanCrashedGSPP( PageCursor cursor, int gsppOffset, AtomicInteger cleanedPointers )
{
cleanCrashedGSP( cursor, gsppOffset, cleanedPointers );
cleanCrashedGSP( cursor, gsppOffset + GenerationSafePointer.SIZE, cleanedPointers );
}
/**
* NOTE: No shouldRetry is used because cursor is assumed to be a write cursor.
*/
private void cleanCrashedGSP( PageCursor cursor, int gspOffset, AtomicInteger cleanedPointers )
{
if ( hasCrashedGSP( cursor, gspOffset ) )
{
cursor.setOffset( gspOffset );
GenerationSafePointer.clean( cursor );
cleanedPointers.incrementAndGet();
}
}
private String unexpectedGenerations( )
{
return "Unexpected generations, stableGeneration=" + stableGeneration + ", unstableGeneration=" + unstableGeneration;
}
}