-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NativeLabelScanStoreStartupIT.java
170 lines (144 loc) · 5.76 KB
/
NativeLabelScanStoreStartupIT.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.graphdb;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import org.neo4j.collection.PrimitiveLongCollections;
import org.neo4j.index.internal.gbptree.GroupingRecoveryCleanupWorkCollector;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.impl.labelscan.LabelScanStoreTest;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.api.labelscan.LabelScanWriter;
import org.neo4j.kernel.api.labelscan.NodeLabelUpdate;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.kernel.impl.index.labelscan.NativeLabelScanStore;
import org.neo4j.storageengine.api.schema.LabelScanReader;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;
import org.neo4j.test.rule.RandomRule;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class NativeLabelScanStoreStartupIT
{
private static final Label LABEL = Label.label( "testLabel" );
@Rule
public final DatabaseRule dbRule = new EmbeddedDatabaseRule();
@Rule
public final RandomRule random = new RandomRule();
private int labelId;
@Test
public void scanStoreStartWithoutExistentIndex() throws Throwable
{
LabelScanStore labelScanStore = getLabelScanStore();
GroupingRecoveryCleanupWorkCollector workCollector = getGroupingRecoveryCleanupWorkCollector();
labelScanStore.shutdown();
workCollector.shutdown();
deleteLabelScanStoreFiles( dbRule.getStoreDirFile() );
workCollector.init();
labelScanStore.init();
workCollector.start();
labelScanStore.start();
checkLabelScanStoreAccessible( labelScanStore );
}
@Test
public void scanStoreRecreateCorruptedIndexOnStartup() throws Throwable
{
LabelScanStore labelScanStore = getLabelScanStore();
GroupingRecoveryCleanupWorkCollector workCollector = getGroupingRecoveryCleanupWorkCollector();
createTestNode();
long[] labels = readNodesForLabel( labelScanStore );
assertEquals( "Label scan store see 1 label for node", 1, labels.length );
labelScanStore.force( IOLimiter.UNLIMITED );
labelScanStore.shutdown();
workCollector.shutdown();
corruptLabelScanStoreFiles( dbRule.getStoreDirFile() );
workCollector.init();
labelScanStore.init();
workCollector.start();
labelScanStore.start();
long[] rebuildLabels = readNodesForLabel( labelScanStore );
assertArrayEquals( "Store should rebuild corrupted index", labels, rebuildLabels );
}
private LabelScanStore getLabelScanStore()
{
return getDependency( LabelScanStore.class );
}
private GroupingRecoveryCleanupWorkCollector getGroupingRecoveryCleanupWorkCollector()
{
return dbRule.getDependencyResolver().resolveDependency( GroupingRecoveryCleanupWorkCollector.class );
}
private <T> T getDependency( Class<T> clazz )
{
return dbRule.getDependencyResolver().resolveDependency( clazz );
}
private long[] readNodesForLabel( LabelScanStore labelScanStore )
{
try ( LabelScanReader reader = labelScanStore.newReader() )
{
return PrimitiveLongCollections.asArray( reader.nodesWithLabel( labelId ) );
}
}
private Node createTestNode()
{
Node node;
try ( Transaction transaction = dbRule.beginTx() )
{
node = dbRule.createNode( LABEL);
KernelTransaction ktx = dbRule.getDependencyResolver()
.resolveDependency( ThreadToStatementContextBridge.class )
.getKernelTransactionBoundToThisThread( true );
labelId = ktx.tokenRead().nodeLabel( LABEL.name() );
transaction.success();
}
return node;
}
private void scrambleFile( File file ) throws IOException
{
LabelScanStoreTest.scrambleFile( random.random(), file );
}
private static File storeFile( File directory )
{
return new File( directory, NativeLabelScanStore.FILE_NAME );
}
private void corruptLabelScanStoreFiles( File storeDirectory ) throws IOException
{
scrambleFile( storeFile( storeDirectory ) );
}
private void deleteLabelScanStoreFiles( File storeDirectory )
{
assertTrue( storeFile( storeDirectory ).delete() );
}
private void checkLabelScanStoreAccessible( LabelScanStore labelScanStore ) throws IOException
{
int labelId = 1;
try ( LabelScanWriter labelScanWriter = labelScanStore.newWriter() )
{
labelScanWriter.write( NodeLabelUpdate.labelChanges( 1, new long[]{}, new long[]{labelId} ) );
}
try ( LabelScanReader labelScanReader = labelScanStore.newReader() )
{
assertEquals( 1, labelScanReader.nodesWithLabel( labelId ).next() );
}
}
}