From 373baa734e9615148fbf6c063269e05eee447d79 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 30 Nov 2020 10:04:45 +0800 Subject: [PATCH 01/26] add auth backup and auth restore --- .../com/baidu/hugegraph/base/ToolClient.java | 13 +- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 41 +- .../com/baidu/hugegraph/cmd/SubCommands.java | 195 +++++++- .../hugegraph/constant/AuthRestoreFlow.java | 30 ++ .../constant/AuthRestoreStrategy.java | 42 ++ .../hugegraph/manager/AuthBackupManager.java | 142 ++++++ .../hugegraph/manager/AuthRestoreManager.java | 465 ++++++++++++++++++ .../manager/BackupRestoreBaseManager.java | 30 +- 8 files changed, 904 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java create mode 100644 src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java create mode 100644 src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java create mode 100644 src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java diff --git a/src/main/java/com/baidu/hugegraph/base/ToolClient.java b/src/main/java/com/baidu/hugegraph/base/ToolClient.java index d054e08..a67a541 100644 --- a/src/main/java/com/baidu/hugegraph/base/ToolClient.java +++ b/src/main/java/com/baidu/hugegraph/base/ToolClient.java @@ -19,18 +19,19 @@ package com.baidu.hugegraph.base; -import java.nio.file.Paths; - -import org.apache.commons.lang3.StringUtils; - +import com.baidu.hugegraph.driver.AuthManager; import com.baidu.hugegraph.driver.GraphManager; import com.baidu.hugegraph.driver.GremlinManager; import com.baidu.hugegraph.driver.HugeClient; import com.baidu.hugegraph.driver.SchemaManager; import com.baidu.hugegraph.driver.TaskManager; import com.baidu.hugegraph.driver.TraverserManager; + import com.baidu.hugegraph.util.E; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; + +import java.nio.file.Paths; public class ToolClient { @@ -113,6 +114,10 @@ public static String homePath() { return homePath; } + public AuthManager authManager() { + return this.client.auth(); + } + public static class ConnectionInfo { private String url; diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index dab5361..2f94d8d 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -19,21 +19,11 @@ package com.baidu.hugegraph.cmd; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.base.ToolClient.ConnectionInfo; import com.baidu.hugegraph.base.ToolManager; -import com.baidu.hugegraph.manager.BackupManager; -import com.baidu.hugegraph.manager.DumpGraphManager; -import com.baidu.hugegraph.manager.GraphsManager; -import com.baidu.hugegraph.manager.GremlinManager; -import com.baidu.hugegraph.manager.RestoreManager; -import com.baidu.hugegraph.manager.TasksManager; +import com.baidu.hugegraph.manager.*; import com.baidu.hugegraph.structure.Task; import com.baidu.hugegraph.structure.constant.GraphMode; import com.baidu.hugegraph.structure.gremlin.Result; @@ -43,6 +33,11 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParametersDelegate; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import static com.baidu.hugegraph.manager.BackupManager.BACKUP_DEFAULT_TIMEOUT; public class HugeGraphCommand { @@ -328,6 +323,22 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Tasks are cleared[force=%s]", taskClear.force()); break; + case "auth-backup": + Printer.print("Auth backup start !"); + SubCommands.AuthBackup authBackup = this.subCommand(subCmd); + AuthBackupManager authBackupManager = manager(AuthBackupManager.class); + + authBackupManager.init(authBackup); + authBackupManager.authBackup(authBackup.types()); + break; + case "auth-restore": + Printer.print("Auth restore start !"); + SubCommands.AuthRestore authRestore = this.subCommand(subCmd); + AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); + + authRestoreManager.init(authRestore); + authRestoreManager.authRestore(authRestore.types()); + break; case "help": jCommander.usage(); break; @@ -423,8 +434,12 @@ public static void main(String[] args) { jCommander.usage(); System.exit(-1); } - - cmd.execute(subCommand, jCommander); + try { + cmd.execute(subCommand, jCommander); + } catch (Exception e) { + Printer.print("Exception in 'main' is %s", e); + System.exit(-1); + } System.exit(0); } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 5a71e38..0e7e838 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -19,32 +19,23 @@ package com.baidu.hugegraph.cmd; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.io.FileUtils; - import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.constant.AuthRestoreStrategy; import com.baidu.hugegraph.manager.TasksManager; import com.baidu.hugegraph.structure.constant.GraphMode; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; -import com.beust.jcommander.DynamicParameter; -import com.beust.jcommander.IParameterValidator; -import com.beust.jcommander.IStringConverter; -import com.beust.jcommander.Parameter; -import com.beust.jcommander.ParameterException; -import com.beust.jcommander.Parameters; -import com.beust.jcommander.ParametersDelegate; +import com.beust.jcommander.*; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.util.*; public class SubCommands { @@ -82,6 +73,9 @@ private void initSubCommands() { this.commands.put("clear", new Clear()); this.commands.put("stop-all", new StopAll()); + this.commands.put("auth-backup", new AuthBackup()); + this.commands.put("auth-restore", new AuthRestore()); + this.commands.put("help", new Help()); } @@ -812,6 +806,122 @@ public static class TaskId { private long taskId; } + public static class AuthBackupRestore { + + @Parameter(names = {"--directory"}, arity = 1, + description = "Directory of auth information, default is " + + "'./{authName}' in local file system " + + "or '{fs.default.name}/{authName}' in HDFS") + public String directory; + + @DynamicParameter(names = "-D", + description = "HDFS config parameters") + private Map hdfsConf = new HashMap<>(); + + @ParametersDelegate + private Retry retry = new Retry(); + + public int retry() { + return this.retry.retry; + } + + public String directory() { + return this.directory; + } + + public Map hdfsConf() { + return this.hdfsConf; + } + + public void directory(String directory) { + this.directory = directory; + } + + public void hdfsConf(Map hdfsConf) { + this.hdfsConf = hdfsConf; + } + } + + public static class AuthBackup extends AuthBackupRestore { + + @ParametersDelegate + private AuthTypes types = new AuthTypes(); + + @Parameter(names = {"--format"}, arity = 1, + validateWith = {FormatValidator.class}, + description = "File format, valid is [json, text]") + public String format = "json"; + + public List types() { + return this.types.types; + } + + public void types(List types) { + this.types.types = types; + } + + public String format() { + return this.format; + } + + public void format(String format) { + this.format = format; + } + } + + public static class AuthRestore extends AuthBackupRestore { + + @ParametersDelegate + private AuthTypes types = new AuthTypes(); + + @Parameter(names = {"--strategy"}, + converter = AuthStrategyConverter.class, + description = "restore strategy, " + + "include: [stop, ignore]") + public String strategy = AuthStrategyConverter.strategy; + + @Parameter(names = {"--init-password"}, arity = 1, + description = "init password") + public String initPassword = StringUtils.EMPTY; + + public List types() { + return this.types.types; + } + + public void types(List types) { + this.types.types = types; + } + + public String strategy() { + return this.strategy; + } + + public void strategy(String strategy) { + this.strategy = strategy; + } + + public String initPassword() { + return this.initPassword; + } + + public void initPassword(String initPassword) { + this.initPassword = initPassword; + } + + } + + public static class AuthTypes { + + @Parameter(names = {"--types", "-t"}, + listConverter = AuthHugeTypeConverter.class, + description = "Type of auth " + + "Concat with ',' if more than one. " + + "'all' means all auth information" + + " in other words, 'all' equals with " + + "'user,group,target,belong,access'") + public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; + } + public static class GraphModeConverter implements IStringConverter { @@ -855,6 +965,57 @@ public List convert(String value) { } } + public static class AuthHugeTypeConverter + implements IStringConverter> { + + public static final List AUTH_ALL_TYPES = ImmutableList.of( + HugeType.TARGET, HugeType.GROUP, + HugeType.USER, HugeType.ACCESS, + HugeType.BELONG + ); + + @Override + public List convert(String value) { + E.checkArgument(value != null && !value.isEmpty(), + "HugeType can't be null or empty"); + String[] types = value.split(","); + if (types.length == 1 && types[0].equalsIgnoreCase("all")) { + return AUTH_ALL_TYPES; + } + List hugeTypes = new ArrayList<>(); + for (String type : types) { + try { + hugeTypes.add(HugeType.valueOf(type.toUpperCase())); + } catch (IllegalArgumentException e) { + throw new ParameterException(String.format( + "Invalid --type '%s', valid value is 'all' or " + + "combination of 'user,group,target," + + "belong,access'", type)); + } + } + return hugeTypes; + } + } + + public static class AuthStrategyConverter + implements IStringConverter { + + public static final String strategy = "stop"; + + @Override + public String convert(String value) { + E.checkArgument(value != null && !value.isEmpty(), + "Strategy can't be null or empty"); + if (AuthRestoreStrategy.STOP.string().equals(value) || + AuthRestoreStrategy.IGNORE.string().equals(value)) { + return value; + } else { + throw new ParameterException(String.format( + "Invalid --strategy '%s', valid value is 'stop' or 'ignore", value)); + } + } + } + public static class MapConverter implements IStringConverter> { diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java new file mode 100644 index 0000000..efbfb14 --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java @@ -0,0 +1,30 @@ +/* + * Copyright 2020 HugeGraph Authors + * + */ + +package com.baidu.hugegraph.constant; + +public enum AuthRestoreFlow { + + CHECK(1, "check"), + RESTORE(2, "restore") + ; + + private int code; + private String name = null; + + AuthRestoreFlow(int code, String name) { + assert code < 256; + this.code = code; + this.name = name; + } + + public int code() { + return this.code; + } + + public String string() { + return this.name; + } +} diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java new file mode 100644 index 0000000..c8d19b3 --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java @@ -0,0 +1,42 @@ +/* + * Copyright 2020 HugeGraph Authors + * + */ + +package com.baidu.hugegraph.constant; + +public enum AuthRestoreStrategy { + + STOP(1, "stop"), + IGNORE(2, "ignore") + ; + + private int code; + private String name = null; + + AuthRestoreStrategy(int code, String name) { + assert code < 256; + this.code = code; + this.name = name; + } + + public int code() { + return this.code; + } + + public static AuthRestoreStrategy getEnumByName(String name) { + AuthRestoreStrategy[] restoreStrategys = AuthRestoreStrategy.values(); + for (AuthRestoreStrategy strategy : restoreStrategys) { + if (strategy.string().equals(name)) { + return strategy; + } + } + return null; + } + + public String string() { + return this.name; + } + + +} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java new file mode 100644 index 0000000..ceb98dc --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -0,0 +1,142 @@ +/* + * Copyright 2020 HugeGraph Authors + * backup of authority + * operation commands include: + * auth-backup + * --types : backup data type, the default is 'all', + * include user,group,target,belong,access + * --directory : backup directory, the default is './auth-backup' + * + */ + +package com.baidu.hugegraph.manager; + +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.base.HdfsDirectory; +import com.baidu.hugegraph.base.LocalDirectory; +import com.baidu.hugegraph.base.Printer; +import com.baidu.hugegraph.base.ToolClient; +import com.baidu.hugegraph.cmd.SubCommands; +import com.baidu.hugegraph.exception.ToolsException; +import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.constant.HugeType; +import com.baidu.hugegraph.util.JsonUtil; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + +public class AuthBackupManager extends BackupRestoreBaseManager { + + private static final String AUTH_BACKUP_NAME = "auth-backup"; + + public AuthBackupManager(ToolClient.ConnectionInfo info) { + super(info, AUTH_BACKUP_NAME); + } + + public void init(SubCommands.AuthBackup authBackup) { + this.retry(authBackup.retry()); + this.directory(authBackup.directory(), authBackup.hdfsConf()); + this.ensureDirectoryExist(true); + } + + public void authBackup(List types) { + for (HugeType type : types) { + switch (type) { + case USER: + this.backupUsers(); + break; + case GROUP: + this.backupGroups(); + break; + case TARGET: + this.backupTargets(); + break; + case BELONG: + this.backupBelongs(); + break; + case ACCESS: + this.backupAccesses(); + break; + default: + throw new AssertionError(String.format( + "Bad backup type: %s", type)); + } + } + this.shutdown(this.type()); + } + + protected void backupUsers() { + Printer.print("Users backup started"); + List users = retry(this.client.authManager()::listUsers, + "querying users of authority"); + long writeLines = this.writeText(HugeType.USER, users); + Printer.print("Users backup finished, write lines: %d", writeLines); + } + + protected void backupGroups() { + Printer.print("Groups backup started"); + List groups = retry(this.client.authManager()::listGroups, + "querying groups of authority"); + long writeLines = this.writeText(HugeType.GROUP, groups); + Printer.print("Groups backup finished, write lines: %d", writeLines); + } + + protected void backupTargets() { + Printer.print("Targets backup started"); + List targets = retry(this.client.authManager()::listTargets, + "querying targets of authority"); + long writeLines = this.writeText(HugeType.TARGET, targets); + Printer.print("Targets backup finished, write lines: %d", writeLines); + } + + protected void backupBelongs() { + Printer.print("Belongs backup started"); + List belongs = retry(this.client.authManager()::listBelongs, + "querying belongs of authority"); + long writeLines = this.writeText(HugeType.BELONG, belongs); + Printer.print("Belongs backup finished, write lines: %d", writeLines); + } + + protected void backupAccesses() { + Printer.print("Accesses backup started"); + List accesses = retry(this.client.authManager()::listAccesses, + "querying accesses of authority"); + long writeLines = this.writeText(HugeType.ACCESS, accesses); + Printer.print("Accesses backup finished, write lines: %d", writeLines); + } + + protected long writeText(HugeType type, List list) { + long count = 0L; + try { + OutputStream os = this.outputStream(type.string(), false); + ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE); + StringBuilder builder = new StringBuilder(LBUF_SIZE); + + for (Object e : list) { + count++; + builder.append(JsonUtil.toJson(e)).append("\n"); + } + baos.write(builder.toString().getBytes(API.CHARSET)); + os.write(baos.toByteArray()); + } catch (Throwable e) { + throw new ToolsException("Failed to serialize %s to %s", + e, type, type.string()); + } + return count; + } + + protected void directory(String dir, Map hdfsConf) { + if (hdfsConf == null || hdfsConf.isEmpty()) { + // Local FS directory + super.directory = LocalDirectory.constructDir(dir, AUTH_BACKUP_NAME); + } else { + // HDFS directory + super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_NAME, + hdfsConf); + } + } + + +} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java new file mode 100644 index 0000000..80fa593 --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -0,0 +1,465 @@ +/* + * Copyright 2020 HugeGraph Authors + * restore authority data + * operation commands include: + * auth-restore + * --types : restore data type, the default is 'all', + * include user,group,target,belong,access + * --directory : backup directory, the default is './auth-backup' + * --init-password : if the restore data includes user, + * an initialization password must be set + * --strategy : restore strategy include 'stop' and 'ignore', + * the default is 'stop' + * + */ + +package com.baidu.hugegraph.manager; + +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.base.HdfsDirectory; +import com.baidu.hugegraph.base.LocalDirectory; +import com.baidu.hugegraph.base.Printer; +import com.baidu.hugegraph.base.ToolClient; +import com.baidu.hugegraph.cmd.SubCommands; +import com.baidu.hugegraph.constant.AuthRestoreFlow; +import com.baidu.hugegraph.constant.AuthRestoreStrategy; +import com.baidu.hugegraph.exception.ToolsException; +import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.constant.HugeType; +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.beust.jcommander.ParameterException; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +public class AuthRestoreManager extends BackupRestoreBaseManager { + + private static final String AUTH_BACKUP_NAME = "auth-backup"; + private static final String AUTH_RESTORE_DIR = "auth-restore"; + + private int conflict_status = 0; + private AuthRestoreStrategy strategy; + private String initPassword; + /** + * The collection of id relationships of users, groups and targets + * is the basic data of belong and accesses。 + */ + private Map idsMap; + private Map usersByName; + private Map groupsByName; + private Map targetsByName; + private Map belongsByName; + private Map accessesByName; + + + public AuthRestoreManager(ToolClient.ConnectionInfo info) { + super(info, AUTH_RESTORE_DIR); + } + + public void init(SubCommands.AuthRestore authRestore) { + this.retry(authRestore.retry()); + this.directory(authRestore.directory(), authRestore.hdfsConf()); + this.ensureDirectoryExist(false); + this.initStrategy(authRestore.strategy()); + this.initPassword(authRestore.types(), authRestore.initPassword()); + this.idsMap = Maps.newHashMap(); + this.usersByName = Maps.newHashMap(); + this.groupsByName = Maps.newHashMap(); + this.targetsByName = Maps.newHashMap(); + this.belongsByName = Maps.newHashMap(); + this.accessesByName = Maps.newHashMap(); + } + + public void authRestore(List types) { + List sortedHugeTypes = this.sortListByCode(types); + this.restore(sortedHugeTypes, AuthRestoreFlow.CHECK.code()); + this.restore(sortedHugeTypes, AuthRestoreFlow.RESTORE.code()); + } + + public void restore(List types, int status) { + for (HugeType type : types) { + switch (type) { + case USER: + if (status == AuthRestoreFlow.CHECK.code()) { + this.checkUseConflict(); + } else { + this.restoreUsers(); + } + break; + case GROUP: + if (status == AuthRestoreFlow.CHECK.code()) { + this.checkGroupsConflict(); + } else { + this.restoreGroups(); + } + break; + case TARGET: + if (status == AuthRestoreFlow.CHECK.code()) { + this.checkTargetsConflict(); + } else { + this.restoreTargets(); + } + break; + case BELONG: + if (status == AuthRestoreFlow.CHECK.code()) { + this.checkBelongsConflict(); + } else { + this.restoreBelongs(); + } + break; + case ACCESS: + if (status == AuthRestoreFlow.CHECK.code()) { + this.checkAccessesConflict(); + } else { + this.restoreAccesses(); + } + break; + default: + throw new AssertionError(String.format( + "Bad restore type: %s", type)); + } + } + if (status == AuthRestoreFlow.RESTORE.code()) { + this.shutdown(this.type()); + } + } + + protected void checkUseConflict() { + List users = retry(this.client.authManager()::listUsers, + "Querying users of authority"); + List userJsons = this.read(HugeType.USER); + Map userMap = Maps.newHashMap(); + for (User user : users) { + userMap.put(user.name(), user); + } + for (String userStr : userJsons) { + int conflict = this.conflict_status; + User restoreUser = JsonUtil.fromJson(userStr, User.class); + if (userMap.containsKey(restoreUser.name())) { + User resourceUser = userMap.get(restoreUser.name()); + if (resourceUser.phone() != null ? + !resourceUser.phone().equals(restoreUser.phone()) : + restoreUser.phone() != null) { + conflict++; + } + if (resourceUser.email() != null ? + !resourceUser.email().equals(restoreUser.email()) : + restoreUser.email() != null) { + conflict++; + } + if (resourceUser.avatar() != null ? + !resourceUser.avatar().equals(restoreUser.avatar()) : + restoreUser.avatar() != null) { + conflict++; + } + if (conflict > this.conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore users conflict of properties"); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore users strategy is not fund"); + } else { + this.idsMap.put(restoreUser.id().toString(), + resourceUser.id().toString()); + } + continue; + } + this.prepareUserRestore(restoreUser); + } + } + + protected void checkGroupsConflict() { + List groups = retry(this.client.authManager()::listGroups, + "Querying users of authority"); + List groupJsons = this.read(HugeType.GROUP); + Map groupMap = Maps.newHashMap(); + for (Group group : groups) { + groupMap.put(group.name(), group); + } + for (String groupStr : groupJsons) { + int conflict = this.conflict_status; + Group restoreGroup = JsonUtil.fromJson(groupStr, Group.class); + if (groupMap.containsKey(restoreGroup.name())) { + Group resourceGroup = groupMap.get(restoreGroup.name()); + if (resourceGroup.description() != null ? + !resourceGroup.description().equals(restoreGroup.description()) : + restoreGroup.description() != null) { + conflict++; + } + if (conflict > this.conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore groups conflict"); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore groups strategy is not fund"); + } else { + this.idsMap.put(restoreGroup.id().toString(), + resourceGroup.id().toString()); + } + continue; + } + this.prepareGroupRestore(restoreGroup); + } + } + + protected void checkTargetsConflict() { + List targets = retry(this.client.authManager()::listTargets, + "Querying targets of authority"); + List targetJsons = this.read(HugeType.TARGET); + Map targetMap = Maps.newHashMap(); + for (Target target : targets) { + targetMap.put(target.name(), target); + } + for (String targetStr : targetJsons) { + int conflict = this.conflict_status; + Target restoreTarget = JsonUtil.fromJson(targetStr, Target.class); + if (targetMap.containsKey(restoreTarget.name())) { + Target resourceTarget = targetMap.get(restoreTarget.name()); + if (resourceTarget.graph() != null ? + !resourceTarget.graph().equals(restoreTarget.graph()) : + restoreTarget.graph() != null) { + conflict++; + } + if (resourceTarget.url() != null ? + !resourceTarget.url().equals(restoreTarget.url()) : + restoreTarget.url() != null) { + conflict++; + } + if (conflict > this.conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore targets conflict with stop strategy"); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore targets strategy is not fund"); + } else { + this.idsMap.put(restoreTarget.id().toString(), + resourceTarget.id().toString()); + } + continue; + } + this.prepareTargetForRestore(restoreTarget); + } + } + + protected void checkBelongsConflict() { + List belongs = retry(this.client.authManager()::listBelongs, + "Querying belongs of authority"); + List belongJsons = this.read(HugeType.BELONG); + Map belongMap = Maps.newHashMap(); + for (Belong belong : belongs) { + belongMap.put(belong.user() + ":" + belong.group(), + belong); + } + for (String str : belongJsons) { + Belong restoreBelong = JsonUtil.fromJson(str, Belong.class); + if (checkIdMaps(restoreBelong.user().toString(), + restoreBelong.group().toString())) { + continue; + } + String ids = this.idsMap.get(restoreBelong.user()) + ":" + + this.idsMap.get(restoreBelong.group()); + if (belongMap.containsKey(ids)) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore belongs conflict with stop strategy"); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore belongs strategy is not fund"); + continue; + } + this.belongsByName.put(restoreBelong.id().toString(), restoreBelong); + } + } + + protected void checkAccessesConflict() { + List accesses = retry(this.client.authManager()::listAccesses, + "Querying accesses of authority"); + List accessJsons = this.read(HugeType.ACCESS); + Map accessMap = Maps.newHashMap(); + for (Access access : accesses) { + accessMap.put(access.group() + ":" + access.target(), + access); + } + for (String str : accessJsons) { + Access restoreAccess = JsonUtil.fromJson(str, Access.class); + if (checkIdMaps(restoreAccess.group().toString(), + restoreAccess.target().toString())) { + continue; + } + String ids = this.idsMap.get(restoreAccess.group()) + ":" + + this.idsMap.get(restoreAccess.target()); + if (accessMap.containsKey(ids)) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore accesses conflict with stop strategy"); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore accesses strategy is not fund"); + continue; + } + this.accessesByName.put(restoreAccess.id().toString(), restoreAccess); + } + } + + protected void restoreAccesses() { + int counts = 0; + for (Map.Entry entry : accessesByName.entrySet()) { + Access restoreAccess = entry.getValue(); + restoreAccess.target(idsMap.get(restoreAccess.target().toString())); + restoreAccess.group(idsMap.get(restoreAccess.group().toString())); + Access access = retry(() -> { + return this.client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); + idsMap.put(restoreAccess.id().toString(), access.id().toString()); + counts++; + } + Printer.print("Restore accesses finished, counts is %s !", counts); + } + + protected void restoreBelongs() { + int counts = 0; + for (Map.Entry entry : belongsByName.entrySet()) { + Belong restoreBelong = entry.getValue(); + restoreBelong.user(idsMap.get(restoreBelong.user().toString())); + restoreBelong.group(idsMap.get(restoreBelong.group().toString())); + Belong belong = retry(() -> { + return this.client.authManager().createBelong(restoreBelong); + }, "Restore targets of authority"); + idsMap.put(belong.id().toString(), belong.id().toString()); + counts++; + } + Printer.print("Restore belongs finished, counts is %s !", counts); + } + + protected void restoreTargets() { + int counts = 0; + for (Map.Entry entry: this.targetsByName.entrySet()) { + Target target = retry(() -> { + return this.client.authManager().createTarget(entry.getValue()); + }, "Restore targets of authority"); + idsMap.put(target.id().toString(), target.id().toString()); + counts++; + } + Printer.print("Restore targets finished, counts is %s !", counts); + } + + protected void restoreGroups() { + int counts = 0; + for (Map.Entry entry: this.groupsByName.entrySet()) { + Group group = retry(() -> { + return this.client.authManager().createGroup(entry.getValue()); + }, "Restore groups of authority"); + idsMap.put(group.id().toString(), group.id().toString()); + counts++; + } + Printer.print("Restore groups finished, counts is %s !", counts); + } + + protected void restoreUsers() { + int counts = 0; + for (Map.Entry entry: this.usersByName.entrySet()) { + User user = entry.getValue(); + user.password(this.initPassword); + User createUser = retry(() -> { + return this.client.authManager().createUser(user); + }, "Restore users of authority"); + idsMap.put(createUser.id().toString(), createUser.id().toString()); + counts++; + } + Printer.print("Restore users finished, counts is %s !", counts); + } + + protected void prepareTargetForRestore(Target restoreTarget) { + this.idsMap.put(restoreTarget.id().toString(), restoreTarget.id().toString()); + this.targetsByName.put(restoreTarget.name(), restoreTarget); + } + + protected void prepareGroupRestore(Group restoreGroup) { + this.idsMap.put(restoreGroup.id().toString(), restoreGroup.id().toString()); + this.groupsByName.put(restoreGroup.name(), restoreGroup); + } + + protected void prepareUserRestore(User restoreUser) { + this.idsMap.put(restoreUser.id().toString(), restoreUser.id().toString()); + this.usersByName.put(restoreUser.name(), restoreUser); + } + + private boolean checkIdMaps(String oneId, String otherId) { + if (!idsMap.containsKey(oneId) || + !idsMap.containsKey(otherId)) { + return true; + } + return false; + } + + protected List read(HugeType type) { + List resultList = Lists.newArrayList(); + InputStream is = this.inputStream(type.string()); + try (InputStreamReader isr = new InputStreamReader(is, API.CHARSET); + BufferedReader reader = new BufferedReader(isr)) { + String line; + while ((line = reader.readLine()) != null) { + resultList.add(line); + } + } catch (IOException e) { + throw new ToolsException("Failed to deserialize %s from %s", + e, type, type.string()); + } + return resultList; + } + + protected void directory(String dir, Map hdfsConf) { + if (hdfsConf == null || hdfsConf.isEmpty()) { + // Local FS directory + super.directory = LocalDirectory.constructDir(dir, AUTH_BACKUP_NAME); + } else { + // HDFS directory + super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_NAME, + hdfsConf); + } + } + + private void initStrategy(String strategy) { + if (!StringUtils.isEmpty(strategy)) { + this.strategy = AuthRestoreStrategy.getEnumByName(strategy); + } else { + throw new ParameterException(String.format( + "Bad restore strategy: %s", strategy)); + } + } + + public List sortListByCode(List hugeTypes) { + return hugeTypes.stream(). + sorted(Comparator.comparing(HugeType::code)). + collect(Collectors.toList()); + } + + public void initPassword(List types, String password) { + if (types.contains(HugeType.USER) && Strings.isEmpty(password)) { + throw new ParameterException(String.format( + "The following option is required: [--init-password]")); + } else { + this.initPassword = password; + } + + } + + public static void main(String[] strings) { + + } + + + + + +} diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java index d8363d3..7092bae 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java @@ -19,26 +19,8 @@ package com.baidu.hugegraph.manager; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.BiConsumer; - import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.base.Directory; -import com.baidu.hugegraph.base.HdfsDirectory; -import com.baidu.hugegraph.base.LocalDirectory; -import com.baidu.hugegraph.base.Printer; -import com.baidu.hugegraph.base.RetryManager; -import com.baidu.hugegraph.base.ToolClient; +import com.baidu.hugegraph.base.*; import com.baidu.hugegraph.cmd.SubCommands; import com.baidu.hugegraph.concurrent.KeyLock; import com.baidu.hugegraph.exception.ToolsException; @@ -48,6 +30,14 @@ import com.baidu.hugegraph.util.E; import com.google.common.collect.ImmutableMap; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; + import static com.baidu.hugegraph.base.Directory.closeAndIgnoreException; public class BackupRestoreBaseManager extends RetryManager { @@ -205,7 +195,7 @@ protected OutputStream outputStream(String file, boolean compress) { return os; } - private InputStream inputStream(String file) { + protected InputStream inputStream(String file) { InputStream is = this.inputStreams.get(file); if (is != null) { return is; From b1f53bb5023fa3388db61b0d97995fecc5bbee81 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 30 Nov 2020 13:05:03 +0800 Subject: [PATCH 02/26] update some code format --- .../com/baidu/hugegraph/cmd/SubCommands.java | 37 ++++++++++--------- .../hugegraph/manager/AuthBackupManager.java | 13 +++---- .../hugegraph/manager/AuthRestoreManager.java | 34 +++++++---------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 0e7e838..21e3cdc 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -809,13 +809,13 @@ public static class TaskId { public static class AuthBackupRestore { @Parameter(names = {"--directory"}, arity = 1, - description = "Directory of auth information, default is " + - "'./{authName}' in local file system " + - "or '{fs.default.name}/{authName}' in HDFS") + description = "Directory of auth information, default is " + + "'./{authName}' in local file system " + + "or '{fs.default.name}/{authName}' in HDFS") public String directory; @DynamicParameter(names = "-D", - description = "HDFS config parameters") + description = "HDFS config parameters") private Map hdfsConf = new HashMap<>(); @ParametersDelegate @@ -848,8 +848,8 @@ public static class AuthBackup extends AuthBackupRestore { private AuthTypes types = new AuthTypes(); @Parameter(names = {"--format"}, arity = 1, - validateWith = {FormatValidator.class}, - description = "File format, valid is [json, text]") + validateWith = {FormatValidator.class}, + description = "File format, valid is [json, text]") public String format = "json"; public List types() { @@ -913,12 +913,12 @@ public void initPassword(String initPassword) { public static class AuthTypes { @Parameter(names = {"--types", "-t"}, - listConverter = AuthHugeTypeConverter.class, - description = "Type of auth " + - "Concat with ',' if more than one. " + - "'all' means all auth information" + - " in other words, 'all' equals with " + - "'user,group,target,belong,access'") + listConverter = AuthHugeTypeConverter.class, + description = "Type of auth " + + "Concat with ',' if more than one. " + + "'all' means all auth information" + + " in other words, 'all' equals with " + + "'user,group,target,belong,access'") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } @@ -977,7 +977,7 @@ public static class AuthHugeTypeConverter @Override public List convert(String value) { E.checkArgument(value != null && !value.isEmpty(), - "HugeType can't be null or empty"); + "HugeType can't be null or empty"); String[] types = value.split(","); if (types.length == 1 && types[0].equalsIgnoreCase("all")) { return AUTH_ALL_TYPES; @@ -988,9 +988,9 @@ public List convert(String value) { hugeTypes.add(HugeType.valueOf(type.toUpperCase())); } catch (IllegalArgumentException e) { throw new ParameterException(String.format( - "Invalid --type '%s', valid value is 'all' or " + - "combination of 'user,group,target," + - "belong,access'", type)); + "Invalid --type '%s', valid value is 'all' or " + + "combination of 'user,group,target," + + "belong,access'", type)); } } return hugeTypes; @@ -1005,13 +1005,14 @@ public static class AuthStrategyConverter @Override public String convert(String value) { E.checkArgument(value != null && !value.isEmpty(), - "Strategy can't be null or empty"); + "Strategy can't be null or empty"); if (AuthRestoreStrategy.STOP.string().equals(value) || AuthRestoreStrategy.IGNORE.string().equals(value)) { return value; } else { throw new ParameterException(String.format( - "Invalid --strategy '%s', valid value is 'stop' or 'ignore", value)); + "Invalid --strategy '%s', valid value is 'stop' or " + + "'ignore", value)); } } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java index ceb98dc..7c5d475 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -61,7 +61,7 @@ public void authBackup(List types) { break; default: throw new AssertionError(String.format( - "Bad backup type: %s", type)); + "Bad backup type: %s", type)); } } this.shutdown(this.type()); @@ -70,7 +70,7 @@ public void authBackup(List types) { protected void backupUsers() { Printer.print("Users backup started"); List users = retry(this.client.authManager()::listUsers, - "querying users of authority"); + "querying users of authority"); long writeLines = this.writeText(HugeType.USER, users); Printer.print("Users backup finished, write lines: %d", writeLines); } @@ -78,7 +78,7 @@ protected void backupUsers() { protected void backupGroups() { Printer.print("Groups backup started"); List groups = retry(this.client.authManager()::listGroups, - "querying groups of authority"); + "querying groups of authority"); long writeLines = this.writeText(HugeType.GROUP, groups); Printer.print("Groups backup finished, write lines: %d", writeLines); } @@ -86,7 +86,7 @@ protected void backupGroups() { protected void backupTargets() { Printer.print("Targets backup started"); List targets = retry(this.client.authManager()::listTargets, - "querying targets of authority"); + "querying targets of authority"); long writeLines = this.writeText(HugeType.TARGET, targets); Printer.print("Targets backup finished, write lines: %d", writeLines); } @@ -94,7 +94,7 @@ protected void backupTargets() { protected void backupBelongs() { Printer.print("Belongs backup started"); List belongs = retry(this.client.authManager()::listBelongs, - "querying belongs of authority"); + "querying belongs of authority"); long writeLines = this.writeText(HugeType.BELONG, belongs); Printer.print("Belongs backup finished, write lines: %d", writeLines); } @@ -102,7 +102,7 @@ protected void backupBelongs() { protected void backupAccesses() { Printer.print("Accesses backup started"); List accesses = retry(this.client.authManager()::listAccesses, - "querying accesses of authority"); + "querying accesses of authority"); long writeLines = this.writeText(HugeType.ACCESS, accesses); Printer.print("Accesses backup finished, write lines: %d", writeLines); } @@ -138,5 +138,4 @@ protected void directory(String dir, Map hdfsConf) { } } - } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 80fa593..94b6c82 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -52,7 +52,7 @@ public class AuthRestoreManager extends BackupRestoreBaseManager { private int conflict_status = 0; private AuthRestoreStrategy strategy; private String initPassword; - /** + /* * The collection of id relationships of users, groups and targets * is the basic data of belong and accesses。 */ @@ -128,7 +128,7 @@ public void restore(List types, int status) { break; default: throw new AssertionError(String.format( - "Bad restore type: %s", type)); + "Bad restore type: %s", type)); } } if (status == AuthRestoreFlow.RESTORE.code()) { @@ -138,7 +138,7 @@ public void restore(List types, int status) { protected void checkUseConflict() { List users = retry(this.client.authManager()::listUsers, - "Querying users of authority"); + "Querying users of authority"); List userJsons = this.read(HugeType.USER); Map userMap = Maps.newHashMap(); for (User user : users) { @@ -182,7 +182,7 @@ protected void checkUseConflict() { protected void checkGroupsConflict() { List groups = retry(this.client.authManager()::listGroups, - "Querying users of authority"); + "Querying users of authority"); List groupJsons = this.read(HugeType.GROUP); Map groupMap = Maps.newHashMap(); for (Group group : groups) { @@ -216,7 +216,7 @@ protected void checkGroupsConflict() { protected void checkTargetsConflict() { List targets = retry(this.client.authManager()::listTargets, - "Querying targets of authority"); + "Querying targets of authority"); List targetJsons = this.read(HugeType.TARGET); Map targetMap = Maps.newHashMap(); for (Target target : targets) { @@ -255,7 +255,7 @@ protected void checkTargetsConflict() { protected void checkBelongsConflict() { List belongs = retry(this.client.authManager()::listBelongs, - "Querying belongs of authority"); + "Querying belongs of authority"); List belongJsons = this.read(HugeType.BELONG); Map belongMap = Maps.newHashMap(); for (Belong belong : belongs) { @@ -272,10 +272,10 @@ protected void checkBelongsConflict() { this.idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore belongs conflict with stop strategy"); + "Restore belongs conflict with stop strategy"); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, - "Restore belongs strategy is not fund"); + "Restore belongs strategy is not fund"); continue; } this.belongsByName.put(restoreBelong.id().toString(), restoreBelong); @@ -301,10 +301,10 @@ protected void checkAccessesConflict() { this.idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore accesses conflict with stop strategy"); + "Restore accesses conflict with stop strategy"); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, - "Restore accesses strategy is not fund"); + "Restore accesses strategy is not fund"); continue; } this.accessesByName.put(restoreAccess.id().toString(), restoreAccess); @@ -413,7 +413,7 @@ protected List read(HugeType type) { } } catch (IOException e) { throw new ToolsException("Failed to deserialize %s from %s", - e, type, type.string()); + e, type, type.string()); } return resultList; } @@ -434,7 +434,7 @@ private void initStrategy(String strategy) { this.strategy = AuthRestoreStrategy.getEnumByName(strategy); } else { throw new ParameterException(String.format( - "Bad restore strategy: %s", strategy)); + "Bad restore strategy: %s", strategy)); } } @@ -447,19 +447,11 @@ public List sortListByCode(List hugeTypes) { public void initPassword(List types, String password) { if (types.contains(HugeType.USER) && Strings.isEmpty(password)) { throw new ParameterException(String.format( - "The following option is required: [--init-password]")); + "The following option is required: [--init-password]")); } else { this.initPassword = password; } } - public static void main(String[] strings) { - - } - - - - - } From 5e42e78a3cb078fffe4406de7450e6cd3653aa00 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 1 Dec 2020 15:24:53 +0800 Subject: [PATCH 03/26] Add functional tests for auth backup and restore --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 33 +++ .../com/baidu/hugegraph/cmd/SubCommands.java | 13 +- .../test/functional/AuthBackupTest.java | 103 ++++++++ .../test/functional/AuthRestoreTest.java | 229 ++++++++++++++++++ .../hugegraph/test/functional/AuthTest.java | 36 +++ .../hugegraph/test/functional/ClientUtil.java | 59 +++++ .../hugegraph/test/functional/FileUtil.java | 134 ++++++++++ src/test/resources/auth/auth_accesses.txt | 1 + src/test/resources/auth/auth_belongs.txt | 1 + src/test/resources/auth/auth_groups.txt | 1 + src/test/resources/auth/auth_targets.txt | 1 + src/test/resources/auth/auth_users.txt | 1 + .../resources/auth/auth_users_conflict.txt | 1 + 13 files changed, 610 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java create mode 100644 src/test/resources/auth/auth_accesses.txt create mode 100644 src/test/resources/auth/auth_belongs.txt create mode 100644 src/test/resources/auth/auth_groups.txt create mode 100644 src/test/resources/auth/auth_targets.txt create mode 100644 src/test/resources/auth/auth_users.txt create mode 100644 src/test/resources/auth/auth_users_conflict.txt diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 2f94d8d..4fb9e24 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -33,6 +33,7 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParametersDelegate; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -44,6 +45,8 @@ public class HugeGraphCommand { private static final int DEFAULT_CLEAR_TIMEOUT = 300; + private static final String TEST_MODE = "--test-mode"; + private SubCommands subCommands; @ParametersDelegate @@ -69,6 +72,9 @@ public class HugeGraphCommand { private SubCommands.TrustStorePassword trustStorePassword = new SubCommands.TrustStorePassword(); + @ParametersDelegate + private SubCommands.TestMode testMode = new SubCommands.TestMode(); + public HugeGraphCommand() { this.subCommands = new SubCommands(); } @@ -138,6 +144,14 @@ public void trustStorePassword(String trustStorePassword) { this.trustStorePassword.trustStorePassword = trustStorePassword; } + public String testMode() { + return this.testMode.testMode; + } + + private void testMode(String testMode) { + this.testMode.testMode = testMode; + } + public JCommander jCommander() { JCommander.Builder builder = JCommander.newBuilder(); @@ -414,6 +428,15 @@ private GraphMode mode() { } public static void main(String[] args) { + List list = Arrays.asList(args); + if (!list.contains(TEST_MODE)) { + mainMode(args); + } else { + testMode(args); + } + } + + public static void mainMode(String[] args) { HugeGraphCommand cmd = new HugeGraphCommand(); JCommander jCommander = cmd.jCommander(); @@ -442,4 +465,14 @@ public static void main(String[] args) { } System.exit(0); } + + public static void testMode(String[] args) { + HugeGraphCommand cmd = new HugeGraphCommand(); + JCommander jCommander = cmd.jCommander(); + + jCommander.parse(args); + String subCommand = jCommander.getParsedCommand(); + cmd.execute(subCommand, jCommander); + } + } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 21e3cdc..6aea488 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -129,7 +129,7 @@ public static class Backup extends BackupRestore { public boolean compress = true; @Parameter(names = {"--label"}, arity = 1, - description = "Vertex or edge label, only valid when type " + + description = "Vertex label or edge label, only valid when type " + "is vertex or edge") public String label; @@ -675,6 +675,13 @@ public static class TrustStorePassword { public String trustStorePassword; } + public static class TestMode { + + @Parameter(names = {"--test-mode"}, arity = 1, + description = "Test mode") + public String testMode = "test"; + } + public static class HugeTypes { @Parameter(names = {"--huge-types", "-t"}, @@ -810,8 +817,8 @@ public static class AuthBackupRestore { @Parameter(names = {"--directory"}, arity = 1, description = "Directory of auth information, default is " + - "'./{authName}' in local file system " + - "or '{fs.default.name}/{authName}' in HDFS") + "'./{auth-backup}' in local file system " + + "or '{fs.default.name}/{auth-backup}' in HDFS") public String directory; @DynamicParameter(names = "-D", diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java new file mode 100644 index 0000000..c6dd241 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import com.baidu.hugegraph.cmd.HugeGraphCommand; +import com.baidu.hugegraph.testutil.Assert; +import com.beust.jcommander.ParameterException; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class AuthBackupTest extends AuthTest{ + + @Before + public void init() { + FileUtil.clearFile(DEFAULT_URL); + } + + @Test + public void testAuthBackup() { + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-backup" + }; + + HugeGraphCommand.main(args); + + Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); + List fileNames = FileUtil.getFileDirectoryNames(DEFAULT_URL); + Assert.assertTrue(fileNames.size() == 5); + } + + @Test + public void testAuthBackupByTypes() { + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-backup", + "--types", "user,group" + }; + + HugeGraphCommand.main(args); + + Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); + List fileNames = FileUtil.getFileDirectoryNames(DEFAULT_URL); + Assert.assertTrue(fileNames.size() == 2); + } + + @Test + public void testAuthBackupByTypesWithException() { + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-backup", + "--types", "user,group,test" + }; + + Assert.assertThrows(ParameterException.class, () -> { + HugeGraphCommand.main(args); + }); + } + + @Test + public void testAuthBackupByDirectory() { + String directory = "./backup"; + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-backup", + "--directory", directory + }; + + HugeGraphCommand.main(args); + + Assert.assertTrue(FileUtil.checkFileExists(directory)); + List fileNames = FileUtil.getFileDirectoryNames(directory); + Assert.assertTrue(fileNames.size() == 5); + } + +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java new file mode 100644 index 0000000..9dca518 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -0,0 +1,229 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import com.baidu.hugegraph.cmd.HugeGraphCommand; +import com.baidu.hugegraph.driver.HugeClient; +import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.constant.HugeType; +import com.baidu.hugegraph.testutil.Assert; +import com.beust.jcommander.ParameterException; +import com.google.common.collect.Maps; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +public class AuthRestoreTest extends AuthTest{ + + private HugeClient client; + + @Before + public void init() { + ClientUtil initUtil = new ClientUtil(URL, GRAPH, USER_NAME, + USER_PASSWORD, TIME_OUT, PROTOCOL, + TRUST_STORE_FILE, TRUST_STORE_PASSWORD); + this.client = initUtil.hugeClient; + } + + @Test + public void testAuthRestoreByAll() { + this.loadData("./auth-backup/" + HugeType.USER.string(), + "/auth/auth_users.txt"); + this.loadData("./auth-backup/" + HugeType.TARGET.string(), + "/auth/auth_targets.txt"); + this.loadData("./auth-backup/" + HugeType.GROUP.string(), + "/auth/auth_groups.txt"); + this.loadData("./auth-backup/" + HugeType.BELONG.string(), + "/auth/auth_belongs.txt"); + this.loadData("./auth-backup/" + HugeType.ACCESS.string(), + "/auth/auth_accesses.txt"); + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--directory", DEFAULT_URL, + "--init-password", "123456", + "--strategy", "ignore" + }; + + HugeGraphCommand.main(args); + + List userList = this.client.auth().listUsers(); + Map userMap = Maps.newHashMap(); + for (User user1 : userList) { + userMap.put(user1.name(), user1); + } + Assert.assertTrue(userMap.containsKey("test_user123")); + + List groups = this.client.auth().listGroups(); + Map groupMap = Maps.newHashMap(); + for (Group group : groups) { + groupMap.put(group.name(), group); + } + Assert.assertTrue(groupMap.containsKey("test_group123")); + + List targets = this.client.auth().listTargets(); + Map targetMap = Maps.newHashMap(); + for (Target target : targets) { + targetMap.put(target.name(), target); + } + Assert.assertTrue(targetMap.containsKey("test_target123")); + } + + @Test + public void testAuthRestoreByUser() { + this.loadData("./auth-backup/" + HugeType.USER.string(), + "/auth/auth_users.txt"); + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--directory", DEFAULT_URL, + "--init-password", "123456" + }; + + HugeGraphCommand.main(args); + + List userList = this.client.auth().listUsers(); + Map userMap = Maps.newHashMap(); + for (User user1 : userList) { + userMap.put(user1.name(), user1); + } + + Assert.assertTrue(userMap.containsKey("test_user123")); + + } + + @Test + public void testAuthRestoreWithException() { + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--directory", DEFAULT_URL + }; + + Assert.assertThrows(ParameterException.class, () -> { + HugeGraphCommand.main(args); + }); + } + + @Test + public void testAuthRestoreByStrategyConfict() { + this.loadData("./auth-backup/" + HugeType.USER.string(), + "/auth/auth_users_conflict.txt"); + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--strategy", "stop", + "--init-password", "123456" + }; + + Assert.assertThrows(IllegalArgumentException.class, () -> { + HugeGraphCommand.main(args); + }); + } + + @Test + public void testAuthRestoreByStrategyIgnore() { + this.loadData("./auth-backup/" + HugeType.USER.string(), + "/auth/auth_users_conflict.txt"); + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--strategy", "ignore", + "--init-password", "123456" + }; + + HugeGraphCommand.main(args); + + List userList = this.client.auth().listUsers(); + Map userMap = Maps.newHashMap(); + for (User user1 : userList) { + userMap.put(user1.name(), user1); + } + + Assert.assertTrue(userMap.containsKey("test_user123")); + } + + @Test + public void testAuthRestoreByStrategyStop() { + this.loadData("./auth-backup/" + HugeType.USER.string(), + "/auth/auth_users_conflict.txt"); + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--strategy", "stop", + "--init-password", "123123" + }; + + Assert.assertThrows(IllegalArgumentException.class, () -> { + HugeGraphCommand.main(args); + }); + } + + @Test + public void testAuthRestoreByDirectoryException() { + String filePath = "./auth-test-test"; + + String[] args = new String[]{ + "--test-mode", "test", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--strategy", "stop", + "--init-password", "123456", + "--directory", filePath + }; + + Assert.assertThrows(IllegalStateException.class, () -> { + HugeGraphCommand.main(args); + }); + } + + private void loadData(String restoreFilePath, String dataFilePath) { + List list = FileUtil.read(FileUtil.configPath(dataFilePath)); + + FileUtil.writeText(restoreFilePath, list); + } + +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java new file mode 100644 index 0000000..7d32d24 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +public class AuthTest { + + public static final String DEFAULT_URL = "./auth-backup"; + public static final String USER_NAME = "admin"; + public static final String USER_PASSWORD = "123456"; + public static final String URL = "http://127.0.0.1:8080"; + public static final String GRAPH = "hugegraph"; + public static final Integer TIME_OUT = 30; + public static final String PROTOCOL = "http"; + public static final String TRUST_STORE_FILE = ""; + public static final String TRUST_STORE_PASSWORD = ""; + + + +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java new file mode 100644 index 0000000..3837ce1 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import com.baidu.hugegraph.driver.HugeClient; + +public class ClientUtil { + + private String url; + private String graph; + private String username; + private String password; + private Integer timeout; + private String protocol; + private String trustStoreFile; + private String trustStorePassword; + protected HugeClient hugeClient; + + public ClientUtil(String url, String graph, String username, + String password, Integer timeout, String protocol, + String trustStoreFile, String trustStorePassword) { + this.url = url; + this.graph = graph; + this.username = username; + this.password = password; + this.timeout = timeout; + this.protocol = protocol; + this.trustStoreFile = trustStoreFile; + this.trustStorePassword = trustStorePassword; + this.client(); + } + + protected void client() { + this.hugeClient = HugeClient.builder(this.url, this.graph) + .configUser(this.username, this.password) + .configTimeout(this.timeout) + .configSSL(this.protocol, this.trustStoreFile, + this.trustStorePassword) + .build(); + } + +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java new file mode 100644 index 0000000..bf610ad --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java @@ -0,0 +1,134 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.base.Printer; +import com.baidu.hugegraph.exception.ToolsException; +import com.google.common.collect.Lists; +import org.apache.commons.collections.ListUtils; + +import java.io.*; +import java.nio.file.Paths; +import java.util.List; + +public class FileUtil { + + protected static final int LBUF_SIZE = 1024; + protected static final String CONFIG_PATH = "src/test/resources"; + + public static String configPath(String fileName) { + return Paths.get(CONFIG_PATH, fileName).toString(); + } + + public static boolean checkFileExists(String filePath) { + File file = new File(filePath); + if (file.exists()) { + return true; + } + return false; + } + + public static List getFileDirectoryNames(String filePath) { + File file = new File(filePath); + if (!file.exists()) { + return ListUtils.EMPTY_LIST; + } + String[] files = file.list(); + List list = Lists.newArrayList(); + for (int i = 0; i < files.length; i++) { + File fileDir = new File(file, files[i]); + list.add(fileDir.getName()); + } + + return list; + } + + public static void clearFile(String filePath) { + File file = new File(filePath); + if(file.exists()) { + String[] files = file.list(); + for (int i = 0; i < files.length; i++) { + File fileDir = new File(file, files[i]); + fileDir.delete(); + } + } + } + + public static long writeText(String filePath, List list) { + long count = 0L; + FileOutputStream os = null; + try { + os = new FileOutputStream(filePath); + ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE); + StringBuilder builder = new StringBuilder(LBUF_SIZE); + + for (Object e : list) { + count++; + builder.append(e).append("\n"); + } + baos.write(builder.toString().getBytes(API.CHARSET)); + os.write(baos.toByteArray()); + } catch (Throwable e) { + throw new ToolsException("Failed writeText file path is %s", + e, filePath); + }finally { + if(os != null) { + try { + os.close(); + }catch (Exception e) { + Printer.print("Failed to close file"); + } + + } + + } + return count; + } + + public static List read(String filePath) { + List resultList = Lists.newArrayList(); + InputStream is = null; + try { + is = new FileInputStream(filePath); + InputStreamReader isr = new InputStreamReader(is, API.CHARSET); + BufferedReader reader = new BufferedReader(isr); + String line; + while ((line = reader.readLine()) != null) { + resultList.add(line); + } + }catch (Exception e) { + throw new ToolsException("Failed read file path is %s", + e, filePath); + }finally { + if(is != null) { + try { + is.close(); + }catch (Exception e) { + Printer.print("Failed to close file"); + } + + } + + } + return resultList; + } + +} diff --git a/src/test/resources/auth/auth_accesses.txt b/src/test/resources/auth/auth_accesses.txt new file mode 100644 index 0000000..2eaadbf --- /dev/null +++ b/src/test/resources/auth/auth_accesses.txt @@ -0,0 +1 @@ +{"id":"S-66:test_group123>-88>11>S-66:test_target123","group":"-66:test_group123","target":"-66:test_target123","access_permission":"READ","access_description":"test","access_create":"2020-11-11 15:54:54.008","access_update":"2020-11-18 15:01:13.518","access_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_belongs.txt b/src/test/resources/auth/auth_belongs.txt new file mode 100644 index 0000000..512e4e7 --- /dev/null +++ b/src/test/resources/auth/auth_belongs.txt @@ -0,0 +1 @@ +{"id":"S-66:test_user123>-82>>S-66:test_group123","user":"-66:test_user123","group":"-66:test_group123","belong_description":"restore test","belong_create":"2020-12-01 09:44:40.117","belong_update":"2020-12-01 09:44:40.117","belong_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_groups.txt b/src/test/resources/auth/auth_groups.txt new file mode 100644 index 0000000..1d075ae --- /dev/null +++ b/src/test/resources/auth/auth_groups.txt @@ -0,0 +1 @@ +{"id":"-66:test_group123","group_name":"test_group123","group_description":"user is conflict check user restore","group_create":"2020-11-27 20:08:21.270","group_update":"2020-11-27 20:08:21.270","group_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_targets.txt b/src/test/resources/auth/auth_targets.txt new file mode 100644 index 0000000..88803f4 --- /dev/null +++ b/src/test/resources/auth/auth_targets.txt @@ -0,0 +1 @@ +{"id":"-66:test_target123","target_name":"test_target123","target_graph":"hugegraph","target_url":"127.0.0.1:8080","target_resources":[{"type":"ALL","label":"*","properties":null}],"target_create":"2020-11-11 15:32:01.192","target_update":"2020-11-11 15:32:01.192","target_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_users.txt b/src/test/resources/auth/auth_users.txt new file mode 100644 index 0000000..cdf6acd --- /dev/null +++ b/src/test/resources/auth/auth_users.txt @@ -0,0 +1 @@ +{"id":"-66:test_user123","user_name":"test_user123","user_password":"$2a$04$vXkz8UYV7Gwagj6zA1ifNuSQfAmzuYb2tXdqDoWKEG.nYVc186JXO","user_create":"2020-11-30 22:26:42.225","user_update":"2020-11-30 22:26:42.225","user_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_users_conflict.txt b/src/test/resources/auth/auth_users_conflict.txt new file mode 100644 index 0000000..1a6c8cc --- /dev/null +++ b/src/test/resources/auth/auth_users_conflict.txt @@ -0,0 +1 @@ +{"id":"-66:test_user123","user_name":"test_user123","user_phone":"13255447788","user_password":"$2a$04$vXkz8UYV7Gwagj6zA1ifNuSQfAmzuYb2tXdqDoWKEG.nYVc186JXO","user_create":"2020-11-30 22:26:42.225","user_update":"2020-11-30 22:26:42.225","user_creator":"admin"} \ No newline at end of file From 9c1f61b78cd4bb2667dd50af6a2862bb1d22d08f Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 1 Dec 2020 15:31:11 +0800 Subject: [PATCH 04/26] update auth restore format --- .../hugegraph/test/functional/AuthRestoreTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 9dca518..802c4b7 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -47,15 +47,15 @@ public void init() { @Test public void testAuthRestoreByAll() { this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users.txt"); + "/auth/auth_users.txt"); this.loadData("./auth-backup/" + HugeType.TARGET.string(), - "/auth/auth_targets.txt"); + "/auth/auth_targets.txt"); this.loadData("./auth-backup/" + HugeType.GROUP.string(), - "/auth/auth_groups.txt"); + "/auth/auth_groups.txt"); this.loadData("./auth-backup/" + HugeType.BELONG.string(), - "/auth/auth_belongs.txt"); + "/auth/auth_belongs.txt"); this.loadData("./auth-backup/" + HugeType.ACCESS.string(), - "/auth/auth_accesses.txt"); + "/auth/auth_accesses.txt"); String[] args = new String[]{ "--test-mode", "test", From 038980c023cebe523c80a2957a203f836c1e0b48 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 1 Dec 2020 16:47:03 +0800 Subject: [PATCH 05/26] add travis for tools --- .travis.yml | 27 ++++++ assembly/travis/conf/gremlin-server.yaml | 96 +++++++++++++++++++ assembly/travis/conf/hugegraph.properties | 61 ++++++++++++ assembly/travis/conf/rest-server.properties | 12 +++ assembly/travis/install-hugegraph.sh | 45 +++++++++ .../hugegraph/manager/AuthRestoreManager.java | 40 ++++---- .../test/functional/AuthRestoreTest.java | 12 +-- .../hugegraph/test/functional/ClientUtil.java | 7 +- .../test/functional/FuncTestSuite.java | 31 ++++++ src/test/resources/auth/auth_accesses.txt | 2 +- src/test/resources/auth/auth_belongs.txt | 2 +- src/test/resources/auth/auth_groups.txt | 2 +- src/test/resources/auth/auth_targets.txt | 2 +- src/test/resources/auth/auth_users.txt | 2 +- .../resources/auth/auth_users_conflict.txt | 2 +- 15 files changed, 306 insertions(+), 37 deletions(-) create mode 100644 .travis.yml create mode 100644 assembly/travis/conf/gremlin-server.yaml create mode 100644 assembly/travis/conf/hugegraph.properties create mode 100644 assembly/travis/conf/rest-server.properties create mode 100755 assembly/travis/install-hugegraph.sh create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..27f7932 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ +language: java + +jdk: + - openjdk8 + +sudo: required + +branches: + only: + - master + - /^release-.*$/ + - /^test-.*$/ + +install: mvn compile -Dmaven.javadoc.skip=true | grep -v "Downloading\|Downloaded" + +before_script: + - $TRAVIS_DIR/install-hugegraph.sh $TRAVIS_BRANCH | grep -v "Downloading\|Downloaded" + +script: + - mvn test -Dtest=FuncTestSuite + +after_success: + - bash <(curl -s https://codecov.io/bash) + +env: + global: + - TRAVIS_DIR=assembly/travis diff --git a/assembly/travis/conf/gremlin-server.yaml b/assembly/travis/conf/gremlin-server.yaml new file mode 100644 index 0000000..600e631 --- /dev/null +++ b/assembly/travis/conf/gremlin-server.yaml @@ -0,0 +1,96 @@ +# host and port of gremlin server, need to be consistent with host and port in rest-server.properties +#host: 127.0.0.1 +#port: 8182 + +# timeout in ms of gremlin query +scriptEvaluationTimeout: 30000 + +channelizer: org.apache.tinkerpop.gremlin.server.channel.WsAndHttpChannelizer +graphs: { + hugegraph: conf/hugegraph.properties +} +scriptEngines: { + gremlin-groovy: { + plugins: { + com.baidu.hugegraph.plugin.HugeGraphGremlinPlugin: {}, + org.apache.tinkerpop.gremlin.server.jsr223.GremlinServerGremlinPlugin: {}, + org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: { + classImports: [ + java.lang.Math, + com.baidu.hugegraph.type.define.Directions, + com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser, + com.baidu.hugegraph.traversal.algorithm.CustomizedCrosspointsTraverser, + com.baidu.hugegraph.traversal.algorithm.FusiformSimilarityTraverser, + com.baidu.hugegraph.traversal.algorithm.HugeTraverser, + com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser, + com.baidu.hugegraph.traversal.algorithm.PathsTraverser, + com.baidu.hugegraph.traversal.algorithm.PersonalRankTraverser, + com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser, + com.baidu.hugegraph.traversal.algorithm.SubGraphTraverser, + com.baidu.hugegraph.traversal.optimize.Text, + com.baidu.hugegraph.traversal.optimize.TraversalUtil, + com.baidu.hugegraph.util.DateUtil + ], + methodImports: [java.lang.Math#*] + }, + org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: { + files: [scripts/empty-sample.groovy] + } + } + } +} +serializers: + - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0, + config: { + serializeResultToString: false, + ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry] + } + } + - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1, + config: { + serializeResultToString: false, + ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry] + } + } + - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, + config: { + serializeResultToString: false, + ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry] + } + } + - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV2d0, + config: { + serializeResultToString: false, + ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry] + } + } + - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, + config: { + serializeResultToString: false, + ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry] + } + } +metrics: { + consoleReporter: {enabled: false, interval: 180000}, + csvReporter: {enabled: true, interval: 180000, fileName: /tmp/gremlin-server-metrics.csv}, + jmxReporter: {enabled: false}, + slf4jReporter: {enabled: false, interval: 180000}, + gangliaReporter: {enabled: false, interval: 180000, addressingMode: MULTICAST}, + graphiteReporter: {enabled: false, interval: 180000} +} +maxInitialLineLength: 4096 +maxHeaderSize: 8192 +maxChunkSize: 8192 +maxContentLength: 65536 +maxAccumulationBufferComponents: 1024 +resultIterationBatchSize: 64 +writeBufferLowWaterMark: 32768 +writeBufferHighWaterMark: 65536 +ssl: { + enabled: false +} +authentication: { + authenticator: com.baidu.hugegraph.auth.StandardAuthenticator, + authenticationHandler: com.baidu.hugegraph.auth.WsAndHttpBasicAuthHandler, + config: {tokens: conf/rest-server.properties} +} diff --git a/assembly/travis/conf/hugegraph.properties b/assembly/travis/conf/hugegraph.properties new file mode 100644 index 0000000..9c683e9 --- /dev/null +++ b/assembly/travis/conf/hugegraph.properties @@ -0,0 +1,61 @@ +# gremlin entrence to create graph +gremlin.graph=com.baidu.hugegraph.auth.HugeFactoryAuthProxy + +# cache config +#schema.cache_capacity=100000 +# vertex-cache default is 1000w, 10min expired +#vertex.cache_capacity=10000000 +#vertex.cache_expire=600 +# edge-cache default is 100w, 10min expired +#edge.cache_capacity=1000000 +#edge.cache_expire=600 + + +# schema illegal name template +#schema.illegal_name_regex=\s+|~.* + +#vertex.default_label=vertex + +backend=rocksdb +serializer=binary + +store=hugegraph + +search.text_analyzer=jieba +search.text_analyzer_mode=INDEX + +# rocksdb backend config +#rocksdb.data_path=/path/to/disk +#rocksdb.wal_path=/path/to/disk + + +# cassandra backend config +cassandra.host=localhost +cassandra.port=9042 +cassandra.username= +cassandra.password= +#cassandra.connect_timeout=5 +#cassandra.read_timeout=20 +#cassandra.keyspace.strategy=SimpleStrategy +#cassandra.keyspace.replication=3 + +# hbase backend config +#hbase.hosts=localhost +#hbase.port=2181 +#hbase.znode_parent=/hbsae +#hbase.threads_max=64 + +# mysql backend config +#jdbc.driver=com.mysql.jdbc.Driver +#jdbc.url=jdbc:mysql://127.0.0.1:3306 +#jdbc.username=root +#jdbc.password= +#jdbc.reconnect_max_times=3 +#jdbc.reconnect_interval=3 +#jdbc.sslmode=false + +# palo backend config +#palo.host=127.0.0.1 +#palo.poll_interval=10 +#palo.temp_dir=./palo-data +#palo.file_limit_size=32 diff --git a/assembly/travis/conf/rest-server.properties b/assembly/travis/conf/rest-server.properties new file mode 100644 index 0000000..a9ea865 --- /dev/null +++ b/assembly/travis/conf/rest-server.properties @@ -0,0 +1,12 @@ +# bind url +restserver.url=http://127.0.0.1:8080 +# gremlin server url, need to be consistent with host and port in gremlin-server.yaml +#gremlinserver.url=http://127.0.0.1:8182 + +# graphs list with pair NAME:CONF_PATH +graphs=[hugegraph:conf/hugegraph.properties] + +# authentication +auth.authenticator=com.baidu.hugegraph.auth.StandardAuthenticator +#auth.admin_token= +#auth.user_tokens=[] diff --git a/assembly/travis/install-hugegraph.sh b/assembly/travis/install-hugegraph.sh new file mode 100755 index 0000000..3c4eedf --- /dev/null +++ b/assembly/travis/install-hugegraph.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -ev + +TRAVIS_DIR=`dirname $0` + +if [ $# -ne 1 ]; then + echo "Must pass base branch name of pull request" + exit 1 +fi + +CLIENT_BRANCH=$1 +HUGEGRAPH_BRANCH=$CLIENT_BRANCH + +HUGEGRAPH_GIT_URL="https://github.com/hugegraph/hugegraph.git" + +git clone $HUGEGRAPH_GIT_URL + +cd hugegraph + +git checkout $HUGEGRAPH_BRANCH + +mvn package -DskipTests + +mv hugegraph-*.tar.gz ../ + +cd ../ + +rm -rf hugegraph + +tar -zxvf hugegraph-*.tar.gz + +HTTPS_SERVER_DIR="hugegraph_https" + +mkdir $HTTPS_SERVER_DIR + +cp -r hugegraph-*/. $HTTPS_SERVER_DIR + +cd hugegraph-* + +cp ../$TRAVIS_DIR/conf/* conf + +echo -e "123456" | bin/init-store.sh + +bin/start-hugegraph.sh diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 94b6c82..2df1bf3 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -317,10 +317,9 @@ protected void restoreAccesses() { Access restoreAccess = entry.getValue(); restoreAccess.target(idsMap.get(restoreAccess.target().toString())); restoreAccess.group(idsMap.get(restoreAccess.group().toString())); - Access access = retry(() -> { - return this.client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); - idsMap.put(restoreAccess.id().toString(), access.id().toString()); + retry(() -> { + return this.client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); counts++; } Printer.print("Restore accesses finished, counts is %s !", counts); @@ -332,10 +331,9 @@ protected void restoreBelongs() { Belong restoreBelong = entry.getValue(); restoreBelong.user(idsMap.get(restoreBelong.user().toString())); restoreBelong.group(idsMap.get(restoreBelong.group().toString())); - Belong belong = retry(() -> { - return this.client.authManager().createBelong(restoreBelong); - }, "Restore targets of authority"); - idsMap.put(belong.id().toString(), belong.id().toString()); + retry(() -> { + return this.client.authManager().createBelong(restoreBelong); + }, "Restore targets of authority"); counts++; } Printer.print("Restore belongs finished, counts is %s !", counts); @@ -344,10 +342,11 @@ protected void restoreBelongs() { protected void restoreTargets() { int counts = 0; for (Map.Entry entry: this.targetsByName.entrySet()) { + Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return this.client.authManager().createTarget(entry.getValue()); - }, "Restore targets of authority"); - idsMap.put(target.id().toString(), target.id().toString()); + return this.client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); + idsMap.put(restoreTarget.id().toString(), target.id().toString()); counts++; } Printer.print("Restore targets finished, counts is %s !", counts); @@ -356,10 +355,11 @@ protected void restoreTargets() { protected void restoreGroups() { int counts = 0; for (Map.Entry entry: this.groupsByName.entrySet()) { + Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return this.client.authManager().createGroup(entry.getValue()); - }, "Restore groups of authority"); - idsMap.put(group.id().toString(), group.id().toString()); + return this.client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); + idsMap.put(restoreGroup.id().toString(), group.id().toString()); counts++; } Printer.print("Restore groups finished, counts is %s !", counts); @@ -368,12 +368,12 @@ protected void restoreGroups() { protected void restoreUsers() { int counts = 0; for (Map.Entry entry: this.usersByName.entrySet()) { - User user = entry.getValue(); - user.password(this.initPassword); - User createUser = retry(() -> { - return this.client.authManager().createUser(user); - }, "Restore users of authority"); - idsMap.put(createUser.id().toString(), createUser.id().toString()); + User restoreUser = entry.getValue(); + restoreUser.password(this.initPassword); + User user = retry(() -> { + return this.client.authManager().createUser(restoreUser); + }, "Restore users of authority"); + idsMap.put(restoreUser.id().toString(), user.id().toString()); counts++; } Printer.print("Restore users finished, counts is %s !", counts); diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 802c4b7..0b8d069 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -39,7 +39,7 @@ public class AuthRestoreTest extends AuthTest{ @Before public void init() { ClientUtil initUtil = new ClientUtil(URL, GRAPH, USER_NAME, - USER_PASSWORD, TIME_OUT, PROTOCOL, + USER_PASSWORD, TIME_OUT, TRUST_STORE_FILE, TRUST_STORE_PASSWORD); this.client = initUtil.hugeClient; } @@ -74,21 +74,21 @@ public void testAuthRestoreByAll() { for (User user1 : userList) { userMap.put(user1.name(), user1); } - Assert.assertTrue(userMap.containsKey("test_user123")); + Assert.assertTrue(userMap.containsKey("test_user1")); List groups = this.client.auth().listGroups(); Map groupMap = Maps.newHashMap(); for (Group group : groups) { groupMap.put(group.name(), group); } - Assert.assertTrue(groupMap.containsKey("test_group123")); + Assert.assertTrue(groupMap.containsKey("test_group6")); List targets = this.client.auth().listTargets(); Map targetMap = Maps.newHashMap(); for (Target target : targets) { targetMap.put(target.name(), target); } - Assert.assertTrue(targetMap.containsKey("test_target123")); + Assert.assertTrue(targetMap.containsKey("test_target1")); } @Test @@ -114,7 +114,7 @@ public void testAuthRestoreByUser() { userMap.put(user1.name(), user1); } - Assert.assertTrue(userMap.containsKey("test_user123")); + Assert.assertTrue(userMap.containsKey("test_user1")); } @@ -177,7 +177,7 @@ public void testAuthRestoreByStrategyIgnore() { userMap.put(user1.name(), user1); } - Assert.assertTrue(userMap.containsKey("test_user123")); + Assert.assertTrue(userMap.containsKey("test_user1")); } @Test diff --git a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java index 3837ce1..0bcba1b 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java @@ -28,20 +28,18 @@ public class ClientUtil { private String username; private String password; private Integer timeout; - private String protocol; private String trustStoreFile; private String trustStorePassword; protected HugeClient hugeClient; public ClientUtil(String url, String graph, String username, - String password, Integer timeout, String protocol, + String password, Integer timeout, String trustStoreFile, String trustStorePassword) { this.url = url; this.graph = graph; this.username = username; this.password = password; this.timeout = timeout; - this.protocol = protocol; this.trustStoreFile = trustStoreFile; this.trustStorePassword = trustStorePassword; this.client(); @@ -51,8 +49,7 @@ protected void client() { this.hugeClient = HugeClient.builder(this.url, this.graph) .configUser(this.username, this.password) .configTimeout(this.timeout) - .configSSL(this.protocol, this.trustStoreFile, - this.trustStorePassword) + .configSSL(trustStoreFile, trustStorePassword) .build(); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java b/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java new file mode 100644 index 0000000..0a9a785 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + AuthBackupTest.class, + AuthRestoreTest.class +}) +public class FuncTestSuite { +} diff --git a/src/test/resources/auth/auth_accesses.txt b/src/test/resources/auth/auth_accesses.txt index 2eaadbf..ffae858 100644 --- a/src/test/resources/auth/auth_accesses.txt +++ b/src/test/resources/auth/auth_accesses.txt @@ -1 +1 @@ -{"id":"S-66:test_group123>-88>11>S-66:test_target123","group":"-66:test_group123","target":"-66:test_target123","access_permission":"READ","access_description":"test","access_create":"2020-11-11 15:54:54.008","access_update":"2020-11-18 15:01:13.518","access_creator":"admin"} \ No newline at end of file +{"id":"S-66:test_group6>-88>11>S-66:test_target1","group":"-66:test_group6","target":"-66:test_target1","access_permission":"READ","access_description":"test","access_create":"2020-11-11 15:54:54.008","access_update":"2020-11-18 15:01:13.518","access_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_belongs.txt b/src/test/resources/auth/auth_belongs.txt index 512e4e7..66e8233 100644 --- a/src/test/resources/auth/auth_belongs.txt +++ b/src/test/resources/auth/auth_belongs.txt @@ -1 +1 @@ -{"id":"S-66:test_user123>-82>>S-66:test_group123","user":"-66:test_user123","group":"-66:test_group123","belong_description":"restore test","belong_create":"2020-12-01 09:44:40.117","belong_update":"2020-12-01 09:44:40.117","belong_creator":"admin"} \ No newline at end of file +{"id":"S-66:test_user1>-82>>S-66:test_group6","user":"-66:test_user1","group":"-66:test_group6","belong_description":"restore test","belong_create":"2020-12-01 09:44:40.117","belong_update":"2020-12-01 09:44:40.117","belong_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_groups.txt b/src/test/resources/auth/auth_groups.txt index 1d075ae..3dedb4b 100644 --- a/src/test/resources/auth/auth_groups.txt +++ b/src/test/resources/auth/auth_groups.txt @@ -1 +1 @@ -{"id":"-66:test_group123","group_name":"test_group123","group_description":"user is conflict check user restore","group_create":"2020-11-27 20:08:21.270","group_update":"2020-11-27 20:08:21.270","group_creator":"admin"} \ No newline at end of file +{"id":"-66:test_group6","group_name":"test_group6","group_description":"user is conflict check user restore test test","group_create":"2020-11-27 20:08:21.270","group_update":"2020-11-27 20:08:21.270","group_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_targets.txt b/src/test/resources/auth/auth_targets.txt index 88803f4..96d411f 100644 --- a/src/test/resources/auth/auth_targets.txt +++ b/src/test/resources/auth/auth_targets.txt @@ -1 +1 @@ -{"id":"-66:test_target123","target_name":"test_target123","target_graph":"hugegraph","target_url":"127.0.0.1:8080","target_resources":[{"type":"ALL","label":"*","properties":null}],"target_create":"2020-11-11 15:32:01.192","target_update":"2020-11-11 15:32:01.192","target_creator":"admin"} \ No newline at end of file +{"id":"-66:test_target1","target_name":"test_target1","target_graph":"hugegraph","target_url":"127.0.0.1:8080","target_resources":[{"type":"ALL","label":"*","properties":null}],"target_create":"2020-11-11 15:32:01.192","target_update":"2020-11-11 15:32:01.192","target_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_users.txt b/src/test/resources/auth/auth_users.txt index cdf6acd..233cbb1 100644 --- a/src/test/resources/auth/auth_users.txt +++ b/src/test/resources/auth/auth_users.txt @@ -1 +1 @@ -{"id":"-66:test_user123","user_name":"test_user123","user_password":"$2a$04$vXkz8UYV7Gwagj6zA1ifNuSQfAmzuYb2tXdqDoWKEG.nYVc186JXO","user_create":"2020-11-30 22:26:42.225","user_update":"2020-11-30 22:26:42.225","user_creator":"admin"} \ No newline at end of file +{"id":"-66:test_user1","user_name":"test_user1","user_password":"$2a$04$vXkz8UYV7Gwagj6zA1ifNuSQfAmzuYb2tXdqDoWKEG.nYVc186JXO","user_create":"2020-11-30 22:26:42.225","user_update":"2020-11-30 22:26:42.225","user_creator":"admin"} \ No newline at end of file diff --git a/src/test/resources/auth/auth_users_conflict.txt b/src/test/resources/auth/auth_users_conflict.txt index 1a6c8cc..7dc07c5 100644 --- a/src/test/resources/auth/auth_users_conflict.txt +++ b/src/test/resources/auth/auth_users_conflict.txt @@ -1 +1 @@ -{"id":"-66:test_user123","user_name":"test_user123","user_phone":"13255447788","user_password":"$2a$04$vXkz8UYV7Gwagj6zA1ifNuSQfAmzuYb2tXdqDoWKEG.nYVc186JXO","user_create":"2020-11-30 22:26:42.225","user_update":"2020-11-30 22:26:42.225","user_creator":"admin"} \ No newline at end of file +{"id":"-63:admin","user_name":"admin","user_phone":"13255447788","user_password":"$2a$04$1tl1IKTncjcmMojLdt2qO.EAJ1w0TGunAZ5IJXWwBgPLvTPk366Ly","user_create":"2020-11-11 11:41:12.254","user_update":"2020-11-11 11:41:12.254","user_creator":"system"} \ No newline at end of file From cfc5f5add5484193c594e2ead6d935c7541c5060 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 3 Dec 2020 11:02:10 +0800 Subject: [PATCH 06/26] update some format and some code optimization --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 112 ++++++++++++------ .../com/baidu/hugegraph/cmd/SubCommands.java | 62 +++++++--- .../hugegraph/constant/AuthRestoreFlow.java | 19 ++- .../constant/AuthRestoreStrategy.java | 19 ++- .../hugegraph/manager/AuthBackupManager.java | 22 ++-- .../hugegraph/manager/AuthRestoreManager.java | 67 ++++++----- .../manager/BackupRestoreBaseManager.java | 12 +- .../hugegraph/manager/DumpGraphManager.java | 3 - .../test/functional/AuthBackupTest.java | 9 +- .../test/functional/AuthRestoreTest.java | 20 ++-- .../hugegraph/test/functional/AuthTest.java | 4 - .../hugegraph/test/functional/ClientUtil.java | 1 - .../hugegraph/test/functional/FileUtil.java | 90 ++++++-------- 13 files changed, 259 insertions(+), 181 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 4fb9e24..cd7145e 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -19,6 +19,13 @@ package com.baidu.hugegraph.cmd; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.Scanner; + import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.base.ToolClient.ConnectionInfo; @@ -33,12 +40,6 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParametersDelegate; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import static com.baidu.hugegraph.manager.BackupManager.BACKUP_DEFAULT_TIMEOUT; public class HugeGraphCommand { @@ -338,7 +339,7 @@ private void execute(String subCmd, JCommander jCommander) { taskClear.force()); break; case "auth-backup": - Printer.print("Auth backup start !"); + Printer.print("Auth backup start..."); SubCommands.AuthBackup authBackup = this.subCommand(subCmd); AuthBackupManager authBackupManager = manager(AuthBackupManager.class); @@ -346,7 +347,7 @@ private void execute(String subCmd, JCommander jCommander) { authBackupManager.authBackup(authBackup.types()); break; case "auth-restore": - Printer.print("Auth restore start !"); + Printer.print("Auth restore start..."); SubCommands.AuthRestore authRestore = this.subCommand(subCmd); AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); @@ -428,51 +429,96 @@ private GraphMode mode() { } public static void main(String[] args) { - List list = Arrays.asList(args); - if (!list.contains(TEST_MODE)) { - mainMode(args); - } else { - testMode(args); + HugeGraphCommand cmd = new HugeGraphCommand(); + + JCommander jCommander = parseJCommand(args, cmd); + if (jCommander == null && isTestMode(args)) { + throw new ParameterException( + "Command typing error, " + + "please make sure your command"); + } else if (jCommander == null) { + System.exit(-1); + } + + try { + cmd.execute(jCommander.getParsedCommand(), jCommander); + } catch (Exception e) { + Printer.print("Exception in 'main' is %s", e); + if (!isTestMode(args)) { + if (isPrintStackException()) { + e.printStackTrace(); + } + System.exit(-1); + } else { + throw new RuntimeException( + "Exception in 'main' is", e); + } + } + + if (!isTestMode(args)) { + System.exit(0); } } - public static void mainMode(String[] args) { - HugeGraphCommand cmd = new HugeGraphCommand(); + public static JCommander parseJCommand(String[] args, HugeGraphCommand cmd) { JCommander jCommander = cmd.jCommander(); if (args.length == 0) { jCommander.usage(); - System.exit(-1); + return null; } try { jCommander.parse(args); } catch (ParameterException e) { Printer.print(e.getMessage()); - System.exit(-1); + throw new ParameterException(String.format( + "make sure your command, you can use " + + "command 'hugegraph help'."), e); } String subCommand = jCommander.getParsedCommand(); if (subCommand == null) { - Printer.print("Must provide one sub-command"); - jCommander.usage(); - System.exit(-1); - } - try { - cmd.execute(subCommand, jCommander); - } catch (Exception e) { - Printer.print("Exception in 'main' is %s", e); - System.exit(-1); + printCommonCommand(jCommander); + return null; } - System.exit(0); + + return jCommander; } - public static void testMode(String[] args) { - HugeGraphCommand cmd = new HugeGraphCommand(); - JCommander jCommander = cmd.jCommander(); + private static boolean isTestMode(String[] args) { + List list = Arrays.asList(args); + return list.contains(TEST_MODE); + } - jCommander.parse(args); - String subCommand = jCommander.getParsedCommand(); - cmd.execute(subCommand, jCommander); + public static boolean isPrintStackException() { + System.out.println("Input yes or no to view the stack " + + "information for the exception : "); + Scanner scan = new Scanner(System.in); + while (true) { + String info = scan.nextLine(); + if (info.equalsIgnoreCase("yes")) { + return true; + } else if (info.equalsIgnoreCase("no")) { + return false; + } else { + System.out.println(" Unrecognized command : " + + info); + } + } } + public static void printCommonCommand(JCommander jCommander) { + Printer.print("======================================"); + Printer.print("Warning : must provide one sub-command"); + Printer.print("======================================"); + Printer.print("Here are some common sub-command :"); + Map map = jCommander.getCommands(); + for (String key : map.keySet()) { + Printer.print("||"+key); + } + Printer.print("======================================"); + Printer.print("You can use 'help' to see more detailed " + + "\ncommands, such as './bin/hugegraph help'"); + Printer.print("======================================"); + } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 6aea488..0f031f3 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -19,6 +19,17 @@ package com.baidu.hugegraph.cmd; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; + import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.constant.AuthRestoreStrategy; import com.baidu.hugegraph.manager.TasksManager; @@ -26,16 +37,16 @@ import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; -import com.beust.jcommander.*; +import com.beust.jcommander.DynamicParameter; +import com.beust.jcommander.IParameterValidator; +import com.beust.jcommander.IStringConverter; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.ParameterException; +import com.beust.jcommander.Parameters; +import com.beust.jcommander.ParametersDelegate; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; - -import java.io.File; -import java.io.IOException; -import java.util.*; public class SubCommands { @@ -686,12 +697,13 @@ public static class HugeTypes { @Parameter(names = {"--huge-types", "-t"}, listConverter = HugeTypeListConverter.class, - description = "Type of schema/data. " + - "Concat with ',' if more than one. " + - "'all' means all vertices, edges and schema," + - " in other words, 'all' equals with " + - "'vertex,edge,vertex_label," + - "edge_label,property_key,index_label'") + description = "Type of schema/data. Concat with ',' if more " + + "than one. other types include ‘all’ and ‘schema’" + + " .’all' means all vertices, edges and schema,in " + + "other words, 'all' equals with 'vertex,edge," + + "vertex_label,edge_label,property_key,index_label’." + + " ‘schema’ equals with 'vertex_label,edge_label," + + "property_key,index_label'.") public List types = HugeTypeListConverter.ALL_TYPES; } @@ -883,12 +895,16 @@ public static class AuthRestore extends AuthBackupRestore { @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, - description = "restore strategy, " + - "include: [stop, ignore]") + description = "The strategy that needs to be chosen in the " + + "event of a conflict in restore. strategy include " + + "’stop’ and ‘ignore’, default is ’stop’. ’stop’ means " + + "if there a conflict, stop restore. ‘ignore’ means if " + + "there a conflict, ignore and continue to restore.") public String strategy = AuthStrategyConverter.strategy; @Parameter(names = {"--init-password"}, arity = 1, - description = "init password") + description = "Init user password, if restore type include " + + "‘user’, must init user password.") public String initPassword = StringUtils.EMPTY; public List types() { @@ -949,6 +965,11 @@ public static class HugeTypeListConverter HugeType.VERTEX, HugeType.EDGE ); + public static final List SCHEMA_TYPES = ImmutableList.of( + HugeType.PROPERTY_KEY, HugeType.VERTEX_LABEL, + HugeType.EDGE_LABEL, HugeType.INDEX_LABEL + ); + @Override public List convert(String value) { E.checkArgument(value != null && !value.isEmpty(), @@ -957,6 +978,9 @@ public List convert(String value) { if (types.length == 1 && types[0].equalsIgnoreCase("all")) { return ALL_TYPES; } + if (types.length == 1 && types[0].equalsIgnoreCase("schema")) { + return SCHEMA_TYPES; + } List hugeTypes = new ArrayList<>(); for (String type : types) { try { @@ -973,7 +997,7 @@ public List convert(String value) { } public static class AuthHugeTypeConverter - implements IStringConverter> { + implements IStringConverter> { public static final List AUTH_ALL_TYPES = ImmutableList.of( HugeType.TARGET, HugeType.GROUP, @@ -1005,7 +1029,7 @@ public List convert(String value) { } public static class AuthStrategyConverter - implements IStringConverter { + implements IStringConverter { public static final String strategy = "stop"; @@ -1045,7 +1069,7 @@ public Map convert(String value) { } public static class FileNameToContentConverter - implements IStringConverter { + implements IStringConverter { @Override public String convert(String value) { diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java index efbfb14..e702939 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java @@ -1,6 +1,20 @@ /* - * Copyright 2020 HugeGraph Authors + * Copyright 2017 HugeGraph Authors * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. */ package com.baidu.hugegraph.constant; @@ -8,8 +22,7 @@ public enum AuthRestoreFlow { CHECK(1, "check"), - RESTORE(2, "restore") - ; + RESTORE(2, "restore"); private int code; private String name = null; diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java index c8d19b3..175c43e 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java @@ -1,6 +1,20 @@ /* - * Copyright 2020 HugeGraph Authors + * Copyright 2017 HugeGraph Authors * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. */ package com.baidu.hugegraph.constant; @@ -8,8 +22,7 @@ public enum AuthRestoreStrategy { STOP(1, "stop"), - IGNORE(2, "ignore") - ; + IGNORE(2, "ignore"); private int code; private String name = null; diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java index 7c5d475..d47458d 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 HugeGraph Authors + * Copyright 2017 HugeGraph Authors * backup of authority * operation commands include: * auth-backup @@ -11,6 +11,11 @@ package com.baidu.hugegraph.manager; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.base.HdfsDirectory; import com.baidu.hugegraph.base.LocalDirectory; @@ -22,11 +27,6 @@ import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.JsonUtil; -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; -import java.util.List; -import java.util.Map; - public class AuthBackupManager extends BackupRestoreBaseManager { private static final String AUTH_BACKUP_NAME = "auth-backup"; @@ -68,7 +68,7 @@ public void authBackup(List types) { } protected void backupUsers() { - Printer.print("Users backup started"); + Printer.print("Users backup started..."); List users = retry(this.client.authManager()::listUsers, "querying users of authority"); long writeLines = this.writeText(HugeType.USER, users); @@ -76,7 +76,7 @@ protected void backupUsers() { } protected void backupGroups() { - Printer.print("Groups backup started"); + Printer.print("Groups backup started..."); List groups = retry(this.client.authManager()::listGroups, "querying groups of authority"); long writeLines = this.writeText(HugeType.GROUP, groups); @@ -84,7 +84,7 @@ protected void backupGroups() { } protected void backupTargets() { - Printer.print("Targets backup started"); + Printer.print("Targets backup started..."); List targets = retry(this.client.authManager()::listTargets, "querying targets of authority"); long writeLines = this.writeText(HugeType.TARGET, targets); @@ -92,7 +92,7 @@ protected void backupTargets() { } protected void backupBelongs() { - Printer.print("Belongs backup started"); + Printer.print("Belongs backup started..."); List belongs = retry(this.client.authManager()::listBelongs, "querying belongs of authority"); long writeLines = this.writeText(HugeType.BELONG, belongs); @@ -100,7 +100,7 @@ protected void backupBelongs() { } protected void backupAccesses() { - Printer.print("Accesses backup started"); + Printer.print("Accesses backup started..."); List accesses = retry(this.client.authManager()::listAccesses, "querying accesses of authority"); long writeLines = this.writeText(HugeType.ACCESS, accesses); diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 2df1bf3..3f91380 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -1,20 +1,36 @@ /* - * Copyright 2020 HugeGraph Authors - * restore authority data - * operation commands include: - * auth-restore - * --types : restore data type, the default is 'all', - * include user,group,target,belong,access - * --directory : backup directory, the default is './auth-backup' - * --init-password : if the restore data includes user, - * an initialization password must be set - * --strategy : restore strategy include 'stop' and 'ignore', - * the default is 'stop' + * Copyright 2017 HugeGraph Authors * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. */ package com.baidu.hugegraph.manager; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; + import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.base.HdfsDirectory; import com.baidu.hugegraph.base.LocalDirectory; @@ -31,18 +47,6 @@ import com.beust.jcommander.ParameterException; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.util.Strings; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - public class AuthRestoreManager extends BackupRestoreBaseManager { @@ -166,7 +170,8 @@ protected void checkUseConflict() { } if (conflict > this.conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore users conflict of properties"); + "Restore users conflict with stop strategy, " + + "user name is s%", restoreUser.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, "Restore users strategy is not fund"); @@ -200,7 +205,8 @@ protected void checkGroupsConflict() { } if (conflict > this.conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore groups conflict"); + "Restore groups conflict with stop strategy, " + + "group name is s%", restoreGroup.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, "Restore groups strategy is not fund"); @@ -239,7 +245,8 @@ protected void checkTargetsConflict() { } if (conflict > this.conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore targets conflict with stop strategy"); + "Restore targets conflict with stop strategy, " + + "target name is s%", restoreTarget.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, "Restore targets strategy is not fund"); @@ -272,7 +279,8 @@ protected void checkBelongsConflict() { this.idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore belongs conflict with stop strategy"); + "Restore belongs conflict with stop strategy, " + + "belong id is s%", restoreBelong.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, "Restore belongs strategy is not fund"); @@ -301,7 +309,8 @@ protected void checkAccessesConflict() { this.idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore accesses conflict with stop strategy"); + "Restore accesses conflict with stop strategy," + + "accesses id is s%", restoreAccess.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, "Restore accesses strategy is not fund"); @@ -451,7 +460,5 @@ public void initPassword(List types, String password) { } else { this.initPassword = password; } - } - } diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java index 7092bae..3fdcf43 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java @@ -171,13 +171,13 @@ protected void read(String file, HugeType type, InputStream is = this.inputStream(file); try (InputStreamReader isr = new InputStreamReader(is, API.CHARSET); BufferedReader reader = new BufferedReader(isr)) { - String line; - while ((line = reader.readLine()) != null) { - consumer.accept(type.string(), line); - } + String line; + while ((line = reader.readLine()) != null) { + consumer.accept(type.string(), line); + } } catch (IOException e) { - throw new ToolsException("Failed to deserialize %s from %s", - e, type, file); + throw new ToolsException("Failed to deserialize %s from %s", + e, type, file); } } diff --git a/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java b/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java index 7a9655a..301bef7 100644 --- a/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java @@ -20,10 +20,7 @@ package com.baidu.hugegraph.manager; import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; import java.io.OutputStream; -import java.nio.file.Paths; import java.util.Collection; import java.util.List; diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index c6dd241..3e530c5 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -19,13 +19,14 @@ package com.baidu.hugegraph.test.functional; -import com.baidu.hugegraph.cmd.HugeGraphCommand; -import com.baidu.hugegraph.testutil.Assert; -import com.beust.jcommander.ParameterException; +import java.util.List; + import org.junit.Before; import org.junit.Test; -import java.util.List; +import com.baidu.hugegraph.cmd.HugeGraphCommand; +import com.baidu.hugegraph.testutil.Assert; +import com.beust.jcommander.ParameterException; public class AuthBackupTest extends AuthTest{ diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 0b8d069..22349ff 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -19,18 +19,18 @@ package com.baidu.hugegraph.test.functional; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.driver.HugeClient; import com.baidu.hugegraph.structure.auth.*; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.testutil.Assert; -import com.beust.jcommander.ParameterException; import com.google.common.collect.Maps; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; -import java.util.Map; public class AuthRestoreTest extends AuthTest{ @@ -129,7 +129,7 @@ public void testAuthRestoreWithException() { "--directory", DEFAULT_URL }; - Assert.assertThrows(ParameterException.class, () -> { + Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); }); } @@ -149,7 +149,7 @@ public void testAuthRestoreByStrategyConfict() { "--init-password", "123456" }; - Assert.assertThrows(IllegalArgumentException.class, () -> { + Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); }); } @@ -195,7 +195,7 @@ public void testAuthRestoreByStrategyStop() { "--init-password", "123123" }; - Assert.assertThrows(IllegalArgumentException.class, () -> { + Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); }); } @@ -215,7 +215,7 @@ public void testAuthRestoreByDirectoryException() { "--directory", filePath }; - Assert.assertThrows(IllegalStateException.class, () -> { + Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); }); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java index 7d32d24..8e06563 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java @@ -27,10 +27,6 @@ public class AuthTest { public static final String URL = "http://127.0.0.1:8080"; public static final String GRAPH = "hugegraph"; public static final Integer TIME_OUT = 30; - public static final String PROTOCOL = "http"; public static final String TRUST_STORE_FILE = ""; public static final String TRUST_STORE_PASSWORD = ""; - - - } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java index 0bcba1b..c55aac0 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java @@ -52,5 +52,4 @@ protected void client() { .configSSL(trustStoreFile, trustStorePassword) .build(); } - } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java index bf610ad..6728e06 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java @@ -19,16 +19,22 @@ package com.baidu.hugegraph.test.functional; +import java.io.ByteArrayOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Paths; +import java.util.List; + import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.exception.ToolsException; import com.google.common.collect.Lists; import org.apache.commons.collections.ListUtils; -import java.io.*; -import java.nio.file.Paths; -import java.util.List; - public class FileUtil { protected static final int LBUF_SIZE = 1024; @@ -63,7 +69,7 @@ public static List getFileDirectoryNames(String filePath) { public static void clearFile(String filePath) { File file = new File(filePath); - if(file.exists()) { + if (file.exists()) { String[] files = file.list(); for (int i = 0; i < files.length; i++) { File fileDir = new File(file, files[i]); @@ -74,61 +80,37 @@ public static void clearFile(String filePath) { public static long writeText(String filePath, List list) { long count = 0L; - FileOutputStream os = null; - try { - os = new FileOutputStream(filePath); - ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE); - StringBuilder builder = new StringBuilder(LBUF_SIZE); - - for (Object e : list) { - count++; - builder.append(e).append("\n"); - } - baos.write(builder.toString().getBytes(API.CHARSET)); - os.write(baos.toByteArray()); - } catch (Throwable e) { - throw new ToolsException("Failed writeText file path is %s", - e, filePath); - }finally { - if(os != null) { - try { - os.close(); - }catch (Exception e) { - Printer.print("Failed to close file"); - } - - } - + try (FileOutputStream os = new FileOutputStream(filePath); + ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE)) { + StringBuilder builder = new StringBuilder(LBUF_SIZE); + for (Object e : list) { + count++; + builder.append(e).append("\n"); + } + baos.write(builder.toString().getBytes(API.CHARSET)); + os.write(baos.toByteArray()); + } catch (IOException e) { + throw new ToolsException("Failed writeText file path is %s", + e, filePath); } + return count; } public static List read(String filePath) { List resultList = Lists.newArrayList(); - InputStream is = null; - try { - is = new FileInputStream(filePath); - InputStreamReader isr = new InputStreamReader(is, API.CHARSET); - BufferedReader reader = new BufferedReader(isr); - String line; - while ((line = reader.readLine()) != null) { - resultList.add(line); - } - }catch (Exception e) { - throw new ToolsException("Failed read file path is %s", - e, filePath); - }finally { - if(is != null) { - try { - is.close(); - }catch (Exception e) { - Printer.print("Failed to close file"); - } - - } - + try (InputStream is = new FileInputStream(filePath); + InputStreamReader isr = new InputStreamReader(is, API.CHARSET)) { + BufferedReader reader = new BufferedReader(isr); + String line; + while ((line = reader.readLine()) != null) { + resultList.add(line); + } + } catch (IOException e) { + throw new ToolsException("Failed read file path is %s", + e, filePath); } + return resultList; } - } From a984e857ec1a1609dc6fab33e289d71afce5b359 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 4 Dec 2020 21:55:49 +0800 Subject: [PATCH 07/26] update some format and code optimization --- .../com/baidu/hugegraph/base/ToolClient.java | 14 +- .../com/baidu/hugegraph/base/ToolManager.java | 4 + .../baidu/hugegraph/cmd/HugeGraphCommand.java | 170 +++++++++--------- .../com/baidu/hugegraph/cmd/SubCommands.java | 4 +- .../constant/AuthRestoreStrategy.java | 2 - .../baidu/hugegraph/constant/Constants.java | 29 +++ .../hugegraph/manager/AuthBackupManager.java | 11 +- .../hugegraph/manager/AuthRestoreManager.java | 37 ++-- .../hugegraph/manager/BackupManager.java | 11 +- .../manager/BackupRestoreBaseManager.java | 25 +-- .../hugegraph/manager/DumpGraphManager.java | 22 ++- .../hugegraph/manager/RestoreManager.java | 11 +- .../com/baidu/hugegraph/util/ToolUtil.java | 118 ++++++++++++ .../test/functional/AuthBackupTest.java | 12 +- .../test/functional/AuthRestoreTest.java | 20 +-- 15 files changed, 340 insertions(+), 150 deletions(-) create mode 100644 src/main/java/com/baidu/hugegraph/constant/Constants.java create mode 100644 src/main/java/com/baidu/hugegraph/util/ToolUtil.java diff --git a/src/main/java/com/baidu/hugegraph/base/ToolClient.java b/src/main/java/com/baidu/hugegraph/base/ToolClient.java index a67a541..8516958 100644 --- a/src/main/java/com/baidu/hugegraph/base/ToolClient.java +++ b/src/main/java/com/baidu/hugegraph/base/ToolClient.java @@ -19,6 +19,10 @@ package com.baidu.hugegraph.base; +import java.nio.file.Paths; + +import org.apache.commons.lang3.StringUtils; + import com.baidu.hugegraph.driver.AuthManager; import com.baidu.hugegraph.driver.GraphManager; import com.baidu.hugegraph.driver.GremlinManager; @@ -26,12 +30,8 @@ import com.baidu.hugegraph.driver.SchemaManager; import com.baidu.hugegraph.driver.TaskManager; import com.baidu.hugegraph.driver.TraverserManager; - import com.baidu.hugegraph.util.E; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.lang3.StringUtils; - -import java.nio.file.Paths; public class ToolClient { @@ -118,6 +118,12 @@ public AuthManager authManager() { return this.client.auth(); } + public void close() { + if (this.client != null) { + this.client.close(); + } + } + public static class ConnectionInfo { private String url; diff --git a/src/main/java/com/baidu/hugegraph/base/ToolManager.java b/src/main/java/com/baidu/hugegraph/base/ToolManager.java index f5d00f1..1828d2b 100644 --- a/src/main/java/com/baidu/hugegraph/base/ToolManager.java +++ b/src/main/java/com/baidu/hugegraph/base/ToolManager.java @@ -65,4 +65,8 @@ protected List readList(String key, Class clazz, "Failed to deserialize %s", e, content); } } + + public void close () { + this.client.close(); + } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index cd7145e..7d712d7 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -24,21 +24,33 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import java.util.Scanner; + +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.util.Strings; import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.base.ToolClient.ConnectionInfo; import com.baidu.hugegraph.base.ToolManager; -import com.baidu.hugegraph.manager.*; +import com.baidu.hugegraph.constant.Constants; +import com.baidu.hugegraph.manager.AuthBackupManager; +import com.baidu.hugegraph.manager.AuthRestoreManager; +import com.baidu.hugegraph.manager.BackupManager; +import com.baidu.hugegraph.manager.DumpGraphManager; +import com.baidu.hugegraph.manager.GraphsManager; +import com.baidu.hugegraph.manager.GremlinManager; +import com.baidu.hugegraph.manager.RestoreManager; +import com.baidu.hugegraph.manager.TasksManager; import com.baidu.hugegraph.structure.Task; import com.baidu.hugegraph.structure.constant.GraphMode; import com.baidu.hugegraph.structure.gremlin.Result; import com.baidu.hugegraph.structure.gremlin.ResultSet; import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.ToolUtil; import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParametersDelegate; +import com.google.common.collect.Lists; import static com.baidu.hugegraph.manager.BackupManager.BACKUP_DEFAULT_TIMEOUT; @@ -46,10 +58,12 @@ public class HugeGraphCommand { private static final int DEFAULT_CLEAR_TIMEOUT = 300; - private static final String TEST_MODE = "--test-mode"; - private SubCommands subCommands; + private JCommander jCommander; + + private List taskManager = Lists.newArrayList(); + @ParametersDelegate private SubCommands.Url url = new SubCommands.Url(); @@ -78,6 +92,7 @@ public class HugeGraphCommand { public HugeGraphCommand() { this.subCommands = new SubCommands(); + this.jCommander = jCommander(); } public Map subCommands() { @@ -145,11 +160,11 @@ public void trustStorePassword(String trustStorePassword) { this.trustStorePassword.trustStorePassword = trustStorePassword; } - public String testMode() { + public boolean testMode() { return this.testMode.testMode; } - private void testMode(String testMode) { + private void testMode(boolean testMode) { this.testMode.testMode = testMode; } @@ -181,12 +196,14 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start backup!", this.graph()); SubCommands.Backup backup = this.subCommand(subCmd); BackupManager backupManager = manager(BackupManager.class); + this.taskManager.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); break; case "restore": GraphsManager graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); GraphMode mode = graphsManager.mode(this.graph()); E.checkState(mode.maintaining(), "Invalid mode '%s' of graph '%s' for restore " + @@ -195,6 +212,7 @@ private void execute(String subCmd, JCommander jCommander) { this.graph(), mode); SubCommands.Restore restore = this.subCommand(subCmd); RestoreManager restoreManager = manager(RestoreManager.class); + this.taskManager.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -212,6 +230,7 @@ private void execute(String subCmd, JCommander jCommander) { } backup = convMigrate2Backup(migrate); backupManager = manager(BackupManager.class); + this.taskManager.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); @@ -224,6 +243,7 @@ private void execute(String subCmd, JCommander jCommander) { this.trustStoreFile(migrate.targetTrustStoreFile()); this.trustStorePassword(migrate.targetTrustStorePassword()); graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); GraphMode origin = graphsManager.mode(migrate.targetGraph()); // Set target graph mode mode = migrate.mode(); @@ -237,6 +257,7 @@ private void execute(String subCmd, JCommander jCommander) { String directory = backupManager.directory().directory(); restore = convMigrate2Restore(migrate, directory); restoreManager = manager(RestoreManager.class); + this.taskManager.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -248,6 +269,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start dump!", this.graph()); SubCommands.DumpGraph dump = this.subCommand(subCmd); DumpGraphManager dumpManager = manager(DumpGraphManager.class); + this.taskManager.add(dumpManager); dumpManager.init(dump); dumpManager.dumpFormatter(dump.formatter()); @@ -255,10 +277,12 @@ private void execute(String subCmd, JCommander jCommander) { break; case "graph-list": graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); Printer.printList("Graphs", graphsManager.list()); break; case "graph-get": graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); Printer.printMap("Graph info", graphsManager.get(this.graph())); break; @@ -268,23 +292,27 @@ private void execute(String subCmd, JCommander jCommander) { this.timeout(DEFAULT_CLEAR_TIMEOUT); } graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); graphsManager.clear(this.graph(), graphClear.confirmMessage()); Printer.print("Graph '%s' is cleared", this.graph()); break; case "graph-mode-set": SubCommands.GraphModeSet graphModeSet = this.subCommand(subCmd); graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); graphsManager.mode(this.graph(), graphModeSet.mode()); Printer.print("Set graph '%s' mode to '%s'", this.graph(), graphModeSet.mode()); break; case "graph-mode-get": graphsManager = manager(GraphsManager.class); + this.taskManager.add(graphsManager); Printer.printKV("Graph mode", graphsManager.mode(this.graph())); break; case "gremlin-execute": SubCommands.Gremlin gremlin = this.subCommand(subCmd); GremlinManager gremlinManager = manager(GremlinManager.class); + this.taskManager.add(gremlinManager); Printer.print("Run gremlin script"); ResultSet result = gremlinManager.execute(gremlin.script(), gremlin.bindings(), @@ -298,6 +326,7 @@ private void execute(String subCmd, JCommander jCommander) { case "gremlin-schedule": SubCommands.GremlinJob job = this.subCommand(subCmd); gremlinManager = manager(GremlinManager.class); + this.taskManager.add(gremlinManager); Printer.print("Run gremlin script as async job"); long taskId = gremlinManager.executeAsTask(job.script(), job.bindings(), @@ -307,6 +336,7 @@ private void execute(String subCmd, JCommander jCommander) { case "task-list": SubCommands.TaskList taskList = this.subCommand(subCmd); TasksManager tasksManager = manager(TasksManager.class); + this.taskManager.add(tasksManager); List tasks = tasksManager.list(taskList.status(), taskList.limit()); List results = tasks.stream().map(Task::asMap) @@ -316,24 +346,28 @@ private void execute(String subCmd, JCommander jCommander) { case "task-get": SubCommands.TaskGet taskGet = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); + this.taskManager.add(tasksManager); Printer.printKV("Task info", tasksManager.get(taskGet.taskId()).asMap()); break; case "task-delete": SubCommands.TaskDelete taskDelete = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); + this.taskManager.add(tasksManager); tasksManager.delete(taskDelete.taskId()); Printer.print("Task '%s' is deleted", taskDelete.taskId()); break; case "task-cancel": SubCommands.TaskCancel taskCancel = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); + this.taskManager.add(tasksManager); tasksManager.cancel(taskCancel.taskId()); Printer.print("Task '%s' is cancelled", taskCancel.taskId()); break; case "task-clear": SubCommands.TaskClear taskClear = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); + this.taskManager.add(tasksManager); tasksManager.clear(taskClear.force()); Printer.print("Tasks are cleared[force=%s]", taskClear.force()); @@ -342,6 +376,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth backup start..."); SubCommands.AuthBackup authBackup = this.subCommand(subCmd); AuthBackupManager authBackupManager = manager(AuthBackupManager.class); + this.taskManager.add(authBackupManager); authBackupManager.init(authBackup); authBackupManager.authBackup(authBackup.types()); @@ -350,6 +385,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth restore start..."); SubCommands.AuthRestore authRestore = this.subCommand(subCmd); AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); + this.taskManager.add(authRestoreManager); authRestoreManager.init(authRestore); authRestoreManager.authRestore(authRestore.types()); @@ -363,6 +399,11 @@ private void execute(String subCmd, JCommander jCommander) { } } + private void execute(String[] args) { + this.parseCommand(args); + this.execute(this.jCommander.getParsedCommand(), this.jCommander); + } + private void checkMainParams() { E.checkArgument(this.url() != null, "Url can't be null"); E.checkArgument(this.username() == null && this.password() == null || @@ -428,97 +469,56 @@ private GraphMode mode() { return mode; } - public static void main(String[] args) { - HugeGraphCommand cmd = new HugeGraphCommand(); - - JCommander jCommander = parseJCommand(args, cmd); - if (jCommander == null && isTestMode(args)) { - throw new ParameterException( - "Command typing error, " + - "please make sure your command"); - } else if (jCommander == null) { - System.exit(-1); - } - - try { - cmd.execute(jCommander.getParsedCommand(), jCommander); - } catch (Exception e) { - Printer.print("Exception in 'main' is %s", e); - if (!isTestMode(args)) { - if (isPrintStackException()) { - e.printStackTrace(); - } - System.exit(-1); - } else { - throw new RuntimeException( - "Exception in 'main' is", e); - } - } - - if (!isTestMode(args)) { - System.exit(0); - } - } - - public static JCommander parseJCommand(String[] args, HugeGraphCommand cmd) { - JCommander jCommander = cmd.jCommander(); - + public void parseCommand(String[] args) { if (args.length == 0) { - jCommander.usage(); - return null; + ToolUtil.exitWithUsageOrThrow(this.jCommander, + Constants.EXIT_CODE_ERROR, + this.testMode()); } - try { - jCommander.parse(args); - } catch (ParameterException e) { - Printer.print(e.getMessage()); - throw new ParameterException(String.format( - "make sure your command, you can use " + - "command 'hugegraph help'."), e); - } - - String subCommand = jCommander.getParsedCommand(); + this.parseCommandForHelp(args); + this.jCommander.parse(args); + String subCommand = this.jCommander.getParsedCommand(); if (subCommand == null) { - printCommonCommand(jCommander); - return null; + ToolUtil.printCommandsCategory(this.jCommander); + ToolUtil.exitOrThrow(Constants.EXIT_CODE_ERROR, this.testMode()); } - return jCommander; } - private static boolean isTestMode(String[] args) { + public void parseCommandForHelp(String[] args) { + String helpValue = Strings.EMPTY; List list = Arrays.asList(args); - return list.contains(TEST_MODE); - } - public static boolean isPrintStackException() { - System.out.println("Input yes or no to view the stack " + - "information for the exception : "); - Scanner scan = new Scanner(System.in); - while (true) { - String info = scan.nextLine(); - if (info.equalsIgnoreCase("yes")) { - return true; - } else if (info.equalsIgnoreCase("no")) { - return false; - } else { - System.out.println(" Unrecognized command : " - + info); - } + if (list.contains(Constants.COMMAND_HELP) && + list.size() > list.indexOf(Constants.COMMAND_HELP) + 1) { + helpValue = list.get(list.indexOf(Constants.COMMAND_HELP) + 1); + } + + Map commanderMap = this.jCommander.getCommands(); + if (StringUtils.isNotEmpty(helpValue)) { + if (commanderMap.containsKey(helpValue)) { + ToolUtil.exitWithUsageOrThrow(commanderMap.get(helpValue), + Constants.EXIT_CODE_ERROR, + this.testMode()); + } else { + throw new ParameterException(String.format( + "Unexpected help sub-command %s", helpValue)); + } } } - public static void printCommonCommand(JCommander jCommander) { - Printer.print("======================================"); - Printer.print("Warning : must provide one sub-command"); - Printer.print("======================================"); - Printer.print("Here are some common sub-command :"); - Map map = jCommander.getCommands(); - for (String key : map.keySet()) { - Printer.print("||"+key); + public static void main(String[] args) { + HugeGraphCommand cmd = null; + try { + cmd = new HugeGraphCommand(); + cmd.execute(args); + } catch (Throwable e) { + ToolUtil.printException(e, cmd != null && + cmd.testMode()); + } finally { + ToolUtil.shutdown(cmd == null ? + null : + cmd.taskManager); } - Printer.print("======================================"); - Printer.print("You can use 'help' to see more detailed " + - "\ncommands, such as './bin/hugegraph help'"); - Printer.print("======================================"); } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 0f031f3..f04bf99 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -689,8 +689,8 @@ public static class TrustStorePassword { public static class TestMode { @Parameter(names = {"--test-mode"}, arity = 1, - description = "Test mode") - public String testMode = "test"; + description = "Whether the hugegraph-tools work in test mode") + public boolean testMode = false; } public static class HugeTypes { diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java index 175c43e..046bf6c 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java @@ -50,6 +50,4 @@ public static AuthRestoreStrategy getEnumByName(String name) { public String string() { return this.name; } - - } diff --git a/src/main/java/com/baidu/hugegraph/constant/Constants.java b/src/main/java/com/baidu/hugegraph/constant/Constants.java new file mode 100644 index 0000000..3c7806e --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/constant/Constants.java @@ -0,0 +1,29 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.constant; + +public final class Constants { + + public static final int EXIT_CODE_ERROR = -1; + + public static final String INPUT_YES = "yes"; + public static final String INPUT_Y= "y"; + public static final String COMMAND_HELP = "help"; +} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java index d47458d..46ab9dc 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -42,6 +42,16 @@ public void init(SubCommands.AuthBackup authBackup) { } public void authBackup(List types) { + try { + this.doAuthBackup(types); + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); + } + } + + public void doAuthBackup(List types) { for (HugeType type : types) { switch (type) { case USER: @@ -64,7 +74,6 @@ public void authBackup(List types) { "Bad backup type: %s", type)); } } - this.shutdown(this.type()); } protected void backupUsers() { diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 3f91380..7d272fc 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -88,11 +88,17 @@ public void init(SubCommands.AuthRestore authRestore) { public void authRestore(List types) { List sortedHugeTypes = this.sortListByCode(types); - this.restore(sortedHugeTypes, AuthRestoreFlow.CHECK.code()); - this.restore(sortedHugeTypes, AuthRestoreFlow.RESTORE.code()); + try { + this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.CHECK.code()); + this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.RESTORE.code()); + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); + } } - public void restore(List types, int status) { + public void doAuthRestore(List types, int status) { for (HugeType type : types) { switch (type) { case USER: @@ -135,9 +141,6 @@ public void restore(List types, int status) { "Bad restore type: %s", type)); } } - if (status == AuthRestoreFlow.RESTORE.code()) { - this.shutdown(this.type()); - } } protected void checkUseConflict() { @@ -322,10 +325,10 @@ protected void checkAccessesConflict() { protected void restoreAccesses() { int counts = 0; - for (Map.Entry entry : accessesByName.entrySet()) { + for (Map.Entry entry : this.accessesByName.entrySet()) { Access restoreAccess = entry.getValue(); - restoreAccess.target(idsMap.get(restoreAccess.target().toString())); - restoreAccess.group(idsMap.get(restoreAccess.group().toString())); + restoreAccess.target(this.idsMap.get(restoreAccess.target().toString())); + restoreAccess.group(this.idsMap.get(restoreAccess.group().toString())); retry(() -> { return this.client.authManager().createAccess(restoreAccess); }, "Restore access of authority"); @@ -336,10 +339,10 @@ protected void restoreAccesses() { protected void restoreBelongs() { int counts = 0; - for (Map.Entry entry : belongsByName.entrySet()) { + for (Map.Entry entry : this.belongsByName.entrySet()) { Belong restoreBelong = entry.getValue(); - restoreBelong.user(idsMap.get(restoreBelong.user().toString())); - restoreBelong.group(idsMap.get(restoreBelong.group().toString())); + restoreBelong.user(this.idsMap.get(restoreBelong.user().toString())); + restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); retry(() -> { return this.client.authManager().createBelong(restoreBelong); }, "Restore targets of authority"); @@ -355,7 +358,7 @@ protected void restoreTargets() { Target target = retry(() -> { return this.client.authManager().createTarget(restoreTarget); }, "Restore targets of authority"); - idsMap.put(restoreTarget.id().toString(), target.id().toString()); + this.idsMap.put(restoreTarget.id().toString(), target.id().toString()); counts++; } Printer.print("Restore targets finished, counts is %s !", counts); @@ -368,7 +371,7 @@ protected void restoreGroups() { Group group = retry(() -> { return this.client.authManager().createGroup(restoreGroup); }, "Restore groups of authority"); - idsMap.put(restoreGroup.id().toString(), group.id().toString()); + this.idsMap.put(restoreGroup.id().toString(), group.id().toString()); counts++; } Printer.print("Restore groups finished, counts is %s !", counts); @@ -382,7 +385,7 @@ protected void restoreUsers() { User user = retry(() -> { return this.client.authManager().createUser(restoreUser); }, "Restore users of authority"); - idsMap.put(restoreUser.id().toString(), user.id().toString()); + this.idsMap.put(restoreUser.id().toString(), user.id().toString()); counts++; } Printer.print("Restore users finished, counts is %s !", counts); @@ -404,8 +407,8 @@ protected void prepareUserRestore(User restoreUser) { } private boolean checkIdMaps(String oneId, String otherId) { - if (!idsMap.containsKey(oneId) || - !idsMap.containsKey(otherId)) { + if (!this.idsMap.containsKey(oneId) || + !this.idsMap.containsKey(otherId)) { return true; } return false; diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupManager.java index 7e9e237..c08ed93 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupManager.java @@ -120,6 +120,16 @@ public long splitSize() { } public void backup(List types) { + try { + this.doBackup(types); + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); + } + } + + public void doBackup(List types) { this.startTimer(); for (HugeType type : types) { switch (type) { @@ -146,7 +156,6 @@ public void backup(List types) { "Bad backup type: %s", type)); } } - this.shutdown(this.type()); this.printSummary(); } diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java index 3fdcf43..6578e2c 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java @@ -19,6 +19,21 @@ package com.baidu.hugegraph.manager; +import static com.baidu.hugegraph.base.Directory.closeAndIgnoreException; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; + import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.base.*; import com.baidu.hugegraph.cmd.SubCommands; @@ -30,16 +45,6 @@ import com.baidu.hugegraph.util.E; import com.google.common.collect.ImmutableMap; -import java.io.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.BiConsumer; - -import static com.baidu.hugegraph.base.Directory.closeAndIgnoreException; - public class BackupRestoreBaseManager extends RetryManager { public static final int BATCH = 500; diff --git a/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java b/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java index 301bef7..fe65d60 100644 --- a/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/DumpGraphManager.java @@ -71,17 +71,21 @@ public void init(SubCommands.DumpGraph dump) { public void dump() { this.startTimer(); - - // Fetch data to JsonGraph - this.backupVertices(); - this.backupEdges(); - - // Dump to file - for (String table : this.graph.tables()) { - this.submit(() -> dump(table, this.graph.table(table).values())); + try { + // Fetch data to JsonGraph + this.backupVertices(); + this.backupEdges(); + + // Dump to file + for (String table : this.graph.tables()) { + this.submit(() -> dump(table, this.graph.table(table).values())); + } + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); } - this.shutdown(this.type()); this.printSummary("dump graph"); } diff --git a/src/main/java/com/baidu/hugegraph/manager/RestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/RestoreManager.java index 713848b..91ac811 100644 --- a/src/main/java/com/baidu/hugegraph/manager/RestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/RestoreManager.java @@ -60,6 +60,16 @@ public void mode(GraphMode mode) { } public void restore(List types) { + try { + this.doRestore(types); + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); + } + } + + public void doRestore(List types) { E.checkNotNull(this.mode, "mode"); this.startTimer(); for (HugeType type : types) { @@ -87,7 +97,6 @@ public void restore(List types) { "Bad restore type: %s", type)); } } - this.shutdown(this.type()); this.printSummary(); if (this.clean) { this.removeDirectory(); diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java new file mode 100644 index 0000000..51f668d --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -0,0 +1,118 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.util; + +import java.lang.reflect.UndeclaredThrowableException; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import org.apache.commons.collections.CollectionUtils; + +import com.baidu.hugegraph.base.Printer; +import com.baidu.hugegraph.base.ToolManager; +import com.baidu.hugegraph.constant.Constants; +import com.beust.jcommander.JCommander; +import com.beust.jcommander.ParameterException; + +public final class ToolUtil { + + public static void shutdown(List taskManager) { + if (CollectionUtils.isEmpty(taskManager)) { + return; + } + for (ToolManager toolManager : taskManager) { + toolManager.close(); + } + } + + public static void printException(Throwable e, + boolean isTestMode) { + Printer.print("Failed to execute %s", e.getMessage()); + if (isTestMode) { + throw new RuntimeException(e); + } + if (isPrintStackException()) { + e.printStackTrace(); + } + } + + public static boolean isPrintStackException() { + System.out.println("Type y(yes) to print exception stack[default n]?"); + Scanner scan = new Scanner(System.in); + String inputInfomation = scan.nextLine(); + + if (inputInfomation.equalsIgnoreCase(Constants.INPUT_YES) || + inputInfomation.equalsIgnoreCase(Constants.INPUT_Y)) { + return true; + } + + return false; + } + + public static void exitWithUsageOrThrow(JCommander commander, + int code, + boolean isTestNode) { + if (isTestNode) { + throw new ParameterException( + "Failed to parse command"); + } + commander.usage(); + System.exit(code); + } + + public static void exitOrThrow(int code, + boolean isTestNode) { + if (isTestNode) { + throw new ParameterException( + "Failed to parse command"); + } + System.exit(code); + } + + public static RuntimeException targetRuntimeException(Throwable t) { + Throwable e; + if (t instanceof UndeclaredThrowableException) { + e = ((UndeclaredThrowableException) t).getUndeclaredThrowable() + .getCause(); + } else { + e = t; + } + if (e instanceof RuntimeException) { + return (RuntimeException) e; + } + return new RuntimeException(e); + } + + public static void printCommandsCategory(JCommander jCommander) { + Printer.print("======================================"); + Printer.print("Warning : must provide one sub-command"); + Printer.print("======================================"); + Printer.print("Here are some sub-command :"); + Map map = jCommander.getCommands(); + for (String key : map.keySet()) { + Printer.print("||" + key); + } + Printer.print("======================================"); + Printer.print("Can use 'hugegraph help' or " + + "\n'hugegraph help sub-command'"); + Printer.print("======================================"); + } +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 3e530c5..2273319 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -26,7 +26,6 @@ import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.testutil.Assert; -import com.beust.jcommander.ParameterException; public class AuthBackupTest extends AuthTest{ @@ -38,7 +37,7 @@ public void init() { @Test public void testAuthBackup() { String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup" @@ -54,7 +53,7 @@ public void testAuthBackup() { @Test public void testAuthBackupByTypes() { String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", @@ -71,14 +70,14 @@ public void testAuthBackupByTypes() { @Test public void testAuthBackupByTypesWithException() { String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", "--types", "user,group,test" }; - Assert.assertThrows(ParameterException.class, () -> { + Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); }); } @@ -87,7 +86,7 @@ public void testAuthBackupByTypesWithException() { public void testAuthBackupByDirectory() { String directory = "./backup"; String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", @@ -100,5 +99,4 @@ public void testAuthBackupByDirectory() { List fileNames = FileUtil.getFileDirectoryNames(directory); Assert.assertTrue(fileNames.size() == 5); } - } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 22349ff..00849cf 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -58,7 +58,7 @@ public void testAuthRestoreByAll() { "/auth/auth_accesses.txt"); String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -79,14 +79,14 @@ public void testAuthRestoreByAll() { List groups = this.client.auth().listGroups(); Map groupMap = Maps.newHashMap(); for (Group group : groups) { - groupMap.put(group.name(), group); + groupMap.put(group.name(), group); } Assert.assertTrue(groupMap.containsKey("test_group6")); List targets = this.client.auth().listTargets(); Map targetMap = Maps.newHashMap(); for (Target target : targets) { - targetMap.put(target.name(), target); + targetMap.put(target.name(), target); } Assert.assertTrue(targetMap.containsKey("test_target1")); } @@ -97,7 +97,7 @@ public void testAuthRestoreByUser() { "/auth/auth_users.txt"); String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -115,13 +115,12 @@ public void testAuthRestoreByUser() { } Assert.assertTrue(userMap.containsKey("test_user1")); - } @Test public void testAuthRestoreWithException() { String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -140,7 +139,7 @@ public void testAuthRestoreByStrategyConfict() { "/auth/auth_users_conflict.txt"); String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -160,7 +159,7 @@ public void testAuthRestoreByStrategyIgnore() { "/auth/auth_users_conflict.txt"); String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -186,7 +185,7 @@ public void testAuthRestoreByStrategyStop() { "/auth/auth_users_conflict.txt"); String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -205,7 +204,7 @@ public void testAuthRestoreByDirectoryException() { String filePath = "./auth-test-test"; String[] args = new String[]{ - "--test-mode", "test", + "--test-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -225,5 +224,4 @@ private void loadData(String restoreFilePath, String dataFilePath) { FileUtil.writeText(restoreFilePath, list); } - } From 8bb22f7a8b60ea03841a7b60a1fd91941b2be76e Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 7 Dec 2020 20:14:14 +0800 Subject: [PATCH 08/26] update code format --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 46 +++++++++---------- .../com/baidu/hugegraph/cmd/SubCommands.java | 19 ++++---- .../hugegraph/manager/AuthBackupManager.java | 31 +++++++++---- .../hugegraph/manager/AuthRestoreManager.java | 25 ++++------ .../manager/BackupRestoreBaseManager.java | 19 +++++--- .../com/baidu/hugegraph/util/ToolUtil.java | 12 ++--- 6 files changed, 81 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 7d712d7..ce12222 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -62,7 +62,7 @@ public class HugeGraphCommand { private JCommander jCommander; - private List taskManager = Lists.newArrayList(); + private List taskManagers = Lists.newArrayList(); @ParametersDelegate private SubCommands.Url url = new SubCommands.Url(); @@ -196,14 +196,14 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start backup!", this.graph()); SubCommands.Backup backup = this.subCommand(subCmd); BackupManager backupManager = manager(BackupManager.class); - this.taskManager.add(backupManager); + this.taskManagers.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); break; case "restore": GraphsManager graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); GraphMode mode = graphsManager.mode(this.graph()); E.checkState(mode.maintaining(), "Invalid mode '%s' of graph '%s' for restore " + @@ -212,7 +212,7 @@ private void execute(String subCmd, JCommander jCommander) { this.graph(), mode); SubCommands.Restore restore = this.subCommand(subCmd); RestoreManager restoreManager = manager(RestoreManager.class); - this.taskManager.add(restoreManager); + this.taskManagers.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -230,7 +230,7 @@ private void execute(String subCmd, JCommander jCommander) { } backup = convMigrate2Backup(migrate); backupManager = manager(BackupManager.class); - this.taskManager.add(backupManager); + this.taskManagers.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); @@ -243,7 +243,7 @@ private void execute(String subCmd, JCommander jCommander) { this.trustStoreFile(migrate.targetTrustStoreFile()); this.trustStorePassword(migrate.targetTrustStorePassword()); graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); GraphMode origin = graphsManager.mode(migrate.targetGraph()); // Set target graph mode mode = migrate.mode(); @@ -257,7 +257,7 @@ private void execute(String subCmd, JCommander jCommander) { String directory = backupManager.directory().directory(); restore = convMigrate2Restore(migrate, directory); restoreManager = manager(RestoreManager.class); - this.taskManager.add(restoreManager); + this.taskManagers.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -269,7 +269,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start dump!", this.graph()); SubCommands.DumpGraph dump = this.subCommand(subCmd); DumpGraphManager dumpManager = manager(DumpGraphManager.class); - this.taskManager.add(dumpManager); + this.taskManagers.add(dumpManager); dumpManager.init(dump); dumpManager.dumpFormatter(dump.formatter()); @@ -277,12 +277,12 @@ private void execute(String subCmd, JCommander jCommander) { break; case "graph-list": graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); Printer.printList("Graphs", graphsManager.list()); break; case "graph-get": graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); Printer.printMap("Graph info", graphsManager.get(this.graph())); break; @@ -292,27 +292,27 @@ private void execute(String subCmd, JCommander jCommander) { this.timeout(DEFAULT_CLEAR_TIMEOUT); } graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); graphsManager.clear(this.graph(), graphClear.confirmMessage()); Printer.print("Graph '%s' is cleared", this.graph()); break; case "graph-mode-set": SubCommands.GraphModeSet graphModeSet = this.subCommand(subCmd); graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); graphsManager.mode(this.graph(), graphModeSet.mode()); Printer.print("Set graph '%s' mode to '%s'", this.graph(), graphModeSet.mode()); break; case "graph-mode-get": graphsManager = manager(GraphsManager.class); - this.taskManager.add(graphsManager); + this.taskManagers.add(graphsManager); Printer.printKV("Graph mode", graphsManager.mode(this.graph())); break; case "gremlin-execute": SubCommands.Gremlin gremlin = this.subCommand(subCmd); GremlinManager gremlinManager = manager(GremlinManager.class); - this.taskManager.add(gremlinManager); + this.taskManagers.add(gremlinManager); Printer.print("Run gremlin script"); ResultSet result = gremlinManager.execute(gremlin.script(), gremlin.bindings(), @@ -326,7 +326,7 @@ private void execute(String subCmd, JCommander jCommander) { case "gremlin-schedule": SubCommands.GremlinJob job = this.subCommand(subCmd); gremlinManager = manager(GremlinManager.class); - this.taskManager.add(gremlinManager); + this.taskManagers.add(gremlinManager); Printer.print("Run gremlin script as async job"); long taskId = gremlinManager.executeAsTask(job.script(), job.bindings(), @@ -336,7 +336,7 @@ private void execute(String subCmd, JCommander jCommander) { case "task-list": SubCommands.TaskList taskList = this.subCommand(subCmd); TasksManager tasksManager = manager(TasksManager.class); - this.taskManager.add(tasksManager); + this.taskManagers.add(tasksManager); List tasks = tasksManager.list(taskList.status(), taskList.limit()); List results = tasks.stream().map(Task::asMap) @@ -346,28 +346,28 @@ private void execute(String subCmd, JCommander jCommander) { case "task-get": SubCommands.TaskGet taskGet = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManager.add(tasksManager); + this.taskManagers.add(tasksManager); Printer.printKV("Task info", tasksManager.get(taskGet.taskId()).asMap()); break; case "task-delete": SubCommands.TaskDelete taskDelete = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManager.add(tasksManager); + this.taskManagers.add(tasksManager); tasksManager.delete(taskDelete.taskId()); Printer.print("Task '%s' is deleted", taskDelete.taskId()); break; case "task-cancel": SubCommands.TaskCancel taskCancel = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManager.add(tasksManager); + this.taskManagers.add(tasksManager); tasksManager.cancel(taskCancel.taskId()); Printer.print("Task '%s' is cancelled", taskCancel.taskId()); break; case "task-clear": SubCommands.TaskClear taskClear = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManager.add(tasksManager); + this.taskManagers.add(tasksManager); tasksManager.clear(taskClear.force()); Printer.print("Tasks are cleared[force=%s]", taskClear.force()); @@ -376,7 +376,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth backup start..."); SubCommands.AuthBackup authBackup = this.subCommand(subCmd); AuthBackupManager authBackupManager = manager(AuthBackupManager.class); - this.taskManager.add(authBackupManager); + this.taskManagers.add(authBackupManager); authBackupManager.init(authBackup); authBackupManager.authBackup(authBackup.types()); @@ -385,7 +385,7 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth restore start..."); SubCommands.AuthRestore authRestore = this.subCommand(subCmd); AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); - this.taskManager.add(authRestoreManager); + this.taskManagers.add(authRestoreManager); authRestoreManager.init(authRestore); authRestoreManager.authRestore(authRestore.types()); @@ -518,7 +518,7 @@ public static void main(String[] args) { } finally { ToolUtil.shutdown(cmd == null ? null : - cmd.taskManager); + cmd.taskManagers); } } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index f04bf99..cda1765 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -699,11 +699,11 @@ public static class HugeTypes { listConverter = HugeTypeListConverter.class, description = "Type of schema/data. Concat with ',' if more " + "than one. other types include ‘all’ and ‘schema’" + - " .’all' means all vertices, edges and schema,in " + - "other words, 'all' equals with 'vertex,edge," + - "vertex_label,edge_label,property_key,index_label’." + - " ‘schema’ equals with 'vertex_label,edge_label," + - "property_key,index_label'.") + " .’all' means all vertices, edges and schema, in " + + "other words, 'all' equals with 'vertex, edge, " + + "vertex_label, edge_label, property_key, index_label’." + + " ‘schema’ equals with 'vertex_label, edge_label, " + + "property_key, index_label'.") public List types = HugeTypeListConverter.ALL_TYPES; } @@ -859,6 +859,10 @@ public void directory(String directory) { public void hdfsConf(Map hdfsConf) { this.hdfsConf = hdfsConf; } + + public void retry(int retry) { + this.retry.retry = retry; + } } public static class AuthBackup extends AuthBackupRestore { @@ -895,7 +899,7 @@ public static class AuthRestore extends AuthBackupRestore { @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, - description = "The strategy that needs to be chosen in the " + + description = "Valid Strategies that needs to be chosen in the " + "event of a conflict in restore. strategy include " + "’stop’ and ‘ignore’, default is ’stop’. ’stop’ means " + "if there a conflict, stop restore. ‘ignore’ means if " + @@ -930,7 +934,6 @@ public String initPassword() { public void initPassword(String initPassword) { this.initPassword = initPassword; } - } public static class AuthTypes { @@ -941,7 +944,7 @@ public static class AuthTypes { "Concat with ',' if more than one. " + "'all' means all auth information" + " in other words, 'all' equals with " + - "'user,group,target,belong,access'") + "'user, group, target, belong, access'") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java index 46ab9dc..4ad44af 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -1,12 +1,20 @@ /* * Copyright 2017 HugeGraph Authors - * backup of authority - * operation commands include: - * auth-backup - * --types : backup data type, the default is 'all', - * include user,group,target,belong,access - * --directory : backup directory, the default is './auth-backup' * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. */ package com.baidu.hugegraph.manager; @@ -23,7 +31,11 @@ import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.cmd.SubCommands; import com.baidu.hugegraph.exception.ToolsException; -import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.auth.Access; +import com.baidu.hugegraph.structure.auth.Belong; +import com.baidu.hugegraph.structure.auth.Group; +import com.baidu.hugegraph.structure.auth.Target; +import com.baidu.hugegraph.structure.auth.User; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.JsonUtil; @@ -71,7 +83,7 @@ public void doAuthBackup(List types) { break; default: throw new AssertionError(String.format( - "Bad backup type: %s", type)); + "Bad auth backup type: %s", type)); } } } @@ -131,7 +143,7 @@ protected long writeText(HugeType type, List list) { os.write(baos.toByteArray()); } catch (Throwable e) { throw new ToolsException("Failed to serialize %s to %s", - e, type, type.string()); + e, list, type.string()); } return count; } @@ -146,5 +158,4 @@ protected void directory(String dir, Map hdfsConf) { hdfsConf); } } - } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 7d272fc..2c55a8d 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; import com.baidu.hugegraph.api.API; @@ -40,7 +39,11 @@ import com.baidu.hugegraph.constant.AuthRestoreFlow; import com.baidu.hugegraph.constant.AuthRestoreStrategy; import com.baidu.hugegraph.exception.ToolsException; -import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.auth.Access; +import com.baidu.hugegraph.structure.auth.Belong; +import com.baidu.hugegraph.structure.auth.Group; +import com.baidu.hugegraph.structure.auth.Target; +import com.baidu.hugegraph.structure.auth.User; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.JsonUtil; @@ -67,7 +70,6 @@ public class AuthRestoreManager extends BackupRestoreBaseManager { private Map belongsByName; private Map accessesByName; - public AuthRestoreManager(ToolClient.ConnectionInfo info) { super(info, AUTH_RESTORE_DIR); } @@ -76,7 +78,7 @@ public void init(SubCommands.AuthRestore authRestore) { this.retry(authRestore.retry()); this.directory(authRestore.directory(), authRestore.hdfsConf()); this.ensureDirectoryExist(false); - this.initStrategy(authRestore.strategy()); + this.strategy = AuthRestoreStrategy.getEnumByName(authRestore.strategy()); this.initPassword(authRestore.types(), authRestore.initPassword()); this.idsMap = Maps.newHashMap(); this.usersByName = Maps.newHashMap(); @@ -138,7 +140,7 @@ public void doAuthRestore(List types, int status) { break; default: throw new AssertionError(String.format( - "Bad restore type: %s", type)); + "Bad auth restore type: %s", type)); } } } @@ -425,7 +427,7 @@ protected List read(HugeType type) { } } catch (IOException e) { throw new ToolsException("Failed to deserialize %s from %s", - e, type, type.string()); + e, resultList, type.string()); } return resultList; } @@ -437,16 +439,7 @@ protected void directory(String dir, Map hdfsConf) { } else { // HDFS directory super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_NAME, - hdfsConf); - } - } - - private void initStrategy(String strategy) { - if (!StringUtils.isEmpty(strategy)) { - this.strategy = AuthRestoreStrategy.getEnumByName(strategy); - } else { - throw new ParameterException(String.format( - "Bad restore strategy: %s", strategy)); + hdfsConf); } } diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java index 6578e2c..d2b38f7 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java @@ -35,7 +35,12 @@ import java.util.function.BiConsumer; import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.base.*; +import com.baidu.hugegraph.base.Directory; +import com.baidu.hugegraph.base.HdfsDirectory; +import com.baidu.hugegraph.base.LocalDirectory; +import com.baidu.hugegraph.base.Printer; +import com.baidu.hugegraph.base.RetryManager; +import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.cmd.SubCommands; import com.baidu.hugegraph.concurrent.KeyLock; import com.baidu.hugegraph.exception.ToolsException; @@ -176,13 +181,13 @@ protected void read(String file, HugeType type, InputStream is = this.inputStream(file); try (InputStreamReader isr = new InputStreamReader(is, API.CHARSET); BufferedReader reader = new BufferedReader(isr)) { - String line; - while ((line = reader.readLine()) != null) { - consumer.accept(type.string(), line); - } + String line; + while ((line = reader.readLine()) != null) { + consumer.accept(type.string(), line); + } } catch (IOException e) { - throw new ToolsException("Failed to deserialize %s from %s", - e, type, file); + throw new ToolsException("Failed to deserialize %s from %s", + e, type, file); } } diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index 51f668d..5021bc5 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -34,11 +34,11 @@ public final class ToolUtil { - public static void shutdown(List taskManager) { - if (CollectionUtils.isEmpty(taskManager)) { + public static void shutdown(List taskManagers) { + if (CollectionUtils.isEmpty(taskManagers)) { return; } - for (ToolManager toolManager : taskManager) { + for (ToolManager toolManager : taskManagers) { toolManager.close(); } } @@ -71,8 +71,7 @@ public static void exitWithUsageOrThrow(JCommander commander, int code, boolean isTestNode) { if (isTestNode) { - throw new ParameterException( - "Failed to parse command"); + throw new ParameterException("Failed to parse command"); } commander.usage(); System.exit(code); @@ -81,8 +80,7 @@ public static void exitWithUsageOrThrow(JCommander commander, public static void exitOrThrow(int code, boolean isTestNode) { if (isTestNode) { - throw new ParameterException( - "Failed to parse command"); + throw new ParameterException("Failed to parse command"); } System.exit(code); } From 7ae26241acae5191ccac23ef9a5e687019bf43c4 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 8 Dec 2020 11:25:27 +0800 Subject: [PATCH 09/26] Update and optimize code format --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 29 ++- .../com/baidu/hugegraph/cmd/SubCommands.java | 43 ++-- .../hugegraph/manager/AuthRestoreManager.java | 235 +++++++++--------- .../com/baidu/hugegraph/util/ToolUtil.java | 41 ++- .../test/functional/AuthBackupTest.java | 8 +- .../test/functional/AuthRestoreTest.java | 121 ++++++++- 6 files changed, 293 insertions(+), 184 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index ce12222..858f6cf 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -486,24 +486,27 @@ public void parseCommand(String[] args) { } public void parseCommandForHelp(String[] args) { - String helpValue = Strings.EMPTY; + String subCommand = Strings.EMPTY; List list = Arrays.asList(args); - if (list.contains(Constants.COMMAND_HELP) && - list.size() > list.indexOf(Constants.COMMAND_HELP) + 1) { - helpValue = list.get(list.indexOf(Constants.COMMAND_HELP) + 1); + if (list.contains(Constants.COMMAND_HELP)) { + int index = list.indexOf(Constants.COMMAND_HELP); + if (list.size() > index + 1) { + subCommand = list.get(index + 1); + } } Map commanderMap = this.jCommander.getCommands(); - if (StringUtils.isNotEmpty(helpValue)) { - if (commanderMap.containsKey(helpValue)) { - ToolUtil.exitWithUsageOrThrow(commanderMap.get(helpValue), - Constants.EXIT_CODE_ERROR, - this.testMode()); - } else { - throw new ParameterException(String.format( - "Unexpected help sub-command %s", helpValue)); - } + if (StringUtils.isEmpty(subCommand)) { + return; + } + if (commanderMap.containsKey(subCommand)) { + ToolUtil.exitWithUsageOrThrow(commanderMap.get(subCommand), + Constants.EXIT_CODE_ERROR, + this.testMode()); + } else { + throw new ParameterException(String.format( + "Unexpected help sub-command %s", subCommand)); } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index cda1765..17e1162 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -870,11 +871,6 @@ public static class AuthBackup extends AuthBackupRestore { @ParametersDelegate private AuthTypes types = new AuthTypes(); - @Parameter(names = {"--format"}, arity = 1, - validateWith = {FormatValidator.class}, - description = "File format, valid is [json, text]") - public String format = "json"; - public List types() { return this.types.types; } @@ -882,14 +878,6 @@ public List types() { public void types(List types) { this.types.types = types; } - - public String format() { - return this.format; - } - - public void format(String format) { - this.format = format; - } } public static class AuthRestore extends AuthBackupRestore { @@ -899,8 +887,8 @@ public static class AuthRestore extends AuthBackupRestore { @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, - description = "Valid Strategies that needs to be chosen in the " + - "event of a conflict in restore. strategy include " + + description = "That Strategy that needs to be chosen in the " + + "event of a conflict in restore. valid strategies include " + "’stop’ and ‘ignore’, default is ’stop’. ’stop’ means " + "if there a conflict, stop restore. ‘ignore’ means if " + "there a conflict, ignore and continue to restore.") @@ -940,11 +928,13 @@ public static class AuthTypes { @Parameter(names = {"--types", "-t"}, listConverter = AuthHugeTypeConverter.class, - description = "Type of auth " + - "Concat with ',' if more than one. " + - "'all' means all auth information" + - " in other words, 'all' equals with " + - "'user, group, target, belong, access'") + description = "Type of auth concat with ',' if more than one. " + + "'all' means all auth information in other words, " + + "'all' equals with 'user, group, target, belong, " + + "access’. in addition, only ’belong’ or ‘access’ " + + "can not backup or restore, if type contains ‘belong’ " + + "then should contains ’user’ and ‘group’. if type " + + "contains ‘access’ then should contains ’group’ and ‘target’.") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } @@ -1016,6 +1006,19 @@ public List convert(String value) { if (types.length == 1 && types[0].equalsIgnoreCase("all")) { return AUTH_ALL_TYPES; } + List typeList = Arrays.asList(types); + E.checkArgument(!typeList.contains(HugeType.BELONG.toString().toLowerCase()) || + (typeList.contains(HugeType.USER.toString().toLowerCase()) && + typeList.contains(HugeType.GROUP.toString().toLowerCase())), + "Invalid --type '%s', if type contains ‘belong’" + + " then should contains ’user’ and ‘group’.", + value); + E.checkArgument(!typeList.contains(HugeType.ACCESS.toString().toLowerCase()) || + (typeList.contains(HugeType.GROUP.toString().toLowerCase()) && + typeList.contains(HugeType.TARGET.toString().toLowerCase())), + "Invalid --type '%s', if type contains ‘access’" + + " then should contains ’group’ and ‘target’.", + value); List hugeTypes = new ArrayList<>(); for (String type : types) { try { diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 2c55a8d..511fd62 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; import com.baidu.hugegraph.api.API; @@ -55,8 +56,8 @@ public class AuthRestoreManager extends BackupRestoreBaseManager { private static final String AUTH_BACKUP_NAME = "auth-backup"; private static final String AUTH_RESTORE_DIR = "auth-restore"; + private static final int conflict_status = 0; - private int conflict_status = 0; private AuthRestoreStrategy strategy; private String initPassword; /* @@ -148,135 +149,129 @@ public void doAuthRestore(List types, int status) { protected void checkUseConflict() { List users = retry(this.client.authManager()::listUsers, "Querying users of authority"); - List userJsons = this.read(HugeType.USER); Map userMap = Maps.newHashMap(); for (User user : users) { userMap.put(user.name(), user); } - for (String userStr : userJsons) { - int conflict = this.conflict_status; - User restoreUser = JsonUtil.fromJson(userStr, User.class); - if (userMap.containsKey(restoreUser.name())) { - User resourceUser = userMap.get(restoreUser.name()); - if (resourceUser.phone() != null ? - !resourceUser.phone().equals(restoreUser.phone()) : - restoreUser.phone() != null) { - conflict++; - } - if (resourceUser.email() != null ? - !resourceUser.email().equals(restoreUser.email()) : - restoreUser.email() != null) { - conflict++; - } - if (resourceUser.avatar() != null ? - !resourceUser.avatar().equals(restoreUser.avatar()) : - restoreUser.avatar() != null) { - conflict++; - } - if (conflict > this.conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore users conflict with stop strategy, " + - "user name is s%", restoreUser.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore users strategy is not fund"); - } else { - this.idsMap.put(restoreUser.id().toString(), - resourceUser.id().toString()); - } + List userJsons = this.read(HugeType.USER); + for (String user : userJsons) { + int conflict = conflict_status; + User restoreUser = JsonUtil.fromJson(user, User.class); + if (!userMap.containsKey(restoreUser.name())) { + this.prepareUserForRestore(restoreUser); continue; } - this.prepareUserRestore(restoreUser); + User existUser = userMap.get(restoreUser.name()); + if (!StringUtils.equals(existUser.phone(), + restoreUser.phone())) { + conflict++; + } + if (!StringUtils.equals(existUser.email(), + restoreUser.email())) { + conflict++; + } + if (!StringUtils.equals(existUser.avatar(), + restoreUser.avatar())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore users conflict with stop strategy, " + + "user name is s%", restoreUser.name()); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore users strategy is not found"); + } else { + this.idsMap.put(restoreUser.id().toString(), + existUser.id().toString()); + } } } protected void checkGroupsConflict() { List groups = retry(this.client.authManager()::listGroups, "Querying users of authority"); - List groupJsons = this.read(HugeType.GROUP); Map groupMap = Maps.newHashMap(); for (Group group : groups) { groupMap.put(group.name(), group); } - for (String groupStr : groupJsons) { - int conflict = this.conflict_status; - Group restoreGroup = JsonUtil.fromJson(groupStr, Group.class); - if (groupMap.containsKey(restoreGroup.name())) { - Group resourceGroup = groupMap.get(restoreGroup.name()); - if (resourceGroup.description() != null ? - !resourceGroup.description().equals(restoreGroup.description()) : - restoreGroup.description() != null) { - conflict++; - } - if (conflict > this.conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore groups conflict with stop strategy, " + - "group name is s%", restoreGroup.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore groups strategy is not fund"); - } else { - this.idsMap.put(restoreGroup.id().toString(), - resourceGroup.id().toString()); - } + List groupJsons = this.read(HugeType.GROUP); + for (String group : groupJsons) { + int conflict = conflict_status; + Group restoreGroup = JsonUtil.fromJson(group, Group.class); + if (!groupMap.containsKey(restoreGroup.name())) { + this.prepareGroupForRestore(restoreGroup); continue; } - this.prepareGroupRestore(restoreGroup); + Group existGroup = groupMap.get(restoreGroup.name()); + if (!StringUtils.equals(existGroup.description(), + restoreGroup.description())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore groups conflict with stop strategy, " + + "group name is s%", restoreGroup.name()); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore groups strategy is not found"); + } else { + this.idsMap.put(restoreGroup.id().toString(), + existGroup.id().toString()); + } } } protected void checkTargetsConflict() { List targets = retry(this.client.authManager()::listTargets, "Querying targets of authority"); - List targetJsons = this.read(HugeType.TARGET); Map targetMap = Maps.newHashMap(); for (Target target : targets) { targetMap.put(target.name(), target); } - for (String targetStr : targetJsons) { - int conflict = this.conflict_status; - Target restoreTarget = JsonUtil.fromJson(targetStr, Target.class); - if (targetMap.containsKey(restoreTarget.name())) { - Target resourceTarget = targetMap.get(restoreTarget.name()); - if (resourceTarget.graph() != null ? - !resourceTarget.graph().equals(restoreTarget.graph()) : - restoreTarget.graph() != null) { - conflict++; - } - if (resourceTarget.url() != null ? - !resourceTarget.url().equals(restoreTarget.url()) : - restoreTarget.url() != null) { - conflict++; - } - if (conflict > this.conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore targets conflict with stop strategy, " + - "target name is s%", restoreTarget.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore targets strategy is not fund"); - } else { - this.idsMap.put(restoreTarget.id().toString(), - resourceTarget.id().toString()); - } + List targetJsons = this.read(HugeType.TARGET); + for (String target : targetJsons) { + int conflict = conflict_status; + Target restoreTarget = JsonUtil.fromJson(target, Target.class); + if (!targetMap.containsKey(restoreTarget.name())) { + this.prepareTargetForRestore(restoreTarget); continue; } - this.prepareTargetForRestore(restoreTarget); + Target existTarget = targetMap.get(restoreTarget.name()); + if (!StringUtils.equals(existTarget.graph(), + restoreTarget.graph())) { + conflict++; + } + if (!StringUtils.equals(existTarget.url(), + restoreTarget.url())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, + "Restore targets conflict with stop strategy, " + + "target name is s%", restoreTarget.name()); + E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || + this.strategy == AuthRestoreStrategy.IGNORE, + "Restore targets strategy is not found"); + } else { + this.idsMap.put(restoreTarget.id().toString(), + existTarget.id().toString()); + } } } protected void checkBelongsConflict() { List belongs = retry(this.client.authManager()::listBelongs, "Querying belongs of authority"); - List belongJsons = this.read(HugeType.BELONG); Map belongMap = Maps.newHashMap(); for (Belong belong : belongs) { belongMap.put(belong.user() + ":" + belong.group(), belong); } - for (String str : belongJsons) { - Belong restoreBelong = JsonUtil.fromJson(str, Belong.class); - if (checkIdMaps(restoreBelong.user().toString(), + List belongJsons = this.read(HugeType.BELONG); + for (String belong : belongJsons) { + Belong restoreBelong = JsonUtil.fromJson(belong, Belong.class); + if (!checkAllExistInIdMaps(restoreBelong.user().toString(), restoreBelong.group().toString())) { continue; } @@ -288,7 +283,7 @@ protected void checkBelongsConflict() { "belong id is s%", restoreBelong.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, - "Restore belongs strategy is not fund"); + "Restore belongs strategy is not found"); continue; } this.belongsByName.put(restoreBelong.id().toString(), restoreBelong); @@ -298,15 +293,15 @@ protected void checkBelongsConflict() { protected void checkAccessesConflict() { List accesses = retry(this.client.authManager()::listAccesses, "Querying accesses of authority"); - List accessJsons = this.read(HugeType.ACCESS); Map accessMap = Maps.newHashMap(); for (Access access : accesses) { accessMap.put(access.group() + ":" + access.target(), - access); + access); } - for (String str : accessJsons) { - Access restoreAccess = JsonUtil.fromJson(str, Access.class); - if (checkIdMaps(restoreAccess.group().toString(), + List accessJsons = this.read(HugeType.ACCESS); + for (String access : accessJsons) { + Access restoreAccess = JsonUtil.fromJson(access, Access.class); + if (!checkAllExistInIdMaps(restoreAccess.group().toString(), restoreAccess.target().toString())) { continue; } @@ -318,7 +313,7 @@ protected void checkAccessesConflict() { "accesses id is s%", restoreAccess.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, - "Restore accesses strategy is not fund"); + "Restore accesses strategy is not found"); continue; } this.accessesByName.put(restoreAccess.id().toString(), restoreAccess); @@ -326,7 +321,7 @@ protected void checkAccessesConflict() { } protected void restoreAccesses() { - int counts = 0; + int count = 0; for (Map.Entry entry : this.accessesByName.entrySet()) { Access restoreAccess = entry.getValue(); restoreAccess.target(this.idsMap.get(restoreAccess.target().toString())); @@ -334,63 +329,63 @@ protected void restoreAccesses() { retry(() -> { return this.client.authManager().createAccess(restoreAccess); }, "Restore access of authority"); - counts++; + count++; } - Printer.print("Restore accesses finished, counts is %s !", counts); + Printer.print("Restore accesses finished, count is %d !", count); } protected void restoreBelongs() { - int counts = 0; + int count = 0; for (Map.Entry entry : this.belongsByName.entrySet()) { Belong restoreBelong = entry.getValue(); restoreBelong.user(this.idsMap.get(restoreBelong.user().toString())); restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); retry(() -> { return this.client.authManager().createBelong(restoreBelong); - }, "Restore targets of authority"); - counts++; + }, "Restore belongs of authority"); + count++; } - Printer.print("Restore belongs finished, counts is %s !", counts); + Printer.print("Restore belongs finished, count is %d !", count); } protected void restoreTargets() { - int counts = 0; - for (Map.Entry entry: this.targetsByName.entrySet()) { + int count = 0; + for (Map.Entry entry : this.targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { return this.client.authManager().createTarget(restoreTarget); }, "Restore targets of authority"); this.idsMap.put(restoreTarget.id().toString(), target.id().toString()); - counts++; + count++; } - Printer.print("Restore targets finished, counts is %s !", counts); + Printer.print("Restore targets finished, count is %d !", count); } protected void restoreGroups() { - int counts = 0; - for (Map.Entry entry: this.groupsByName.entrySet()) { + int count = 0; + for (Map.Entry entry : this.groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { return this.client.authManager().createGroup(restoreGroup); }, "Restore groups of authority"); this.idsMap.put(restoreGroup.id().toString(), group.id().toString()); - counts++; + count++; } - Printer.print("Restore groups finished, counts is %s !", counts); + Printer.print("Restore groups finished, count is %d !", count); } protected void restoreUsers() { - int counts = 0; - for (Map.Entry entry: this.usersByName.entrySet()) { + int count = 0; + for (Map.Entry entry : this.usersByName.entrySet()) { User restoreUser = entry.getValue(); restoreUser.password(this.initPassword); User user = retry(() -> { return this.client.authManager().createUser(restoreUser); }, "Restore users of authority"); this.idsMap.put(restoreUser.id().toString(), user.id().toString()); - counts++; + count++; } - Printer.print("Restore users finished, counts is %s !", counts); + Printer.print("Restore users finished, count is %d !", count); } protected void prepareTargetForRestore(Target restoreTarget) { @@ -398,19 +393,19 @@ protected void prepareTargetForRestore(Target restoreTarget) { this.targetsByName.put(restoreTarget.name(), restoreTarget); } - protected void prepareGroupRestore(Group restoreGroup) { + protected void prepareGroupForRestore(Group restoreGroup) { this.idsMap.put(restoreGroup.id().toString(), restoreGroup.id().toString()); this.groupsByName.put(restoreGroup.name(), restoreGroup); } - protected void prepareUserRestore(User restoreUser) { + protected void prepareUserForRestore(User restoreUser) { this.idsMap.put(restoreUser.id().toString(), restoreUser.id().toString()); this.usersByName.put(restoreUser.name(), restoreUser); } - private boolean checkIdMaps(String oneId, String otherId) { - if (!this.idsMap.containsKey(oneId) || - !this.idsMap.containsKey(otherId)) { + private boolean checkAllExistInIdMaps(String oneId, String otherId) { + if (this.idsMap.containsKey(oneId) && + this.idsMap.containsKey(otherId)) { return true; } return false; diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index 5021bc5..e48b806 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -44,33 +44,29 @@ public static void shutdown(List taskManagers) { } public static void printException(Throwable e, - boolean isTestMode) { + boolean testMode) { Printer.print("Failed to execute %s", e.getMessage()); - if (isTestMode) { + if (testMode) { throw new RuntimeException(e); } - if (isPrintStackException()) { - e.printStackTrace(); - } + printExceptionStackByUserChoise(e); } - public static boolean isPrintStackException() { + public static void printExceptionStackByUserChoise(Throwable e) { System.out.println("Type y(yes) to print exception stack[default n]?"); Scanner scan = new Scanner(System.in); String inputInfomation = scan.nextLine(); if (inputInfomation.equalsIgnoreCase(Constants.INPUT_YES) || inputInfomation.equalsIgnoreCase(Constants.INPUT_Y)) { - return true; + e.printStackTrace(); } - - return false; } public static void exitWithUsageOrThrow(JCommander commander, int code, - boolean isTestNode) { - if (isTestNode) { + boolean testMode) { + if (testMode) { throw new ParameterException("Failed to parse command"); } commander.usage(); @@ -78,8 +74,8 @@ public static void exitWithUsageOrThrow(JCommander commander, } public static void exitOrThrow(int code, - boolean isTestNode) { - if (isTestNode) { + boolean testMode) { + if (testMode) { throw new ParameterException("Failed to parse command"); } System.exit(code); @@ -100,17 +96,18 @@ public static RuntimeException targetRuntimeException(Throwable t) { } public static void printCommandsCategory(JCommander jCommander) { - Printer.print("======================================"); + Printer.print("================================================"); Printer.print("Warning : must provide one sub-command"); - Printer.print("======================================"); + Printer.print("================================================"); Printer.print("Here are some sub-command :"); - Map map = jCommander.getCommands(); - for (String key : map.keySet()) { - Printer.print("||" + key); + Map subCommandeMap = jCommander.getCommands(); + for (String subCommand : subCommandeMap.keySet()) { + Printer.print("||" + subCommand); } - Printer.print("======================================"); - Printer.print("Can use 'hugegraph help' or " + - "\n'hugegraph help sub-command'"); - Printer.print("======================================"); + Printer.print("================================================="); + Printer.print("Can use 'hugegraph help' to get detail help info " + + "\nof all sub-commands or 'hugegraph help sub-command' " + + "\nto get detail help info of one sub-command"); + Printer.print("================================================="); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 2273319..c88a459 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -27,7 +27,7 @@ import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.testutil.Assert; -public class AuthBackupTest extends AuthTest{ +public class AuthBackupTest extends AuthTest { @Before public void init() { @@ -79,6 +79,12 @@ public void testAuthBackupByTypesWithException() { Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException")); + Assert.assertContains("valid value is 'all' or combination of " + + "'user,group,target,belong,access'", + msg); }); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 00849cf..283242d 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.collections.CollectionUtils; import org.junit.Before; import org.junit.Test; @@ -30,9 +31,10 @@ import com.baidu.hugegraph.structure.auth.*; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.testutil.Assert; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; -public class AuthRestoreTest extends AuthTest{ +public class AuthRestoreTest extends AuthTest { private HugeClient client; @@ -69,12 +71,14 @@ public void testAuthRestoreByAll() { HugeGraphCommand.main(args); + List idList = Lists.newArrayList(); List userList = this.client.auth().listUsers(); Map userMap = Maps.newHashMap(); for (User user1 : userList) { userMap.put(user1.name(), user1); } Assert.assertTrue(userMap.containsKey("test_user1")); + idList.add(userMap.get("test_user1").id().toString()); List groups = this.client.auth().listGroups(); Map groupMap = Maps.newHashMap(); @@ -82,6 +86,7 @@ public void testAuthRestoreByAll() { groupMap.put(group.name(), group); } Assert.assertTrue(groupMap.containsKey("test_group6")); + idList.add(groupMap.get("test_group6").id().toString()); List targets = this.client.auth().listTargets(); Map targetMap = Maps.newHashMap(); @@ -89,6 +94,31 @@ public void testAuthRestoreByAll() { targetMap.put(target.name(), target); } Assert.assertTrue(targetMap.containsKey("test_target1")); + idList.add(targetMap.get("test_target1").id().toString()); + + List belongs = this.client.auth().listBelongs(); + Assert.assertTrue(CollectionUtils.isNotEmpty(belongs)); + boolean checkUserAndGroup = false; + for (Belong belong : belongs) { + if (idList.contains(belong.user().toString()) && + idList.contains(belong.group().toString())) { + checkUserAndGroup = true; + break; + } + } + Assert.assertTrue(checkUserAndGroup); + + List accesses = this.client.auth().listAccesses(); + Assert.assertTrue(CollectionUtils.isNotEmpty(accesses)); + boolean checkGroupAndTarget = false; + for (Access access : accesses) { + if (idList.contains(access.group().toString()) && + idList.contains(access.target().toString())) { + checkGroupAndTarget = true; + break; + } + } + Assert.assertTrue(checkGroupAndTarget); } @Test @@ -130,11 +160,17 @@ public void testAuthRestoreWithException() { Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.endsWith("The following option is required: [--init-password]")); + Assert.assertContains("com.beust.jcommander.ParameterException: The " + + "following option is required: [--init-password]", + msg); }); } @Test - public void testAuthRestoreByStrategyConfict() { + public void testAuthRestoreByStrategyConflict() { this.loadData("./auth-backup/" + HugeType.USER.string(), "/auth/auth_users_conflict.txt"); @@ -150,6 +186,12 @@ public void testAuthRestoreByStrategyConfict() { Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException")); + Assert.assertContains("java.lang.IllegalArgumentException: " + + "Restore users conflict with stop strategy", + msg); }); } @@ -180,9 +222,8 @@ public void testAuthRestoreByStrategyIgnore() { } @Test - public void testAuthRestoreByStrategyStop() { - this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users_conflict.txt"); + public void testAuthRestoreByDirectoryException() { + String filePath = "./auth-test-test"; String[] args = new String[]{ "--test-mode", "true", @@ -191,16 +232,22 @@ public void testAuthRestoreByStrategyStop() { "auth-restore", "--types", "user", "--strategy", "stop", - "--init-password", "123123" + "--init-password", "123456", + "--directory", filePath }; Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("java.lang.IllegalStateException")); + Assert.assertContains("The directory does not exist", + msg); }); } @Test - public void testAuthRestoreByDirectoryException() { + public void testAuthRestoreByTypesWithException() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -208,7 +255,59 @@ public void testAuthRestoreByDirectoryException() { "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", - "--types", "user", + "--types", "user,test", + "--strategy", "stop", + "--init-password", "123456", + "--directory", filePath + }; + + Assert.assertThrows(RuntimeException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException:")); + Assert.assertContains("valid value is 'all' or combination of " + + "'user,group,target,belong,access'", + msg); + }); + } + + @Test + public void testAuthRestoreByTypesWithBelongException() { + String filePath = "./auth-test-test"; + + String[] args = new String[]{ + "--test-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "belong", + "--strategy", "stop", + "--init-password", "123456", + "--directory", filePath + }; + + Assert.assertThrows(RuntimeException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); + Assert.assertContains("if type contains ‘belong’ then should " + + "contains ’user’ and ‘group’.", + msg); + }); + } + + @Test + public void testAuthRestoreByTypesWithAccessException() { + String filePath = "./auth-test-test"; + + String[] args = new String[]{ + "--test-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "access", "--strategy", "stop", "--init-password", "123456", "--directory", filePath @@ -216,6 +315,12 @@ public void testAuthRestoreByDirectoryException() { Assert.assertThrows(RuntimeException.class, () -> { HugeGraphCommand.main(args); + }, (e) -> { + String msg = e.getMessage(); + Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); + Assert.assertContains("if type contains ‘access’ then should " + + "contains ’group’ and ‘target’.", + msg); }); } From 0021560d3bdea9432a5b976539f8c83c46c0a28d Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 9 Dec 2020 21:01:00 +0800 Subject: [PATCH 10/26] Update and optimize code format --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 84 ++++++++++--------- .../com/baidu/hugegraph/cmd/SubCommands.java | 32 +++---- .../constant/AuthRestoreStrategy.java | 2 +- .../hugegraph/exception/ExitException.java | 50 +++++++++++ .../hugegraph/manager/AuthRestoreManager.java | 2 +- .../manager/BackupRestoreBaseManager.java | 6 +- .../com/baidu/hugegraph/util/ToolUtil.java | 52 +++--------- .../test/functional/AuthBackupTest.java | 4 +- .../test/functional/AuthRestoreTest.java | 55 +++++------- .../hugegraph/test/functional/AuthTest.java | 2 +- .../test/{functional => util}/ClientUtil.java | 4 +- .../test/{functional => util}/FileUtil.java | 9 +- 12 files changed, 159 insertions(+), 143 deletions(-) create mode 100644 src/main/java/com/baidu/hugegraph/exception/ExitException.java rename src/test/java/com/baidu/hugegraph/test/{functional => util}/ClientUtil.java (96%) rename src/test/java/com/baidu/hugegraph/test/{functional => util}/FileUtil.java (98%) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 858f6cf..5ae39af 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; @@ -33,6 +34,7 @@ import com.baidu.hugegraph.base.ToolClient.ConnectionInfo; import com.baidu.hugegraph.base.ToolManager; import com.baidu.hugegraph.constant.Constants; +import com.baidu.hugegraph.exception.ExitException; import com.baidu.hugegraph.manager.AuthBackupManager; import com.baidu.hugegraph.manager.AuthRestoreManager; import com.baidu.hugegraph.manager.BackupManager; @@ -60,9 +62,7 @@ public class HugeGraphCommand { private SubCommands subCommands; - private JCommander jCommander; - - private List taskManagers = Lists.newArrayList(); + private List taskManagers; @ParametersDelegate private SubCommands.Url url = new SubCommands.Url(); @@ -92,7 +92,7 @@ public class HugeGraphCommand { public HugeGraphCommand() { this.subCommands = new SubCommands(); - this.jCommander = jCommander(); + this.taskManagers = Lists.newArrayList(); } public Map subCommands() { @@ -390,9 +390,6 @@ private void execute(String subCmd, JCommander jCommander) { authRestoreManager.init(authRestore); authRestoreManager.authRestore(authRestore.types()); break; - case "help": - jCommander.usage(); - break; default: throw new ParameterException(String.format( "Invalid sub-command: %s", subCmd)); @@ -400,8 +397,8 @@ private void execute(String subCmd, JCommander jCommander) { } private void execute(String[] args) { - this.parseCommand(args); - this.execute(this.jCommander.getParsedCommand(), this.jCommander); + JCommander jCommander = this.parseCommand(args); + this.execute(jCommander.getParsedCommand(), jCommander); } private void checkMainParams() { @@ -469,59 +466,68 @@ private GraphMode mode() { return mode; } - public void parseCommand(String[] args) { + public JCommander parseCommand(String[] args) { + JCommander jCommander = this.jCommander(); if (args.length == 0) { - ToolUtil.exitWithUsageOrThrow(this.jCommander, - Constants.EXIT_CODE_ERROR, - this.testMode()); + throw new ExitException(ToolUtil.commandUsage(jCommander), + "Command is null, print help command usage detail and exit"); } - this.parseCommandForHelp(args); - this.jCommander.parse(args); - String subCommand = this.jCommander.getParsedCommand(); + this.parseCommandForHelp(args, jCommander); + jCommander.parse(args); + String subCommand = jCommander.getParsedCommand(); if (subCommand == null) { - ToolUtil.printCommandsCategory(this.jCommander); - ToolUtil.exitOrThrow(Constants.EXIT_CODE_ERROR, this.testMode()); + ToolUtil.printCommandsCategory(jCommander); + throw new ExitException("", "Command is null, " + + "print command category and exit"); } - + return jCommander; } - public void parseCommandForHelp(String[] args) { + public void parseCommandForHelp(String[] args, JCommander jCommander) { String subCommand = Strings.EMPTY; List list = Arrays.asList(args); - - if (list.contains(Constants.COMMAND_HELP)) { - int index = list.indexOf(Constants.COMMAND_HELP); - if (list.size() > index + 1) { - subCommand = list.get(index + 1); - } + if (!list.contains(Constants.COMMAND_HELP)) { + return; + } + int index = list.indexOf(Constants.COMMAND_HELP); + if (list.size() > index + 1) { + subCommand = list.get(index + 1); } - - Map commanderMap = this.jCommander.getCommands(); if (StringUtils.isEmpty(subCommand)) { - return; + throw new ExitException(ToolUtil.commandUsage(jCommander), + "Print help command usage detail and exit"); } + + Map commanderMap = jCommander.getCommands(); if (commanderMap.containsKey(subCommand)) { - ToolUtil.exitWithUsageOrThrow(commanderMap.get(subCommand), - Constants.EXIT_CODE_ERROR, - this.testMode()); + throw new ExitException(ToolUtil.commandUsage(commanderMap.get(subCommand)), + "Print help sub-command usage detail and exit"); } else { throw new ParameterException(String.format( "Unexpected help sub-command %s", subCommand)); } } + public void shutdown() { + if (CollectionUtils.isEmpty(this.taskManagers)) { + return; + } + for (ToolManager toolManager : this.taskManagers) { + toolManager.close(); + } + } + public static void main(String[] args) { - HugeGraphCommand cmd = null; + HugeGraphCommand cmd = new HugeGraphCommand(); try { - cmd = new HugeGraphCommand(); cmd.execute(args); + } catch (ExitException e) { + ToolUtil.exitWithUsageOrThrow(e, Constants.EXIT_CODE_ERROR, + cmd.testMode()); } catch (Throwable e) { - ToolUtil.printException(e, cmd != null && - cmd.testMode()); + ToolUtil.printException(e, cmd.testMode()); } finally { - ToolUtil.shutdown(cmd == null ? - null : - cmd.taskManagers); + cmd.shutdown(); } } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 17e1162..596f9d7 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -887,16 +887,16 @@ public static class AuthRestore extends AuthBackupRestore { @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, - description = "That Strategy that needs to be chosen in the " + - "event of a conflict in restore. valid strategies include " + - "’stop’ and ‘ignore’, default is ’stop’. ’stop’ means " + - "if there a conflict, stop restore. ‘ignore’ means if " + + description = "The strategy needs to be chosen in the event " + + "of a conflict in restore. valid strategies include " + + "'stop' and 'ignore', default is 'stop'. 'stop' means " + + "if there a conflict, stop restore. 'ignore' means if " + "there a conflict, ignore and continue to restore.") public String strategy = AuthStrategyConverter.strategy; @Parameter(names = {"--init-password"}, arity = 1, description = "Init user password, if restore type include " + - "‘user’, must init user password.") + "'user', must init user password.") public String initPassword = StringUtils.EMPTY; public List types() { @@ -928,13 +928,13 @@ public static class AuthTypes { @Parameter(names = {"--types", "-t"}, listConverter = AuthHugeTypeConverter.class, - description = "Type of auth concat with ',' if more than one. " + - "'all' means all auth information in other words, " + - "'all' equals with 'user, group, target, belong, " + - "access’. in addition, only ’belong’ or ‘access’ " + - "can not backup or restore, if type contains ‘belong’ " + - "then should contains ’user’ and ‘group’. if type " + - "contains ‘access’ then should contains ’group’ and ‘target’.") + description = "Type of auth data to restore and backup, concat with " + + "',' if more than one. 'all' means all auth information" + + " in other words, 'all' equals with 'user, group, target, " + + "belong, access’. in addition, only 'belong' or 'access' " + + "can not backup or restore, if type contains 'belong' " + + "then should contains 'user' and 'group'. if type contains " + + "'access’ then should contains 'group' and 'target'.") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } @@ -1010,14 +1010,14 @@ public List convert(String value) { E.checkArgument(!typeList.contains(HugeType.BELONG.toString().toLowerCase()) || (typeList.contains(HugeType.USER.toString().toLowerCase()) && typeList.contains(HugeType.GROUP.toString().toLowerCase())), - "Invalid --type '%s', if type contains ‘belong’" + - " then should contains ’user’ and ‘group’.", + "Invalid --type '%s', if type contains 'belong'" + + " then should contains 'user' and 'group'.", value); E.checkArgument(!typeList.contains(HugeType.ACCESS.toString().toLowerCase()) || (typeList.contains(HugeType.GROUP.toString().toLowerCase()) && typeList.contains(HugeType.TARGET.toString().toLowerCase())), - "Invalid --type '%s', if type contains ‘access’" + - " then should contains ’group’ and ‘target’.", + "Invalid --type '%s', if type contains 'access'" + + " then should contains 'group' and 'target'.", value); List hugeTypes = new ArrayList<>(); for (String type : types) { diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java index 046bf6c..1ecd863 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java @@ -37,7 +37,7 @@ public int code() { return this.code; } - public static AuthRestoreStrategy getEnumByName(String name) { + public static AuthRestoreStrategy fromName(String name) { AuthRestoreStrategy[] restoreStrategys = AuthRestoreStrategy.values(); for (AuthRestoreStrategy strategy : restoreStrategys) { if (strategy.string().equals(name)) { diff --git a/src/main/java/com/baidu/hugegraph/exception/ExitException.java b/src/main/java/com/baidu/hugegraph/exception/ExitException.java new file mode 100644 index 0000000..12f9ed9 --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/exception/ExitException.java @@ -0,0 +1,50 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.exception; + +public class ExitException extends RuntimeException { + + private final String exitMessage; + + public ExitException(String exitMessage, String message) { + super(message); + this.exitMessage = exitMessage; + } + + public ExitException(String exitMessage, String message, Throwable cause) { + super(message, cause); + this.exitMessage = exitMessage; + } + + public ExitException(String exitMessage, String message, Object... args) { + super(String.format(message, args)); + this.exitMessage = exitMessage; + } + + public ExitException(String exitMessage, String message, Throwable cause, + Object... args) { + super(String.format(message, args), cause); + this.exitMessage = exitMessage; + } + + public String exitMessage() { + return this.exitMessage; + } +} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 511fd62..6a6410c 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -79,7 +79,7 @@ public void init(SubCommands.AuthRestore authRestore) { this.retry(authRestore.retry()); this.directory(authRestore.directory(), authRestore.hdfsConf()); this.ensureDirectoryExist(false); - this.strategy = AuthRestoreStrategy.getEnumByName(authRestore.strategy()); + this.strategy = AuthRestoreStrategy.fromName(authRestore.strategy()); this.initPassword(authRestore.types(), authRestore.initPassword()); this.idsMap = Maps.newHashMap(); this.usersByName = Maps.newHashMap(); diff --git a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java index d2b38f7..2d5d0d4 100644 --- a/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/BackupRestoreBaseManager.java @@ -19,8 +19,6 @@ package com.baidu.hugegraph.manager; -import static com.baidu.hugegraph.base.Directory.closeAndIgnoreException; - import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -199,7 +197,7 @@ protected OutputStream outputStream(String file, boolean compress) { os = this.directory.outputStream(file, compress, true); OutputStream prev = this.outputStreams.putIfAbsent(file, os); if (prev != null) { - closeAndIgnoreException(os); + Directory.closeAndIgnoreException(os); os = prev; } return os; @@ -213,7 +211,7 @@ protected InputStream inputStream(String file) { is = this.directory.inputStream(file); InputStream prev = this.inputStreams.putIfAbsent(file, is); if (prev != null) { - closeAndIgnoreException(is); + Directory.closeAndIgnoreException(is); is = prev; } return is; diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index e48b806..04d0984 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -19,30 +19,16 @@ package com.baidu.hugegraph.util; -import java.lang.reflect.UndeclaredThrowableException; -import java.util.List; import java.util.Map; import java.util.Scanner; -import org.apache.commons.collections.CollectionUtils; - import com.baidu.hugegraph.base.Printer; -import com.baidu.hugegraph.base.ToolManager; import com.baidu.hugegraph.constant.Constants; +import com.baidu.hugegraph.exception.ExitException; import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; public final class ToolUtil { - public static void shutdown(List taskManagers) { - if (CollectionUtils.isEmpty(taskManagers)) { - return; - } - for (ToolManager toolManager : taskManagers) { - toolManager.close(); - } - } - public static void printException(Throwable e, boolean testMode) { Printer.print("Failed to execute %s", e.getMessage()); @@ -63,36 +49,15 @@ public static void printExceptionStackByUserChoise(Throwable e) { } } - public static void exitWithUsageOrThrow(JCommander commander, + public static void exitWithUsageOrThrow(ExitException e, int code, boolean testMode) { if (testMode) { - throw new ParameterException("Failed to parse command"); - } - commander.usage(); - System.exit(code); - } - - public static void exitOrThrow(int code, - boolean testMode) { - if (testMode) { - throw new ParameterException("Failed to parse command"); - } - System.exit(code); - } - - public static RuntimeException targetRuntimeException(Throwable t) { - Throwable e; - if (t instanceof UndeclaredThrowableException) { - e = ((UndeclaredThrowableException) t).getUndeclaredThrowable() - .getCause(); + throw e; } else { - e = t; + Printer.print(e.exitMessage()); + System.exit(code); } - if (e instanceof RuntimeException) { - return (RuntimeException) e; - } - return new RuntimeException(e); } public static void printCommandsCategory(JCommander jCommander) { @@ -110,4 +75,11 @@ public static void printCommandsCategory(JCommander jCommander) { "\nto get detail help info of one sub-command"); Printer.print("================================================="); } + + public static String commandUsage(JCommander jCommander) { + StringBuilder sb = new StringBuilder(); + jCommander.usage(sb); + + return sb.toString(); + } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index c88a459..a512cec 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import com.baidu.hugegraph.cmd.HugeGraphCommand; +import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; public class AuthBackupTest extends AuthTest { @@ -83,8 +84,7 @@ public void testAuthBackupByTypesWithException() { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException")); Assert.assertContains("valid value is 'all' or combination of " + - "'user,group,target,belong,access'", - msg); + "'user,group,target,belong,access'", msg); }); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 283242d..79ff23e 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -30,6 +30,8 @@ import com.baidu.hugegraph.driver.HugeClient; import com.baidu.hugegraph.structure.auth.*; import com.baidu.hugegraph.structure.constant.HugeType; +import com.baidu.hugegraph.test.util.ClientUtil; +import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -48,16 +50,11 @@ public void init() { @Test public void testAuthRestoreByAll() { - this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users.txt"); - this.loadData("./auth-backup/" + HugeType.TARGET.string(), - "/auth/auth_targets.txt"); - this.loadData("./auth-backup/" + HugeType.GROUP.string(), - "/auth/auth_groups.txt"); - this.loadData("./auth-backup/" + HugeType.BELONG.string(), - "/auth/auth_belongs.txt"); - this.loadData("./auth-backup/" + HugeType.ACCESS.string(), - "/auth/auth_accesses.txt"); + this.loadData(HugeType.USER.string(), "/auth/auth_users.txt"); + this.loadData(HugeType.TARGET.string(), "/auth/auth_targets.txt"); + this.loadData(HugeType.GROUP.string(), "/auth/auth_groups.txt"); + this.loadData(HugeType.BELONG.string(), "/auth/auth_belongs.txt"); + this.loadData(HugeType.ACCESS.string(), "/auth/auth_accesses.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -123,8 +120,7 @@ public void testAuthRestoreByAll() { @Test public void testAuthRestoreByUser() { - this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users.txt"); + this.loadData(HugeType.USER.string(), "/auth/auth_users.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -162,17 +158,16 @@ public void testAuthRestoreWithException() { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.endsWith("The following option is required: [--init-password]")); + Assert.assertTrue(msg.endsWith("The following option is " + + "required: [--init-password]")); Assert.assertContains("com.beust.jcommander.ParameterException: The " + - "following option is required: [--init-password]", - msg); + "following option is required: [--init-password]", msg); }); } @Test public void testAuthRestoreByStrategyConflict() { - this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users_conflict.txt"); + this.loadData(HugeType.USER.string(), "/auth/auth_users_conflict.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -189,16 +184,14 @@ public void testAuthRestoreByStrategyConflict() { }, (e) -> { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException")); - Assert.assertContains("java.lang.IllegalArgumentException: " + - "Restore users conflict with stop strategy", - msg); + Assert.assertContains("java.lang.IllegalArgumentException: Restore " + + "users conflict with stop strategy", msg); }); } @Test public void testAuthRestoreByStrategyIgnore() { - this.loadData("./auth-backup/" + HugeType.USER.string(), - "/auth/auth_users_conflict.txt"); + this.loadData(HugeType.USER.string(), "/auth/auth_users_conflict.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -241,8 +234,7 @@ public void testAuthRestoreByDirectoryException() { }, (e) -> { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("java.lang.IllegalStateException")); - Assert.assertContains("The directory does not exist", - msg); + Assert.assertContains("The directory does not exist", msg); }); } @@ -267,8 +259,7 @@ public void testAuthRestoreByTypesWithException() { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException:")); Assert.assertContains("valid value is 'all' or combination of " + - "'user,group,target,belong,access'", - msg); + "'user,group,target,belong,access'", msg); }); } @@ -292,9 +283,8 @@ public void testAuthRestoreByTypesWithBelongException() { }, (e) -> { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); - Assert.assertContains("if type contains ‘belong’ then should " + - "contains ’user’ and ‘group’.", - msg); + Assert.assertContains("if type contains 'belong' then should " + + "contains 'user' and 'group'.", msg); }); } @@ -318,15 +308,14 @@ public void testAuthRestoreByTypesWithAccessException() { }, (e) -> { String msg = e.getMessage(); Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); - Assert.assertContains("if type contains ‘access’ then should " + - "contains ’group’ and ‘target’.", - msg); + Assert.assertContains("if type contains 'access' then should " + + "contains 'group' and 'target'.", msg); }); } private void loadData(String restoreFilePath, String dataFilePath) { List list = FileUtil.read(FileUtil.configPath(dataFilePath)); - FileUtil.writeText(restoreFilePath, list); + FileUtil.writeText(DEFAULT_URL + restoreFilePath, list); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java index 8e06563..acb9da4 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java @@ -21,7 +21,7 @@ public class AuthTest { - public static final String DEFAULT_URL = "./auth-backup"; + public static final String DEFAULT_URL = "./auth-backup/"; public static final String USER_NAME = "admin"; public static final String USER_PASSWORD = "123456"; public static final String URL = "http://127.0.0.1:8080"; diff --git a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java similarity index 96% rename from src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java rename to src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java index c55aac0..5aab06c 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/ClientUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.test.functional; +package com.baidu.hugegraph.test.util; import com.baidu.hugegraph.driver.HugeClient; @@ -30,7 +30,7 @@ public class ClientUtil { private Integer timeout; private String trustStoreFile; private String trustStorePassword; - protected HugeClient hugeClient; + public HugeClient hugeClient; public ClientUtil(String url, String graph, String username, String password, Integer timeout, diff --git a/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java similarity index 98% rename from src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java rename to src/test/java/com/baidu/hugegraph/test/util/FileUtil.java index 6728e06..432eff1 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java @@ -17,23 +17,24 @@ * under the License. */ -package com.baidu.hugegraph.test.functional; +package com.baidu.hugegraph.test.util; -import java.io.ByteArrayOutputStream; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.file.Paths; import java.util.List; +import org.apache.commons.collections.ListUtils; + import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.exception.ToolsException; import com.google.common.collect.Lists; -import org.apache.commons.collections.ListUtils; public class FileUtil { From 215852487e5f095d3d30a4a1368a06eea3881b68 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 10 Dec 2020 20:38:34 +0800 Subject: [PATCH 11/26] Update and optimize some code --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 32 ++------- .../com/baidu/hugegraph/cmd/SubCommands.java | 8 +-- .../hugegraph/exception/ExitException.java | 30 ++++---- .../hugegraph/manager/AuthRestoreManager.java | 47 ++++++------ .../com/baidu/hugegraph/util/ToolUtil.java | 42 +++++++---- .../test/functional/AuthBackupTest.java | 6 +- .../test/functional/AuthRestoreTest.java | 71 +++++++++---------- .../hugegraph/test/functional/AuthTest.java | 1 + .../baidu/hugegraph/test/util/ClientUtil.java | 6 +- .../baidu/hugegraph/test/util/FileUtil.java | 4 +- 10 files changed, 122 insertions(+), 125 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 5ae39af..7103241 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -196,14 +196,12 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start backup!", this.graph()); SubCommands.Backup backup = this.subCommand(subCmd); BackupManager backupManager = manager(BackupManager.class); - this.taskManagers.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); break; case "restore": GraphsManager graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); GraphMode mode = graphsManager.mode(this.graph()); E.checkState(mode.maintaining(), "Invalid mode '%s' of graph '%s' for restore " + @@ -212,7 +210,6 @@ private void execute(String subCmd, JCommander jCommander) { this.graph(), mode); SubCommands.Restore restore = this.subCommand(subCmd); RestoreManager restoreManager = manager(RestoreManager.class); - this.taskManagers.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -230,7 +227,6 @@ private void execute(String subCmd, JCommander jCommander) { } backup = convMigrate2Backup(migrate); backupManager = manager(BackupManager.class); - this.taskManagers.add(backupManager); backupManager.init(backup); backupManager.backup(backup.types()); @@ -243,7 +239,6 @@ private void execute(String subCmd, JCommander jCommander) { this.trustStoreFile(migrate.targetTrustStoreFile()); this.trustStorePassword(migrate.targetTrustStorePassword()); graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); GraphMode origin = graphsManager.mode(migrate.targetGraph()); // Set target graph mode mode = migrate.mode(); @@ -257,7 +252,6 @@ private void execute(String subCmd, JCommander jCommander) { String directory = backupManager.directory().directory(); restore = convMigrate2Restore(migrate, directory); restoreManager = manager(RestoreManager.class); - this.taskManagers.add(restoreManager); restoreManager.init(restore); restoreManager.mode(mode); @@ -269,7 +263,6 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Graph '%s' start dump!", this.graph()); SubCommands.DumpGraph dump = this.subCommand(subCmd); DumpGraphManager dumpManager = manager(DumpGraphManager.class); - this.taskManagers.add(dumpManager); dumpManager.init(dump); dumpManager.dumpFormatter(dump.formatter()); @@ -277,12 +270,10 @@ private void execute(String subCmd, JCommander jCommander) { break; case "graph-list": graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); Printer.printList("Graphs", graphsManager.list()); break; case "graph-get": graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); Printer.printMap("Graph info", graphsManager.get(this.graph())); break; @@ -292,27 +283,23 @@ private void execute(String subCmd, JCommander jCommander) { this.timeout(DEFAULT_CLEAR_TIMEOUT); } graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); graphsManager.clear(this.graph(), graphClear.confirmMessage()); Printer.print("Graph '%s' is cleared", this.graph()); break; case "graph-mode-set": SubCommands.GraphModeSet graphModeSet = this.subCommand(subCmd); graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); graphsManager.mode(this.graph(), graphModeSet.mode()); Printer.print("Set graph '%s' mode to '%s'", this.graph(), graphModeSet.mode()); break; case "graph-mode-get": graphsManager = manager(GraphsManager.class); - this.taskManagers.add(graphsManager); Printer.printKV("Graph mode", graphsManager.mode(this.graph())); break; case "gremlin-execute": SubCommands.Gremlin gremlin = this.subCommand(subCmd); GremlinManager gremlinManager = manager(GremlinManager.class); - this.taskManagers.add(gremlinManager); Printer.print("Run gremlin script"); ResultSet result = gremlinManager.execute(gremlin.script(), gremlin.bindings(), @@ -326,7 +313,6 @@ private void execute(String subCmd, JCommander jCommander) { case "gremlin-schedule": SubCommands.GremlinJob job = this.subCommand(subCmd); gremlinManager = manager(GremlinManager.class); - this.taskManagers.add(gremlinManager); Printer.print("Run gremlin script as async job"); long taskId = gremlinManager.executeAsTask(job.script(), job.bindings(), @@ -336,7 +322,6 @@ private void execute(String subCmd, JCommander jCommander) { case "task-list": SubCommands.TaskList taskList = this.subCommand(subCmd); TasksManager tasksManager = manager(TasksManager.class); - this.taskManagers.add(tasksManager); List tasks = tasksManager.list(taskList.status(), taskList.limit()); List results = tasks.stream().map(Task::asMap) @@ -346,28 +331,24 @@ private void execute(String subCmd, JCommander jCommander) { case "task-get": SubCommands.TaskGet taskGet = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManagers.add(tasksManager); Printer.printKV("Task info", tasksManager.get(taskGet.taskId()).asMap()); break; case "task-delete": SubCommands.TaskDelete taskDelete = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManagers.add(tasksManager); tasksManager.delete(taskDelete.taskId()); Printer.print("Task '%s' is deleted", taskDelete.taskId()); break; case "task-cancel": SubCommands.TaskCancel taskCancel = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManagers.add(tasksManager); tasksManager.cancel(taskCancel.taskId()); Printer.print("Task '%s' is cancelled", taskCancel.taskId()); break; case "task-clear": SubCommands.TaskClear taskClear = this.subCommand(subCmd); tasksManager = manager(TasksManager.class); - this.taskManagers.add(tasksManager); tasksManager.clear(taskClear.force()); Printer.print("Tasks are cleared[force=%s]", taskClear.force()); @@ -376,7 +357,6 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth backup start..."); SubCommands.AuthBackup authBackup = this.subCommand(subCmd); AuthBackupManager authBackupManager = manager(AuthBackupManager.class); - this.taskManagers.add(authBackupManager); authBackupManager.init(authBackup); authBackupManager.authBackup(authBackup.types()); @@ -385,7 +365,6 @@ private void execute(String subCmd, JCommander jCommander) { Printer.print("Auth restore start..."); SubCommands.AuthRestore authRestore = this.subCommand(subCmd); AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); - this.taskManagers.add(authRestoreManager); authRestoreManager.init(authRestore); authRestoreManager.authRestore(authRestore.types()); @@ -417,8 +396,10 @@ private T manager(Class clz) { this.timeout(), this.trustStoreFile(), this.trustStorePassword()); - return clz.getConstructor(ToolClient.ConnectionInfo.class) - .newInstance(info); + T toolManager = clz.getConstructor(ToolClient.ConnectionInfo.class) + .newInstance(info); + this.taskManagers.add(toolManager); + return toolManager; } catch (Exception e) { throw new RuntimeException(String.format( "Construct manager failed for class '%s', please make " + @@ -476,9 +457,8 @@ public JCommander parseCommand(String[] args) { jCommander.parse(args); String subCommand = jCommander.getParsedCommand(); if (subCommand == null) { - ToolUtil.printCommandsCategory(jCommander); - throw new ExitException("", "Command is null, " + - "print command category and exit"); + throw new ExitException(ToolUtil.commandsCategoryDetails(jCommander), + "Command is null, print command category and exit"); } return jCommander; } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 596f9d7..679013c 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -1011,13 +1011,13 @@ public List convert(String value) { (typeList.contains(HugeType.USER.toString().toLowerCase()) && typeList.contains(HugeType.GROUP.toString().toLowerCase())), "Invalid --type '%s', if type contains 'belong'" + - " then should contains 'user' and 'group'.", + " then 'user' and 'group' are required.", value); E.checkArgument(!typeList.contains(HugeType.ACCESS.toString().toLowerCase()) || (typeList.contains(HugeType.GROUP.toString().toLowerCase()) && typeList.contains(HugeType.TARGET.toString().toLowerCase())), "Invalid --type '%s', if type contains 'access'" + - " then should contains 'group' and 'target'.", + " then 'group' and 'target' are required.", value); List hugeTypes = new ArrayList<>(); for (String type : types) { @@ -1026,8 +1026,8 @@ public List convert(String value) { } catch (IllegalArgumentException e) { throw new ParameterException(String.format( "Invalid --type '%s', valid value is 'all' or " + - "combination of 'user,group,target," + - "belong,access'", type)); + "combination of [user,group,target," + + "belong,access]", type)); } } return hugeTypes; diff --git a/src/main/java/com/baidu/hugegraph/exception/ExitException.java b/src/main/java/com/baidu/hugegraph/exception/ExitException.java index 12f9ed9..db75888 100644 --- a/src/main/java/com/baidu/hugegraph/exception/ExitException.java +++ b/src/main/java/com/baidu/hugegraph/exception/ExitException.java @@ -21,30 +21,30 @@ public class ExitException extends RuntimeException { - private final String exitMessage; + private final String details; - public ExitException(String exitMessage, String message) { - super(message); - this.exitMessage = exitMessage; + public ExitException(String details, String reason) { + super(reason); + this.details = details; } - public ExitException(String exitMessage, String message, Throwable cause) { - super(message, cause); - this.exitMessage = exitMessage; + public ExitException(String details, String reason, Throwable cause) { + super(reason, cause); + this.details = details; } - public ExitException(String exitMessage, String message, Object... args) { - super(String.format(message, args)); - this.exitMessage = exitMessage; + public ExitException(String details, String reason, Object... args) { + super(String.format(reason, args)); + this.details = details; } - public ExitException(String exitMessage, String message, Throwable cause, + public ExitException(String details, String reason, Throwable cause, Object... args) { - super(String.format(message, args), cause); - this.exitMessage = exitMessage; + super(String.format(reason, args), cause); + this.details = details; } - public String exitMessage() { - return this.exitMessage; + public String details() { + return this.details; } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 6a6410c..997aec0 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -92,8 +92,8 @@ public void init(SubCommands.AuthRestore authRestore) { public void authRestore(List types) { List sortedHugeTypes = this.sortListByCode(types); try { - this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.CHECK.code()); - this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.RESTORE.code()); + this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.CHECK); + this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.RESTORE); } catch (Throwable e) { throw e; } finally { @@ -101,39 +101,40 @@ public void authRestore(List types) { } } - public void doAuthRestore(List types, int status) { + public void doAuthRestore(List types, + AuthRestoreFlow authRestoreFlow) { for (HugeType type : types) { switch (type) { case USER: - if (status == AuthRestoreFlow.CHECK.code()) { + if (authRestoreFlow == AuthRestoreFlow.CHECK) { this.checkUseConflict(); } else { this.restoreUsers(); } break; case GROUP: - if (status == AuthRestoreFlow.CHECK.code()) { + if (authRestoreFlow == AuthRestoreFlow.CHECK) { this.checkGroupsConflict(); } else { this.restoreGroups(); } break; case TARGET: - if (status == AuthRestoreFlow.CHECK.code()) { + if (authRestoreFlow == AuthRestoreFlow.CHECK) { this.checkTargetsConflict(); } else { this.restoreTargets(); } break; case BELONG: - if (status == AuthRestoreFlow.CHECK.code()) { + if (authRestoreFlow == AuthRestoreFlow.CHECK) { this.checkBelongsConflict(); } else { this.restoreBelongs(); } break; case ACCESS: - if (status == AuthRestoreFlow.CHECK.code()) { + if (authRestoreFlow == AuthRestoreFlow.CHECK) { this.checkAccessesConflict(); } else { this.restoreAccesses(); @@ -176,7 +177,7 @@ protected void checkUseConflict() { } if (conflict > conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore users conflict with stop strategy, " + + "Restore users conflict with STOP strategy, " + "user name is s%", restoreUser.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, @@ -210,7 +211,7 @@ protected void checkGroupsConflict() { } if (conflict > conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore groups conflict with stop strategy, " + + "Restore groups conflict with STOP strategy, " + "group name is s%", restoreGroup.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, @@ -248,7 +249,7 @@ protected void checkTargetsConflict() { } if (conflict > conflict_status) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore targets conflict with stop strategy, " + + "Restore targets conflict with STOP strategy, " + "target name is s%", restoreTarget.name()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, @@ -279,7 +280,7 @@ protected void checkBelongsConflict() { this.idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore belongs conflict with stop strategy, " + + "Restore belongs conflict with STOP strategy, " + "belong id is s%", restoreBelong.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, @@ -309,7 +310,7 @@ protected void checkAccessesConflict() { this.idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore accesses conflict with stop strategy," + + "Restore accesses conflict with STOP strategy," + "accesses id is s%", restoreAccess.id()); E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || this.strategy == AuthRestoreStrategy.IGNORE, @@ -327,8 +328,8 @@ protected void restoreAccesses() { restoreAccess.target(this.idsMap.get(restoreAccess.target().toString())); restoreAccess.group(this.idsMap.get(restoreAccess.group().toString())); retry(() -> { - return this.client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); + return this.client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); count++; } Printer.print("Restore accesses finished, count is %d !", count); @@ -341,8 +342,8 @@ protected void restoreBelongs() { restoreBelong.user(this.idsMap.get(restoreBelong.user().toString())); restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); retry(() -> { - return this.client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); + return this.client.authManager().createBelong(restoreBelong); + }, "Restore belongs of authority"); count++; } Printer.print("Restore belongs finished, count is %d !", count); @@ -353,8 +354,8 @@ protected void restoreTargets() { for (Map.Entry entry : this.targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return this.client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); + return this.client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); this.idsMap.put(restoreTarget.id().toString(), target.id().toString()); count++; } @@ -366,8 +367,8 @@ protected void restoreGroups() { for (Map.Entry entry : this.groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return this.client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); + return this.client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); this.idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } @@ -380,8 +381,8 @@ protected void restoreUsers() { User restoreUser = entry.getValue(); restoreUser.password(this.initPassword); User user = retry(() -> { - return this.client.authManager().createUser(restoreUser); - }, "Restore users of authority"); + return this.client.authManager().createUser(restoreUser); + }, "Restore users of authority"); this.idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index 04d0984..b8762f4 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -33,12 +33,15 @@ public static void printException(Throwable e, boolean testMode) { Printer.print("Failed to execute %s", e.getMessage()); if (testMode) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } throw new RuntimeException(e); } - printExceptionStackByUserChoise(e); + printExceptionStackIfNeeded(e); } - public static void printExceptionStackByUserChoise(Throwable e) { + public static void printExceptionStackIfNeeded(Throwable e) { System.out.println("Type y(yes) to print exception stack[default n]?"); Scanner scan = new Scanner(System.in); String inputInfomation = scan.nextLine(); @@ -55,25 +58,36 @@ public static void exitWithUsageOrThrow(ExitException e, if (testMode) { throw e; } else { - Printer.print(e.exitMessage()); + Printer.print(e.details()); System.exit(code); } } - public static void printCommandsCategory(JCommander jCommander) { - Printer.print("================================================"); - Printer.print("Warning : must provide one sub-command"); - Printer.print("================================================"); - Printer.print("Here are some sub-command :"); + public static String commandsCategoryDetails(JCommander jCommander) { + StringBuffer sb = new StringBuffer(); + sb.append("================================================"); + sb.append("\n"); + sb.append("Warning : must provide one sub-command"); + sb.append("\n"); + sb.append("================================================"); + sb.append("\n"); + sb.append("Here are some sub-command :"); + sb.append("\n"); Map subCommandeMap = jCommander.getCommands(); for (String subCommand : subCommandeMap.keySet()) { - Printer.print("||" + subCommand); + sb.append("||"); + sb.append(subCommand); + sb.append("\n"); } - Printer.print("================================================="); - Printer.print("Can use 'hugegraph help' to get detail help info " + - "\nof all sub-commands or 'hugegraph help sub-command' " + - "\nto get detail help info of one sub-command"); - Printer.print("================================================="); + sb.append("================================================"); + sb.append("\n"); + sb.append("Can use 'hugegraph help' to get detail help info " + + "of all sub-commands or 'hugegraph help sub-command' " + + "to get detail help info of one sub-command"); + sb.append("\n"); + sb.append("================================================"); + + return sb.toString(); } public static String commandUsage(JCommander jCommander) { diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index a512cec..70b0631 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -27,6 +27,7 @@ import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; +import com.beust.jcommander.ParameterException; public class AuthBackupTest extends AuthTest { @@ -78,13 +79,12 @@ public void testAuthBackupByTypesWithException() { "--types", "user,group,test" }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException")); Assert.assertContains("valid value is 'all' or combination of " + - "'user,group,target,belong,access'", msg); + "[user,group,target,belong,access]", msg); }); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 79ff23e..5c96c4d 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -28,11 +28,16 @@ import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.driver.HugeClient; -import com.baidu.hugegraph.structure.auth.*; +import com.baidu.hugegraph.structure.auth.Access; +import com.baidu.hugegraph.structure.auth.Belong; +import com.baidu.hugegraph.structure.auth.Group; +import com.baidu.hugegraph.structure.auth.Target; +import com.baidu.hugegraph.structure.auth.User; import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.test.util.ClientUtil; import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; +import com.beust.jcommander.ParameterException; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -42,19 +47,19 @@ public class AuthRestoreTest extends AuthTest { @Before public void init() { - ClientUtil initUtil = new ClientUtil(URL, GRAPH, USER_NAME, - USER_PASSWORD, TIME_OUT, - TRUST_STORE_FILE, TRUST_STORE_PASSWORD); - this.client = initUtil.hugeClient; + ClientUtil clientUtil = new ClientUtil(URL, GRAPH, USER_NAME, + USER_PASSWORD, TIME_OUT, + TRUST_STORE_FILE, TRUST_STORE_PASSWORD); + this.client = clientUtil.hugeClient(); } @Test public void testAuthRestoreByAll() { - this.loadData(HugeType.USER.string(), "/auth/auth_users.txt"); - this.loadData(HugeType.TARGET.string(), "/auth/auth_targets.txt"); - this.loadData(HugeType.GROUP.string(), "/auth/auth_groups.txt"); - this.loadData(HugeType.BELONG.string(), "/auth/auth_belongs.txt"); - this.loadData(HugeType.ACCESS.string(), "/auth/auth_accesses.txt"); + this.loadData(HugeType.USER, "auth_users.txt"); + this.loadData(HugeType.TARGET, "auth_targets.txt"); + this.loadData(HugeType.GROUP, "auth_groups.txt"); + this.loadData(HugeType.BELONG, "auth_belongs.txt"); + this.loadData(HugeType.ACCESS, "auth_accesses.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -120,7 +125,7 @@ public void testAuthRestoreByAll() { @Test public void testAuthRestoreByUser() { - this.loadData(HugeType.USER.string(), "/auth/auth_users.txt"); + this.loadData(HugeType.USER, "auth_users.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -154,20 +159,18 @@ public void testAuthRestoreWithException() { "--directory", DEFAULT_URL }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); Assert.assertTrue(msg.endsWith("The following option is " + "required: [--init-password]")); - Assert.assertContains("com.beust.jcommander.ParameterException: The " + - "following option is required: [--init-password]", msg); }); } @Test public void testAuthRestoreByStrategyConflict() { - this.loadData(HugeType.USER.string(), "/auth/auth_users_conflict.txt"); + this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -179,19 +182,17 @@ public void testAuthRestoreByStrategyConflict() { "--init-password", "123456" }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException")); - Assert.assertContains("java.lang.IllegalArgumentException: Restore " + - "users conflict with stop strategy", msg); + Assert.assertContains("Restore users conflict with STOP strategy", msg); }); } @Test public void testAuthRestoreByStrategyIgnore() { - this.loadData(HugeType.USER.string(), "/auth/auth_users_conflict.txt"); + this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ "--test-mode", "true", @@ -229,11 +230,10 @@ public void testAuthRestoreByDirectoryException() { "--directory", filePath }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(IllegalStateException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("java.lang.IllegalStateException")); Assert.assertContains("The directory does not exist", msg); }); } @@ -253,13 +253,12 @@ public void testAuthRestoreByTypesWithException() { "--directory", filePath }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("com.beust.jcommander.ParameterException:")); Assert.assertContains("valid value is 'all' or combination of " + - "'user,group,target,belong,access'", msg); + "[user,group,target,belong,access]", msg); }); } @@ -278,13 +277,12 @@ public void testAuthRestoreByTypesWithBelongException() { "--directory", filePath }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); - Assert.assertContains("if type contains 'belong' then should " + - "contains 'user' and 'group'.", msg); + Assert.assertContains("if type contains 'belong' then " + + "'user' and 'group' are required.", msg); }); } @@ -303,19 +301,18 @@ public void testAuthRestoreByTypesWithAccessException() { "--directory", filePath }; - Assert.assertThrows(RuntimeException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); - Assert.assertTrue(msg.startsWith("java.lang.IllegalArgumentException:")); - Assert.assertContains("if type contains 'access' then should " + - "contains 'group' and 'target'.", msg); + Assert.assertContains("if type contains 'access' then " + + "'group' and 'target' are required.", msg); }); } - private void loadData(String restoreFilePath, String dataFilePath) { - List list = FileUtil.read(FileUtil.configPath(dataFilePath)); - - FileUtil.writeText(DEFAULT_URL + restoreFilePath, list); + private void loadData(HugeType hugeType, String dataFilePath) { + List list = FileUtil.read(FileUtil.configPath(DEFAULT_TEST_URL + + dataFilePath)); + FileUtil.writeText(DEFAULT_URL + hugeType.string(), list); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java index acb9da4..b9578a5 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java @@ -22,6 +22,7 @@ public class AuthTest { public static final String DEFAULT_URL = "./auth-backup/"; + public static final String DEFAULT_TEST_URL = "/auth/"; public static final String USER_NAME = "admin"; public static final String USER_PASSWORD = "123456"; public static final String URL = "http://127.0.0.1:8080"; diff --git a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java index 5aab06c..7d73943 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java @@ -30,7 +30,7 @@ public class ClientUtil { private Integer timeout; private String trustStoreFile; private String trustStorePassword; - public HugeClient hugeClient; + protected HugeClient hugeClient; public ClientUtil(String url, String graph, String username, String password, Integer timeout, @@ -52,4 +52,8 @@ protected void client() { .configSSL(trustStoreFile, trustStorePassword) .build(); } + + public HugeClient hugeClient() { + return this.hugeClient; + } } diff --git a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java index 432eff1..fab4147 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java @@ -92,7 +92,7 @@ public static long writeText(String filePath, List list) { os.write(baos.toByteArray()); } catch (IOException e) { throw new ToolsException("Failed writeText file path is %s", - e, filePath); + e, filePath); } return count; @@ -109,7 +109,7 @@ public static List read(String filePath) { } } catch (IOException e) { throw new ToolsException("Failed read file path is %s", - e, filePath); + e, filePath); } return resultList; From 1e6fde05aa0b08bd430c383e456132b6b2c41617 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 11 Dec 2020 21:19:15 +0800 Subject: [PATCH 12/26] Update and optimize some code --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 63 +++++++----- .../com/baidu/hugegraph/cmd/SubCommands.java | 28 +++--- .../baidu/hugegraph/constant/Constants.java | 2 + .../hugegraph/exception/ExitException.java | 32 +++++- .../hugegraph/manager/AuthRestoreManager.java | 88 +++++++++-------- .../com/baidu/hugegraph/util/ToolUtil.java | 28 +++--- .../test/functional/AuthBackupTest.java | 12 +-- .../test/functional/AuthRestoreTest.java | 50 +++++----- .../test/functional/CommandTest.java | 98 +++++++++++++++++++ .../test/functional/FuncTestSuite.java | 3 +- .../baidu/hugegraph/test/util/ClientUtil.java | 10 +- 11 files changed, 279 insertions(+), 135 deletions(-) create mode 100644 src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 7103241..715a9d6 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -88,7 +88,7 @@ public class HugeGraphCommand { new SubCommands.TrustStorePassword(); @ParametersDelegate - private SubCommands.TestMode testMode = new SubCommands.TestMode(); + private SubCommands.ThrowMode throwMode = new SubCommands.ThrowMode(); public HugeGraphCommand() { this.subCommands = new SubCommands(); @@ -160,12 +160,12 @@ public void trustStorePassword(String trustStorePassword) { this.trustStorePassword.trustStorePassword = trustStorePassword; } - public boolean testMode() { - return this.testMode.testMode; + public boolean throwMode() { + return this.throwMode.throwMode; } - private void testMode(boolean testMode) { - this.testMode.testMode = testMode; + private void throwMode(boolean throwMode) { + this.throwMode.throwMode = throwMode; } public JCommander jCommander() { @@ -450,24 +450,34 @@ private GraphMode mode() { public JCommander parseCommand(String[] args) { JCommander jCommander = this.jCommander(); if (args.length == 0) { - throw new ExitException(ToolUtil.commandUsage(jCommander), - "Command is null, print help command usage detail and exit"); + throw ExitException.exception(ToolUtil.commandUsage(jCommander), + "No command found, please input" + + " command"); + } + if (this.parseHelp(args, jCommander)) { + assert false; + } else { + jCommander.parse(args); } - this.parseCommandForHelp(args, jCommander); - jCommander.parse(args); String subCommand = jCommander.getParsedCommand(); if (subCommand == null) { - throw new ExitException(ToolUtil.commandsCategoryDetails(jCommander), - "Command is null, print command category and exit"); + throw new ExitException(ToolUtil.commandsCategory(jCommander), + "No sub-Command found"); } return jCommander; } - public void parseCommandForHelp(String[] args, JCommander jCommander) { + public boolean parseHelp(String[] args, JCommander jCommander) { String subCommand = Strings.EMPTY; List list = Arrays.asList(args); if (!list.contains(Constants.COMMAND_HELP)) { - return; + return false; + } + //parse command for throw mode + if (list.contains(Constants.COMMAND_THROW_MODE)) { + int index = list.indexOf(Constants.COMMAND_THROW_MODE) + 1; + jCommander.parse(Constants.COMMAND_THROW_MODE, + list.get(index)); } int index = list.indexOf(Constants.COMMAND_HELP); if (list.size() > index + 1) { @@ -475,16 +485,17 @@ public void parseCommandForHelp(String[] args, JCommander jCommander) { } if (StringUtils.isEmpty(subCommand)) { throw new ExitException(ToolUtil.commandUsage(jCommander), - "Print help command usage detail and exit"); + "Command : hugegragh help"); } - Map commanderMap = jCommander.getCommands(); - if (commanderMap.containsKey(subCommand)) { - throw new ExitException(ToolUtil.commandUsage(commanderMap.get(subCommand)), - "Print help sub-command usage detail and exit"); + Map commands = jCommander.getCommands(); + if (commands.containsKey(subCommand)) { + throw new ExitException(ToolUtil.commandUsage(commands.get(subCommand)), + String.format("Hugegragh help %s", subCommand)); } else { - throw new ParameterException(String.format( - "Unexpected help sub-command %s", subCommand)); + throw ExitException.exception(ToolUtil.commandsCategory(jCommander), + String.format("Unexpected help " + + "sub-command %s", subCommand)); } } @@ -499,15 +510,21 @@ public void shutdown() { public static void main(String[] args) { HugeGraphCommand cmd = new HugeGraphCommand(); + int exitCode = Constants.EXIT_CODE_NORMAL; try { cmd.execute(args); } catch (ExitException e) { - ToolUtil.exitWithUsageOrThrow(e, Constants.EXIT_CODE_ERROR, - cmd.testMode()); + exitCode = e.exitCode(); + ToolUtil.exitOrThrow(e, cmd.throwMode()); } catch (Throwable e) { - ToolUtil.printException(e, cmd.testMode()); + exitCode = Constants.EXIT_CODE_ERROR; + ToolUtil.printOrThrow(e, cmd.throwMode()); } finally { cmd.shutdown(); } + + if(exitCode == Constants.EXIT_CODE_ERROR) { + System.exit(exitCode); + } } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 679013c..5f11254 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -687,11 +687,12 @@ public static class TrustStorePassword { public String trustStorePassword; } - public static class TestMode { + public static class ThrowMode { - @Parameter(names = {"--test-mode"}, arity = 1, - description = "Whether the hugegraph-tools work in test mode") - public boolean testMode = false; + @Parameter(names = {"--throw-mode"}, arity = 1, + description = "Whether the hugegraph-tools work " + + "to throw an exception") + public boolean throwMode = false; } public static class HugeTypes { @@ -1011,14 +1012,12 @@ public List convert(String value) { (typeList.contains(HugeType.USER.toString().toLowerCase()) && typeList.contains(HugeType.GROUP.toString().toLowerCase())), "Invalid --type '%s', if type contains 'belong'" + - " then 'user' and 'group' are required.", - value); + " then 'user' and 'group' are required.", value); E.checkArgument(!typeList.contains(HugeType.ACCESS.toString().toLowerCase()) || (typeList.contains(HugeType.GROUP.toString().toLowerCase()) && typeList.contains(HugeType.TARGET.toString().toLowerCase())), "Invalid --type '%s', if type contains 'access'" + - " then 'group' and 'target' are required.", - value); + " then 'group' and 'target' are required.", value); List hugeTypes = new ArrayList<>(); for (String type : types) { try { @@ -1043,14 +1042,11 @@ public static class AuthStrategyConverter public String convert(String value) { E.checkArgument(value != null && !value.isEmpty(), "Strategy can't be null or empty"); - if (AuthRestoreStrategy.STOP.string().equals(value) || - AuthRestoreStrategy.IGNORE.string().equals(value)) { - return value; - } else { - throw new ParameterException(String.format( - "Invalid --strategy '%s', valid value is 'stop' or " + - "'ignore", value)); - } + E.checkArgument(AuthRestoreStrategy.STOP.string().equals(value) || + AuthRestoreStrategy.IGNORE.string().equals(value), + "Invalid --strategy '%s', valid value is" + + " 'stop' or 'ignore", value); + return value; } } diff --git a/src/main/java/com/baidu/hugegraph/constant/Constants.java b/src/main/java/com/baidu/hugegraph/constant/Constants.java index 3c7806e..e365e01 100644 --- a/src/main/java/com/baidu/hugegraph/constant/Constants.java +++ b/src/main/java/com/baidu/hugegraph/constant/Constants.java @@ -22,8 +22,10 @@ public final class Constants { public static final int EXIT_CODE_ERROR = -1; + public static final int EXIT_CODE_NORMAL = 0; public static final String INPUT_YES = "yes"; public static final String INPUT_Y= "y"; public static final String COMMAND_HELP = "help"; + public static final String COMMAND_THROW_MODE = "--throw-mode"; } diff --git a/src/main/java/com/baidu/hugegraph/exception/ExitException.java b/src/main/java/com/baidu/hugegraph/exception/ExitException.java index db75888..b1e577a 100644 --- a/src/main/java/com/baidu/hugegraph/exception/ExitException.java +++ b/src/main/java/com/baidu/hugegraph/exception/ExitException.java @@ -19,32 +19,58 @@ package com.baidu.hugegraph.exception; +import com.baidu.hugegraph.constant.Constants; + public class ExitException extends RuntimeException { private final String details; + private final Integer exitCode; public ExitException(String details, String reason) { super(reason); this.details = details; + this.exitCode = Constants.EXIT_CODE_NORMAL; + } + + public ExitException(Integer exitCode, String details, + String reason) { + super(reason); + this.details = details; + this.exitCode = exitCode; } - public ExitException(String details, String reason, Throwable cause) { + public ExitException(Integer exitCode, String details, + String reason, Throwable cause) { super(reason, cause); this.details = details; + this.exitCode = exitCode; } - public ExitException(String details, String reason, Object... args) { + public ExitException(Integer exitCode, String details, + String reason, Object... args) { super(String.format(reason, args)); this.details = details; + this.exitCode = exitCode; } - public ExitException(String details, String reason, Throwable cause, + public ExitException(Integer exitCode, String details, + String reason, Throwable cause, Object... args) { super(String.format(reason, args), cause); this.details = details; + this.exitCode = exitCode; } public String details() { return this.details; } + + public Integer exitCode() { + return this.exitCode; + } + + public static ExitException exception(String details, String reason) { + return new ExitException(Constants.EXIT_CODE_ERROR, + details, reason); + } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index 997aec0..d91c0dc 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -37,7 +37,6 @@ import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.cmd.SubCommands; -import com.baidu.hugegraph.constant.AuthRestoreFlow; import com.baidu.hugegraph.constant.AuthRestoreStrategy; import com.baidu.hugegraph.exception.ToolsException; import com.baidu.hugegraph.structure.auth.Access; @@ -48,7 +47,6 @@ import com.baidu.hugegraph.structure.constant.HugeType; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.JsonUtil; -import com.beust.jcommander.ParameterException; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -92,8 +90,8 @@ public void init(SubCommands.AuthRestore authRestore) { public void authRestore(List types) { List sortedHugeTypes = this.sortListByCode(types); try { - this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.CHECK); - this.doAuthRestore(sortedHugeTypes, AuthRestoreFlow.RESTORE); + this.doAuthCheckConflict(sortedHugeTypes); + this.doAuthRestore(sortedHugeTypes); } catch (Throwable e) { throw e; } finally { @@ -101,44 +99,23 @@ public void authRestore(List types) { } } - public void doAuthRestore(List types, - AuthRestoreFlow authRestoreFlow) { + private void doAuthCheckConflict(List types) { for (HugeType type : types) { switch (type) { case USER: - if (authRestoreFlow == AuthRestoreFlow.CHECK) { - this.checkUseConflict(); - } else { - this.restoreUsers(); - } + this.checkUsersConflict(); break; case GROUP: - if (authRestoreFlow == AuthRestoreFlow.CHECK) { - this.checkGroupsConflict(); - } else { - this.restoreGroups(); - } + this.checkGroupsConflict(); break; case TARGET: - if (authRestoreFlow == AuthRestoreFlow.CHECK) { - this.checkTargetsConflict(); - } else { - this.restoreTargets(); - } + this.checkTargetsConflict(); break; case BELONG: - if (authRestoreFlow == AuthRestoreFlow.CHECK) { - this.checkBelongsConflict(); - } else { - this.restoreBelongs(); - } + this.checkBelongsConflict(); break; case ACCESS: - if (authRestoreFlow == AuthRestoreFlow.CHECK) { - this.checkAccessesConflict(); - } else { - this.restoreAccesses(); - } + this.checkAccessesConflict(); break; default: throw new AssertionError(String.format( @@ -147,7 +124,32 @@ public void doAuthRestore(List types, } } - protected void checkUseConflict() { + private void doAuthRestore(List types) { + for (HugeType type : types) { + switch (type) { + case USER: + this.restoreUsers(); + break; + case GROUP: + this.restoreGroups(); + break; + case TARGET: + this.restoreTargets(); + break; + case BELONG: + this.restoreBelongs(); + break; + case ACCESS: + this.restoreAccesses(); + break; + default: + throw new AssertionError(String.format( + "Bad auth restore type: %s", type)); + } + } + } + + protected void checkUsersConflict() { List users = retry(this.client.authManager()::listUsers, "Querying users of authority"); Map userMap = Maps.newHashMap(); @@ -328,8 +330,8 @@ protected void restoreAccesses() { restoreAccess.target(this.idsMap.get(restoreAccess.target().toString())); restoreAccess.group(this.idsMap.get(restoreAccess.group().toString())); retry(() -> { - return this.client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); + return this.client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); count++; } Printer.print("Restore accesses finished, count is %d !", count); @@ -342,8 +344,8 @@ protected void restoreBelongs() { restoreBelong.user(this.idsMap.get(restoreBelong.user().toString())); restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); retry(() -> { - return this.client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); + return this.client.authManager().createBelong(restoreBelong); + }, "Restore belongs of authority"); count++; } Printer.print("Restore belongs finished, count is %d !", count); @@ -354,8 +356,8 @@ protected void restoreTargets() { for (Map.Entry entry : this.targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return this.client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); + return this.client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); this.idsMap.put(restoreTarget.id().toString(), target.id().toString()); count++; } @@ -367,8 +369,8 @@ protected void restoreGroups() { for (Map.Entry entry : this.groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return this.client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); + return this.client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); this.idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } @@ -381,8 +383,8 @@ protected void restoreUsers() { User restoreUser = entry.getValue(); restoreUser.password(this.initPassword); User user = retry(() -> { - return this.client.authManager().createUser(restoreUser); - }, "Restore users of authority"); + return this.client.authManager().createUser(restoreUser); + }, "Restore users of authority"); this.idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } @@ -447,7 +449,7 @@ public List sortListByCode(List hugeTypes) { public void initPassword(List types, String password) { if (types.contains(HugeType.USER) && Strings.isEmpty(password)) { - throw new ParameterException(String.format( + throw new IllegalArgumentException(String.format( "The following option is required: [--init-password]")); } else { this.initPassword = password; diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index b8762f4..fad906a 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -29,7 +29,7 @@ public final class ToolUtil { - public static void printException(Throwable e, + public static void printOrThrow(Throwable e, boolean testMode) { Printer.print("Failed to execute %s", e.getMessage()); if (testMode) { @@ -52,18 +52,18 @@ public static void printExceptionStackIfNeeded(Throwable e) { } } - public static void exitWithUsageOrThrow(ExitException e, - int code, - boolean testMode) { - if (testMode) { + public static void exitOrThrow(ExitException e, boolean throwMode) { + if (throwMode) { throw e; - } else { - Printer.print(e.details()); - System.exit(code); } + + if(e.exitCode() != Constants.EXIT_CODE_NORMAL) { + Printer.print(e.getMessage()); + } + Printer.print(e.details()); } - public static String commandsCategoryDetails(JCommander jCommander) { + public static String commandsCategory(JCommander jCommander) { StringBuffer sb = new StringBuffer(); sb.append("================================================"); sb.append("\n"); @@ -73,16 +73,16 @@ public static String commandsCategoryDetails(JCommander jCommander) { sb.append("\n"); sb.append("Here are some sub-command :"); sb.append("\n"); - Map subCommandeMap = jCommander.getCommands(); - for (String subCommand : subCommandeMap.keySet()) { - sb.append("||"); + Map subCommandes = jCommander.getCommands(); + for (String subCommand : subCommandes.keySet()) { + sb.append("|"); sb.append(subCommand); sb.append("\n"); } sb.append("================================================"); sb.append("\n"); - sb.append("Can use 'hugegraph help' to get detail help info " + - "of all sub-commands or 'hugegraph help sub-command' " + + sb.append("Please use 'hugegraph help' to get detail help info " + + "of all sub-commands or 'hugegraph help {sub-command}' " + "to get detail help info of one sub-command"); sb.append("\n"); sb.append("================================================"); diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 70b0631..9c464d6 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -39,7 +39,7 @@ public void init() { @Test public void testAuthBackup() { String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup" @@ -55,7 +55,7 @@ public void testAuthBackup() { @Test public void testAuthBackupByTypes() { String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", @@ -72,7 +72,7 @@ public void testAuthBackupByTypes() { @Test public void testAuthBackupByTypesWithException() { String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", @@ -82,9 +82,9 @@ public void testAuthBackupByTypesWithException() { Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); Assert.assertContains("valid value is 'all' or combination of " + - "[user,group,target,belong,access]", msg); + "[user,group,target,belong,access]", + e.getMessage()); }); } @@ -92,7 +92,7 @@ public void testAuthBackupByTypesWithException() { public void testAuthBackupByDirectory() { String directory = "./backup"; String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-backup", diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 5c96c4d..31f6a11 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -50,7 +50,7 @@ public void init() { ClientUtil clientUtil = new ClientUtil(URL, GRAPH, USER_NAME, USER_PASSWORD, TIME_OUT, TRUST_STORE_FILE, TRUST_STORE_PASSWORD); - this.client = clientUtil.hugeClient(); + this.client = clientUtil.client(); } @Test @@ -62,7 +62,7 @@ public void testAuthRestoreByAll() { this.loadData(HugeType.ACCESS, "auth_accesses.txt"); String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -128,7 +128,7 @@ public void testAuthRestoreByUser() { this.loadData(HugeType.USER, "auth_users.txt"); String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -151,7 +151,7 @@ public void testAuthRestoreByUser() { @Test public void testAuthRestoreWithException() { String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -159,7 +159,7 @@ public void testAuthRestoreWithException() { "--directory", DEFAULT_URL }; - Assert.assertThrows(ParameterException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { String msg = e.getMessage(); @@ -173,7 +173,7 @@ public void testAuthRestoreByStrategyConflict() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -185,8 +185,8 @@ public void testAuthRestoreByStrategyConflict() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); - Assert.assertContains("Restore users conflict with STOP strategy", msg); + Assert.assertContains("Restore users conflict with STOP strategy", + e.getMessage()); }); } @@ -195,7 +195,7 @@ public void testAuthRestoreByStrategyIgnore() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -220,7 +220,7 @@ public void testAuthRestoreByDirectoryException() { String filePath = "./auth-test-test"; String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -233,8 +233,8 @@ public void testAuthRestoreByDirectoryException() { Assert.assertThrows(IllegalStateException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); - Assert.assertContains("The directory does not exist", msg); + Assert.assertContains("The directory does not exist", + e.getMessage()); }); } @@ -243,7 +243,7 @@ public void testAuthRestoreByTypesWithException() { String filePath = "./auth-test-test"; String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -256,9 +256,9 @@ public void testAuthRestoreByTypesWithException() { Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); Assert.assertContains("valid value is 'all' or combination of " + - "[user,group,target,belong,access]", msg); + "[user,group,target,belong,access]", + e.getMessage()); }); } @@ -267,7 +267,7 @@ public void testAuthRestoreByTypesWithBelongException() { String filePath = "./auth-test-test"; String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -280,9 +280,9 @@ public void testAuthRestoreByTypesWithBelongException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); Assert.assertContains("if type contains 'belong' then " + - "'user' and 'group' are required.", msg); + "'user' and 'group' are required.", + e.getMessage()); }); } @@ -291,7 +291,7 @@ public void testAuthRestoreByTypesWithAccessException() { String filePath = "./auth-test-test"; String[] args = new String[]{ - "--test-mode", "true", + "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", @@ -304,15 +304,17 @@ public void testAuthRestoreByTypesWithAccessException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, (e) -> { - String msg = e.getMessage(); Assert.assertContains("if type contains 'access' then " + - "'group' and 'target' are required.", msg); + "'group' and 'target' are required.", + e.getMessage()); }); } private void loadData(HugeType hugeType, String dataFilePath) { - List list = FileUtil.read(FileUtil.configPath(DEFAULT_TEST_URL + - dataFilePath)); - FileUtil.writeText(DEFAULT_URL + hugeType.string(), list); + String restoreDataPath = DEFAULT_URL + hugeType.string(); + String testRestoreDataPath = DEFAULT_TEST_URL + dataFilePath; + + List list = FileUtil.read(FileUtil.configPath(testRestoreDataPath)); + FileUtil.writeText(restoreDataPath, list); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java new file mode 100644 index 0000000..ed23bc4 --- /dev/null +++ b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.test.functional; + +import org.junit.Test; + +import com.baidu.hugegraph.cmd.HugeGraphCommand; +import com.baidu.hugegraph.exception.ExitException; +import com.baidu.hugegraph.testutil.Assert; + +public class CommandTest extends AuthTest { + + @Test + public void helpCommandTest() { + String[] args = new String[]{ + "--throw-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "help" + }; + + Assert.assertThrows(ExitException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + Assert.assertContains("Command : hugegragh help", + e.getMessage()); + }); + } + + @Test + public void helpSubCommandTest() { + String[] args = new String[]{ + "--throw-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "help", "auth-backup" + }; + + Assert.assertThrows(ExitException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + Assert.assertContains("Hugegragh help auth-backup", + e.getMessage()); + }); + } + + @Test + public void badHelpSubCommandTest() { + String badCommand = "asd"; + String[] args = new String[]{ + "--throw-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "help", badCommand + }; + + Assert.assertThrows(ExitException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + Assert.assertContains(String.format( + "Unexpected help sub-command %s", + badCommand), e.getMessage()); + }); + } + + @Test + public void emptyCommandTest() { + String[] args = new String[]{ + "--throw-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD + }; + + Assert.assertThrows(ExitException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + Assert.assertContains("No sub-Command found", + e.getMessage()); + }); + } +} diff --git a/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java b/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java index 0a9a785..6e770f6 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/FuncTestSuite.java @@ -25,7 +25,8 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ AuthBackupTest.class, - AuthRestoreTest.class + AuthRestoreTest.class, + CommandTest.class }) public class FuncTestSuite { } diff --git a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java index 7d73943..a4f54f0 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java @@ -30,7 +30,7 @@ public class ClientUtil { private Integer timeout; private String trustStoreFile; private String trustStorePassword; - protected HugeClient hugeClient; + private HugeClient hugeClient; public ClientUtil(String url, String graph, String username, String password, Integer timeout, @@ -42,18 +42,18 @@ public ClientUtil(String url, String graph, String username, this.timeout = timeout; this.trustStoreFile = trustStoreFile; this.trustStorePassword = trustStorePassword; - this.client(); + this.hugeClient(); } - protected void client() { + protected void hugeClient() { this.hugeClient = HugeClient.builder(this.url, this.graph) .configUser(this.username, this.password) .configTimeout(this.timeout) - .configSSL(trustStoreFile, trustStorePassword) + .configSSL(this.trustStoreFile, this.trustStorePassword) .build(); } - public HugeClient hugeClient() { + public HugeClient client() { return this.hugeClient; } } From 0e569ac6c34a379075c6c07057fbea37006ee8d5 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 14 Dec 2020 14:35:14 +0800 Subject: [PATCH 13/26] Add inner class for auth restore --- .../hugegraph/manager/AuthRestoreManager.java | 591 +++++++++--------- .../test/functional/AuthRestoreTest.java | 24 + 2 files changed, 330 insertions(+), 285 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index d91c0dc..be652ed 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; @@ -68,6 +69,7 @@ public class AuthRestoreManager extends BackupRestoreBaseManager { private Map targetsByName; private Map belongsByName; private Map accessesByName; + private List authRestores; public AuthRestoreManager(ToolClient.ConnectionInfo info) { super(info, AUTH_RESTORE_DIR); @@ -85,13 +87,14 @@ public void init(SubCommands.AuthRestore authRestore) { this.targetsByName = Maps.newHashMap(); this.belongsByName = Maps.newHashMap(); this.accessesByName = Maps.newHashMap(); + this.authRestores = Lists.newArrayList(); } public void authRestore(List types) { List sortedHugeTypes = this.sortListByCode(types); try { - this.doAuthCheckConflict(sortedHugeTypes); - this.doAuthRestore(sortedHugeTypes); + this.doAddAuths(sortedHugeTypes); + this.doAuthRestore(); } catch (Throwable e) { throw e; } finally { @@ -99,23 +102,23 @@ public void authRestore(List types) { } } - private void doAuthCheckConflict(List types) { + private void doAddAuths(List types) { for (HugeType type : types) { switch (type) { case USER: - this.checkUsersConflict(); + this.authRestores.add(new AuthUser()); break; case GROUP: - this.checkGroupsConflict(); + this.authRestores.add(new AuthGroup()); break; case TARGET: - this.checkTargetsConflict(); + this.authRestores.add(new AuthTarget()); break; case BELONG: - this.checkBelongsConflict(); + this.authRestores.add(new AuthBelong()); break; case ACCESS: - this.checkAccessesConflict(); + this.authRestores.add(new AuthAccess()); break; default: throw new AssertionError(String.format( @@ -124,288 +127,17 @@ private void doAuthCheckConflict(List types) { } } - private void doAuthRestore(List types) { - for (HugeType type : types) { - switch (type) { - case USER: - this.restoreUsers(); - break; - case GROUP: - this.restoreGroups(); - break; - case TARGET: - this.restoreTargets(); - break; - case BELONG: - this.restoreBelongs(); - break; - case ACCESS: - this.restoreAccesses(); - break; - default: - throw new AssertionError(String.format( - "Bad auth restore type: %s", type)); - } - } - } - - protected void checkUsersConflict() { - List users = retry(this.client.authManager()::listUsers, - "Querying users of authority"); - Map userMap = Maps.newHashMap(); - for (User user : users) { - userMap.put(user.name(), user); - } - List userJsons = this.read(HugeType.USER); - for (String user : userJsons) { - int conflict = conflict_status; - User restoreUser = JsonUtil.fromJson(user, User.class); - if (!userMap.containsKey(restoreUser.name())) { - this.prepareUserForRestore(restoreUser); - continue; - } - User existUser = userMap.get(restoreUser.name()); - if (!StringUtils.equals(existUser.phone(), - restoreUser.phone())) { - conflict++; - } - if (!StringUtils.equals(existUser.email(), - restoreUser.email())) { - conflict++; - } - if (!StringUtils.equals(existUser.avatar(), - restoreUser.avatar())) { - conflict++; - } - if (conflict > conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore users conflict with STOP strategy, " + - "user name is s%", restoreUser.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore users strategy is not found"); - } else { - this.idsMap.put(restoreUser.id().toString(), - existUser.id().toString()); + private void doAuthRestore() { + if (CollectionUtils.isNotEmpty(this.authRestores)) { + for (AuthRestore authRestore : this.authRestores) { + authRestore.checkConflict(); } - } - } - - protected void checkGroupsConflict() { - List groups = retry(this.client.authManager()::listGroups, - "Querying users of authority"); - Map groupMap = Maps.newHashMap(); - for (Group group : groups) { - groupMap.put(group.name(), group); - } - List groupJsons = this.read(HugeType.GROUP); - for (String group : groupJsons) { - int conflict = conflict_status; - Group restoreGroup = JsonUtil.fromJson(group, Group.class); - if (!groupMap.containsKey(restoreGroup.name())) { - this.prepareGroupForRestore(restoreGroup); - continue; - } - Group existGroup = groupMap.get(restoreGroup.name()); - if (!StringUtils.equals(existGroup.description(), - restoreGroup.description())) { - conflict++; - } - if (conflict > conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore groups conflict with STOP strategy, " + - "group name is s%", restoreGroup.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore groups strategy is not found"); - } else { - this.idsMap.put(restoreGroup.id().toString(), - existGroup.id().toString()); + for (AuthRestore authRestore : this.authRestores) { + authRestore.restore(); } } } - protected void checkTargetsConflict() { - List targets = retry(this.client.authManager()::listTargets, - "Querying targets of authority"); - Map targetMap = Maps.newHashMap(); - for (Target target : targets) { - targetMap.put(target.name(), target); - } - List targetJsons = this.read(HugeType.TARGET); - for (String target : targetJsons) { - int conflict = conflict_status; - Target restoreTarget = JsonUtil.fromJson(target, Target.class); - if (!targetMap.containsKey(restoreTarget.name())) { - this.prepareTargetForRestore(restoreTarget); - continue; - } - Target existTarget = targetMap.get(restoreTarget.name()); - if (!StringUtils.equals(existTarget.graph(), - restoreTarget.graph())) { - conflict++; - } - if (!StringUtils.equals(existTarget.url(), - restoreTarget.url())) { - conflict++; - } - if (conflict > conflict_status) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore targets conflict with STOP strategy, " + - "target name is s%", restoreTarget.name()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore targets strategy is not found"); - } else { - this.idsMap.put(restoreTarget.id().toString(), - existTarget.id().toString()); - } - } - } - - protected void checkBelongsConflict() { - List belongs = retry(this.client.authManager()::listBelongs, - "Querying belongs of authority"); - Map belongMap = Maps.newHashMap(); - for (Belong belong : belongs) { - belongMap.put(belong.user() + ":" + belong.group(), - belong); - } - List belongJsons = this.read(HugeType.BELONG); - for (String belong : belongJsons) { - Belong restoreBelong = JsonUtil.fromJson(belong, Belong.class); - if (!checkAllExistInIdMaps(restoreBelong.user().toString(), - restoreBelong.group().toString())) { - continue; - } - String ids = this.idsMap.get(restoreBelong.user()) + ":" + - this.idsMap.get(restoreBelong.group()); - if (belongMap.containsKey(ids)) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore belongs conflict with STOP strategy, " + - "belong id is s%", restoreBelong.id()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore belongs strategy is not found"); - continue; - } - this.belongsByName.put(restoreBelong.id().toString(), restoreBelong); - } - } - - protected void checkAccessesConflict() { - List accesses = retry(this.client.authManager()::listAccesses, - "Querying accesses of authority"); - Map accessMap = Maps.newHashMap(); - for (Access access : accesses) { - accessMap.put(access.group() + ":" + access.target(), - access); - } - List accessJsons = this.read(HugeType.ACCESS); - for (String access : accessJsons) { - Access restoreAccess = JsonUtil.fromJson(access, Access.class); - if (!checkAllExistInIdMaps(restoreAccess.group().toString(), - restoreAccess.target().toString())) { - continue; - } - String ids = this.idsMap.get(restoreAccess.group()) + ":" + - this.idsMap.get(restoreAccess.target()); - if (accessMap.containsKey(ids)) { - E.checkArgument(this.strategy != AuthRestoreStrategy.STOP, - "Restore accesses conflict with STOP strategy," + - "accesses id is s%", restoreAccess.id()); - E.checkArgument(this.strategy == AuthRestoreStrategy.STOP || - this.strategy == AuthRestoreStrategy.IGNORE, - "Restore accesses strategy is not found"); - continue; - } - this.accessesByName.put(restoreAccess.id().toString(), restoreAccess); - } - } - - protected void restoreAccesses() { - int count = 0; - for (Map.Entry entry : this.accessesByName.entrySet()) { - Access restoreAccess = entry.getValue(); - restoreAccess.target(this.idsMap.get(restoreAccess.target().toString())); - restoreAccess.group(this.idsMap.get(restoreAccess.group().toString())); - retry(() -> { - return this.client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); - count++; - } - Printer.print("Restore accesses finished, count is %d !", count); - } - - protected void restoreBelongs() { - int count = 0; - for (Map.Entry entry : this.belongsByName.entrySet()) { - Belong restoreBelong = entry.getValue(); - restoreBelong.user(this.idsMap.get(restoreBelong.user().toString())); - restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); - retry(() -> { - return this.client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); - count++; - } - Printer.print("Restore belongs finished, count is %d !", count); - } - - protected void restoreTargets() { - int count = 0; - for (Map.Entry entry : this.targetsByName.entrySet()) { - Target restoreTarget = entry.getValue(); - Target target = retry(() -> { - return this.client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); - this.idsMap.put(restoreTarget.id().toString(), target.id().toString()); - count++; - } - Printer.print("Restore targets finished, count is %d !", count); - } - - protected void restoreGroups() { - int count = 0; - for (Map.Entry entry : this.groupsByName.entrySet()) { - Group restoreGroup = entry.getValue(); - Group group = retry(() -> { - return this.client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); - this.idsMap.put(restoreGroup.id().toString(), group.id().toString()); - count++; - } - Printer.print("Restore groups finished, count is %d !", count); - } - - protected void restoreUsers() { - int count = 0; - for (Map.Entry entry : this.usersByName.entrySet()) { - User restoreUser = entry.getValue(); - restoreUser.password(this.initPassword); - User user = retry(() -> { - return this.client.authManager().createUser(restoreUser); - }, "Restore users of authority"); - this.idsMap.put(restoreUser.id().toString(), user.id().toString()); - count++; - } - Printer.print("Restore users finished, count is %d !", count); - } - - protected void prepareTargetForRestore(Target restoreTarget) { - this.idsMap.put(restoreTarget.id().toString(), restoreTarget.id().toString()); - this.targetsByName.put(restoreTarget.name(), restoreTarget); - } - - protected void prepareGroupForRestore(Group restoreGroup) { - this.idsMap.put(restoreGroup.id().toString(), restoreGroup.id().toString()); - this.groupsByName.put(restoreGroup.name(), restoreGroup); - } - - protected void prepareUserForRestore(User restoreUser) { - this.idsMap.put(restoreUser.id().toString(), restoreUser.id().toString()); - this.usersByName.put(restoreUser.name(), restoreUser); - } - private boolean checkAllExistInIdMaps(String oneId, String otherId) { if (this.idsMap.containsKey(oneId) && this.idsMap.containsKey(otherId)) { @@ -455,4 +187,293 @@ public void initPassword(List types, String password) { this.initPassword = password; } } + + private abstract class AuthRestore { + + public abstract void checkConflict(); + + public abstract void restore(); + } + + private class AuthUser extends AuthRestore{ + + @Override + public void checkConflict() { + List users = retry(client.authManager()::listUsers, + "Querying users of authority"); + Map userMap = Maps.newHashMap(); + for (User user : users) { + userMap.put(user.name(), user); + } + List userJsons = read(HugeType.USER); + for (String user : userJsons) { + int conflict = conflict_status; + User restoreUser = JsonUtil.fromJson(user, User.class); + if (!userMap.containsKey(restoreUser.name())) { + this.prepareUserForRestore(restoreUser); + continue; + } + User existUser = userMap.get(restoreUser.name()); + if (!StringUtils.equals(existUser.phone(), + restoreUser.phone())) { + conflict++; + } + if (!StringUtils.equals(existUser.email(), + restoreUser.email())) { + conflict++; + } + if (!StringUtils.equals(existUser.avatar(), + restoreUser.avatar())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(strategy != AuthRestoreStrategy.STOP, + "Restore users conflict with STOP strategy, " + + "user name is s%", restoreUser.name()); + E.checkArgument(strategy == AuthRestoreStrategy.STOP || + strategy == AuthRestoreStrategy.IGNORE, + "Restore users strategy is not found"); + } else { + idsMap.put(restoreUser.id().toString(), + existUser.id().toString()); + } + } + } + + @Override + public void restore() { + int count = 0; + for (Map.Entry entry : usersByName.entrySet()) { + User restoreUser = entry.getValue(); + restoreUser.password(initPassword); + User user = retry(() -> { + return client.authManager().createUser(restoreUser); + }, "Restore users of authority"); + idsMap.put(restoreUser.id().toString(), user.id().toString()); + count++; + } + Printer.print("Restore users finished, count is %d !", count); + } + + private void prepareUserForRestore(User restoreUser) { + idsMap.put(restoreUser.id().toString(), restoreUser.id().toString()); + usersByName.put(restoreUser.name(), restoreUser); + } + } + + private class AuthGroup extends AuthRestore { + + @Override + public void checkConflict() { + List groups = retry(client.authManager()::listGroups, + "Querying users of authority"); + Map groupMap = Maps.newHashMap(); + for (Group group : groups) { + groupMap.put(group.name(), group); + } + List groupJsons = read(HugeType.GROUP); + for (String group : groupJsons) { + int conflict = conflict_status; + Group restoreGroup = JsonUtil.fromJson(group, Group.class); + if (!groupMap.containsKey(restoreGroup.name())) { + this.prepareGroupForRestore(restoreGroup); + continue; + } + Group existGroup = groupMap.get(restoreGroup.name()); + if (!StringUtils.equals(existGroup.description(), + restoreGroup.description())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(strategy != AuthRestoreStrategy.STOP, + "Restore groups conflict with STOP strategy, " + + "group name is s%", restoreGroup.name()); + E.checkArgument(strategy == AuthRestoreStrategy.STOP || + strategy == AuthRestoreStrategy.IGNORE, + "Restore groups strategy is not found"); + } else { + idsMap.put(restoreGroup.id().toString(), + existGroup.id().toString()); + } + } + } + + @Override + public void restore() { + int count = 0; + for (Map.Entry entry : groupsByName.entrySet()) { + Group restoreGroup = entry.getValue(); + Group group = retry(() -> { + return client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); + idsMap.put(restoreGroup.id().toString(), group.id().toString()); + count++; + } + Printer.print("Restore groups finished, count is %d !", count); + } + + protected void prepareGroupForRestore(Group restoreGroup) { + idsMap.put(restoreGroup.id().toString(), restoreGroup.id().toString()); + groupsByName.put(restoreGroup.name(), restoreGroup); + } + } + + private class AuthTarget extends AuthRestore { + + @Override + public void checkConflict() { + List targets = retry(client.authManager()::listTargets, + "Querying targets of authority"); + Map targetMap = Maps.newHashMap(); + for (Target target : targets) { + targetMap.put(target.name(), target); + } + List targetJsons = read(HugeType.TARGET); + for (String target : targetJsons) { + int conflict = conflict_status; + Target restoreTarget = JsonUtil.fromJson(target, Target.class); + if (!targetMap.containsKey(restoreTarget.name())) { + this.prepareTargetForRestore(restoreTarget); + continue; + } + Target existTarget = targetMap.get(restoreTarget.name()); + if (!StringUtils.equals(existTarget.graph(), + restoreTarget.graph())) { + conflict++; + } + if (!StringUtils.equals(existTarget.url(), + restoreTarget.url())) { + conflict++; + } + if (conflict > conflict_status) { + E.checkArgument(strategy != AuthRestoreStrategy.STOP, + "Restore targets conflict with STOP strategy, " + + "target name is s%", restoreTarget.name()); + E.checkArgument(strategy == AuthRestoreStrategy.STOP || + strategy == AuthRestoreStrategy.IGNORE, + "Restore targets strategy is not found"); + } else { + idsMap.put(restoreTarget.id().toString(), + existTarget.id().toString()); + } + } + } + + @Override + public void restore() { + int count = 0; + for (Map.Entry entry : targetsByName.entrySet()) { + Target restoreTarget = entry.getValue(); + Target target = retry(() -> { + return client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); + idsMap.put(restoreTarget.id().toString(), target.id().toString()); + count++; + } + Printer.print("Restore targets finished, count is %d !", count); + } + + protected void prepareTargetForRestore(Target restoreTarget) { + idsMap.put(restoreTarget.id().toString(), restoreTarget.id().toString()); + targetsByName.put(restoreTarget.name(), restoreTarget); + } + } + + private class AuthBelong extends AuthRestore { + + @Override + public void checkConflict() { + List belongs = retry(client.authManager()::listBelongs, + "Querying belongs of authority"); + Map belongMap = Maps.newHashMap(); + for (Belong belong : belongs) { + belongMap.put(belong.user() + ":" + belong.group(), + belong); + } + List belongJsons = read(HugeType.BELONG); + for (String belong : belongJsons) { + Belong restoreBelong = JsonUtil.fromJson(belong, Belong.class); + if (!checkAllExistInIdMaps(restoreBelong.user().toString(), + restoreBelong.group().toString())) { + continue; + } + String ids = idsMap.get(restoreBelong.user()) + ":" + + idsMap.get(restoreBelong.group()); + if (belongMap.containsKey(ids)) { + E.checkArgument(strategy != AuthRestoreStrategy.STOP, + "Restore belongs conflict with STOP strategy, " + + "belong id is s%", restoreBelong.id()); + E.checkArgument(strategy == AuthRestoreStrategy.STOP || + strategy == AuthRestoreStrategy.IGNORE, + "Restore belongs strategy is not found"); + continue; + } + belongsByName.put(restoreBelong.id().toString(), restoreBelong); + } + } + + @Override + public void restore() { + int count = 0; + for (Map.Entry entry : belongsByName.entrySet()) { + Belong restoreBelong = entry.getValue(); + restoreBelong.user(idsMap.get(restoreBelong.user().toString())); + restoreBelong.group(idsMap.get(restoreBelong.group().toString())); + retry(() -> { + return client.authManager().createBelong(restoreBelong); + }, "Restore belongs of authority"); + count++; + } + Printer.print("Restore belongs finished, count is %d !", count); + } + } + + private class AuthAccess extends AuthRestore { + + @Override + public void checkConflict() { + List accesses = retry(client.authManager()::listAccesses, + "Querying accesses of authority"); + Map accessMap = Maps.newHashMap(); + for (Access access : accesses) { + accessMap.put(access.group() + ":" + access.target(), + access); + } + List accessJsons = read(HugeType.ACCESS); + for (String access : accessJsons) { + Access restoreAccess = JsonUtil.fromJson(access, Access.class); + if (!checkAllExistInIdMaps(restoreAccess.group().toString(), + restoreAccess.target().toString())) { + continue; + } + String ids = idsMap.get(restoreAccess.group()) + ":" + + idsMap.get(restoreAccess.target()); + if (accessMap.containsKey(ids)) { + E.checkArgument(strategy != AuthRestoreStrategy.STOP, + "Restore accesses conflict with STOP strategy," + + "accesses id is s%", restoreAccess.id()); + E.checkArgument(strategy == AuthRestoreStrategy.STOP || + strategy == AuthRestoreStrategy.IGNORE, + "Restore accesses strategy is not found"); + continue; + } + accessesByName.put(restoreAccess.id().toString(), restoreAccess); + } + } + + @Override + public void restore() { + int count = 0; + for (Map.Entry entry : accessesByName.entrySet()) { + Access restoreAccess = entry.getValue(); + restoreAccess.target(idsMap.get(restoreAccess.target().toString())); + restoreAccess.group(idsMap.get(restoreAccess.group().toString())); + retry(() -> { + return client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); + count++; + } + Printer.print("Restore accesses finished, count is %d !", count); + } + } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 31f6a11..ba1d28c 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -317,4 +317,28 @@ private void loadData(HugeType hugeType, String dataFilePath) { List list = FileUtil.read(FileUtil.configPath(testRestoreDataPath)); FileUtil.writeText(restoreDataPath, list); } + + @Test + public void testAuthRestoreByStrategyWithException() { + String filePath = "./auth-test-test"; + + String[] args = new String[]{ + "--throw-mode", "true", + "--user", USER_NAME, + "--password", USER_PASSWORD, + "auth-restore", + "--types", "user", + "--strategy", "test", + "--init-password", "123456", + "--directory", filePath + }; + + Assert.assertThrows(IllegalArgumentException.class, () -> { + HugeGraphCommand.main(args); + }, (e) -> { + Assert.assertContains("Invalid --strategy 'test', valid " + + "value is 'stop' or 'ignore", + e.getMessage()); + }); + } } From fdf2512b04caa667a29576765b36dc5df50c0693 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 14 Dec 2020 17:08:34 +0800 Subject: [PATCH 14/26] Update and optimize some code --- assembly/travis/conf/gremlin-server.yaml | 16 ++++- assembly/travis/conf/hugegraph.properties | 9 ++- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 22 +++---- .../com/baidu/hugegraph/cmd/SubCommands.java | 12 ++-- .../hugegraph/exception/ExitException.java | 18 +++++- .../hugegraph/manager/AuthBackupManager.java | 14 ++--- .../hugegraph/manager/AuthRestoreManager.java | 10 ++-- .../test/functional/AuthBackupTest.java | 4 +- .../test/functional/AuthRestoreTest.java | 58 +++++++++--------- .../test/functional/CommandTest.java | 16 ++--- .../baidu/hugegraph/test/util/ClientUtil.java | 59 ------------------- 11 files changed, 108 insertions(+), 130 deletions(-) delete mode 100644 src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java diff --git a/assembly/travis/conf/gremlin-server.yaml b/assembly/travis/conf/gremlin-server.yaml index 600e631..484ee12 100644 --- a/assembly/travis/conf/gremlin-server.yaml +++ b/assembly/travis/conf/gremlin-server.yaml @@ -17,16 +17,30 @@ scriptEngines: { org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: { classImports: [ java.lang.Math, + com.baidu.hugegraph.backend.id.IdGenerator, com.baidu.hugegraph.type.define.Directions, - com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser, + com.baidu.hugegraph.type.define.NodeRole, + com.baidu.hugegraph.traversal.algorithm.CollectionPathsTraverser, + com.baidu.hugegraph.traversal.algorithm.CountTraverser, com.baidu.hugegraph.traversal.algorithm.CustomizedCrosspointsTraverser, + com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser, com.baidu.hugegraph.traversal.algorithm.FusiformSimilarityTraverser, com.baidu.hugegraph.traversal.algorithm.HugeTraverser, + com.baidu.hugegraph.traversal.algorithm.JaccardSimilarTraverser, + com.baidu.hugegraph.traversal.algorithm.KneighborTraverser, + com.baidu.hugegraph.traversal.algorithm.KoutTraverser, + com.baidu.hugegraph.traversal.algorithm.MultiNodeShortestPathTraverser, com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser, com.baidu.hugegraph.traversal.algorithm.PathsTraverser, com.baidu.hugegraph.traversal.algorithm.PersonalRankTraverser, + com.baidu.hugegraph.traversal.algorithm.SameNeighborTraverser, com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser, + com.baidu.hugegraph.traversal.algorithm.SingleSourceShortestPathTraverser, com.baidu.hugegraph.traversal.algorithm.SubGraphTraverser, + com.baidu.hugegraph.traversal.algorithm.TemplatePathsTraverser, + com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep, + com.baidu.hugegraph.traversal.algorithm.steps.RepeatEdgeStep, + com.baidu.hugegraph.traversal.algorithm.steps.WeightedEdgeStep, com.baidu.hugegraph.traversal.optimize.Text, com.baidu.hugegraph.traversal.optimize.TraversalUtil, com.baidu.hugegraph.util.DateUtil diff --git a/assembly/travis/conf/hugegraph.properties b/assembly/travis/conf/hugegraph.properties index 9c683e9..c66d0a7 100644 --- a/assembly/travis/conf/hugegraph.properties +++ b/assembly/travis/conf/hugegraph.properties @@ -42,7 +42,7 @@ cassandra.password= # hbase backend config #hbase.hosts=localhost #hbase.port=2181 -#hbase.znode_parent=/hbsae +#hbase.znode_parent=/hbase #hbase.threads_max=64 # mysql backend config @@ -59,3 +59,10 @@ cassandra.password= #palo.poll_interval=10 #palo.temp_dir=./palo-data #palo.file_limit_size=32 + +# postgresql & cockroachdb backend config +#jdbc.driver=org.postgresql.Driver +#jdbc.url=jdbc:postgresql://localhost:5432/ +#jdbc.username=postgres +#jdbc.password= +#jdbc.postgresql.connect_database=template1 diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 715a9d6..a835b2c 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -461,8 +461,9 @@ public JCommander parseCommand(String[] args) { } String subCommand = jCommander.getParsedCommand(); if (subCommand == null) { - throw new ExitException(ToolUtil.commandsCategory(jCommander), - "No sub-Command found"); + throw ExitException.normal(ToolUtil.commandsCategory( + jCommander), + "No sub-Command found"); } return jCommander; } @@ -473,7 +474,7 @@ public boolean parseHelp(String[] args, JCommander jCommander) { if (!list.contains(Constants.COMMAND_HELP)) { return false; } - //parse command for throw mode + //Parse the '--throw-mode' command if (list.contains(Constants.COMMAND_THROW_MODE)) { int index = list.indexOf(Constants.COMMAND_THROW_MODE) + 1; jCommander.parse(Constants.COMMAND_THROW_MODE, @@ -484,18 +485,19 @@ public boolean parseHelp(String[] args, JCommander jCommander) { subCommand = list.get(index + 1); } if (StringUtils.isEmpty(subCommand)) { - throw new ExitException(ToolUtil.commandUsage(jCommander), - "Command : hugegragh help"); + throw ExitException.normal(ToolUtil.commandUsage(jCommander), + "Command : hugegragh help"); } Map commands = jCommander.getCommands(); if (commands.containsKey(subCommand)) { - throw new ExitException(ToolUtil.commandUsage(commands.get(subCommand)), - String.format("Hugegragh help %s", subCommand)); + throw ExitException.normal(ToolUtil.commandUsage( + commands.get(subCommand)), + "Hugegragh help %s", subCommand); } else { throw ExitException.exception(ToolUtil.commandsCategory(jCommander), - String.format("Unexpected help " + - "sub-command %s", subCommand)); + "Unexpected help sub-command " + + "%s", subCommand); } } @@ -523,7 +525,7 @@ public static void main(String[] args) { cmd.shutdown(); } - if(exitCode == Constants.EXIT_CODE_ERROR) { + if (exitCode != Constants.EXIT_CODE_NORMAL) { System.exit(exitCode); } } diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 5f11254..4503dcc 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -700,11 +700,11 @@ public static class HugeTypes { @Parameter(names = {"--huge-types", "-t"}, listConverter = HugeTypeListConverter.class, description = "Type of schema/data. Concat with ',' if more " + - "than one. other types include ‘all’ and ‘schema’" + - " .’all' means all vertices, edges and schema, in " + + "than one. other types include 'all' and 'schema'" + + " .'all' means all vertices, edges and schema, in " + "other words, 'all' equals with 'vertex, edge, " + - "vertex_label, edge_label, property_key, index_label’." + - " ‘schema’ equals with 'vertex_label, edge_label, " + + "vertex_label, edge_label, property_key, index_label'." + + " 'schema' equals with 'vertex_label, edge_label, " + "property_key, index_label'.") public List types = HugeTypeListConverter.ALL_TYPES; } @@ -932,10 +932,10 @@ public static class AuthTypes { description = "Type of auth data to restore and backup, concat with " + "',' if more than one. 'all' means all auth information" + " in other words, 'all' equals with 'user, group, target, " + - "belong, access’. in addition, only 'belong' or 'access' " + + "belong, access'. in addition, only 'belong' or 'access' " + "can not backup or restore, if type contains 'belong' " + "then should contains 'user' and 'group'. if type contains " + - "'access’ then should contains 'group' and 'target'.") + "'access' then should contains 'group' and 'target'.") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } diff --git a/src/main/java/com/baidu/hugegraph/exception/ExitException.java b/src/main/java/com/baidu/hugegraph/exception/ExitException.java index b1e577a..552466d 100644 --- a/src/main/java/com/baidu/hugegraph/exception/ExitException.java +++ b/src/main/java/com/baidu/hugegraph/exception/ExitException.java @@ -32,6 +32,13 @@ public ExitException(String details, String reason) { this.exitCode = Constants.EXIT_CODE_NORMAL; } + public ExitException(String details, String reason, + Object... args) { + super(String.format(reason, args)); + this.details = details; + this.exitCode = Constants.EXIT_CODE_NORMAL; + } + public ExitException(Integer exitCode, String details, String reason) { super(reason); @@ -69,8 +76,15 @@ public Integer exitCode() { return this.exitCode; } - public static ExitException exception(String details, String reason) { + public static ExitException exception(String details, String reason, + Object... args) { return new ExitException(Constants.EXIT_CODE_ERROR, - details, reason); + details, reason, args); + } + + public static ExitException normal(String details, String reason, + Object... args) { + return new ExitException(Constants.EXIT_CODE_NORMAL, + details, reason, args); } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java index 4ad44af..a502c78 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java @@ -63,7 +63,7 @@ public void authBackup(List types) { } } - public void doAuthBackup(List types) { + private void doAuthBackup(List types) { for (HugeType type : types) { switch (type) { case USER: @@ -88,7 +88,7 @@ public void doAuthBackup(List types) { } } - protected void backupUsers() { + private void backupUsers() { Printer.print("Users backup started..."); List users = retry(this.client.authManager()::listUsers, "querying users of authority"); @@ -96,7 +96,7 @@ protected void backupUsers() { Printer.print("Users backup finished, write lines: %d", writeLines); } - protected void backupGroups() { + private void backupGroups() { Printer.print("Groups backup started..."); List groups = retry(this.client.authManager()::listGroups, "querying groups of authority"); @@ -104,7 +104,7 @@ protected void backupGroups() { Printer.print("Groups backup finished, write lines: %d", writeLines); } - protected void backupTargets() { + private void backupTargets() { Printer.print("Targets backup started..."); List targets = retry(this.client.authManager()::listTargets, "querying targets of authority"); @@ -112,7 +112,7 @@ protected void backupTargets() { Printer.print("Targets backup finished, write lines: %d", writeLines); } - protected void backupBelongs() { + private void backupBelongs() { Printer.print("Belongs backup started..."); List belongs = retry(this.client.authManager()::listBelongs, "querying belongs of authority"); @@ -120,7 +120,7 @@ protected void backupBelongs() { Printer.print("Belongs backup finished, write lines: %d", writeLines); } - protected void backupAccesses() { + private void backupAccesses() { Printer.print("Accesses backup started..."); List accesses = retry(this.client.authManager()::listAccesses, "querying accesses of authority"); @@ -128,7 +128,7 @@ protected void backupAccesses() { Printer.print("Accesses backup finished, write lines: %d", writeLines); } - protected long writeText(HugeType type, List list) { + private long writeText(HugeType type, List list) { long count = 0L; try { OutputStream os = this.outputStream(type.string(), false); diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java index be652ed..63717ec 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java @@ -146,7 +146,7 @@ private boolean checkAllExistInIdMaps(String oneId, String otherId) { return false; } - protected List read(HugeType type) { + private List read(HugeType type) { List resultList = Lists.newArrayList(); InputStream is = this.inputStream(type.string()); try (InputStreamReader isr = new InputStreamReader(is, API.CHARSET); @@ -173,13 +173,13 @@ protected void directory(String dir, Map hdfsConf) { } } - public List sortListByCode(List hugeTypes) { + private List sortListByCode(List hugeTypes) { return hugeTypes.stream(). sorted(Comparator.comparing(HugeType::code)). collect(Collectors.toList()); } - public void initPassword(List types, String password) { + private void initPassword(List types, String password) { if (types.contains(HugeType.USER) && Strings.isEmpty(password)) { throw new IllegalArgumentException(String.format( "The following option is required: [--init-password]")); @@ -312,7 +312,7 @@ public void restore() { Printer.print("Restore groups finished, count is %d !", count); } - protected void prepareGroupForRestore(Group restoreGroup) { + private void prepareGroupForRestore(Group restoreGroup) { idsMap.put(restoreGroup.id().toString(), restoreGroup.id().toString()); groupsByName.put(restoreGroup.name(), restoreGroup); } @@ -373,7 +373,7 @@ public void restore() { Printer.print("Restore targets finished, count is %d !", count); } - protected void prepareTargetForRestore(Target restoreTarget) { + private void prepareTargetForRestore(Target restoreTarget) { idsMap.put(restoreTarget.id().toString(), restoreTarget.id().toString()); targetsByName.put(restoreTarget.name(), restoreTarget); } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 9c464d6..0c6ae41 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -70,7 +70,7 @@ public void testAuthBackupByTypes() { } @Test - public void testAuthBackupByTypesWithException() { + public void testAuthBackupWithTypesException() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -81,7 +81,7 @@ public void testAuthBackupByTypesWithException() { Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("valid value is 'all' or combination of " + "[user,group,target,belong,access]", e.getMessage()); diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index ba1d28c..1155ef8 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -34,7 +34,6 @@ import com.baidu.hugegraph.structure.auth.Target; import com.baidu.hugegraph.structure.auth.User; import com.baidu.hugegraph.structure.constant.HugeType; -import com.baidu.hugegraph.test.util.ClientUtil; import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; import com.beust.jcommander.ParameterException; @@ -47,14 +46,15 @@ public class AuthRestoreTest extends AuthTest { @Before public void init() { - ClientUtil clientUtil = new ClientUtil(URL, GRAPH, USER_NAME, - USER_PASSWORD, TIME_OUT, - TRUST_STORE_FILE, TRUST_STORE_PASSWORD); - this.client = clientUtil.client(); + client = HugeClient.builder(URL, GRAPH) + .configUser(USER_NAME, USER_PASSWORD) + .configTimeout(TIME_OUT) + .configSSL(TRUST_STORE_FILE, TRUST_STORE_PASSWORD) + .build(); } @Test - public void testAuthRestoreByAll() { + public void testAuthRestoreForAllType() { this.loadData(HugeType.USER, "auth_users.txt"); this.loadData(HugeType.TARGET, "auth_targets.txt"); this.loadData(HugeType.GROUP, "auth_groups.txt"); @@ -124,7 +124,7 @@ public void testAuthRestoreByAll() { } @Test - public void testAuthRestoreByUser() { + public void testAuthRestoreForUser() { this.loadData(HugeType.USER, "auth_users.txt"); String[] args = new String[]{ @@ -149,7 +149,7 @@ public void testAuthRestoreByUser() { } @Test - public void testAuthRestoreWithException() { + public void testRestoreWithInitPasswordException() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -161,7 +161,7 @@ public void testAuthRestoreWithException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { String msg = e.getMessage(); Assert.assertTrue(msg.endsWith("The following option is " + "required: [--init-password]")); @@ -169,7 +169,7 @@ public void testAuthRestoreWithException() { } @Test - public void testAuthRestoreByStrategyConflict() { + public void testAuthRestoreWithStrategyException() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ @@ -184,14 +184,14 @@ public void testAuthRestoreByStrategyConflict() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("Restore users conflict with STOP strategy", e.getMessage()); }); } @Test - public void testAuthRestoreByStrategyIgnore() { + public void testAuthRestoreWithStrategyIgnore() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ @@ -212,11 +212,11 @@ public void testAuthRestoreByStrategyIgnore() { userMap.put(user1.name(), user1); } - Assert.assertTrue(userMap.containsKey("test_user1")); + Assert.assertTrue(userMap.containsKey("admin")); } @Test - public void testAuthRestoreByDirectoryException() { + public void testAuthRestoreWithDirectoryException() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -232,14 +232,14 @@ public void testAuthRestoreByDirectoryException() { Assert.assertThrows(IllegalStateException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("The directory does not exist", e.getMessage()); }); } @Test - public void testAuthRestoreByTypesWithException() { + public void testAuthRestoreWithTypesException() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -255,7 +255,7 @@ public void testAuthRestoreByTypesWithException() { Assert.assertThrows(ParameterException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("valid value is 'all' or combination of " + "[user,group,target,belong,access]", e.getMessage()); @@ -279,7 +279,7 @@ public void testAuthRestoreByTypesWithBelongException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("if type contains 'belong' then " + "'user' and 'group' are required.", e.getMessage()); @@ -303,23 +303,15 @@ public void testAuthRestoreByTypesWithAccessException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("if type contains 'access' then " + "'group' and 'target' are required.", e.getMessage()); }); } - private void loadData(HugeType hugeType, String dataFilePath) { - String restoreDataPath = DEFAULT_URL + hugeType.string(); - String testRestoreDataPath = DEFAULT_TEST_URL + dataFilePath; - - List list = FileUtil.read(FileUtil.configPath(testRestoreDataPath)); - FileUtil.writeText(restoreDataPath, list); - } - @Test - public void testAuthRestoreByStrategyWithException() { + public void testAuthRestoreWithBadStrategyException() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -335,10 +327,18 @@ public void testAuthRestoreByStrategyWithException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("Invalid --strategy 'test', valid " + "value is 'stop' or 'ignore", e.getMessage()); }); } + + private void loadData(HugeType hugeType, String dataFilePath) { + String restoreDataPath = DEFAULT_URL + hugeType.string(); + String testRestoreDataPath = DEFAULT_TEST_URL + dataFilePath; + + List list = FileUtil.read(FileUtil.configPath(testRestoreDataPath)); + FileUtil.writeText(restoreDataPath, list); + } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java index ed23bc4..acc8345 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java @@ -28,7 +28,7 @@ public class CommandTest extends AuthTest { @Test - public void helpCommandTest() { + public void testHelpCommand() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -38,14 +38,14 @@ public void helpCommandTest() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("Command : hugegragh help", e.getMessage()); }); } @Test - public void helpSubCommandTest() { + public void testHelpSubCommand() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -55,14 +55,14 @@ public void helpSubCommandTest() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("Hugegragh help auth-backup", e.getMessage()); }); } @Test - public void badHelpSubCommandTest() { + public void testBadHelpSubCommandException() { String badCommand = "asd"; String[] args = new String[]{ "--throw-mode", "true", @@ -73,7 +73,7 @@ public void badHelpSubCommandTest() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains(String.format( "Unexpected help sub-command %s", badCommand), e.getMessage()); @@ -81,7 +81,7 @@ public void badHelpSubCommandTest() { } @Test - public void emptyCommandTest() { + public void testEmptyCommandException() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -90,7 +90,7 @@ public void emptyCommandTest() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); - }, (e) -> { + }, e -> { Assert.assertContains("No sub-Command found", e.getMessage()); }); diff --git a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java b/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java deleted file mode 100644 index a4f54f0..0000000 --- a/src/test/java/com/baidu/hugegraph/test/util/ClientUtil.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package com.baidu.hugegraph.test.util; - -import com.baidu.hugegraph.driver.HugeClient; - -public class ClientUtil { - - private String url; - private String graph; - private String username; - private String password; - private Integer timeout; - private String trustStoreFile; - private String trustStorePassword; - private HugeClient hugeClient; - - public ClientUtil(String url, String graph, String username, - String password, Integer timeout, - String trustStoreFile, String trustStorePassword) { - this.url = url; - this.graph = graph; - this.username = username; - this.password = password; - this.timeout = timeout; - this.trustStoreFile = trustStoreFile; - this.trustStorePassword = trustStorePassword; - this.hugeClient(); - } - - protected void hugeClient() { - this.hugeClient = HugeClient.builder(this.url, this.graph) - .configUser(this.username, this.password) - .configTimeout(this.timeout) - .configSSL(this.trustStoreFile, this.trustStorePassword) - .build(); - } - - public HugeClient client() { - return this.hugeClient; - } -} From a6eb1d9b77b5fd0a2a97f36bcdaf6aea2794e6b2 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 16 Dec 2020 16:43:45 +0800 Subject: [PATCH 15/26] Update and optimize some code --- .../java/com/baidu/hugegraph/cmd/HugeGraphCommand.java | 7 ++++--- src/main/java/com/baidu/hugegraph/cmd/SubCommands.java | 4 ++-- .../com/baidu/hugegraph/test/functional/CommandTest.java | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index a835b2c..48fdba8 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -463,7 +463,7 @@ public JCommander parseCommand(String[] args) { if (subCommand == null) { throw ExitException.normal(ToolUtil.commandsCategory( jCommander), - "No sub-Command found"); + "No sub-command found"); } return jCommander; } @@ -474,7 +474,7 @@ public boolean parseHelp(String[] args, JCommander jCommander) { if (!list.contains(Constants.COMMAND_HELP)) { return false; } - //Parse the '--throw-mode' command + // Parse the '--throw-mode' command if (list.contains(Constants.COMMAND_THROW_MODE)) { int index = list.indexOf(Constants.COMMAND_THROW_MODE) + 1; jCommander.parse(Constants.COMMAND_THROW_MODE, @@ -493,7 +493,8 @@ public boolean parseHelp(String[] args, JCommander jCommander) { if (commands.containsKey(subCommand)) { throw ExitException.normal(ToolUtil.commandUsage( commands.get(subCommand)), - "Hugegragh help %s", subCommand); + "Command : hugegragh help %s", + subCommand); } else { throw ExitException.exception(ToolUtil.commandsCategory(jCommander), "Unexpected help sub-command " + diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 4503dcc..ce23436 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -700,8 +700,8 @@ public static class HugeTypes { @Parameter(names = {"--huge-types", "-t"}, listConverter = HugeTypeListConverter.class, description = "Type of schema/data. Concat with ',' if more " + - "than one. other types include 'all' and 'schema'" + - " .'all' means all vertices, edges and schema, in " + + "than one. other types include 'all' and 'schema'." + + " 'all' means all vertices, edges and " + "schema, in " + "other words, 'all' equals with 'vertex, edge, " + "vertex_label, edge_label, property_key, index_label'." + " 'schema' equals with 'vertex_label, edge_label, " + diff --git a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java index acc8345..6a53797 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java @@ -56,7 +56,7 @@ public void testHelpSubCommand() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - Assert.assertContains("Hugegragh help auth-backup", + Assert.assertContains("Command : hugegragh help auth-backup", e.getMessage()); }); } @@ -91,7 +91,7 @@ public void testEmptyCommandException() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - Assert.assertContains("No sub-Command found", + Assert.assertContains("No sub-command found", e.getMessage()); }); } From 00df91d5e0855fdf0e7ef6679799bf89ca58bae0 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 17 Dec 2020 16:12:03 +0800 Subject: [PATCH 16/26] Update and optimize auth backup --- .../baidu/hugegraph/cmd/HugeGraphCommand.java | 11 +- .../com/baidu/hugegraph/cmd/SubCommands.java | 2 +- .../hugegraph/manager/AuthBackupManager.java | 161 ----------------- ...ger.java => AuthBackupRestoreManager.java} | 168 +++++++++++++++--- 4 files changed, 148 insertions(+), 194 deletions(-) delete mode 100644 src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java rename src/main/java/com/baidu/hugegraph/manager/{AuthRestoreManager.java => AuthBackupRestoreManager.java} (77%) diff --git a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java index 48fdba8..2eb5c53 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java +++ b/src/main/java/com/baidu/hugegraph/cmd/HugeGraphCommand.java @@ -35,8 +35,7 @@ import com.baidu.hugegraph.base.ToolManager; import com.baidu.hugegraph.constant.Constants; import com.baidu.hugegraph.exception.ExitException; -import com.baidu.hugegraph.manager.AuthBackupManager; -import com.baidu.hugegraph.manager.AuthRestoreManager; +import com.baidu.hugegraph.manager.AuthBackupRestoreManager; import com.baidu.hugegraph.manager.BackupManager; import com.baidu.hugegraph.manager.DumpGraphManager; import com.baidu.hugegraph.manager.GraphsManager; @@ -356,18 +355,18 @@ private void execute(String subCmd, JCommander jCommander) { case "auth-backup": Printer.print("Auth backup start..."); SubCommands.AuthBackup authBackup = this.subCommand(subCmd); - AuthBackupManager authBackupManager = manager(AuthBackupManager.class); + AuthBackupRestoreManager authBackupManager = manager(AuthBackupRestoreManager.class); authBackupManager.init(authBackup); - authBackupManager.authBackup(authBackup.types()); + authBackupManager.backup(authBackup.types()); break; case "auth-restore": Printer.print("Auth restore start..."); SubCommands.AuthRestore authRestore = this.subCommand(subCmd); - AuthRestoreManager authRestoreManager = manager(AuthRestoreManager.class); + AuthBackupRestoreManager authRestoreManager = manager(AuthBackupRestoreManager.class); authRestoreManager.init(authRestore); - authRestoreManager.authRestore(authRestore.types()); + authRestoreManager.restore(authRestore.types()); break; default: throw new ParameterException(String.format( diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index ce23436..96b232d 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -701,7 +701,7 @@ public static class HugeTypes { listConverter = HugeTypeListConverter.class, description = "Type of schema/data. Concat with ',' if more " + "than one. other types include 'all' and 'schema'." + - " 'all' means all vertices, edges and " + "schema, in " + + " 'all' means all vertices, edges and schema, in " + "other words, 'all' equals with 'vertex, edge, " + "vertex_label, edge_label, property_key, index_label'." + " 'schema' equals with 'vertex_label, edge_label, " + diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java deleted file mode 100644 index a502c78..0000000 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupManager.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package com.baidu.hugegraph.manager; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; -import java.util.List; -import java.util.Map; - -import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.base.HdfsDirectory; -import com.baidu.hugegraph.base.LocalDirectory; -import com.baidu.hugegraph.base.Printer; -import com.baidu.hugegraph.base.ToolClient; -import com.baidu.hugegraph.cmd.SubCommands; -import com.baidu.hugegraph.exception.ToolsException; -import com.baidu.hugegraph.structure.auth.Access; -import com.baidu.hugegraph.structure.auth.Belong; -import com.baidu.hugegraph.structure.auth.Group; -import com.baidu.hugegraph.structure.auth.Target; -import com.baidu.hugegraph.structure.auth.User; -import com.baidu.hugegraph.structure.constant.HugeType; -import com.baidu.hugegraph.util.JsonUtil; - -public class AuthBackupManager extends BackupRestoreBaseManager { - - private static final String AUTH_BACKUP_NAME = "auth-backup"; - - public AuthBackupManager(ToolClient.ConnectionInfo info) { - super(info, AUTH_BACKUP_NAME); - } - - public void init(SubCommands.AuthBackup authBackup) { - this.retry(authBackup.retry()); - this.directory(authBackup.directory(), authBackup.hdfsConf()); - this.ensureDirectoryExist(true); - } - - public void authBackup(List types) { - try { - this.doAuthBackup(types); - } catch (Throwable e) { - throw e; - } finally { - this.shutdown(this.type()); - } - } - - private void doAuthBackup(List types) { - for (HugeType type : types) { - switch (type) { - case USER: - this.backupUsers(); - break; - case GROUP: - this.backupGroups(); - break; - case TARGET: - this.backupTargets(); - break; - case BELONG: - this.backupBelongs(); - break; - case ACCESS: - this.backupAccesses(); - break; - default: - throw new AssertionError(String.format( - "Bad auth backup type: %s", type)); - } - } - } - - private void backupUsers() { - Printer.print("Users backup started..."); - List users = retry(this.client.authManager()::listUsers, - "querying users of authority"); - long writeLines = this.writeText(HugeType.USER, users); - Printer.print("Users backup finished, write lines: %d", writeLines); - } - - private void backupGroups() { - Printer.print("Groups backup started..."); - List groups = retry(this.client.authManager()::listGroups, - "querying groups of authority"); - long writeLines = this.writeText(HugeType.GROUP, groups); - Printer.print("Groups backup finished, write lines: %d", writeLines); - } - - private void backupTargets() { - Printer.print("Targets backup started..."); - List targets = retry(this.client.authManager()::listTargets, - "querying targets of authority"); - long writeLines = this.writeText(HugeType.TARGET, targets); - Printer.print("Targets backup finished, write lines: %d", writeLines); - } - - private void backupBelongs() { - Printer.print("Belongs backup started..."); - List belongs = retry(this.client.authManager()::listBelongs, - "querying belongs of authority"); - long writeLines = this.writeText(HugeType.BELONG, belongs); - Printer.print("Belongs backup finished, write lines: %d", writeLines); - } - - private void backupAccesses() { - Printer.print("Accesses backup started..."); - List accesses = retry(this.client.authManager()::listAccesses, - "querying accesses of authority"); - long writeLines = this.writeText(HugeType.ACCESS, accesses); - Printer.print("Accesses backup finished, write lines: %d", writeLines); - } - - private long writeText(HugeType type, List list) { - long count = 0L; - try { - OutputStream os = this.outputStream(type.string(), false); - ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE); - StringBuilder builder = new StringBuilder(LBUF_SIZE); - - for (Object e : list) { - count++; - builder.append(JsonUtil.toJson(e)).append("\n"); - } - baos.write(builder.toString().getBytes(API.CHARSET)); - os.write(baos.toByteArray()); - } catch (Throwable e) { - throw new ToolsException("Failed to serialize %s to %s", - e, list, type.string()); - } - return count; - } - - protected void directory(String dir, Map hdfsConf) { - if (hdfsConf == null || hdfsConf.isEmpty()) { - // Local FS directory - super.directory = LocalDirectory.constructDir(dir, AUTH_BACKUP_NAME); - } else { - // HDFS directory - super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_NAME, - hdfsConf); - } - } -} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java similarity index 77% rename from src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java rename to src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index 63717ec..2212f39 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -20,9 +20,11 @@ package com.baidu.hugegraph.manager; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.util.Comparator; import java.util.List; import java.util.Map; @@ -51,7 +53,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -public class AuthRestoreManager extends BackupRestoreBaseManager { +public class AuthBackupRestoreManager extends BackupRestoreBaseManager { private static final String AUTH_BACKUP_NAME = "auth-backup"; private static final String AUTH_RESTORE_DIR = "auth-restore"; @@ -69,12 +71,18 @@ public class AuthRestoreManager extends BackupRestoreBaseManager { private Map targetsByName; private Map belongsByName; private Map accessesByName; - private List authRestores; + private List authManagers; - public AuthRestoreManager(ToolClient.ConnectionInfo info) { + public AuthBackupRestoreManager(ToolClient.ConnectionInfo info) { super(info, AUTH_RESTORE_DIR); } + public void init(SubCommands.AuthBackup authBackup) { + this.retry(authBackup.retry()); + this.directory(authBackup.directory(), authBackup.hdfsConf()); + this.ensureDirectoryExist(true); + } + public void init(SubCommands.AuthRestore authRestore) { this.retry(authRestore.retry()); this.directory(authRestore.directory(), authRestore.hdfsConf()); @@ -87,14 +95,49 @@ public void init(SubCommands.AuthRestore authRestore) { this.targetsByName = Maps.newHashMap(); this.belongsByName = Maps.newHashMap(); this.accessesByName = Maps.newHashMap(); - this.authRestores = Lists.newArrayList(); + this.authManagers = Lists.newArrayList(); } - public void authRestore(List types) { + public void backup(List types) { + try { + this.doBackup(types); + } catch (Throwable e) { + throw e; + } finally { + this.shutdown(this.type()); + } + } + + private void doBackup(List types) { + for (HugeType type : types) { + switch (type) { + case USER: + new UserManager().backup(); + break; + case GROUP: + new GroupManager().backup(); + break; + case TARGET: + new TargetManager().backup(); + break; + case BELONG: + new BelongManager().backup(); + break; + case ACCESS: + new AccessManager().backup(); + break; + default: + throw new AssertionError(String.format( + "Bad auth backup type: %s", type)); + } + } + } + + public void restore(List types) { List sortedHugeTypes = this.sortListByCode(types); try { this.doAddAuths(sortedHugeTypes); - this.doAuthRestore(); + this.doRestore(); } catch (Throwable e) { throw e; } finally { @@ -106,19 +149,19 @@ private void doAddAuths(List types) { for (HugeType type : types) { switch (type) { case USER: - this.authRestores.add(new AuthUser()); + this.authManagers.add(new UserManager()); break; case GROUP: - this.authRestores.add(new AuthGroup()); + this.authManagers.add(new GroupManager()); break; case TARGET: - this.authRestores.add(new AuthTarget()); + this.authManagers.add(new TargetManager()); break; case BELONG: - this.authRestores.add(new AuthBelong()); + this.authManagers.add(new BelongManager()); break; case ACCESS: - this.authRestores.add(new AuthAccess()); + this.authManagers.add(new AccessManager()); break; default: throw new AssertionError(String.format( @@ -127,13 +170,13 @@ private void doAddAuths(List types) { } } - private void doAuthRestore() { - if (CollectionUtils.isNotEmpty(this.authRestores)) { - for (AuthRestore authRestore : this.authRestores) { - authRestore.checkConflict(); + private void doRestore() { + if (CollectionUtils.isNotEmpty(this.authManagers)) { + for (AuthManager authManager : this.authManagers) { + authManager.checkConflict(); } - for (AuthRestore authRestore : this.authRestores) { - authRestore.restore(); + for (AuthManager authManager : this.authManagers) { + authManager.restore(); } } } @@ -162,6 +205,26 @@ private List read(HugeType type) { return resultList; } + private long writeText(HugeType type, List list) { + long count = 0L; + try { + OutputStream os = this.outputStream(type.string(), false); + ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE); + StringBuilder builder = new StringBuilder(LBUF_SIZE); + + for (Object e : list) { + count++; + builder.append(JsonUtil.toJson(e)).append("\n"); + } + baos.write(builder.toString().getBytes(API.CHARSET)); + os.write(baos.toByteArray()); + } catch (Throwable e) { + throw new ToolsException("Failed to serialize %s to %s", + e, list, type.string()); + } + return count; + } + protected void directory(String dir, Map hdfsConf) { if (hdfsConf == null || hdfsConf.isEmpty()) { // Local FS directory @@ -188,14 +251,26 @@ private void initPassword(List types, String password) { } } - private abstract class AuthRestore { + private interface AuthManager { - public abstract void checkConflict(); + void backup(); - public abstract void restore(); + void checkConflict(); + + void restore(); } - private class AuthUser extends AuthRestore{ + private class UserManager implements AuthManager { + + @Override + public void backup() { + Printer.print("Users backup started..."); + List users = retry(client.authManager()::listUsers, + "querying users of authority"); + long writeLines = writeText(HugeType.USER, users); + Printer.print("Users backup finished, write lines: %d", + writeLines); + } @Override public void checkConflict() { @@ -261,7 +336,17 @@ private void prepareUserForRestore(User restoreUser) { } } - private class AuthGroup extends AuthRestore { + private class GroupManager implements AuthManager { + + @Override + public void backup() { + Printer.print("Groups backup started..."); + List groups = retry(client.authManager()::listGroups, + "querying groups of authority"); + long writeLines = writeText(HugeType.GROUP, groups); + Printer.print("Groups backup finished, write lines: %d", + writeLines); + } @Override public void checkConflict() { @@ -318,7 +403,17 @@ private void prepareGroupForRestore(Group restoreGroup) { } } - private class AuthTarget extends AuthRestore { + private class TargetManager implements AuthManager { + + @Override + public void backup() { + Printer.print("Targets backup started..."); + List targets = retry(client.authManager()::listTargets, + "querying targets of authority"); + long writeLines = writeText(HugeType.TARGET, targets); + Printer.print("Targets backup finished, write lines: %d", + writeLines); + } @Override public void checkConflict() { @@ -379,7 +474,17 @@ private void prepareTargetForRestore(Target restoreTarget) { } } - private class AuthBelong extends AuthRestore { + private class BelongManager implements AuthManager { + + @Override + public void backup() { + Printer.print("Belongs backup started..."); + List belongs = retry(client.authManager()::listBelongs, + "querying belongs of authority"); + long writeLines = writeText(HugeType.BELONG, belongs); + Printer.print("Belongs backup finished, write lines: %d", + writeLines); + } @Override public void checkConflict() { @@ -428,7 +533,17 @@ public void restore() { } } - private class AuthAccess extends AuthRestore { + private class AccessManager implements AuthManager { + + @Override + public void backup() { + Printer.print("Accesses backup started..."); + List accesses = retry(client.authManager()::listAccesses, + "querying accesses of authority"); + long writeLines = writeText(HugeType.ACCESS, accesses); + Printer.print("Accesses backup finished, write lines: %d", + writeLines); + } @Override public void checkConflict() { @@ -473,7 +588,8 @@ public void restore() { }, "Restore access of authority"); count++; } - Printer.print("Restore accesses finished, count is %d !", count); + Printer.print("Restore accesses finished, count is %d !", + count); } } } From 7292549c31a6970832f46380028f698dbe70d9bc Mon Sep 17 00:00:00 2001 From: xuliguo Date: Thu, 17 Dec 2020 19:52:10 +0800 Subject: [PATCH 17/26] Update and optimize some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 7 +- .../manager/AuthBackupRestoreManager.java | 73 +++++++------------ .../com/baidu/hugegraph/util/ToolUtil.java | 5 +- .../test/functional/AuthRestoreTest.java | 4 +- .../hugegraph/test/functional/AuthTest.java | 2 +- .../test/functional/CommandTest.java | 20 ++++- 6 files changed, 51 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 96b232d..d70ca44 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -830,9 +830,10 @@ public static class TaskId { public static class AuthBackupRestore { @Parameter(names = {"--directory"}, arity = 1, - description = "Directory of auth information, default is " + - "'./{auth-backup}' in local file system " + - "or '{fs.default.name}/{auth-backup}' in HDFS") + description = "Directory of auth information, default " + + "is './{auth-backup-restore}' in local " + + "file system or '{fs.default.name}/" + + "{auth-backup-restore}' in HDFS") public String directory; @DynamicParameter(names = "-D", diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index 2212f39..ac38fa2 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -30,7 +30,6 @@ import java.util.Map; import java.util.stream.Collectors; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; @@ -55,9 +54,8 @@ public class AuthBackupRestoreManager extends BackupRestoreBaseManager { - private static final String AUTH_BACKUP_NAME = "auth-backup"; - private static final String AUTH_RESTORE_DIR = "auth-restore"; - private static final int conflict_status = 0; + private static final String AUTH_BACKUP_RESTORE = "auth-backup-restore"; + private static final int NO_CONFLICT = 0; private AuthRestoreStrategy strategy; private String initPassword; @@ -74,13 +72,14 @@ public class AuthBackupRestoreManager extends BackupRestoreBaseManager { private List authManagers; public AuthBackupRestoreManager(ToolClient.ConnectionInfo info) { - super(info, AUTH_RESTORE_DIR); + super(info, AUTH_BACKUP_RESTORE); } public void init(SubCommands.AuthBackup authBackup) { this.retry(authBackup.retry()); this.directory(authBackup.directory(), authBackup.hdfsConf()); this.ensureDirectoryExist(true); + this.authManagers = Lists.newArrayList(); } public void init(SubCommands.AuthRestore authRestore) { @@ -100,7 +99,8 @@ public void init(SubCommands.AuthRestore authRestore) { public void backup(List types) { try { - this.doBackup(types); + this.doAddAuths(types); + this.doBackup(); } catch (Throwable e) { throw e; } finally { @@ -108,28 +108,9 @@ public void backup(List types) { } } - private void doBackup(List types) { - for (HugeType type : types) { - switch (type) { - case USER: - new UserManager().backup(); - break; - case GROUP: - new GroupManager().backup(); - break; - case TARGET: - new TargetManager().backup(); - break; - case BELONG: - new BelongManager().backup(); - break; - case ACCESS: - new AccessManager().backup(); - break; - default: - throw new AssertionError(String.format( - "Bad auth backup type: %s", type)); - } + private void doBackup() { + for (AuthManager authManager : this.authManagers) { + authManager.backup(); } } @@ -145,6 +126,15 @@ public void restore(List types) { } } + private void doRestore() { + for (AuthManager authManager : this.authManagers) { + authManager.checkConflict(); + } + for (AuthManager authManager : this.authManagers) { + authManager.restore(); + } + } + private void doAddAuths(List types) { for (HugeType type : types) { switch (type) { @@ -170,17 +160,6 @@ private void doAddAuths(List types) { } } - private void doRestore() { - if (CollectionUtils.isNotEmpty(this.authManagers)) { - for (AuthManager authManager : this.authManagers) { - authManager.checkConflict(); - } - for (AuthManager authManager : this.authManagers) { - authManager.restore(); - } - } - } - private boolean checkAllExistInIdMaps(String oneId, String otherId) { if (this.idsMap.containsKey(oneId) && this.idsMap.containsKey(otherId)) { @@ -228,10 +207,10 @@ private long writeText(HugeType type, List list) { protected void directory(String dir, Map hdfsConf) { if (hdfsConf == null || hdfsConf.isEmpty()) { // Local FS directory - super.directory = LocalDirectory.constructDir(dir, AUTH_BACKUP_NAME); + super.directory = LocalDirectory.constructDir(dir, AUTH_BACKUP_RESTORE); } else { // HDFS directory - super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_NAME, + super.directory = HdfsDirectory.constructDir(dir, AUTH_BACKUP_RESTORE, hdfsConf); } } @@ -282,7 +261,7 @@ public void checkConflict() { } List userJsons = read(HugeType.USER); for (String user : userJsons) { - int conflict = conflict_status; + int conflict = NO_CONFLICT; User restoreUser = JsonUtil.fromJson(user, User.class); if (!userMap.containsKey(restoreUser.name())) { this.prepareUserForRestore(restoreUser); @@ -301,7 +280,7 @@ public void checkConflict() { restoreUser.avatar())) { conflict++; } - if (conflict > conflict_status) { + if (conflict > NO_CONFLICT) { E.checkArgument(strategy != AuthRestoreStrategy.STOP, "Restore users conflict with STOP strategy, " + "user name is s%", restoreUser.name()); @@ -358,7 +337,7 @@ public void checkConflict() { } List groupJsons = read(HugeType.GROUP); for (String group : groupJsons) { - int conflict = conflict_status; + int conflict = NO_CONFLICT; Group restoreGroup = JsonUtil.fromJson(group, Group.class); if (!groupMap.containsKey(restoreGroup.name())) { this.prepareGroupForRestore(restoreGroup); @@ -369,7 +348,7 @@ public void checkConflict() { restoreGroup.description())) { conflict++; } - if (conflict > conflict_status) { + if (conflict > NO_CONFLICT) { E.checkArgument(strategy != AuthRestoreStrategy.STOP, "Restore groups conflict with STOP strategy, " + "group name is s%", restoreGroup.name()); @@ -425,7 +404,7 @@ public void checkConflict() { } List targetJsons = read(HugeType.TARGET); for (String target : targetJsons) { - int conflict = conflict_status; + int conflict = NO_CONFLICT; Target restoreTarget = JsonUtil.fromJson(target, Target.class); if (!targetMap.containsKey(restoreTarget.name())) { this.prepareTargetForRestore(restoreTarget); @@ -440,7 +419,7 @@ public void checkConflict() { restoreTarget.url())) { conflict++; } - if (conflict > conflict_status) { + if (conflict > NO_CONFLICT) { E.checkArgument(strategy != AuthRestoreStrategy.STOP, "Restore targets conflict with STOP strategy, " + "target name is s%", restoreTarget.name()); diff --git a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java index fad906a..11ecbf9 100644 --- a/src/main/java/com/baidu/hugegraph/util/ToolUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ToolUtil.java @@ -29,10 +29,9 @@ public final class ToolUtil { - public static void printOrThrow(Throwable e, - boolean testMode) { + public static void printOrThrow(Throwable e, boolean throwMode) { Printer.print("Failed to execute %s", e.getMessage()); - if (testMode) { + if (throwMode) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 1155ef8..2aa6122 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -263,7 +263,7 @@ public void testAuthRestoreWithTypesException() { } @Test - public void testAuthRestoreByTypesWithBelongException() { + public void testAuthRestoreByBelongWithoutDependency() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -287,7 +287,7 @@ public void testAuthRestoreByTypesWithBelongException() { } @Test - public void testAuthRestoreByTypesWithAccessException() { + public void testAuthRestoreByAccessWithoutDependency() { String filePath = "./auth-test-test"; String[] args = new String[]{ diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java index b9578a5..5d3ad3a 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthTest.java @@ -21,7 +21,7 @@ public class AuthTest { - public static final String DEFAULT_URL = "./auth-backup/"; + public static final String DEFAULT_URL = "./auth-backup-restore/"; public static final String DEFAULT_TEST_URL = "/auth/"; public static final String USER_NAME = "admin"; public static final String USER_PASSWORD = "123456"; diff --git a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java index 6a53797..c59ee9d 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java @@ -39,8 +39,11 @@ public void testHelpCommand() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { + ExitException exception = (ExitException)e; Assert.assertContains("Command : hugegragh help", - e.getMessage()); + exception.getMessage()); + Assert.assertContains("Usage: hugegraph [options] [command]", + exception.details()); }); } @@ -56,8 +59,11 @@ public void testHelpSubCommand() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { + ExitException exception = (ExitException)e; Assert.assertContains("Command : hugegragh help auth-backup", - e.getMessage()); + exception.getMessage()); + Assert.assertContains("Usage: auth-backup [options]", + exception.details()); }); } @@ -74,9 +80,12 @@ public void testBadHelpSubCommandException() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { + ExitException exception = (ExitException)e; Assert.assertContains(String.format( "Unexpected help sub-command %s", - badCommand), e.getMessage()); + badCommand), exception.getMessage()); + Assert.assertContains("Here are some sub-command ", + exception.details()); }); } @@ -91,8 +100,11 @@ public void testEmptyCommandException() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { + ExitException exception = (ExitException)e; Assert.assertContains("No sub-command found", - e.getMessage()); + exception.getMessage()); + Assert.assertContains("Warning : must provide one sub-command", + exception.details()); }); } } From b84753b7f678d239629e1ea724a8cacb465860e8 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 18 Dec 2020 20:07:11 +0800 Subject: [PATCH 18/26] Update and optimize some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 6 +- ....java => AuthRestoreConflictStrategy.java} | 10 +- .../hugegraph/constant/AuthRestoreFlow.java | 43 ----- .../manager/AuthBackupRestoreManager.java | 160 ++++++++++-------- .../test/functional/AuthBackupTest.java | 8 +- .../test/functional/AuthRestoreTest.java | 19 ++- .../test/functional/CommandTest.java | 8 +- .../baidu/hugegraph/test/util/FileUtil.java | 6 +- 8 files changed, 114 insertions(+), 146 deletions(-) rename src/main/java/com/baidu/hugegraph/constant/{AuthRestoreStrategy.java => AuthRestoreConflictStrategy.java} (79%) delete mode 100644 src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index d70ca44..d84b4a0 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -32,7 +32,7 @@ import org.apache.commons.lang3.StringUtils; import com.baidu.hugegraph.api.API; -import com.baidu.hugegraph.constant.AuthRestoreStrategy; +import com.baidu.hugegraph.constant.AuthRestoreConflictStrategy; import com.baidu.hugegraph.manager.TasksManager; import com.baidu.hugegraph.structure.constant.GraphMode; import com.baidu.hugegraph.structure.constant.HugeType; @@ -1043,8 +1043,8 @@ public static class AuthStrategyConverter public String convert(String value) { E.checkArgument(value != null && !value.isEmpty(), "Strategy can't be null or empty"); - E.checkArgument(AuthRestoreStrategy.STOP.string().equals(value) || - AuthRestoreStrategy.IGNORE.string().equals(value), + E.checkArgument(AuthRestoreConflictStrategy.STOP.string().equals(value) || + AuthRestoreConflictStrategy.IGNORE.string().equals(value), "Invalid --strategy '%s', valid value is" + " 'stop' or 'ignore", value); return value; diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java similarity index 79% rename from src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java rename to src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java index 1ecd863..2464ad0 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java @@ -19,7 +19,7 @@ package com.baidu.hugegraph.constant; -public enum AuthRestoreStrategy { +public enum AuthRestoreConflictStrategy { STOP(1, "stop"), IGNORE(2, "ignore"); @@ -27,7 +27,7 @@ public enum AuthRestoreStrategy { private int code; private String name = null; - AuthRestoreStrategy(int code, String name) { + AuthRestoreConflictStrategy(int code, String name) { assert code < 256; this.code = code; this.name = name; @@ -37,9 +37,9 @@ public int code() { return this.code; } - public static AuthRestoreStrategy fromName(String name) { - AuthRestoreStrategy[] restoreStrategys = AuthRestoreStrategy.values(); - for (AuthRestoreStrategy strategy : restoreStrategys) { + public static AuthRestoreConflictStrategy fromName(String name) { + AuthRestoreConflictStrategy[] restoreStrategys = AuthRestoreConflictStrategy.values(); + for (AuthRestoreConflictStrategy strategy : restoreStrategys) { if (strategy.string().equals(name)) { return strategy; } diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java deleted file mode 100644 index e702939..0000000 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreFlow.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package com.baidu.hugegraph.constant; - -public enum AuthRestoreFlow { - - CHECK(1, "check"), - RESTORE(2, "restore"); - - private int code; - private String name = null; - - AuthRestoreFlow(int code, String name) { - assert code < 256; - this.code = code; - this.name = name; - } - - public int code() { - return this.code; - } - - public String string() { - return this.name; - } -} diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index ac38fa2..fc58e9e 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; @@ -39,7 +40,7 @@ import com.baidu.hugegraph.base.Printer; import com.baidu.hugegraph.base.ToolClient; import com.baidu.hugegraph.cmd.SubCommands; -import com.baidu.hugegraph.constant.AuthRestoreStrategy; +import com.baidu.hugegraph.constant.AuthRestoreConflictStrategy; import com.baidu.hugegraph.exception.ToolsException; import com.baidu.hugegraph.structure.auth.Access; import com.baidu.hugegraph.structure.auth.Belong; @@ -57,7 +58,7 @@ public class AuthBackupRestoreManager extends BackupRestoreBaseManager { private static final String AUTH_BACKUP_RESTORE = "auth-backup-restore"; private static final int NO_CONFLICT = 0; - private AuthRestoreStrategy strategy; + private AuthRestoreConflictStrategy strategy; private String initPassword; /* * The collection of id relationships of users, groups and targets @@ -69,7 +70,7 @@ public class AuthBackupRestoreManager extends BackupRestoreBaseManager { private Map targetsByName; private Map belongsByName; private Map accessesByName; - private List authManagers; + private List conflicts; public AuthBackupRestoreManager(ToolClient.ConnectionInfo info) { super(info, AUTH_BACKUP_RESTORE); @@ -79,14 +80,13 @@ public void init(SubCommands.AuthBackup authBackup) { this.retry(authBackup.retry()); this.directory(authBackup.directory(), authBackup.hdfsConf()); this.ensureDirectoryExist(true); - this.authManagers = Lists.newArrayList(); } public void init(SubCommands.AuthRestore authRestore) { this.retry(authRestore.retry()); this.directory(authRestore.directory(), authRestore.hdfsConf()); this.ensureDirectoryExist(false); - this.strategy = AuthRestoreStrategy.fromName(authRestore.strategy()); + this.strategy = AuthRestoreConflictStrategy.fromName(authRestore.strategy()); this.initPassword(authRestore.types(), authRestore.initPassword()); this.idsMap = Maps.newHashMap(); this.usersByName = Maps.newHashMap(); @@ -94,13 +94,12 @@ public void init(SubCommands.AuthRestore authRestore) { this.targetsByName = Maps.newHashMap(); this.belongsByName = Maps.newHashMap(); this.accessesByName = Maps.newHashMap(); - this.authManagers = Lists.newArrayList(); + this.conflicts = Lists.newArrayList(); } public void backup(List types) { try { - this.doAddAuths(types); - this.doBackup(); + this.doBackup(this.addAuthManagers(types)); } catch (Throwable e) { throw e; } finally { @@ -108,8 +107,8 @@ public void backup(List types) { } } - private void doBackup() { - for (AuthManager authManager : this.authManagers) { + private void doBackup(List authManagers) { + for (AuthManager authManager : authManagers) { authManager.backup(); } } @@ -117,8 +116,7 @@ private void doBackup() { public void restore(List types) { List sortedHugeTypes = this.sortListByCode(types); try { - this.doAddAuths(sortedHugeTypes); - this.doRestore(); + this.doRestore(this.addAuthManagers(sortedHugeTypes)); } catch (Throwable e) { throw e; } finally { @@ -126,38 +124,43 @@ public void restore(List types) { } } - private void doRestore() { - for (AuthManager authManager : this.authManagers) { + private void doRestore(List authManagers) { + for (AuthManager authManager : authManagers) { authManager.checkConflict(); } - for (AuthManager authManager : this.authManagers) { + E.checkArgument(CollectionUtils.isEmpty(this.conflicts), + "Restore conflict with STOP strategy, conflicting " + + "data is s%", JsonUtil.toJson(this.conflicts)); + for (AuthManager authManager : authManagers) { authManager.restore(); } } - private void doAddAuths(List types) { + private List addAuthManagers(List types) { + List authManagers = Lists.newArrayList(); for (HugeType type : types) { switch (type) { case USER: - this.authManagers.add(new UserManager()); + authManagers.add(new UserManager()); break; case GROUP: - this.authManagers.add(new GroupManager()); + authManagers.add(new GroupManager()); break; case TARGET: - this.authManagers.add(new TargetManager()); + authManagers.add(new TargetManager()); break; case BELONG: - this.authManagers.add(new BelongManager()); + authManagers.add(new BelongManager()); break; case ACCESS: - this.authManagers.add(new AccessManager()); + authManagers.add(new AccessManager()); break; default: throw new AssertionError(String.format( "Bad auth restore type: %s", type)); } } + return authManagers; } private boolean checkAllExistInIdMaps(String oneId, String otherId) { @@ -168,7 +171,7 @@ private boolean checkAllExistInIdMaps(String oneId, String otherId) { return false; } - private List read(HugeType type) { + private List readRestoreData(HugeType type) { List resultList = Lists.newArrayList(); InputStream is = this.inputStream(type.string()); try (InputStreamReader isr = new InputStreamReader(is, API.CHARSET); @@ -184,7 +187,7 @@ private List read(HugeType type) { return resultList; } - private long writeText(HugeType type, List list) { + private long writeBackupData(HugeType type, List list) { long count = 0L; try { OutputStream os = this.outputStream(type.string(), false); @@ -216,18 +219,17 @@ protected void directory(String dir, Map hdfsConf) { } private List sortListByCode(List hugeTypes) { - return hugeTypes.stream(). - sorted(Comparator.comparing(HugeType::code)). - collect(Collectors.toList()); + return hugeTypes.stream() + .sorted(Comparator.comparing(HugeType::code)) + .collect(Collectors.toList()); } private void initPassword(List types, String password) { - if (types.contains(HugeType.USER) && Strings.isEmpty(password)) { - throw new IllegalArgumentException(String.format( - "The following option is required: [--init-password]")); - } else { - this.initPassword = password; - } + E.checkArgument(!types.contains(HugeType.USER) || + Strings.isNotEmpty(password), + "The following option is " + + "required: [--init-password]"); + this.initPassword = password; } private interface AuthManager { @@ -246,7 +248,7 @@ public void backup() { Printer.print("Users backup started..."); List users = retry(client.authManager()::listUsers, "querying users of authority"); - long writeLines = writeText(HugeType.USER, users); + long writeLines = writeBackupData(HugeType.USER, users); Printer.print("Users backup finished, write lines: %d", writeLines); } @@ -259,7 +261,7 @@ public void checkConflict() { for (User user : users) { userMap.put(user.name(), user); } - List userJsons = read(HugeType.USER); + List userJsons = readRestoreData(HugeType.USER); for (String user : userJsons) { int conflict = NO_CONFLICT; User restoreUser = JsonUtil.fromJson(user, User.class); @@ -281,12 +283,13 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy != AuthRestoreStrategy.STOP, - "Restore users conflict with STOP strategy, " + - "user name is s%", restoreUser.name()); - E.checkArgument(strategy == AuthRestoreStrategy.STOP || - strategy == AuthRestoreStrategy.IGNORE, + E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || + strategy == AuthRestoreConflictStrategy.IGNORE, "Restore users strategy is not found"); + if (strategy == AuthRestoreConflictStrategy.STOP) { + conflicts.add(String.format("user name : %s", + restoreUser.name())); + } } else { idsMap.put(restoreUser.id().toString(), existUser.id().toString()); @@ -301,8 +304,8 @@ public void restore() { User restoreUser = entry.getValue(); restoreUser.password(initPassword); User user = retry(() -> { - return client.authManager().createUser(restoreUser); - }, "Restore users of authority"); + return client.authManager().createUser(restoreUser); + }, "Restore users of authority"); idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } @@ -322,7 +325,7 @@ public void backup() { Printer.print("Groups backup started..."); List groups = retry(client.authManager()::listGroups, "querying groups of authority"); - long writeLines = writeText(HugeType.GROUP, groups); + long writeLines = writeBackupData(HugeType.GROUP, groups); Printer.print("Groups backup finished, write lines: %d", writeLines); } @@ -335,7 +338,7 @@ public void checkConflict() { for (Group group : groups) { groupMap.put(group.name(), group); } - List groupJsons = read(HugeType.GROUP); + List groupJsons = readRestoreData(HugeType.GROUP); for (String group : groupJsons) { int conflict = NO_CONFLICT; Group restoreGroup = JsonUtil.fromJson(group, Group.class); @@ -349,12 +352,13 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy != AuthRestoreStrategy.STOP, - "Restore groups conflict with STOP strategy, " + - "group name is s%", restoreGroup.name()); - E.checkArgument(strategy == AuthRestoreStrategy.STOP || - strategy == AuthRestoreStrategy.IGNORE, + E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || + strategy == AuthRestoreConflictStrategy.IGNORE, "Restore groups strategy is not found"); + if (strategy == AuthRestoreConflictStrategy.STOP) { + conflicts.add(String.format("group name : %s", + restoreGroup.name())); + } } else { idsMap.put(restoreGroup.id().toString(), existGroup.id().toString()); @@ -368,8 +372,8 @@ public void restore() { for (Map.Entry entry : groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); + return client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } @@ -389,7 +393,7 @@ public void backup() { Printer.print("Targets backup started..."); List targets = retry(client.authManager()::listTargets, "querying targets of authority"); - long writeLines = writeText(HugeType.TARGET, targets); + long writeLines = writeBackupData(HugeType.TARGET, targets); Printer.print("Targets backup finished, write lines: %d", writeLines); } @@ -402,7 +406,7 @@ public void checkConflict() { for (Target target : targets) { targetMap.put(target.name(), target); } - List targetJsons = read(HugeType.TARGET); + List targetJsons = readRestoreData(HugeType.TARGET); for (String target : targetJsons) { int conflict = NO_CONFLICT; Target restoreTarget = JsonUtil.fromJson(target, Target.class); @@ -420,12 +424,16 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy != AuthRestoreStrategy.STOP, + E.checkArgument(strategy != AuthRestoreConflictStrategy.STOP, "Restore targets conflict with STOP strategy, " + "target name is s%", restoreTarget.name()); - E.checkArgument(strategy == AuthRestoreStrategy.STOP || - strategy == AuthRestoreStrategy.IGNORE, + E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || + strategy == AuthRestoreConflictStrategy.IGNORE, "Restore targets strategy is not found"); + if (strategy == AuthRestoreConflictStrategy.STOP) { + conflicts.add(String.format("target name : %s", + restoreTarget.name())); + } } else { idsMap.put(restoreTarget.id().toString(), existTarget.id().toString()); @@ -439,8 +447,8 @@ public void restore() { for (Map.Entry entry : targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); + return client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); idsMap.put(restoreTarget.id().toString(), target.id().toString()); count++; } @@ -460,7 +468,7 @@ public void backup() { Printer.print("Belongs backup started..."); List belongs = retry(client.authManager()::listBelongs, "querying belongs of authority"); - long writeLines = writeText(HugeType.BELONG, belongs); + long writeLines = writeBackupData(HugeType.BELONG, belongs); Printer.print("Belongs backup finished, write lines: %d", writeLines); } @@ -474,7 +482,7 @@ public void checkConflict() { belongMap.put(belong.user() + ":" + belong.group(), belong); } - List belongJsons = read(HugeType.BELONG); + List belongJsons = readRestoreData(HugeType.BELONG); for (String belong : belongJsons) { Belong restoreBelong = JsonUtil.fromJson(belong, Belong.class); if (!checkAllExistInIdMaps(restoreBelong.user().toString(), @@ -484,12 +492,13 @@ public void checkConflict() { String ids = idsMap.get(restoreBelong.user()) + ":" + idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { - E.checkArgument(strategy != AuthRestoreStrategy.STOP, - "Restore belongs conflict with STOP strategy, " + - "belong id is s%", restoreBelong.id()); - E.checkArgument(strategy == AuthRestoreStrategy.STOP || - strategy == AuthRestoreStrategy.IGNORE, + E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || + strategy == AuthRestoreConflictStrategy.IGNORE, "Restore belongs strategy is not found"); + if (strategy == AuthRestoreConflictStrategy.STOP) { + conflicts.add(String.format("belong id : %s", + restoreBelong.id())); + } continue; } belongsByName.put(restoreBelong.id().toString(), restoreBelong); @@ -504,8 +513,8 @@ public void restore() { restoreBelong.user(idsMap.get(restoreBelong.user().toString())); restoreBelong.group(idsMap.get(restoreBelong.group().toString())); retry(() -> { - return client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); + return client.authManager().createBelong(restoreBelong); + }, "Restore belongs of authority"); count++; } Printer.print("Restore belongs finished, count is %d !", count); @@ -519,7 +528,7 @@ public void backup() { Printer.print("Accesses backup started..."); List accesses = retry(client.authManager()::listAccesses, "querying accesses of authority"); - long writeLines = writeText(HugeType.ACCESS, accesses); + long writeLines = writeBackupData(HugeType.ACCESS, accesses); Printer.print("Accesses backup finished, write lines: %d", writeLines); } @@ -533,7 +542,7 @@ public void checkConflict() { accessMap.put(access.group() + ":" + access.target(), access); } - List accessJsons = read(HugeType.ACCESS); + List accessJsons = readRestoreData(HugeType.ACCESS); for (String access : accessJsons) { Access restoreAccess = JsonUtil.fromJson(access, Access.class); if (!checkAllExistInIdMaps(restoreAccess.group().toString(), @@ -543,12 +552,13 @@ public void checkConflict() { String ids = idsMap.get(restoreAccess.group()) + ":" + idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { - E.checkArgument(strategy != AuthRestoreStrategy.STOP, - "Restore accesses conflict with STOP strategy," + - "accesses id is s%", restoreAccess.id()); - E.checkArgument(strategy == AuthRestoreStrategy.STOP || - strategy == AuthRestoreStrategy.IGNORE, + E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || + strategy == AuthRestoreConflictStrategy.IGNORE, "Restore accesses strategy is not found"); + if (strategy == AuthRestoreConflictStrategy.STOP) { + conflicts.add(String.format("accesses id : %s", + restoreAccess.id())); + } continue; } accessesByName.put(restoreAccess.id().toString(), restoreAccess); @@ -563,8 +573,8 @@ public void restore() { restoreAccess.target(idsMap.get(restoreAccess.target().toString())); restoreAccess.group(idsMap.get(restoreAccess.group().toString())); retry(() -> { - return client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); + return client.authManager().createAccess(restoreAccess); + }, "Restore access of authority"); count++; } Printer.print("Restore accesses finished, count is %d !", diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 0c6ae41..0d3c8f8 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -48,7 +48,7 @@ public void testAuthBackup() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); - List fileNames = FileUtil.getFileDirectoryNames(DEFAULT_URL); + List fileNames = FileUtil.getFileSubdirectories(DEFAULT_URL); Assert.assertTrue(fileNames.size() == 5); } @@ -65,12 +65,12 @@ public void testAuthBackupByTypes() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); - List fileNames = FileUtil.getFileDirectoryNames(DEFAULT_URL); + List fileNames = FileUtil.getFileSubdirectories(DEFAULT_URL); Assert.assertTrue(fileNames.size() == 2); } @Test - public void testAuthBackupWithTypesException() { + public void testAuthBackupWithWrongType() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -102,7 +102,7 @@ public void testAuthBackupByDirectory() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(directory)); - List fileNames = FileUtil.getFileDirectoryNames(directory); + List fileNames = FileUtil.getFileSubdirectories(directory); Assert.assertTrue(fileNames.size() == 5); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 2aa6122..843de14 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -149,7 +149,7 @@ public void testAuthRestoreForUser() { } @Test - public void testRestoreWithInitPasswordException() { + public void testRestoreWithoutInitPassword() { String[] args = new String[]{ "--throw-mode", "true", "--user", USER_NAME, @@ -169,7 +169,7 @@ public void testRestoreWithInitPasswordException() { } @Test - public void testAuthRestoreWithStrategyException() { + public void testAuthRestoreWithStopStrategy() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ @@ -185,13 +185,13 @@ public void testAuthRestoreWithStrategyException() { Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, e -> { - Assert.assertContains("Restore users conflict with STOP strategy", + Assert.assertContains("Restore conflict with STOP strategy", e.getMessage()); }); } @Test - public void testAuthRestoreWithStrategyIgnore() { + public void testAuthRestoreWithIgnoreStrategy() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ @@ -216,7 +216,7 @@ public void testAuthRestoreWithStrategyIgnore() { } @Test - public void testAuthRestoreWithDirectoryException() { + public void testAuthRestoreWithWrongDirectory() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -239,7 +239,7 @@ public void testAuthRestoreWithDirectoryException() { } @Test - public void testAuthRestoreWithTypesException() { + public void testAuthRestoreWithWrongType() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -311,7 +311,7 @@ public void testAuthRestoreByAccessWithoutDependency() { } @Test - public void testAuthRestoreWithBadStrategyException() { + public void testAuthRestoreWithWrongStrategy() { String filePath = "./auth-test-test"; String[] args = new String[]{ @@ -338,7 +338,8 @@ private void loadData(HugeType hugeType, String dataFilePath) { String restoreDataPath = DEFAULT_URL + hugeType.string(); String testRestoreDataPath = DEFAULT_TEST_URL + dataFilePath; - List list = FileUtil.read(FileUtil.configPath(testRestoreDataPath)); - FileUtil.writeText(restoreDataPath, list); + List list = FileUtil.readTestRestoreData(FileUtil.configPath( + testRestoreDataPath)); + FileUtil.writeTestRestoreData(restoreDataPath, list); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java index c59ee9d..5676d16 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/CommandTest.java @@ -39,7 +39,7 @@ public void testHelpCommand() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - ExitException exception = (ExitException)e; + ExitException exception = (ExitException) e; Assert.assertContains("Command : hugegragh help", exception.getMessage()); Assert.assertContains("Usage: hugegraph [options] [command]", @@ -59,7 +59,7 @@ public void testHelpSubCommand() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - ExitException exception = (ExitException)e; + ExitException exception = (ExitException) e; Assert.assertContains("Command : hugegragh help auth-backup", exception.getMessage()); Assert.assertContains("Usage: auth-backup [options]", @@ -80,7 +80,7 @@ public void testBadHelpSubCommandException() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - ExitException exception = (ExitException)e; + ExitException exception = (ExitException) e; Assert.assertContains(String.format( "Unexpected help sub-command %s", badCommand), exception.getMessage()); @@ -100,7 +100,7 @@ public void testEmptyCommandException() { Assert.assertThrows(ExitException.class, () -> { HugeGraphCommand.main(args); }, e -> { - ExitException exception = (ExitException)e; + ExitException exception = (ExitException) e; Assert.assertContains("No sub-command found", exception.getMessage()); Assert.assertContains("Warning : must provide one sub-command", diff --git a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java index fab4147..044c7a8 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java @@ -53,7 +53,7 @@ public static boolean checkFileExists(String filePath) { return false; } - public static List getFileDirectoryNames(String filePath) { + public static List getFileSubdirectories(String filePath) { File file = new File(filePath); if (!file.exists()) { return ListUtils.EMPTY_LIST; @@ -79,7 +79,7 @@ public static void clearFile(String filePath) { } } - public static long writeText(String filePath, List list) { + public static long writeTestRestoreData(String filePath, List list) { long count = 0L; try (FileOutputStream os = new FileOutputStream(filePath); ByteArrayOutputStream baos = new ByteArrayOutputStream(LBUF_SIZE)) { @@ -98,7 +98,7 @@ public static long writeText(String filePath, List list) { return count; } - public static List read(String filePath) { + public static List readTestRestoreData(String filePath) { List resultList = Lists.newArrayList(); try (InputStream is = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(is, API.CHARSET)) { From 65f03fad835748da5cf5f243eb4b73be660bf46d Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 21 Dec 2020 09:30:36 +0800 Subject: [PATCH 19/26] Update and optimized some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 36 ++--- .../constant/AuthRestoreConflictStrategy.java | 23 ++++ .../manager/AuthBackupRestoreManager.java | 127 +++++++++--------- .../test/functional/AuthBackupTest.java | 8 +- .../test/functional/AuthRestoreTest.java | 8 +- .../baidu/hugegraph/test/util/FileUtil.java | 2 +- 6 files changed, 117 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index d84b4a0..a096404 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -847,24 +847,24 @@ public int retry() { return this.retry.retry; } - public String directory() { - return this.directory; + public void retry(int retry) { + this.retry.retry = retry; } - public Map hdfsConf() { - return this.hdfsConf; + public String directory() { + return this.directory; } public void directory(String directory) { this.directory = directory; } - public void hdfsConf(Map hdfsConf) { - this.hdfsConf = hdfsConf; + public Map hdfsConf() { + return this.hdfsConf; } - public void retry(int retry) { - this.retry.retry = retry; + public void hdfsConf(Map hdfsConf) { + this.hdfsConf = hdfsConf; } } @@ -894,7 +894,7 @@ public static class AuthRestore extends AuthBackupRestore { "'stop' and 'ignore', default is 'stop'. 'stop' means " + "if there a conflict, stop restore. 'ignore' means if " + "there a conflict, ignore and continue to restore.") - public String strategy = AuthStrategyConverter.strategy; + public AuthRestoreConflictStrategy strategy = AuthStrategyConverter.strategy; @Parameter(names = {"--init-password"}, arity = 1, description = "Init user password, if restore type include " + @@ -909,11 +909,11 @@ public void types(List types) { this.types.types = types; } - public String strategy() { + public AuthRestoreConflictStrategy strategy() { return this.strategy; } - public void strategy(String strategy) { + public void strategy(AuthRestoreConflictStrategy strategy) { this.strategy = strategy; } @@ -1024,7 +1024,7 @@ public List convert(String value) { try { hugeTypes.add(HugeType.valueOf(type.toUpperCase())); } catch (IllegalArgumentException e) { - throw new ParameterException(String.format( + throw new IllegalArgumentException(String.format( "Invalid --type '%s', valid value is 'all' or " + "combination of [user,group,target," + "belong,access]", type)); @@ -1035,19 +1035,19 @@ public List convert(String value) { } public static class AuthStrategyConverter - implements IStringConverter { + implements IStringConverter { - public static final String strategy = "stop"; + public static final AuthRestoreConflictStrategy strategy = + AuthRestoreConflictStrategy.STOP; @Override - public String convert(String value) { + public AuthRestoreConflictStrategy convert(String value) { E.checkArgument(value != null && !value.isEmpty(), "Strategy can't be null or empty"); - E.checkArgument(AuthRestoreConflictStrategy.STOP.string().equals(value) || - AuthRestoreConflictStrategy.IGNORE.string().equals(value), + E.checkArgument(AuthRestoreConflictStrategy.matchStrategy(value), "Invalid --strategy '%s', valid value is" + " 'stop' or 'ignore", value); - return value; + return AuthRestoreConflictStrategy.fromName(value); } } diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java index 2464ad0..0a1036d 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java @@ -50,4 +50,27 @@ public static AuthRestoreConflictStrategy fromName(String name) { public String string() { return this.name; } + + public static boolean matchStrategy(String strategy) { + if (AuthRestoreConflictStrategy.STOP.string().equals(strategy) || + AuthRestoreConflictStrategy.IGNORE.string().equals(strategy)) { + return true; + } + return false; + } + + public static boolean matchStrategy(AuthRestoreConflictStrategy strategy) { + if (AuthRestoreConflictStrategy.STOP == strategy || + AuthRestoreConflictStrategy.IGNORE == strategy) { + return true; + } + return false; + } + + public static boolean matchStopStrategy(AuthRestoreConflictStrategy strategy) { + if (AuthRestoreConflictStrategy.STOP == strategy) { + return true; + } + return false; + } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index fc58e9e..223ec06 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -70,7 +70,6 @@ public class AuthBackupRestoreManager extends BackupRestoreBaseManager { private Map targetsByName; private Map belongsByName; private Map accessesByName; - private List conflicts; public AuthBackupRestoreManager(ToolClient.ConnectionInfo info) { super(info, AUTH_BACKUP_RESTORE); @@ -86,7 +85,7 @@ public void init(SubCommands.AuthRestore authRestore) { this.retry(authRestore.retry()); this.directory(authRestore.directory(), authRestore.hdfsConf()); this.ensureDirectoryExist(false); - this.strategy = AuthRestoreConflictStrategy.fromName(authRestore.strategy()); + this.strategy = authRestore.strategy(); this.initPassword(authRestore.types(), authRestore.initPassword()); this.idsMap = Maps.newHashMap(); this.usersByName = Maps.newHashMap(); @@ -94,7 +93,6 @@ public void init(SubCommands.AuthRestore authRestore) { this.targetsByName = Maps.newHashMap(); this.belongsByName = Maps.newHashMap(); this.accessesByName = Maps.newHashMap(); - this.conflicts = Lists.newArrayList(); } public void backup(List types) { @@ -108,8 +106,10 @@ public void backup(List types) { } private void doBackup(List authManagers) { + E.checkState(CollectionUtils.isNotEmpty(authManagers), + "Backup data is empty, please check the type"); for (AuthManager authManager : authManagers) { - authManager.backup(); + authManager.backup(); } } @@ -125,14 +125,17 @@ public void restore(List types) { } private void doRestore(List authManagers) { + E.checkState(CollectionUtils.isNotEmpty(authManagers), + "Restore data is empty, please check the type"); + List allConflicts = Lists.newArrayList(); for (AuthManager authManager : authManagers) { - authManager.checkConflict(); + allConflicts.addAll(authManager.checkConflict()); } - E.checkArgument(CollectionUtils.isEmpty(this.conflicts), - "Restore conflict with STOP strategy, conflicting " + - "data is s%", JsonUtil.toJson(this.conflicts)); + E.checkState(CollectionUtils.isEmpty(allConflicts), + "Restore conflict with STOP strategy, conflicting " + + "data is s%", JsonUtil.toJson(allConflicts)); for (AuthManager authManager : authManagers) { - authManager.restore(); + authManager.restore(); } } @@ -225,10 +228,10 @@ private List sortListByCode(List hugeTypes) { } private void initPassword(List types, String password) { - E.checkArgument(!types.contains(HugeType.USER) || - Strings.isNotEmpty(password), - "The following option is " + - "required: [--init-password]"); + E.checkState(!types.contains(HugeType.USER) || + Strings.isNotEmpty(password), + "The following option is " + + "required: [--init-password]"); this.initPassword = password; } @@ -236,7 +239,7 @@ private interface AuthManager { void backup(); - void checkConflict(); + List checkConflict(); void restore(); } @@ -254,7 +257,7 @@ public void backup() { } @Override - public void checkConflict() { + public List checkConflict() { List users = retry(client.authManager()::listUsers, "Querying users of authority"); Map userMap = Maps.newHashMap(); @@ -262,6 +265,7 @@ public void checkConflict() { userMap.put(user.name(), user); } List userJsons = readRestoreData(HugeType.USER); + List conflicts = Lists.newArrayList(); for (String user : userJsons) { int conflict = NO_CONFLICT; User restoreUser = JsonUtil.fromJson(user, User.class); @@ -283,18 +287,18 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || - strategy == AuthRestoreConflictStrategy.IGNORE, - "Restore users strategy is not found"); - if (strategy == AuthRestoreConflictStrategy.STOP) { - conflicts.add(String.format("user name : %s", - restoreUser.name())); + E.checkState(AuthRestoreConflictStrategy.matchStrategy( + strategy), + "Restore users strategy is not found"); + if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + conflicts.add(restoreUser.toString()); } } else { idsMap.put(restoreUser.id().toString(), existUser.id().toString()); } } + return conflicts; } @Override @@ -304,8 +308,8 @@ public void restore() { User restoreUser = entry.getValue(); restoreUser.password(initPassword); User user = retry(() -> { - return client.authManager().createUser(restoreUser); - }, "Restore users of authority"); + return client.authManager().createUser(restoreUser); + }, "Restore users of authority"); idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } @@ -331,7 +335,7 @@ public void backup() { } @Override - public void checkConflict() { + public List checkConflict() { List groups = retry(client.authManager()::listGroups, "Querying users of authority"); Map groupMap = Maps.newHashMap(); @@ -339,6 +343,7 @@ public void checkConflict() { groupMap.put(group.name(), group); } List groupJsons = readRestoreData(HugeType.GROUP); + List conflicts = Lists.newArrayList(); for (String group : groupJsons) { int conflict = NO_CONFLICT; Group restoreGroup = JsonUtil.fromJson(group, Group.class); @@ -352,18 +357,18 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || - strategy == AuthRestoreConflictStrategy.IGNORE, - "Restore groups strategy is not found"); - if (strategy == AuthRestoreConflictStrategy.STOP) { - conflicts.add(String.format("group name : %s", - restoreGroup.name())); + E.checkState(AuthRestoreConflictStrategy.matchStrategy( + strategy), + "Restore groups strategy is not found"); + if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + conflicts.add(restoreGroup.toString()); } } else { idsMap.put(restoreGroup.id().toString(), existGroup.id().toString()); } } + return conflicts; } @Override @@ -372,8 +377,8 @@ public void restore() { for (Map.Entry entry : groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); + return client.authManager().createGroup(restoreGroup); + }, "Restore groups of authority"); idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } @@ -399,7 +404,7 @@ public void backup() { } @Override - public void checkConflict() { + public List checkConflict() { List targets = retry(client.authManager()::listTargets, "Querying targets of authority"); Map targetMap = Maps.newHashMap(); @@ -407,6 +412,7 @@ public void checkConflict() { targetMap.put(target.name(), target); } List targetJsons = readRestoreData(HugeType.TARGET); + List conflicts = Lists.newArrayList(); for (String target : targetJsons) { int conflict = NO_CONFLICT; Target restoreTarget = JsonUtil.fromJson(target, Target.class); @@ -424,21 +430,18 @@ public void checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkArgument(strategy != AuthRestoreConflictStrategy.STOP, - "Restore targets conflict with STOP strategy, " + - "target name is s%", restoreTarget.name()); - E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || - strategy == AuthRestoreConflictStrategy.IGNORE, - "Restore targets strategy is not found"); - if (strategy == AuthRestoreConflictStrategy.STOP) { - conflicts.add(String.format("target name : %s", - restoreTarget.name())); + E.checkState(AuthRestoreConflictStrategy.matchStrategy( + strategy), + "Restore targets strategy is not found"); + if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + conflicts.add(restoreTarget.toString()); } } else { idsMap.put(restoreTarget.id().toString(), existTarget.id().toString()); } } + return conflicts; } @Override @@ -447,16 +450,18 @@ public void restore() { for (Map.Entry entry : targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); - idsMap.put(restoreTarget.id().toString(), target.id().toString()); + return client.authManager().createTarget(restoreTarget); + }, "Restore targets of authority"); + idsMap.put(restoreTarget.id().toString(), + target.id().toString()); count++; } Printer.print("Restore targets finished, count is %d !", count); } private void prepareTargetForRestore(Target restoreTarget) { - idsMap.put(restoreTarget.id().toString(), restoreTarget.id().toString()); + idsMap.put(restoreTarget.id().toString(), + restoreTarget.id().toString()); targetsByName.put(restoreTarget.name(), restoreTarget); } } @@ -474,7 +479,7 @@ public void backup() { } @Override - public void checkConflict() { + public List checkConflict() { List belongs = retry(client.authManager()::listBelongs, "Querying belongs of authority"); Map belongMap = Maps.newHashMap(); @@ -483,6 +488,7 @@ public void checkConflict() { belong); } List belongJsons = readRestoreData(HugeType.BELONG); + List conflicts = Lists.newArrayList(); for (String belong : belongJsons) { Belong restoreBelong = JsonUtil.fromJson(belong, Belong.class); if (!checkAllExistInIdMaps(restoreBelong.user().toString(), @@ -492,17 +498,17 @@ public void checkConflict() { String ids = idsMap.get(restoreBelong.user()) + ":" + idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { - E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || - strategy == AuthRestoreConflictStrategy.IGNORE, - "Restore belongs strategy is not found"); - if (strategy == AuthRestoreConflictStrategy.STOP) { - conflicts.add(String.format("belong id : %s", - restoreBelong.id())); + E.checkState(AuthRestoreConflictStrategy.matchStrategy( + strategy), + "Restore belongs strategy is not found"); + if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + conflicts.add(restoreBelong.toString()); } continue; } belongsByName.put(restoreBelong.id().toString(), restoreBelong); } + return conflicts; } @Override @@ -534,7 +540,7 @@ public void backup() { } @Override - public void checkConflict() { + public List checkConflict() { List accesses = retry(client.authManager()::listAccesses, "Querying accesses of authority"); Map accessMap = Maps.newHashMap(); @@ -543,6 +549,7 @@ public void checkConflict() { access); } List accessJsons = readRestoreData(HugeType.ACCESS); + List conflicts = Lists.newArrayList(); for (String access : accessJsons) { Access restoreAccess = JsonUtil.fromJson(access, Access.class); if (!checkAllExistInIdMaps(restoreAccess.group().toString(), @@ -552,17 +559,17 @@ public void checkConflict() { String ids = idsMap.get(restoreAccess.group()) + ":" + idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { - E.checkArgument(strategy == AuthRestoreConflictStrategy.STOP || - strategy == AuthRestoreConflictStrategy.IGNORE, - "Restore accesses strategy is not found"); - if (strategy == AuthRestoreConflictStrategy.STOP) { - conflicts.add(String.format("accesses id : %s", - restoreAccess.id())); + E.checkState(AuthRestoreConflictStrategy.matchStrategy( + strategy), + "Restore accesses strategy is not found"); + if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + conflicts.add(restoreAccess.toString()); } continue; } accessesByName.put(restoreAccess.id().toString(), restoreAccess); } + return conflicts; } @Override diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index 0d3c8f8..fc96382 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -48,7 +48,7 @@ public void testAuthBackup() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); - List fileNames = FileUtil.getFileSubdirectories(DEFAULT_URL); + List fileNames = FileUtil.subdirectories(DEFAULT_URL); Assert.assertTrue(fileNames.size() == 5); } @@ -65,7 +65,7 @@ public void testAuthBackupByTypes() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(DEFAULT_URL)); - List fileNames = FileUtil.getFileSubdirectories(DEFAULT_URL); + List fileNames = FileUtil.subdirectories(DEFAULT_URL); Assert.assertTrue(fileNames.size() == 2); } @@ -79,7 +79,7 @@ public void testAuthBackupWithWrongType() { "--types", "user,group,test" }; - Assert.assertThrows(ParameterException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, e -> { Assert.assertContains("valid value is 'all' or combination of " + @@ -102,7 +102,7 @@ public void testAuthBackupByDirectory() { HugeGraphCommand.main(args); Assert.assertTrue(FileUtil.checkFileExists(directory)); - List fileNames = FileUtil.getFileSubdirectories(directory); + List fileNames = FileUtil.subdirectories(directory); Assert.assertTrue(fileNames.size() == 5); } } diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java index 843de14..22e6a9e 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthRestoreTest.java @@ -159,7 +159,7 @@ public void testRestoreWithoutInitPassword() { "--directory", DEFAULT_URL }; - Assert.assertThrows(IllegalArgumentException.class, () -> { + Assert.assertThrows(IllegalStateException.class, () -> { HugeGraphCommand.main(args); }, e -> { String msg = e.getMessage(); @@ -169,7 +169,7 @@ public void testRestoreWithoutInitPassword() { } @Test - public void testAuthRestoreWithStopStrategy() { + public void testAuthRestoreWithConflictAndStopStrategy() { this.loadData(HugeType.USER, "auth_users_conflict.txt"); String[] args = new String[]{ @@ -182,7 +182,7 @@ public void testAuthRestoreWithStopStrategy() { "--init-password", "123456" }; - Assert.assertThrows(IllegalArgumentException.class, () -> { + Assert.assertThrows(IllegalStateException.class, () -> { HugeGraphCommand.main(args); }, e -> { Assert.assertContains("Restore conflict with STOP strategy", @@ -253,7 +253,7 @@ public void testAuthRestoreWithWrongType() { "--directory", filePath }; - Assert.assertThrows(ParameterException.class, () -> { + Assert.assertThrows(IllegalArgumentException.class, () -> { HugeGraphCommand.main(args); }, e -> { Assert.assertContains("valid value is 'all' or combination of " + diff --git a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java index 044c7a8..abcdfbc 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java @@ -53,7 +53,7 @@ public static boolean checkFileExists(String filePath) { return false; } - public static List getFileSubdirectories(String filePath) { + public static List subdirectories(String filePath) { File file = new File(filePath); if (!file.exists()) { return ListUtils.EMPTY_LIST; From d11a231818a18c7b85cbcac8124a2d3f67b2e980 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Wed, 23 Dec 2020 21:11:52 +0800 Subject: [PATCH 20/26] Update and optimize some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 4 ++-- .../manager/AuthBackupRestoreManager.java | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index a096404..ddf6cdd 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -1045,8 +1045,8 @@ public AuthRestoreConflictStrategy convert(String value) { E.checkArgument(value != null && !value.isEmpty(), "Strategy can't be null or empty"); E.checkArgument(AuthRestoreConflictStrategy.matchStrategy(value), - "Invalid --strategy '%s', valid value is" + - " 'stop' or 'ignore", value); + "Invalid --strategy '%s', valid value is " + + "'stop' or 'ignore", value); return AuthRestoreConflictStrategy.fromName(value); } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index 223ec06..f88063e 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -308,7 +308,7 @@ public void restore() { User restoreUser = entry.getValue(); restoreUser.password(initPassword); User user = retry(() -> { - return client.authManager().createUser(restoreUser); + return client.authManager().createUser(restoreUser); }, "Restore users of authority"); idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; @@ -377,7 +377,7 @@ public void restore() { for (Map.Entry entry : groupsByName.entrySet()) { Group restoreGroup = entry.getValue(); Group group = retry(() -> { - return client.authManager().createGroup(restoreGroup); + return client.authManager().createGroup(restoreGroup); }, "Restore groups of authority"); idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; @@ -450,7 +450,7 @@ public void restore() { for (Map.Entry entry : targetsByName.entrySet()) { Target restoreTarget = entry.getValue(); Target target = retry(() -> { - return client.authManager().createTarget(restoreTarget); + return client.authManager().createTarget(restoreTarget); }, "Restore targets of authority"); idsMap.put(restoreTarget.id().toString(), target.id().toString()); @@ -484,8 +484,8 @@ public List checkConflict() { "Querying belongs of authority"); Map belongMap = Maps.newHashMap(); for (Belong belong : belongs) { - belongMap.put(belong.user() + ":" + belong.group(), - belong); + String belongKey = belong.user() + ":" + belong.group(); + belongMap.put(belongKey, belong); } List belongJsons = readRestoreData(HugeType.BELONG); List conflicts = Lists.newArrayList(); @@ -520,7 +520,7 @@ public void restore() { restoreBelong.group(idsMap.get(restoreBelong.group().toString())); retry(() -> { return client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); + }, "Restore belongs of authority"); count++; } Printer.print("Restore belongs finished, count is %d !", count); @@ -545,8 +545,8 @@ public List checkConflict() { "Querying accesses of authority"); Map accessMap = Maps.newHashMap(); for (Access access : accesses) { - accessMap.put(access.group() + ":" + access.target(), - access); + String accessKey = access.group() + ":" + access.target(); + accessMap.put(accessKey, access); } List accessJsons = readRestoreData(HugeType.ACCESS); List conflicts = Lists.newArrayList(); @@ -581,7 +581,7 @@ public void restore() { restoreAccess.group(idsMap.get(restoreAccess.group().toString())); retry(() -> { return client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); + }, "Restore access of authority"); count++; } Printer.print("Restore accesses finished, count is %d !", From 4b9e59d3c699ea6a538436fae4508e7703d21498 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 25 Dec 2020 20:27:46 +0800 Subject: [PATCH 21/26] Opdate and optimize some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 13 ++++--- .../constant/AuthRestoreConflictStrategy.java | 37 ++++++++----------- .../manager/AuthBackupRestoreManager.java | 30 +++++++-------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index ddf6cdd..4be05cc 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -890,15 +890,18 @@ public static class AuthRestore extends AuthBackupRestore { @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, description = "The strategy needs to be chosen in the event " + - "of a conflict in restore. valid strategies include " + - "'stop' and 'ignore', default is 'stop'. 'stop' means " + - "if there a conflict, stop restore. 'ignore' means if " + - "there a conflict, ignore and continue to restore.") + "of a conflict when restoring. valid " + + "strategies include 'stop' and 'ignore', " + + "default is 'stop'. 'stop' means if there " + + "a conflict, stop restore. 'ignore' means if " + + "there a conflict, ignore and continue to " + + "restore.") public AuthRestoreConflictStrategy strategy = AuthStrategyConverter.strategy; @Parameter(names = {"--init-password"}, arity = 1, description = "Init user password, if restore type include " + - "'user', must init user password.") + "'user', please specify the init-password of " + + "users.") public String initPassword = StringUtils.EMPTY; public List types() { diff --git a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java index 0a1036d..ff86506 100644 --- a/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java +++ b/src/main/java/com/baidu/hugegraph/constant/AuthRestoreConflictStrategy.java @@ -37,20 +37,18 @@ public int code() { return this.code; } - public static AuthRestoreConflictStrategy fromName(String name) { - AuthRestoreConflictStrategy[] restoreStrategys = AuthRestoreConflictStrategy.values(); - for (AuthRestoreConflictStrategy strategy : restoreStrategys) { - if (strategy.string().equals(name)) { - return strategy; - } - } - return null; - } - public String string() { return this.name; } + public boolean isStopStrategy() { + return this == AuthRestoreConflictStrategy.STOP; + } + + public boolean isIgnoreStrategy() { + return this == AuthRestoreConflictStrategy.IGNORE; + } + public static boolean matchStrategy(String strategy) { if (AuthRestoreConflictStrategy.STOP.string().equals(strategy) || AuthRestoreConflictStrategy.IGNORE.string().equals(strategy)) { @@ -59,18 +57,13 @@ public static boolean matchStrategy(String strategy) { return false; } - public static boolean matchStrategy(AuthRestoreConflictStrategy strategy) { - if (AuthRestoreConflictStrategy.STOP == strategy || - AuthRestoreConflictStrategy.IGNORE == strategy) { - return true; - } - return false; - } - - public static boolean matchStopStrategy(AuthRestoreConflictStrategy strategy) { - if (AuthRestoreConflictStrategy.STOP == strategy) { - return true; + public static AuthRestoreConflictStrategy fromName(String name) { + AuthRestoreConflictStrategy[] restoreStrategys = AuthRestoreConflictStrategy.values(); + for (AuthRestoreConflictStrategy strategy : restoreStrategys) { + if (strategy.string().equals(name)) { + return strategy; + } } - return false; + return null; } } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index f88063e..c22b63c 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -287,10 +287,10 @@ public List checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkState(AuthRestoreConflictStrategy.matchStrategy( - strategy), + E.checkState(strategy.isStopStrategy() || + strategy.isIgnoreStrategy(), "Restore users strategy is not found"); - if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + if (strategy.isStopStrategy()) { conflicts.add(restoreUser.toString()); } } else { @@ -357,10 +357,10 @@ public List checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkState(AuthRestoreConflictStrategy.matchStrategy( - strategy), + E.checkState(strategy.isStopStrategy() || + strategy.isIgnoreStrategy(), "Restore groups strategy is not found"); - if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + if (strategy.isStopStrategy()) { conflicts.add(restoreGroup.toString()); } } else { @@ -430,10 +430,10 @@ public List checkConflict() { conflict++; } if (conflict > NO_CONFLICT) { - E.checkState(AuthRestoreConflictStrategy.matchStrategy( - strategy), + E.checkState(strategy.isStopStrategy() || + strategy.isIgnoreStrategy(), "Restore targets strategy is not found"); - if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + if (strategy.isStopStrategy()) { conflicts.add(restoreTarget.toString()); } } else { @@ -498,10 +498,10 @@ public List checkConflict() { String ids = idsMap.get(restoreBelong.user()) + ":" + idsMap.get(restoreBelong.group()); if (belongMap.containsKey(ids)) { - E.checkState(AuthRestoreConflictStrategy.matchStrategy( - strategy), + E.checkState(strategy.isStopStrategy() || + strategy.isIgnoreStrategy(), "Restore belongs strategy is not found"); - if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + if (strategy.isStopStrategy()) { conflicts.add(restoreBelong.toString()); } continue; @@ -559,10 +559,10 @@ public List checkConflict() { String ids = idsMap.get(restoreAccess.group()) + ":" + idsMap.get(restoreAccess.target()); if (accessMap.containsKey(ids)) { - E.checkState(AuthRestoreConflictStrategy.matchStrategy( - strategy), + E.checkState(strategy.isStopStrategy() || + strategy.isIgnoreStrategy(), "Restore accesses strategy is not found"); - if (AuthRestoreConflictStrategy.matchStopStrategy(strategy)) { + if (strategy.isStopStrategy()) { conflicts.add(restoreAccess.toString()); } continue; From 4c3d5c586088eb961eb9a42041060f6bbfbea6a7 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Fri, 25 Dec 2020 20:46:20 +0800 Subject: [PATCH 22/26] add checkstyle for code --- checkstyle.xml | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 22 +++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 checkstyle.xml diff --git a/checkstyle.xml b/checkstyle.xml new file mode 100644 index 0000000..08076e7 --- /dev/null +++ b/checkstyle.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3d606b6..80a7a97 100644 --- a/pom.xml +++ b/pom.xml @@ -238,6 +238,28 @@ release + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.0 + + checkstyle.xml + UTF-8 + true + true + false + false + + + + validate + validate + + check + + + + maven-source-plugin From 81aaed2dc8da18b76c6b5eb5994d929f042efef7 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 28 Dec 2020 09:16:02 +0800 Subject: [PATCH 23/26] Optimize some code --- checkstyle.xml | 2 +- .../manager/AuthBackupRestoreManager.java | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/checkstyle.xml b/checkstyle.xml index 08076e7..80c26bf 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -84,4 +84,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index c22b63c..b85c16f 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -313,7 +313,8 @@ public void restore() { idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } - Printer.print("Restore users finished, count is %d !", count); + Printer.print("Restore users finished, total count is %d", + count); } private void prepareUserForRestore(User restoreUser) { @@ -382,7 +383,8 @@ public void restore() { idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } - Printer.print("Restore groups finished, count is %d !", count); + Printer.print("Restore groups finished, total count is %d", + count); } private void prepareGroupForRestore(Group restoreGroup) { @@ -456,7 +458,8 @@ public void restore() { target.id().toString()); count++; } - Printer.print("Restore targets finished, count is %d !", count); + Printer.print("Restore targets finished, total count is %d", + count); } private void prepareTargetForRestore(Target restoreTarget) { @@ -523,7 +526,8 @@ public void restore() { }, "Restore belongs of authority"); count++; } - Printer.print("Restore belongs finished, count is %d !", count); + Printer.print("Restore belongs finished, total count is %d", + count); } } @@ -584,7 +588,7 @@ public void restore() { }, "Restore access of authority"); count++; } - Printer.print("Restore accesses finished, count is %d !", + Printer.print("Restore accesses finished, total count is %d", count); } } From 57d3eb9a449c84c53d63cd2f3524e1a535275c05 Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 4 Jan 2021 12:52:24 +0800 Subject: [PATCH 24/26] Update retry description to lower case --- .../manager/AuthBackupRestoreManager.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index b85c16f..211960e 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -259,7 +259,7 @@ public void backup() { @Override public List checkConflict() { List users = retry(client.authManager()::listUsers, - "Querying users of authority"); + "querying users of authority"); Map userMap = Maps.newHashMap(); for (User user : users) { userMap.put(user.name(), user); @@ -309,7 +309,7 @@ public void restore() { restoreUser.password(initPassword); User user = retry(() -> { return client.authManager().createUser(restoreUser); - }, "Restore users of authority"); + }, "restore users of authority"); idsMap.put(restoreUser.id().toString(), user.id().toString()); count++; } @@ -338,7 +338,7 @@ public void backup() { @Override public List checkConflict() { List groups = retry(client.authManager()::listGroups, - "Querying users of authority"); + "querying users of authority"); Map groupMap = Maps.newHashMap(); for (Group group : groups) { groupMap.put(group.name(), group); @@ -379,7 +379,7 @@ public void restore() { Group restoreGroup = entry.getValue(); Group group = retry(() -> { return client.authManager().createGroup(restoreGroup); - }, "Restore groups of authority"); + }, "restore groups of authority"); idsMap.put(restoreGroup.id().toString(), group.id().toString()); count++; } @@ -408,7 +408,7 @@ public void backup() { @Override public List checkConflict() { List targets = retry(client.authManager()::listTargets, - "Querying targets of authority"); + "querying targets of authority"); Map targetMap = Maps.newHashMap(); for (Target target : targets) { targetMap.put(target.name(), target); @@ -453,7 +453,7 @@ public void restore() { Target restoreTarget = entry.getValue(); Target target = retry(() -> { return client.authManager().createTarget(restoreTarget); - }, "Restore targets of authority"); + }, "restore targets of authority"); idsMap.put(restoreTarget.id().toString(), target.id().toString()); count++; @@ -484,7 +484,7 @@ public void backup() { @Override public List checkConflict() { List belongs = retry(client.authManager()::listBelongs, - "Querying belongs of authority"); + "querying belongs of authority"); Map belongMap = Maps.newHashMap(); for (Belong belong : belongs) { String belongKey = belong.user() + ":" + belong.group(); @@ -523,7 +523,7 @@ public void restore() { restoreBelong.group(idsMap.get(restoreBelong.group().toString())); retry(() -> { return client.authManager().createBelong(restoreBelong); - }, "Restore belongs of authority"); + }, "restore belongs of authority"); count++; } Printer.print("Restore belongs finished, total count is %d", @@ -546,7 +546,7 @@ public void backup() { @Override public List checkConflict() { List accesses = retry(client.authManager()::listAccesses, - "Querying accesses of authority"); + "querying accesses of authority"); Map accessMap = Maps.newHashMap(); for (Access access : accesses) { String accessKey = access.group() + ":" + access.target(); @@ -585,7 +585,7 @@ public void restore() { restoreAccess.group(idsMap.get(restoreAccess.group().toString())); retry(() -> { return client.authManager().createAccess(restoreAccess); - }, "Restore access of authority"); + }, "restore access of authority"); count++; } Printer.print("Restore accesses finished, total count is %d", From 4079bd389a2b758e59dd1e41176ba8cfced6c99d Mon Sep 17 00:00:00 2001 From: xuliguo Date: Mon, 4 Jan 2021 20:46:20 +0800 Subject: [PATCH 25/26] Update and optimize some code --- .../com/baidu/hugegraph/cmd/SubCommands.java | 51 ++++++++----------- .../manager/AuthBackupRestoreManager.java | 6 +-- .../test/functional/AuthBackupTest.java | 3 +- .../baidu/hugegraph/test/util/FileUtil.java | 10 ++-- 4 files changed, 30 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 4be05cc..3e16da8 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -829,6 +829,9 @@ public static class TaskId { public static class AuthBackupRestore { + @ParametersDelegate + private AuthTypes types = new AuthTypes(); + @Parameter(names = {"--directory"}, arity = 1, description = "Directory of auth information, default " + "is './{auth-backup-restore}' in local " + @@ -843,6 +846,14 @@ public static class AuthBackupRestore { @ParametersDelegate private Retry retry = new Retry(); + public List types() { + return this.types.types; + } + + public void types(List types) { + this.types.types = types; + } + public int retry() { return this.retry.retry; } @@ -869,28 +880,14 @@ public void hdfsConf(Map hdfsConf) { } public static class AuthBackup extends AuthBackupRestore { - - @ParametersDelegate - private AuthTypes types = new AuthTypes(); - - public List types() { - return this.types.types; - } - - public void types(List types) { - this.types.types = types; - } } public static class AuthRestore extends AuthBackupRestore { - @ParametersDelegate - private AuthTypes types = new AuthTypes(); - @Parameter(names = {"--strategy"}, converter = AuthStrategyConverter.class, description = "The strategy needs to be chosen in the event " + - "of a conflict when restoring. valid " + + "of a conflict when restoring. Valid " + "strategies include 'stop' and 'ignore', " + "default is 'stop'. 'stop' means if there " + "a conflict, stop restore. 'ignore' means if " + @@ -904,14 +901,6 @@ public static class AuthRestore extends AuthBackupRestore { "users.") public String initPassword = StringUtils.EMPTY; - public List types() { - return this.types.types; - } - - public void types(List types) { - this.types.types = types; - } - public AuthRestoreConflictStrategy strategy() { return this.strategy; } @@ -933,13 +922,15 @@ public static class AuthTypes { @Parameter(names = {"--types", "-t"}, listConverter = AuthHugeTypeConverter.class, - description = "Type of auth data to restore and backup, concat with " + - "',' if more than one. 'all' means all auth information" + - " in other words, 'all' equals with 'user, group, target, " + - "belong, access'. in addition, only 'belong' or 'access' " + - "can not backup or restore, if type contains 'belong' " + - "then should contains 'user' and 'group'. if type contains " + - "'access' then should contains 'group' and 'target'.") + description = "Type of auth data to restore and backup, " + + "concat with ',' if more than one. 'all' " + + "means all auth information. In other words, " + + "'all' equals with 'user, group, target, " + + "belong, access'. In addition, 'belong' or " + + "'access' can not backup or restore alone, if " + + "type contains 'belong' then should contains " + + "'user' and 'group'. if type contains 'access' " + + "then should contains 'group' and 'target'.") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; } diff --git a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java index 211960e..a1a49f2 100644 --- a/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java +++ b/src/main/java/com/baidu/hugegraph/manager/AuthBackupRestoreManager.java @@ -185,7 +185,7 @@ private List readRestoreData(HugeType type) { } } catch (IOException e) { throw new ToolsException("Failed to deserialize %s from %s", - e, resultList, type.string()); + e, type.string(), resultList); } return resultList; } @@ -338,7 +338,7 @@ public void backup() { @Override public List checkConflict() { List groups = retry(client.authManager()::listGroups, - "querying users of authority"); + "querying groups of authority"); Map groupMap = Maps.newHashMap(); for (Group group : groups) { groupMap.put(group.name(), group); @@ -585,7 +585,7 @@ public void restore() { restoreAccess.group(idsMap.get(restoreAccess.group().toString())); retry(() -> { return client.authManager().createAccess(restoreAccess); - }, "restore access of authority"); + }, "restore accesses of authority"); count++; } Printer.print("Restore accesses finished, total count is %d", diff --git a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java index fc96382..3a63a71 100644 --- a/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java +++ b/src/test/java/com/baidu/hugegraph/test/functional/AuthBackupTest.java @@ -27,13 +27,12 @@ import com.baidu.hugegraph.cmd.HugeGraphCommand; import com.baidu.hugegraph.test.util.FileUtil; import com.baidu.hugegraph.testutil.Assert; -import com.beust.jcommander.ParameterException; public class AuthBackupTest extends AuthTest { @Before public void init() { - FileUtil.clearFile(DEFAULT_URL); + FileUtil.clearDirectories(DEFAULT_URL); } @Test diff --git a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java index abcdfbc..13858f9 100644 --- a/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java +++ b/src/test/java/com/baidu/hugegraph/test/util/FileUtil.java @@ -68,7 +68,7 @@ public static List subdirectories(String filePath) { return list; } - public static void clearFile(String filePath) { + public static void clearDirectories(String filePath) { File file = new File(filePath); if (file.exists()) { String[] files = file.list(); @@ -91,7 +91,7 @@ public static long writeTestRestoreData(String filePath, List list) { baos.write(builder.toString().getBytes(API.CHARSET)); os.write(baos.toByteArray()); } catch (IOException e) { - throw new ToolsException("Failed writeText file path is %s", + throw new ToolsException("Failed write file path is %s", e, filePath); } @@ -99,19 +99,19 @@ public static long writeTestRestoreData(String filePath, List list) { } public static List readTestRestoreData(String filePath) { - List resultList = Lists.newArrayList(); + List results = Lists.newArrayList(); try (InputStream is = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(is, API.CHARSET)) { BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { - resultList.add(line); + results.add(line); } } catch (IOException e) { throw new ToolsException("Failed read file path is %s", e, filePath); } - return resultList; + return results; } } From 3589982ab6fea514d6c45cabd224559ab37cf78e Mon Sep 17 00:00:00 2001 From: xuliguo Date: Tue, 5 Jan 2021 12:13:14 +0800 Subject: [PATCH 26/26] =?UTF-8?q?Update=20the=20first=20code=20after=20the?= =?UTF-8?q?=20=E2=80=98.=E2=80=99=20to=20uppercase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/baidu/hugegraph/cmd/SubCommands.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java index 3e16da8..743a88f 100644 --- a/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java +++ b/src/main/java/com/baidu/hugegraph/cmd/SubCommands.java @@ -674,8 +674,8 @@ public static class Protocol { public static class TrustStoreFile { @Parameter(names = {"--trust-store-file"}, arity = 1, - description = "The path of client truststore file used when https " + - "protocol is enabled") + description = "The path of client truststore file used when " + + "https protocol is enabled") public String trustStoreFile; } @@ -700,12 +700,13 @@ public static class HugeTypes { @Parameter(names = {"--huge-types", "-t"}, listConverter = HugeTypeListConverter.class, description = "Type of schema/data. Concat with ',' if more " + - "than one. other types include 'all' and 'schema'." + - " 'all' means all vertices, edges and schema, in " + - "other words, 'all' equals with 'vertex, edge, " + - "vertex_label, edge_label, property_key, index_label'." + - " 'schema' equals with 'vertex_label, edge_label, " + - "property_key, index_label'.") + "than one. Other types include 'all' and " + + "'schema'. 'all' means all vertices, edges and " + + "schema. In other words, 'all' equals with " + + "'vertex, edge, vertex_label, edge_label, " + + "property_key, index_label'. 'schema' equals " + + "with 'vertex_label, edge_label, property_key, " + + "index_label'.") public List types = HugeTypeListConverter.ALL_TYPES; } @@ -929,7 +930,7 @@ public static class AuthTypes { "belong, access'. In addition, 'belong' or " + "'access' can not backup or restore alone, if " + "type contains 'belong' then should contains " + - "'user' and 'group'. if type contains 'access' " + + "'user' and 'group'. If type contains 'access' " + "then should contains 'group' and 'target'.") public List types = AuthHugeTypeConverter.AUTH_ALL_TYPES; }