This is an automated email from the ASF dual-hosted git repository.

zhaijia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 54baa27  ISSUE #893: more options for UpdateCookieCmd
54baa27 is described below

commit 54baa27426a9cb5e697f6b32333070e7f5ef02fb
Author: Charan Reddy Guttapalem <[email protected]>
AuthorDate: Mon Dec 25 21:08:17 2017 +0800

    ISSUE #893: more options for UpdateCookieCmd
    
    Descriptions of the changes in this PR:
    
    - add ListCookie Options to UpdateCookie command
    - add DeleteCookie Option to UpdateCookie command
    - move expandstorage command to UpdateCookie command as option,
    since expandstorage is just a cookie update operation
    - in listfilesondisc command use canonical path (full path)
    
    Author: Charan Reddy Guttapalem <[email protected]>
    Author: cguttapalem <[email protected]>
    
    Reviewers: Jia Zhai <None>, Sijie Guo <[email protected]>
    
    This closes #894 from reddycharan/updatecookiecmd, closes #893
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  | 233 +++++++++++++++------
 .../bookkeeper/discover/RegistrationManager.java   |   1 -
 .../bookkeeper/discover/ZKRegistrationManager.java |   8 +-
 3 files changed, 171 insertions(+), 71 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index fc707d1..d7728d6 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -50,6 +50,7 @@ import java.util.Formatter;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -87,6 +88,7 @@ import org.apache.bookkeeper.net.BookieSocketAddress;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback;
 import org.apache.bookkeeper.replication.AuditorElector;
 import org.apache.bookkeeper.stats.NullStatsLogger;
+import org.apache.bookkeeper.util.BookKeeperConstants;
 import org.apache.bookkeeper.util.DiskChecker;
 import org.apache.bookkeeper.util.EntryFormatter;
 import org.apache.bookkeeper.util.IOUtils;
@@ -100,6 +102,8 @@ import org.apache.commons.cli.BasicParser;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.MissingArgumentException;
+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.commons.configuration.CompositeConfiguration;
@@ -107,6 +111,7 @@ import 
org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.mutable.MutableBoolean;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.ZooKeeper;
 import org.slf4j.Logger;
@@ -141,7 +146,6 @@ public class BookieShell implements Tool {
     static final String CMD_LISTBOOKIES = "listbookies";
     static final String CMD_LISTFILESONDISC = "listfilesondisc";
     static final String CMD_UPDATECOOKIE = "updatecookie";
-    static final String CMD_EXPANDSTORAGE = "expandstorage";
     static final String CMD_UPDATELEDGER = "updateledgers";
     static final String CMD_DELETELEDGER = "deleteledger";
     static final String CMD_BOOKIEINFO = "bookieinfo";
@@ -1327,7 +1331,7 @@ public class BookieShell implements Tool {
                 List<File> journalFiles = listFilesAndSort(journalDirs, "txn");
                 System.out.println("--------- Printing the list of Journal 
Files ---------");
                 for (File journalFile : journalFiles) {
-                    System.out.println(journalFile.getName());
+                    System.out.println(journalFile.getCanonicalPath());
                 }
                 System.out.println();
             }
@@ -1336,7 +1340,7 @@ public class BookieShell implements Tool {
                 List<File> ledgerFiles = listFilesAndSort(ledgerDirs, "log");
                 System.out.println("--------- Printing the list of 
EntryLog/Ledger Files ---------");
                 for (File ledgerFile : ledgerFiles) {
-                    System.out.println(ledgerFile.getName());
+                    System.out.println(ledgerFile.getCanonicalPath());
                 }
                 System.out.println();
             }
@@ -1345,7 +1349,7 @@ public class BookieShell implements Tool {
                 List<File> indexFiles = listFilesAndSort(indexDirs, "idx");
                 System.out.println("--------- Printing the list of Index Files 
---------");
                 for (File indexFile : indexFiles) {
-                    System.out.println(indexFile.getName());
+                    System.out.println(indexFile.getCanonicalPath());
                 }
             }
             return 0;
@@ -1598,10 +1602,23 @@ public class BookieShell implements Tool {
      */
     class UpdateCookieCmd extends MyCommand {
         Options opts = new Options();
+        private static final String BOOKIEID = "bookieId";
+        private static final String EXPANDSTORAGE = "expandstorage";
+        private static final String LIST = "list";
+        private static final String DELETE = "delete";
+        private static final String HOSTNAME = "hostname";
+        private static final String IP = "ip";
+        private static final String FORCE = "force";
 
         UpdateCookieCmd() {
             super(CMD_UPDATECOOKIE);
-            opts.addOption("b", "bookieId", true, "Bookie Id");
+            opts.addOption("b", BOOKIEID, true, "Bookie Id");
+            opts.addOption("e", EXPANDSTORAGE, false, "Expand Storage");
+            opts.addOption("l", LIST, false, "List paths of all the cookies 
present locally and on zookkeeper");
+            @SuppressWarnings("static-access")
+            Option deleteOption = 
OptionBuilder.withLongOpt(DELETE).hasOptionalArgs(1)
+                    .withDescription("Delete cookie both locally and in 
ZooKeeper").create("d");
+            opts.addOption(deleteOption);
         }
 
         @Override
@@ -1611,36 +1628,69 @@ public class BookieShell implements Tool {
 
         @Override
         String getDescription() {
-            return "Update bookie id in cookie.";
+            return "Command to update cookie"
+                    + "bookieId - Update bookie id in cookie\n"
+                    + "expandstorage - Add new empty ledger/index directories."
+                    + " Update the directories info in the conf file before 
running the command\n"
+                    + "list - list the local cookie files path and ZK 
cookiePath "
+                    + "delete - Delete cookies locally and in zookeeper";
         }
 
         @Override
         String getUsage() {
-            return "updatecookie -bookieId <hostname|ip>";
+            return "updatecookie [-bookieId <hostname|ip>] [-expandstorage] 
[-list] [-delete <force>]";
         }
 
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
-            final String bookieId = cmdLine.getOptionValue("bookieId");
-            if (StringUtils.isBlank(bookieId)) {
-                LOG.error("Invalid argument list!");
+            int retValue = -1;
+            Option[] options = cmdLine.getOptions();
+            if (options.length != 1) {
+                LOG.error("Invalid command!");
                 this.printUsage();
                 return -1;
             }
-            if (!StringUtils.equals(bookieId, "hostname") && 
!StringUtils.equals(bookieId, "ip")) {
-                LOG.error("Invalid option value:" + bookieId);
+            Option thisCommandOption = options[0];
+            if (thisCommandOption.getLongOpt().equals(BOOKIEID)) {
+                final String bookieId = cmdLine.getOptionValue(BOOKIEID);
+                if (StringUtils.isBlank(bookieId)) {
+                    LOG.error("Invalid argument list!");
+                    this.printUsage();
+                    return -1;
+                }
+                if (!StringUtils.equals(bookieId, HOSTNAME) && 
!StringUtils.equals(bookieId, IP)) {
+                    LOG.error("Invalid option value:" + bookieId);
+                    this.printUsage();
+                    return -1;
+                }
+                boolean useHostName = getOptionalValue(bookieId, HOSTNAME);
+                if (!bkConf.getUseHostNameAsBookieID() && useHostName) {
+                    LOG.error(
+                            "Expects config useHostNameAsBookieID=true as the 
option value passed is 'hostname'");
+                    return -1;
+                } else if (bkConf.getUseHostNameAsBookieID() && !useHostName) {
+                    LOG.error("Expects configuration 
useHostNameAsBookieID=false as the option value passed is 'ip'");
+                    return -1;
+                }
+                retValue = updateBookieIdInCookie(bookieId, useHostName);
+            } else if (thisCommandOption.getLongOpt().equals(EXPANDSTORAGE)) {
+                bkConf.setAllowStorageExpansion(true);
+                return expandStorage();
+            } else if (thisCommandOption.getLongOpt().equals(LIST)) {
+                return listOrDeleteCookies(false, false);
+            } else if (thisCommandOption.getLongOpt().equals(DELETE)) {
+                boolean force = false;
+                String optionValue = thisCommandOption.getValue();
+                if (!StringUtils.isEmpty(optionValue) && 
optionValue.equals(FORCE)) {
+                    force = true;
+                }
+                return listOrDeleteCookies(true, force);
+            } else {
+                LOG.error("Invalid command!");
                 this.printUsage();
                 return -1;
             }
-            boolean useHostName = getOptionalValue(bookieId, "hostname");
-            if (!bkConf.getUseHostNameAsBookieID() && useHostName) {
-                LOG.error("Expects configuration useHostNameAsBookieID=true as 
the option value passed is 'hostname'");
-                return -1;
-            } else if (bkConf.getUseHostNameAsBookieID() && !useHostName) {
-                LOG.error("Expects configuration useHostNameAsBookieID=false 
as the option value passed is 'ip'");
-                return -1;
-            }
-            return updateBookieIdInCookie(bookieId, useHostName);
+            return retValue;
         }
 
         private int updateBookieIdInCookie(final String bookieId, final 
boolean useHostname) throws BookieException,
@@ -1720,6 +1770,30 @@ public class BookieShell implements Tool {
             return 0;
         }
 
+        private int expandStorage() throws InterruptedException {
+            try (RegistrationManager rm = new ZKRegistrationManager()) {
+                rm.initialize(bkConf, () -> { }, NullStatsLogger.INSTANCE);
+
+                List<File> allLedgerDirs = Lists.newArrayList();
+                allLedgerDirs.addAll(Arrays.asList(ledgerDirectories));
+                if (indexDirectories != ledgerDirectories) {
+                    allLedgerDirs.addAll(Arrays.asList(indexDirectories));
+                }
+
+                try {
+                    Bookie.checkEnvironmentWithStorageExpansion(
+                            bkConf, rm, Arrays.asList(journalDirectories), 
allLedgerDirs);
+                } catch (BookieException e) {
+                    LOG.error("Exception while updating cookie for storage 
expansion", e);
+                    return -1;
+                }
+            } catch (BookieException e) {
+                LOG.error("Exception while establishing RegistrationManager 
connection.", e);
+                return -1;
+            }
+            return 0;
+        }
+
         private boolean verifyCookie(Cookie oldCookie, File dir) throws 
IOException {
             try {
                 Cookie cookie = Cookie.readFromDirectory(dir);
@@ -1729,63 +1803,85 @@ public class BookieShell implements Tool {
             }
             return true;
         }
-    }
-
-    /**
-     * Expand the storage directories owned by a bookie.
-     */
-    class ExpandStorageCmd extends MyCommand {
-        Options opts = new Options();
-
-        ExpandStorageCmd() {
-            super(CMD_EXPANDSTORAGE);
-        }
 
-        @Override
-        Options getOptions() {
-            return opts;
-        }
+        private int listOrDeleteCookies(boolean delete, boolean force)
+                throws IOException, BookieException {
+            BookieSocketAddress bookieAddress = 
Bookie.getBookieAddress(bkConf);
+            File[] journalDirs = bkConf.getJournalDirs();
+            File[] ledgerDirs = bkConf.getLedgerDirs();
+            File[] indexDirs = bkConf.getIndexDirs();
+            File[] allDirs = ArrayUtils.addAll(journalDirs, ledgerDirs);
+            if (indexDirs != null) {
+                allDirs = ArrayUtils.addAll(allDirs, indexDirs);
+            }
 
-        @Override
-        String getDescription() {
-            return "Add new empty ledger/index directories. Update the 
directories"
-                    + "info in the conf file before running the command.";
-        }
+            File[] allCurDirs = Bookie.getCurrentDirectories(allDirs);
+            List<File> allVersionFiles = new LinkedList<File>();
+            File versionFile;
+            for (File curDir : allCurDirs) {
+                versionFile = new File(curDir, 
BookKeeperConstants.VERSION_FILENAME);
+                if (versionFile.exists()) {
+                    allVersionFiles.add(versionFile);
+                }
+            }
 
-        @Override
-        String getUsage() {
-            return "expandstorage";
-        }
+            if (!allVersionFiles.isEmpty()) {
+                if (delete) {
+                    boolean confirm = force;
+                    if (!confirm) {
+                        confirm = IOUtils.confirmPrompt("Are you sure you want 
to delete Cookies locally?");
+                    }
+                    if (confirm) {
+                        for (File verFile : allVersionFiles) {
+                            if (!verFile.delete()) {
+                                LOG.error(
+                                        "Failed to delete Local cookie file 
{}. So aborting deletecookie of Bookie: {}",
+                                        verFile, bookieAddress);
+                                return -1;
+                            }
+                        }
+                        LOG.info("Deleted Local Cookies of Bookie: {}", 
bookieAddress);
+                    } else {
+                        LOG.info("Skipping deleting local Cookies of Bookie: 
{}", bookieAddress);
+                    }
+                } else {
+                    LOG.info("Listing local Cookie Files of Bookie: {}", 
bookieAddress);
+                    for (File verFile : allVersionFiles) {
+                        LOG.info(verFile.getCanonicalPath());
+                    }
+                }
+            } else {
+                LOG.info("No local cookies for Bookie: {}", bookieAddress);
+            }
 
-        @Override
-        int runCmd(CommandLine cmdLine) {
-            ServerConfiguration conf = new ServerConfiguration(bkConf);
-            try (RegistrationManager rm = new ZKRegistrationManager()) {
+            try (ZKRegistrationManager rm = new ZKRegistrationManager()) {
+                Versioned<Cookie> cookie = null;
                 try {
-                    rm.initialize(bkConf, () -> {
-                    }, NullStatsLogger.INSTANCE);
-                } catch (BookieException e) {
-                    LOG.error("Exception while establishing zookeeper 
connection.", e);
-                    return -1;
+                    rm.initialize(bkConf, () -> { }, NullStatsLogger.INSTANCE);
+                    cookie = Cookie.readFromRegistrationManager(rm, 
bookieAddress);
+                } catch (CookieNotFoundException nne) {
+                    LOG.info("No cookie for {} in ZooKeeper", bookieAddress);
+                    return 0;
                 }
 
-                List<File> allLedgerDirs = Lists.newArrayList();
-                allLedgerDirs.addAll(Arrays.asList(ledgerDirectories));
-                if (indexDirectories != ledgerDirectories) {
-                    allLedgerDirs.addAll(Arrays.asList(indexDirectories));
-                }
+                if (delete) {
+                    boolean confirm = force;
+                    if (!confirm) {
+                        confirm = IOUtils.confirmPrompt("Are you sure you want 
to delete Cookies from Zookeeper?");
+                    }
 
-                try {
-                    conf.setAllowStorageExpansion(true);
-                    Bookie.checkEnvironmentWithStorageExpansion(conf, rm,
-                            Lists.newArrayList(journalDirectories), 
allLedgerDirs);
-                } catch (BookieException e) {
-                    LOG.error(
-                            "Exception while updating cookie for storage 
expansion", e);
-                    return -1;
+                    if (confirm) {
+                        cookie.getValue().deleteFromRegistrationManager(rm, 
bkConf, cookie.getVersion());
+                        LOG.info("Deleted Cookie from Zookeeper for Bookie: 
{}", bookieAddress);
+                    } else {
+                        LOG.info("Skipping deleting cookie from ZooKeeper for 
Bookie: {}", bookieAddress);
+                    }
+                } else {
+                    LOG.info("{} bookie's Cookie path in Zookeeper: {} ", 
bookieAddress,
+                            rm.getCookiePath(bookieAddress.toString()));
                 }
-                return 0;
             }
+            return 0;
         }
     }
 
@@ -2376,7 +2472,6 @@ public class BookieShell implements Tool {
         commands.put(CMD_LISTBOOKIES, new ListBookiesCmd());
         commands.put(CMD_LISTFILESONDISC, new ListDiskFilesCmd());
         commands.put(CMD_UPDATECOOKIE, new UpdateCookieCmd());
-        commands.put(CMD_EXPANDSTORAGE, new ExpandStorageCmd());
         commands.put(CMD_UPDATELEDGER, new UpdateLedgerCmd());
         commands.put(CMD_DELETELEDGER, new DeleteLedgerCmd());
         commands.put(CMD_BOOKIEINFO, new BookieInfoCmd());
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/RegistrationManager.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/RegistrationManager.java
index 2b274d7..23b0139 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/RegistrationManager.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/RegistrationManager.java
@@ -104,5 +104,4 @@ public interface RegistrationManager extends AutoCloseable {
      * @throws BookieException when fail to remove cookie
      */
     void removeCookie(String bookieId, Version version) throws BookieException;
-
 }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
index dbe924d..172b696 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
@@ -165,7 +165,13 @@ public class ZKRegistrationManager implements 
RegistrationManager {
         }
     }
 
-    private String getCookiePath(String bookieId) {
+    /**
+     * Returns the CookiePath of the bookie in the ZooKeeper.
+     *
+     * @param bookieId bookie id
+     * @return
+     */
+    public String getCookiePath(String bookieId) {
         return this.cookiePath + "/" + bookieId;
     }
 

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to