You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pulsar.apache.org by rdhabalia <gi...@git.apache.org> on 2017/06/23 21:12:38 UTC

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

GitHub user rdhabalia opened a pull request:

    https://github.com/apache/incubator-pulsar/pull/526

    Add managed-ledger admin tool to access ml binary information

    ### Motivation
    
    After merging #394, broker will start storing managed-ledger data into binary format which can't be access by `bin/pulsar zookeeper-shell` anymore. So, incase of any data-corruption (eg. any human-error while accessing znode), we need a quick way to build a tool/script to access ml-data. therefore, adding python-admin tool which provides API and command-line tool access to access ml data.
    
    ### Modifications
    
    Added a tool which provides api to 
    1. get managed-ledger info
    2. get managed-cursor info
    3. update markDelete of managed-cursor
    4. update ledger-list of ml
    
    ### Result
    
    No functional change. 


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/rdhabalia/pulsar python-tool

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-pulsar/pull/526.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #526
    
----
commit 705da1e29112da8723a85174ef33678b46bc8732
Author: rdhabalia <rd...@apache.org>
Date:   2017-06-23T20:47:52Z

    Add managed-ledger admin tool to access ml binary information

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat closed the pull request at:

    https://github.com/apache/incubator-pulsar/pull/526


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar issue #526: Add managed-ledger admin tool to access ml bina...

Posted by rdhabalia <gi...@git.apache.org>.
Github user rdhabalia commented on the issue:

    https://github.com/apache/incubator-pulsar/pull/526
  
    We don't have doc for managed-ledger protobuf compilation instruction. If we think it will be helpful then I will create a separate PR to add ML-protobuf location, compilation and will add python protobuf generation instruction as well.??


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123855309
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    +
    +from mltool import MLDataFormats_pb2
    +
    +'''
    +This util provides api to access managed-ledger data if it's ONLY stored in binary format in to zookeeper.
    +It also provides command line tool-access to execute these commands.
    +
    +Run: run from parent directory
    +python -m mltool.ManagedLedgerAdminTool
    +'''
    +managedLedgerPath = "/managed-ledgers/"
    +printMlCommand = "print-ml"
    +deleteMlLedgerIds = "delete-ml-ledgers"
    +printCursorsCommands = "print-cursor"
    +updateMakDeleteCursor = "update-mark-delete-cursor"
    +
    +'''
    +Returns managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +''' 
    +def getManagedLedgerInfo(zk, mlPath):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        return mlInfo
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get data for {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Delete specific ledgerIds from the managed-ledger info and updates into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +'''  
    +def deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIds):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        ledgerInfoList = mlInfo.ledgerInfo
    +        
    +        for ledgerInfo in ledgerInfoList:
    +            print ledgerInfo.ledgerId
    +            if ledgerInfo.ledgerId in deletLedgerIds:
    +                ledgerInfoList.remove(ledgerInfo)
    +                updatedMlInfo = mlInfo.SerializeToString();
    +                zk.set(mlPath, updatedMlInfo, -1)
    +                print 'Updated {} with value\n{}'.format(mlPath, str(mlInfo))
    +        
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to delete ledgerIds for {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Returns managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +'''
    +def getManagedCursorInfo(zk, mlPath):
    +    try:
    +        cursors = zk.get_children(mlPath)
    +        cursorList = {}
    +        for cursor in cursors:
    +            cursorData = zk.get(mlPath + "/" + cursor)[0]
    +            cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +            cursorInfo.ParseFromString(cursorData)
    +            cursorList[cursor] = cursorInfo
    +        return cursorList
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get ml-cursor {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Update mark-delete position of the given managed-cursor into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +''' 
    +def updateCursorMarkDelete(zk, cursorPath, markDeleteLedgerId, markDeleteEntryId):
    +    try:
    +        cursorData = zk.get(cursorPath)[0]
    +        cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +        cursorInfo.ParseFromString(cursorData)
    +        cursorInfo.markDeleteLedgerId = markDeleteLedgerId
    +        cursorInfo.markDeleteEntryId = markDeleteEntryId
    +        sData = cursorInfo.SerializeToString()
    +        zk.set(cursorPath, sData, -1)
    +        print 'Updated {} with value \n{}'.format(cursorPath, cursorInfo)
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to update ml-cursor {} due to {}'.format(cursorPath, repr(e))
    +
    +
    +
    +'''
    +print managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +    
    +eg: 
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command print-ml
    +''' 
    +def printManagedLedgerCommand(zk, mlPath):
    +    print getManagedLedgerInfo(zk, mlPath)
    +    
    +
    +'''
    +print managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --command print-cursor
    +'''
    +def printManagedCursorCommand(zk, mlPath, cursorName):
    +    try:
    +        if cursorName:
    +            print getManagedCursorInfo(zk, mlPath)[cursorName]
    +        else:
    +            print 'Usage: --command {} [--cursorName]'.format(printCursorsCommands)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'No cursor found for {}/{}'.format(mlPath, cursorName)
    +
    +'''
    +delete specific ledgerIds from the managed-ledger info
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command delete-ml-ledgers --ledgerIds 3
    +'''    
    +def deleteMLLedgerIdsCommand(zk, mlPath, deleteLedgerIds):
    +    try:
    +        if deleteLedgerIds:
    +            deletLedgerIds = set(deleteLedgerIds.split(","))
    +            deletLedgerIdSet = set()
    +            for id in deletLedgerIds:
    +                deletLedgerIdSet.add(long(id))
    +            print     deletLedgerIdSet
    +            deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIdSet)
    +        else:
    +            print 'Usage: --command {} [--ledgerIds]'.format(deleteMlLedgerIds)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to delete ml-ledger_ids {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Update mark-delete position of the given managed-cursor
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --cursorMarkDelete 0:1 --command update-mark-delete-cursor 
    +'''    
    +def updateMarkDeleteOfCursorCommand(zk, mlPath, cursorName, markDeletePosition):
    +    try:
    +        if cursorName:
    +            if markDeletePosition:
    +                positionPair = markDeletePosition.split(":")
    +                if len(positionPair) == 2:
    +                    updateCursorMarkDelete(zk, mlPath + "/" + cursorName, (long(positionPair[0])), long(positionPair[1]))
    +                else:
    +                    print "markDeletePosition must be in format <ledger_id>:<entry_id>"
    +            else:
    +                print 'Usage: --command {} [----cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +        else:
    +            print 'Usage: --command {} [--cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to update ml-cursor {}/{} due to {}'.format(mlPath, cursorName, repr(e))
    +            
    +if __name__ in '__main__':
    +        parser = argparse.ArgumentParser()
    +        commandHelpText = 'Managed-ledger command: \n{}, {}, {}, {}'.format(printMlCommand, deleteMlLedgerIds, printCursorsCommands, updateMakDeleteCursor)
    +        parser.add_argument("--zkServer", "-zk", required=True, help="ZooKeeperServer:port")
    +        parser.add_argument("--command", "-cmd", required=True, help=commandHelpText)
    --- End diff --
    
    Instead of getting `--command` as an argument, to behave the same way as the other tools, can we get it as a positional parameter? eg: 
    
    ```
    ManagedLedgerAdminTool.py print-cursor --zkServer localhost:2181 \
           --mlPath sample/standalone/ns1/persistent/test --cursorName s1
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123854374
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/MLDataFormats_pb2.py ---
    @@ -0,0 +1,342 @@
    +# Generated by the protocol buffer compiler.  DO NOT EDIT!
    --- End diff --
    
    Make sure to that the maven license plugin will ignore this, otherwise maven install will fail


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123883091
  
    --- Diff: bin/pulsar-managed-ledger-admin.py ---
    @@ -0,0 +1,312 @@
    +#!/usr/bin/env bash
    +#
    +# 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 argparse
    +import traceback
    +from google.protobuf.text_format import Merge
    +from google.protobuf.text_format import MessageToString
    +import sys
    +    
    +from proto import MLDataFormats_pb2
    +
    +try:
    +    from kazoo.client import KazooClient
    +except Exception as missingLib:
    +    print "You need Kazoo ZK client library. Get it from: pip install kazoo"
    --- End diff --
    
    Need to exit here with error code


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123866121
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    --- End diff --
    
    Also, license headers are missing


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123866446
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    +
    +from mltool import MLDataFormats_pb2
    +
    +'''
    +This util provides api to access managed-ledger data if it's ONLY stored in binary format in to zookeeper.
    +It also provides command line tool-access to execute these commands.
    +
    +Run: run from parent directory
    +python -m mltool.ManagedLedgerAdminTool
    +'''
    +managedLedgerPath = "/managed-ledgers/"
    +printMlCommand = "print-ml"
    +deleteMlLedgerIds = "delete-ml-ledgers"
    +printCursorsCommands = "print-cursor"
    +updateMakDeleteCursor = "update-mark-delete-cursor"
    +
    +'''
    +Returns managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +''' 
    +def getManagedLedgerInfo(zk, mlPath):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    --- End diff --
    
    Is this handling the protobuf text format as well?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123854712
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    --- End diff --
    
    Should we just place this scripts under `bin/python/...` ?
    And include this in the src/bin distribution tgz.
    
    At the same time, let's just use a similar naming as the other CLI tools. Eg: 
    
    `bin/pulsar-managed-ledger-admin`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123854887
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    --- End diff --
    
    if importing `kazoo` fails, can we print something like:
    ```
    You ned Kazoo ZK client library. Get it from: "pip install kazoo"
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123883078
  
    --- Diff: bin/pulsar-managed-ledger-admin.py ---
    @@ -0,0 +1,312 @@
    +#!/usr/bin/env bash
    --- End diff --
    
    This should be `python` instead of `bash`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123886484
  
    --- Diff: bin/pulsar-managed-ledger-admin.py ---
    @@ -0,0 +1,312 @@
    +#!/usr/bin/env bash
    --- End diff --
    
    We can just call it `bin/pulsar-managed-ledger-admin`, no need for py extension here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123855309
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    +
    +from mltool import MLDataFormats_pb2
    +
    +'''
    +This util provides api to access managed-ledger data if it's ONLY stored in binary format in to zookeeper.
    +It also provides command line tool-access to execute these commands.
    +
    +Run: run from parent directory
    +python -m mltool.ManagedLedgerAdminTool
    +'''
    +managedLedgerPath = "/managed-ledgers/"
    +printMlCommand = "print-ml"
    +deleteMlLedgerIds = "delete-ml-ledgers"
    +printCursorsCommands = "print-cursor"
    +updateMakDeleteCursor = "update-mark-delete-cursor"
    +
    +'''
    +Returns managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +''' 
    +def getManagedLedgerInfo(zk, mlPath):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        return mlInfo
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get data for {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Delete specific ledgerIds from the managed-ledger info and updates into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +'''  
    +def deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIds):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        ledgerInfoList = mlInfo.ledgerInfo
    +        
    +        for ledgerInfo in ledgerInfoList:
    +            print ledgerInfo.ledgerId
    +            if ledgerInfo.ledgerId in deletLedgerIds:
    +                ledgerInfoList.remove(ledgerInfo)
    +                updatedMlInfo = mlInfo.SerializeToString();
    +                zk.set(mlPath, updatedMlInfo, -1)
    +                print 'Updated {} with value\n{}'.format(mlPath, str(mlInfo))
    +        
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to delete ledgerIds for {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Returns managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +'''
    +def getManagedCursorInfo(zk, mlPath):
    +    try:
    +        cursors = zk.get_children(mlPath)
    +        cursorList = {}
    +        for cursor in cursors:
    +            cursorData = zk.get(mlPath + "/" + cursor)[0]
    +            cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +            cursorInfo.ParseFromString(cursorData)
    +            cursorList[cursor] = cursorInfo
    +        return cursorList
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get ml-cursor {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Update mark-delete position of the given managed-cursor into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +''' 
    +def updateCursorMarkDelete(zk, cursorPath, markDeleteLedgerId, markDeleteEntryId):
    +    try:
    +        cursorData = zk.get(cursorPath)[0]
    +        cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +        cursorInfo.ParseFromString(cursorData)
    +        cursorInfo.markDeleteLedgerId = markDeleteLedgerId
    +        cursorInfo.markDeleteEntryId = markDeleteEntryId
    +        sData = cursorInfo.SerializeToString()
    +        zk.set(cursorPath, sData, -1)
    +        print 'Updated {} with value \n{}'.format(cursorPath, cursorInfo)
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to update ml-cursor {} due to {}'.format(cursorPath, repr(e))
    +
    +
    +
    +'''
    +print managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +    
    +eg: 
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command print-ml
    +''' 
    +def printManagedLedgerCommand(zk, mlPath):
    +    print getManagedLedgerInfo(zk, mlPath)
    +    
    +
    +'''
    +print managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --command print-cursor
    +'''
    +def printManagedCursorCommand(zk, mlPath, cursorName):
    +    try:
    +        if cursorName:
    +            print getManagedCursorInfo(zk, mlPath)[cursorName]
    +        else:
    +            print 'Usage: --command {} [--cursorName]'.format(printCursorsCommands)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'No cursor found for {}/{}'.format(mlPath, cursorName)
    +
    +'''
    +delete specific ledgerIds from the managed-ledger info
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command delete-ml-ledgers --ledgerIds 3
    +'''    
    +def deleteMLLedgerIdsCommand(zk, mlPath, deleteLedgerIds):
    +    try:
    +        if deleteLedgerIds:
    +            deletLedgerIds = set(deleteLedgerIds.split(","))
    +            deletLedgerIdSet = set()
    +            for id in deletLedgerIds:
    +                deletLedgerIdSet.add(long(id))
    +            print     deletLedgerIdSet
    +            deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIdSet)
    +        else:
    +            print 'Usage: --command {} [--ledgerIds]'.format(deleteMlLedgerIds)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to delete ml-ledger_ids {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Update mark-delete position of the given managed-cursor
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --cursorMarkDelete 0:1 --command update-mark-delete-cursor 
    +'''    
    +def updateMarkDeleteOfCursorCommand(zk, mlPath, cursorName, markDeletePosition):
    +    try:
    +        if cursorName:
    +            if markDeletePosition:
    +                positionPair = markDeletePosition.split(":")
    +                if len(positionPair) == 2:
    +                    updateCursorMarkDelete(zk, mlPath + "/" + cursorName, (long(positionPair[0])), long(positionPair[1]))
    +                else:
    +                    print "markDeletePosition must be in format <ledger_id>:<entry_id>"
    +            else:
    +                print 'Usage: --command {} [----cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +        else:
    +            print 'Usage: --command {} [--cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to update ml-cursor {}/{} due to {}'.format(mlPath, cursorName, repr(e))
    +            
    +if __name__ in '__main__':
    +        parser = argparse.ArgumentParser()
    +        commandHelpText = 'Managed-ledger command: \n{}, {}, {}, {}'.format(printMlCommand, deleteMlLedgerIds, printCursorsCommands, updateMakDeleteCursor)
    +        parser.add_argument("--zkServer", "-zk", required=True, help="ZooKeeperServer:port")
    +        parser.add_argument("--command", "-cmd", required=True, help=commandHelpText)
    --- End diff --
    
    Instead of getting `--command` as an argument, to behave the same way as the other tools, can we get it as a positional parameter? eg: 
    
    ```
    ManagedLedgerAdminTool.py print-cursor --zkServer localhost:2181 \
           --mlPath sample/standalone/ns1/persistent/test --cursorName s1
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar issue #526: Add managed-ledger admin tool to access ml bina...

Posted by rdhabalia <gi...@git.apache.org>.
Github user rdhabalia commented on the issue:

    https://github.com/apache/incubator-pulsar/pull/526
  
    @merlimat  addressed all the changes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by merlimat <gi...@git.apache.org>.
Github user merlimat commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123855445
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    +
    +from mltool import MLDataFormats_pb2
    +
    +'''
    +This util provides api to access managed-ledger data if it's ONLY stored in binary format in to zookeeper.
    +It also provides command line tool-access to execute these commands.
    +
    +Run: run from parent directory
    +python -m mltool.ManagedLedgerAdminTool
    +'''
    +managedLedgerPath = "/managed-ledgers/"
    +printMlCommand = "print-ml"
    +deleteMlLedgerIds = "delete-ml-ledgers"
    +printCursorsCommands = "print-cursor"
    +updateMakDeleteCursor = "update-mark-delete-cursor"
    +
    +'''
    +Returns managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +''' 
    +def getManagedLedgerInfo(zk, mlPath):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        return mlInfo
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get data for {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Delete specific ledgerIds from the managed-ledger info and updates into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +'''  
    +def deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIds):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    +        ledgerInfoList = mlInfo.ledgerInfo
    +        
    +        for ledgerInfo in ledgerInfoList:
    +            print ledgerInfo.ledgerId
    +            if ledgerInfo.ledgerId in deletLedgerIds:
    +                ledgerInfoList.remove(ledgerInfo)
    +                updatedMlInfo = mlInfo.SerializeToString();
    +                zk.set(mlPath, updatedMlInfo, -1)
    +                print 'Updated {} with value\n{}'.format(mlPath, str(mlInfo))
    +        
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to delete ledgerIds for {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Returns managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +'''
    +def getManagedCursorInfo(zk, mlPath):
    +    try:
    +        cursors = zk.get_children(mlPath)
    +        cursorList = {}
    +        for cursor in cursors:
    +            cursorData = zk.get(mlPath + "/" + cursor)[0]
    +            cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +            cursorInfo.ParseFromString(cursorData)
    +            cursorList[cursor] = cursorInfo
    +        return cursorList
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to get ml-cursor {} due to {}'.format(mlPath, repr(e))
    +
    +
    +'''
    +Update mark-delete position of the given managed-cursor into zk
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +''' 
    +def updateCursorMarkDelete(zk, cursorPath, markDeleteLedgerId, markDeleteEntryId):
    +    try:
    +        cursorData = zk.get(cursorPath)[0]
    +        cursorInfo = MLDataFormats_pb2.ManagedCursorInfo()
    +        cursorInfo.ParseFromString(cursorData)
    +        cursorInfo.markDeleteLedgerId = markDeleteLedgerId
    +        cursorInfo.markDeleteEntryId = markDeleteEntryId
    +        sData = cursorInfo.SerializeToString()
    +        zk.set(cursorPath, sData, -1)
    +        print 'Updated {} with value \n{}'.format(cursorPath, cursorInfo)
    +    except Exception as e:
    +            traceback.print_exc()
    +            print 'Failed to update ml-cursor {} due to {}'.format(cursorPath, repr(e))
    +
    +
    +
    +'''
    +print managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +    
    +eg: 
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command print-ml
    +''' 
    +def printManagedLedgerCommand(zk, mlPath):
    +    print getManagedLedgerInfo(zk, mlPath)
    +    
    +
    +'''
    +print managed-ledger cursor info for given managed-cursor path
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --command print-cursor
    +'''
    +def printManagedCursorCommand(zk, mlPath, cursorName):
    +    try:
    +        if cursorName:
    +            print getManagedCursorInfo(zk, mlPath)[cursorName]
    +        else:
    +            print 'Usage: --command {} [--cursorName]'.format(printCursorsCommands)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'No cursor found for {}/{}'.format(mlPath, cursorName)
    +
    +'''
    +delete specific ledgerIds from the managed-ledger info
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +deleteLedgerIds : str
    +    comma separated deleting ledger-ids (eg: 123,124)
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --command delete-ml-ledgers --ledgerIds 3
    +'''    
    +def deleteMLLedgerIdsCommand(zk, mlPath, deleteLedgerIds):
    +    try:
    +        if deleteLedgerIds:
    +            deletLedgerIds = set(deleteLedgerIds.split(","))
    +            deletLedgerIdSet = set()
    +            for id in deletLedgerIds:
    +                deletLedgerIdSet.add(long(id))
    +            print     deletLedgerIdSet
    +            deleteLedgerIdsFromManagedLedgerInfo(zk, mlPath, deletLedgerIdSet)
    +        else:
    +            print 'Usage: --command {} [--ledgerIds]'.format(deleteMlLedgerIds)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to delete ml-ledger_ids {} due to {}'.format(mlPath, repr(e))
    +
    +'''
    +Update mark-delete position of the given managed-cursor
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +cursorName : str
    +    managed-cursor path
    +markDeletePosition: str
    +    markDeletePosition combination of <ledgerId>:<entryId> (eg. 123:1)
    +
    +eg:
    +--zkServer localhost:2181 --mlPath sample/standalone/ns1/persistent/test --cursorName s1 --cursorMarkDelete 0:1 --command update-mark-delete-cursor 
    +'''    
    +def updateMarkDeleteOfCursorCommand(zk, mlPath, cursorName, markDeletePosition):
    +    try:
    +        if cursorName:
    +            if markDeletePosition:
    +                positionPair = markDeletePosition.split(":")
    +                if len(positionPair) == 2:
    +                    updateCursorMarkDelete(zk, mlPath + "/" + cursorName, (long(positionPair[0])), long(positionPair[1]))
    +                else:
    +                    print "markDeletePosition must be in format <ledger_id>:<entry_id>"
    +            else:
    +                print 'Usage: --command {} [----cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +        else:
    +            print 'Usage: --command {} [--cursorName] [--cursorMarkDelete]'.format(updateMakDeleteCursor)
    +    except Exception as e:
    +        traceback.print_exc()
    +        print 'Failed to update ml-cursor {}/{} due to {}'.format(mlPath, cursorName, repr(e))
    +            
    +if __name__ in '__main__':
    +        parser = argparse.ArgumentParser()
    +        commandHelpText = 'Managed-ledger command: \n{}, {}, {}, {}'.format(printMlCommand, deleteMlLedgerIds, printCursorsCommands, updateMakDeleteCursor)
    +        parser.add_argument("--zkServer", "-zk", required=True, help="ZooKeeperServer:port")
    +        parser.add_argument("--command", "-cmd", required=True, help=commandHelpText)
    +        parser.add_argument("--mlPath", "-mlp", required=True, help="Managed-ledger path")
    --- End diff --
    
    `mlPath` and `ml` in general is quite confusing for people not used to the context. Can we use `managed-ledger` instead?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-pulsar pull request #526: Add managed-ledger admin tool to access ...

Posted by rdhabalia <gi...@git.apache.org>.
Github user rdhabalia commented on a diff in the pull request:

    https://github.com/apache/incubator-pulsar/pull/526#discussion_r123883007
  
    --- Diff: pulsar-client-admin/src/main/resources/mltool/ManagedLedgerAdminTool.py ---
    @@ -0,0 +1,261 @@
    +#!/usr/bin/env python
    +
    +import argparse
    +import traceback
    +
    +from kazoo.client import KazooClient
    +
    +from mltool import MLDataFormats_pb2
    +
    +'''
    +This util provides api to access managed-ledger data if it's ONLY stored in binary format in to zookeeper.
    +It also provides command line tool-access to execute these commands.
    +
    +Run: run from parent directory
    +python -m mltool.ManagedLedgerAdminTool
    +'''
    +managedLedgerPath = "/managed-ledgers/"
    +printMlCommand = "print-ml"
    +deleteMlLedgerIds = "delete-ml-ledgers"
    +printCursorsCommands = "print-cursor"
    +updateMakDeleteCursor = "update-mark-delete-cursor"
    +
    +'''
    +Returns managed-ledger info for given managed-leger path 
    +Parameters
    +----------
    +zk : KazooClient
    +   Zookeeper-client instance to query zk-client.
    +mlPath : str
    +    managed-ledger path
    +''' 
    +def getManagedLedgerInfo(zk, mlPath):
    +    try:
    +        # get managed-ledger info
    +        mlData = zk.get(mlPath)[0]
    +        mlInfo = MLDataFormats_pb2.ManagedLedgerInfo()
    +        mlInfo.ParseFromString(mlData)
    --- End diff --
    
    yes, earlier I thought we will use this tool after merging PR which forces to write in Binary format. However, I have added Text support also.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---