Skip to content

错误处理 ​

所有 C++ 客户端操作都返回 fluss::Result 结构体,而不是抛出异常。这让你能够显式控制错误处理。

Result 结构体 ​

cpp
#include "fluss.hpp"

// All operations return fluss::Result
fluss::Result result = admin.CreateTable(path, descriptor);
if (!result.Ok()) {
    std::cerr << "Error code: " << result.error_code << std::endl;
    std::cerr << "Error message: " << result.error_message << std::endl;
}
字段 / 方法类型描述
error_codeint32_t0 表示成功,非 0 表示错误
error_messagestd::string人类可读的错误描述
Ok()bool操作成功时返回 true

处理错误 ​

在每次操作后检查 Result 并决定如何应对,例如记录日志并继续、重试或中止:

cpp
fluss::Connection conn;
fluss::Result result = fluss::Connection::Create(config, conn);
if (!result.Ok()) {
    // Log, retry, or propagate the error as appropriate
    std::cerr << "Connection failed (code " << result.error_code
              << "): " << result.error_message << std::endl;
    return 1;
}

检查连接状态 ​

使用 Available() 在使用连接或对象之前验证其是否有效:

cpp
fluss::Connection conn;
if (!conn.Available()) {
    // Connection not initialized or already moved
}

fluss::Configuration config;
config.bootstrap_servers = "127.0.0.1:9123";
fluss::Result result = fluss::Connection::Create(config, conn);
if (result.Ok() && conn.Available()) {
    // Connection is ready to use
}

错误码 ​

服务端错误带有特定的错误码(>0 或 -1)。客户端错误(连接失败、类型不匹配等)使用 ErrorCode::CLIENT_ERROR(-2)。使用 fluss::ErrorCode 来匹配特定错误码:

cpp
fluss::Result result = admin.DropTable(table_path);
if (!result.Ok()) {
    if (result.error_code == fluss::ErrorCode::TABLE_NOT_EXIST) {
        std::cerr << "Table does not exist" << std::endl;
    } else if (result.error_code == fluss::ErrorCode::PARTITION_NOT_EXISTS) {
        std::cerr << "Partition does not exist" << std::endl;
    } else if (result.error_code == fluss::ErrorCode::CLIENT_ERROR) {
        std::cerr << "Client-side error: " << result.error_message << std::endl;
    } else {
        std::cerr << "Server error (code " << result.error_code
                  << "): " << result.error_message << std::endl;
    }
}

常见错误码 ​

常量错误码描述
ErrorCode::CLIENT_ERROR-2客户端错误(非来自服务端)
ErrorCode::UNKNOWN_SERVER_ERROR-1意外的服务端错误
ErrorCode::NETWORK_EXCEPTION1服务端在响应前断开连接
ErrorCode::DATABASE_NOT_EXIST4数据库不存在
ErrorCode::DATABASE_ALREADY_EXIST6数据库已存在
ErrorCode::TABLE_NOT_EXIST7表不存在
ErrorCode::TABLE_ALREADY_EXIST8表已存在
ErrorCode::INVALID_TABLE_EXCEPTION15无效的表操作
ErrorCode::REQUEST_TIME_OUT25请求超时
ErrorCode::PARTITION_NOT_EXISTS36分区不存在
ErrorCode::PARTITION_ALREADY_EXISTS42分区已存在
ErrorCode::PARTITION_SPEC_INVALID_EXCEPTION43无效的分区 spec
ErrorCode::LEADER_NOT_AVAILABLE_EXCEPTION44该分区没有可用的 leader
ErrorCode::AUTHENTICATE_EXCEPTION46认证失败(凭据错误)

完整的具名常量列表请参见 fluss.hpp 中的 fluss::ErrorCode。

重试逻辑 ​

有些错误是暂时性的,例如服务端可能临时不可用、正在选举中或处于高负载。可以使用 IsRetriable() 来决定重试该操作,而不是将错误视为永久性的。

ErrorCode::IsRetriable(int32_t code) 是一个可直接在错误码上调用的静态辅助方法:

cpp
fluss::Result result = writer.Append(row);
if (!result.Ok()) {
    if (result.IsRetriable()) {
        // Transient failure — safe to retry 
    } else {
        // Permanent failure — log and abort
        std::cerr << "Fatal error (code " << result.error_code
                  << "): " << result.error_message << std::endl;
    }
}

Result::IsRetriable() 委托给 ErrorCode::IsRetriable(),因此你也可以直接在错误码上调用它:

cpp
if (fluss::ErrorCode::IsRetriable(result.error_code)) {
    // retry
}

可重试的错误码 ​

常量错误码原因
ErrorCode::NETWORK_EXCEPTION1服务端断开连接
ErrorCode::CORRUPT_MESSAGE3CRC 或大小错误
ErrorCode::SCHEMA_NOT_EXIST9Schema 可能不存在
ErrorCode::LOG_STORAGE_EXCEPTION10暂时性的 log 存储错误
ErrorCode::KV_STORAGE_EXCEPTION11暂时性的 KV 存储错误
ErrorCode::NOT_LEADER_OR_FOLLOWER12Leader 选举正在进行
ErrorCode::CORRUPT_RECORD_EXCEPTION14记录损坏
ErrorCode::UNKNOWN_TABLE_OR_BUCKET_EXCEPTION21元数据尚不可用
ErrorCode::REQUEST_TIME_OUT25请求超时
ErrorCode::STORAGE_EXCEPTION26暂时性的存储错误
ErrorCode::NOT_ENOUGH_REPLICAS_AFTER_APPEND_EXCEPTION28已写入服务端但 ISR 规模不足
ErrorCode::NOT_ENOUGH_REPLICAS_EXCEPTION29写入时 ISR 规模不足
ErrorCode::LEADER_NOT_AVAILABLE_EXCEPTION44该分区没有可用的 leader

客户端错误(ErrorCode::CLIENT_ERROR,错误码 -2)从 IsRetriable() 始终返回 false。

常见错误场景 ​

连接被拒绝 ​

集群未运行或地址不正确:

cpp
fluss::Configuration config;
config.bootstrap_servers = "127.0.0.1:9123";
fluss::Connection conn;
fluss::Result result = fluss::Connection::Create(config, conn);
if (!result.Ok()) {
    // "Connection refused" or timeout error
    std::cerr << "Cannot connect to cluster: " << result.error_message << std::endl;
}

表未找到 ​

尝试访问一个不存在的表:

cpp
fluss::Table table;
fluss::Result result = conn.GetTable(fluss::TablePath("fluss", "nonexistent"), table);
if (!result.Ok()) {
    if (result.error_code == fluss::ErrorCode::TABLE_NOT_EXIST) {
        std::cerr << "Table not found" << std::endl;
    }
}

分区未找到 ​

当 writer_dynamic_create_partition_enabled 设为 false 时,写入一个不存在的分区会失败:

cpp
auto row = table.NewRow();
row.Set("user_id", 1);
row.Set("region", "US");
row.Set("score", static_cast<int64_t>(100));
fluss::WriteResult wr;
fluss::Result result = writer.Upsert(row, wr);
if (result.Ok()) {
    result = wr.Wait();
}
if (!result.Ok() && result.error_code == fluss::ErrorCode::PARTITION_NOT_EXISTS) {
    std::cerr << "Partition not found, create partitions before writing" << std::endl;
}

认证失败 ​

SASL 凭据不正确或用户不存在:

cpp
fluss::Configuration config;
config.bootstrap_servers = "127.0.0.1:9123";
config.security_protocol = "sasl";
config.security_sasl_username = "admin";
config.security_sasl_password = "wrong-password";

fluss::Connection conn;
fluss::Result result = fluss::Connection::Create(config, conn);
if (!result.Ok()) {
    if (result.error_code == fluss::ErrorCode::AUTHENTICATE_EXCEPTION) {
        std::cerr << "Authentication failed: " << result.error_message << std::endl;
    }
}

Schema 不匹配 ​

写入时使用了错误的类型或列索引:

cpp
fluss::GenericRow row;
// Setting wrong type for a column will result in an error
// when the row is sent to the server
row.SetString(0, "not_an_integer");  // Column 0 expects Int
fluss::Result result = writer.Append(row);
if (!result.Ok()) {
    std::cerr << "Schema mismatch: " << result.error_message << std::endl;
}

最佳实践 ​

  1. 始终检查 Result:切勿忽略返回 Result 的操作的返回值。
  2. 优雅地处理错误:记录错误并重试或优雅地失败,而不是崩溃。
  3. 验证连接状态:在操作前使用 Available() 检查连接有效性。
  4. 关闭动态创建时写入前先创建分区:当 writer_dynamic_create_partition_enabled 设为 false 时,写入不存在的分区会以 PARTITION_NOT_EXISTS 失败。

Fluss 中文文档