-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ManyPropertyKeysIT.java
227 lines (199 loc) · 7.74 KB
/
ManyPropertyKeysIT.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
/*
* 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.kernel.impl.core;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.File;
import java.util.Collection;
import org.neo4j.dbms.database.DatabaseManager;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier;
import org.neo4j.kernel.api.InwardKernel;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.PropertyKeyTokenStore;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.OtherThreadExecutor;
import org.neo4j.test.OtherThreadExecutor.WorkerCommand;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static org.junit.Assert.assertEquals;
/**
* Tests for handling many property keys (even after restart of database)
* as well as concurrent creation of property keys.
*/
public class ManyPropertyKeysIT
{
private final PageCacheRule pageCacheRule = new PageCacheRule();
private final TestDirectory testDirectory = TestDirectory.testDirectory();
private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
@Rule
public final RuleChain ruleChain = RuleChain.outerRule( testDirectory )
.around( fileSystemRule ).around( pageCacheRule );
private File databaseDir;
@Before
public void setup()
{
databaseDir = testDirectory.databaseDir();
}
@Test
public void creating_many_property_keys_should_have_all_loaded_the_next_restart() throws Exception
{
// GIVEN
// The previous limit to load was 2500, so go some above that
GraphDatabaseAPI db = databaseWithManyPropertyKeys( 3000 );
int countBefore = propertyKeyCount( db );
// WHEN
db.shutdown();
db = database();
createNodeWithProperty( db, key( 2800 ), true );
// THEN
assertEquals( countBefore, propertyKeyCount( db ) );
db.shutdown();
}
@Test
public void concurrently_creating_same_property_key_in_different_transactions_should_end_up_with_same_key_id()
throws Exception
{
// GIVEN
GraphDatabaseAPI db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newImpermanentDatabase();
OtherThreadExecutor<WorkerState> worker1 = new OtherThreadExecutor<>( "w1", new WorkerState( db ) );
OtherThreadExecutor<WorkerState> worker2 = new OtherThreadExecutor<>( "w2", new WorkerState( db ) );
worker1.execute( new BeginTx() );
worker2.execute( new BeginTx() );
// WHEN
String key = "mykey";
worker1.execute( new CreateNodeAndSetProperty( key ) );
worker2.execute( new CreateNodeAndSetProperty( key ) );
worker1.execute( new FinishTx() );
worker2.execute( new FinishTx() );
worker1.close();
worker2.close();
// THEN
assertEquals( 1, propertyKeyCount( db ) );
db.shutdown();
}
private GraphDatabaseAPI database()
{
return (GraphDatabaseAPI) new TestGraphDatabaseFactory().newEmbeddedDatabase( testDirectory.storeDir() );
}
private GraphDatabaseAPI databaseWithManyPropertyKeys( int propertyKeyCount )
{
PageCache pageCache = pageCacheRule.getPageCache( fileSystemRule.get() );
StoreFactory storeFactory =
new StoreFactory( DatabaseManager.DEFAULT_DATABASE_NAME, databaseDir, Config.defaults(), new DefaultIdGeneratorFactory( fileSystemRule.get() ),
pageCache, fileSystemRule.get(), NullLogProvider.getInstance(), EmptyVersionContextSupplier.EMPTY );
NeoStores neoStores = storeFactory.openAllNeoStores( true );
PropertyKeyTokenStore store = neoStores.getPropertyKeyTokenStore();
for ( int i = 0; i < propertyKeyCount; i++ )
{
PropertyKeyTokenRecord record = new PropertyKeyTokenRecord( (int) store.nextId() );
record.setInUse( true );
Collection<DynamicRecord> nameRecords = store.allocateNameRecords( PropertyStore.encodeString( key( i ) ) );
record.addNameRecords( nameRecords );
record.setNameId( (int) Iterables.first( nameRecords ).getId() );
store.updateRecord( record );
}
neoStores.close();
return database();
}
private String key( int i )
{
return "key" + i;
}
private Node createNodeWithProperty( GraphDatabaseService db, String key, Object value )
{
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode();
node.setProperty( key, value );
tx.success();
return node;
}
}
private int propertyKeyCount( GraphDatabaseAPI db ) throws TransactionFailureException
{
InwardKernel kernelAPI = db.getDependencyResolver().resolveDependency( InwardKernel.class );
try ( KernelTransaction tx = kernelAPI.beginTransaction( KernelTransaction.Type.implicit, AnonymousContext.read() ) )
{
return tx.tokenRead().propertyKeyCount();
}
}
private static class WorkerState
{
protected final GraphDatabaseService db;
protected Transaction tx;
WorkerState( GraphDatabaseService db )
{
this.db = db;
}
}
private static class BeginTx implements WorkerCommand<WorkerState, Void>
{
@Override
public Void doWork( WorkerState state )
{
state.tx = state.db.beginTx();
return null;
}
}
private static class CreateNodeAndSetProperty implements WorkerCommand<WorkerState, Void>
{
private final String key;
CreateNodeAndSetProperty( String key )
{
this.key = key;
}
@Override
public Void doWork( WorkerState state )
{
Node node = state.db.createNode();
node.setProperty( key, true );
return null;
}
}
private static class FinishTx implements WorkerCommand<WorkerState, Void>
{
@Override
public Void doWork( WorkerState state )
{
state.tx.success();
state.tx.close();
return null;
}
}
}