apr_redis.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file apr_redis.h
  18. * @brief Client interface for redis
  19. * @remark To use this interface you must have a separate redis
  20. * for more information.
  21. */
  22. #ifndef APR_REDIS_H
  23. #define APR_REDIS_H
  24. #include "apr.h"
  25. #include "apr_pools.h"
  26. #include "apr_time.h"
  27. #include "apr_strings.h"
  28. #include "apr_network_io.h"
  29. #include "apr_ring.h"
  30. #include "apr_buckets.h"
  31. #include "apr_reslist.h"
  32. #include "apr_hash.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif /* __cplusplus */
  36. #ifndef RC_DEFAULT_SERVER_PORT
  37. #define RC_DEFAULT_SERVER_PORT 6379
  38. #endif
  39. #ifndef RC_DEFAULT_SERVER_MIN
  40. #define RC_DEFAULT_SERVER_MIN 0
  41. #endif
  42. #ifndef RC_DEFAULT_SERVER_SMAX
  43. #define RC_DEFAULT_SERVER_SMAX 1
  44. #endif
  45. #ifndef RC_DEFAULT_SERVER_TTL
  46. #define RC_DEFAULT_SERVER_TTL 600
  47. #endif
  48. /**
  49. * @defgroup APR_Util_RC Redis Client Routines
  50. * @ingroup APR_Util
  51. * @{
  52. */
  53. /** Specifies the status of a redis server */
  54. typedef enum
  55. {
  56. APR_RC_SERVER_LIVE, /**< Server is alive and responding to requests */
  57. APR_RC_SERVER_DEAD /**< Server is not responding to requests */
  58. } apr_redis_server_status_t;
  59. /** Opaque redis client connection object */
  60. typedef struct apr_redis_conn_t apr_redis_conn_t;
  61. /** Redis Server Info Object */
  62. typedef struct apr_redis_server_t apr_redis_server_t;
  63. struct apr_redis_server_t
  64. {
  65. const char *host; /**< Hostname of this Server */
  66. apr_port_t port; /**< Port of this Server */
  67. apr_redis_server_status_t status; /**< @see apr_redis_server_status_t */
  68. #if APR_HAS_THREADS || defined(DOXYGEN)
  69. apr_reslist_t *conns; /**< Resource list of actual client connections */
  70. #else
  71. apr_redis_conn_t *conn;
  72. #endif
  73. apr_pool_t *p; /** Pool to use for private allocations */
  74. #if APR_HAS_THREADS
  75. apr_thread_mutex_t *lock;
  76. #endif
  77. apr_time_t btime;
  78. apr_uint32_t rwto;
  79. struct
  80. {
  81. int major;
  82. int minor;
  83. int patch;
  84. char *number;
  85. } version;
  86. };
  87. typedef struct apr_redis_t apr_redis_t;
  88. /* Custom hash callback function prototype, user for server selection.
  89. * @param baton user selected baton
  90. * @param data data to hash
  91. * @param data_len length of data
  92. */
  93. typedef apr_uint32_t (*apr_redis_hash_func)(void *baton,
  94. const char *data,
  95. const apr_size_t data_len);
  96. /* Custom Server Select callback function prototype.
  97. * @param baton user selected baton
  98. * @param rc redis instance, use rc->live_servers to select a node
  99. * @param hash hash of the selected key.
  100. */
  101. typedef apr_redis_server_t* (*apr_redis_server_func)(void *baton,
  102. apr_redis_t *rc,
  103. const apr_uint32_t hash);
  104. /** Container for a set of redis servers */
  105. struct apr_redis_t
  106. {
  107. apr_uint32_t flags; /**< Flags, Not currently used */
  108. apr_uint16_t nalloc; /**< Number of Servers Allocated */
  109. apr_uint16_t ntotal; /**< Number of Servers Added */
  110. apr_redis_server_t **live_servers; /**< Array of Servers */
  111. apr_pool_t *p; /** Pool to use for allocations */
  112. void *hash_baton;
  113. apr_redis_hash_func hash_func;
  114. void *server_baton;
  115. apr_redis_server_func server_func;
  116. };
  117. /**
  118. * Creates a crc32 hash used to split keys between servers
  119. * @param rc The redis client object to use
  120. * @param data Data to be hashed
  121. * @param data_len Length of the data to use
  122. * @return crc32 hash of data
  123. * @remark The crc32 hash is not compatible with old redisd clients.
  124. */
  125. APU_DECLARE(apr_uint32_t) apr_redis_hash(apr_redis_t *rc,
  126. const char *data,
  127. const apr_size_t data_len);
  128. /**
  129. * Pure CRC32 Hash. Used by some clients.
  130. */
  131. APU_DECLARE(apr_uint32_t) apr_redis_hash_crc32(void *baton,
  132. const char *data,
  133. const apr_size_t data_len);
  134. /**
  135. * hash compatible with the standard Perl Client.
  136. */
  137. APU_DECLARE(apr_uint32_t) apr_redis_hash_default(void *baton,
  138. const char *data,
  139. const apr_size_t data_len);
  140. /**
  141. * Picks a server based on a hash
  142. * @param rc The redis client object to use
  143. * @param hash Hashed value of a Key
  144. * @return server that controls specified hash
  145. * @see apr_redis_hash
  146. */
  147. APU_DECLARE(apr_redis_server_t *) apr_redis_find_server_hash(apr_redis_t *rc,
  148. const apr_uint32_t hash);
  149. /**
  150. * server selection compatible with the standard Perl Client.
  151. */
  152. APU_DECLARE(apr_redis_server_t *) apr_redis_find_server_hash_default(void *baton,
  153. apr_redis_t *rc,
  154. const apr_uint32_t hash);
  155. /**
  156. * Adds a server to a client object
  157. * @param rc The redis client object to use
  158. * @param server Server to add
  159. * @remark Adding servers is not thread safe, and should be done once at startup.
  160. * @warning Changing servers after startup may cause keys to go to
  161. * different servers.
  162. */
  163. APU_DECLARE(apr_status_t) apr_redis_add_server(apr_redis_t *rc,
  164. apr_redis_server_t *server);
  165. /**
  166. * Finds a Server object based on a hostname/port pair
  167. * @param rc The redis client object to use
  168. * @param host Hostname of the server
  169. * @param port Port of the server
  170. * @return Server with matching Hostname and Port, or NULL if none was found.
  171. */
  172. APU_DECLARE(apr_redis_server_t *) apr_redis_find_server(apr_redis_t *rc,
  173. const char *host,
  174. apr_port_t port);
  175. /**
  176. * Enables a Server for use again
  177. * @param rc The redis client object to use
  178. * @param rs Server to Activate
  179. */
  180. APU_DECLARE(apr_status_t) apr_redis_enable_server(apr_redis_t *rc,
  181. apr_redis_server_t *rs);
  182. /**
  183. * Disable a Server
  184. * @param rc The redis client object to use
  185. * @param rs Server to Disable
  186. */
  187. APU_DECLARE(apr_status_t) apr_redis_disable_server(apr_redis_t *rc,
  188. apr_redis_server_t *rs);
  189. /**
  190. * Creates a new Server Object
  191. * @param p Pool to use
  192. * @param host hostname of the server
  193. * @param port port of the server
  194. * @param min minimum number of client sockets to open
  195. * @param smax soft maximum number of client connections to open
  196. * @param max hard maximum number of client connections
  197. * @param ttl time to live in microseconds of a client connection
  198. * @param rwto r/w timeout value in seconds of a client connection
  199. * @param ns location of the new server object
  200. * @see apr_reslist_create
  201. * @remark min, smax, and max are only used when APR_HAS_THREADS
  202. */
  203. APU_DECLARE(apr_status_t) apr_redis_server_create(apr_pool_t *p,
  204. const char *host,
  205. apr_port_t port,
  206. apr_uint32_t min,
  207. apr_uint32_t smax,
  208. apr_uint32_t max,
  209. apr_uint32_t ttl,
  210. apr_uint32_t rwto,
  211. apr_redis_server_t **ns);
  212. /**
  213. * Creates a new redisd client object
  214. * @param p Pool to use
  215. * @param max_servers maximum number of servers
  216. * @param flags Not currently used
  217. * @param rc location of the new redis client object
  218. */
  219. APU_DECLARE(apr_status_t) apr_redis_create(apr_pool_t *p,
  220. apr_uint16_t max_servers,
  221. apr_uint32_t flags,
  222. apr_redis_t **rc);
  223. /**
  224. * Gets a value from the server, allocating the value out of p
  225. * @param rc client to use
  226. * @param p Pool to use
  227. * @param key null terminated string containing the key
  228. * @param baton location of the allocated value
  229. * @param len length of data at baton
  230. * @param flags any flags set by the client for this key
  231. * @return
  232. */
  233. APU_DECLARE(apr_status_t) apr_redis_getp(apr_redis_t *rc,
  234. apr_pool_t *p,
  235. const char* key,
  236. char **baton,
  237. apr_size_t *len,
  238. apr_uint16_t *flags);
  239. /**
  240. * Sets a value by key on the server
  241. * @param rc client to use
  242. * @param key null terminated string containing the key
  243. * @param baton data to store on the server
  244. * @param data_size length of data at baton
  245. * @param flags any flags set by the client for this key
  246. */
  247. APU_DECLARE(apr_status_t) apr_redis_set(apr_redis_t *rc,
  248. const char *key,
  249. char *baton,
  250. const apr_size_t data_size,
  251. apr_uint16_t flags);
  252. /**
  253. * Sets a value by key on the server
  254. * @param rc client to use
  255. * @param key null terminated string containing the key
  256. * @param baton data to store on the server
  257. * @param data_size length of data at baton
  258. * @param timeout time in seconds for the data to live on the server
  259. * @param flags any flags set by the client for this key
  260. */
  261. APU_DECLARE(apr_status_t) apr_redis_setex(apr_redis_t *rc,
  262. const char *key,
  263. char *baton,
  264. const apr_size_t data_size,
  265. apr_uint32_t timeout,
  266. apr_uint16_t flags);
  267. /**
  268. * Deletes a key from a server
  269. * @param rc client to use
  270. * @param key null terminated string containing the key
  271. * @param timeout time for the delete to stop other clients from adding
  272. */
  273. APU_DECLARE(apr_status_t) apr_redis_delete(apr_redis_t *rc,
  274. const char *key,
  275. apr_uint32_t timeout);
  276. /**
  277. * Query a server's version
  278. * @param rs server to query
  279. * @param p Pool to allocate answer from
  280. * @param baton location to store server version string
  281. */
  282. APU_DECLARE(apr_status_t) apr_redis_version(apr_redis_server_t *rs,
  283. apr_pool_t *p,
  284. char **baton);
  285. /**
  286. * Query a server's INFO
  287. * @param rs server to query
  288. * @param p Pool to allocate answer from
  289. * @param baton location to store server INFO response string
  290. */
  291. APU_DECLARE(apr_status_t) apr_redis_info(apr_redis_server_t *rs,
  292. apr_pool_t *p,
  293. char **baton);
  294. /**
  295. * Increments a value
  296. * @param rc client to use
  297. * @param key null terminated string containing the key
  298. * @param inc number to increment by
  299. * @param new_value new value after incrementing
  300. */
  301. APU_DECLARE(apr_status_t) apr_redis_incr(apr_redis_t *rc,
  302. const char *key,
  303. apr_int32_t inc,
  304. apr_uint32_t *new_value);
  305. /**
  306. * Decrements a value
  307. * @param rc client to use
  308. * @param key null terminated string containing the key
  309. * @param inc number to decrement by
  310. * @param new_value new value after decrementing
  311. */
  312. APU_DECLARE(apr_status_t) apr_redis_decr(apr_redis_t *rc,
  313. const char *key,
  314. apr_int32_t inc,
  315. apr_uint32_t *new_value);
  316. /**
  317. * Pings the server
  318. * @param rs Server to ping
  319. */
  320. APU_DECLARE(apr_status_t) apr_redis_ping(apr_redis_server_t *rs);
  321. /**
  322. * Gets multiple values from the server, allocating the values out of p
  323. * @param rc client to use
  324. * @param temp_pool Pool used for temporary allocations. May be cleared inside this
  325. * call.
  326. * @param data_pool Pool used to allocate data for the returned values.
  327. * @param values hash of apr_redis_value_t keyed by strings, contains the
  328. * result of the multiget call.
  329. * @return
  330. */
  331. APU_DECLARE(apr_status_t) apr_redis_multgetp(apr_redis_t *rc,
  332. apr_pool_t *temp_pool,
  333. apr_pool_t *data_pool,
  334. apr_hash_t *values);
  335. typedef enum
  336. {
  337. APR_RS_SERVER_MASTER, /**< Server is a master */
  338. APR_RS_SERVER_SLAVE, /**< Server is a slave */
  339. APR_RS_SERVER_UNKNOWN /**< Server role is unknown */
  340. } apr_redis_server_role_t;
  341. typedef struct
  342. {
  343. /* # Server */
  344. /** Major version number of this server */
  345. apr_uint32_t major;
  346. /** Minor version number of this server */
  347. apr_uint32_t minor;
  348. /** Patch version number of this server */
  349. apr_uint32_t patch;
  350. /** Process id of this server process */
  351. apr_uint32_t process_id;
  352. /** Number of seconds this server has been running */
  353. apr_uint32_t uptime_in_seconds;
  354. /** Bitsize of the arch on the current machine */
  355. apr_uint32_t arch_bits;
  356. /* # Clients */
  357. /** Number of connected clients */
  358. apr_uint32_t connected_clients;
  359. /** Number of blocked clients */
  360. apr_uint32_t blocked_clients;
  361. /* # Memory */
  362. /** Max memory of this server */
  363. apr_uint64_t maxmemory;
  364. /** Amount of used memory */
  365. apr_uint64_t used_memory;
  366. /** Total memory available on this server */
  367. apr_uint64_t total_system_memory;
  368. /* # Stats */
  369. /** Total connections received */
  370. apr_uint64_t total_connections_received;
  371. /** Total commands processed */
  372. apr_uint64_t total_commands_processed;
  373. /** Total commands rejected */
  374. apr_uint64_t rejected_connections;
  375. /** Total net input bytes */
  376. apr_uint64_t total_net_input_bytes;
  377. /** Total net output bytes */
  378. apr_uint64_t total_net_output_bytes;
  379. /** Keyspace hits */
  380. apr_uint64_t keyspace_hits;
  381. /** Keyspace misses */
  382. apr_uint64_t keyspace_misses;
  383. /* # Replication */
  384. /** Role */
  385. apr_redis_server_role_t role;
  386. /** Number of connected slave */
  387. apr_uint32_t connected_slaves;
  388. /* # CPU */
  389. /** Accumulated CPU user time for this process */
  390. apr_uint32_t used_cpu_sys;
  391. /** Accumulated CPU system time for this process */
  392. apr_uint32_t used_cpu_user;
  393. /* # Cluster */
  394. /** Is cluster enabled */
  395. apr_uint32_t cluster_enabled;
  396. } apr_redis_stats_t;
  397. /**
  398. * Query a server for statistics
  399. * @param rs server to query
  400. * @param p Pool to allocate answer from
  401. * @param stats location of the new statistics structure
  402. */
  403. APU_DECLARE(apr_status_t) apr_redis_stats(apr_redis_server_t *rs,
  404. apr_pool_t *p,
  405. apr_redis_stats_t **stats);
  406. /** @} */
  407. #ifdef __cplusplus
  408. }
  409. #endif
  410. #endif /* APR_REDIS_H */