Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API call for retrieving db index #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/etcdlib/api/etcdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ int etcdlib_del(etcdlib_t *etcdlib, const char* key);
*/
int etcdlib_watch(etcdlib_t *etcdlib, const char* key, long long index, char** action, char** prevValue, char** value, char** rkey, long long* modifiedIndex);

/**
* @desc Retrieve the current database index
* @param const etcdlib_t* etcdlib. The ETCD-LIB instance
* @param int* modifiedIndex. The X-Etcd-Index value as retrieved from the etcd server.
*/
int etcdlib_get_db_index(etcdlib_t *etcdlib, int* modifiedIndex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename modifiedIndex to currentEtcdIndex


#ifdef __cplusplus
}
#endif
Expand Down
34 changes: 34 additions & 0 deletions libs/etcdlib/src/etcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,40 @@ static long long etcd_get_current_index(const char* headerData) {
}


int etcdlib_get_db_index(etcdlib_t *etcdlib, int* modifiedIndex) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename modifiedIndex to currentEtcdIndex


int res = -1;
struct MemoryStruct reply;

reply.memory = malloc(1); /* will be grown as needed by the realloc above */
reply.memorySize = 0; /* no data at this point */
reply.header = malloc(1); /* will be grown as needed by the realloc above */
reply.headerSize = 0; /* no data at this point */

int retVal = ETCDLIB_RC_OK;
char *url;
asprintf(&url, "http://%s:%d/v2/keys", etcdlib->host, etcdlib->port);
res = performRequest(&etcdlib->curl, &etcdlib->mutex, url, GET, NULL, (void *) &reply);
free(url);

if (res == CURLE_OK) {
long long indexFromHeader = etcd_get_current_index(reply.header);
*modifiedIndex = (int)indexFromHeader;
} else if (res == CURLE_OPERATION_TIMEDOUT) {
retVal = ETCDLIB_RC_TIMEOUT;
} else {
retVal = ETCDLIB_RC_ERROR;
fprintf(stderr, "Error getting etcd value, curl error: '%s'\n", curl_easy_strerror(res));
}

if (reply.memory) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it happens multiple times in the current code, would you mind removing the if statement here? free() explicitly handles NULL values properly by ignoring them.

free(reply.memory);
}
free(reply.header);
return retVal;
}


int etcd_get_directory(const char* directory, etcdlib_key_value_callback callback, void* arg, long long* modifiedIndex) {
return etcdlib_get_directory(&g_etcdlib, directory, callback, arg, modifiedIndex);
}
Expand Down