-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PhysicalRaftLogContractTest.java
234 lines (201 loc) · 8.74 KB
/
PhysicalRaftLogContractTest.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2002-2016 "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.coreedge.raft.log;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.neo4j.kernel.impl.transaction.log.LogVersionBridge.NO_MORE_CHANNELS;
import static org.neo4j.kernel.impl.transaction.log.entry.LogHeader.LOG_HEADER_SIZE;
import static org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Stack;
import org.junit.After;
import org.junit.Test;
import org.neo4j.coreedge.raft.ReplicatedInteger;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.transaction.log.PhysicalLogFile;
import org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel;
import org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel;
import org.neo4j.kernel.impl.transaction.log.ReadableLogChannel;
import org.neo4j.kernel.impl.transaction.log.entry.LogVersions;
import org.neo4j.kernel.internal.DatabaseHealth;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.logging.NullLogProvider;
public class PhysicalRaftLogContractTest extends RaftLogContractTest
{
private PhysicalRaftLog raftLog;
private LifeSupport life = new LifeSupport();
private FileSystemAbstraction fileSystem;
@Override
public RaftLog createRaftLog() throws IOException
{
this.raftLog = createRaftLog( 100 );
return raftLog;
}
@After
public void tearDown() throws Throwable
{
life.stop();
life.shutdown();
}
private PhysicalRaftLog createRaftLog( int cacheSize )
{
if ( fileSystem == null )
{
fileSystem = new EphemeralFileSystemAbstraction();
}
File directory = new File( "raft-log" );
fileSystem.mkdir( directory );
PhysicalRaftLog newRaftLog = new PhysicalRaftLog( fileSystem, directory, 1000000, cacheSize, 10, 10,
new PhysicalLogFile.Monitor.Adapter(), new DummyRaftableContentSerializer(), () -> mock( DatabaseHealth.class ),
NullLogProvider.getInstance() );
life.add( newRaftLog );
life.init();
life.start();
return newRaftLog;
}
@Test
public void shouldReadBackInCachedEntry() throws Throwable
{
// Given
PhysicalRaftLog raftLog = (PhysicalRaftLog) createRaftLog();
int term = 0;
ReplicatedInteger content = ReplicatedInteger.valueOf( 4 );
// When
long entryIndex = raftLog.append( new RaftLogEntry( term, content ) );
// Then
assertEquals( entryIndex, raftLog.appendIndex() );
assertEquals( content, raftLog.readLogEntry( entryIndex ).content() );
assertEquals( term, raftLog.readEntryTerm( entryIndex ) );
}
@Test
public void shouldReadBackNonCachedEntry() throws Exception
{
// Given
int cacheSize = 1;
PhysicalRaftLog raftLog = createRaftLog( cacheSize );
int term = 0;
ReplicatedInteger content1 = ReplicatedInteger.valueOf( 4 );
ReplicatedInteger content2 = ReplicatedInteger.valueOf( 5 );
// When
long entryIndex1 = raftLog.append( new RaftLogEntry( term, content1 ) );
long entryIndex2 = raftLog.append( new RaftLogEntry( term, content2 ) ); // this will push the first entry out of cache
// Then
// entry 1 should be there
assertEquals( content1, raftLog.readLogEntry( entryIndex1 ).content() );
assertEquals( term, raftLog.readEntryTerm( entryIndex1 ) );
// entry 2 should be there also
assertEquals( content2, raftLog.readLogEntry( entryIndex2 ).content() );
assertEquals( term, raftLog.readEntryTerm( entryIndex2 ) );
}
@Test
public void shouldRestoreCommitIndexOnStartup() throws Throwable
{
// Given
PhysicalRaftLog raftLog = createRaftLog( 100 /* cache size */ );
int term = 0;
ReplicatedInteger content1 = ReplicatedInteger.valueOf( 4 );
ReplicatedInteger content2 = ReplicatedInteger.valueOf( 5 );
long entryIndex1 = raftLog.append( new RaftLogEntry( term, content1 ) );
long entryIndex2 = raftLog.append( new RaftLogEntry( term, content2 ) );
raftLog.commit( entryIndex1 );
// When
// we restart the raft log
life.remove( raftLog ); // stops the removed instance
raftLog = createRaftLog( 100 );
// Then
assertEquals( entryIndex1, raftLog.commitIndex() );
assertEquals( entryIndex2, raftLog.appendIndex() );
}
@Test
public void shouldRestoreCorrectCommitAndAppendIndexOnStartupAfterTruncation() throws Exception
{
// Given
PhysicalRaftLog raftLog = createRaftLog( 100 /* cache size */ );
int term = 0;
ReplicatedInteger content = ReplicatedInteger.valueOf( 4 );
raftLog.append( new RaftLogEntry( term, content ) );
raftLog.append( new RaftLogEntry( term, content ) );
long entryIndex3 = raftLog.append( new RaftLogEntry( term, content ) );
long entryIndex4 = raftLog.append( new RaftLogEntry( term, content ) );
raftLog.commit( entryIndex3 );
raftLog.truncate( entryIndex4 );
// When
// we restart the raft log
life.remove( raftLog ); // stops the removed instance
raftLog = createRaftLog( 100 );
// Then
assertEquals( entryIndex3, raftLog.commitIndex() );
assertEquals( entryIndex3, raftLog.appendIndex() );
}
@Test
public void shouldRestoreCorrectCommitAndAppendIndexWithTruncationRecordsAndAppendedRecordsAfterThat() throws Exception
{
// Given
PhysicalRaftLog raftLog = createRaftLog( 100 /* cache size */ );
int term = 0;
ReplicatedInteger content = ReplicatedInteger.valueOf( 4 );
raftLog.append( new RaftLogEntry( term, content ) );
raftLog.append( new RaftLogEntry( term, content ) );
long entryIndex3 = raftLog.append( new RaftLogEntry( term, content ) );
long entryIndex4 = raftLog.append( new RaftLogEntry( term, content ) );
raftLog.commit( entryIndex3 );
raftLog.truncate( entryIndex4 );
long entryIndex5 = raftLog.append( new RaftLogEntry( term, content ) );
// When
// we restart the raft log
life.remove( raftLog ); // stops the removed instance
raftLog = createRaftLog( 100 );
// Then
assertEquals( entryIndex3, raftLog.commitIndex() );
assertEquals( entryIndex5, raftLog.appendIndex() );
}
@Test
public void shouldReturnNullOnEndOfFile() throws Exception
{
PhysicalRaftLog raftLog = createRaftLog( 1 /* cache size */ );
raftLog.append(new RaftLogEntry( 0, ReplicatedInteger.valueOf( 0 ) ));
life.remove( raftLog );
StoreChannel logStoreChannel = fileSystem.open( new File( "raft-log/raft.log.0" ), "r" );
readLogHeader( ByteBuffer.allocateDirect( LOG_HEADER_SIZE ), logStoreChannel, false );
PhysicalLogVersionedStoreChannel channel = new PhysicalLogVersionedStoreChannel(
logStoreChannel, 0, LogVersions.CURRENT_LOG_VERSION );
ReadableLogChannel logChannel = new ReadAheadLogChannel( channel, NO_MORE_CHANNELS );
try ( PhysicalRaftLogEntryCursor cursor = new PhysicalRaftLogEntryCursor( new RaftRecordCursor<>( logChannel, new DummyRaftableContentSerializer() ),
new Stack<>(), 0 ) )
{
boolean firstRecordEncountered = false;
while( cursor.next() )
{
RaftLogAppendRecord record = cursor.get();
if ( record.getLogIndex() == 0L )
{
assertFalse( firstRecordEncountered );
}
firstRecordEncountered = true;
}
}
}
}