LogDevice API
Client.h
1 
8 #pragma once
9 
10 #include <chrono>
11 #include <functional>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 
17 #include "logdevice/include/AsyncReader.h"
18 #include "logdevice/include/ClientFactory.h"
19 #include "logdevice/include/ClientSettings.h"
20 #include "logdevice/include/ClusterAttributes.h"
21 #include "logdevice/include/ConfigSubscriptionHandle.h"
22 #include "logdevice/include/Err.h"
23 #include "logdevice/include/LogHeadAttributes.h"
24 #include "logdevice/include/LogTailAttributes.h"
25 #include "logdevice/include/LogsConfigTypes.h"
26 #include "logdevice/include/Reader.h"
27 #include "logdevice/include/Record.h"
28 #include "logdevice/include/types.h"
29 
37 namespace facebook { namespace logdevice {
38 
39 // Enum which is used to determine accuracy of findTime or findKey in order to
40 // get best accuracy-speed trade off for each specific use case. See
41 // findTimeSync() or findKeySync() for documentation about each accuracy mode.
42 enum class FindKeyAccuracy { STRICT, APPROXIMATE };
43 
44 // Struct which is used to provide the result of findKey. See findKey() for
45 // documentation about the returned values.
46 struct FindKeyResult {
47  Status status;
48  lsn_t lo;
49  lsn_t hi;
50 };
51 
52 // Enum which is used to determine accuracy of dataSize -- only approximate
53 // mode is currently supported.
54 enum class DataSizeAccuracy { APPROXIMATE, COUNT };
55 
68 typedef std::function<void(Status st, const DataRecord& r)> append_callback_t;
69 
76 typedef std::function<void(Status, lsn_t result)> find_time_callback_t;
77 
84 typedef std::function<void(FindKeyResult result)> find_key_callback_t;
85 
92 typedef std::function<void(Status, uint64_t result)> byte_offset_callback_t;
93 
100 typedef std::function<void(Status)> trim_callback_t;
101 
108 typedef std::function<void(Status status, bool empty)> is_empty_callback_t;
109 
116 typedef std::function<void(Status status, size_t size)> data_size_callback_t;
117 
124 typedef std::function<void(Status status, lsn_t)> get_tail_lsn_callback_t;
125 
132 typedef std::function<void(Status status, std::pair<logid_t, logid_t>)>
133  get_log_range_by_name_callback_t;
134 
141 typedef std::function<void(Status status, std::map<std::string, logid_range_t>)>
142  get_log_ranges_by_namespace_callback_t;
143 
149 typedef std::function<void()> config_update_callback_t;
150 
158 typedef std::function<void(Status status, std::unique_ptr<client::LogGroup>)>
159  get_log_group_callback_t;
160 
161 typedef std::function<void(Status status,
162  std::unique_ptr<client::LogGroup>,
163  const std::string& failure_reason)>
164  make_log_group_callback_t;
165 
166 typedef std::function<void(Status status,
167  std::unique_ptr<client::LogAttributes>)>
168  get_log_attributes_callback_t;
169 
170 typedef std::function<void(Status status, std::unique_ptr<client::Directory>)>
171  get_directory_callback_t;
172 
173 typedef std::function<void(Status status,
174  std::unique_ptr<client::Directory>,
175  const std::string& failure_reason)>
176  make_directory_callback_t;
177 
178 typedef std::function<
179  void(Status, uint64_t version, const std::string& failure_reason)>
180  logsconfig_status_callback_t;
181 
188 typedef std::function<void(Status status, std::unique_ptr<LogTailAttributes>)>
189  get_tail_attributes_callback_t;
190 
197 typedef std::function<void(Status status, std::unique_ptr<LogHeadAttributes>)>
198  get_head_attributes_callback_t;
199 
200 class Client {
201  public:
212  virtual ~Client() = default;
213 
286  virtual lsn_t
287  appendSync(logid_t logid,
288  std::string payload,
289  AppendAttributes attrs = AppendAttributes(),
290  std::chrono::milliseconds* timestamp = nullptr) noexcept = 0;
291 
308  virtual lsn_t
309  appendSync(logid_t logid,
310  PayloadGroup&& payload_group,
311  AppendAttributes attrs = AppendAttributes(),
312  std::chrono::milliseconds* timestamp = nullptr) noexcept = 0;
313 
332  virtual lsn_t
333  appendSync(logid_t logid,
334  const Payload& payload,
335  AppendAttributes attrs = AppendAttributes(),
336  std::chrono::milliseconds* timestamp = nullptr) noexcept = 0;
337 
375  virtual int append(logid_t logid,
376  std::string payload,
377  append_callback_t cb,
378  AppendAttributes attrs = AppendAttributes()) noexcept = 0;
379 
385  virtual int append(logid_t logid,
386  PayloadGroup&& payload_group,
387  append_callback_t cb,
388  AppendAttributes attrs = AppendAttributes()) noexcept = 0;
389 
395  virtual int append(logid_t logid,
396  const Payload& payload,
397  append_callback_t cb,
398  AppendAttributes attrs = AppendAttributes()) noexcept = 0;
399 
425  virtual std::unique_ptr<Reader>
426  createReader(size_t max_logs, ssize_t buffer_size = -1) noexcept = 0;
427 
432  virtual std::unique_ptr<AsyncReader>
433  createAsyncReader(ssize_t buffer_size = -1) noexcept = 0;
434 
439  virtual void setTimeout(std::chrono::milliseconds timeout) noexcept = 0;
440 
472  virtual int trimSync(logid_t logid, lsn_t lsn) noexcept = 0;
473 
482  virtual int trim(logid_t logid, lsn_t lsn, trim_callback_t cb) noexcept = 0;
483 
492  virtual void addWriteToken(std::string) noexcept = 0;
493 
570  virtual lsn_t
571  findTimeSync(logid_t logid,
572  std::chrono::milliseconds timestamp,
573  Status* status_out = nullptr,
574  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
575 
644  virtual FindKeyResult
645  findKeySync(logid_t logid,
646  std::string key,
647  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
648 
658  virtual int
659  findTime(logid_t logid,
660  std::chrono::milliseconds timestamp,
661  find_time_callback_t cb,
662  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
663 
672  virtual int
673  findKey(logid_t logid,
674  std::string key,
675  find_key_callback_t cb,
676  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
677 
704  virtual int isLogEmptySync(logid_t logid, bool* empty) noexcept = 0;
705 
715  virtual int isLogEmpty(logid_t logid, is_empty_callback_t cb) noexcept = 0;
716 
747  virtual int dataSizeSync(logid_t logid,
748  std::chrono::milliseconds start,
749  std::chrono::milliseconds end,
750  DataSizeAccuracy accuracy,
751  size_t* size) noexcept = 0;
767  virtual int dataSize(logid_t logid,
768  std::chrono::milliseconds start,
769  std::chrono::milliseconds end,
770  DataSizeAccuracy accuracy,
771  data_size_callback_t cb) noexcept = 0;
772 
810  virtual lsn_t getTailLSNSync(logid_t logid) noexcept = 0;
811 
821  virtual int getTailLSN(logid_t logid,
822  get_tail_lsn_callback_t cb) noexcept = 0;
823 
862  virtual std::unique_ptr<LogTailAttributes>
863  getTailAttributesSync(logid_t logid) noexcept = 0;
864 
874  virtual int getTailAttributes(logid_t logid,
875  get_tail_attributes_callback_t cb) noexcept = 0;
876 
895  virtual std::unique_ptr<LogHeadAttributes>
896  getHeadAttributesSync(logid_t logid) noexcept = 0;
897 
907  virtual int getHeadAttributes(logid_t logid,
908  get_head_attributes_callback_t cb) noexcept = 0;
934  virtual logid_range_t getLogRangeByName(const std::string& name) noexcept = 0;
935 
944  virtual void
945  getLogRangeByName(const std::string& name,
946  get_log_range_by_name_callback_t cb) noexcept = 0;
947 
952  virtual std::string getLogNamespaceDelimiter() noexcept = 0;
953 
974  virtual std::map<std::string, logid_range_t>
975  getLogRangesByNamespace(const std::string& ns) noexcept = 0;
976 
985  virtual void getLogRangesByNamespace(
986  const std::string& ns,
987  get_log_ranges_by_namespace_callback_t cb) noexcept = 0;
988 
1004  virtual std::unique_ptr<client::LogGroup>
1005  getLogGroupSync(const std::string& path) noexcept = 0;
1006 
1016  virtual void getLogGroup(const std::string& path,
1017  get_log_group_callback_t cb) noexcept = 0;
1018 
1034  virtual std::unique_ptr<client::LogGroup>
1035  getLogGroupByIdSync(const logid_t logid) noexcept = 0;
1036 
1050  virtual void getLogGroupById(const logid_t logid,
1051  get_log_group_callback_t cb) noexcept = 0;
1052 
1070  virtual int makeDirectory(const std::string& path,
1071  bool mk_intermediate_dirs,
1072  const client::LogAttributes& attrs,
1073  make_directory_callback_t cb) noexcept = 0;
1074 
1082  virtual std::unique_ptr<client::Directory> makeDirectorySync(
1083  const std::string& path,
1084  bool mk_intermediate_dirs = false,
1085  const client::LogAttributes& attrs = client::LogAttributes(),
1086  std::string* failure_reason = nullptr) noexcept = 0;
1087 
1102  virtual int removeDirectory(const std::string& path,
1103  bool recursive,
1104  logsconfig_status_callback_t) noexcept = 0;
1105 
1115  virtual bool removeDirectorySync(const std::string& path,
1116  bool recursive = false,
1117  uint64_t* version = nullptr) noexcept = 0;
1118 
1128  virtual bool removeLogGroupSync(const std::string& path,
1129  uint64_t* version = nullptr) noexcept = 0;
1130 
1141  virtual int removeLogGroup(const std::string& path,
1142  logsconfig_status_callback_t cb) noexcept = 0;
1143 
1165  virtual int rename(const std::string& from_path,
1166  const std::string& to_path,
1167  logsconfig_status_callback_t cb) noexcept = 0;
1168 
1180  virtual bool renameSync(const std::string& from_path,
1181  const std::string& to_path,
1182  uint64_t* version = nullptr,
1183  std::string* failure_reason = nullptr) noexcept = 0;
1184 
1207  virtual int makeLogGroup(const std::string& path,
1208  const logid_range_t& range,
1209  const client::LogAttributes& attrs,
1210  bool mk_intermediate_dirs,
1211  make_log_group_callback_t cb) noexcept = 0;
1224  virtual std::unique_ptr<client::LogGroup>
1225  makeLogGroupSync(const std::string& path,
1226  const logid_range_t& range,
1227  const client::LogAttributes& attrs = client::LogAttributes(),
1228  bool mk_intermediate_dirs = false,
1229  std::string* failure_reason = nullptr) noexcept = 0;
1230 
1247  virtual int setAttributes(const std::string& path,
1248  const client::LogAttributes& attrs,
1249  logsconfig_status_callback_t cb) noexcept = 0;
1250 
1260  virtual bool
1261  setAttributesSync(const std::string& path,
1262  const client::LogAttributes& attrs,
1263  uint64_t* version = nullptr,
1264  std::string* failure_reason = nullptr) noexcept = 0;
1265 
1279  virtual int setLogGroupRange(const std::string& path,
1280  const logid_range_t& range,
1281  logsconfig_status_callback_t) noexcept = 0;
1282 
1292  virtual bool
1293  setLogGroupRangeSync(const std::string& path,
1294  const logid_range_t& range,
1295  uint64_t* version = nullptr,
1296  std::string* failure_reason = nullptr) noexcept = 0;
1297 
1304  virtual int getDirectory(const std::string& path,
1305  get_directory_callback_t) noexcept = 0;
1306 
1310  virtual std::unique_ptr<client::Directory>
1311  getDirectorySync(const std::string& path) noexcept = 0;
1312 
1328  virtual bool syncLogsConfigVersion(uint64_t version) noexcept = 0;
1329 
1339  virtual ConfigSubscriptionHandle
1340  notifyOnLogsConfigVersion(uint64_t version,
1341  std::function<void()>) noexcept = 0;
1342 
1349  virtual std::unique_ptr<ClusterAttributes>
1350  getClusterAttributes() noexcept = 0;
1351 
1365  virtual ConfigSubscriptionHandle
1366  subscribeToConfigUpdates(config_update_callback_t) noexcept = 0;
1367 
1373  virtual size_t getMaxPayloadSize() noexcept = 0;
1374 
1379  virtual ClientSettings& settings() = 0;
1380 
1385  virtual std::string getAllReadStreamsDebugInfo() noexcept = 0;
1386 
1412  virtual void publishEvent(Severity sev,
1413  std::string name_space,
1414  std::string type,
1415  std::string data = "",
1416  std::string context = "") noexcept = 0;
1417 
1418  protected:
1423  Client() = default;
1424 
1425  private:
1426  Client(const Client&) = delete; // non-copyable
1427  Client& operator=(const Client&) = delete; // non-assignable
1428 };
1429 
1430 }} // namespace facebook::logdevice
Definition: Client.h:200
Definition: AsyncReader.h:16
Definition: ClientSettings.h:25
Definition: Client.h:46
Definition: ConfigSubscriptionHandle.h:31