Implementation:4paradigm OpenMLDB Executor
| 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
- Principle:4paradigm_OpenMLDB_Failover_Management
- Principle:4paradigm_OpenMLDB_Cluster_Scaling
- Principle:4paradigm_OpenMLDB_Data_Recovery
- Principle:4paradigm_OpenMLDB_Cluster_Operation_Verification