Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:4paradigm OpenMLDB Executor

From Leeroopedia
Knowledge Sources
Domains Cluster_Operations, Distributed_Systems
Last Updated 2026-10-12 12:00 GMT

Overview

Python class providing administrative control over an OpenMLDB cluster via CLI subprocess calls to the openmldb binary.

Description

The Executor class (tools/tool.py) is the core library for OpenMLDB cluster operations. It wraps the openmldb binary's client modes (ns_client, client, sql_client) in Python methods, providing a programmatic interface for cluster management. It connects via ZooKeeper, discovers the nameserver leader, and executes commands as subprocess calls. Key capabilities include: auto-failover management, table/tablet status queries, table partition operations (migrate, change leader, add/del replica), table recovery, and operation status polling.

Usage

Use the Executor class when writing Python scripts for cluster operations — data recovery, scaling, rolling upgrades, or custom automation. It is the foundation of the openmldb_ops.py CLI tool. Instantiate with the openmldb binary path, ZooKeeper cluster address, and root path, then call Connect() to discover the cluster topology.

Code Reference

Source Location

  • Repository: OpenMLDB
  • File: tools/tool.py (L77-469)

Signature

class Status:
    def __init__(self, code=0, msg="ok")
    def OK(self) -> bool
    def GetMsg(self) -> str
    def GetCode(self) -> int

class Partition:
    def __init__(self, name, tid, pid, endpoint, is_leader, is_alive, offset)
    def GetTid(self) -> str
    def GetName(self) -> str
    def GetPid(self) -> str
    def GetOffset(self) -> int
    def GetEndpoint(self) -> str
    def IsAlive(self) -> bool
    def IsLeader(self) -> bool
    def GetKey(self) -> str  # "{tid}_{pid}"

class Executor:
    def __init__(self, openmldb_bin_path: str, zk_cluster: str, zk_root_path: str)
    def Connect(self) -> Status
    def RunWithReturncode(self, command, universal_newlines=True, useshell=USE_SHELL, env=os.environ) -> (Status, output)
    def GetNsLeader(self) -> (Status, str)
    def ParseResult(self, output: str) -> list
    def GetAutofailover(self) -> (Status, bool)
    def SetAutofailover(self, value: str) -> Status
    def GetAllDatabase(self) -> (Status, list)
    def GetTableInfo(self, database: str, table_name: str = '') -> (Status, list)
    def GetTableInfoHTTP(self, database: str, table_name: str = '') -> (Status, list)
    def ParseTableInfo(self, table_info) -> (Status, dict)
    def ParseTableInfoJson(self, table_info: dict) -> dict
    def GetTablePartition(self, database: str, table_name: str) -> (Status, dict)
    def GetAllTable(self, database: str) -> (Status, list)
    def GetTableStatus(self, endpoint: str, tid: str = '', pid: str = '') -> (Status, dict)
    def ShowTableStatus(self, pattern: str = '%') -> (Status, list)
    def ShowTablet(self) -> (Status, list)
    def ShowOpStatus(self, database: str, name: str = '', pid: str = '') -> (Status, list)
    def LoadTableHTTP(self, endpoint, name, tid, pid, storage) -> Status
    def GetLeaderFollowerOffset(self, endpoint, tid, pid) -> (Status, list)
    def RecoverTablePartition(self, database, name, pid, endpoint, sync=False) -> Status
    def UpdateTableAlive(self, database, name, pid, endpoint, is_alive) -> Status
    def ChangeLeader(self, database, name, pid, endpoint="auto", sync=False) -> Status
    def CancelOp(self, database, op_id) -> Status
    def Migrate(self, database, name, pid, src_endpoint, desc_endpoint, sync=False) -> Status
    def AddReplica(self, database, name, pid, endpoint, sync=False) -> Status
    def DelReplica(self, database, name, pid, endpoint, sync=False) -> Status
    def WaitingOP(self, database, name, pid) -> Status

Import

from tool import Executor, Partition, Status

executor = Executor("/work/openmldb/bin/openmldb", "127.0.0.1:6181", "/onebox")
status = executor.Connect()

I/O Contract

Inputs

Name Type Required Description
openmldb_bin_path str Yes Path to the openmldb binary
zk_cluster str Yes ZooKeeper cluster address (e.g. 127.0.0.1:6181)
zk_root_path str Yes ZK root path (e.g. /onebox or /openmldb)

Outputs

Name Type Description
Status Status object code=0 on success, non-zero with msg on failure
Parsed results list/dict Table-format output parsed from CLI stdout into lists of records

Usage Examples

Connect and Check Cluster State

from tool import Executor

# Create and connect executor
executor = Executor(
    "/work/openmldb/bin/openmldb",
    "127.0.0.1:6181",
    "/onebox"
)
status = executor.Connect()
if not status.OK():
    print("Connect failed:", status.GetMsg())
    exit(1)

# Check auto-failover state
status, auto_failover = executor.GetAutofailover()
print("Auto-failover enabled:", auto_failover)

# Show all tablets
status, tablets = executor.ShowTablet()
for tablet in tablets:
    print("Tablet:", tablet[0], "State:", tablet[2])

Disable/Enable Auto-Failover

# Disable auto-failover before maintenance
executor.SetAutofailover("false")

# ... perform maintenance operations ...

# Re-enable auto-failover
executor.SetAutofailover("true")

Migrate a Partition

# Migrate partition from one tablet to another
status = executor.Migrate(
    database="demo_db",
    name="test_table",
    pid="0",
    src_endpoint="127.0.0.1:9520",
    desc_endpoint="127.0.0.1:9521",
    sync=True  # Wait for operation to complete
)
if status.OK():
    print("Migrate succeeded")
else:
    print("Migrate failed:", status.GetMsg())

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment