-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CountsMigrator.java
181 lines (168 loc) · 8.59 KB
/
CountsMigrator.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
/*
* Copyright (c) 2002-2017 "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.storemigration.participant;
import java.io.File;
import java.io.IOException;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.CountsComputer;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.MetaDataStore.Position;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.StoreFailureException;
import org.neo4j.kernel.impl.store.StoreFile;
import org.neo4j.kernel.impl.store.StoreType;
import org.neo4j.kernel.impl.store.counts.CountsTracker;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.kernel.impl.store.format.StoreVersion;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_3;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_0;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.ReadOnlyIdGeneratorFactory;
import org.neo4j.kernel.impl.storemigration.ExistingTargetStrategy;
import org.neo4j.kernel.impl.storemigration.StoreFileType;
import org.neo4j.kernel.impl.storemigration.StoreUpgrader;
import org.neo4j.kernel.impl.storemigration.monitoring.MigrationProgressMonitor;
import org.neo4j.kernel.lifecycle.Lifespan;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.unsafe.impl.batchimport.cache.NumberArrayFactory;
import static org.neo4j.kernel.impl.store.MetaDataStore.DEFAULT_NAME;
import static org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForVersion;
import static org.neo4j.kernel.impl.storemigration.FileOperation.DELETE;
import static org.neo4j.kernel.impl.storemigration.FileOperation.MOVE;
/**
* Rebuilds the count store during migration.
* <p>
* Since the database may or may not reside in the upgrade directory, depending on whether the new format has
* different capabilities or not, we rebuild the count store using the information the store directory if we fail to
* open the store in the upgrade directory.
* <p>
* Just one out of many potential participants in a {@link StoreUpgrader migration}.
*
* @see StoreUpgrader
*/
public class CountsMigrator extends AbstractStoreMigrationParticipant
{
private static final Iterable<StoreFile> COUNTS_STORE_FILES = Iterables
.iterable( StoreFile.COUNTS_STORE_LEFT, StoreFile.COUNTS_STORE_RIGHT );
private final Config config;
private final FileSystemAbstraction fileSystem;
private final PageCache pageCache;
public CountsMigrator( FileSystemAbstraction fileSystem, PageCache pageCache, Config config )
{
super( "Count rebuilding" );
this.fileSystem = fileSystem;
this.pageCache = pageCache;
this.config = config;
}
@Override
public void migrate( File storeDir, File migrationDir, MigrationProgressMonitor.Section progressMonitor,
String versionToMigrateFrom, String versionToMigrateTo ) throws IOException
{
if ( countStoreRebuildRequired( versionToMigrateFrom ) )
{
// create counters from scratch
StoreFile.fileOperation( DELETE, fileSystem, migrationDir, migrationDir, COUNTS_STORE_FILES, true, null,
StoreFileType.STORE );
File neoStore = new File( storeDir, DEFAULT_NAME );
long lastTxId = MetaDataStore.getRecord( pageCache, neoStore, Position.LAST_TRANSACTION_ID );
try
{
rebuildCountsFromScratch( migrationDir, migrationDir, lastTxId, progressMonitor, versionToMigrateTo,
pageCache, NullLogProvider.getInstance() );
}
catch ( StoreFailureException e )
{
//This means that we did not perform a full migration, as the formats had the same capabilities. Thus
// we should use the store directory for information when rebuilding the count store. Note that we
// still put the new count store in the migration directory.
rebuildCountsFromScratch( storeDir, migrationDir, lastTxId, progressMonitor, versionToMigrateFrom,
pageCache, NullLogProvider.getInstance() );
}
}
}
@Override
public void moveMigratedFiles( File migrationDir, File storeDir, String versionToUpgradeFrom,
String versionToUpgradeTo ) throws IOException
{
// Delete any current count files in the store directory.
StoreFile.fileOperation( DELETE, fileSystem, storeDir, null, COUNTS_STORE_FILES, true, null,
StoreFileType.values() );
// Move the migrated ones into the store directory
StoreFile.fileOperation( MOVE, fileSystem, migrationDir, storeDir, COUNTS_STORE_FILES, true,
// allow to skip non existent source files
ExistingTargetStrategy.OVERWRITE, // allow to overwrite target files
StoreFileType.values() );
// We do not need to move files with the page cache, as the count files always reside on the normal file system.
}
@Override
public void cleanup( File migrationDir ) throws IOException
{
fileSystem.deleteRecursively( migrationDir );
}
@Override
public String toString()
{
return "Kernel Node Count Rebuilder";
}
boolean countStoreRebuildRequired( String versionToMigrateFrom )
{
return StandardV2_3.STORE_VERSION.equals( versionToMigrateFrom ) ||
StandardV3_0.STORE_VERSION.equals( versionToMigrateFrom ) ||
StoreVersion.HIGH_LIMIT_V3_0_0.versionString().equals( versionToMigrateFrom ) ||
StoreVersion.HIGH_LIMIT_V3_0_6.versionString().equals( versionToMigrateFrom ) ||
StoreVersion.HIGH_LIMIT_V3_1_0.versionString().equals( versionToMigrateFrom );
}
private void rebuildCountsFromScratch( File storeDirToReadFrom, File migrationDir, long lastTxId,
MigrationProgressMonitor.Section progressMonitor, String expectedStoreVersion, PageCache pageCache,
LogProvider logProvider )
{
final File storeFileBase = new File( migrationDir,
MetaDataStore.DEFAULT_NAME + StoreFactory.COUNTS_STORE );
RecordFormats recordFormats = selectForVersion( expectedStoreVersion );
IdGeneratorFactory idGeneratorFactory = new ReadOnlyIdGeneratorFactory( fileSystem );
StoreFactory storeFactory = new StoreFactory( storeDirToReadFrom, config, idGeneratorFactory, pageCache,
fileSystem, recordFormats, logProvider );
try ( NeoStores neoStores = storeFactory
.openNeoStores( StoreType.NODE, StoreType.RELATIONSHIP, StoreType.LABEL_TOKEN,
StoreType.RELATIONSHIP_TYPE_TOKEN ) )
{
neoStores.verifyStoreOk();
NodeStore nodeStore = neoStores.getNodeStore();
RelationshipStore relationshipStore = neoStores.getRelationshipStore();
try ( Lifespan life = new Lifespan() )
{
int highLabelId = (int) neoStores.getLabelTokenStore().getHighId();
int highRelationshipTypeId = (int) neoStores.getRelationshipTypeTokenStore().getHighId();
CountsComputer initializer = new CountsComputer( lastTxId, nodeStore, relationshipStore, highLabelId,
highRelationshipTypeId, NumberArrayFactory.auto( pageCache, migrationDir, true ),
progressMonitor );
life.add( new CountsTracker( logProvider, fileSystem, pageCache, config,
storeFileBase ).setInitializer( initializer ) );
}
}
}
}