-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RaftMessageDecoder.java
180 lines (155 loc) · 7.32 KB
/
RaftMessageDecoder.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.causalclustering.messaging.marshalling.v1;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.io.IOException;
import java.time.Clock;
import java.util.List;
import org.neo4j.causalclustering.core.consensus.RaftMessages;
import org.neo4j.causalclustering.core.consensus.log.RaftLogEntry;
import org.neo4j.causalclustering.core.replication.ReplicatedContent;
import org.neo4j.causalclustering.identity.ClusterId;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.causalclustering.messaging.EndOfStreamException;
import org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4;
import org.neo4j.causalclustering.messaging.marshalling.ChannelMarshal;
import org.neo4j.storageengine.api.ReadableChannel;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.APPEND_ENTRIES_REQUEST;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.APPEND_ENTRIES_RESPONSE;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.HEARTBEAT;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.HEARTBEAT_RESPONSE;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.LOG_COMPACTION_INFO;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.NEW_ENTRY_REQUEST;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.PRE_VOTE_REQUEST;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.PRE_VOTE_RESPONSE;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.VOTE_REQUEST;
import static org.neo4j.causalclustering.core.consensus.RaftMessages.Type.VOTE_RESPONSE;
public class RaftMessageDecoder extends ByteToMessageDecoder
{
private final ChannelMarshal<ReplicatedContent> marshal;
private final Clock clock;
public RaftMessageDecoder( ChannelMarshal<ReplicatedContent> marshal, Clock clock )
{
this.marshal = marshal;
this.clock = clock;
}
@Override
public void decode( ChannelHandlerContext ctx, ByteBuf buffer, List<Object> list ) throws Exception
{
ReadableChannel channel = new NetworkReadableClosableChannelNetty4( buffer );
ClusterId clusterId = ClusterId.Marshal.INSTANCE.unmarshal( channel );
int messageTypeWire = channel.getInt();
RaftMessages.Type[] values = RaftMessages.Type.values();
RaftMessages.Type messageType = values[messageTypeWire];
MemberId from = retrieveMember( channel );
RaftMessages.RaftMessage result;
if ( messageType.equals( VOTE_REQUEST ) )
{
MemberId candidate = retrieveMember( channel );
long term = channel.getLong();
long lastLogIndex = channel.getLong();
long lastLogTerm = channel.getLong();
result = new RaftMessages.Vote.Request( from, term, candidate, lastLogIndex, lastLogTerm );
}
else if ( messageType.equals( VOTE_RESPONSE ) )
{
long term = channel.getLong();
boolean voteGranted = channel.get() == 1;
result = new RaftMessages.Vote.Response( from, term, voteGranted );
}
else if ( messageType.equals( PRE_VOTE_REQUEST ) )
{
MemberId candidate = retrieveMember( channel );
long term = channel.getLong();
long lastLogIndex = channel.getLong();
long lastLogTerm = channel.getLong();
result = new RaftMessages.PreVote.Request( from, term, candidate, lastLogIndex, lastLogTerm );
}
else if ( messageType.equals( PRE_VOTE_RESPONSE ) )
{
long term = channel.getLong();
boolean voteGranted = channel.get() == 1;
result = new RaftMessages.PreVote.Response( from, term, voteGranted );
}
else if ( messageType.equals( APPEND_ENTRIES_REQUEST ) )
{
// how many
long term = channel.getLong();
long prevLogIndex = channel.getLong();
long prevLogTerm = channel.getLong();
long leaderCommit = channel.getLong();
long count = channel.getLong();
RaftLogEntry[] entries = new RaftLogEntry[(int) count];
for ( int i = 0; i < count; i++ )
{
long entryTerm = channel.getLong();
final ReplicatedContent content = marshal.unmarshal( channel );
entries[i] = new RaftLogEntry( entryTerm, content );
}
result = new RaftMessages.AppendEntries.Request( from, term, prevLogIndex, prevLogTerm, entries,
leaderCommit );
}
else if ( messageType.equals( APPEND_ENTRIES_RESPONSE ) )
{
long term = channel.getLong();
boolean success = channel.get() == 1;
long matchIndex = channel.getLong();
long appendIndex = channel.getLong();
result = new RaftMessages.AppendEntries.Response( from, term, success, matchIndex, appendIndex );
}
else if ( messageType.equals( NEW_ENTRY_REQUEST ) )
{
ReplicatedContent content = marshal.unmarshal( channel );
result = new RaftMessages.NewEntry.Request( from, content );
}
else if ( messageType.equals( HEARTBEAT ) )
{
long leaderTerm = channel.getLong();
long commitIndexTerm = channel.getLong();
long commitIndex = channel.getLong();
result = new RaftMessages.Heartbeat( from, leaderTerm, commitIndex, commitIndexTerm );
}
else if ( messageType.equals( HEARTBEAT_RESPONSE ) )
{
result = new RaftMessages.HeartbeatResponse( from );
}
else if ( messageType.equals( LOG_COMPACTION_INFO ) )
{
long leaderTerm = channel.getLong();
long prevIndex = channel.getLong();
result = new RaftMessages.LogCompactionInfo( from, leaderTerm, prevIndex );
}
else
{
throw new IllegalArgumentException( "Unknown message type" );
}
list.add( RaftMessages.ReceivedInstantClusterIdAwareMessage.of( clock.instant(), clusterId, result ) );
}
private MemberId retrieveMember( ReadableChannel buffer ) throws IOException, EndOfStreamException
{
MemberId.Marshal memberIdMarshal = new MemberId.Marshal();
return memberIdMarshal.unmarshal( buffer );
}
}