All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Typedefs | Functions
cluster.h File Reference

Common functions for interacting with Redis cluster via Hiredis. More...

+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  fr_redis_cluster_state
 Redis connection sequence state. More...
 

Typedefs

typedef struct
fr_redis_cluster_state 
fr_redis_cluster_state_t
 Redis connection sequence state. More...
 
typedef struct fr_redis_cluster fr_redis_cluster_t
 

Functions

fr_redis_cluster_tfr_redis_cluster_alloc (TALLOC_CTX *ctx, CONF_SECTION *cs, fr_redis_conf_t *conf)
 Allocate and initialise a new cluster structure. More...
 
void * fr_redis_cluster_conn_create (TALLOC_CTX *ctx, void *instance, struct timeval const *timeout)
 Create a new connection to a Redis node. More...
 
bool fr_redis_cluster_min_version (fr_redis_cluster_t *cluster, char const *min_version)
 Check if members of the cluster are above a certain version. More...
 
int fr_redis_cluster_pool_by_node_addr (fr_connection_pool_t **pool, fr_redis_cluster_t *cluster, fr_ipaddr_t *ipaddr, uint16_t port, bool create)
 Get the pool associated with a node in the cluster. More...
 
fr_redis_rcode_t fr_redis_cluster_state_init (fr_redis_cluster_state_t *state, fr_redis_conn_t **conn, fr_redis_cluster_t *cluster, REQUEST *request, uint8_t const *key, size_t key_len, bool read_only)
 Resolve a key to a pool, and reserve a connection in that pool. More...
 
fr_redis_rcode_t fr_redis_cluster_state_next (fr_redis_cluster_state_t *state, fr_redis_conn_t **conn, fr_redis_cluster_t *cluster, REQUEST *request, fr_redis_rcode_t status, redisReply **reply)
 Get the next connection to attempt a command against. More...
 

Detailed Description

Common functions for interacting with Redis cluster via Hiredis.

Id:
6455947cb2d7987e776342f2dac7e535f0580a20
Author
Arran Cudbard-Bell
Id:
d03fcba8b0ec02bb38d599014c4344a30291c648
Author
Arran Cudbard-Bell

Definition in file cluster.h.


Data Structure Documentation

struct fr_redis_cluster_state

Redis connection sequence state.

Tracks how many operations we've performed attempting to execute a single command.

Used by callers of the cluster code. Allocated on the stack and passed to fr_redis_cluster_state_init and fr_redis_cluster_state_next.

Definition at line 43 of file cluster.h.

+ Collaboration diagram for fr_redis_cluster_state:
Data Fields
bool close_conn Set by caller of fr_redis_cluster_state_next, to indicate that connection must be closed, as it's now in an unknown state.
uint32_t in_pool How many available connections are there in the pool.
uint8_t const * key Key we performed hashing on.
size_t key_len Length of the key.
struct fr_redis_cluster_node * node Node we're communicating with.
uint32_t reconnects How many connections we've tried in this pool.
uint32_t redirects How many redirects have we followed.
uint32_t retries How many time's we've received TRYAGAIN.

Typedef Documentation

Redis connection sequence state.

Tracks how many operations we've performed attempting to execute a single command.

Used by callers of the cluster code. Allocated on the stack and passed to fr_redis_cluster_state_init and fr_redis_cluster_state_next.

Definition at line 34 of file cluster.h.

Function Documentation

fr_redis_cluster_t* fr_redis_cluster_alloc ( TALLOC_CTX *  ctx,
CONF_SECTION module,
fr_redis_conf_t conf 
)

Allocate and initialise a new cluster structure.

This holds all the data necessary to manage a pool of pools for a specific redis cluster.

Note
Will not error out unless cs.pool.start > 0. This is consistent with other pool based modules/code.
Parameters
ctxto link the lifetime of the cluster structure to.
moduleConfiguration section to search for 'server' conf pairs in.
confBase redis server configuration. Cluster nodes share database number and password.
Returns

Definition at line 2059 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* fr_redis_cluster_conn_create ( TALLOC_CTX *  ctx,
void *  instance,
struct timeval const *  timeout 
)

Create a new connection to a Redis node.

Parameters
[in]ctxto allocate connection structure in. Will be freed at the same time as the pool.
[in]instancedata of type cluster_node_t. Holds parameters for establishing new connection.
[in]timeoutThe maximum time allowed to complete the connection.
Returns

Definition at line 1415 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool fr_redis_cluster_min_version ( fr_redis_cluster_t cluster,
char const *  min_version 
)

Check if members of the cluster are above a certain version.

Parameters
clusterto perform check on.
min_versionthat must be found on each node for the check to succeed. Must be in the format
<major>.<minor>.<release> 
.
Returns
  • true if all contactable members are above min_version.
  • false if at least one member if not above minimum version (use fr_strerror to retrieve node information).

Definition at line 2031 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_redis_cluster_pool_by_node_addr ( fr_connection_pool_t **  pool,
fr_redis_cluster_t cluster,
fr_ipaddr_t ipaddr,
uint16_t  port,
bool  create 
)

Get the pool associated with a node in the cluster.

Note
This is used for testing only. It's not ifdef'd out because tests need to run against production builds too.
Parameters
[out]poolassociated with the node.
[in]clusterto search for node in.
[in]ipaddrof node.
[in]portof node.
[in]createEstablish a connection to the specified node if it was previously unknown to the cluster client.
Returns
  • 0 on success.
  • -1 if no such node exists.

Definition at line 1919 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

fr_redis_rcode_t fr_redis_cluster_state_init ( fr_redis_cluster_state_t state,
fr_redis_conn_t **  conn,
fr_redis_cluster_t cluster,
REQUEST request,
uint8_t const *  key,
size_t  key_len,
bool  read_only 
)

Resolve a key to a pool, and reserve a connection in that pool.

This should be used with fr_redis_cluster_state_next, and fr_redis_command_status, to transparently locate the cluster node we need to perform the operation on.

Example code below shows how this function is used in conjunction with fr_redis_cluster_state_next to follow redirects, and reconnect handles.

int s_ret;
redis_conn_state state;
redisReply *reply;
for (s_ret = fr_redis_cluster_state_init(&state, &conn, cluster, key, key_len, false);
s_ret = fr_redis_cluster_state_next(&state, &conn, cluster, request, status, &reply)) {
reply = redisCommand(conn->handle, "SET foo bar");
status = fr_redis_command_status(conn, reply);
}
// Reply is freed if ret == REDIS_RCODE_TRY_AGAIN, but left in all other cases to allow error
// processing, or extraction of results.
if (s_ret != REDIS_RCODE_SUCCESS) {
// Error
}
// Success
Parameters
[out]stateto track current pool and various counters, will be initialised.
[out]connWhere to write the reserved connection to.
[in]clusterof pools.
[in]requestThe current request.
[in]keyto resolve to a cluster node/pool. If no key is NULL or key_len is 0 a random slot will be chosen.
[in]key_lenLength of the key.
[in]read_onlyIf true, will use random slave pool in preference to the master, falling back to the master if no slaves are available.
Returns
  • REDIS_RCODE_TRY_AGAIN - try your command with this connection (provided via command).
  • REDIS_RCODE_RECONNECT - when no additional connections available.

Definition at line 1594 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

fr_redis_rcode_t fr_redis_cluster_state_next ( fr_redis_cluster_state_t state,
fr_redis_conn_t **  conn,
fr_redis_cluster_t cluster,
REQUEST request,
fr_redis_rcode_t  status,
redisReply **  reply 
)

Get the next connection to attempt a command against.

Will process reconnect and redirect states performing the actions necessary.

If a remap is in progress, has ocurred within the last second, has recently failed, or fails, the '-MOVE' will be treated as a temporary redirect (-ASK).

This allows the server to be more responsive during remaps, as unless the worker has been redirected to a node we don't currently have a pool for, it can grab a connection for the node it was redirected to, and continue.

Note
Irrespective of return code, the connection passed via conn will be released, A new connection to attempt command on will be provided via conn.
reply will be automatically freed and set to NULL if a new connection is provided in all other cases, the caller is responsible for freeing the reply.
Parameters
[in,out]statecontaining the current pool, and various counters which control retries, and limit redirects.
[in,out]connwe received the '-ASK' or '-MOVE' redirect on. Will be replaced with a connection in the new pool the key points to.
[in]requestThe current request.
[in]clusterof pools.
[in]statusof the last command, must be REDIS_RCODE_MOVE or REDIS_RCODE_ASK.
[in]replyfrom last command. Freed if 0 is returned, else caller must free.
Returns
  • REDIS_RCODE_SUCCESS - on success.
  • REDIS_RCODE_TRY_AGAIN - try new connection (provided via conn). Will free reply.
  • REDIS_RCODE_ERROR - on failure or command error.
  • REDIS_RCODE_RECONNECT - when no additional connections available.

Definition at line 1714 of file cluster.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function: