
alluxio.shaded.client.org.apache.zookeeper.ZooKeeperMain Maven / Gradle / Ivy
/**
* 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 alluxio.shaded.client.com.liance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.alluxio.shaded.client.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package alluxio.shaded.client.org.apache.zookeeper;
import java.alluxio.shaded.client.io.BufferedReader;
import java.alluxio.shaded.client.io.IOException;
import java.alluxio.shaded.client.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import alluxio.shaded.client.org.apache.yetus.audience.InterfaceAudience;
import alluxio.shaded.client.org.apache.zookeeper.cli.CliException;
import alluxio.shaded.client.org.apache.zookeeper.cli.CommandNotFoundException;
import alluxio.shaded.client.org.apache.zookeeper.cli.MalformedCommandException;
import alluxio.shaded.client.org.slf4j.Logger;
import alluxio.shaded.client.org.slf4j.LoggerFactory;
import alluxio.shaded.client.org.apache.zookeeper.ZooDefs.Ids;
import alluxio.shaded.client.org.apache.zookeeper.data.Stat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import alluxio.shaded.client.org.apache.zookeeper.cli.AddAuthCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.CliCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.CloseCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.CreateCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.DelQuotaCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.DeleteAllCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.DeleteCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.RemoveWatchesCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.GetAclCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.GetCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.GetConfigCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.ListQuotaCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.Ls2Command;
import alluxio.shaded.client.org.apache.zookeeper.cli.LsCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.ReconfigCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.SetAclCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.SetCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.SetQuotaCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.StatCommand;
import alluxio.shaded.client.org.apache.zookeeper.cli.SyncCommand;
import alluxio.shaded.client.org.apache.zookeeper.client.ZKClientConfig;
import alluxio.shaded.client.org.apache.zookeeper.admin.ZooKeeperAdmin;
/**
* The alluxio.shaded.client.com.and line client to ZooKeeper.
*
*/
@InterfaceAudience.Public
public class ZooKeeperMain {
private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperMain.class);
static final Map alluxio.shaded.client.com.andMap = new HashMap( );
static final Map alluxio.shaded.client.com.andMapCli =
new HashMap( );
protected MyCommandOptions cl = new MyCommandOptions();
protected HashMap history = new HashMap( );
protected int alluxio.shaded.client.com.andCount = 0;
protected boolean printWatches = true;
protected int exitCode = 0;
protected ZooKeeper zk;
protected String host = "";
public boolean getPrintWatches( ) {
return printWatches;
}
static {
alluxio.shaded.client.com.andMap.put("connect", "host:port");
alluxio.shaded.client.com.andMap.put("history","");
alluxio.shaded.client.com.andMap.put("redo","cmdno");
alluxio.shaded.client.com.andMap.put("printwatches", "on|off");
alluxio.shaded.client.com.andMap.put("quit", "");
new CloseCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new CreateCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new DeleteCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new DeleteAllCommand().addToMap(alluxio.shaded.client.com.andMapCli);
// Depricated: rmr
new DeleteAllCommand("rmr").addToMap(alluxio.shaded.client.com.andMapCli);
new SetCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new GetCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new LsCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new Ls2Command().addToMap(alluxio.shaded.client.com.andMapCli);
new GetAclCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new SetAclCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new StatCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new SyncCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new SetQuotaCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new ListQuotaCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new DelQuotaCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new AddAuthCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new ReconfigCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new GetConfigCommand().addToMap(alluxio.shaded.client.com.andMapCli);
new RemoveWatchesCommand().addToMap(alluxio.shaded.client.com.andMapCli);
// add all to alluxio.shaded.client.com.andMap
for (Entry entry : alluxio.shaded.client.com.andMapCli.entrySet()) {
alluxio.shaded.client.com.andMap.put(entry.getKey(), entry.getValue().getOptionStr());
}
}
static void usage() {
System.err.println("ZooKeeper -server host:port cmd args");
List cmdList = new ArrayList(alluxio.shaded.client.com.andMap.keySet());
Collections.sort(cmdList);
for (String cmd : cmdList) {
System.err.println("\t"+cmd+ " " + alluxio.shaded.client.com.andMap.get(cmd));
}
}
private class MyWatcher implements Watcher {
public void process(WatchedEvent event) {
if (getPrintWatches()) {
ZooKeeperMain.printMessage("WATCHER::");
ZooKeeperMain.printMessage(event.toString());
}
}
}
/**
* A storage class for both alluxio.shaded.client.com.and line options and shell alluxio.shaded.client.com.ands.
*
*/
static class MyCommandOptions {
private Map options = new HashMap();
private List cmdArgs = null;
private String alluxio.shaded.client.com.and = null;
public static final Pattern ARGS_PATTERN = Pattern.alluxio.shaded.client.com.ile("\\s*([^\"\']\\S*|\"[^\"]*\"|'[^']*')\\s*");
public static final Pattern QUOTED_PATTERN = Pattern.alluxio.shaded.client.com.ile("^([\'\"])(.*)(\\1)$");
public MyCommandOptions() {
options.put("server", "localhost:2181");
options.put("timeout", "30000");
}
public String getOption(String opt) {
return options.get(opt);
}
public String getCommand( ) {
return alluxio.shaded.client.com.and;
}
public String getCmdArgument( int index ) {
return cmdArgs.get(index);
}
public int getNumArguments( ) {
return cmdArgs.size();
}
public String[] getArgArray() {
return cmdArgs.toArray(new String[0]);
}
/**
* Parses a alluxio.shaded.client.com.and line that may contain one or more flags
* before an optional alluxio.shaded.client.com.and string
* @param args alluxio.shaded.client.com.and line arguments
* @return true if parsing succeeded, false otherwise.
*/
public boolean parseOptions(String[] args) {
List argList = Arrays.asList(args);
Iterator it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e){
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
alluxio.shaded.client.com.and = opt;
cmdArgs = new ArrayList( );
cmdArgs.add( alluxio.shaded.client.com.and );
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}
/**
* Breaks a string into alluxio.shaded.client.com.and + arguments.
* @param cmdstring string of form "cmd arg1 arg2..etc"
* @return true if parsing succeeded.
*/
public boolean parseCommand( String cmdstring ) {
Matcher matcher = ARGS_PATTERN.matcher(cmdstring);
List args = new LinkedList();
while (matcher.find()) {
String value = matcher.group(1);
if (QUOTED_PATTERN.matcher(value).matches()) {
// Strip off the surrounding quotes
value = value.substring(1, value.length() - 1);
}
args.add(value);
}
if (args.isEmpty()){
return false;
}
alluxio.shaded.client.com.and = args.get(0);
cmdArgs = args;
return true;
}
}
/**
* Makes a list of possible alluxio.shaded.client.com.letions, either for alluxio.shaded.client.com.ands
* or for zk nodes if the token to alluxio.shaded.client.com.lete begins with /
*
*/
protected void addToHistory(int i,String cmd) {
history.put(i, cmd);
}
public static List getCommands() {
List cmdList = new ArrayList(alluxio.shaded.client.com.andMap.keySet());
Collections.sort(cmdList);
return cmdList;
}
protected String getPrompt() {
return "[zk: " + host + "("+zk.getState()+")" + " " + alluxio.shaded.client.com.andCount + "] ";
}
public static void printMessage(String msg) {
System.out.println("\n"+msg);
}
protected void connectToZK(String newHost) throws InterruptedException, IOException {
if (zk != null && zk.getState().isAlive()) {
zk.close();
}
host = newHost;
boolean readOnly = cl.getOption("readonly") != null;
if (cl.getOption("secure") != null) {
System.setProperty(ZKClientConfig.SECURE_CLIENT, "true");
System.out.println("Secure connection is enabled");
}
zk = new ZooKeeperAdmin(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly);
}
public static void main(String args[]) throws CliException, IOException, InterruptedException
{
ZooKeeperMain main = new ZooKeeperMain(args);
main.run();
}
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
System.out.println("Connecting to " + cl.getOption("server"));
connectToZK(cl.getOption("server"));
}
public ZooKeeperMain(ZooKeeper zk) {
this.zk = zk;
}
void run() throws CliException, IOException, InterruptedException {
if (cl.getCommand() == null) {
System.out.println("Welcome to ZooKeeper!");
boolean jlinemissing = false;
// only use jline if it's in the classpath
try {
Class> consoleC = Class.forName("jline.console.ConsoleReader");
Class> alluxio.shaded.client.com.letorC =
Class.forName("alluxio.shaded.client.org.apache.zookeeper.JLineZNodeCompleter");
System.out.println("JLine support is enabled");
Object console =
consoleC.getConstructor().newInstance();
Object alluxio.shaded.client.com.letor =
alluxio.shaded.client.com.letorC.getConstructor(ZooKeeper.class).newInstance(zk);
Method addCompletor = consoleC.getMethod("addCompleter",
Class.forName("jline.console.alluxio.shaded.client.com.leter.Completer"));
addCompletor.invoke(console, alluxio.shaded.client.com.letor);
String line;
Method readLine = consoleC.getMethod("readLine", String.class);
while ((line = (String)readLine.invoke(console, getPrompt())) != null) {
executeLine(line);
}
} catch (ClassNotFoundException e) {
LOG.debug("Unable to start jline", e);
jlinemissing = true;
} catch (NoSuchMethodException e) {
LOG.debug("Unable to start jline", e);
jlinemissing = true;
} catch (InvocationTargetException e) {
LOG.debug("Unable to start jline", e);
jlinemissing = true;
} catch (IllegalAccessException e) {
LOG.debug("Unable to start jline", e);
jlinemissing = true;
} catch (InstantiationException e) {
LOG.debug("Unable to start jline", e);
jlinemissing = true;
}
if (jlinemissing) {
System.out.println("JLine support is disabled");
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
executeLine(line);
}
}
} else {
// Command line args non-null. Run what was passed.
processCmd(cl);
}
System.exit(exitCode);
}
public void executeLine(String line) throws CliException, InterruptedException, IOException {
if (!line.equals("")) {
cl.parseCommand(line);
addToHistory(alluxio.shaded.client.com.andCount,line);
processCmd(cl);
alluxio.shaded.client.com.andCount++;
}
}
/**
* trim the quota tree to recover unwanted tree elements
* in the quota's tree
* @param zk the zookeeper client
* @param path the path to start from and go up and see if their
* is any unwanted parent in the path.
* @return true if sucessful
* @throws KeeperException
* @throws IOException
* @throws InterruptedException
*/
private static boolean trimProcQuotas(ZooKeeper zk, String path)
throws KeeperException, IOException, InterruptedException
{
if (Quotas.quotaZookeeper.equals(path)) {
return true;
}
List children = zk.getChildren(path, false);
if (children.size() == 0) {
zk.delete(path, -1);
String parent = path.substring(0, path.lastIndexOf('/'));
return trimProcQuotas(zk, parent);
} else {
return true;
}
}
/**
* this method deletes quota for a node.
* @param zk the zookeeper client
* @param path the path to delete quota for
* @param bytes true if number of bytes needs to
* be unset
* @param numNodes true if number of nodes needs
* to be unset
* @return true if quota deletion is successful
* @throws KeeperException
* @throws IOException
* @throws InterruptedException
*/
public static boolean delQuota(ZooKeeper zk, String path,
boolean bytes, boolean numNodes)
throws KeeperException, IOException, InterruptedException
{
String parentPath = Quotas.quotaZookeeper + path;
String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
if (zk.exists(quotaPath, false) == null) {
System.out.println("Quota does not exist for " + path);
return true;
}
byte[] data = null;
try {
data = zk.getData(quotaPath, false, new Stat());
} catch(KeeperException.NoNodeException ne) {
System.err.println("quota does not exist for " + path);
return true;
}
StatsTrack strack = new StatsTrack(new String(data));
if (bytes && !numNodes) {
strack.setBytes(-1L);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (!bytes && numNodes) {
strack.setCount(-1);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (bytes && numNodes) {
// delete till you can find a node with more than
// one child
List children = zk.getChildren(parentPath, false);
/// delete the direct children first
for (String child: children) {
zk.delete(parentPath + "/" + child, -1);
}
// cut the tree till their is more than one child
trimProcQuotas(zk, parentPath);
}
return true;
}
private static void checkIfParentQuota(ZooKeeper zk, String path)
throws InterruptedException, KeeperException
{
final String[] splits = path.split("/");
String quotaPath = Quotas.quotaZookeeper;
for (String str: splits) {
if (str.length() == 0) {
// this should only be for the beginning of the path
// i.e. "/..." - split(path)[0] is empty string before first '/'
continue;
}
quotaPath += "/" + str;
List children = null;
try {
children = zk.getChildren(quotaPath, false);
} catch(KeeperException.NoNodeException ne) {
LOG.debug("child removed during quota check", ne);
return;
}
if (children.size() == 0) {
return;
}
for (String child: children) {
if (Quotas.limitNode.equals(child)) {
throw new IllegalArgumentException(path + " has a parent "
+ quotaPath + " which has a quota");
}
}
}
}
/**
* this method creates a quota node for the path
* @param zk the ZooKeeper client
* @param path the path for which quota needs to be created
* @param bytes the limit of bytes on this path
* @param numNodes the limit of number of nodes on this path
* @return true if its successful and false if not.
*/
public static boolean createQuota(ZooKeeper zk, String path,
long bytes, int numNodes)
throws KeeperException, IOException, InterruptedException
{
// check if the path exists. We cannot create
// quota for a path that already exists in zookeeper
// for now.
Stat initStat = zk.exists(path, false);
if (initStat == null) {
throw new IllegalArgumentException(path + " does not exist.");
}
// now check if their is already existing
// parent or child that has quota
String quotaPath = Quotas.quotaZookeeper;
// check for more than 2 children --
// if zookeeper_stats and zookeeper_qutoas
// are not the children then this path
// is an ancestor of some path that
// already has quota
String realPath = Quotas.quotaZookeeper + path;
try {
List children = zk.getChildren(realPath, false);
for (String child: children) {
if (!child.startsWith("zookeeper_")) {
throw new IllegalArgumentException(path + " has child " +
child + " which has a quota");
}
}
} catch(KeeperException.NoNodeException ne) {
// this is fine
}
//check for any parent that has been quota
checkIfParentQuota(zk, path);
// this is valid node for quota
// start creating all the parents
if (zk.exists(quotaPath, false) == null) {
try {
zk.create(Quotas.procZookeeper, null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create(Quotas.quotaZookeeper, null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
} catch(KeeperException.NodeExistsException ne) {
// do nothing
}
}
// now create the direct children
// and the stat and quota nodes
String[] splits = path.split("/");
StringBuilder sb = new StringBuilder();
sb.append(quotaPath);
for (int i=1; i= 2) {
Integer i = Integer.decode(args[1]);
if (alluxio.shaded.client.com.andCount <= i || i < 0) { // don't allow redoing this redo
throw new MalformedCommandException("Command index out of range");
}
cl.parseCommand(history.get(i));
if (cl.getCommand().equals("redo")) {
throw new MalformedCommandException("No redoing redos");
}
history.put(alluxio.shaded.client.com.andCount, history.get(i));
processCmd(cl);
} else if (cmd.equals("history")) {
for (int i = alluxio.shaded.client.com.andCount - 10; i <= alluxio.shaded.client.com.andCount; ++i) {
if (i < 0) continue;
System.out.println(i + " - " + history.get(i));
}
} else if (cmd.equals("printwatches")) {
if (args.length == 1) {
System.out.println("printwatches is " + (printWatches ? "on" : "off"));
} else {
printWatches = args[1].equals("on");
}
} else if (cmd.equals("connect")) {
if (args.length >= 2) {
connectToZK(args[1]);
} else {
connectToZK(host);
}
}
// Below alluxio.shaded.client.com.ands all need a live connection
if (zk == null || !zk.getState().isAlive()) {
System.out.println("Not connected");
return false;
}
// execute from alluxio.shaded.client.com.andMap
CliCommand cliCmd = alluxio.shaded.client.com.andMapCli.get(cmd);
if(cliCmd != null) {
cliCmd.setZk(zk);
watch = cliCmd.parse(args).exec();
} else if (!alluxio.shaded.client.com.andMap.containsKey(cmd)) {
usage();
}
return watch;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy