You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@distributedlog.apache.org by si...@apache.org on 2016/12/28 01:05:21 UTC

[13/20] incubator-distributedlog git commit: DL-109: Add a tool to find the stream containing the given ledger

DL-109: Add a tool to find the stream containing the given ledger


Project: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/commit/3dd39c18
Tree: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/tree/3dd39c18
Diff: http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/diff/3dd39c18

Branch: refs/heads/master
Commit: 3dd39c1857a219fb452a895ef2d34efd292657c5
Parents: ac0b7ec
Author: Sijie Guo <si...@twitter.com>
Authored: Thu Oct 27 10:18:52 2016 -0700
Committer: Sijie Guo <si...@twitter.com>
Committed: Tue Dec 27 16:49:28 2016 -0800

----------------------------------------------------------------------
 .../tools/DistributedLogTool.java               | 41 ++++++++++++++++++++
 1 file changed, 41 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-distributedlog/blob/3dd39c18/distributedlog-core/src/main/java/com/twitter/distributedlog/tools/DistributedLogTool.java
----------------------------------------------------------------------
diff --git a/distributedlog-core/src/main/java/com/twitter/distributedlog/tools/DistributedLogTool.java b/distributedlog-core/src/main/java/com/twitter/distributedlog/tools/DistributedLogTool.java
index bcb7853..e710337 100644
--- a/distributedlog-core/src/main/java/com/twitter/distributedlog/tools/DistributedLogTool.java
+++ b/distributedlog-core/src/main/java/com/twitter/distributedlog/tools/DistributedLogTool.java
@@ -257,6 +257,10 @@ public class DistributedLogTool extends Tool {
             return this.factory;
         }
 
+        protected DistributedLogNamespace getNamespace() throws IOException {
+            return getFactory().getNamespace();
+        }
+
         protected LogSegmentMetadataStore getLogSegmentMetadataStore() throws IOException {
             DistributedLogNamespace namespace = getFactory().getNamespace();
             assert(namespace instanceof BKDistributedLogNamespace);
@@ -2048,6 +2052,42 @@ public class DistributedLogTool extends Tool {
         }
     }
 
+    protected static class FindLedgerCommand extends PerLedgerCommand {
+
+        FindLedgerCommand() {
+            super("findledger", "find the stream for a given ledger");
+        }
+
+        @Override
+        protected int runCmd() throws Exception {
+            Iterator<String> logs = getNamespace().getLogs();
+            while (logs.hasNext()) {
+                String logName = logs.next();
+                if (processLog(logName)) {
+                    System.out.println("Found ledger " + getLedgerID() + " at log stream '" + logName + "'");
+                }
+            }
+            return 0;
+        }
+
+        boolean processLog(String logName) throws Exception {
+            DistributedLogManager dlm = getNamespace().openLog(logName);
+            try {
+                List<LogSegmentMetadata> segments = dlm.getLogSegments();
+                for (LogSegmentMetadata segment : segments) {
+                    if (getLedgerID() == segment.getLedgerId()) {
+                        System.out.println("Found ledger " + getLedgerID() + " at log segment "
+                                + segment + " for stream '" + logName + "'");
+                        return true;
+                    }
+                }
+                return false;
+            } finally {
+                dlm.close();
+            }
+        }
+    }
+
     protected static class ReadLastConfirmedCommand extends PerLedgerCommand {
 
         ReadLastConfirmedCommand() {
@@ -2643,6 +2683,7 @@ public class DistributedLogTool extends Tool {
         addCommand(new DeleteAllocatorPoolCommand());
         addCommand(new DeleteLedgersCommand());
         addCommand(new DumpCommand());
+        addCommand(new FindLedgerCommand());
         addCommand(new InspectCommand());
         addCommand(new InspectStreamCommand());
         addCommand(new ListCommand());