-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransactionToApply.java
215 lines (191 loc) · 7.63 KB
/
TransactionToApply.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
/*
* Copyright (c) 2002-2018 "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.api;
import java.io.IOException;
import java.util.function.LongConsumer;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContext;
import org.neo4j.io.pagecache.tracing.cursor.context.VersionContext;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.Commitment;
import org.neo4j.kernel.impl.transaction.log.LogPosition;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.impl.util.HexPrinter;
import org.neo4j.storageengine.api.CommandsToApply;
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import static org.neo4j.helpers.Format.date;
/**
* A chain of transactions to apply. Transactions form a linked list, each pointing to the {@link #next()}
* or {@code null}. This design chosen for less garbage and convenience, i.e. that we pass in a number of transactions
* while also expecting some results for each and every one of those transactions back. The results are
* written directly into each instance instead of creating another data structure which is then returned.
* This is an internal class so even if it mixes arguments with results it's easier to work with,
* requires less code... and less objects.
*
* State and methods are divided up into two parts, one part being the responsibility of the user to manage,
* the other part up to the commit process to manage.
*
* The access pattern looks like:
* <ol>
* <li>=== USER ===</li>
* <li>Construct instances</li>
* <li>Form the linked list using {@link #next(TransactionToApply)}</li>
* <li>Pass into {@link TransactionCommitProcess#commit(TransactionToApply, CommitEvent, TransactionApplicationMode)}</li>
* <li>=== COMMIT PROCESS ===</li>
* <li>Commit, where {@link #commitment(Commitment, long)} is called to store the {@link Commitment} and transaction id</li>
* <li>Apply, where {@link #commitment()},
* {@link #transactionRepresentation()} and {@link #next()} are called</li>
* <li>=== USER ===</li>
* <li>Data about the commit can now also be accessed using f.ex {@link #commitment()} or {@link #transactionId()}</li>
* </ol>
*/
public class TransactionToApply implements CommandsToApply, AutoCloseable
{
public static final long TRANSACTION_ID_NOT_SPECIFIED = 0;
// These fields are provided by user
private final TransactionRepresentation transactionRepresentation;
private long transactionId;
private final VersionContext versionContext;
private TransactionToApply nextTransactionInBatch;
// These fields are provided by commit process, storage engine, or recovery process
private Commitment commitment;
private LongConsumer closedCallback;
private LogPosition logPosition;
/**
* Used when committing a transaction that hasn't already gotten a transaction id assigned.
*/
public TransactionToApply( TransactionRepresentation transactionRepresentation )
{
this( transactionRepresentation, EmptyVersionContext.EMPTY );
}
/**
* Used when committing a transaction that hasn't already gotten a transaction id assigned.
*/
public TransactionToApply( TransactionRepresentation transactionRepresentation, VersionContext versionContext )
{
this( transactionRepresentation, TRANSACTION_ID_NOT_SPECIFIED, versionContext );
}
public TransactionToApply( TransactionRepresentation transactionRepresentation, long transactionId )
{
this( transactionRepresentation, transactionId, EmptyVersionContext.EMPTY );
}
public TransactionToApply( TransactionRepresentation transactionRepresentation, long transactionId,
VersionContext versionContext )
{
this.transactionRepresentation = transactionRepresentation;
this.transactionId = transactionId;
this.versionContext = versionContext;
}
// These methods are called by the user when building a batch
public void next( TransactionToApply next )
{
nextTransactionInBatch = next;
}
// These methods are called by the commit process
public Commitment commitment()
{
return commitment;
}
@Override
public long transactionId()
{
return transactionId;
}
@Override
public boolean accept( Visitor<StorageCommand,IOException> visitor ) throws IOException
{
return transactionRepresentation.accept( visitor );
}
public TransactionRepresentation transactionRepresentation()
{
return transactionRepresentation;
}
@Override
public boolean requiresApplicationOrdering()
{
return commitment.hasExplicitIndexChanges();
}
public void commitment( Commitment commitment, long transactionId )
{
this.commitment = commitment;
this.transactionId = transactionId;
this.versionContext.initWrite( transactionId );
}
public void logPosition( LogPosition position )
{
this.logPosition = position;
}
@Override
public TransactionToApply next()
{
return nextTransactionInBatch;
}
public void onClose( LongConsumer closedCallback )
{
this.closedCallback = closedCallback;
}
@Override
public void close()
{
if ( closedCallback != null )
{
closedCallback.accept( transactionId );
}
}
@Override
public String toString()
{
TransactionRepresentation tr = this.transactionRepresentation;
return "Transaction #" + transactionId +
(logPosition != null ? " at log position " + logPosition : " (no log position)") +
" {started " + date( tr.getTimeStarted() ) +
", committed " + date( tr.getTimeCommitted() ) +
", with " + countCommands() + " commands in this transaction" +
", authored by " + tr.getAuthorId() +
", with master id " + tr.getMasterId() +
", lock session " + tr.getLockSessionId() +
", latest committed transaction id when started was " + tr.getLatestCommittedTxWhenStarted() +
", additional header bytes: " + HexPrinter.hex( tr.additionalHeader(), Integer.MAX_VALUE, "" ) + "}";
}
private String countCommands()
{
class Counter implements Visitor<StorageCommand,IOException>
{
private int count;
@Override
public boolean visit( StorageCommand element ) throws IOException
{
count++;
return false;
}
}
try
{
Counter counter = new Counter();
accept( counter );
return String.valueOf( counter.count );
}
catch ( Throwable e )
{
return "(unable to count: " + e.getMessage() + ")";
}
}
}