-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BatchingNeoStores.java
293 lines (269 loc) · 12.8 KB
/
BatchingNeoStores.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
/*
* 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.unsafe.impl.batchimport.store;
import java.io.File;
import java.io.IOException;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Service;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.KernelExtensionFactory;
import org.neo4j.kernel.extension.KernelExtensions;
import org.neo4j.kernel.extension.UnsatisfiedDependencyStrategies;
import org.neo4j.kernel.extension.dependency.HighestSelectionStrategy;
import org.neo4j.kernel.impl.api.index.IndexStoreView;
import org.neo4j.kernel.impl.api.scan.LabelScanStoreProvider;
import org.neo4j.kernel.impl.factory.DatabaseInfo;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.pagecache.ConfiguringPageCacheFactory;
import org.neo4j.kernel.impl.spi.KernelContext;
import org.neo4j.kernel.impl.spi.SimpleKernelContext;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.RecordStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.impl.store.counts.CountsTracker;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.util.Dependencies;
import org.neo4j.kernel.impl.util.OsBeanUtil;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.unsafe.impl.batchimport.AdditionalInitialIds;
import org.neo4j.unsafe.impl.batchimport.Configuration;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
import org.neo4j.unsafe.impl.batchimport.store.BatchingTokenRepository.BatchingLabelTokenRepository;
import org.neo4j.unsafe.impl.batchimport.store.BatchingTokenRepository.BatchingPropertyKeyTokenRepository;
import org.neo4j.unsafe.impl.batchimport.store.BatchingTokenRepository.BatchingRelationshipTypeTokenRepository;
import org.neo4j.unsafe.impl.batchimport.store.io.IoTracer;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.String.valueOf;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.dense_node_threshold;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.mapped_memory_page_size;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.pagecache_memory;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
/**
* Creator and accessor of {@link NeoStores} with some logic to provide very batch friendly services to the
* {@link NeoStores} when instantiating it. Different services for specific purposes.
*/
public class BatchingNeoStores implements AutoCloseable
{
private final FileSystemAbstraction fileSystem;
private final BatchingPropertyKeyTokenRepository propertyKeyRepository;
private final BatchingLabelTokenRepository labelRepository;
private final BatchingRelationshipTypeTokenRepository relationshipTypeRepository;
private final LogProvider logProvider;
private final File storeDir;
private final Config neo4jConfig;
private final PageCache pageCache;
private final NeoStores neoStores;
private final LifeSupport life = new LifeSupport();
private final LabelScanStore labelScanStore;
private final IoTracer ioTracer;
public BatchingNeoStores( FileSystemAbstraction fileSystem, File storeDir, Configuration config,
LogService logService, AdditionalInitialIds initialIds, Config dbConfig )
{
this.fileSystem = fileSystem;
this.logProvider = logService.getInternalLogProvider();
this.storeDir = storeDir;
long pageSize = config.pageSize();
// 30 is the minimum number of pages the page cache wants to keep free at all times.
// Having less than that might result in an evicted page will reading, which would mean
// unnecessary re-reading. Having slightly more leaves some leg room.
long optimalMappedMemorySize = pageSize * 40;
long limitedMemorySize = max(
2 * pageSize, // page cache requires at the very least memory enough for two pages
applyEnvironmentLimitationsTo( optimalMappedMemorySize ) );
this.neo4jConfig = new Config( stringMap( dbConfig.getParams(),
dense_node_threshold.name(), valueOf( config.denseNodeThreshold() ),
pagecache_memory.name(), valueOf( limitedMemorySize ),
mapped_memory_page_size.name(), valueOf( pageSize ) ),
GraphDatabaseSettings.class );
final PageCacheTracer tracer = new DefaultPageCacheTracer();
this.pageCache = createPageCache( fileSystem, neo4jConfig, logProvider, tracer );
this.ioTracer = tracer::bytesWritten;
this.neoStores = newNeoStores( pageCache );
if ( alreadyContainsData( neoStores ) )
{
neoStores.close();
throw new IllegalStateException( storeDir + " already contains data, cannot do import here" );
}
try
{
neoStores.rebuildCountStoreIfNeeded();
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
neoStores.getMetaDataStore().setLastCommittedAndClosedTransactionId(
initialIds.lastCommittedTransactionId(), initialIds.lastCommittedTransactionChecksum(),
initialIds.lastCommittedTransactionLogVersion(), initialIds.lastCommittedTransactionLogByteOffset() );
this.propertyKeyRepository = new BatchingPropertyKeyTokenRepository(
neoStores.getPropertyKeyTokenStore(), initialIds.highPropertyKeyTokenId() );
this.labelRepository = new BatchingLabelTokenRepository(
neoStores.getLabelTokenStore(), initialIds.highLabelTokenId() );
this.relationshipTypeRepository = new BatchingRelationshipTypeTokenRepository(
neoStores.getRelationshipTypeTokenStore(), initialIds.highRelationshipTypeTokenId() );
// Initialize kernel extensions
Dependencies dependencies = new Dependencies();
dependencies.satisfyDependency( neo4jConfig );
dependencies.satisfyDependency( fileSystem );
dependencies.satisfyDependency( this );
dependencies.satisfyDependency( logService );
dependencies.satisfyDependency( IndexStoreView.EMPTY );
KernelContext kernelContext = new SimpleKernelContext( fileSystem, storeDir, DatabaseInfo.UNKNOWN,
dependencies );
@SuppressWarnings( { "unchecked", "rawtypes" } )
KernelExtensions extensions = life.add( new KernelExtensions(
kernelContext, (Iterable) Service.load( KernelExtensionFactory.class ),
dependencies, UnsatisfiedDependencyStrategies.ignore() ) );
life.start();
labelScanStore = life.add( extensions.resolveDependency( LabelScanStoreProvider.class,
HighestSelectionStrategy.getInstance() ).getLabelScanStore() );
}
/**
* An attempt to limit amount of memory used by the page cache in a severely limited environment.
* This shouldn't be a problem in most scenarios since the optimal mapped memory size is in the range
* of 100-200 MiB and so shouldn't impose a noticeable dent in memory usage.
*
* @param optimalMappedMemorySize amount of mapped memory that would be considered optimal for the import.
* @return in most cases the optimal size, although in some very limited environments a smaller size.
*/
private long applyEnvironmentLimitationsTo( long optimalMappedMemorySize )
{
long freePhysicalMemory = OsBeanUtil.getFreePhysicalMemory();
if ( freePhysicalMemory == OsBeanUtil.VALUE_UNAVAILABLE )
{
// We have no idea how much free memory there is, let's simply go with what we'd like to have
return optimalMappedMemorySize;
}
// We got a hint about amount of free memory. Let's acquire tops a fifth of the free memory
// since other parts of the importer also needs memory to function.
return min( optimalMappedMemorySize, freePhysicalMemory / 5 );
}
private static PageCache createPageCache( FileSystemAbstraction fileSystem, Config config, LogProvider log,
PageCacheTracer tracer )
{
return new ConfiguringPageCacheFactory( fileSystem, config, tracer,
log.getLog( BatchingNeoStores.class ) ).getOrCreatePageCache();
}
private boolean alreadyContainsData( NeoStores neoStores )
{
return neoStores.getNodeStore().getHighId() > 0 || neoStores.getRelationshipStore().getHighId() > 0;
}
/**
* A way to create the underlying {@link NeoStores} files in the {@link FileSystemAbstraction file system}
* before instantiating the real one. This allows some store contents to be populated before an import.
* Useful for store migration where the {@link ParallelBatchImporter} is used as migrator and some of
* its data need to be communicated by copying a store file.
*/
public static void createStore( FileSystemAbstraction fileSystem, String storeDir, Config dbConfig )
throws IOException
{
try ( PageCache pageCache = createPageCache( fileSystem, dbConfig, NullLogProvider.getInstance(),
PageCacheTracer.NULL ) )
{
StoreFactory storeFactory =
new StoreFactory( fileSystem, new File( storeDir ), pageCache, NullLogProvider.getInstance() );
try ( NeoStores neoStores = storeFactory.openAllNeoStores( true ) )
{
neoStores.getMetaDataStore();
neoStores.getLabelTokenStore();
neoStores.getNodeStore();
neoStores.getPropertyStore();
neoStores.getRelationshipGroupStore();
neoStores.getRelationshipStore();
neoStores.getSchemaStore();
}
}
}
private NeoStores newNeoStores( PageCache pageCache )
{
BatchingIdGeneratorFactory idGeneratorFactory = new BatchingIdGeneratorFactory( fileSystem );
StoreFactory storeFactory =
new StoreFactory( storeDir, neo4jConfig, idGeneratorFactory, pageCache, fileSystem, logProvider );
return storeFactory.openAllNeoStores( true );
}
public IoTracer getIoTracer()
{
return ioTracer;
}
public NodeStore getNodeStore()
{
return neoStores.getNodeStore();
}
public PropertyStore getPropertyStore()
{
return neoStores.getPropertyStore();
}
public BatchingPropertyKeyTokenRepository getPropertyKeyRepository()
{
return propertyKeyRepository;
}
public BatchingLabelTokenRepository getLabelRepository()
{
return labelRepository;
}
public BatchingRelationshipTypeTokenRepository getRelationshipTypeRepository()
{
return relationshipTypeRepository;
}
public RelationshipStore getRelationshipStore()
{
return neoStores.getRelationshipStore();
}
public RecordStore<RelationshipGroupRecord> getRelationshipGroupStore()
{
return neoStores.getRelationshipGroupStore();
}
public CountsTracker getCountsStore()
{
return neoStores.getCounts();
}
@Override
public void close() throws IOException
{
// Flush out all pending changes
propertyKeyRepository.close();
labelRepository.close();
relationshipTypeRepository.close();
// Close the neo store
life.shutdown();
neoStores.close();
pageCache.close();
}
public long getLastCommittedTransactionId()
{
return neoStores.getMetaDataStore().getLastCommittedTransactionId();
}
public LabelScanStore getLabelScanStore()
{
return labelScanStore;
}
}