-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SegmentedRaftLogRotationTest.java
100 lines (87 loc) · 3.71 KB
/
SegmentedRaftLogRotationTest.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
/*
* 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.segmented;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;
import org.neo4j.coreedge.raft.ReplicatedInteger;
import org.neo4j.coreedge.raft.ReplicatedString;
import org.neo4j.coreedge.raft.log.DummyRaftableContentSerializer;
import org.neo4j.coreedge.raft.log.RaftLogEntry;
import org.neo4j.kernel.lifecycle.Lifespan;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.rule.Resources;
import static org.junit.Assert.assertEquals;
import static org.neo4j.coreedge.server.CoreEdgeClusterSettings.raft_log_pruning_strategy;
import static org.neo4j.test.rule.Resources.InitialLifecycle.STARTED;
import static org.neo4j.test.rule.Resources.TestPath.EXISTING_DIRECTORY;
public class SegmentedRaftLogRotationTest
{
private final static int ROTATE_AT_SIZE_IN_BYTES = 100;
@Rule
public final Resources resourceManager = new Resources( EXISTING_DIRECTORY );
private SegmentedRaftLog createRaftLog( long rotateAtSize ) throws IOException
{
return new SegmentedRaftLog( resourceManager.fileSystem(), resourceManager.testPath(), rotateAtSize,
new DummyRaftableContentSerializer(), NullLogProvider.getInstance(),
raft_log_pruning_strategy.getDefaultValue() );
}
@Test
@Resources.Life( STARTED )
public void shouldRotateOnAppendWhenRotateSizeIsReached() throws Exception
{
// When
SegmentedRaftLog log = resourceManager.managed( createRaftLog( ROTATE_AT_SIZE_IN_BYTES ) );
log.append( new RaftLogEntry( 0, replicatedStringOfBytes( ROTATE_AT_SIZE_IN_BYTES ) ) );
// Then
File[] files = resourceManager.fileSystem().listFiles( resourceManager.testPath() );
assertEquals( 2, files.length );
}
@Test
@Resources.Life( STARTED )
public void shouldBeAbleToRecoverToLatestStateAfterRotation() throws Throwable
{
// Given
int term = 0;
long indexToRestoreTo;
try ( Lifespan lifespan = new Lifespan() )
{
SegmentedRaftLog log = lifespan.add( createRaftLog( ROTATE_AT_SIZE_IN_BYTES ) );
log.append( new RaftLogEntry( term, replicatedStringOfBytes( ROTATE_AT_SIZE_IN_BYTES - 40 ) ) );
indexToRestoreTo = log.append( new RaftLogEntry( term, ReplicatedInteger.valueOf( 1 ) ) );
}
// When
SegmentedRaftLog log = resourceManager.managed( createRaftLog( ROTATE_AT_SIZE_IN_BYTES ) );
// Then
assertEquals( indexToRestoreTo, log.appendIndex() );
assertEquals( term, log.readEntryTerm( indexToRestoreTo ) );
}
private ReplicatedString replicatedStringOfBytes( int size )
{
StringBuilder builder = new StringBuilder();
for ( int i = 0; i < size; i++ )
{
builder.append( "i" );
}
return new ReplicatedString( builder.toString() );
}
}