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<void(Status, const std::string& failure_reason)>
179  status_callback_t;
180 
187 typedef std::function<void(Status status, std::unique_ptr<LogTailAttributes>)>
188  get_tail_attributes_callback_t;
189 
196 typedef std::function<void(Status status, std::unique_ptr<LogHeadAttributes>)>
197  get_head_attributes_callback_t;
198 
199 class Client {
200  public:
204  [[deprecated("Replaced by ClientFactory")]] static std::shared_ptr<Client>
205  create(std::string cluster_name,
206  std::string config_url,
207  std::string credentials,
208  std::chrono::milliseconds timeout,
209  std::unique_ptr<ClientSettings> settings,
210  std::string csid = "") noexcept;
211 
216  Client() = default;
217 
223  virtual ~Client() = default;
224 
297  virtual lsn_t
298  appendSync(logid_t logid,
299  std::string payload,
300  AppendAttributes attrs = AppendAttributes(),
301  std::chrono::milliseconds* timestamp = nullptr) noexcept = 0;
302 
322  virtual lsn_t
323  appendSync(logid_t logid,
324  const Payload& payload,
325  AppendAttributes attrs = AppendAttributes(),
326  std::chrono::milliseconds* timestamp = nullptr) noexcept = 0;
327 
365  virtual int append(logid_t logid,
366  std::string payload,
367  append_callback_t cb,
368  AppendAttributes attrs = AppendAttributes()) noexcept = 0;
369 
385  virtual int append(logid_t logid,
386  const Payload& payload,
387  append_callback_t cb,
388  AppendAttributes attrs = AppendAttributes()) noexcept = 0;
389 
415  virtual std::unique_ptr<Reader>
416  createReader(size_t max_logs, ssize_t buffer_size = -1) noexcept = 0;
417 
422  virtual std::unique_ptr<AsyncReader>
423  createAsyncReader(ssize_t buffer_size = -1) noexcept = 0;
424 
429  virtual void setTimeout(std::chrono::milliseconds timeout) noexcept = 0;
430 
462  virtual int trimSync(logid_t logid, lsn_t lsn) noexcept = 0;
463 
472  virtual int trim(logid_t logid, lsn_t lsn, trim_callback_t cb) noexcept = 0;
473 
482  virtual void addWriteToken(std::string) noexcept = 0;
483 
560  virtual lsn_t
561  findTimeSync(logid_t logid,
562  std::chrono::milliseconds timestamp,
563  Status* status_out = nullptr,
564  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
565 
634  virtual FindKeyResult
635  findKeySync(logid_t logid,
636  std::string key,
637  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
638 
648  virtual int
649  findTime(logid_t logid,
650  std::chrono::milliseconds timestamp,
651  find_time_callback_t cb,
652  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
653 
662  virtual int
663  findKey(logid_t logid,
664  std::string key,
665  find_key_callback_t cb,
666  FindKeyAccuracy accuracy = FindKeyAccuracy::STRICT) noexcept = 0;
667 
691  virtual int isLogEmptySync(logid_t logid, bool* empty) noexcept = 0;
692 
702  virtual int isLogEmpty(logid_t logid, is_empty_callback_t cb) noexcept = 0;
703 
737  virtual int dataSizeSync(logid_t logid,
738  std::chrono::milliseconds start,
739  std::chrono::milliseconds end,
740  DataSizeAccuracy accuracy,
741  size_t* size) noexcept = 0;
757  virtual int dataSize(logid_t logid,
758  std::chrono::milliseconds start,
759  std::chrono::milliseconds end,
760  DataSizeAccuracy accuracy,
761  data_size_callback_t cb) noexcept = 0;
762 
800  virtual lsn_t getTailLSNSync(logid_t logid) noexcept = 0;
801 
811  virtual int getTailLSN(logid_t logid,
812  get_tail_lsn_callback_t cb) noexcept = 0;
813 
852  virtual std::unique_ptr<LogTailAttributes>
853  getTailAttributesSync(logid_t logid) noexcept = 0;
854 
864  virtual int getTailAttributes(logid_t logid,
865  get_tail_attributes_callback_t cb) noexcept = 0;
866 
885  virtual std::unique_ptr<LogHeadAttributes>
886  getHeadAttributesSync(logid_t logid) noexcept = 0;
887 
897  virtual int getHeadAttributes(logid_t logid,
898  get_head_attributes_callback_t cb) noexcept = 0;
932  virtual logid_range_t getLogRangeByName(const std::string& name) noexcept = 0;
933 
942  virtual void
943  getLogRangeByName(const std::string& name,
944  get_log_range_by_name_callback_t cb) noexcept = 0;
945 
950  virtual std::string getLogNamespaceDelimiter() noexcept = 0;
951 
972  virtual std::map<std::string, logid_range_t>
973  getLogRangesByNamespace(const std::string& ns) noexcept = 0;
974 
983  virtual void getLogRangesByNamespace(
984  const std::string& ns,
985  get_log_ranges_by_namespace_callback_t cb) noexcept = 0;
986 
1002  virtual std::unique_ptr<client::LogGroup>
1003  getLogGroupSync(const std::string& path) noexcept = 0;
1004 
1014  virtual void getLogGroup(const std::string& path,
1015  get_log_group_callback_t cb) noexcept = 0;
1016 
1032  virtual std::unique_ptr<client::LogGroup>
1033  getLogGroupByIdSync(const logid_t logid) noexcept = 0;
1034 
1048  virtual void getLogGroupById(const logid_t logid,
1049  get_log_group_callback_t cb) noexcept = 0;
1050 
1068  virtual int makeDirectory(const std::string& path,
1069  bool mk_intermediate_dirs,
1070  const client::LogAttributes& attrs,
1071  make_directory_callback_t cb) noexcept = 0;
1072 
1080  virtual std::unique_ptr<client::Directory> makeDirectorySync(
1081  const std::string& path,
1082  bool mk_intermediate_dirs = false,
1083  const client::LogAttributes& attrs = client::LogAttributes(),
1084  std::string* failure_reason = nullptr) noexcept = 0;
1085 
1101  virtual int removeDirectory(const std::string& path,
1102  bool recursive,
1103  status_callback_t) noexcept = 0;
1104 
1110  virtual bool removeDirectorySync(const std::string& path,
1111  bool recursive = false) noexcept = 0;
1112 
1119  virtual bool removeLogGroupSync(const std::string& path) noexcept = 0;
1120 
1131  virtual int removeLogGroup(const std::string& path,
1132  status_callback_t cb) noexcept = 0;
1133 
1155  virtual int rename(const std::string& from_path,
1156  const std::string& to_path,
1157  status_callback_t cb) noexcept = 0;
1158 
1166  virtual bool renameSync(const std::string& from_path,
1167  const std::string& to_path,
1168  std::string* failure_reason = nullptr) noexcept = 0;
1169 
1192  virtual int makeLogGroup(const std::string& path,
1193  const logid_range_t& range,
1194  const client::LogAttributes& attrs,
1195  bool mk_intermediate_dirs,
1196  make_log_group_callback_t cb) noexcept = 0;
1202  virtual std::unique_ptr<client::LogGroup>
1203  makeLogGroupSync(const std::string& path,
1204  const logid_range_t& range,
1205  const client::LogAttributes& attrs = client::LogAttributes(),
1206  bool mk_intermediate_dirs = false,
1207  std::string* failure_reason = nullptr) noexcept = 0;
1208 
1225  virtual int setAttributes(const std::string& path,
1226  const client::LogAttributes& attrs,
1227  status_callback_t cb) noexcept = 0;
1228 
1234  virtual bool
1235  setAttributesSync(const std::string& path,
1236  const client::LogAttributes& attrs,
1237  std::string* failure_reason = nullptr) noexcept = 0;
1238 
1252  virtual int setLogGroupRange(const std::string& path,
1253  const logid_range_t& range,
1254  status_callback_t) noexcept = 0;
1255 
1261  virtual bool
1262  setLogGroupRangeSync(const std::string& path,
1263  const logid_range_t& range,
1264  std::string* failure_reason = nullptr) noexcept = 0;
1265 
1272  virtual int getDirectory(const std::string& path,
1273  get_directory_callback_t) noexcept = 0;
1274 
1278  virtual std::unique_ptr<client::Directory>
1279  getDirectorySync(const std::string& path) noexcept = 0;
1280 
1290  virtual bool syncLogsConfigVersion(uint64_t version) noexcept = 0;
1291 
1301  virtual ConfigSubscriptionHandle
1302  notifyOnLogsConfigVersion(uint64_t version,
1303  std::function<void()>) noexcept = 0;
1304 
1311  virtual std::unique_ptr<ClusterAttributes>
1312  getClusterAttributes() noexcept = 0;
1313 
1327  virtual ConfigSubscriptionHandle
1328  subscribeToConfigUpdates(config_update_callback_t) noexcept = 0;
1329 
1335  virtual size_t getMaxPayloadSize() noexcept = 0;
1336 
1341  virtual ClientSettings& settings() = 0;
1342 
1347  virtual std::string getAllReadStreamsDebugInfo() noexcept = 0;
1348 
1374  virtual void publishEvent(Severity sev,
1375  std::string name_space,
1376  std::string type,
1377  std::string data = "",
1378  std::string context = "") noexcept = 0;
1379 
1380  private:
1381  Client(const Client&) = delete; // non-copyable
1382  Client& operator=(const Client&) = delete; // non-assignable
1383 };
1384 
1385 }} // namespace facebook::logdevice
Definition: Client.h:199
Definition: AsyncReader.h:16
Definition: ClientSettings.h:25
Definition: Client.h:46
Definition: ConfigSubscriptionHandle.h:31