Derecho  0.9
Distributed systems toolkit for RDMA
test.cpp
Go to the documentation of this file.
2 #include <derecho/conf/conf.hpp>
3 #include <functional>
4 #include <iostream>
5 #include <sstream>
6 
7 #define NUM_APP_ARGS (1)
8 
9 int main(int argc, char** argv) {
10  if((argc < (NUM_APP_ARGS + 1)) || ((argc > (NUM_APP_ARGS + 1)) && strcmp("--", argv[argc - NUM_APP_ARGS - 1]))) {
11  std::cerr << "Usage: " << argv[0] << " [ derecho-config-list -- ] <aio|bio>" << std::endl;
12  return -1;
13  }
14 
15  bool use_aio = false;
16  if(strcmp("aio", argv[argc - NUM_APP_ARGS]) == 0) {
17  use_aio = true;
18  } else if(strcmp("bio", argv[1]) != 0) {
19  std::cerr << "unrecognized argument:" << argv[argc - NUM_APP_ARGS] << ". Using bio (blocking io) instead." << std::endl;
20  }
21 
22  derecho::Conf::initialize(argc, argv);
23  std::cout << "Starting object store service..." << std::endl;
24  // oss - objectstore service
26  [&](const objectstore::OID& oid, const objectstore::Object& object) {
27  std::cout << "watcher: " << oid << "->" << object << std::endl;
28  });
29  // print some message
30  std::cout << "Object store service started. \n\tIs replica:" << std::boolalpha << oss.isReplica()
31  << "\n\tUsing aio API:" << use_aio
32  << std::noboolalpha << "." << std::endl;
33 
34  bool bNextCommand = true; // waiting for the next command.
35 
36  // prepare the commandline tool:
37  std::map<std::string, std::pair<std::string, std::function<bool(std::string&)>>> commands = {
38  {"put", // command
39  {
40  "put <oid> <string>", // help info
41  [&oss, use_aio](std::string args) -> bool {
42  std::istringstream ss(args);
43  std::string oid, odata;
44  ss >> oid >> odata;
45  objectstore::Object object(std::stol(oid), odata.c_str(), odata.length() + 1);
46  try {
47  if(use_aio) {
48  // asynchronous api
50  decltype(results)::ReplyMap& replies = results.get();
51  std::cout << "aio returns:" << std::endl;
52  for(auto& reply_pair : replies) {
53  std::tuple<persistent::version_t,uint64_t> res = reply_pair.second.get();
54  std::cout << reply_pair.first << ": version=" << std::get<0>(res)
55  << ", timestamp=" << std::get<1>(res) << std::endl;
56  }
57  } else {
58  std::tuple<persistent::version_t,uint64_t> result = oss.bio_put(object);
59  // synchronous api
60  std::cout << "version:" << std::get<0>(result)
61  << ", timestamp:" << std::get<1>(result) << std::endl;
62  }
63  } catch(...) {
64  return false;
65  }
66  return true;
67  }}}, // put
68  {"get", // command
69  {
70  "get <oid> [version]", // help info
71  [&oss, use_aio](std::string& args) -> bool {
72  char* argcopy = strdup(args.c_str());
73  char* token = std::strtok(argcopy, " ");
74  uint64_t oid = std::stol(token);
76  if((token = std::strtok(nullptr, " ")) != nullptr) {
77  ver = std::stol(token);
78  }
79  try {
80  if(use_aio) {
81  // asynchronous api
82  derecho::rpc::QueryResults<const objectstore::Object> results = oss.aio_get(oid, ver);
83  decltype(results)::ReplyMap& replies = results.get();
84  std::cout << "aio returns:" << std::endl;
85  for(auto& reply_pair : replies) {
86  std::cout << reply_pair.first << ":" << reply_pair.second.get() << std::endl;
87  }
88  } else {
89  // synchronous api
90  objectstore::Object obj = oss.bio_get(oid, ver);
91  std::cout << obj << std::endl;
92  }
93  } catch(...) {
94  free(argcopy);
95  return false;
96  }
97  free(argcopy);
98  return true;
99  }}},
100  {"tget",
101  {
102  "tget <oid> <unix time in us>", // help info
103  [&oss, use_aio](std::string& args) -> bool {
104  std::istringstream ss(args);
105  objectstore::OID oid;
106  uint64_t ts_us;
107  ss >> oid >> ts_us;
108  try {
109  if(use_aio) {
110  // asynchronous api
111  derecho::rpc::QueryResults<const objectstore::Object> results = oss.aio_get(oid,ts_us);
112  decltype(results)::ReplyMap& replies = results.get();
113  std::cout << "aio returns:" << std::endl;
114  for(auto& reply_pair : replies) {
115  std::cout << reply_pair.first << ":" << reply_pair.second.get() << std::endl;
116  }
117  } else {
118  // synchronous api
119  objectstore::Object obj = oss.bio_get(oid, ts_us);
120  std::cout << obj << std::endl;
121  }
122  } catch(...) {
123  return false;
124  }
125  return true;
126  }}},
127  {"remove", // command
128  {
129  "remove <oid>", // help info
130  [&oss, use_aio](std::string& args) -> bool {
131  try {
132  if(use_aio) {
133  // asynchronous api
134  derecho::rpc::QueryResults<std::tuple<version_t,uint64_t>> results = oss.aio_remove(std::stol(args));
135  decltype(results)::ReplyMap& replies = results.get();
136  std::cout << "aio returns:" << std::endl;
137  for(auto& reply_pair : replies) {
138  std::tuple<version_t,uint64_t> res = reply_pair.second.get();
139  std::cout << reply_pair.first << ": version=" << std::get<0>(res)
140  << ", timestamp=" << std::get<1>(res) << std::endl;
141  }
142  } else {
143  std::tuple<version_t,uint64_t> ver = oss.bio_remove(std::stol(args));
144  std::cout << "version:" << std::get<0>(ver)
145  << ", timestamp:" << std::get<1>(ver) << std::endl;
146  }
147  } catch(...) {
148  return false;
149  }
150  return true;
151  }}},
152  {"leave", // command
153  {
154  "leave", // help info
155  [&oss, &bNextCommand](std::string&) -> bool {
156  oss.leave();
157  bNextCommand = false;
158  return true;
159  }}}};
160 
161  std::function<void()> help = [commands]() {
162  std::cout << "Commands:" << std::endl;
163  for(auto& cmd_entry : commands) {
164  std::cout << "\t" << cmd_entry.second.first << std::endl;
165  }
166  };
167 
168  help();
169  // main command loop
170  while(bNextCommand) {
171  std::string line;
172  std::cout << "cmd>";
173  std::getline(std::cin, line, '\n');
174  std::string::size_type first_space_pos = line.find(' ');
175  std::string command;
176  std::string arguments;
177  if(first_space_pos == std::string::npos) {
178  command = line;
179  } else {
180  command = line.substr(0, first_space_pos);
181  arguments = line.substr(first_space_pos + 1);
182  }
183  if(commands.find(command) == commands.end()) {
184  help();
185  } else {
186  bool bRet = commands[command].second(arguments);
187  std::cout << "Result:" << std::boolalpha << bRet << std::noboolalpha
188  << std::endl;
189  }
190  }
191  return 0;
192 }
static IObjectStoreService & getObjectStoreService(int argc, char **argv, const ObjectWatcher &ow={})
virtual const bool isReplica()=0
int argc
static void initialize(int argc, char *argv[], const char *conf_file=nullptr)
Definition: conf.cpp:67
char ** argv
int main(int argc, char **argv)
Definition: test.cpp:5
#define INVALID_VERSION
Definition: PersistLog.hpp:28
uint64_t OID
Definition: Object.hpp:70
#define NUM_APP_ARGS
Definition: test.cpp:7
Data structure that (indirectly) holds a set of futures for a single RPC function call; there is one ...
Definition: rpc_utils.hpp:158
ReplyMap & get()
Block until the ReplyMap is fulfilled, then return the map by reference.
Definition: rpc_utils.hpp:235