-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ReplicatedTransactionFactory.java
167 lines (144 loc) · 6.57 KB
/
ReplicatedTransactionFactory.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
/*
* 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 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.causalclustering.core.state.machines.tx;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.neo4j.causalclustering.messaging.MessageTooBigException;
import org.neo4j.causalclustering.messaging.NetworkFlushableChannelNetty4;
import org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageCommandReaderFactory;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel;
import org.neo4j.kernel.impl.transaction.log.entry.InvalidLogEntryHandler;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader;
import org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter;
import org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader;
import org.neo4j.storageengine.api.StorageCommand;
import static org.neo4j.io.ByteUnit.gibiBytes;
public class ReplicatedTransactionFactory
{
private static final long MAX_SERIALIZED_TX_SIZE = gibiBytes( 1 );
private ReplicatedTransactionFactory()
{
throw new AssertionError( "Should not be instantiated" );
}
public static ReplicatedTransaction createImmutableReplicatedTransaction( TransactionRepresentation tx )
{
ByteBuf transactionBuffer = Unpooled.buffer();
NetworkFlushableChannelNetty4 channel = new NetworkFlushableChannelNetty4( transactionBuffer, MAX_SERIALIZED_TX_SIZE );
try
{
TransactionSerializer.write( tx, channel );
}
catch ( MessageTooBigException e )
{
throw new IllegalStateException( "Transaction size was too large to replicate across the cluster.", e );
}
catch ( IOException e )
{
// TODO: This should not happen. All operations are in memory, no IOException should be thrown
// Easier said than done though, we use the LogEntry handling routines which throw IOException
throw new RuntimeException( e );
}
/*
* This trims down the array to send up to the actual index it was written. Not doing this would send additional
* zeroes which not only wasteful, but also not handled by the LogEntryReader receiving this.
*/
byte[] txBytes = Arrays.copyOf( transactionBuffer.array(), transactionBuffer.writerIndex() );
transactionBuffer.release();
return new ReplicatedTransaction( txBytes );
}
public static TransactionRepresentation extractTransactionRepresentation( ReplicatedTransaction transactionCommand, byte[] extraHeader )
{
ByteBuf txBuffer = Unpooled.wrappedBuffer( transactionCommand.getTxBytes() );
NetworkReadableClosableChannelNetty4 channel = new NetworkReadableClosableChannelNetty4( txBuffer );
try
{
return read( channel, extraHeader );
}
catch ( IOException e )
{
// TODO: This should not happen. All operations are in memory, no IOException should be thrown
// Easier said than done though, we use the LogEntry handling routines which throw IOException
throw new RuntimeException( e );
}
}
private static class TransactionSerializer
{
public static void write( TransactionRepresentation tx, NetworkFlushableChannelNetty4 channel ) throws
IOException
{
channel.putInt( tx.getAuthorId() );
channel.putInt( tx.getMasterId() );
channel.putLong( tx.getLatestCommittedTxWhenStarted() );
channel.putLong( tx.getTimeStarted() );
channel.putLong( tx.getTimeCommitted() );
channel.putInt( tx.getLockSessionId() );
byte[] additionalHeader = tx.additionalHeader();
if ( additionalHeader != null )
{
channel.putInt( additionalHeader.length );
channel.put( additionalHeader, additionalHeader.length );
}
else
{
channel.putInt( 0 );
}
new LogEntryWriter( channel ).serialize( tx );
}
}
public static TransactionRepresentation read( NetworkReadableClosableChannelNetty4 channel, byte[] extraHeader ) throws IOException
{
LogEntryReader<ReadableClosablePositionAwareChannel> reader = new VersionAwareLogEntryReader<>(
new RecordStorageCommandReaderFactory(), InvalidLogEntryHandler.STRICT );
int authorId = channel.getInt();
int masterId = channel.getInt();
long latestCommittedTxWhenStarted = channel.getLong();
long timeStarted = channel.getLong();
long timeCommitted = channel.getLong();
int lockSessionId = channel.getInt();
int headerLength = channel.getInt();
byte[] header;
if ( headerLength == 0 )
{
header = extraHeader;
}
else
{
header = new byte[headerLength];
}
channel.get( header, headerLength );
LogEntryCommand entryRead;
List<StorageCommand> commands = new LinkedList<>();
while ( (entryRead = (LogEntryCommand) reader.readLogEntry( channel )) != null )
{
commands.add( entryRead.getCommand() );
}
PhysicalTransactionRepresentation tx = new PhysicalTransactionRepresentation( commands );
tx.setHeader( header, masterId, authorId, timeStarted, latestCommittedTxWhenStarted, timeCommitted, lockSessionId );
return tx;
}
}