/
DefaultMasterImplSPI.java
191 lines (173 loc) · 7.45 KB
/
DefaultMasterImplSPI.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
/*
* Copyright (c) 2002-2015 "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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.ha.cluster;
import java.io.File;
import java.io.IOException;
import org.neo4j.com.RequestContext;
import org.neo4j.com.Response;
import org.neo4j.com.storecopy.ResponsePacker;
import org.neo4j.com.storecopy.StoreCopyServer;
import org.neo4j.com.storecopy.StoreWriter;
import org.neo4j.function.Supplier;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.IdGeneratorFactory;
import org.neo4j.kernel.IdType;
import org.neo4j.kernel.NeoStoreDataSource;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.ha.TransactionChecksumLookup;
import org.neo4j.kernel.ha.com.master.MasterImpl;
import org.neo4j.kernel.ha.id.IdAllocation;
import org.neo4j.kernel.impl.api.TransactionApplicationMode;
import org.neo4j.kernel.impl.api.TransactionCommitProcess;
import org.neo4j.kernel.impl.core.LabelTokenHolder;
import org.neo4j.kernel.impl.core.PropertyKeyTokenHolder;
import org.neo4j.kernel.impl.core.RelationshipTypeTokenHolder;
import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.store.StoreId;
import org.neo4j.kernel.impl.store.id.IdGenerator;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.monitoring.Monitors;
public class DefaultMasterImplSPI implements MasterImpl.SPI
{
private static final int ID_GRAB_SIZE = 1000;
private final GraphDatabaseAPI graphDb;
private final TransactionChecksumLookup txChecksumLookup;
private final FileSystemAbstraction fileSystem;
private final LabelTokenHolder labels;
private final PropertyKeyTokenHolder propertyKeyTokenHolder;
private final RelationshipTypeTokenHolder relationshipTypeTokenHolder;
private final IdGeneratorFactory idGeneratorFactory;
private final NeoStoreDataSource neoStoreDataSource;
private final File storeDir;
private final ResponsePacker responsePacker;
private final Monitors monitors;
private final TransactionCommitProcess transactionCommitProcess;
private final CheckPointer checkPointer;
public DefaultMasterImplSPI( final GraphDatabaseAPI graphDb,
FileSystemAbstraction fileSystemAbstraction,
Monitors monitors,
LabelTokenHolder labels, PropertyKeyTokenHolder propertyKeyTokenHolder,
RelationshipTypeTokenHolder relationshipTypeTokenHolder,
IdGeneratorFactory idGeneratorFactory,
TransactionCommitProcess transactionCommitProcess,
CheckPointer checkPointer,
TransactionIdStore transactionIdStore,
LogicalTransactionStore logicalTransactionStore,
NeoStoreDataSource neoStoreDataSource)
{
this.graphDb = graphDb;
// Hmm, fetching the dependencies here instead of handing them in the constructor directly feels bad,
// but it seems like there's some intricate usage and need for the db's dependency resolver.
this.fileSystem = fileSystemAbstraction;
this.labels = labels;
this.propertyKeyTokenHolder = propertyKeyTokenHolder;
this.relationshipTypeTokenHolder = relationshipTypeTokenHolder;
this.idGeneratorFactory = idGeneratorFactory;
this.transactionCommitProcess = transactionCommitProcess;
this.checkPointer = checkPointer;
this.neoStoreDataSource = neoStoreDataSource;
this.storeDir = new File( graphDb.getStoreDir() );
this.txChecksumLookup = new TransactionChecksumLookup( transactionIdStore, logicalTransactionStore );
this.responsePacker = new ResponsePacker( logicalTransactionStore, transactionIdStore, new Supplier<StoreId>()
{
@Override
public StoreId get()
{
return graphDb.storeId();
}
} );
this.monitors = monitors;
}
@Override
public boolean isAccessible()
{
// Wait for 5s for the database to become available, if not already so
return graphDb.isAvailable( 5000 );
}
@Override
public int getOrCreateLabel( String name )
{
return labels.getOrCreateId( name );
}
@Override
public int getOrCreateProperty( String name )
{
return propertyKeyTokenHolder.getOrCreateId( name );
}
@Override
public IdAllocation allocateIds( IdType idType )
{
IdGenerator generator = idGeneratorFactory.get( idType );
return new IdAllocation( generator.nextIdBatch( ID_GRAB_SIZE ), generator.getHighId(),
generator.getDefragCount() );
}
@Override
public StoreId storeId()
{
return graphDb.storeId();
}
@Override
public long applyPreparedTransaction( TransactionRepresentation preparedTransaction ) throws IOException,
TransactionFailureException
{
try ( LockGroup locks = new LockGroup() )
{
return transactionCommitProcess.commit( preparedTransaction, locks, CommitEvent.NULL,
TransactionApplicationMode.EXTERNAL );
}
}
@Override
public Integer createRelationshipType( String name )
{
return relationshipTypeTokenHolder.getOrCreateId( name );
}
@Override
public long getTransactionChecksum( long txId ) throws IOException
{
return txChecksumLookup.applyAsLong( txId );
}
@Override
public RequestContext flushStoresAndStreamStoreFiles( StoreWriter writer )
{
StoreCopyServer streamer = new StoreCopyServer( neoStoreDataSource,
checkPointer, fileSystem, storeDir, monitors.newMonitor( StoreCopyServer.Monitor.class ) );
return streamer.flushStoresAndStreamStoreFiles( writer, false );
}
@Override
public <T> Response<T> packTransactionStreamResponse( RequestContext context, T response )
{
return responsePacker.packTransactionStreamResponse( context, response );
}
@Override
public <T> Response<T> packTransactionObligationResponse( RequestContext context, T response )
{
return responsePacker.packTransactionObligationResponse( context, response );
}
@Override
public <T> Response<T> packEmptyResponse( T response )
{
return responsePacker.packEmptyResponse( response );
}
}