Conversation
| hugeTypes.add(HugeType.valueOf(type.toUpperCase())); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new ParameterException(String.format( | ||
| "Invalid --type '%s', valid value is 'all' or " + |
There was a problem hiding this comment.
align with ParameterException
| @Override | ||
| public String convert(String value) { | ||
| E.checkArgument(value != null && !value.isEmpty(), | ||
| "Strategy can't be null or empty"); |
| return value; | ||
| } else { | ||
| throw new ParameterException(String.format( | ||
| "Invalid --strategy '%s', valid value is 'stop' or 'ignore", value)); |
There was a problem hiding this comment.
align with ParameterException, and ensure don't exceed 80 chars
| break; | ||
| default: | ||
| throw new AssertionError(String.format( | ||
| "Bad backup type: %s", type)); |
| protected void backupUsers() { | ||
| Printer.print("Users backup started"); | ||
| List<User> users = retry(this.client.authManager()::listUsers, | ||
| "querying users of authority"); |
| protected void backupGroups() { | ||
| Printer.print("Groups backup started"); | ||
| List<Group> groups = retry(this.client.authManager()::listGroups, | ||
| "querying groups of authority"); |
There was a problem hiding this comment.
also check other places for alignment
| private int conflict_status = 0; | ||
| private AuthRestoreStrategy strategy; | ||
| private String initPassword; | ||
| /** |
There was a problem hiding this comment.
use "/*" here, "/**" is just for java doc
|
|
||
|
|
||
|
|
||
|
|
|
|
||
| } | ||
|
|
||
| public static void main(String[] strings) { |
d316753 to
d8b1298
Compare
d8b1298 to
038980c
Compare
| taskClear.force()); | ||
| break; | ||
| case "auth-backup": | ||
| Printer.print("Auth backup start !"); |
| authBackupManager.authBackup(authBackup.types()); | ||
| break; | ||
| case "auth-restore": | ||
| Printer.print("Auth restore start !"); |
| Printer.print("Exception in 'main' is %s", e); | ||
| System.exit(-1); | ||
| } | ||
| System.exit(0); |
There was a problem hiding this comment.
throw exception if test mode else System.exit(-1)
| cmd.execute(subCommand, jCommander); | ||
| System.exit(0); | ||
| } | ||
|
|
|
|
||
| @Parameter(names = {"--strategy"}, | ||
| converter = AuthStrategyConverter.class, | ||
| description = "restore strategy, " + |
| public static final String TRUST_STORE_PASSWORD = ""; | ||
|
|
||
|
|
||
|
|
| .configSSL(trustStoreFile, trustStorePassword) | ||
| .build(); | ||
| } | ||
|
|
| }catch (Exception e) { | ||
| Printer.print("Failed to close file"); | ||
| } | ||
|
|
| }finally { | ||
| if(is != null) { | ||
| try { | ||
| is.close(); |
There was a problem hiding this comment.
use try (resource) to auto close
| if(is != null) { | ||
| try { | ||
| is.close(); | ||
| }catch (Exception e) { |
| import com.beust.jcommander.ParameterException; | ||
| import com.beust.jcommander.Parameters; | ||
| import com.beust.jcommander.ParametersDelegate; | ||
| import com.beust.jcommander.*; |
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.*; |
There was a problem hiding this comment.
put java.* and org.* first and put com.baidu.hugegraph last
| @@ -0,0 +1,30 @@ | |||
| /* | |||
| } | ||
|
|
||
| } | ||
|
|
d962f77 to
cfc5f5a
Compare
| import com.baidu.hugegraph.driver.SchemaManager; | ||
| import com.baidu.hugegraph.driver.TaskManager; | ||
| import com.baidu.hugegraph.driver.TraverserManager; | ||
|
|
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.nio.file.Paths; |
| public static void main(String[] args) { | ||
| HugeGraphCommand cmd = new HugeGraphCommand(); | ||
|
|
||
| JCommander jCommander = parseJCommand(args, cmd); |
There was a problem hiding this comment.
rename to parseCommand and catch it
| Printer.print("Must provide one sub-command"); | ||
| jCommander.usage(); | ||
| System.exit(-1); | ||
| printCommonCommand(jCommander); |
| } | ||
| Printer.print("======================================"); | ||
| Printer.print("You can use 'help' to see more detailed " + | ||
| "\ncommands, such as './bin/hugegraph help'"); |
| } else { | ||
| System.out.println(" Unrecognized command : " | ||
| + info); | ||
| } |
There was a problem hiding this comment.
only print if type y else not, default is [n]
| } | ||
|
|
||
| public static boolean isPrintStackException() { | ||
| System.out.println("Input yes or no to view the stack " + |
There was a problem hiding this comment.
Type y(yes) to print exception stack[default n]?
| import com.baidu.hugegraph.manager.GremlinManager; | ||
| import com.baidu.hugegraph.manager.RestoreManager; | ||
| import com.baidu.hugegraph.manager.TasksManager; | ||
| import com.baidu.hugegraph.manager.*; |
| Printer.print(e.getMessage()); | ||
| System.exit(-1); | ||
| throw new ParameterException(String.format( | ||
| "make sure your command, you can use " + |
| System.exit(-1); | ||
| } else { | ||
| throw new RuntimeException( | ||
| "Exception in 'main' is", e); |
dc371f9 to
529b88e
Compare
529b88e to
a984e85
Compare
| "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 " + |
| @Parameter(names = {"--strategy"}, | ||
| converter = AuthStrategyConverter.class, | ||
| description = "The strategy that needs to be chosen in the " + | ||
| "event of a conflict in restore. strategy include " + |
| public void initPassword(String initPassword) { | ||
| this.initPassword = initPassword; | ||
| } | ||
|
|
| @@ -0,0 +1,150 @@ | |||
| /* | |||
| import com.baidu.hugegraph.base.Printer; | ||
| import com.baidu.hugegraph.base.RetryManager; | ||
| import com.baidu.hugegraph.base.ToolClient; | ||
| import com.baidu.hugegraph.base.*; |
| while ((line = reader.readLine()) != null) { | ||
| consumer.accept(type.string(), line); | ||
| } | ||
| String line; |
|
|
||
| public final class ToolUtil { | ||
|
|
||
| public static void shutdown(List<ToolManager> taskManager) { |
| boolean isTestNode) { | ||
| if (isTestNode) { | ||
| throw new ParameterException( | ||
| "Failed to parse command"); |
There was a problem hiding this comment.
throw new ParameterException("Failed to parse command");
| boolean isTestNode) { | ||
| if (isTestNode) { | ||
| throw new ParameterException( | ||
| "Failed to parse command"); |
| com.baidu.hugegraph.traversal.algorithm.SubGraphTraverser, | ||
| com.baidu.hugegraph.traversal.optimize.Text, | ||
| com.baidu.hugegraph.traversal.optimize.TraversalUtil, | ||
| com.baidu.hugegraph.util.DateUtil |
There was a problem hiding this comment.
update after https://github.com/hugegraph/hugegraph/pull/1289 merge
| import com.baidu.hugegraph.testutil.Assert; | ||
| import com.google.common.collect.Maps; | ||
|
|
||
| public class AuthRestoreTest extends AuthTest{ |
| for (Target target : targets) { | ||
| targetMap.put(target.name(), target); | ||
| } | ||
| Assert.assertTrue(targetMap.containsKey("test_target1")); |
| } | ||
|
|
||
| @Test | ||
| public void testAuthRestoreByStrategyConfict() { |
|
|
||
| Assert.assertThrows(RuntimeException.class, () -> { | ||
| HugeGraphCommand.main(args); | ||
| }); |
There was a problem hiding this comment.
also assert exception message
| int conflict = this.conflict_status; | ||
| User restoreUser = JsonUtil.fromJson(userStr, User.class); | ||
| if (userMap.containsKey(restoreUser.name())) { | ||
| User resourceUser = userMap.get(restoreUser.name()); |
| } | ||
| for (String str : belongJsons) { | ||
| Belong restoreBelong = JsonUtil.fromJson(str, Belong.class); | ||
| if (checkIdMaps(restoreBelong.user().toString(), |
| }, "Restore access of authority"); | ||
| counts++; | ||
| } | ||
| Printer.print("Restore accesses finished, counts is %s !", counts); |
| restoreBelong.group(this.idsMap.get(restoreBelong.group().toString())); | ||
| retry(() -> { | ||
| return this.client.authManager().createBelong(restoreBelong); | ||
| }, "Restore targets of authority"); |
| this.usersByName.put(restoreUser.name(), restoreUser); | ||
| } | ||
|
|
||
| private boolean checkIdMaps(String oneId, String otherId) { |
3457b13 to
8abe8a9
Compare
4ab761e to
b84753b
Compare
| "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) || |
There was a problem hiding this comment.
exceed 80 chars, we can add AuthRestoreConflictStrategy.STOP.match(value) method
| return client.authManager().createUser(restoreUser); | ||
| }, "Restore users of authority"); | ||
| return client.authManager().createUser(restoreUser); | ||
| }, "Restore users of authority"); |
| return client.authManager().createGroup(restoreGroup); | ||
| }, "Restore groups of authority"); | ||
| return client.authManager().createGroup(restoreGroup); | ||
| }, "Restore groups of authority"); |
| } | ||
| if (conflict > NO_CONFLICT) { | ||
| E.checkArgument(strategy != AuthRestoreStrategy.STOP, | ||
| E.checkArgument(strategy != AuthRestoreConflictStrategy.STOP, |
There was a problem hiding this comment.
don't check it since will return conflicts if strategy=STOP
| return client.authManager().createTarget(restoreTarget); | ||
| }, "Restore targets of authority"); | ||
| return client.authManager().createTarget(restoreTarget); | ||
| }, "Restore targets of authority"); |
| } | ||
|
|
||
| public static List<String> getFileDirectoryNames(String filePath) { | ||
| public static List<String> getFileSubdirectories(String filePath) { |
There was a problem hiding this comment.
just subdirectories() method is ok
| "Restore targets strategy is not found"); | ||
| if (strategy == AuthRestoreConflictStrategy.STOP) { | ||
| conflicts.add(String.format("target name : %s", | ||
| restoreTarget.name())); |
There was a problem hiding this comment.
use conflicts.add(restoreTarget) and rewrite restoreTarget.toString
| private Map<String, Belong> belongsByName; | ||
| private Map<String, Access> accessesByName; | ||
| private List<AuthManager> authManagers; | ||
| private List<String> conflicts; |
There was a problem hiding this comment.
just let checkConflict() return List<String> conflicts
| "--init-password", "123456" | ||
| }; | ||
|
|
||
| Assert.assertThrows(IllegalArgumentException.class, () -> { |
There was a problem hiding this comment.
don't throw IllegalArgumentException, prefer IllegalStateException
| } | ||
|
|
||
| @Test | ||
| public void testAuthRestoreWithStopStrategy() { |
There was a problem hiding this comment.
testAuthRestoreWithConflictAndStopStrategy
| try { | ||
| hugeTypes.add(HugeType.valueOf(type.toUpperCase())); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new ParameterException(String.format( |
There was a problem hiding this comment.
replace ParameterException with IllegalArgumentException
|
|
||
| private void doRestore() { | ||
| for (AuthManager authManager : this.authManagers) { | ||
| authManager.checkConflict(); |
| this.hdfsConf = hdfsConf; | ||
| } | ||
|
|
||
| public void retry(int retry) { |
| public static final String strategy = "stop"; | ||
|
|
||
| @Override | ||
| public String convert(String value) { |
| this.hdfsConf = hdfsConf; | ||
| } | ||
|
|
||
| public void retry(int retry) { |
There was a problem hiding this comment.
move to line 849, put similar methods together
| public static final String strategy = "stop"; | ||
|
|
||
| @Override | ||
| public String convert(String value) { |
98ed387 to
65f03fa
Compare
| "Strategy can't be null or empty"); | ||
| E.checkArgument(AuthRestoreConflictStrategy.matchStrategy(value), | ||
| "Invalid --strategy '%s', valid value is" + | ||
| " 'stop' or 'ignore", value); |
There was a problem hiding this comment.
put space to the previous line
| User restoreUser = entry.getValue(); | ||
| restoreUser.password(initPassword); | ||
| User user = retry(() -> { | ||
| return client.authManager().createUser(restoreUser); |
| for (Map.Entry<String, Target> entry : targetsByName.entrySet()) { | ||
| Target restoreTarget = entry.getValue(); | ||
| Target target = retry(() -> { | ||
| return client.authManager().createTarget(restoreTarget); |
| "Querying belongs of authority"); | ||
| Map<String, Belong> belongMap = Maps.newHashMap(); | ||
| for (Belong belong : belongs) { | ||
| belongMap.put(belong.user() + ":" + belong.group(), |
There was a problem hiding this comment.
add local var key = belong.user() + ":" + belong.group()
| restoreBelong.group(idsMap.get(restoreBelong.group().toString())); | ||
| retry(() -> { | ||
| return client.authManager().createBelong(restoreBelong); | ||
| }, "Restore belongs of authority"); |
| restoreAccess.group(idsMap.get(restoreAccess.group().toString())); | ||
| retry(() -> { | ||
| return client.authManager().createAccess(restoreAccess); | ||
| }, "Restore access of authority"); |
| return false; | ||
| } | ||
|
|
||
| public static boolean matchStrategy(AuthRestoreConflictStrategy strategy) { |
| return false; | ||
| } | ||
|
|
||
| public static boolean matchStopStrategy(AuthRestoreConflictStrategy strategy) { |
There was a problem hiding this comment.
don't set static and change to isStopStrategy(), move to line 53
also add isIgnoreStrategy()
|
|
||
| @Parameter(names = {"--init-password"}, arity = 1, | ||
| description = "Init user password, if restore type include " + | ||
| "'user', must init user password.") |
There was a problem hiding this comment.
please specify the init-password of users
| @Parameter(names = {"--strategy"}, | ||
| converter = AuthStrategyConverter.class, | ||
| description = "The strategy needs to be chosen in the event " + | ||
| "of a conflict in restore. valid strategies include " + |
There was a problem hiding this comment.
in restore => when restoring
checkstyle.xml
Outdated
| <!--覆盖clone()方法时调用了super.clone()方法--> | ||
| <module name="SuperClone"/> | ||
| </module> | ||
| </module> No newline at end of file |
| idsMap.put(restoreGroup.id().toString(), group.id().toString()); | ||
| count++; | ||
| } | ||
| Printer.print("Restore groups finished, count is %d !", count); |
| public void backup() { | ||
| Printer.print("Belongs backup started..."); | ||
| List<Belong> belongs = retry(client.authManager()::listBelongs, | ||
| "querying belongs of authority"); |
| } | ||
|
|
||
| public static List<String> readTestRestoreData(String filePath) { | ||
| List<String> resultList = Lists.newArrayList(); |
| baos.write(builder.toString().getBytes(API.CHARSET)); | ||
| os.write(baos.toByteArray()); | ||
| } catch (IOException e) { | ||
| throw new ToolsException("Failed writeText file path is %s", |
| return list; | ||
| } | ||
|
|
||
| public static void clearFile(String filePath) { |
There was a problem hiding this comment.
seems function is clear directories
| @Parameter(names = {"--strategy"}, | ||
| converter = AuthStrategyConverter.class, | ||
| description = "The strategy needs to be chosen in the event " + | ||
| "of a conflict when restoring. valid " + |
| 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, " + |
| "',' 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' " + |
There was a problem hiding this comment.
'belong' or 'access' can not backup or restore alone
| public static class AuthBackup extends AuthBackupRestore { | ||
|
|
||
| @ParametersDelegate | ||
| private AuthTypes types = new AuthTypes(); |
There was a problem hiding this comment.
move types to AuthBackupRestore
| } | ||
| } catch (IOException e) { | ||
| throw new ToolsException("Failed to deserialize %s from %s", | ||
| e, resultList, type.string()); |
| @Override | ||
| public List<String> checkConflict() { | ||
| List<Group> groups = retry(client.authManager()::listGroups, | ||
| "querying users of authority"); |
| restoreAccess.group(idsMap.get(restoreAccess.group().toString())); | ||
| retry(() -> { | ||
| return client.authManager().createAccess(restoreAccess); | ||
| }, "restore access of authority"); |
No description provided.