Skip to content

Commit

Permalink
couchbase: fix multiple hosts usage
Browse files Browse the repository at this point in the history
Closes #1216
  • Loading branch information
razvancrainea committed Nov 21, 2017
1 parent 56490e4 commit c23ad23
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 18 deletions.
12 changes: 6 additions & 6 deletions modules/cachedb_couchbase/README
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Edited by

Vladut-Stefan Paiu

Copyright © 2013 www.opensips-solutions.com
Copyright 2013 www.opensips-solutions.com
__________________________________________________________

Table of Contents
Expand Down Expand Up @@ -95,12 +95,12 @@ Chapter 1. Admin Guide
Example 1.1. Set cachedb_url parameter
...
modparam("cachedb_couchbase", "cachedb_url","couchbase:group1://localhos
t:6379/default");
t:6379/default")
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random
_url:8888/my_bucket");
#Multiple hosts
_url:8888/my_bucket")
# Multiple hosts
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random
_url1:8888;random_url2:8888;random_url3:8888/my_bucket");
_url1:8888,random_url2:8888,random_url3:8888/my_bucket")
...

1.5.2. timeout (int)
Expand All @@ -119,7 +119,7 @@ modparam("cachedb_couchbase", "timeout",5000000);
last. Anything above the threshold will trigger a warning
message to the log

Default value is 0 ( unlimited - no warnings ).
Default value is "0 ( unlimited - no warnings )".

Example 1.3. Set exec_threshold parameter
...
Expand Down
91 changes: 83 additions & 8 deletions modules/cachedb_couchbase/cachedb_couchbase_dbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,85 @@ lcb_error_t cb_remove(lcb_t instance, const void *command_cookie, lcb_size_t num
return op_error;
}

#define CBASE_BUF_SIZE 256

/*
* fill the options based on the id field
* the buf and len are used to store the URL/Host in case we need to build it
*/
int couchbase_fill_options(struct cachedb_id *id, struct lcb_create_st *opt,
char *buf, int len)
{
#if LCB_VERSION <= 0x020300
char *p;
#endif
int l;
memset(opt, 0, sizeof(*opt));

#if LCB_VERSION <= 0x020300
opt->version = 0;
opt->v.v0.user = id->username;
opt->v.v0.passwd = id->password;
opt->v.v0.bucket = id->database;
if (id->flags & CACHEDB_ID_MULTIPLE_HOSTS) {
p = q_memchr(id->host, ',', len);
if (p) {
l = p - id->host;
if (l >= len) {
LM_ERR("Not enough space for the host [%.*s]%d>=%d\n",
l, id->host, l, CBASE_BUF_SIZE);
return -1;
}
memcpy(buf, id->host, l);
buf[l] = 0;
LM_WARN("Version %s does not support multiple hosts connection! "
"Connecting only to first host: %s!\n",
LCB_VERSION_STRING, buf);
opt->v.v0.host = buf;
}
}
/* when it comes with multiple hosts, the port is already in the id->host
* field, so we no longer need to worry to put it in the buffer */
if (id->port) {
if (snprintf(buf, len, "%s:%hu", id->host, id->port) >= len) {
LM_ERR("cannot print %s:%hu in %d buffer\n", id->host, id->port, len);
return -1;
}
opt->v.v0.host = buf;
} else if (!opt->v.v0.host) {
opt->v.v0.host = id->host;
}
LM_DBG("Connecting HOST: %s BUCKET: %s\n", opt->v.v0.host, opt->v.v0.bucket);
#else
opt->version = 3;
opt->v.v3.username = id->username;
opt->v.v3.passwd = id->password;

/* we don't care whether it has CACHEDB_ID_MULTIPLE_HOSTS, because
* - if it does, it does not have a port and it should be printed as
* string
* - if it does not, simply port the host as string and port if necessary
*/
if (!id->port)
l = snprintf(buf, len, "couchbase://%s/%s", id->host, id->database);
else
l = snprintf(buf, len, "couchbase://%s:%hu/%s", id->host, id->port,
id->database);
if (l >= len) {
LM_ERR("not enough buffer to print the URL: %.*s\n", len, buf);
return -1;
}
opt->v.v3.connstr = buf;
LM_DBG("Connecting URL: %s\n", opt->v.v3.connstr);
#endif

return 0;
}

couchbase_con* couchbase_connect(struct cachedb_id* id, int is_reconnect)
{
/* buffer used to temporary store the host, in case we need to build it */
char tmp_buf[CBASE_BUF_SIZE];
couchbase_con *con;
struct lcb_create_st options;
lcb_uint32_t tmo = 0;
Expand All @@ -221,13 +298,11 @@ couchbase_con* couchbase_connect(struct cachedb_id* id, int is_reconnect)
con->id = id;
con->ref = 1;

/* TODO - support custom ports - couchbase expects host:port in id->host */
memset(&options,0,sizeof(struct lcb_create_st));
options.version = 0;
options.v.v0.host = id->host;
options.v.v0.user = id->username;
options.v.v0.passwd = id->password;
options.v.v0.bucket = id->database;
if (couchbase_fill_options(id, &options, tmp_buf, CBASE_BUF_SIZE) < 0) {
LM_ERR("cannot create connection options!\n");
return 0;
}

rc=lcb_create(&instance, &options);
if (rc!=LCB_SUCCESS) {
LM_ERR("Failed to create libcouchbase instance: 0x%02x, %s\n",
Expand Down Expand Up @@ -632,7 +707,7 @@ int couchbase_get_counter(cachedb_con *connection,str *attr,int *val)

stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);

if (str2sint((str *)&get_res,val)) {
LM_ERR("Failued to convert counter [%.*s] to int\n",get_res.len,get_res.s);
pkg_free(get_res.s);
Expand Down
8 changes: 4 additions & 4 deletions modules/cachedb_couchbase/doc/cachedb_couchbase_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@
<title>Set <varname>cachedb_url</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("cachedb_couchbase", "cachedb_url","couchbase:group1://localhost:6379/default");
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random_url:8888/my_bucket");
#Multiple hosts
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random_url1:8888;random_url2:8888;random_url3:8888/my_bucket");
modparam("cachedb_couchbase", "cachedb_url","couchbase:group1://localhost:6379/default")
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random_url:8888/my_bucket")
# Multiple hosts
modparam("cachedb_couchbase", "cachedb_url","couchbase:cluster1://random_url1:8888,random_url2:8888,random_url3:8888/my_bucket")
...
</programlisting>
</example>
Expand Down

0 comments on commit c23ad23

Please sign in to comment.