-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
IndexWorkSyncTransactionApplicationStressIT.java
243 lines (219 loc) · 9.6 KB
/
IndexWorkSyncTransactionApplicationStressIT.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
/*
* 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.kernel.impl.transaction.command;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.TransactionQueue;
import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.api.index.IndexProxy;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProvider;
import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProviderFactory;
import org.neo4j.kernel.impl.api.state.TxState;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngineRule;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.transaction.command.Command.NodeCommand;
import org.neo4j.kernel.impl.util.Dependencies;
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.test.PageCacheRule;
import org.neo4j.test.TargetDirectory;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.Workers;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.neo4j.helpers.TimeUtil.parseTimeMillis;
import static org.neo4j.kernel.api.properties.Property.noNodeProperty;
import static org.neo4j.kernel.api.properties.Property.property;
import static org.neo4j.kernel.impl.transaction.command.Commands.createIndexRule;
import static org.neo4j.kernel.impl.transaction.command.Commands.transactionRepresentation;
import static org.neo4j.kernel.impl.transaction.log.Commitment.NO_COMMITMENT;
public class IndexWorkSyncTransactionApplicationStressIT
{
private final FileSystemAbstraction fs = new DefaultFileSystemAbstraction();
private final RecordStorageEngineRule storageEngineRule = new RecordStorageEngineRule();
private final TargetDirectory.TestDirectory directory = TargetDirectory.testDirForTest( getClass() );
private final PageCacheRule pageCacheRule = new PageCacheRule();
@Rule
public RuleChain ruleChain = RuleChain.outerRule( directory )
.around( pageCacheRule )
.around( storageEngineRule );
private final int labelId = 0;
private final int propertyKeyId = 0;
@Test
public void shouldApplyIndexUpdatesInWorkSyncedBatches() throws Exception
{
// GIVEN
long duration = parseTimeMillis.apply( System.getProperty( getClass().getName() + ".duration", "2s" ) );
int numThreads = Integer.getInteger( getClass().getName() + ".numThreads",
Runtime.getRuntime().availableProcessors() );
RecordStorageEngine storageEngine = storageEngineRule
.getWith( fs, pageCacheRule.getPageCache( fs ) )
.storeDirectory( directory.directory() )
.indexProvider( new InMemoryIndexProvider() )
.build();
storageEngine.apply( tx( asList( createIndexRule(
InMemoryIndexProviderFactory.PROVIDER_DESCRIPTOR, 1, labelId, propertyKeyId ) ) ),
TransactionApplicationMode.EXTERNAL );
Dependencies dependencies = new Dependencies();
storageEngine.satisfyDependencies( dependencies );
IndexProxy index = dependencies.resolveDependency( IndexingService.class )
.getIndexProxy( new IndexDescriptor( labelId, propertyKeyId ) );
awaitOnline( index );
// WHEN
Workers<Worker> workers = new Workers<>( getClass().getSimpleName() );
final AtomicBoolean end = new AtomicBoolean();
for ( int i = 0; i < numThreads; i++ )
{
workers.start( new Worker( i, end, storageEngine, 10, index ) );
}
// let the threads hammer the storage engine for some time
Thread.sleep( duration );
end.set( true );
// THEN (assertions as part of the workers applying transactions)
workers.awaitAndThrowOnError( RuntimeException.class );
}
private void awaitOnline( IndexProxy index ) throws InterruptedException
{
while ( index.getState() == InternalIndexState.POPULATING )
{
Thread.sleep( 10 );
}
}
private static Object propertyValue( int id, int progress )
{
return id + "_" + progress;
}
private static TransactionToApply tx( Collection<StorageCommand> commands )
{
TransactionToApply tx = new TransactionToApply( transactionRepresentation( commands ) );
tx.commitment( NO_COMMITMENT, 0 );
return tx;
}
private class Worker implements Runnable
{
private final int id;
private final AtomicBoolean end;
private final RecordStorageEngine storageEngine;
private final NodeStore nodeIds;
private final int batchSize;
private final IndexProxy index;
private int i, base;
public Worker( int id, AtomicBoolean end, RecordStorageEngine storageEngine, int batchSize, IndexProxy index )
{
this.id = id;
this.end = end;
this.storageEngine = storageEngine;
this.batchSize = batchSize;
this.index = index;
NeoStores neoStores = this.storageEngine.testAccessNeoStores();
this.nodeIds = neoStores.getNodeStore();
}
@Override
public void run()
{
try
{
TransactionQueue queue = new TransactionQueue( batchSize, (tx, last) -> {
// Apply
storageEngine.apply( tx, TransactionApplicationMode.EXTERNAL );
// And verify that all nodes are in the index
verifyIndex( tx );
base += batchSize;
} );
for ( ; !end.get(); i++ )
{
queue.queue( createNodeAndProperty( i ) );
}
queue.empty();
}
catch ( Exception e )
{
throw new RuntimeException( e );
}
}
private TransactionToApply createNodeAndProperty( int progress ) throws Exception
{
TransactionState txState = new TxState();
long nodeId = nodeIds.nextId();
txState.nodeDoCreate( nodeId );
txState.nodeDoAddLabel( labelId, nodeId );
txState.nodeDoReplaceProperty( nodeId,
noNodeProperty( nodeId, propertyKeyId ),
property( propertyKeyId, propertyValue( id, progress ) ) );
Collection<StorageCommand> commands = new ArrayList<>();
try ( StorageStatement statement = storageEngine.storeReadLayer().newStatement() )
{
storageEngine.createCommands( commands, txState, statement, null, 0 );
}
return tx( commands );
}
private void verifyIndex( TransactionToApply tx ) throws Exception
{
try ( IndexReader reader = index.newReader() )
{
NodeVisitor visitor = new NodeVisitor();
for ( int i = 0; tx != null; i++ )
{
tx.transactionRepresentation().accept( visitor.clear() );
Object propertyValue = propertyValue( id, base + i );
PrimitiveLongIterator hits = reader.seek( propertyValue );
assertEquals( "Index doesn't contain " + visitor.nodeId + " " + propertyValue,
visitor.nodeId, PrimitiveLongCollections.single( hits, -1 ) );
tx = tx.next();
}
}
}
}
private static class NodeVisitor implements Visitor<StorageCommand,IOException>
{
long nodeId;
@Override
public boolean visit( StorageCommand element ) throws IOException
{
if ( element instanceof NodeCommand )
{
nodeId = ((NodeCommand)element).getKey();
}
return false;
}
public NodeVisitor clear()
{
nodeId = -1;
return this;
}
}
}