client-cpp icon indicating copy to clipboard operation
client-cpp copied to clipboard

Question: How to handle error thrown by TiKV

Open JaySon-Huang opened this issue 3 years ago • 10 comments

## Setup a cluster by TiUP playground
client-cpp>  tiup playground --nightly

## Then run the example program of `tikv/client-cpp`
client-cpp> make run-example
RUST_LOG=debug /data3/my/client-cpp/target/tikv-example
[2021-03-02T14:29:15Z INFO  tikv_client_common::security] connect to rpc server at endpoint: "127.0.0.1:2379"
[2021-03-02T14:29:15Z WARN  tikv_client_pd::cluster] PD endpoint 127.0.0.1:2379 failed to respond: Grpc(RpcFailure(RpcStatus { status: 14-UNAVAILABLE, details: Some("failed to connect to all addresses") }))
terminate called after throwing an instance of 'rust::cxxbridge1::Error'
  what():  [/root/.cargo/git/checkouts/client-rust-5a1ccd35a54db20f/89ac804/tikv-client-pd/src/cluster.rs:174]: PD cluster failed to respond
make: *** [run-example] Aborted

client-cpp>  make run-example
RUST_LOG=debug /data3/my/client-cpp/target/tikv-example
[2021-03-02T14:30:34Z INFO  tikv_client_common::security] connect to rpc server at endpoint: "127.0.0.1:2379"
[2021-03-02T14:30:34Z INFO  tikv_client_pd::cluster] All PD endpoints are consistent: ["127.0.0.1:2379"]
[2021-03-02T14:30:34Z INFO  tikv_client_common::security] connect to rpc server at endpoint: "http://127.0.0.1:2379"
[2021-03-02T14:30:34Z INFO  tikv_client_common::security] connect to rpc server at endpoint: "http://127.0.0.1:2379"
get key k1:v2
terminate called after throwing an instance of 'rust::cxxbridge1::Error'
  what():  Leader of region 2 is not found
make: *** [run-example] Aborted

I try to run the example program while setting up a tiup-playground cluster, it throws some errors.

For example, if I want to write some code to handle leader not found error, now I have to write it like

    auto txn = client.begin();

    txn.put("k1", "v2");

    bool success = false;
    while (!success) {
        try {
            txn.commit();
            success = true;
        } catch (rust::cxxbridge1::Error &e) {
            // Ignore Leader not found and try to commit again
            std::string err_msg = e.what();
            if (err_msg.find("Leader of region") == std::string::npos) {
                // Other error, throw exception
                throw;
            }
        }
    }

Can client-cpp throw different kind of Exception? So I can write codes like:

    auto txn = client.begin();

    txn.put("k1", "v2");

    bool success = false;
    while (!success) {
        try {
            txn.commit();
            success = true;
        } catch (tikv_client::LeaderNotFoundException &e) {
            // Ignore Leader not found and try to commit again
        }
    }

JaySon-Huang avatar Mar 02 '21 14:03 JaySon-Huang