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 4 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

I would like to work on this one, can any maintainer assign this issue to me?

anishagg17 avatar May 17 '21 13:05 anishagg17

It seems that rust::cxxbridge1::Error has LeaderNotFound as an instance, have you tried rust::cxxbridge1::LeaderNotFound as a type?

anishagg17 avatar May 17 '21 15:05 anishagg17

@anishagg17 As described in the cxx tutorial, any errors from Rust will be wrapped into an instance of rust::Error provided by cxx, which distinguishes errors only by the message string. I'd like to add custom error types that inherit std::exception and reflect the errors in the rust client. This requires us to return a custom FFI-Result instead of the std::result::Result which should simply be a struct containing an error message and an error code.

andylokandy avatar May 18 '21 03:05 andylokandy

Hi, @andylokandy can you please share your environment specifications?

I have macOS, the make command failed with clang as it wasn't able to link ssl and crypto, then I added the paths to include ssl and crypto library but didn't work

c++ $(cur_makefile_path)/example/main.cpp -o $(cur_makefile_path)/target/tikv-example -std=c++17 -g -I$(cur_makefile_path)/include -L$(cur_makefile_path)/target/debug -ltikv_client -lpthread -ldl -L/usr/local/Cellar/[email protected]/1.1.1k/lib -L/usr/local/opt/[email protected]/lib -I/usr/local/Cellar/[email protected]/1.1.1k/lib -I/usr/local/opt/[email protected]/include/openssl -lssl -lcrypto  -v

I even tried gcc11 but none works.

anishagg17 avatar May 19 '21 18:05 anishagg17

What is the error message it outputs?

andylokandy avatar May 19 '21 19:05 andylokandy

clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [target/tikv-example] Error 1

anishagg17 avatar May 19 '21 19:05 anishagg17

Try using the openssl from homebrew: https://formulae.brew.sh/formula/openssh

andylokandy avatar May 19 '21 19:05 andylokandy

Try using the openssl from homebrew: https://formulae.brew.sh/formula/openssh

still the same error, I have installed openssl previously also. The link you provided is for openssh, I installed it and ran into same error

anishagg17 avatar May 19 '21 19:05 anishagg17

I am using WSL2 with distro as Ubuntu and the tiup playground nightly runs fine. But when I am trying to run make run-example it says failed to run custom build command for tikv-client-proto v0.0.0 (https://github.com/tikv/client-rust.git?rev=3fad149#3fad149f)``.

Caused by: process didn't exit successfully: /mnt/c/Users/JATIN/Desktop/Rust/client-cpp/target/debug/build/tikv-client-proto-5d1618a7041c86b4/build-script-build (exit code: 101) --- stderr thread 'main' panicked at 'Could not create file: Os { code: 36, kind: Other, message: "File name too long" }', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/protobuf-build-0.12.0/src/wrapper.rs:41:58
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace warning: build failed, waiting for other jobs to finish... Building [===================================================> ] 238/248: grpcio-sys(build)

j619s avatar May 20 '21 05:05 j619s

I am using WSL2 with distro as Ubuntu and the tiup playground nightly runs fine. But when I am trying to run make run-example it says tikv-client-proto v0.0.0 (https://github.com/tikv/client-rust.git?rev=3fad149#3fad149f)``%60%60).failed to run custom build command for

Caused by: process didn't exit successfully: (exit code: 101) --- stderr thread 'main' panicked at 'Could not create file: Os { code: 36, kind: Other, message: "File name too long" }', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/protobuf-build-0.12.0/src/wrapper.rs:41:58 note: run with environment variable to display a backtrace warning: build failed, waiting for other jobs to finish... Building [===================================================> ] 238/248: grpcio-sys(build)/mnt/c/Users/JATIN/Desktop/Rust/client-cpp/target/debug/build/tikv-client-proto-5d1618a7041c86b4/build-script-build``RUST_BACKTRACE=1

I think it's a separate problem, perhaps you should open another issue to discuss it

Smityz avatar Jun 20 '23 06:06 Smityz