Crude DeleteV3, version 2 backward compatibility, implementation
This commit is contained in:
parent
326693a95e
commit
66f9be45ba
|
|
@ -42,7 +42,11 @@ namespace etcd
|
||||||
*/
|
*/
|
||||||
pplx::task<Response> set(std::string const & key, std::string const & value);
|
pplx::task<Response> set(std::string const & key, std::string const & value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FBDL temporary set and get items to etcd v3
|
||||||
|
*/
|
||||||
void setv3(std::string const&, std::string const&);
|
void setv3(std::string const&, std::string const&);
|
||||||
|
void getv3(std::string const&);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new key and sets it's value. Fails if the key already exists.
|
* Creates a new key and sets it's value. Fails if the key already exists.
|
||||||
|
|
@ -139,6 +143,7 @@ namespace etcd
|
||||||
pplx::task<Response> send_get_request(web::http::uri_builder & uri);
|
pplx::task<Response> send_get_request(web::http::uri_builder & uri);
|
||||||
pplx::task<Response> send_del_request(web::http::uri_builder & uri);
|
pplx::task<Response> send_del_request(web::http::uri_builder & uri);
|
||||||
pplx::task<Response> send_put_request(web::http::uri_builder & uri, std::string const & key, std::string const & value);
|
pplx::task<Response> send_put_request(web::http::uri_builder & uri, std::string const & key, std::string const & value);
|
||||||
|
pplx::task<Response> removeEntry(std::string const &);
|
||||||
|
|
||||||
web::http::client::http_client client;
|
web::http::client::http_client client;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* DeleteRpcResponse.h
|
||||||
|
*
|
||||||
|
* Created on: Jun 5, 2016
|
||||||
|
* Author: ubuntu
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SRC_DELETERPCRESPONSE_H_
|
||||||
|
#define SRC_DELETERPCRESPONSE_H_
|
||||||
|
|
||||||
|
#include "etcd/Response.hpp"
|
||||||
|
#include "proto/rpc.grpc.pb.h"
|
||||||
|
|
||||||
|
namespace etcd {
|
||||||
|
class DeleteRpcResponse : public etcd::Response, public etcd::Value {
|
||||||
|
public:
|
||||||
|
DeleteRpcResponse();
|
||||||
|
virtual ~DeleteRpcResponse();
|
||||||
|
void fillUpV2ResponseValues(etcdserverpb::RangeResponse);
|
||||||
|
|
||||||
|
etcdserverpb::DeleteRangeResponse deleteResponse;
|
||||||
|
grpc::Status status;
|
||||||
|
grpc::ClientContext context;
|
||||||
|
grpc::CompletionQueue completionQueue;
|
||||||
|
std::unique_ptr<grpc::ClientAsyncResponseReader<etcdserverpb::DeleteRangeResponse>> rpcInstance;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif /* SRC_DELETERPCRESPONSE_H_ */
|
||||||
|
|
@ -41,6 +41,7 @@ namespace etcd
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class Response;
|
friend class Response;
|
||||||
|
friend class DeleteRpcResponse;
|
||||||
Value();
|
Value();
|
||||||
Value(web::json::value const & json_value);
|
Value(web::json::value const & json_value);
|
||||||
std::string _key;
|
std::string _key;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
add_library(etcd-cpp-api SHARED ../proto/kv.pb.cc ../proto/auth.pb.cc ../proto/rpc.pb.cc ../proto/rpc.grpc.pb.cc Client.cpp Response.cpp Value.cpp json_constants.cpp)
|
add_library(etcd-cpp-api SHARED ../proto/kv.pb.cc ../proto/auth.pb.cc ../proto/rpc.pb.cc ../proto/rpc.grpc.pb.cc Client.cpp Response.cpp Value.cpp json_constants.cpp DeleteRpcResponse.cpp)
|
||||||
set_property(TARGET etcd-cpp-api PROPERTY CXX_STANDARD 11)
|
set_property(TARGET etcd-cpp-api PROPERTY CXX_STANDARD 11)
|
||||||
|
|
||||||
target_link_libraries(etcd-cpp-api ${CPPREST_LIB} boost_system ssl crypto protobuf grpc++)
|
target_link_libraries(etcd-cpp-api ${CPPREST_LIB} boost_system ssl crypto protobuf grpc++)
|
||||||
|
|
|
||||||
110
src/Client.cpp
110
src/Client.cpp
|
|
@ -1,6 +1,7 @@
|
||||||
#include "etcd/Client.hpp"
|
#include "etcd/Client.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "etcd/DeleteRpcResponse.h"
|
||||||
|
|
||||||
etcd::Client::Client(std::string const & address)
|
etcd::Client::Client(std::string const & address)
|
||||||
: client(address)
|
: client(address)
|
||||||
|
|
@ -97,16 +98,107 @@ pplx::task<etcd::Response> etcd::Client::modify_if(std::string const & key, std:
|
||||||
return send_put_request(uri, "value", value);
|
return send_put_request(uri, "value", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pplx::task<etcd::Response> etcd::Client::removeEntry(std::string const & entryKey) {
|
||||||
|
|
||||||
|
etcd::DeleteRpcResponse *drp = new etcd::DeleteRpcResponse();
|
||||||
|
|
||||||
|
//get muna
|
||||||
|
std::cout<<"blocking call for get rpc first " << entryKey << std::endl;
|
||||||
|
etcdserverpb::RangeRequest rangeRequest;
|
||||||
|
rangeRequest.set_key(entryKey);
|
||||||
|
|
||||||
|
etcdserverpb::RangeResponse rangeResponse;
|
||||||
|
grpc::ClientContext context;
|
||||||
|
|
||||||
|
grpc::Status status = stub_->Range(&context, rangeRequest, &rangeResponse);
|
||||||
|
|
||||||
|
std::cout << "checking status" << std::endl;
|
||||||
|
if(status.ok()) {
|
||||||
|
std::cout << "get OK" << std::endl;
|
||||||
|
std::cout << "size: " << rangeResponse.kvs_size() << std::endl;
|
||||||
|
std::cout << "kvs 0 key: " << rangeResponse.kvs(0).key() << std::endl;
|
||||||
|
std::cout << "kvs 0 value: " << rangeResponse.kvs(0).value() << std::endl;
|
||||||
|
std::cout << "kvs.Get 0 value: " << rangeResponse.kvs().Get(0).value() << std::endl;
|
||||||
|
|
||||||
|
drp->fillUpV2ResponseValues(rangeResponse);
|
||||||
|
std::cout << "this drp object has now a previous value of: " << drp->as_string() << std::endl;
|
||||||
|
std::cout << "checking for check" << std::endl;
|
||||||
|
std::cout << "resp.prev_value().as_string: " << drp->prev_value().as_string() << std::endl;
|
||||||
|
std::cout << "resp.action(): " << drp->action() << std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "get NOK" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//then delete
|
||||||
|
std::cout << "removing etcd v3 entry naman" << std::endl;
|
||||||
|
|
||||||
|
etcdserverpb::DeleteRangeRequest deleteRangeRequest;
|
||||||
|
deleteRangeRequest.set_key(entryKey);
|
||||||
|
|
||||||
|
drp->rpcInstance = stub_->AsyncDeleteRange(&drp->context, deleteRangeRequest, &drp->completionQueue);
|
||||||
|
drp->rpcInstance->Finish(&drp->deleteResponse, &drp->status, (void*)drp);
|
||||||
|
|
||||||
|
return pplx::task<etcd::Response>([drp]()
|
||||||
|
{
|
||||||
|
std::cout << "doing delete v3 entry task" << std::endl;
|
||||||
|
void* got_tag;
|
||||||
|
bool ok = false;
|
||||||
|
etcd::Response resp;
|
||||||
|
|
||||||
|
drp->completionQueue.Next(&got_tag, &ok);
|
||||||
|
GPR_ASSERT(got_tag == (void*)drp);
|
||||||
|
GPR_ASSERT(ok);
|
||||||
|
|
||||||
|
etcd::DeleteRpcResponse* deleteResponse = static_cast<etcd::DeleteRpcResponse*>(got_tag);
|
||||||
|
|
||||||
|
if (deleteResponse->status.ok()){
|
||||||
|
std::cout << "doing delete v3 entry task OK" << std::endl;
|
||||||
|
resp = *drp; //simply stripping off the response part
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cout << "doing delete v3 entry task NOK" << std::endl;
|
||||||
|
delete deleteResponse;
|
||||||
|
std::cout << "delete done, returning" << std::endl;
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void etcd::Client::getv3(std::string const & key) {
|
||||||
|
std::cout<<"blocking call for get rpc " << key << std::endl;
|
||||||
|
etcdserverpb::RangeRequest rangeRequest;
|
||||||
|
rangeRequest.set_key(key);
|
||||||
|
|
||||||
|
etcdserverpb::RangeResponse rangeResponse;
|
||||||
|
grpc::ClientContext context;
|
||||||
|
|
||||||
|
grpc::Status status = stub_->Range(&context, rangeRequest, &rangeResponse);
|
||||||
|
|
||||||
|
std::cout << "checking status" << std::endl;
|
||||||
|
if(status.ok()) {
|
||||||
|
std::cout << "get OK" << std::endl;
|
||||||
|
std::cout << "size: " << rangeResponse.kvs_size() << std::endl;
|
||||||
|
std::cout << "kvs 0 key: " << rangeResponse.kvs(0).key() << std::endl;
|
||||||
|
std::cout << "kvs 0 value: " << rangeResponse.kvs(0).value() << std::endl;
|
||||||
|
std::cout << "kvs.Get 0 value: " << rangeResponse.kvs().Get(0).value() << std::endl;
|
||||||
|
|
||||||
|
DeleteRpcResponse drp;
|
||||||
|
drp.fillUpV2ResponseValues(rangeResponse);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "get NOK" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pplx::task<etcd::Response> etcd::Client::rm(std::string const & key)
|
pplx::task<etcd::Response> etcd::Client::rm(std::string const & key)
|
||||||
{
|
{
|
||||||
std::cout << "FBDL: rm is invoked" << std::endl;
|
std::cout << "FBDL: handling rm for key: " << key << std::endl;
|
||||||
web::http::uri_builder uri("/v2/keys" + key); // /v2/keys/test/key1
|
return removeEntry(key);
|
||||||
std::cout << "FBDL url: " << uri.to_string() << std::endl;
|
|
||||||
|
|
||||||
uri.append_query("dir=false"); // /v2/keys/test/key1?dir=false
|
|
||||||
std::cout << "FBDL url after append query: " << uri.to_string() << std::endl;
|
|
||||||
|
|
||||||
return Response::create(client.request("DELETE", uri.to_string()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pplx::task<etcd::Response> etcd::Client::rm_if(std::string const & key, std::string const & old_value)
|
pplx::task<etcd::Response> etcd::Client::rm_if(std::string const & key, std::string const & old_value)
|
||||||
|
|
@ -165,3 +257,5 @@ pplx::task<etcd::Response> etcd::Client::watch(std::string const & key, int from
|
||||||
uri.append_query("recursive=true");
|
uri.append_query("recursive=true");
|
||||||
return send_get_request(uri);
|
return send_get_request(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* DeleteRpcResponse.cpp
|
||||||
|
*
|
||||||
|
* Created on: Jun 5, 2016
|
||||||
|
* Author: ubuntu
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "etcd/DeleteRpcResponse.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
etcd::DeleteRpcResponse::DeleteRpcResponse() {
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
etcd::DeleteRpcResponse::~DeleteRpcResponse() {
|
||||||
|
// TODO Auto-generated destructor stub
|
||||||
|
std::cout << "destroyinh delete response" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
//prototype in handling the response object
|
||||||
|
void etcd::DeleteRpcResponse::fillUpV2ResponseValues(etcdserverpb::RangeResponse getResponse) {
|
||||||
|
std::cout << "inside fillup attempting fillupv2 response values" << std::endl;
|
||||||
|
|
||||||
|
std::cout << "setting the value of: " << getResponse.kvs().Get(0).value() << std::endl;
|
||||||
|
_prev_value.value = getResponse.kvs().Get(0).value();
|
||||||
|
_action = "delete";
|
||||||
|
|
||||||
|
// //the experiments done on how to fill up the field of this object.
|
||||||
|
// //access the fields na protected sa Value
|
||||||
|
// this->Value::value = "yeah";
|
||||||
|
// std::cout << "pre contents: " << this->_value.as_string() << std::endl;
|
||||||
|
//
|
||||||
|
// //or create an instance here and return it filled up
|
||||||
|
// DeleteRpcResponse r;
|
||||||
|
// r.Value::value = "yo!";
|
||||||
|
// this->_value = r; //strip off value part
|
||||||
|
//
|
||||||
|
// std::cout << "contents: " << this->_value.as_string() << std::endl;
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,9 @@
|
||||||
|
|
||||||
pplx::task<etcd::Response> etcd::Response::create(pplx::task<web::http::http_response> response_task)
|
pplx::task<etcd::Response> etcd::Response::create(pplx::task<web::http::http_response> response_task)
|
||||||
{
|
{
|
||||||
std::cout << "FBDL Response create" << std::endl;
|
|
||||||
return pplx::task<etcd::Response> (
|
return pplx::task<etcd::Response> (
|
||||||
[response_task]()
|
[response_task]()
|
||||||
{
|
{
|
||||||
std::cout << "FBDL inside response task" << std::endl;
|
|
||||||
auto json_task = response_task.get().extract_json();
|
auto json_task = response_task.get().extract_json();
|
||||||
return etcd::Response(response_task.get(), json_task.get());
|
return etcd::Response(response_task.get(), json_task.get());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ TEST_CASE("simplified read")
|
||||||
{
|
{
|
||||||
etcd::Client etcd("http://127.0.0.1:4001");
|
etcd::Client etcd("http://127.0.0.1:4001");
|
||||||
CHECK("42" == etcd.get("/test/key1").get().value().as_string());
|
CHECK("42" == etcd.get("/test/key1").get().value().as_string());
|
||||||
|
std::cout << "get error code kahit success: " << etcd.get("/test/key1").get().error_code() << std::endl;
|
||||||
CHECK(100 == etcd.get("/test/key2").get().error_code()); // Key not found
|
CHECK(100 == etcd.get("/test/key2").get().error_code()); // Key not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,29 +57,41 @@ TEST_CASE("modify a key")
|
||||||
CHECK("43" == etcd.modify("/test/key1", "42").get().prev_value().as_string());
|
CHECK("43" == etcd.modify("/test/key1", "42").get().prev_value().as_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("set a key")
|
//TEST_CASE("set a key")
|
||||||
{
|
//{
|
||||||
etcd::Client etcd("http://127.0.0.1:4001");
|
// etcd::Client etcd("http://127.0.0.1:4001");
|
||||||
etcd::Response resp = etcd.set("/test/key1", "43").get();
|
// etcd::Response resp = etcd.set("/test/key1", "43").get();
|
||||||
REQUIRE(0 == resp.error_code()); // overwrite
|
// REQUIRE(0 == resp.error_code()); // overwrite
|
||||||
CHECK("set" == resp.action());
|
// CHECK("set" == resp.action());
|
||||||
CHECK(0 == etcd.set("/test/key2", "43").get().error_code()); // create new
|
// CHECK(0 == etcd.set("/test/key2", "43").get().error_code()); // create new
|
||||||
CHECK("43" == etcd.set("/test/key2", "44").get().prev_value().as_string());
|
// CHECK("43" == etcd.set("/test/key2", "44").get().prev_value().as_string());
|
||||||
CHECK("" == etcd.set("/test/key3", "44").get().prev_value().as_string());
|
// CHECK("" == etcd.set("/test/key3", "44").get().prev_value().as_string());
|
||||||
CHECK(102 == etcd.set("/test", "42").get().error_code()); // Not a file
|
// CHECK(102 == etcd.set("/test", "42").get().error_code()); // Not a file
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
|
//FBDL rm naman
|
||||||
|
//TEST_CASE("delete a value")
|
||||||
|
//{
|
||||||
|
// std::cout << "FBDL do a delete via rm only" <<std::endl;
|
||||||
|
// etcd::Client etcd("http://127.0.0.1:4001");
|
||||||
|
// CHECK(3 == etcd.ls("/test").get().keys().size());
|
||||||
|
//
|
||||||
|
// std::cout << "FBDL invoking rm in test" <<std::endl;
|
||||||
|
// etcd::Response resp = etcd.rm("/test/key1").get();
|
||||||
|
// CHECK("43" == resp.prev_value().as_string());
|
||||||
|
// CHECK("delete" == resp.action());
|
||||||
|
// CHECK(2 == etcd.ls("/test").get().keys().size());
|
||||||
|
//}
|
||||||
|
|
||||||
TEST_CASE("delete a value")
|
TEST_CASE("delete a value V3")
|
||||||
{
|
{
|
||||||
std::cout << "FBDL do a delete via rm only" <<std::endl;
|
std::cout << "FBDL do a delete via rm only V3" <<std::endl;
|
||||||
etcd::Client etcd("http://127.0.0.1:4001");
|
etcd::Client etcd("http://127.0.0.1:4001");
|
||||||
CHECK(3 == etcd.ls("/test").get().keys().size());
|
etcd.setv3("test/key1", "42"); //temporary get
|
||||||
|
|
||||||
std::cout << "FBDL invoking rm in test" <<std::endl;
|
etcd::Response resp = etcd.rm("test/key1").get();
|
||||||
etcd::Response resp = etcd.rm("/test/key1").get();
|
CHECK("42" == resp.prev_value().as_string());
|
||||||
CHECK("43" == resp.prev_value().as_string());
|
|
||||||
CHECK("delete" == resp.action());
|
CHECK("delete" == resp.action());
|
||||||
CHECK(2 == etcd.ls("/test").get().keys().size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("create a directory")
|
TEST_CASE("create a directory")
|
||||||
|
|
@ -136,26 +149,26 @@ TEST_CASE("wait for a value change")
|
||||||
}
|
}
|
||||||
|
|
||||||
//FBDL: first watch
|
//FBDL: first watch
|
||||||
TEST_CASE("FBDL wait for a value change")
|
//TEST_CASE("FBDL wait for a value change")
|
||||||
{
|
//{
|
||||||
std::cout << "FBDL wait for a value change" << std::endl;
|
// std::cout << "FBDL wait for a value change" << std::endl;
|
||||||
etcd::Client etcd("http://127.0.0.1:4001");
|
// etcd::Client etcd("http://127.0.0.1:4001");
|
||||||
// etcd.set("/test/key1", "42").wait();
|
//// etcd.set("/test/key1", "42").wait();
|
||||||
etcd.setv3("test/key1", "42");
|
// etcd.setv3("test/key1", "42");
|
||||||
|
|
||||||
// pplx::task<etcd::Response> res = etcd.watch("/test/key1");
|
|
||||||
// CHECK(!res.is_done());
|
|
||||||
// sleep(1);
|
|
||||||
// CHECK(!res.is_done());
|
|
||||||
// CHECK("42" == etcd.get("/test/key1").get().value().as_string());
|
|
||||||
//
|
//
|
||||||
// etcd.set("/test/key1", "43").get();
|
//// pplx::task<etcd::Response> res = etcd.watch("/test/key1");
|
||||||
// sleep(1);
|
//// CHECK(!res.is_done());
|
||||||
// REQUIRE(res.is_done());
|
//// sleep(1);
|
||||||
// REQUIRE("set" == res.get().action());
|
//// CHECK(!res.is_done());
|
||||||
// CHECK("43" == res.get().value().as_string());
|
//// CHECK("42" == etcd.get("/test/key1").get().value().as_string());
|
||||||
// CHECK("43" == etcd.get("/test/key1").get().value().as_string());
|
////
|
||||||
}
|
//// etcd.set("/test/key1", "43").get();
|
||||||
|
//// sleep(1);
|
||||||
|
//// REQUIRE(res.is_done());
|
||||||
|
//// REQUIRE("set" == res.get().action());
|
||||||
|
//// CHECK("43" == res.get().value().as_string());
|
||||||
|
//// CHECK("43" == etcd.get("/test/key1").get().value().as_string());
|
||||||
|
//}
|
||||||
|
|
||||||
TEST_CASE("wait for a directory change")
|
TEST_CASE("wait for a directory change")
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue