forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbCopy.java
More file actions
160 lines (140 loc) · 5.38 KB
/
DbCopy.java
File metadata and controls
160 lines (140 loc) · 5.38 KB
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
package org.tron.plugins;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import me.tongfei.progressbar.ProgressBar;
import org.tron.plugins.utils.DBUtils;
import org.tron.plugins.utils.FileUtils;
import picocli.CommandLine;
@Slf4j(topic = "copy")
@CommandLine.Command(name = "cp", aliases = "copy",
description = "Quick copy leveldb or rocksdb data.",
exitCodeListHeading = "Exit Codes:%n",
exitCodeList = {
"0:Successful",
"n:Internal error: exception occurred,please check toolkit.log"})
public class DbCopy implements Callable<Integer> {
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;
@CommandLine.Parameters(index = "0", defaultValue = "output-directory/database",
description = "Input path. Default: ${DEFAULT-VALUE}")
private File src;
@CommandLine.Parameters(index = "1", defaultValue = "output-directory-cp/database",
description = "Output path. Default: ${DEFAULT-VALUE}")
private File dest;
@CommandLine.Option(names = {"-h", "--help"})
private boolean help;
@Override
public Integer call() throws Exception {
if (help) {
spec.commandLine().usage(System.out);
return 0;
}
if (dest.exists()) {
logger.info(" {} exist, please delete it first.", dest);
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText(String.format("%s exist, please delete it first.", dest)));
return 402;
}
if (!src.exists()) {
logger.info(" {} does not exist.", src);
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText(String.format("%s does not exist.", src)));
return 404;
}
if (!src.isDirectory()) {
logger.info(" {} is not a directory.", src);
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText(String.format("%s is not a directory.", src)));
return 403;
}
List<File> files = Arrays.stream(Objects.requireNonNull(src.listFiles()))
.filter(File::isDirectory)
.filter(e -> !DBUtils.CHECKPOINT_DB_V2.equals(e.getName()))
.collect(Collectors.toList());
// add checkpoint v2 convert
File cpV2Dir = new File(Paths.get(src.toString(), DBUtils.CHECKPOINT_DB_V2).toString());
List<File> cpList = new ArrayList<>();
if (cpV2Dir.exists()) {
cpList = Arrays.stream(Objects.requireNonNull(cpV2Dir.listFiles()))
.filter(File::isDirectory)
.collect(Collectors.toList());
}
if (files.isEmpty()) {
logger.info("{} does not contain any database.", src);
spec.commandLine().getOut().format("%s does not contain any database.", src).println();
return 0;
}
final long time = System.currentTimeMillis();
List<Copier> services = new ArrayList<>();
files.forEach(f -> services.add(
new DbCopier(src.getPath(), dest.getPath(), f.getName())));
cpList.forEach(f -> services.add(
new DbCopier(
Paths.get(src.getPath(), DBUtils.CHECKPOINT_DB_V2).toString(),
Paths.get(dest.getPath(), DBUtils.CHECKPOINT_DB_V2).toString(),
f.getName())));
List<String> fails = ProgressBar.wrap(services.stream(), "copy task").parallel().map(
dbCopier -> {
try {
return dbCopier.doCopy() ? null : dbCopier.name();
} catch (Exception e) {
logger.error("{}", e);
spec.commandLine().getErr().println(spec.commandLine().getColorScheme()
.errorText(e.getMessage()));
return dbCopier.name();
}
}).filter(Objects::nonNull).collect(Collectors.toList());
// copy info.properties if lite need
Arrays.stream(Objects.requireNonNull(src.listFiles()))
.filter(File::isFile).forEach(f -> FileUtils.copy(Paths.get(src.toString(), f.getName()),
Paths.get(dest.toString(), f.getName())));
long during = (System.currentTimeMillis() - time) / 1000;
spec.commandLine().getOut().format("copy db done, fails: %s, take %d s.",
fails, during).println();
logger.info("database copy use {} seconds total, fails: {}.", during, fails);
return fails.size();
}
interface Copier {
boolean doCopy();
String name();
}
static class DbCopier implements Copier {
private final String srcDir;
private final String dstDir;
private final String dbName;
private final Path srcDbPath;
private final Path dstDbPath;
public DbCopier(String srcDir, String dstDir, String name) {
this.srcDir = srcDir;
this.dstDir = dstDir;
this.dbName = name;
this.srcDbPath = Paths.get(this.srcDir, name);
this.dstDbPath = Paths.get(this.dstDir, name);
}
@Override
public boolean doCopy() {
File srcDb = srcDbPath.toFile();
if (!srcDb.exists()) {
logger.info(" {} does not exist.", srcDb);
return true;
}
FileUtils.createDirIfNotExists(dstDir);
logger.info("Copy database {} start", this.dbName);
FileUtils.copyDir(Paths.get(srcDir), Paths.get(dstDir), dbName);
logger.info("Copy database {} end", this.dbName);
return true;
}
@Override
public String name() {
return dbName;
}
}
}