-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DurableStateStorageTest.java
216 lines (182 loc) · 7.37 KB
/
DurableStateStorageTest.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
/*
* 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.state;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.internal.DatabaseHealth;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.storageengine.api.ReadableChannel;
import org.neo4j.storageengine.api.WritableChannel;
import org.neo4j.test.rule.TargetDirectory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
public class DurableStateStorageTest
{
@Rule
public TargetDirectory.TestDirectory testDir = TargetDirectory.testDirForTest( getClass() );
@Test
public void shouldMaintainStateGivenAnEmptyInitialStore() throws Exception
{
// given
EphemeralFileSystemAbstraction fsa = new EphemeralFileSystemAbstraction();
fsa.mkdir( testDir.directory() );
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>( fsa, testDir.directory(),
"state", new AtomicIntegerMarshal(), 100, health(), NullLogProvider.getInstance() );
// when
storage.persistStoreData( new AtomicInteger( 99 ) );
// then
assertEquals( 4, fsa.getFileSize( stateFileA() ) );
}
@Test
public void shouldRotateToOtherStoreFileAfterSufficientEntries() throws Exception
{
// given
EphemeralFileSystemAbstraction fsa = new EphemeralFileSystemAbstraction();
fsa.mkdir( testDir.directory() );
final int numberOfEntriesBeforeRotation = 100;
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>( fsa, testDir.directory(),
"state", new AtomicIntegerMarshal(), numberOfEntriesBeforeRotation,
health(), NullLogProvider.getInstance() );
// when
for ( int i = 0; i < numberOfEntriesBeforeRotation; i++ )
{
storage.persistStoreData( new AtomicInteger( i ) );
}
// Force the rotation
storage.persistStoreData( new AtomicInteger( 9999 ) );
// then
assertEquals( 4, fsa.getFileSize( stateFileB() ) );
assertEquals( numberOfEntriesBeforeRotation * 4, fsa.getFileSize( stateFileA() ) );
}
@Test
public void shouldRotateBackToFirstStoreFileAfterSufficientEntries() throws Exception
{
// given
EphemeralFileSystemAbstraction fsa = new EphemeralFileSystemAbstraction();
fsa.mkdir( testDir.directory() );
final int numberOfEntriesBeforeRotation = 100;
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>( fsa, testDir.directory(),
"state", new AtomicIntegerMarshal(), numberOfEntriesBeforeRotation,
health(), NullLogProvider.getInstance() );
// when
for ( int i = 0; i < numberOfEntriesBeforeRotation * 2; i++ )
{
storage.persistStoreData( new AtomicInteger( i ) );
}
// Force the rotation back to the first store
storage.persistStoreData( new AtomicInteger( 9999 ) );
// then
assertEquals( 4, fsa.getFileSize( stateFileA() ) );
assertEquals( numberOfEntriesBeforeRotation * 4, fsa.getFileSize( stateFileB() ) );
}
@Test
public void shouldClearFileOnFirstUse() throws Throwable
{
// given
EphemeralFileSystemAbstraction fsa = new EphemeralFileSystemAbstraction();
fsa.mkdir( testDir.directory() );
int rotationCount = 10;
DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>( fsa, testDir.directory(),
"state", new AtomicIntegerMarshal(), rotationCount,
health(), NullLogProvider.getInstance() );
int largestValueWritten = 0;
for (; largestValueWritten < rotationCount * 2; largestValueWritten++ )
{
storage.persistStoreData( new AtomicInteger( largestValueWritten ) );
}
storage.shutdown();
// now both files are full. We reopen, then write some more.
storage = new DurableStateStorage<>( fsa, testDir.directory(),
"state", new AtomicIntegerMarshal(), rotationCount,
health(), NullLogProvider.getInstance() );
storage.persistStoreData( new AtomicInteger( largestValueWritten++ ) );
storage.persistStoreData( new AtomicInteger( largestValueWritten++ ) );
storage.persistStoreData( new AtomicInteger( largestValueWritten ) );
/*
* We have written stuff in fileA but not gotten to the end (resulting in rotation). The largestValueWritten
* should nevertheless be correct
*/
storage.shutdown();
ByteBuffer forReadingBackIn = ByteBuffer.allocate( 10_000 );
StoreChannel lastWrittenTo = fsa.open( stateFileA(), "r" );
lastWrittenTo.read( forReadingBackIn );
forReadingBackIn.flip();
AtomicInteger lastRead = null;
while ( true )
{
try
{
lastRead = new AtomicInteger( forReadingBackIn.getInt() );
}
catch ( BufferUnderflowException e )
{
break;
}
}
// then
assertNotNull( lastRead );
assertEquals( largestValueWritten, lastRead.get() );
}
private static class AtomicIntegerMarshal extends SafeStateMarshal<AtomicInteger>
{
@Override
public void marshal( AtomicInteger state, WritableChannel channel ) throws IOException
{
channel.putInt( state.intValue() );
}
@Override
public AtomicInteger unmarshal0( ReadableChannel channel ) throws IOException
{
return new AtomicInteger( channel.getInt() );
}
@Override
public AtomicInteger startState()
{
return new AtomicInteger( 0 );
}
@Override
public long ordinal( AtomicInteger atomicInteger )
{
return atomicInteger.get();
}
}
private File stateFileA()
{
return new File( new File( testDir.directory(), "state-state" ), "state.a" );
}
private File stateFileB()
{
return new File( new File( testDir.directory(), "state-state" ), "state.b" );
}
@SuppressWarnings("unchecked")
private Supplier<DatabaseHealth> health()
{
return mock( Supplier.class );
}
}