You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by lx...@apache.org on 2018/04/24 22:50:14 UTC
[3/3] helix git commit: Move all command tool classes into a separate
sub-folder.
Move all command tool classes into a separate sub-folder.
Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/67620753
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/67620753
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/67620753
Branch: refs/heads/master
Commit: 6762075306b3dcc94d9d263f2f2dc5c0b4f686f5
Parents: 5f9fadc
Author: Lei Xia <lx...@linkedin.com>
Authored: Wed Mar 21 10:10:35 2018 -0700
Committer: Lei Xia <lx...@linkedin.com>
Committed: Tue Apr 24 15:49:42 2018 -0700
----------------------------------------------------------------------
helix-core/pom.xml | 20 +-
.../apache/helix/tools/IntegrationTestUtil.java | 206 ------
.../java/org/apache/helix/tools/JmxDumper.java | 412 ------------
.../org/apache/helix/tools/LocalZKServer.java | 68 --
.../java/org/apache/helix/tools/TaskAdmin.java | 285 --------
.../apache/helix/tools/YAMLClusterSetup.java | 323 ----------
.../java/org/apache/helix/tools/ZKDumper.java | 232 -------
.../org/apache/helix/tools/ZKLogFormatter.java | 341 ----------
.../java/org/apache/helix/tools/ZkCopy.java | 253 --------
.../java/org/apache/helix/tools/ZkGrep.java | 642 -------------------
.../apache/helix/tools/ZkLogCSVFormatter.java | 320 ---------
.../tools/commandtools/IntegrationTestUtil.java | 259 ++++++++
.../helix/tools/commandtools/JmxDumper.java | 412 ++++++++++++
.../helix/tools/commandtools/LocalZKServer.java | 68 ++
.../helix/tools/commandtools/TaskAdmin.java | 285 ++++++++
.../tools/commandtools/YAMLClusterSetup.java | 324 ++++++++++
.../helix/tools/commandtools/ZKDumper.java | 232 +++++++
.../tools/commandtools/ZKLogFormatter.java | 341 ++++++++++
.../apache/helix/tools/commandtools/ZkCopy.java | 253 ++++++++
.../apache/helix/tools/commandtools/ZkGrep.java | 642 +++++++++++++++++++
.../tools/commandtools/ZkLogCSVFormatter.java | 320 +++++++++
.../java/org/apache/helix/tools/TestZkCopy.java | 4 +-
22 files changed, 3149 insertions(+), 3093 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/pom.xml
----------------------------------------------------------------------
diff --git a/helix-core/pom.xml b/helix-core/pom.xml
index a29243e..4887987 100644
--- a/helix-core/pom.xml
+++ b/helix-core/pom.xml
@@ -200,19 +200,19 @@ under the License.
<name>helix-admin</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.ZKDumper</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.ZKDumper</mainClass>
<name>zk-dumper</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.ZkCopy</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.ZkCopy</mainClass>
<name>zkcopy</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.ZKLogFormatter</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.ZKLogFormatter</mainClass>
<name>zk-log-parser</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.ZkLogCSVFormatter</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.ZkLogCSVFormatter</mainClass>
<name>zk-log-csv-formatter</name>
</program>
<program>
@@ -224,7 +224,7 @@ under the License.
<name>start-participants</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.LocalZKServer</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.LocalZKServer</mainClass>
<name>start-standalone-zookeeper</name>
</program>
<program>
@@ -232,23 +232,23 @@ under the License.
<name>quickstart</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.JmxDumper</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.JmxDumper</mainClass>
<name>JmxDumper</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.YAMLClusterSetup</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.YAMLClusterSetup</mainClass>
<name>yaml-cluster-setup</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.IntegrationTestUtil</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.IntegrationTestUtil</mainClass>
<name>test-util</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.ZkGrep</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.ZkGrep</mainClass>
<name>zkgrep</name>
</program>
<program>
- <mainClass>org.apache.helix.tools.TaskAdmin</mainClass>
+ <mainClass>org.apache.helix.tools.commandtools.TaskAdmin</mainClass>
<name>task-admin</name>
</program>
</programs>
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/IntegrationTestUtil.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/IntegrationTestUtil.java b/helix-core/src/main/java/org/apache/helix/tools/IntegrationTestUtil.java
deleted file mode 100644
index a1dbaac..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/IntegrationTestUtil.java
+++ /dev/null
@@ -1,206 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.OptionGroup;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
-import org.apache.helix.PropertyKey;
-import org.apache.helix.ZNRecord;
-import org.apache.helix.manager.zk.ZNRecordSerializer;
-import org.apache.helix.manager.zk.ZkClient;
-import org.apache.helix.tools.ClusterVerifiers.ClusterLiveNodesVerifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * collection of test utilities for integration tests
- */
-public class IntegrationTestUtil {
- private static Logger LOG = LoggerFactory.getLogger(IntegrationTestUtil.class);
-
- public static final long defaultTimeout = 30 * 1000; // in milliseconds
- public static final String help = "help";
- public static final String zkSvr = "zkSvr";
-
- public static final String verifyExternalView = "verifyExternalView";
- public static final String verifyLiveNodes = "verifyLiveNodes";
- public static final String readZNode = "readZNode";
- public static final String readLeader = "readLeader";
-
- final ZkClient _zkclient;
- final ZNRecordSerializer _serializer;
-
- public IntegrationTestUtil(ZkClient zkclient) {
- _zkclient = zkclient;
- _serializer = new ZNRecordSerializer();
- }
-
- public void verifyExternalView(String[] args) {
- if (args == null || args.length == 0) {
- System.err.println("Illegal arguments for " + verifyExternalView);
- return;
- }
-
- long timeoutValue = defaultTimeout;
-
- String clusterName = args[0];
- List<String> liveNodes = new ArrayList<String>();
- for (int i = 1; i < args.length; i++) {
- liveNodes.add(args[i]);
- }
-
- ClusterExternalViewVerifier verifier =
- new ClusterExternalViewVerifier(_zkclient, clusterName, liveNodes);
- boolean success = verifier.verifyByPolling(timeoutValue);
- System.out.println(success ? "Successful" : "Failed");
-
- }
-
- public void verifyLiveNodes(String[] args) {
- if (args == null || args.length == 0) {
- System.err.println("Illegal arguments for " + verifyLiveNodes);
- return;
- }
-
- long timeoutValue = defaultTimeout;
-
- String clusterName = args[0];
- List<String> liveNodes = new ArrayList<String>();
- for (int i = 1; i < args.length; i++) {
- liveNodes.add(args[i]);
- }
-
- ClusterLiveNodesVerifier verifier =
- new ClusterLiveNodesVerifier(_zkclient, clusterName, liveNodes);
- boolean success = verifier.verify(timeoutValue);
- System.out.println(success ? "Successful" : "Failed");
- }
-
- public void readZNode(String path) {
- ZNRecord record = _zkclient.readData(path, true);
- if (record == null) {
- System.out.println("null");
- } else {
- System.out.println(new String(_serializer.serialize(record)));
- }
- }
-
- @SuppressWarnings("static-access")
- static Options constructCommandLineOptions() {
- Option helpOption =
- OptionBuilder.withLongOpt(help).withDescription("Prints command-line options information")
- .create();
-
- Option zkSvrOption =
- OptionBuilder.hasArgs(1).isRequired(true).withArgName("zookeeperAddress")
- .withLongOpt(zkSvr).withDescription("Provide zookeeper-address").create();
-
- Option verifyExternalViewOption =
- OptionBuilder.hasArgs().isRequired(false).withArgName("clusterName node1 node2..")
- .withLongOpt(verifyExternalView).withDescription("Verify external-view").create();
-
- Option verifyLiveNodesOption =
- OptionBuilder.hasArg().isRequired(false).withArgName("clusterName node1, node2..")
- .withLongOpt(verifyLiveNodes).withDescription("Verify live-nodes").create();
-
- Option readZNodeOption =
- OptionBuilder.hasArgs(1).isRequired(false).withArgName("zkPath").withLongOpt(readZNode)
- .withDescription("Read znode").create();
-
- Option readLeaderOption =
- OptionBuilder.hasArgs(1).isRequired(false).withArgName("clusterName")
- .withLongOpt(readLeader).withDescription("Read cluster controller").create();
-
- OptionGroup optGroup = new OptionGroup();
- optGroup.setRequired(true);
- optGroup.addOption(verifyExternalViewOption);
- optGroup.addOption(verifyLiveNodesOption);
- optGroup.addOption(readZNodeOption);
- optGroup.addOption(readLeaderOption);
-
- Options options = new Options();
- options.addOption(helpOption);
- options.addOption(zkSvrOption);
- options.addOptionGroup(optGroup);
-
- return options;
- }
-
- static void printUsage(Options cliOptions) {
- HelpFormatter helpFormatter = new HelpFormatter();
- helpFormatter.setWidth(1000);
- helpFormatter.printHelp("java " + ClusterExternalViewVerifier.class.getName(), cliOptions);
- }
-
- static void processCommandLineArgs(String[] cliArgs) {
- CommandLineParser cliParser = new GnuParser();
- Options cliOptions = constructCommandLineOptions();
- CommandLine cmd = null;
- try {
- cmd = cliParser.parse(cliOptions, cliArgs);
- } catch (ParseException pe) {
- System.err.println("failed to parse command-line args: " + Arrays.asList(cliArgs)
- + ", exception: " + pe.toString());
- printUsage(cliOptions);
- System.exit(1);
- }
-
- String zkServer = cmd.getOptionValue(zkSvr);
-
- ZkClient zkclient =
- new ZkClient(zkServer, ZkClient.DEFAULT_SESSION_TIMEOUT,
- ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
- IntegrationTestUtil util = new IntegrationTestUtil(zkclient);
-
- if (cmd != null) {
- if (cmd.hasOption(verifyExternalView)) {
- String[] args = cmd.getOptionValues(verifyExternalView);
- util.verifyExternalView(args);
- } else if (cmd.hasOption(verifyLiveNodes)) {
- String[] args = cmd.getOptionValues(verifyLiveNodes);
- util.verifyLiveNodes(args);
- } else if (cmd.hasOption(readZNode)) {
- String path = cmd.getOptionValue(readZNode);
- util.readZNode(path);
- } else if (cmd.hasOption(readLeader)) {
- String clusterName = cmd.getOptionValue(readLeader);
- PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
- util.readZNode(keyBuilder.controllerLeader().getPath());
- } else {
- printUsage(cliOptions);
- }
- }
- }
-
- public static void main(String[] args) {
- processCommandLineArgs(args);
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/JmxDumper.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/JmxDumper.java b/helix-core/src/main/java/org/apache/helix/tools/JmxDumper.java
deleted file mode 100644
index aa39666..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/JmxDumper.java
+++ /dev/null
@@ -1,412 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import java.io.FileWriter;
-import java.io.PrintWriter;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.ConcurrentHashMap;
-
-import javax.management.MBeanAttributeInfo;
-import javax.management.MBeanInfo;
-import javax.management.MBeanOperationInfo;
-import javax.management.MBeanServerConnection;
-import javax.management.MBeanServerDelegate;
-import javax.management.MBeanServerNotification;
-import javax.management.Notification;
-import javax.management.NotificationListener;
-import javax.management.ObjectInstance;
-import javax.management.ObjectName;
-import javax.management.relation.MBeanServerNotificationFilter;
-import javax.management.remote.JMXConnector;
-import javax.management.remote.JMXConnectorFactory;
-import javax.management.remote.JMXServiceURL;
-
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class JmxDumper implements NotificationListener {
- public static final String help = "help";
- public static final String domain = "domain";
- public static final String fields = "fields";
- public static final String pattern = "pattern";
- public static final String operations = "operations";
- public static final String period = "period";
- public static final String className = "className";
- public static final String outputFile = "outputFile";
- public static final String jmxUrl = "jmxUrl";
- public static final String sampleCount = "sampleCount";
-
- private static final Logger _logger = LoggerFactory.getLogger(JmxDumper.class);
- String _domain;
- MBeanServerConnection _mbeanServer;
-
- String _beanClassName;
- String _namePattern;
- int _samplePeriod;
-
- Map<ObjectName, ObjectName> _mbeanNames = new ConcurrentHashMap<ObjectName, ObjectName>();
- Timer _timer;
-
- String _outputFileName;
-
- List<String> _outputFields = new ArrayList<String>();
- Set<String> _operations = new HashSet<String>();
- PrintWriter _outputFile;
- int _samples = 0;
- int _targetSamples = -1;
- String _jmxUrl;
-
- public JmxDumper(String jmxService, String domain, String beanClassName, String namePattern,
- int samplePeriod, List<String> fields, List<String> operations, String outputfile,
- int sampleCount) throws Exception {
- _jmxUrl = jmxService;
- _domain = domain;
- _beanClassName = beanClassName;
- _samplePeriod = samplePeriod;
- _outputFields.addAll(fields);
- _operations.addAll(operations);
- _outputFileName = outputfile;
- _namePattern = namePattern;
- _targetSamples = sampleCount;
-
- JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + _jmxUrl + "/jmxrmi");
- JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
-
- _mbeanServer = jmxc.getMBeanServerConnection();
- MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
- filter.enableAllObjectNames();
- _mbeanServer.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, filter, null);
- init();
- _timer = new Timer(true);
- _timer.scheduleAtFixedRate(new SampleTask(), _samplePeriod, _samplePeriod);
- }
-
- class SampleTask extends TimerTask {
- @Override
- public void run() {
- List<ObjectName> errorMBeans = new ArrayList<ObjectName>();
- _logger.info("Sampling " + _mbeanNames.size() + " beans");
- for (ObjectName beanName : _mbeanNames.keySet()) {
- MBeanInfo info;
- try {
- info = _mbeanServer.getMBeanInfo(beanName);
- } catch (Exception e) {
- _logger.error(e.getMessage() + " removing it");
- errorMBeans.add(beanName);
- continue;
- }
- if (!info.getClassName().equals(_beanClassName)) {
- _logger.warn("Skip: className " + info.getClassName() + " expected : " + _beanClassName);
- continue;
- }
- StringBuffer line = new StringBuffer();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss:SSS");
- String date = dateFormat.format(new Date());
- line.append(date + " ");
- line.append(beanName.toString() + " ");
-
- MBeanAttributeInfo[] infos = info.getAttributes();
- Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>();
- for (MBeanAttributeInfo infoItem : infos) {
- infoMap.put(infoItem.getName(), infoItem);
- }
-
- for (String outputField : _outputFields) {
- try {
- if (infoMap.containsKey(outputField)) {
- Object mbeanAttributeValue = _mbeanServer.getAttribute(beanName, outputField);
- line.append(mbeanAttributeValue.toString() + " ");
- } else {
- _logger.warn(outputField + " not found");
- line.append("null ");
- }
- } catch (Exception e) {
- _logger.error("Error:", e);
- line.append("null ");
- continue;
- }
- }
- MBeanOperationInfo[] operations = info.getOperations();
- Map<String, MBeanOperationInfo> opeMap = new HashMap<String, MBeanOperationInfo>();
- for (MBeanOperationInfo opeItem : operations) {
- opeMap.put(opeItem.getName(), opeItem);
- }
-
- for (String ope : _operations) {
- if (opeMap.containsKey(ope)) {
- try {
- _mbeanServer.invoke(beanName, ope, new Object[0], new String[0]);
- // System.out.println(ope+" invoked");
- } catch (Exception e) {
- _logger.error("Error:", e);
- continue;
- }
- }
- }
- _outputFile.println(line.toString());
- // System.out.println(line);
- }
- for (ObjectName deadBean : errorMBeans) {
- _mbeanNames.remove(deadBean);
- }
-
- _samples++;
- // System.out.println("samples:"+_samples);
- if (_samples == _targetSamples) {
- synchronized (JmxDumper.this) {
- _logger.info(_samples + " samples done, exiting...");
- JmxDumper.this.notifyAll();
- }
- }
- }
- }
-
- void init() throws Exception {
- try {
- Set<ObjectInstance> existingInstances =
- _mbeanServer.queryMBeans(new ObjectName(_namePattern), null);
- _logger.info("Total " + existingInstances.size() + " mbeans matched " + _namePattern);
- for (ObjectInstance instance : existingInstances) {
- if (instance.getClassName().equals(_beanClassName)) {
- _mbeanNames.put(instance.getObjectName(), instance.getObjectName());
- _logger.info("Sampling " + instance.getObjectName());
- }
- }
- FileWriter fos = new FileWriter(_outputFileName);
- System.out.println(_outputFileName);
- _outputFile = new PrintWriter(fos);
- } catch (Exception e) {
- _logger.error("fail to get all existing mbeans in " + _domain, e);
- throw e;
- }
- }
-
- @Override
- public void handleNotification(Notification notification, Object handback) {
- MBeanServerNotification mbs = (MBeanServerNotification) notification;
- if (MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType())) {
- // System.out.println("Adding mbean " + mbs.getMBeanName());
- _logger.info("Adding mbean " + mbs.getMBeanName());
- if (mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain)) {
- addMBean(mbs.getMBeanName());
- }
- } else if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType())) {
- // System.out.println("Removing mbean " + mbs.getMBeanName());
- _logger.info("Removing mbean " + mbs.getMBeanName());
- if (mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain)) {
- removeMBean(mbs.getMBeanName());
- }
- }
- }
-
- private void addMBean(ObjectName beanName) {
- _mbeanNames.put(beanName, beanName);
- }
-
- private void removeMBean(ObjectName beanName) {
- _mbeanNames.remove(beanName);
- }
-
- public static int processCommandLineArgs(String[] cliArgs) throws Exception {
- CommandLineParser cliParser = new GnuParser();
- Options cliOptions = constructCommandLineOptions();
- CommandLine cmd = null;
-
- try {
- cmd = cliParser.parse(cliOptions, cliArgs);
- } catch (ParseException pe) {
- System.err.println("CommandLineClient: failed to parse command-line options: "
- + pe.toString());
- printUsage(cliOptions);
- System.exit(1);
- }
- boolean ret = checkOptionArgsNumber(cmd.getOptions());
- if (ret == false) {
- printUsage(cliOptions);
- System.exit(1);
- }
-
- String portStr = cmd.getOptionValue(jmxUrl);
- // int portVal = Integer.parseInt(portStr);
-
- String periodStr = cmd.getOptionValue(period);
- int periodVal = Integer.parseInt(periodStr);
-
- String domainStr = cmd.getOptionValue(domain);
- String classNameStr = cmd.getOptionValue(className);
- String patternStr = cmd.getOptionValue(pattern);
- String fieldsStr = cmd.getOptionValue(fields);
- String operationsStr = cmd.getOptionValue(operations);
- String resultFile = cmd.getOptionValue(outputFile);
- String sampleCountStr = cmd.getOptionValue(sampleCount, "-1");
- int sampleCount = Integer.parseInt(sampleCountStr);
-
- List<String> fields = Arrays.asList(fieldsStr.split(","));
- List<String> operations = Arrays.asList(operationsStr.split(","));
-
- JmxDumper dumper = null;
- try {
- dumper =
- new JmxDumper(portStr, domainStr, classNameStr, patternStr, periodVal, fields,
- operations, resultFile, sampleCount);
- synchronized (dumper) {
- dumper.wait();
- }
- } finally {
- if (dumper != null) {
- dumper.flushFile();
- }
- }
- return 0;
- }
-
- private void flushFile() {
- if (_outputFile != null) {
- _outputFile.flush();
- _outputFile.close();
- }
- }
-
- private static boolean checkOptionArgsNumber(Option[] options) {
- for (Option option : options) {
- int argNb = option.getArgs();
- String[] args = option.getValues();
- if (argNb == 0) {
- if (args != null && args.length > 0) {
- System.err.println(option.getArgName() + " shall have " + argNb + " arguments (was "
- + Arrays.toString(args) + ")");
- return false;
- }
- } else {
- if (args == null || args.length != argNb) {
- System.err.println(option.getArgName() + " shall have " + argNb + " arguments (was "
- + Arrays.toString(args) + ")");
- return false;
- }
- }
- }
- return true;
- }
-
- @SuppressWarnings("static-access")
- private static Options constructCommandLineOptions() {
- Option helpOption =
- OptionBuilder.withLongOpt(help).withDescription("Prints command-line options info")
- .create();
- Option domainOption =
- OptionBuilder.withLongOpt(domain).withDescription("Domain of the JMX bean").create();
-
- domainOption.setArgs(1);
- domainOption.setRequired(true);
-
- Option fieldsOption =
- OptionBuilder.withLongOpt(fields).withDescription("Fields of the JMX bean to sample")
- .create();
- fieldsOption.setArgs(1);
- fieldsOption.setRequired(false);
-
- Option operationOption =
- OptionBuilder.withLongOpt(operations).withDescription("Operation to invoke").create();
- operationOption.setArgs(1);
- operationOption.setRequired(true);
-
- Option periodOption =
- OptionBuilder.withLongOpt(period).withDescription("Sampling period in MS").create();
- periodOption.setArgs(1);
- periodOption.setRequired(false);
-
- Option classOption =
- OptionBuilder.withLongOpt(className).withDescription("Classname of the MBean").create();
- classOption.setArgs(1);
- classOption.setRequired(true);
-
- Option patternOption =
- OptionBuilder.withLongOpt(pattern).withDescription("pattern of the MBean").create();
- patternOption.setArgs(1);
- patternOption.setRequired(true);
-
- Option outputFileOption =
- OptionBuilder.withLongOpt(outputFile).withDescription("outputFileName").create();
- outputFileOption.setArgs(1);
- outputFileOption.setRequired(false);
-
- Option jmxUrlOption =
- OptionBuilder.withLongOpt(jmxUrl).withDescription("jmx port to connect to").create();
- jmxUrlOption.setArgs(1);
- jmxUrlOption.setRequired(true);
-
- Option sampleCountOption =
- OptionBuilder.withLongOpt(sampleCount).withDescription("# of samples to take").create();
- sampleCountOption.setArgs(1);
- sampleCountOption.setRequired(false);
-
- Options options = new Options();
- options.addOption(helpOption);
- options.addOption(domainOption);
- options.addOption(fieldsOption);
- options.addOption(operationOption);
- options.addOption(classOption);
- options.addOption(outputFileOption);
- options.addOption(jmxUrlOption);
- options.addOption(patternOption);
- options.addOption(periodOption);
- options.addOption(sampleCountOption);
- return options;
- }
-
- public static void printUsage(Options cliOptions) {
- HelpFormatter helpFormatter = new HelpFormatter();
- helpFormatter.printHelp("java " + JmxDumper.class.getName(), cliOptions);
- }
-
- public static void main(String[] args) throws Exception {
- /*
- * List<String> fields = Arrays.asList(new
- * String("AvgLatency,MaxLatency,MinLatency,PacketsReceived,PacketsSent").split(","));
- * List<String> operations = Arrays.asList(new String("resetCounters").split(","));
- * JmxDumper dumper = new JmxDumper(27961, "org.apache.zooKeeperService",
- * "org.apache.zookeeper.server.ConnectionBean",
- * "org.apache.ZooKeeperService:name0=*,name1=Connections,name2=*,name3=*", 1000, fields,
- * operations, "/tmp/1.csv");
- * Thread.currentThread().join();
- */
- int ret = processCommandLineArgs(args);
- System.exit(ret);
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/LocalZKServer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/LocalZKServer.java b/helix-core/src/main/java/org/apache/helix/tools/LocalZKServer.java
deleted file mode 100644
index 4faa52c..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/LocalZKServer.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import org.I0Itec.zkclient.IDefaultNameSpace;
-import org.I0Itec.zkclient.ZkClient;
-import org.I0Itec.zkclient.ZkServer;
-
-/**
- * Provides ability to start zookeeper locally on a particular port
- */
-public class LocalZKServer {
- public void start(int port, String dataDir, String logDir) throws Exception {
-
- IDefaultNameSpace defaultNameSpace = new IDefaultNameSpace() {
-
- @Override
- public void createDefaultNameSpace(ZkClient zkClient) {
-
- }
- };
- ZkServer server = new ZkServer(dataDir, logDir, defaultNameSpace, port);
- server.start();
- Thread.currentThread().join();
- }
-
- public static void main(String[] args) throws Exception {
- int port = 2199;
- String rootDir =
- System.getProperty("java.io.tmpdir") + "/zk-helix/" + System.currentTimeMillis();
- String dataDir = rootDir + "/dataDir";
- String logDir = rootDir + "/logDir";
-
- if (args.length > 0) {
- port = Integer.parseInt(args[0]);
- }
- if (args.length > 1) {
- dataDir = args[1];
- logDir = args[1];
- }
-
- if (args.length > 2) {
- logDir = args[2];
- }
- System.out.println("Starting Zookeeper locally at port:" + port + " dataDir:" + dataDir
- + " logDir:" + logDir);
- LocalZKServer localZKServer = new LocalZKServer();
-
- localZKServer.start(port, dataDir, logDir);
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/TaskAdmin.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/TaskAdmin.java b/helix-core/src/main/java/org/apache/helix/tools/TaskAdmin.java
deleted file mode 100644
index 8044f64..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/TaskAdmin.java
+++ /dev/null
@@ -1,285 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import com.google.common.collect.Lists;
-import java.io.File;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.OptionGroup;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
-import org.apache.helix.HelixManager;
-import org.apache.helix.HelixManagerFactory;
-import org.apache.helix.InstanceType;
-import org.apache.helix.task.JobConfig;
-import org.apache.helix.task.JobContext;
-import org.apache.helix.task.TaskConfig;
-import org.apache.helix.task.TaskDriver;
-import org.apache.helix.task.TaskPartitionState;
-import org.apache.helix.task.TaskState;
-import org.apache.helix.task.Workflow;
-import org.apache.helix.task.WorkflowConfig;
-import org.apache.helix.task.WorkflowContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * CLI for operating workflows and jobs.
- * This is a wrapper of TaskDriver instance to allow command line changes of workflows and jobs.
- */
-public class TaskAdmin {
- /** For logging */
- private static final Logger LOG = LoggerFactory.getLogger(TaskAdmin.class);
-
- /** Required option name for Helix endpoint */
- private static final String ZK_ADDRESS = "zk";
-
- /** Required option name for cluster against which to run task */
- private static final String CLUSTER_NAME_OPTION = "cluster";
-
- /** Required option name for task resource within target cluster */
- private static final String RESOURCE_OPTION = "resource";
-
- /** Field for specifying a workflow file when starting a job */
- private static final String WORKFLOW_FILE_OPTION = "file";
-
- /**
- * Parses the first argument as a driver command and the rest of the
- * arguments are parsed based on that command. Constructs a Helix
- * message and posts it to the controller
- */
- public static void main(String[] args) throws Exception {
- String[] cmdArgs = Arrays.copyOfRange(args, 1, args.length);
- CommandLine cl = parseOptions(cmdArgs, constructOptions(), args[0]);
- String zkAddr = cl.getOptionValue(ZK_ADDRESS);
- String clusterName = cl.getOptionValue(CLUSTER_NAME_OPTION);
- String workflow = cl.getOptionValue(RESOURCE_OPTION);
-
- if (zkAddr == null || clusterName == null || workflow == null) {
- printUsage(constructOptions(), "[cmd]");
- throw new IllegalArgumentException(
- "zk, cluster, and resource must all be non-null for all commands");
- }
-
- HelixManager helixMgr =
- HelixManagerFactory.getZKHelixManager(clusterName, "Admin", InstanceType.ADMINISTRATOR,
- zkAddr);
- helixMgr.connect();
- TaskDriver driver = new TaskDriver(helixMgr);
- try {
- TaskDriver.DriverCommand cmd = TaskDriver.DriverCommand.valueOf(args[0]);
- switch (cmd) {
- case start:
- if (cl.hasOption(WORKFLOW_FILE_OPTION)) {
- driver.start(Workflow.parse(new File(cl.getOptionValue(WORKFLOW_FILE_OPTION))));
- } else {
- throw new IllegalArgumentException("Workflow file is required to start flow.");
- }
- break;
- case stop:
- driver.stop(workflow);
- break;
- case resume:
- driver.resume(workflow);
- break;
- case delete:
- driver.delete(workflow);
- break;
- case list:
- list(driver, workflow);
- break;
- case flush:
- driver.flushQueue(workflow);
- break;
- case clean:
- driver.cleanupQueue(workflow);
- break;
- default:
- throw new IllegalArgumentException("Unknown command " + args[0]);
- }
- } catch (IllegalArgumentException e) {
- LOG.error("Unknown driver command " + args[0]);
- throw e;
- }
-
- helixMgr.disconnect();
- }
-
- private static void list(TaskDriver taskDriver, String workflow) {
- WorkflowConfig wCfg = taskDriver.getWorkflowConfig(workflow);
- if (wCfg == null) {
- LOG.error("Workflow " + workflow + " does not exist!");
- return;
- }
- WorkflowContext wCtx = taskDriver.getWorkflowContext(workflow);
-
- LOG.info("Workflow " + workflow + " consists of the following tasks: " + wCfg.getJobDag()
- .getAllNodes());
- String workflowState =
- (wCtx != null) ? wCtx.getWorkflowState().name() : TaskState.NOT_STARTED.name();
- LOG.info("Current state of workflow is " + workflowState);
- LOG.info("Job states are: ");
- LOG.info("-------");
- for (String job : wCfg.getJobDag().getAllNodes()) {
- TaskState jobState = (wCtx != null) ? wCtx.getJobState(job) : TaskState.NOT_STARTED;
- LOG.info("Job " + job + " is " + jobState);
-
- // fetch job information
- JobConfig jCfg = taskDriver.getJobConfig(job);
- JobContext jCtx = taskDriver.getJobContext(job);
- if (jCfg == null || jCtx == null) {
- LOG.info("-------");
- continue;
- }
-
- // calculate taskPartitions
- List<Integer> partitions = Lists.newArrayList(jCtx.getPartitionSet());
- Collections.sort(partitions);
-
- // report status
- for (Integer partition : partitions) {
- String taskId = jCtx.getTaskIdForPartition(partition);
- taskId = (taskId != null) ? taskId : jCtx.getTargetForPartition(partition);
- LOG.info("Task: " + taskId);
- TaskConfig taskConfig = jCfg.getTaskConfig(taskId);
- if (taskConfig != null) {
- LOG.info("Configuration: " + taskConfig.getConfigMap());
- }
- TaskPartitionState state = jCtx.getPartitionState(partition);
- state = (state != null) ? state : TaskPartitionState.INIT;
- LOG.info("State: " + state);
- String assignedParticipant = jCtx.getAssignedParticipant(partition);
- if (assignedParticipant != null) {
- LOG.info("Assigned participant: " + assignedParticipant);
- }
- LOG.info("-------");
- }
- LOG.info("-------");
- }
- }
-
- /** Constructs option group containing options required by all drivable jobs */
- @SuppressWarnings("static-access")
- private static OptionGroup contructGenericRequiredOptionGroup() {
- Option zkAddressOption =
- OptionBuilder.isRequired().withLongOpt(ZK_ADDRESS)
- .withDescription("ZK address managing cluster").create();
- zkAddressOption.setArgs(1);
- zkAddressOption.setArgName("zkAddress");
-
- Option clusterNameOption =
- OptionBuilder.isRequired().withLongOpt(CLUSTER_NAME_OPTION).withDescription("Cluster name")
- .create();
- clusterNameOption.setArgs(1);
- clusterNameOption.setArgName("clusterName");
-
- Option taskResourceOption =
- OptionBuilder.isRequired().withLongOpt(RESOURCE_OPTION)
- .withDescription("Workflow or job name").create();
- taskResourceOption.setArgs(1);
- taskResourceOption.setArgName("resourceName");
-
- OptionGroup group = new OptionGroup();
- group.addOption(zkAddressOption);
- group.addOption(clusterNameOption);
- group.addOption(taskResourceOption);
- return group;
- }
-
- /** Constructs options set for all basic control messages */
- private static Options constructOptions() {
- Options options = new Options();
- options.addOptionGroup(contructGenericRequiredOptionGroup());
- options.addOptionGroup(constructStartOptionGroup());
- return options;
- }
-
- /** Constructs option group containing options required by all drivable jobs */
- private static OptionGroup constructStartOptionGroup() {
- @SuppressWarnings("static-access")
- Option workflowFileOption =
- OptionBuilder.withLongOpt(WORKFLOW_FILE_OPTION)
- .withDescription("Local file describing workflow").create();
- workflowFileOption.setArgs(1);
- workflowFileOption.setArgName("workflowFile");
-
- OptionGroup group = new OptionGroup();
- group.addOption(workflowFileOption);
- return group;
- }
-
- /** Attempts to parse options for given command, printing usage under failure */
- private static CommandLine parseOptions(String[] args, Options options, String cmdStr) {
- CommandLineParser cliParser = new GnuParser();
- CommandLine cmd = null;
-
- try {
- cmd = cliParser.parse(options, args);
- } catch (ParseException pe) {
- LOG.error("CommandLineClient: failed to parse command-line options: " + pe.toString());
- printUsage(options, cmdStr);
- System.exit(1);
- }
- boolean ret = checkOptionArgsNumber(cmd.getOptions());
- if (!ret) {
- printUsage(options, cmdStr);
- System.exit(1);
- }
-
- return cmd;
- }
-
- /** Ensures options argument counts are correct */
- private static boolean checkOptionArgsNumber(Option[] options) {
- for (Option option : options) {
- int argNb = option.getArgs();
- String[] args = option.getValues();
- if (argNb == 0) {
- if (args != null && args.length > 0) {
- System.err.println(option.getArgName() + " shall have " + argNb + " arguments (was "
- + Arrays.toString(args) + ")");
- return false;
- }
- } else {
- if (args == null || args.length != argNb) {
- System.err.println(option.getArgName() + " shall have " + argNb + " arguments (was "
- + Arrays.toString(args) + ")");
- return false;
- }
- }
- }
- return true;
- }
-
- /** Displays CLI usage for given option set and command name */
- private static void printUsage(Options cliOptions, String cmd) {
- HelpFormatter helpFormatter = new HelpFormatter();
- helpFormatter.setWidth(1000);
- helpFormatter.printHelp("java " + TaskAdmin.class.getName() + " " + cmd, cliOptions);
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/YAMLClusterSetup.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/YAMLClusterSetup.java b/helix-core/src/main/java/org/apache/helix/tools/YAMLClusterSetup.java
deleted file mode 100644
index 6147aaa..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/YAMLClusterSetup.java
+++ /dev/null
@@ -1,323 +0,0 @@
-package org.apache.helix.tools;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.helix.HelixAdmin;
-import org.apache.helix.HelixException;
-import org.apache.helix.manager.zk.ZKHelixAdmin;
-import org.apache.helix.manager.zk.ZKHelixManager;
-import org.apache.helix.model.HelixConfigScope;
-import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
-import org.apache.helix.model.IdealState;
-import org.apache.helix.model.IdealState.RebalanceMode;
-import org.apache.helix.model.InstanceConfig;
-import org.apache.helix.model.StateModelDefinition;
-import org.apache.helix.model.builder.HelixConfigScopeBuilder;
-import org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ParticipantConfig;
-import org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig;
-import org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig.ConstraintsConfig;
-import org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig.StateModelConfig;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.yaml.snakeyaml.Yaml;
-
-/*
- * 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.
- */
-
-/**
- * Supports HelixAdmin operations specified by a YAML configuration file defining a cluster,
- * resources, participants, etc.
- * See the user-rebalanced-lock-manager recipe for an annotated example file.
- */
-public class YAMLClusterSetup {
- private static final Logger LOG = LoggerFactory.getLogger(YAMLClusterSetup.class);
-
- private final String _zkAddress;
-
- /**
- * Start the YAML parser for a given zookeeper instance
- * @param zkAddress
- */
- public YAMLClusterSetup(String zkAddress) {
- _zkAddress = zkAddress;
- }
-
- /**
- * Set up the cluster by parsing a YAML file.
- * @param input InputStream representing the file
- * @return ClusterConfig Java wrapper of the configuration file
- */
- public YAMLClusterConfig setupCluster(InputStream input) {
- // parse the YAML
- Yaml yaml = new Yaml();
- YAMLClusterConfig cfg = yaml.loadAs(input, YAMLClusterConfig.class);
-
- // create the cluster
- HelixAdmin helixAdmin = new ZKHelixAdmin(_zkAddress);
- if (cfg.clusterName == null) {
- throw new HelixException("Cluster name is required!");
- }
- helixAdmin.addCluster(cfg.clusterName);
-
- // add each participant
- if (cfg.participants != null) {
- for (ParticipantConfig participant : cfg.participants) {
- helixAdmin.addInstance(cfg.clusterName, getInstanceCfg(participant));
- }
- }
-
- // add each resource
- if (cfg.resources != null) {
- for (ResourceConfig resource : cfg.resources) {
- if (resource.name == null) {
- throw new HelixException("Resources must be named!");
- }
- if (resource.stateModel == null || resource.stateModel.name == null) {
- throw new HelixException("Resource must specify a named state model!");
- }
- // if states is null, assume using a built-in or already-added state model
- if (resource.stateModel.states != null) {
- StateModelDefinition stateModelDef =
- getStateModelDef(resource.stateModel, resource.constraints);
- helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
- } else {
- StateModelDefinition stateModelDef = null;
- if (resource.stateModel.name.equals("MasterSlave")) {
- stateModelDef =
- new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
- } else if (resource.stateModel.name.equals("OnlineOffline")) {
- stateModelDef =
- new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline());
- } else if (resource.stateModel.name.equals("LeaderStandby")) {
- stateModelDef =
- new StateModelDefinition(StateModelConfigGenerator.generateConfigForLeaderStandby());
- }
- if (stateModelDef != null) {
- try {
- helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
- } catch (HelixException e) {
- LOG.warn("State model definition " + resource.stateModel.name
- + " could not be added.");
- }
- }
- }
- int partitions = 1;
- int replicas = 1;
- if (resource.partitions != null) {
- if (resource.partitions.containsKey("count")) {
- partitions = resource.partitions.get("count");
- }
- if (resource.partitions.containsKey("replicas")) {
- replicas = resource.partitions.get("replicas");
- }
- }
-
- if (resource.rebalancer == null || !resource.rebalancer.containsKey("mode")) {
- throw new HelixException("Rebalance mode is required!");
- }
- helixAdmin.addResource(cfg.clusterName, resource.name, partitions,
- resource.stateModel.name, resource.rebalancer.get("mode"));
- // user-defined rebalancer
- if (resource.rebalancer.containsKey("class")
- && resource.rebalancer.get("mode").equals(RebalanceMode.USER_DEFINED.toString())) {
- IdealState idealState = helixAdmin.getResourceIdealState(cfg.clusterName, resource.name);
- idealState.setRebalancerClassName(resource.rebalancer.get("class"));
- helixAdmin.setResourceIdealState(cfg.clusterName, resource.name, idealState);
- }
- helixAdmin.rebalance(cfg.clusterName, resource.name, replicas);
- }
- }
-
- // enable auto join if this option is set
- if (cfg.autoJoinAllowed != null && cfg.autoJoinAllowed) {
- HelixConfigScope scope =
- new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(cfg.clusterName)
- .build();
- Map<String, String> properties = new HashMap<String, String>();
- properties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, cfg.autoJoinAllowed.toString());
- helixAdmin.setConfig(scope, properties);
- }
- return cfg;
- }
-
- private static InstanceConfig getInstanceCfg(ParticipantConfig participant) {
- if (participant == null || participant.name == null || participant.host == null
- || participant.port == null) {
- throw new HelixException("Participant must have a specified name, host, and port!");
- }
- InstanceConfig instanceCfg = new InstanceConfig(participant.name);
- instanceCfg.setHostName(participant.host);
- instanceCfg.setPort(participant.port.toString());
- return instanceCfg;
- }
-
- private static StateModelDefinition getStateModelDef(StateModelConfig stateModel,
- ConstraintsConfig constraints) {
- // Use a builder to define the state model
- StateModelDefinition.Builder builder = new StateModelDefinition.Builder(stateModel.name);
- if (stateModel.states == null || stateModel.states.size() == 0) {
- throw new HelixException("List of states are required in a state model!");
- }
- Set<String> stateSet = new HashSet<String>(stateModel.states);
- if (stateModel.initialState == null) {
- throw new HelixException("Initial state is required in a state model!");
- } else if (!stateSet.contains(stateModel.initialState)) {
- throw new HelixException("Initial state is not a valid state");
- }
- builder.initialState(stateModel.initialState);
-
- // Build a helper for state priorities
- Map<String, Integer> statePriorities = new HashMap<String, Integer>();
- if (constraints != null && constraints.state != null && constraints.state.priorityList != null) {
- int statePriority = 0;
- for (String state : constraints.state.priorityList) {
- if (!stateSet.contains(state)) {
- throw new HelixException("State " + state
- + " in the state priority list is not in the state list!");
- }
- statePriorities.put(state, statePriority);
- statePriority++;
- }
- }
-
- // Add states, set state priorities
- for (String state : stateModel.states) {
- if (statePriorities.containsKey(state)) {
- builder.addState(state, statePriorities.get(state));
- } else {
- builder.addState(state);
- }
- }
-
- // Set state counts
- for (Map<String, String> counts : constraints.state.counts) {
- String state = counts.get("name");
- if (!stateSet.contains(state)) {
- throw new HelixException("State " + state + " has a count, but not in the state list!");
- }
- builder.dynamicUpperBound(state, counts.get("count"));
- }
-
- // Build a helper for transition priorities
- Map<String, Integer> transitionPriorities = new HashMap<String, Integer>();
- if (constraints != null && constraints.transition != null
- && constraints.transition.priorityList != null) {
- int transitionPriority = 0;
- for (String transition : constraints.transition.priorityList) {
- transitionPriorities.put(transition, transitionPriority);
- transitionPriority++;
- }
- }
-
- // Add the transitions
- if (stateModel.transitions == null || stateModel.transitions.size() == 0) {
- throw new HelixException("Transitions are required!");
- }
- for (Map<String, String> transitions : stateModel.transitions) {
- String name = transitions.get("name");
- String from = transitions.get("from");
- String to = transitions.get("to");
- if (name == null || from == null || to == null) {
- throw new HelixException("All transitions must have a name, a from state, and a to state");
- }
- if (transitionPriorities.containsKey(name)) {
- builder.addTransition(from, to, transitionPriorities.get(name));
- } else {
- builder.addTransition(from, to);
- }
- }
-
- return builder.build();
- }
-
- /**
- * Java wrapper for the YAML input file
- */
- public static class YAMLClusterConfig {
- public String clusterName;
- public List<ResourceConfig> resources;
- public List<ParticipantConfig> participants;
- public Boolean autoJoinAllowed;
-
- public static class ResourceConfig {
- public String name;
- public Map<String, String> rebalancer;
- public Map<String, Integer> partitions;
- public StateModelConfig stateModel;
- public ConstraintsConfig constraints;
-
- public static class StateModelConfig {
- public String name;
- public List<String> states;
- public List<Map<String, String>> transitions;
- public String initialState;
- }
-
- public static class ConstraintsConfig {
- public StateConstraintsConfig state;
- public TransitionConstraintsConfig transition;
-
- public static class StateConstraintsConfig {
- public List<Map<String, String>> counts;
- public List<String> priorityList;
- }
-
- public static class TransitionConstraintsConfig {
- public List<String> priorityList;
- }
- }
- }
-
- public static class ParticipantConfig {
- public String name;
- public String host;
- public Integer port;
- }
- }
-
- /**
- * Start a cluster defined by a YAML file
- * @param args zkAddr, yamlFile
- */
- public static void main(String[] args) {
- if (args.length < 2) {
- LOG.error("USAGE: YAMLClusterSetup zkAddr yamlFile");
- return;
- }
- String zkAddress = args[0];
- String yamlFile = args[1];
-
- InputStream input;
- try {
- input = new FileInputStream(new File(yamlFile));
- } catch (FileNotFoundException e) {
- LOG.error("Could not open " + yamlFile);
- return;
- }
- new YAMLClusterSetup(zkAddress).setupCluster(input);
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java b/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
deleted file mode 100644
index 6fb50ff..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
+++ /dev/null
@@ -1,232 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.List;
-
-import org.I0Itec.zkclient.serialize.ZkSerializer;
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.OptionGroup;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.PosixParser;
-import org.apache.helix.manager.zk.ByteArraySerializer;
-import org.apache.helix.manager.zk.ZkClient;
-
-/**
- * Dumps the Zookeeper file structure on to Disk
- */
-@SuppressWarnings("static-access")
-public class ZKDumper {
- private ZkClient client;
- private FilenameFilter filter;
- static Options options;
- private String suffix = "";
- // enable by default
- private boolean removeSuffix = false;
-
- public String getSuffix() {
- return suffix;
- }
-
- public void setSuffix(String suffix) {
- this.suffix = suffix;
- }
-
- public boolean isRemoveSuffix() {
- return removeSuffix;
- }
-
- public void setRemoveSuffix(boolean removeSuffix) {
- this.removeSuffix = removeSuffix;
- }
-
- static {
- options = new Options();
- OptionGroup optionGroup = new OptionGroup();
-
- Option d =
- OptionBuilder.withLongOpt("download").withDescription("Download from ZK to File System")
- .create();
- d.setArgs(0);
- Option dSuffix =
- OptionBuilder.withLongOpt("addSuffix")
- .withDescription("add suffix to every file downloaded from ZK").create();
- dSuffix.setArgs(1);
- dSuffix.setRequired(false);
-
- Option u =
- OptionBuilder.withLongOpt("upload").withDescription("Upload from File System to ZK")
- .create();
- u.setArgs(0);
- Option uSuffix =
- OptionBuilder.withLongOpt("removeSuffix")
- .withDescription("remove suffix from every file uploaded to ZK").create();
- uSuffix.setArgs(0);
- uSuffix.setRequired(false);
-
- Option del =
- OptionBuilder.withLongOpt("delete").withDescription("Delete given path from ZK").create();
-
- optionGroup.setRequired(true);
- optionGroup.addOption(del);
- optionGroup.addOption(u);
- optionGroup.addOption(d);
- options.addOptionGroup(optionGroup);
- options.addOption("zkSvr", true, "Zookeeper address");
- options.addOption("zkpath", true, "Zookeeper path");
- options.addOption("fspath", true, "Path on local Filesystem to dump");
- options.addOption("h", "help", false, "Print this usage information");
- options.addOption("v", "verbose", false, "Print out VERBOSE information");
- options.addOption(dSuffix);
- options.addOption(uSuffix);
- }
-
- public ZKDumper(String zkAddress) {
- client = new ZkClient(zkAddress, ZkClient.DEFAULT_CONNECTION_TIMEOUT);
-
- ZkSerializer zkSerializer = new ByteArraySerializer();
- client.setZkSerializer(zkSerializer);
- filter = new FilenameFilter() {
-
- @Override
- public boolean accept(File dir, String name) {
- return !name.startsWith(".");
- }
- };
- }
-
- public static void main(String[] args) throws Exception {
- if (args == null || args.length == 0) {
- HelpFormatter helpFormatter = new HelpFormatter();
- helpFormatter.printHelp("java " + ZKDumper.class.getName(), options);
- System.exit(1);
- }
- CommandLineParser parser = new PosixParser();
- CommandLine cmd = parser.parse(options, args);
- cmd.hasOption("zkSvr");
- boolean download = cmd.hasOption("download");
- boolean upload = cmd.hasOption("upload");
- boolean del = cmd.hasOption("delete");
- String zkAddress = cmd.getOptionValue("zkSvr");
- String zkPath = cmd.getOptionValue("zkpath");
- String fsPath = cmd.getOptionValue("fspath");
-
- ZKDumper zkDump = new ZKDumper(zkAddress);
- if (download) {
- if (cmd.hasOption("addSuffix")) {
- zkDump.suffix = cmd.getOptionValue("addSuffix");
- }
- zkDump.download(zkPath, fsPath + zkPath);
- }
- if (upload) {
- if (cmd.hasOption("removeSuffix")) {
- zkDump.removeSuffix = true;
- }
- zkDump.upload(zkPath, fsPath);
- }
- if (del) {
- zkDump.delete(zkPath);
- }
- }
-
- private void delete(String zkPath) {
- client.deleteRecursively(zkPath);
-
- }
-
- public void upload(String zkPath, String fsPath) throws Exception {
- File file = new File(fsPath);
- System.out.println("Uploading " + file.getCanonicalPath() + " to " + zkPath);
- zkPath = zkPath.replaceAll("[/]+", "/");
- int index = -1;
- if (removeSuffix && (index = file.getName().indexOf(".")) > -1) {
- zkPath = zkPath.replaceAll(file.getName().substring(index), "");
- }
- if (file.isDirectory()) {
- File[] children = file.listFiles(filter);
- client.createPersistent(zkPath, true);
- if (children != null && children.length > 0) {
-
- for (File child : children) {
- upload(zkPath + "/" + child.getName(), fsPath + "/" + child.getName());
- }
- } else {
-
- }
- } else {
- byte[] result = new byte[(int) file.length()];
- InputStream input = null;
- try {
- int totalBytesRead = 0;
- input = new BufferedInputStream(new FileInputStream(file));
- while (totalBytesRead < result.length) {
- int bytesRemaining = result.length - totalBytesRead;
- // input.read() returns -1, 0, or more :
- int bytesRead = input.read(result, totalBytesRead, bytesRemaining);
- if (bytesRead > 0) {
- totalBytesRead = totalBytesRead + bytesRead;
- }
- }
- /*
- * the above style is a bit tricky: it places bytes into the 'result'
- * array; 'result' is an output parameter; the while loop usually has a
- * single iteration only.
- */
-
- client.createPersistent(zkPath, result);
- } finally {
- input.close();
- }
-
- }
- }
-
- public void download(String zkPath, String fsPath) throws Exception {
-
- List<String> children = client.getChildren(zkPath);
- if (children != null && children.size() > 0) {
- new File(fsPath).mkdirs();
- for (String child : children) {
- String childPath = zkPath.equals("/") ? "/" + child : zkPath + "/" + child;
- download(childPath, fsPath + "/" + child);
- }
- } else {
- System.out
- .println("Saving " + zkPath + " to " + new File(fsPath + suffix).getCanonicalPath());
- OutputStream out = new FileOutputStream(fsPath + suffix);
- Object readData = client.readData(zkPath);
- if (readData != null) {
- out.write((byte[]) readData);
- }
- out.close();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/ZKLogFormatter.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/ZKLogFormatter.java b/helix-core/src/main/java/org/apache/helix/tools/ZKLogFormatter.java
deleted file mode 100644
index 2d2caf4..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/ZKLogFormatter.java
+++ /dev/null
@@ -1,341 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
-import java.io.BufferedWriter;
-import java.io.EOFException;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.text.DateFormat;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Set;
-import java.util.zip.Adler32;
-import java.util.zip.Checksum;
-
-import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
-
-import org.apache.jute.BinaryInputArchive;
-import org.apache.jute.Record;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.zookeeper.KeeperException.NoNodeException;
-import org.apache.zookeeper.ZooDefs.OpCode;
-import org.apache.zookeeper.data.Stat;
-import org.apache.zookeeper.server.DataNode;
-import org.apache.zookeeper.server.DataTree;
-import org.apache.zookeeper.server.persistence.FileHeader;
-import org.apache.zookeeper.server.persistence.FileSnap;
-import org.apache.zookeeper.server.persistence.FileTxnLog;
-import org.apache.zookeeper.server.util.SerializeUtils;
-import org.apache.zookeeper.txn.TxnHeader;
-
-public class ZKLogFormatter {
- private static final Logger LOG = LoggerFactory.getLogger(ZKLogFormatter.class);
- private static DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(DateFormat.SHORT,
- DateFormat.LONG);
- private static HexBinaryAdapter adapter = new HexBinaryAdapter();
- private static String fieldDelim = ":";
- private static String fieldSep = " ";
-
- static BufferedWriter bw = null;
-
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- if (args.length != 2 && args.length != 3) {
- System.err.println("USAGE: LogFormatter <log|snapshot> log_file");
- System.exit(2);
- }
-
- if (args.length == 3) {
- bw = new BufferedWriter(new FileWriter(new File(args[2])));
- }
-
- if (args[0].equals("log")) {
- readTransactionLog(args[1]);
- } else if (args[0].equals("snapshot")) {
- readSnapshotLog(args[1]);
- }
-
- if (bw != null) {
- bw.close();
- }
- }
-
- private static void readSnapshotLog(String snapshotPath) throws Exception {
- FileInputStream fis = new FileInputStream(snapshotPath);
- BinaryInputArchive ia = BinaryInputArchive.getArchive(fis);
- Map<Long, Integer> sessions = new HashMap<Long, Integer>();
- DataTree dt = new DataTree();
- FileHeader header = new FileHeader();
- header.deserialize(ia, "fileheader");
- if (header.getMagic() != FileSnap.SNAP_MAGIC) {
- throw new IOException("mismatching magic headers " + header.getMagic() + " != "
- + FileSnap.SNAP_MAGIC);
- }
- SerializeUtils.deserializeSnapshot(dt, ia, sessions);
-
- if (bw != null) {
- bw.write(sessions.toString());
- bw.newLine();
- } else {
- System.out.println(sessions);
- }
- traverse(dt, 1, "/");
-
- }
-
- /*
- * Level order traversal
- */
- private static void traverse(DataTree dt, int startId, String startPath) throws Exception {
- LinkedList<Pair> queue = new LinkedList<Pair>();
- queue.add(new Pair(startPath, startId));
- while (!queue.isEmpty()) {
- Pair pair = queue.removeFirst();
- String path = pair._path;
- DataNode head = dt.getNode(path);
- Stat stat = new Stat();
- byte[] data = null;
- try {
- data = dt.getData(path, stat, null);
- } catch (NoNodeException e) {
- e.printStackTrace();
- }
- // print the node
- format(startId, pair, head, data);
- Set<String> children = head.getChildren();
- if (children != null) {
- for (String child : children) {
- String childPath;
- if (path.endsWith("/")) {
- childPath = path + child;
- } else {
- childPath = path + "/" + child;
- }
- queue.add(new Pair(childPath, startId));
- }
- }
- startId = startId + 1;
- }
-
- }
-
- static class Pair {
-
- private final String _path;
- private final int _parentId;
-
- public Pair(String path, int parentId) {
- _path = path;
- _parentId = parentId;
- }
-
- }
-
- private static void format(int id, Pair pair, DataNode head, byte[] data) throws Exception {
- String dataStr = "";
- if (data != null) {
- dataStr = new String(data).replaceAll("[\\s]+", "");
- }
- StringBuffer sb = new StringBuffer();
- // @formatter:off
- sb.append("id").append(fieldDelim).append(id).append(fieldSep);
- sb.append("parent").append(fieldDelim).append(pair._parentId).append(fieldSep);
- sb.append("path").append(fieldDelim).append(pair._path).append(fieldSep);
- sb.append("session").append(fieldDelim)
- .append("0x" + Long.toHexString(head.stat.getEphemeralOwner())).append(fieldSep);
- sb.append("czxid").append(fieldDelim).append("0x" + Long.toHexString(head.stat.getCzxid()))
- .append(fieldSep);
- sb.append("ctime").append(fieldDelim).append(head.stat.getCtime()).append(fieldSep);
- sb.append("mtime").append(fieldDelim).append(head.stat.getMtime()).append(fieldSep);
- sb.append("cmzxid").append(fieldDelim).append("0x" + Long.toHexString(head.stat.getMzxid()))
- .append(fieldSep);
- sb.append("pzxid").append(fieldDelim).append("0x" + Long.toHexString(head.stat.getPzxid()))
- .append(fieldSep);
- sb.append("aversion").append(fieldDelim).append(head.stat.getAversion()).append(fieldSep);
- sb.append("cversion").append(fieldDelim).append(head.stat.getCversion()).append(fieldSep);
- sb.append("version").append(fieldDelim).append(head.stat.getVersion()).append(fieldSep);
- sb.append("data").append(fieldDelim).append(dataStr).append(fieldSep);
- // @formatter:on
-
- if (bw != null) {
- bw.write(sb.toString());
- bw.newLine();
- } else {
- System.out.println(sb);
- }
-
- }
-
- private static void readTransactionLog(String logfilepath) throws FileNotFoundException,
- IOException, EOFException {
- FileInputStream fis = new FileInputStream(logfilepath);
- BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
- FileHeader fhdr = new FileHeader();
- fhdr.deserialize(logStream, "fileheader");
-
- if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
- System.err.println("Invalid magic number for " + logfilepath);
- System.exit(2);
- }
-
- if (bw != null) {
- bw.write("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid()
- + " txnlog format version " + fhdr.getVersion());
- bw.newLine();
- } else {
- System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid()
- + " txnlog format version " + fhdr.getVersion());
- }
-
- int count = 0;
- while (true) {
- long crcValue;
- byte[] bytes;
- try {
- crcValue = logStream.readLong("crcvalue");
-
- bytes = logStream.readBuffer("txnEntry");
- } catch (EOFException e) {
- if (bw != null) {
- bw.write("EOF reached after " + count + " txns.");
- bw.newLine();
- } else {
- System.out.println("EOF reached after " + count + " txns.");
- }
-
- break;
- }
- if (bytes.length == 0) {
- // Since we preallocate, we define EOF to be an
- // empty transaction
- if (bw != null) {
- bw.write("EOF reached after " + count + " txns.");
- bw.newLine();
- } else {
- System.out.println("EOF reached after " + count + " txns.");
- }
-
- return;
- }
- Checksum crc = new Adler32();
- crc.update(bytes, 0, bytes.length);
- if (crcValue != crc.getValue()) {
- throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
- }
- TxnHeader hdr = new TxnHeader();
- Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
- if (bw != null) {
- bw.write(formatTransaction(hdr, txn));
- bw.newLine();
- } else {
- System.out.println(formatTransaction(hdr, txn));
- }
-
- if (logStream.readByte("EOR") != 'B') {
- LOG.error("Last transaction was partial.");
- throw new EOFException("Last transaction was partial.");
- }
- count++;
- }
- }
-
- static String op2String(int op) {
- switch (op) {
- case OpCode.notification:
- return "notification";
- case OpCode.create:
- return "create";
- case OpCode.delete:
- return "delete";
- case OpCode.exists:
- return "exists";
- case OpCode.getData:
- return "getDate";
- case OpCode.setData:
- return "setData";
- case OpCode.getACL:
- return "getACL";
- case OpCode.setACL:
- return "setACL";
- case OpCode.getChildren:
- return "getChildren";
- case OpCode.getChildren2:
- return "getChildren2";
- case OpCode.ping:
- return "ping";
- case OpCode.createSession:
- return "createSession";
- case OpCode.closeSession:
- return "closeSession";
- case OpCode.error:
- return "error";
- default:
- return "unknown " + op;
- }
- }
-
- private static String formatTransaction(TxnHeader header, Record txn) {
- StringBuilder sb = new StringBuilder();
-
- sb.append("time").append(fieldDelim).append(header.getTime());
- sb.append(fieldSep).append("session").append(fieldDelim).append("0x")
- .append(Long.toHexString(header.getClientId()));
- sb.append(fieldSep).append("cxid").append(fieldDelim).append("0x")
- .append(Long.toHexString(header.getCxid()));
- sb.append(fieldSep).append("zxid").append(fieldDelim).append("0x")
- .append(Long.toHexString(header.getZxid()));
- sb.append(fieldSep).append("type").append(fieldDelim).append(op2String(header.getType()));
- if (txn != null) {
- try {
- byte[] data = null;
- for (PropertyDescriptor pd : Introspector.getBeanInfo(txn.getClass())
- .getPropertyDescriptors()) {
- if (pd.getName().equalsIgnoreCase("data")) {
- data = (byte[]) pd.getReadMethod().invoke(txn);
- continue;
- }
- if (pd.getReadMethod() != null && !"class".equals(pd.getName())) {
- sb.append(fieldSep).append(pd.getDisplayName()).append(fieldDelim)
- .append(pd.getReadMethod().invoke(txn).toString().replaceAll("[\\s]+", ""));
- }
- }
- if (data != null) {
- sb.append(fieldSep).append("data").append(fieldDelim)
- .append(new String(data).replaceAll("[\\s]+", ""));
- }
- } catch (Exception e) {
- LOG.error("Error while retrieving bean property values for " + txn.getClass(), e);
- }
- }
-
- return sb.toString();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/helix/blob/67620753/helix-core/src/main/java/org/apache/helix/tools/ZkCopy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/ZkCopy.java b/helix-core/src/main/java/org/apache/helix/tools/ZkCopy.java
deleted file mode 100644
index 648d4ef..0000000
--- a/helix-core/src/main/java/org/apache/helix/tools/ZkCopy.java
+++ /dev/null
@@ -1,253 +0,0 @@
-package org.apache.helix.tools;
-
-/*
- * 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.
- */
-
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.GnuParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.OptionBuilder;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
-import org.apache.helix.AccessOption;
-import org.apache.helix.BaseDataAccessor;
-import org.apache.helix.manager.zk.ByteArraySerializer;
-import org.apache.helix.manager.zk.ZkBaseDataAccessor;
-import org.apache.helix.manager.zk.ZkClient;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.zookeeper.common.PathUtils;
-import org.apache.zookeeper.data.Stat;
-
-/**
- * Tool for copying a zk/file path to another zk/file path
- */
-public class ZkCopy {
- enum ZkCopyScheme {
- zk
- }
-
- private static Logger logger = LoggerFactory.getLogger(ZkCopy.class);
- private static final String src = "src";
- private static final String dst = "dst";
-
- @SuppressWarnings("static-access")
- private static Options constructCmdLineOpt() {
- Option srcOpt =
- OptionBuilder.withLongOpt(src).hasArgs(1).isRequired(true)
- .withArgName("source-URI (e.g. zk://localhost:2181/src-path")
- .withDescription("Provide source URI").create();
-
- Option dstOpt =
- OptionBuilder.withLongOpt(dst).hasArgs(1).isRequired(true)
- .withArgName("destination-URI (e.g. zk://localhost:2181/dst-path")
- .withDescription("Provide destination URI").create();
-
- Options options = new Options();
- options.addOption(srcOpt);
- options.addOption(dstOpt);
- return options;
- }
-
- private static void printUsage(Options cliOptions) {
- HelpFormatter helpFormatter = new HelpFormatter();
- helpFormatter.setWidth(1000);
- helpFormatter.printHelp("java " + ZkCopy.class.getName(), cliOptions);
- }
-
- private static String concatenate(String path, String suffix) {
- if (suffix == null || suffix.isEmpty()) {
- return path;
- }
-
- if (path.endsWith("/") || suffix.startsWith("/")) {
- return path + suffix;
- } else {
- return path + "/" + suffix;
- }
- }
-
- /**
- * Copy a list of paths from src to dst
- * @param srcClient
- * @param srcRootPath
- * @param dstClient
- * @param dstRootPath
- * @param paths
- */
- private static void copy(ZkClient srcClient, String srcRootPath, ZkClient dstClient,
- String dstRootPath, List<String> paths) {
- BaseDataAccessor<Object> srcAccessor = new ZkBaseDataAccessor<Object>(srcClient);
- List<String> readPaths = new ArrayList<String>();
- for (String path : paths) {
- readPaths.add(concatenate(srcRootPath, path));
- }
- List<Stat> stats = new ArrayList<Stat>();
- List<Object> readData = srcAccessor.get(readPaths, stats, 0);
-
- List<String> writePaths = new ArrayList<String>();
- List<Object> writeData = new ArrayList<Object>();
- for (int i = 0; i < paths.size(); i++) {
- if (stats.get(i).getEphemeralOwner() != 0) {
- logger.warn("Skip copying ephemeral znode: " + readPaths.get(i));
- continue;
- }
-
- writePaths.add(concatenate(dstRootPath, paths.get(i)));
- writeData.add(readData.get(i));
- }
-
- if (writePaths.size() > 0) {
- BaseDataAccessor<Object> dstAccessor = new ZkBaseDataAccessor<Object>(dstClient);
- boolean[] success =
- dstAccessor.createChildren(writePaths, writeData, AccessOption.PERSISTENT);
- List<String> successPaths = new ArrayList<String>();
- List<String> failPaths = new ArrayList<String>();
- for (int i = 0; i < success.length; i++) {
- if (success[i]) {
- successPaths.add(writePaths.get(i));
- } else {
- failPaths.add(writePaths.get(i));
- }
- }
-
- // Print
- if (!successPaths.isEmpty()) {
- System.out.println("Copy " + successPaths);
- }
-
- if (!failPaths.isEmpty()) {
- System.out.println("Skip " + failPaths);
- }
- }
- }
-
- private static void zkCopy(ZkClient srcClient, String srcRootPath, ZkClient dstClient, String dstRootPath) {
- // Strip off tailing "/"
- if (!srcRootPath.equals("/") && srcRootPath.endsWith("/")) {
- srcRootPath = srcRootPath.substring(0, srcRootPath.length() - 1);
- }
-
- if (!dstRootPath.equals("/") && dstRootPath.endsWith("/")) {
- dstRootPath = dstRootPath.substring(0, dstRootPath.length() - 1);
- }
-
- // Validate paths
- PathUtils.validatePath(srcRootPath);
- PathUtils.validatePath(dstRootPath);
-
- if (srcRootPath.equals(dstRootPath)) {
- logger.info("srcPath == dstPath. Skip copying");
- return;
- }
-
- if (srcRootPath.startsWith(dstRootPath) || dstRootPath.startsWith(srcRootPath)) {
- throw new IllegalArgumentException(
- "srcPath/dstPath can't be prefix of dstPath/srcPath, was srcPath: " + srcRootPath
- + ", dstPath: " + dstRootPath);
- }
-
- // Recursive copy using BFS
- List<String> queue = new LinkedList<String>();
- String root = "";
- copy(srcClient, srcRootPath, dstClient, dstRootPath, Arrays.asList(root));
-
- queue.add(root);
- while (!queue.isEmpty()) {
- String path = queue.remove(0);
- String fromPath = concatenate(srcRootPath, path);
-
- List<String> children = srcClient.getChildren(fromPath);
- List<String> paths = new ArrayList<String>();
- if (children != null && children.size() > 0) {
- for (String child : children) {
- String childPath = concatenate(path, child);
- paths.add(childPath);
- }
- copy(srcClient, srcRootPath, dstClient, dstRootPath, paths);
- queue.addAll(paths);
- }
- }
- }
-
- public static void main(String[] args) throws Exception {
- CommandLineParser cliParser = new GnuParser();
- Options cliOptions = constructCmdLineOpt();
- CommandLine cmd = null;
-
- try {
- cmd = cliParser.parse(cliOptions, args);
- } catch (ParseException pe) {
- System.err.println("CommandLineClient: failed to parse command-line options: "
- + pe.toString());
- printUsage(cliOptions);
- System.exit(1);
- }
-
- URI srcUri = new URI(cmd.getOptionValue(src));
- URI dstUri = new URI(cmd.getOptionValue(dst));
-
- ZkCopyScheme srcScheme = ZkCopyScheme.valueOf(srcUri.getScheme());
- ZkCopyScheme dstScheme = ZkCopyScheme.valueOf(dstUri.getScheme());
-
- if (srcScheme == ZkCopyScheme.zk && dstScheme == ZkCopyScheme.zk) {
- String srcZkAddr = srcUri.getAuthority();
- String dstZkAddr = dstUri.getAuthority();
-
- ZkClient srcClient = null;
- ZkClient dstClient = null;
- try {
- if (srcZkAddr.equals(dstZkAddr)) {
- srcClient =
- dstClient =
- new ZkClient(srcZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT,
- ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
- } else {
- srcClient =
- new ZkClient(srcZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT,
- ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
- dstClient =
- new ZkClient(dstZkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT,
- ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ByteArraySerializer());
- }
- String srcPath = srcUri.getPath();
- String dstPath = dstUri.getPath();
- zkCopy(srcClient, srcPath, dstClient, dstPath);
- } finally {
- if (srcClient != null) {
- srcClient.close();
- }
- if (dstClient != null) {
- dstClient.close();
- }
- }
- } else {
- System.err.println("Unsupported scheme. srcScheme: " + srcScheme + ", dstScheme: " + dstScheme);
- System.exit(1);
- }
- }
-}