-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OnlineBackupCommandBuilder.java
355 lines (315 loc) · 11.2 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
/*
* 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.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.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.commandline.admin.ParameterisedOutsideWorld;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import static java.lang.String.format;
/**
* 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 SelectedBackupProtocol selectedBackupProtocol;
private Boolean consistencyCheckGraph;
private Boolean consistencyCheckIndexes;
private Boolean consistencyCheckLabel;
private Boolean consistencyCheckOwners;
private OutputStream output;
private Optional<String[]> rawArgs = Optional.empty();
public OnlineBackupCommandBuilder withRawArgs( String... args )
{
rawArgs = Optional.of( args );
return this;
}
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;
}
public OnlineBackupCommandBuilder withSelectedBackupStrategy( SelectedBackupProtocol selectedBackupStrategy )
{
this.selectedBackupProtocol = selectedBackupStrategy;
return this;
}
public boolean backup( File neo4jHome, String backupName ) throws CommandFailed, IncorrectUsage
{
File targetLocation = new File( neo4jHome, backupName );
String[] args;
if ( rawArgs.isPresent() )
{
args = rawArgs.get();
}
else
{
try
{
args = resolveArgs( targetLocation );
}
catch ( IOException e )
{
throw new CommandFailed( "Failed to resolve arguments", e );
}
}
new OnlineBackupCommandProvider()
.create( neo4jHome.toPath(),
configDirFromTarget( neo4jHome.toPath() ),
resolveOutsideWorld() )
.execute( args );
return true;
}
public String[] resolveArgs( File targetLocation ) throws IOException
{
return args(
argBackupName( targetLocation ),
argBackupLocation( targetLocation ),
argFrom(),
argFallbackToFull(),
argSelectedProtocol(),
argTimeout(),
argCheckConsistency(),
argReportDir(),
argAdditionalConf( targetLocation ),
argCcGraph(),
argCcIndexes(),
argCcLabel(),
argCcOwners() );
}
private OutsideWorld resolveOutsideWorld()
{
Optional<OutputStream> output = Optional.ofNullable( this.output );
return new ParameterisedOutsideWorld(
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 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 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 format( "--name=%s", backupName );
}
private static Supplier<IllegalArgumentException> wrongArguments( String message )
{
return () -> new IllegalArgumentException( message );
}
private String argFallbackToFull()
{
return Optional.ofNullable( fallbackToFull )
.map( flag -> format( "--fallback-to-full=%s", flag ) )
.orElse( "" );
}
private String argSelectedProtocol()
{
return Optional.ofNullable( selectedBackupProtocol )
.map( SelectedBackupProtocol::getName )
.map( argValue -> format( "--%s=%s", OnlineBackupContextFactory.ARG_NAME_PROTO_OVERRIDE, argValue ) )
.orElse( "" );
}
private String argTimeout()
{
return Optional.ofNullable( this.timeout )
.map( value -> format("--timeout=%dms", value) )
.orElse( "" );
}
private String argCcOwners()
{
return Optional.ofNullable( this.consistencyCheckOwners )
.map( value -> format( "--check-consistency=%b", this.consistencyCheckOwners ) )
.orElse( "" );
}
private String argCcLabel()
{
return Optional.ofNullable( this.consistencyCheckLabel )
.map( value -> format( "--cc-label-scan-store=%b", this.consistencyCheckLabel ) )
.orElse( "" );
}
private String argCcIndexes()
{
return Optional.ofNullable( this.consistencyCheckIndexes )
.map( value -> format( "--cc-indexes=%b", this.consistencyCheckIndexes ) )
.orElse( "" );
}
private String argCcGraph()
{
return Optional.ofNullable( this.consistencyCheckGraph )
.map( value -> format( "--cc-graph=%b", this.consistencyCheckGraph ) )
.orElse( "" );
}
private String argAdditionalConf( File backupTarget ) throws IOException
{
if ( additionalConfig == null )
{
return "";
}
File configFile = backupTarget.toPath().resolve( "../additional_neo4j.conf" ).toFile();
writeConfigToFile( additionalConfig, configFile );
return format( "--additional-config=%s", configFile );
}
static 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( format( "%s=%s\n", entry.getKey(), entry.getValue() ) );
}
}
}
private String argReportDir()
{
return Optional.ofNullable( this.consistencyReportLocation )
.map( value -> format( "--cc-report-dir=%s", value ))
.orElse( "" );
}
private String argCheckConsistency()
{
return Optional.ofNullable( this.checkConsistency )
.map( value -> 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 Path configDirFromTarget( Path neo4jHome )
{
return neo4jHome.resolve( "conf" );
}
}