moonkeeper

    ZooKeeper client for MoonBit: sessions, watches, ACLs, transactions and a native CLI

    zookeeper
    client
    protocol
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    18 hours ago
    Downloads
    9

    #MoonKeeper

    简体中文 · English

    MoonKeeper 是用 MoonBit 实现的 Apache ZooKeeper 客户端。你可以用它读取配置并监听变化,用临时顺序节点注册服务,或用一个原子事务同时更新多个节点。网络客户端直接通过 TCP 与 ZooKeeper 通信。

    #从 Mooncakes 安装

    在自己的 MoonBit 项目根目录执行:

    moon add backaccouup/moonkeeper@0.1.0

    在使用网络客户端的 moon.pkg 中加入以下配置,然后运行 moon check --target native。本版使用 moonc 1.0.0-rc1+3c0520448 构建和测试。Mooncakes 包页面

    import {
    "backaccouup/moonkeeper/client",
    }
    supported_targets = "native"

    #五分钟试用

    准备 MoonBit 工具链、C 编译器和 ZooKeeper 3.9.3。下面使用本地测试实例,监听 127.0.0.1:21819。在空的测试实例中执行:

    moon update docker compose -f integration/compose.yaml up -d --wait moon run cmd/keeper -- --servers 127.0.0.1:21819 create /keeper-demo --text hello moon run cmd/keeper -- --servers 127.0.0.1:21819 get /keeper-demo moon run cmd/keeper -- --servers 127.0.0.1:21819 watch /keeper-demo --kind data

    get 输出 Base64,示例值是 aGVsbG8=。保持 Watch 命令运行,在第二个终端修改数据。第一个终端收到一次 NodeDataChanged 后退出。

    moon run cmd/keeper -- --servers 127.0.0.1:21819 set /keeper-demo --version 0 --text world moon run cmd/keeper -- --servers 127.0.0.1:21819 delete /keeper-demo --version 1 docker compose -f integration/compose.yaml down --volumes

    已有 ZooKeeper 时,直接将命令中的地址换成自己的测试服务。配置监听与服务注册两个完整示例的启动、观察和清理步骤见使用手册。

    #能力与入口

    能力使用方式
    节点和版本create、get_data、set_data、delete、exists。支持中文路径、可空二进制数据、版本条件和完整 Stat。
    服务发现持久、临时及其顺序模式,列举直接子节点及父节点 Stat。创建结果返回服务端实际分配的路径。
    通知watch_exists、watch_data、watch_children 返回读取结果和一次性 Watch。收到通知后重新读取并注册。
    权限Digest 身份认证、显式创建 ACL、读取 ACL 和按 ACL 版本修改权限。认证与 ACL
    原子事务multi 支持 Create、SetData、Delete 和 Check。返回逐项结果和事务是否提交。事务示例
    会话按 xid 分发并发请求、空闲心跳、限时关闭。提供备用地址和恢复策略后,可在原会话内恢复凭据和 Watch。会话与失败语义

    根协议包为 backaccouup/moonkeeper,native 客户端包为 backaccouup/moonkeeper/client。CLI 使用本地安装的 MoonBit 编译运行,默认后端为 native。

    #构建与验证

    moon fmt --check moon check --target all --deny-warn moon build --target all --deny-warn moon test --target all --deny-warn

    真实服务验收包括单节点测试、与 Kazoo 的双向读写和三节点故障切换。工具链版本、Windows/Linux 命令及测试边界见兼容性与验收。本版使用普通 TCP 和一次性 Watch。TLS、SASL、持久或递归 Watch、TTL/container 节点及集群动态配置不在首版范围内。

    ProtocolError

    pub suberror ProtocolError {
    InvalidTimeout(Int)
    TruncatedFrame
    InvalidFrameLength(Int)
    FrameTooLarge(Int)
    FrameLengthMismatch(Int, Int)
    TruncatedPayload
    InvalidBufferLength(Int)
    TrailingBytes(Int)
    InvalidFrameLimit(Int)
    DecoderClosed
    InvalidPath(String)
    InvalidVersion(Int)
    InvalidAcl(String)
    InvalidMulti(String)
    InvalidAuth
    UnexpectedResponse(Int)
    ServerError(Int)
    } derive(
    Debug
    )

    Malformed wire data or an invalid new-session request. Lengths in these errors are byte counts, excluding the four-byte frame prefix.

    ProtocolError::to_repr

    automatically derived

    Acl

    pub(all) struct Acl {
    permissions : Int
    scheme : String
    id : String
    } derive(Eq,
    Debug
    )

    Acl::equal

    fn Acl::equal(Acl, Acl) -> Bool
    automatically derived

    Acl::not_equal

    fn Acl::not_equal(x : Acl, y : Acl) -> Bool
    automatically derived

    Acl::to_repr

    automatically derived

    AclResult

    pub(all) struct AclResult {
    acl : Array[Acl]
    stat : Stat
    } derive(Eq,
    Debug
    )

    AclResult::equal

    fn AclResult::equal(AclResult, AclResult) -> Bool
    automatically derived

    AclResult::not_equal

    fn AclResult::not_equal(x : AclResult, y : AclResult) -> Bool
    automatically derived

    AclResult::to_repr

    automatically derived

    ChildrenResult

    pub(all) struct ChildrenResult {
    children : Array[String]
    stat : Stat
    } derive(Eq,
    Debug
    )

    ChildrenResult::equal

    automatically derived

    ChildrenResult::not_equal

    fn ChildrenResult::not_equal(x : ChildrenResult, y : ChildrenResult) -> Bool
    automatically derived

    ChildrenResult::to_repr

    automatically derived

    ConnectResponse

    pub struct ConnectResponse {
    protocol_version : Int
    session_timeout_ms : Int
    session_id : Int64
    password : Bytes?
    read_only : Bool
    }

    Decoded wire fields, not proof that a session was established. A nonpositive timeout signals session rejection at the client layer. The password is a session credential; do not print it in application logs.

    CreateMode

    pub(all) enum CreateMode {
    Persistent
    Ephemeral
    PersistentSequential
    EphemeralSequential
    } derive(Eq,
    Debug
    )

    CreateMode::equal

    fn CreateMode::equal(CreateMode, CreateMode) -> Bool
    automatically derived

    CreateMode::not_equal

    fn CreateMode::not_equal(x : CreateMode, y : CreateMode) -> Bool
    automatically derived

    CreateMode::to_repr

    automatically derived

    CreateOptions

    pub(all) struct CreateOptions {
    mode : CreateMode
    acl : Array[Acl]
    }

    CreateOptions::new

    fn CreateOptions::new(mode : CreateMode, acl : Array[Acl]) -> CreateOptions

    DataResult

    pub(all) struct DataResult {
    data : Bytes?
    stat : Stat
    } derive(Eq,
    Debug
    )

    DataResult::equal

    fn DataResult::equal(DataResult, DataResult) -> Bool
    automatically derived

    DataResult::not_equal

    fn DataResult::not_equal(x : DataResult, y : DataResult) -> Bool
    automatically derived

    DataResult::to_repr

    automatically derived

    FrameDecoder

    pub struct FrameDecoder {
    // private fields
    }

    Incremental length-prefixed framing. Only the current partial frame is retained. Completed frames include their four-byte prefix. Errors permanently seal the decoder.

    FrameDecoder::feed

    fn FrameDecoder::feed(self : FrameDecoder, chunk : Bytes) -> Array[Bytes] raise ProtocolError

    FrameDecoder::finish

    fn FrameDecoder::finish(self : FrameDecoder) -> Unit raise ProtocolError

    Seal the stream. A partial header or body is an error; a clean finish is idempotent.

    FrameDecoder::new

    fn FrameDecoder::new(max_frame_size? : Int) -> FrameDecoder raise ProtocolError

    max_frame_size limits payload bytes; zero-length frames are permitted.

    MultiOp

    pub(all) enum MultiOp {
    Create(String, Bytes?, CreateOptions)
    SetData(String, Bytes?, Int)
    Delete(String, Int)
    Check(String, Int)
    }

    MultiOpResult

    pub(all) enum MultiOpResult {
    Created(String)
    DataSet(Stat)
    Deleted
    Checked
    Failed(Int)
    NotApplied(Int)
    } derive(Eq,
    Debug
    )

    MultiOpResult::equal

    automatically derived

    MultiOpResult::not_equal

    fn MultiOpResult::not_equal(x : MultiOpResult, y : MultiOpResult) -> Bool
    automatically derived

    MultiOpResult::to_repr

    automatically derived

    MultiOutcome

    pub(all) enum MultiOutcome {
    Committed(Array[MultiOpResult])
    Aborted(Int, Array[MultiOpResult])
    } derive(Eq,
    Debug
    )

    MultiOutcome::equal

    automatically derived

    MultiOutcome::not_equal

    fn MultiOutcome::not_equal(x : MultiOutcome, y : MultiOutcome) -> Bool
    automatically derived

    MultiOutcome::to_repr

    automatically derived

    ReplyHeader

    pub(all) struct ReplyHeader {
    xid : Int
    zxid : Int64
    err : Int
    }

    RequestHeader

    pub(all) struct RequestHeader {
    xid : Int
    op : Int
    }

    Stat

    pub(all) struct Stat {
    czxid : Int64
    mzxid : Int64
    ctime : Int64
    mtime : Int64
    version : Int
    cversion : Int
    aversion : Int
    ephemeral_owner : Int64
    data_length : Int
    num_children : Int
    pzxid : Int64
    } derive(Eq,
    Debug
    )

    Stat::equal

    fn Stat::equal(Stat, Stat) -> Bool
    automatically derived

    Stat::not_equal

    fn Stat::not_equal(x : Stat, y : Stat) -> Bool
    automatically derived

    Stat::to_repr

    automatically derived

    WatchEvent

    pub(all) struct WatchEvent {
    kind : WatchEventType
    path : String
    } derive(Eq,
    Debug
    )

    WatchEvent::equal

    fn WatchEvent::equal(WatchEvent, WatchEvent) -> Bool
    automatically derived

    WatchEvent::not_equal

    fn WatchEvent::not_equal(x : WatchEvent, y : WatchEvent) -> Bool
    automatically derived

    WatchEvent::to_repr

    automatically derived

    WatchEventType

    pub(all) enum WatchEventType {
    NodeCreated
    NodeDeleted
    NodeDataChanged
    NodeChildrenChanged
    } derive(Eq,
    Debug
    )

    WatchEventType::equal

    automatically derived

    WatchEventType::not_equal

    fn WatchEventType::not_equal(x : WatchEventType, y : WatchEventType) -> Bool
    automatically derived

    WatchEventType::to_repr

    automatically derived

    WatchNotice

    pub(all) enum WatchNotice {
    Node(WatchEvent)
    State(Int)
    } derive(Eq,
    Debug
    )

    WatchNotice::equal

    fn WatchNotice::equal(WatchNotice, WatchNotice) -> Bool
    automatically derived

    WatchNotice::not_equal

    fn WatchNotice::not_equal(x : WatchNotice, y : WatchNotice) -> Bool
    automatically derived

    WatchNotice::to_repr

    automatically derived

    PERM_ADMIN

    let PERM_ADMIN : Int

    PERM_ALL

    let PERM_ALL : Int

    PERM_CREATE

    let PERM_CREATE : Int

    PERM_DELETE

    let PERM_DELETE : Int

    PERM_READ

    let PERM_READ : Int

    PERM_WRITE

    let PERM_WRITE : Int

    creator_acl

    fn creator_acl() -> Array[Acl]

    ZooKeeper expands auth:"" into this session's authenticated identities.

    decode_auth_response

    fn decode_auth_response(payload : Bytes) -> Unit raise ProtocolError

    decode_connect_response

    fn decode_connect_response(frame : Bytes) -> ConnectResponse raise ProtocolError

    Decode exactly one complete, length-prefixed ConnectResponse. Payloads are limited to 1 MiB by this library, independently of server settings. Null and empty passwords remain distinct. The legacy omitted readOnly byte defaults to false; otherwise Java's nonzero-is-true boolean rule is used. Protocol version and timeout are preserved for the future client to validate. This function does not buffer partial TCP reads or consume concatenated frames.

    decode_create_response

    fn decode_create_response(payload : Bytes) -> String raise ProtocolError

    decode_delete_response

    fn decode_delete_response(payload : Bytes) -> Unit raise ProtocolError

    decode_exists_response

    fn decode_exists_response(payload : Bytes) -> Stat? raise ProtocolError

    decode_get_acl_response

    fn decode_get_acl_response(payload : Bytes) -> AclResult raise ProtocolError

    decode_get_children_response

    fn decode_get_children_response(payload : Bytes) -> Array[String] raise ProtocolError

    decode_get_children_with_stat_response

    fn decode_get_children_with_stat_response(payload : Bytes) -> ChildrenResult raise ProtocolError

    decode_get_data_response

    fn decode_get_data_response(payload : Bytes) -> DataResult raise ProtocolError

    decode_multi_response

    fn decode_multi_response(payload : Bytes, ops : Array[MultiOp]) -> MultiOutcome raise ProtocolError

    decode_reply_header

    fn decode_reply_header(payload : Bytes) -> ReplyHeader raise ProtocolError

    decode_set_acl_response

    fn decode_set_acl_response(payload : Bytes) -> Stat raise ProtocolError

    decode_set_data_response

    fn decode_set_data_response(payload : Bytes) -> Stat raise ProtocolError

    decode_watch_notification

    fn decode_watch_notification(payload : Bytes) -> WatchNotice raise ProtocolError

    Notification payload excludes the four-byte frame prefix.

    encode_auth_request

    fn encode_auth_request(scheme : String, credential : Bytes) -> Bytes raise ProtocolError

    The returned frame contains the cleartext credential and must stay private.

    encode_close_session

    fn encode_close_session(xid : Int) -> Bytes

    encode_connect_request

    fn encode_connect_request(session_timeout_ms : Int, read_only? : Bool) -> Bytes raise ProtocolError

    Encode a new-session ConnectRequest, including its four-byte length prefix. Uses protocol version 0, zero last-zxid/session ID, and a 16-byte zero password. read_only allows a connection to a read-only server; it defaults to false. Use encode_resume_request for an established session.

    encode_create_request

    fn encode_create_request(xid : Int, path : String, data : Bytes?, options : CreateOptions) -> Bytes raise ProtocolError

    encode_delete_request

    fn encode_delete_request(xid : Int, path : String, version : Int) -> Bytes raise ProtocolError

    encode_exists_request

    fn encode_exists_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_get_acl_request

    fn encode_get_acl_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_get_children_request

    fn encode_get_children_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_get_children_with_stat_request

    fn encode_get_children_with_stat_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_get_data_request

    fn encode_get_data_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_multi_request

    fn encode_multi_request(xid : Int, ops : Array[MultiOp], max_frame_size? : Int) -> Bytes raise ProtocolError

    encode_resume_request

    fn encode_resume_request(session_timeout_ms : Int, session_id : Int64, password : Bytes, last_zxid : Int64, read_only? : Bool) -> Bytes raise ProtocolError

    encode_set_acl_request

    fn encode_set_acl_request(xid : Int, path : String, acl : Array[Acl], version : Int) -> Bytes raise ProtocolError

    encode_set_data_request

    fn encode_set_data_request(xid : Int, path : String, data : Bytes?, version : Int) -> Bytes raise ProtocolError

    encode_set_watches

    fn encode_set_watches(zxid : Int64, data : Array[String], exists : Array[String], children : Array[String]) -> Bytes raise ProtocolError

    encode_watch_children_request

    fn encode_watch_children_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_watch_data_request

    fn encode_watch_data_request(xid : Int, path : String) -> Bytes raise ProtocolError

    encode_watch_exists_request

    fn encode_watch_exists_request(xid : Int, path : String) -> Bytes raise ProtocolError

    open_acl_unsafe

    fn open_acl_unsafe() -> Array[Acl]

    ZooKeeper's world:anyone ACL with all five permissions.

    validate_create_path

    fn validate_create_path(path : String, sequential : Bool) -> Unit raise ProtocolError

    Sequential creation validates the prefix as ZooKeeper does: a suffix will be appended by the server, so a final slash can be meaningful.

    validate_path

    fn validate_path(path : String) -> Unit raise ProtocolError

    validate_request_frame_size

    fn validate_request_frame_size(wire : Bytes, max_frame_size : Int) -> Unit raise ProtocolError