-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OnlineBackupContextFactoryTest.java
352 lines (299 loc) · 14.6 KB
/
OnlineBackupContextFactoryTest.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* 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.backup.impl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.ByteUnit;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.test.rule.SuppressOutput;
import org.neo4j.test.rule.TestDirectory;
import static java.nio.file.StandardOpenOption.WRITE;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
import static org.neo4j.backup.impl.SelectedBackupProtocol.ANY;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.logical_logs_location;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.pagecache_memory;
public class OnlineBackupContextFactoryTest
{
@Rule
public TestDirectory testDirectory = TestDirectory.testDirectory();
@Rule
public ExpectedException expected = ExpectedException.none();
@Rule
public SuppressOutput suppress = SuppressOutput.suppressAll();
private Path homeDir;
private Path configDir;
private Path configFile;
@Before
public void setUp() throws IOException
{
homeDir = testDirectory.directory( "home" ).toPath();
configDir = testDirectory.directory( "config" ).toPath();
configFile = configDir.resolve( "neo4j.conf" );
String neo4jConfContents = "dbms.backup.address = localhost:1234";
Files.write( configFile, singletonList( neo4jConfContents ) );
}
@Test
public void unspecifiedHostnameIsEmptyOptional() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--from=:1234" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertFalse( requiredArguments.getAddress().getHostname().isPresent() );
assertEquals( 1234, requiredArguments.getAddress().getPort().get().intValue() );
}
@Test
public void unspecifiedPortIsEmptyOptional() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--from=abc" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( "abc", requiredArguments.getAddress().getHostname().get() );
assertFalse( requiredArguments.getAddress().getPort().isPresent() );
}
@Test
public void acceptHostWithTrailingPort() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--from=foo.bar.server:" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( "foo.bar.server", requiredArguments.getAddress().getHostname().get() );
assertFalse( requiredArguments.getAddress().getPort().isPresent() );
}
@Test
public void acceptPortWithPrecedingEmptyHost() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--from=:1234" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertFalse( requiredArguments.getAddress().getHostname().isPresent() );
assertEquals( 1234, requiredArguments.getAddress().getPort().get().intValue() );
}
@Test
public void acceptBothIfSpecified() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--from=foo.bar.server:1234" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( "foo.bar.server", requiredArguments.getAddress().getHostname().get() );
assertEquals( 1234, requiredArguments.getAddress().getPort().get().intValue() );
}
@Test
public void backupDirectoryArgumentIsMandatory() throws Exception
{
expected.expect( IncorrectUsage.class );
expected.expectMessage( "Missing argument 'backup-dir'" );
new OnlineBackupContextFactory( homeDir, configDir ).createContext();
}
@Test
public void shouldDefaultTimeoutToTwentyMinutes() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( "--backup-dir=/", "--name=mybackup" );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( MINUTES.toMillis( 20 ), requiredArguments.getTimeout() );
}
@Test
public void shouldInterpretAUnitlessTimeoutAsSeconds() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( "--timeout=10", "--backup-dir=/", "--name=mybackup" );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( SECONDS.toMillis( 10 ), requiredArguments.getTimeout() );
}
@Test
public void shouldParseATimeoutWithUnits() throws Exception
{
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--timeout=10h" ) );
OnlineBackupRequiredArguments requiredArguments = context.getRequiredArguments();
assertEquals( HOURS.toMillis( 10 ), requiredArguments.getTimeout() );
}
@Test
public void shouldTreatNameArgumentAsMandatory() throws Exception
{
expected.expect( IncorrectUsage.class );
expected.expectMessage( "Missing argument 'name'" );
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
handler.createContext( "--backup-dir=/" );
}
@Test
public void reportDirMustBeAPath() throws Exception
{
expected.expect( IncorrectUsage.class );
expected.expectMessage( "cc-report-dir must be a path" );
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
handler.createContext( requiredAnd( "--check-consistency", "--cc-report-dir" ) );
}
@Test
public void errorHandledForNonExistingAdditionalConfigFile() throws Exception
{
// given
Path additionalConf = homeDir.resolve( "neo4j.conf" );
// and
expected.expect( CommandFailed.class );
expected.expectCause( hasCause( any( NoSuchFileException.class ) ) );
// expect
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
handler.createContext( requiredAnd( "--additional-config=" + additionalConf ) );
}
@Test
public void prioritiseConfigDirOverHomeDir() throws Exception
{
// given
Files.write( configFile, singletonList( "causal_clustering.minimum_core_cluster_size_at_startup=4" ), WRITE );
// and
Path homeDirConfigFile = homeDir.resolve( "neo4j.conf" );
Files.write( homeDirConfigFile, asList( "causal_clustering.minimum_core_cluster_size_at_startup=5", "causal_clustering.raft_in_queue_max_batch=21" ) );
// when
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
Config config = handler.createContext( requiredAnd() ).getConfig();
// then
assertEquals( Integer.valueOf( 3 ), config.get( CausalClusteringSettings.minimum_core_cluster_size_at_formation ) );
assertEquals( Integer.valueOf( 64 ), config.get( CausalClusteringSettings.raft_in_queue_max_batch ) );
}
@Test
public void prioritiseAdditionalOverConfigDir() throws Exception
{
// given
Files.write( configFile, asList( "causal_clustering.minimum_core_cluster_size_at_startup=4", "causal_clustering.raft_in_queue_max_batch=21" ) );
// and
Path additionalConf = homeDir.resolve( "additional-neo4j.conf" );
Files.write( additionalConf, singletonList( "causal_clustering.minimum_core_cluster_size_at_startup=5" ) );
// when
OnlineBackupContextFactory handler = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = handler.createContext( requiredAnd( "--additional-config=" + additionalConf ) );
Config config = context.getConfig();
// then
assertEquals( Integer.valueOf( 3 ), config.get( CausalClusteringSettings.minimum_core_cluster_size_at_formation ) );
assertEquals( Integer.valueOf( 21 ), config.get( CausalClusteringSettings.raft_in_queue_max_batch ) );
}
@Test
public void mustIgnorePageCacheConfigInConfigFile() throws Exception
{
// given
Files.write( configFile, singletonList( pagecache_memory.name() + "=42m" ) );
// when
OnlineBackupContextFactory contextBuilder = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = contextBuilder.createContext( requiredAnd() );
// then
assertThat( context.getConfig().get( pagecache_memory ), is( "8m" ) );
}
@Test
public void mustIgnorePageCacheConfigInAdditionalConfigFile() throws Exception
{
// given
Path additionalConf = homeDir.resolve( "additional-neo4j.conf" );
Files.write( additionalConf, singletonList( pagecache_memory.name() + "=42m" ) );
// when
OnlineBackupContextFactory builder = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = builder.createContext( requiredAnd( "--additional-config=" + additionalConf ) );
// then
assertThat( context.getConfig().get( pagecache_memory ), is( "8m" ) );
}
@Test
public void mustRespectPageCacheConfigFromCommandLineArguments() throws Exception
{
// when
OnlineBackupContextFactory builder = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = builder.createContext( requiredAnd( "--pagecache=42m" ) );
// then
assertThat( context.getConfig().get( pagecache_memory ), is( "42m" ) );
}
@Test
public void logsMustBePlacedInTargetBackupDirectory() throws Exception
{
// when
String name = "mybackup";
Path backupDir = homeDir.resolve( "poke" );
Path backupPath = backupDir.resolve( name );
Files.createDirectories( backupDir );
OnlineBackupContextFactory builder = new OnlineBackupContextFactory( homeDir, configDir );
OnlineBackupContext context = builder.createContext( "--backup-dir=" + backupDir, "--name=" + name );
assertThat( context.getConfig().get( logical_logs_location ).getAbsolutePath(), is( backupPath.toString() ) );
}
@Test
public void defaultProtocolIsAny() throws CommandFailed, IncorrectUsage
{
// given
OnlineBackupContextFactory builder = new OnlineBackupContextFactory( homeDir, configDir );
// when context resolved without proto override value
OnlineBackupContext context = builder.createContext( requiredAnd() );
// then
assertEquals( ANY, context.getRequiredArguments().getSelectedBackupProtocol() );
}
@Test
public void overrideWithLegacy() throws CommandFailed, IncorrectUsage
{
// with
List<String> input = Arrays.asList( "common", "catchup" );
List<SelectedBackupProtocol> expected = Arrays.asList( SelectedBackupProtocol.COMMON, SelectedBackupProtocol.CATCHUP );
for ( int useCase = 0; useCase < input.size(); useCase++ )
{
// given
OnlineBackupContextFactory builder = new OnlineBackupContextFactory( homeDir, configDir );
// when
OnlineBackupContext context = builder.createContext( requiredAnd( "--protocol=" + input.get( useCase ) ) );
// then
assertEquals( expected.get( useCase ), context.getRequiredArguments().getSelectedBackupProtocol() );
}
}
@Test
public void prometheusShouldBeDisabledToAvoidPortConflicts() throws CommandFailed, IncorrectUsage
{
OnlineBackupContext context = new OnlineBackupContextFactory( homeDir, configDir ).createContext( requiredAnd() );
assertEquals( Settings.FALSE, context.getConfig().getRaw().get( "metrics.prometheus.enabled" ) );
}
private String[] requiredAnd( String... additionalArgs )
{
List<String> args = new ArrayList<>();
args.add( "--backup-dir=/" );
args.add( "--name=mybackup" );
Collections.addAll( args, additionalArgs );
return args.toArray( new String[0] );
}
}