-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OnlineBackupCommandBuilder.java
359 lines (318 loc) · 11.6 KB
/
OnlineBackupCommandBuilder.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
353
354
355
356
357
358
359
/*
* Copyright (c) 2002-2017 "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.backup.impl;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
/**
* This class encapsulates the information needed to perform an online backup against a running Neo4j instance
* configured to act as a backup server.
* <p>
* All backup methods return the same instance, allowing for chaining calls.
*/
public class OnlineBackupCommandBuilder
{
private String host;
private Integer port;
private Boolean fallbackToFull;
private Long timeout;
private Boolean checkConsistency;
private File consistencyReportLocation;
private Config additionalConfig;
private Boolean consistencyCheckGraph;
private Boolean consistencyCheckIndexes;
private Boolean consistencyCheckLabel;
private Boolean consistencyCheckOwners;
private OutputStream output;
public OnlineBackupCommandBuilder withHost( String host )
{
this.host = host;
return this;
}
public OnlineBackupCommandBuilder withPort( Integer port )
{
this.port = port;
return this;
}
public OnlineBackupCommandBuilder withFallbackToFull( Boolean flag )
{
this.fallbackToFull = flag;
return this;
}
public OnlineBackupCommandBuilder withTimeout( Long timeoutInMillis )
{
this.timeout = timeoutInMillis;
return this;
}
public OnlineBackupCommandBuilder withConsistencyCheck( Boolean flag )
{
this.checkConsistency = flag;
return this;
}
public OnlineBackupCommandBuilder withReportFlag( File consistencyReportLocation )
{
this.consistencyReportLocation = consistencyReportLocation;
return this;
}
public OnlineBackupCommandBuilder withAdditionalConfig( Config additionalConfig )
{
this.additionalConfig = additionalConfig;
return this;
}
public OnlineBackupCommandBuilder withGraphConsistencyCheck( Boolean flag )
{
this.consistencyCheckGraph = flag;
return this;
}
public OnlineBackupCommandBuilder withIndexConsistencyCheck( Boolean flag )
{
this.consistencyCheckIndexes = flag;
return this;
}
public OnlineBackupCommandBuilder withLabelConsistencyCheck( Boolean flag )
{
this.consistencyCheckLabel = flag;
return this;
}
public OnlineBackupCommandBuilder withOwnerConsistencyCheck( Boolean flag )
{
this.consistencyCheckOwners = flag;
return this;
}
public OnlineBackupCommandBuilder withOutput( OutputStream outputStream )
{
this.output = outputStream;
return this;
}
/**
* Perform a backup with the parameters set using the builder
* @param targetLocation target location of backup (NOTE: uses the parent directory as well)
* @return true if backup was successful
*/
public boolean backup( String targetLocation ) throws CommandFailed, IncorrectUsage
{
return backup( relativeFileFromString( targetLocation ) );
}
/**
* Perform a backup with the parameters set using the builder
* @param targetLocation target location of backup (NOTE: uses the parent directory as well)
* @return true if backup was successful
*/
public boolean backup( File targetLocation ) throws CommandFailed, IncorrectUsage
{
String[] args;
try
{
args = resolveArgs( targetLocation );
}
catch ( IOException e )
{
throw new CommandFailed( "Failed to resolve arguments", e );
}
new OnlineBackupCommandProvider()
.create( neo4jHomeFromTarget( targetLocation ),
configDirFromTarget( targetLocation ),
resolveOutsideWorld() )
.execute( args );
return true;
}
private File relativeFileFromString( String targetLocation )
{
return new File( "." ).toPath().resolve( targetLocation ).toFile();
}
/**
* This is a helper method when debugging tests where in some cases the file is a relative string
* @param targetLocation relative backup location as a string
* @return the arguments that would be passed to the backup command
*/
public String[] resolveArgs( String targetLocation ) throws IOException
{
return resolveArgs( relativeFileFromString( targetLocation ) );
}
public String[] resolveArgs( File targetLocation ) throws IOException
{
return args(
argBackupName( targetLocation ),
argBackupLocation( targetLocation ),
argFrom(),
argFallbackToFull(),
argTimeout(),
argCheckConsistency(),
argReportDir(),
argAdditionalConf( targetLocation ),
argCcGraph(),
argCcIndexes(),
argCcLabel(),
argCcOwners() );
}
private OutsideWorld resolveOutsideWorld()
{
Optional<OutputStream> output = Optional.ofNullable( this.output );
return new ParametrisedOutsideWorld(
System.console(), output.orElse( System.out ),
output.orElse( System.err ),
System.in, new DefaultFileSystemAbstraction() );
}
/**
* Client handles the ports and hosts automatically, so no necessary need to specify
* @return command line parameter for specifying the backup address
*/
private String argFrom()
{
if ( host == null && port == null )
{
return "";
}
String address = String.join( ":",
Optional.ofNullable( host ).orElse( "" ),
Optional.ofNullable( port ).map( port -> Integer.toString( port ) ).orElse( "" ) );
return String.format( "--from=%s", address );
}
/**
* The backup location is the directory that stores multiple backups. Each directory in "backup location" is named after the backup name.
* In order for a backup to belong where the user wants it, the backup location is the parent of the target specified by the user.
* @return backup location command line argument
*/
private String argBackupLocation( File targetLocation )
{
String location = Optional.ofNullable( targetLocation )
.map( f -> targetLocation.getParentFile() )
.orElseThrow( wrongArguments( "No target location specified" ) )
.toString();
return String.format( "--backup-dir=%s", location );
}
private String argBackupName( File targetLocation )
{
String backupName = Optional.ofNullable( targetLocation )
.map( File::getName )
.orElseThrow( wrongArguments( "No target location specified" ) );
return String.format( "--name=%s", backupName );
}
private static Supplier<IllegalArgumentException> wrongArguments( String message )
{
return () -> new IllegalArgumentException( message );
}
private String argFallbackToFull()
{
return Optional.ofNullable( fallbackToFull )
.map( flag -> String.format( "--fallback-to-full=%s", flag ) )
.orElse( "" );
}
private String argTimeout()
{
return Optional.ofNullable( this.timeout )
.map( value -> String.format("--timeout=%dms", value) )
.orElse( "" );
}
private String argCcOwners()
{
return Optional.ofNullable( this.consistencyCheckOwners )
.map( value -> String.format( "--check-consistency=%b", this.consistencyCheckOwners ) )
.orElse( "" );
}
private String argCcLabel()
{
return Optional.ofNullable( this.consistencyCheckLabel )
.map( value -> String.format( "--cc-label-scan-store=%b", this.consistencyCheckLabel ) )
.orElse( "" );
}
private String argCcIndexes()
{
return Optional.ofNullable( this.consistencyCheckIndexes )
.map( value -> String.format( "--cc-indexes=%b", this.consistencyCheckIndexes ) )
.orElse( "" );
}
private String argCcGraph()
{
return Optional.ofNullable( this.consistencyCheckGraph )
.map( value -> String.format( "--cc-graph=%b", this.consistencyCheckGraph ) )
.orElse( "" );
}
private String argAdditionalConf( File backupTarget ) throws IOException
{
if ( additionalConfig == null )
{
return "";
}
File configFile = neo4jHomeFromTarget( backupTarget ).resolve( "../additional_neo4j.conf" ).toFile();
writeConfigToFile( additionalConfig, configFile );
return String.format( "--additional-config=%s", configFile );
}
private void writeConfigToFile( Config config, File file ) throws IOException
{
try ( Writer fileWriter = new BufferedWriter( new FileWriter( file ) ) )
{
for ( Map.Entry<String,String> entry : config.getRaw().entrySet() )
{
fileWriter.write( String.format( "%s=%s\n", entry.getKey(), entry.getValue() ) );
}
}
}
private String argReportDir()
{
return Optional.ofNullable( this.consistencyReportLocation )
.map( value -> String.format( "--cc-report-dir=%s", value ))
.orElse( "" );
}
private String argCheckConsistency()
{
return Optional.ofNullable( this.checkConsistency )
.map( value -> String.format( "--check-consistency=%s", value ) )
.orElse( "" );
}
/**
* Removes empty args and is a convenience method
* @param args nullable, can be empty
* @return cleaned command line parameters
*/
private static String[] args( String... args )
{
return Stream.of( args )
.filter( StringUtils::isNoneEmpty )
.toArray( String[]::new );
}
private static <E> Predicate<E> not( Predicate<E> predicate )
{
return item -> !predicate.test( item );
}
private static Path neo4jHomeFromTarget( File target )
{
return target.toPath();
}
private static Path configDirFromTarget( File target )
{
return neo4jHomeFromTarget( target ).resolve( "config" );
}
}