Skip to content

Commit a85aa54

Browse files
committed
Add libconflate and libvbucket
Change-Id: If3171cc7bb28d335f4540dc8fd40a6c59487a163 Reviewed-on: http://review.couchbase.org/34636 Reviewed-by: Trond Norbye <[email protected]> Tested-by: Trond Norbye <[email protected]>
1 parent 5affe2d commit a85aa54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5832
-3
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
*.[ao]
2+
*.dll
3+
*.dylib
4+
*.so
25
*.exe
36
*.gcda
47
*.gcno
@@ -100,3 +103,9 @@ tags
100103
/moxi_sizes.exe.resource.txt
101104
/moxi_sizes.ilk
102105
/moxi_sizes.pdb
106+
/tests_check_kvpair
107+
/vbucket_regression
108+
/vbucket_testapp
109+
/vbucket_testketama
110+
/vbucketkeygen
111+
/vbuckettool

CMakeLists.txt

+127-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,136 @@ INCLUDE_DIRECTORIES(BEFORE ${DEPS_INCLUDE_DIR}
66
${LIBEVENT_INCLUDE_DIR}
77
${CMAKE_INSTALL_PREFIX}/include
88
${CMAKE_CURRENT_SOURCE_DIR}/include
9-
${CMAKE_CURRENT_SOURCE_DIR}/../platform/include
10-
${CMAKE_CURRENT_SOURCE_DIR}/../libconflate/include
11-
${CMAKE_CURRENT_SOURCE_DIR}/../libmemcached
9+
${CMAKE_SOURCE_DIR}/platform/include
10+
${CMAKE_SOURCE_DIR}/libmemcached
1211
${CMAKE_CURRENT_BINARY_DIR}
1312
${CMAKE_CURRENT_SOURCE_DIR})
1413

14+
INCLUDE_DIRECTORIES(AFTER ${CURL_INCLUDE_DIRS})
15+
16+
17+
ADD_LIBRARY(conflate SHARED
18+
conflate/adhoc_commands.c
19+
conflate/conflate.c
20+
conflate/conflate_internal.h
21+
conflate/kvpair.c
22+
conflate/logging.c
23+
conflate/persist.c
24+
conflate/rest.c
25+
conflate/rest.h
26+
conflate/util.c
27+
conflate/xmpp.c)
28+
29+
IF(WIN32)
30+
INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/win32)
31+
INCLUDE_DIRECTORIES(AFTER ${CMAKE_SOURCE_DIR}/platform/include/win32)
32+
ADD_DEFINITIONS(-Dsnprintf=_snprintf)
33+
ELSE(WIN32)
34+
SET(ZLIB z)
35+
ENDIF(WIN32)
36+
37+
TARGET_LINK_LIBRARIES(conflate ${CURL_LIBRARIES} platform ${ZLIB})
38+
SET_TARGET_PROPERTIES(conflate PROPERTIES SOVERSION 1.0.0)
39+
SET_TARGET_PROPERTIES(conflate PROPERTIES COMPILE_FLAGS
40+
-DBUILDING_LIBCONFLATE=1)
41+
42+
IF (INSTALL_HEADER_FILES)
43+
INSTALL(FILES include/libconflate/conflate.h
44+
DESTINATION include/libconflate)
45+
ENDIF (INSTALL_HEADER_FILES)
46+
47+
INSTALL(TARGETS conflate
48+
RUNTIME DESTINATION bin
49+
LIBRARY DESTINATION lib
50+
ARCHIVE DESTINATION lib)
51+
52+
ADD_EXECUTABLE(tests_check_kvpair
53+
include/libconflate/conflate.h
54+
tests/conflate/check_kvpair.c
55+
tests/conflate/test_common.c
56+
tests/conflate/test_common.h)
57+
TARGET_LINK_LIBRARIES(tests_check_kvpair conflate)
58+
ADD_TEST(libconflate-test-suite tests_check_kvpair)
59+
60+
ADD_LIBRARY(vbucket SHARED
61+
include/libvbucket/vbucket.h
62+
include/libvbucket/visibility.h
63+
vbucket/crc32.c
64+
vbucket/hash.h
65+
vbucket/hash.h
66+
vbucket/ketama.c
67+
vbucket/rfc1321/global.h
68+
vbucket/rfc1321/md5.h
69+
vbucket/vbucket.c)
70+
71+
SET_TARGET_PROPERTIES(vbucket PROPERTIES SOVERSION 1.1.1)
72+
SET_TARGET_PROPERTIES(vbucket PROPERTIES
73+
COMPILE_FLAGS -DBUILDING_LIBVBUCKET=1)
74+
SET_TARGET_PROPERTIES(vbucket PROPERTIES INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib)
75+
76+
IF (WIN32)
77+
TARGET_LINK_LIBRARIES(vbucket cJSON)
78+
ELSE (WIN32)
79+
TARGET_LINK_LIBRARIES(vbucket cJSON m)
80+
ENDIF (WIN32)
81+
82+
IF (INSTALL_HEADER_FILES)
83+
INSTALL(FILES
84+
include/libvbucket/vbucket.h
85+
include/libvbucket/visibility.h
86+
DESTINATION include/libvbucket)
87+
ENDIF(INSTALL_HEADER_FILES)
88+
89+
INSTALL(TARGETS vbucket
90+
RUNTIME DESTINATION bin
91+
LIBRARY DESTINATION lib
92+
ARCHIVE DESTINATION lib)
93+
94+
ADD_EXECUTABLE(vbuckettool
95+
include/libvbucket/vbucket.h
96+
include/libvbucket/visibility.h
97+
vbucket/vbuckettool.c)
98+
TARGET_LINK_LIBRARIES(vbuckettool vbucket)
99+
100+
ADD_EXECUTABLE(vbucketkeygen
101+
include/libvbucket/vbucket.h
102+
include/libvbucket/visibility.h
103+
vbucket/vbucketkeygen.c)
104+
TARGET_LINK_LIBRARIES(vbucketkeygen vbucket)
105+
106+
#
107+
# The tests. These are automatically executed as part of the build!
108+
#
109+
110+
ADD_EXECUTABLE(vbucket_testapp
111+
include/libvbucket/vbucket.h
112+
include/libvbucket/visibility.h
113+
tests/vbucket/macros.h
114+
tests/vbucket/testapp.c)
115+
TARGET_LINK_LIBRARIES(vbucket_testapp vbucket)
116+
117+
ADD_EXECUTABLE(vbucket_regression
118+
include/libvbucket/vbucket.h
119+
include/libvbucket/visibility.h
120+
tests/vbucket/macros.h
121+
tests/vbucket/regression.c)
122+
TARGET_LINK_LIBRARIES(vbucket_regression vbucket)
123+
124+
ADD_EXECUTABLE(vbucket_testketama
125+
vbucket/ketama.c
126+
vbucket/rfc1321/global.h
127+
vbucket/rfc1321/md5.h
128+
tests/vbucket/testketama.c)
129+
TARGET_LINK_LIBRARIES(vbucket_testketama vbucket)
130+
131+
ADD_TEST(vbucket-basic-tests vbucket_testapp ${CMAKE_CURRENT_SOURCE_DIR})
132+
ADD_TEST(vbucket-regression-tests vbucket_regression ${CMAKE_CURRENT_SOURCE_DIR})
133+
ADD_TEST(vbucket-ketama-tests vbucket_testketama)
134+
135+
136+
137+
138+
15139
IF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
16140
SET(UMEM_LIBRARY -lumem)
17141
SET(PRVILEGES_SOURCES src/solaris_priv.c)

conflate/adhoc_commands.c

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <assert.h>
5+
6+
#include <libconflate/conflate.h>
7+
#include "conflate_internal.h"
8+
9+
bool commands_initialized = false;
10+
11+
static enum conflate_mgmt_cb_result process_serverlist(void *opaque,
12+
conflate_handle_t *handle,
13+
const char *cmd,
14+
bool direct,
15+
kvpair_t *conf,
16+
conflate_form_result *r)
17+
{
18+
(void)opaque;
19+
(void)cmd;
20+
(void)r;
21+
22+
/* If we have "config_is_private" set to "yes" we should only
23+
process this if it's direct (i.e. ignore pubsub) */
24+
if (!direct) {
25+
char *priv = conflate_get_private(handle, "config_is_private",
26+
handle->conf->save_path);
27+
28+
if (priv && strcmp(priv, "yes") == 0) {
29+
handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
30+
"Currently using a private config, ignoring update.");
31+
return RV_OK;
32+
}
33+
free(priv);
34+
}
35+
36+
handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
37+
"Processing a serverlist");
38+
39+
/* Persist the config lists */
40+
if (!save_kvpairs(handle, conf, handle->conf->save_path)) {
41+
handle->conf->log(handle->conf->userdata, LOG_LVL_ERROR,
42+
"Can not save config to %s",
43+
handle->conf->save_path);
44+
}
45+
46+
/* Send the config to the callback */
47+
handle->conf->new_config(handle->conf->userdata, conf);
48+
49+
return RV_OK;
50+
}
51+
52+
static enum conflate_mgmt_cb_result process_set_private(void *opaque,
53+
conflate_handle_t *handle,
54+
const char *cmd,
55+
bool direct,
56+
kvpair_t *form,
57+
conflate_form_result *r)
58+
{
59+
enum conflate_mgmt_cb_result rv = RV_ERROR;
60+
61+
char *key = get_simple_kvpair_val(form, "key");
62+
char *value = get_simple_kvpair_val(form, "value");
63+
64+
(void)opaque;
65+
(void)cmd;
66+
(void)r;
67+
68+
/* Only direct stat requests are handled. */
69+
assert(direct);
70+
71+
if (key && value) {
72+
if (conflate_save_private(handle, key, value,
73+
handle->conf->save_path)) {
74+
rv = RV_OK;
75+
}
76+
} else {
77+
rv = RV_BADARG;
78+
}
79+
80+
return rv;
81+
}
82+
83+
static enum conflate_mgmt_cb_result process_get_private(void *opaque,
84+
conflate_handle_t *handle,
85+
const char *cmd,
86+
bool direct,
87+
kvpair_t *form,
88+
conflate_form_result *r)
89+
{
90+
enum conflate_mgmt_cb_result rv = RV_ERROR;
91+
char *key = get_simple_kvpair_val(form, "key");
92+
(void)opaque;
93+
(void)cmd;
94+
95+
/* Only direct stat requests are handled. */
96+
assert(direct);
97+
98+
if (key) {
99+
/* Initialize the form so there's always one there */
100+
char *value;
101+
conflate_init_form(r);
102+
value = conflate_get_private(handle, key, handle->conf->save_path);
103+
if (value) {
104+
conflate_add_field(r, key, value);
105+
free(value);
106+
}
107+
108+
rv = RV_OK;
109+
} else {
110+
rv = RV_BADARG;
111+
}
112+
113+
return rv;
114+
}
115+
116+
static enum conflate_mgmt_cb_result process_delete_private(void *opaque,
117+
conflate_handle_t *handle,
118+
const char *cmd,
119+
bool direct,
120+
kvpair_t *form,
121+
conflate_form_result *r)
122+
{
123+
enum conflate_mgmt_cb_result rv = RV_ERROR;
124+
char *key = get_simple_kvpair_val(form, "key");
125+
(void)opaque;
126+
(void)cmd;
127+
(void)r;
128+
129+
/* Only direct stat requests are handled. */
130+
assert(direct);
131+
132+
if (key) {
133+
if (conflate_delete_private(handle, key, handle->conf->save_path)) {
134+
rv = RV_OK;
135+
}
136+
} else {
137+
rv = RV_BADARG;
138+
}
139+
140+
return rv;
141+
}
142+
143+
void conflate_init_commands(void)
144+
{
145+
if (commands_initialized) {
146+
return;
147+
}
148+
149+
conflate_register_mgmt_cb("set_private",
150+
"Set a private value on the agent.",
151+
process_set_private);
152+
conflate_register_mgmt_cb("get_private",
153+
"Get a private value from the agent.",
154+
process_get_private);
155+
conflate_register_mgmt_cb("rm_private",
156+
"Delete a private value from the agent.",
157+
process_delete_private);
158+
159+
conflate_register_mgmt_cb("serverlist", "Configure a server list.",
160+
process_serverlist);
161+
162+
commands_initialized = true;
163+
}

conflate/conflate.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <libconflate/conflate.h>
6+
#include "conflate_internal.h"
7+
#include "rest.h"
8+
9+
/* Randomly generated by a fair dice roll */
10+
#define INITIALIZATION_MAGIC 142285011
11+
#define HTTP_PREFIX "HTTP:"
12+
13+
void run_conflate(void *);
14+
15+
16+
conflate_config_t* dup_conf(conflate_config_t c) {
17+
conflate_config_t *rv = calloc(sizeof(conflate_config_t), 1);
18+
assert(rv);
19+
20+
rv->jid = safe_strdup(c.jid);
21+
rv->pass = safe_strdup(c.pass);
22+
if (c.host) {
23+
rv->host = safe_strdup(c.host);
24+
}
25+
rv->software = safe_strdup(c.software);
26+
rv->version = safe_strdup(c.version);
27+
rv->save_path = safe_strdup(c.save_path);
28+
rv->userdata = c.userdata;
29+
rv->log = c.log;
30+
rv->new_config = c.new_config;
31+
32+
rv->initialization_marker = (void*)INITIALIZATION_MAGIC;
33+
34+
return rv;
35+
}
36+
37+
void init_conflate(conflate_config_t *conf)
38+
{
39+
assert(conf);
40+
memset(conf, 0x00, sizeof(conflate_config_t));
41+
conf->log = conflate_stderr_logger;
42+
conf->initialization_marker = (void*)INITIALIZATION_MAGIC;
43+
}
44+
45+
bool start_conflate(conflate_config_t conf) {
46+
conflate_handle_t *handle;
47+
void (*run_func)(void*) = NULL;
48+
49+
/* Don't start if we don't believe initialization has occurred. */
50+
if (conf.initialization_marker != (void*)INITIALIZATION_MAGIC) {
51+
assert(conf.initialization_marker == (void*)INITIALIZATION_MAGIC);
52+
return false;
53+
}
54+
55+
handle = calloc(1, sizeof(conflate_handle_t));
56+
assert(handle);
57+
58+
if (strncmp(HTTP_PREFIX, conf.host, strlen(HTTP_PREFIX))) {
59+
run_func = &run_rest_conflate;
60+
} else {
61+
run_func = &run_conflate;
62+
conflate_init_commands();
63+
}
64+
65+
handle->conf = dup_conf(conf);
66+
67+
if (cb_create_thread(&handle->thread, run_func, handle, 1) == 0) {
68+
return true;
69+
} else {
70+
perror("Failed to create thread");
71+
}
72+
73+
return false;
74+
}

0 commit comments

Comments
 (0)