Couchbase C Client  2.10.7
Asynchronous C Client for Couchbase
example/observe/observe.c

Show how to use oberve to request state of the key on the cluster.

/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2013-2017 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* BUILD: `cc -o observe observe.c -lcouchbase`
* RUN: `./observe key`
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <libcouchbase/api3.h>
#define fail(msg) \
fprintf(stderr, "%s\n", msg); \
exit(EXIT_FAILURE)
#define fail2(msg, err) \
fprintf(stderr, "%s\n", msg); \
fprintf(stderr, "Error was 0x%x (%s)\n", err, lcb_strerror(NULL, err)); \
exit(EXIT_FAILURE)
typedef struct {
int master;
lcb_U8 status;
lcb_U64 cas;
} node_info;
typedef struct {
unsigned nresp;
node_info *nodeinfo;
} observe_info;
static void
observe_callback(lcb_t instance, int cbtype, const lcb_RESPBASE *rb)
{
const lcb_RESPOBSERVE *resp = (const lcb_RESPOBSERVE*)rb;
observe_info *obs_info = (observe_info *)rb->cookie;
node_info *ni = &obs_info->nodeinfo[obs_info->nresp];
if (rb->nkey == 0) {
fprintf(stderr, "All nodes have replied\n");
return;
}
if (rb->rc != LCB_SUCCESS) {
fprintf(stderr, "Failed to observe key from node. 0x%x (%s)\n",
rb->rc, lcb_strerror(instance, rb->rc));
obs_info->nresp++;
return;
}
/* Copy over the fields we care about */
ni->cas = resp->cas;
ni->status = resp->status;
ni->master = resp->ismaster;
/* Increase the response counter */
obs_info->nresp++;
}
int main(int argc, char *argv[])
{
lcb_t instance;
lcb_CMDOBSERVE cmd = { 0 };
lcb_MULTICMD_CTX *mctx = NULL;
observe_info obs_info;
unsigned nservers, ii;
struct lcb_create_st create_options = { 0 };
if (argc < 2) {
fail("Requires key as argument\n"
"Usage: observe KEY [CONNSTRING [ PASSWORD [ USERNAME ] ] ]\n");
}
create_options.version = 3;
if (argc > 2) {
create_options.v.v3.connstr = argv[2];
}
if (argc > 3) {
create_options.v.v3.passwd = argv[3];
}
if (argc > 4) {
create_options.v.v3.username = argv[4];
}
if ((err = lcb_create(&instance, &create_options)) != LCB_SUCCESS) {
fail2("cannot create connection instance", err);
}
if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
fail2("Couldn't schedule connection", err);
}
lcb_wait(instance);
if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) {
fail2("Couldn't get initial cluster configuration", err);
}
lcb_install_callback3(instance, LCB_CALLBACK_OBSERVE, observe_callback);
nservers = lcb_get_num_nodes(instance);
obs_info.nodeinfo = calloc(nservers, sizeof (*obs_info.nodeinfo));
obs_info.nresp = 0;
mctx = lcb_observe3_ctxnew(instance);
LCB_CMD_SET_KEY(&cmd, argv[1], strlen(argv[1]));
mctx->addcmd(mctx, (const lcb_CMDBASE*)&cmd);
printf("observing the state of '%s':\n", argv[1]);
if ((err = mctx->done(mctx, &obs_info)) != LCB_SUCCESS) {
fail2("Couldn't schedule observe request", err);
}
lcb_wait(instance);
for (ii = 0; ii < obs_info.nresp; ii++) {
node_info *ni = &obs_info.nodeinfo[ii];
fprintf(stderr, "Got status from %s node:\n", ni->master ? "master" : "replica");
fprintf(stderr, "\tCAS: 0x0%llx\n", ni->cas);
fprintf(stderr, "\tStatus (RAW): 0x%02x\n", ni->status);
fprintf(stderr, "\tExists [CACHE]: %s\n", ni->status & LCB_OBSERVE_NOT_FOUND ? "No" : "Yes");
fprintf(stderr, "\tExists [DISK]: %s\n", ni->status & LCB_OBSERVE_PERSISTED ? "Yes" : "No");
fprintf(stderr, "\n");
}
/* The next example shows how to use lcb_observe() to only request the
* CAS from the master node */
obs_info.nresp = 0;
memset(obs_info.nodeinfo, 0, sizeof(obs_info.nodeinfo[0]) * nservers);
fprintf(stderr, "Will request CAS from master...\n");
mctx = lcb_observe3_ctxnew(instance);
mctx->addcmd(mctx, (const lcb_CMDBASE*)&cmd);
if ((err = mctx->done(mctx, &obs_info)) != LCB_SUCCESS) {
fail2("Couldn't schedule observe request!\n", err);
}
lcb_wait(instance);
assert(obs_info.nresp == 1 && obs_info.nodeinfo[0].master);
fprintf(stderr, "CAS on master is 0x%llx\n", obs_info.nodeinfo[0].cas);
lcb_destroy(instance);
free(obs_info.nodeinfo);
return EXIT_SUCCESS;
}
lcb_CMDOBSERVE
Structure for an observe request.
Definition: couchbase.h:1777
lcb_wait
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
lcb_RESPOBSERVE::cas
lcb_CAS cas
CAS for response (if applicable)
Definition: couchbase.h:1807
lcb_strerror
const char * lcb_strerror(lcb_t instance, lcb_error_t error)
Get a textual descrtiption for the given error code.
lcb_RESPOBSERVE
Response structure for an observe command.
Definition: couchbase.h:1806
LCB_CMDOBSERVE_F_MASTER_ONLY
#define LCB_CMDOBSERVE_F_MASTER_ONLY
Set this bit in the cmdflags field to indicate that only the master node should be contacted.
Definition: couchbase.h:1771
lcb_RESPBASE::nkey
lcb_SIZE nkey
Size of key.
Definition: couchbase.h:626
lcb_create_st3::passwd
const char * passwd
Password for bucket.
Definition: couchbase.h:293
lcb_get_bootstrap_status
lcb_error_t lcb_get_bootstrap_status(lcb_t instance)
Gets the initial bootstrap status.
lcb_create_st3::connstr
const char * connstr
Connection string.
Definition: couchbase.h:282
lcb_connect
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
lcb_create_st3::username
const char * username
Username to use for authentication.
Definition: couchbase.h:288
lcb_RESPOBSERVE::status
lcb_U8 status
Bit set of flags.
Definition: couchbase.h:1808
lcb_error_t
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
lcb_CMDBASE::cmdflags
lcb_U32 cmdflags
Common flags for the command.
Definition: couchbase.h:523
lcb_RESPOBSERVE::ismaster
lcb_U8 ismaster
Set to true if this response came from the master node.
Definition: couchbase.h:1809
lcb_RESPBASE::rc
lcb_error_t rc
Status code.
Definition: couchbase.h:626
lcb_create_st::lcb_CRST_u::v3
struct lcb_create_st3 v3
Use this field.
Definition: couchbase.h:338
lcb_create_st
Wrapper structure for lcb_create()
Definition: couchbase.h:328
LCB_OBSERVE_NOT_FOUND
@ LCB_OBSERVE_NOT_FOUND
The item missing on the disk and the memory.
Definition: couchbase.h:1794
lcb_RESPBASE::cookie
void * cookie
Application-defined pointer passed as the cookie parameter when scheduling the command.
Definition: couchbase.h:626
LCB_CMD_SET_KEY
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: couchbase.h:556
lcb_t
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_create
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
couchbase.h
LCB_SUCCESS
@ LCB_SUCCESS
Success.
Definition: error.h:478
lcb_RESPBASE
Base response structure for callbacks.
Definition: couchbase.h:625
LCB_OBSERVE_PERSISTED
@ LCB_OBSERVE_PERSISTED
The item hit the disk.
Definition: couchbase.h:1792
lcb_destroy
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.
lcb_CMDBASE
Common ABI header for all commands.
Definition: couchbase.h:522
lcb_get_num_nodes
lcb_S32 lcb_get_num_nodes(lcb_t instance)
Get the number of the nodes in the cluster.
lcb_create_st::version
int version
Indicates which field in the lcb_CRST_u union should be used.
Definition: couchbase.h:330
LCB_CALLBACK_OBSERVE
@ LCB_CALLBACK_OBSERVE
lcb_observe3_ctxnew()
Definition: couchbase.h:707
lcb_install_callback3
lcb_RESPCALLBACK lcb_install_callback3(lcb_t instance, int cbtype, lcb_RESPCALLBACK cb)
lcb_observe3_ctxnew
lcb_MULTICMD_CTX * lcb_observe3_ctxnew(lcb_t instance)
Create a new multi context for an observe operation.