Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(cache): Set the cache default as off
Browse files Browse the repository at this point in the history
With command `--cache Y`, user can turn on cache server in CLI.
  • Loading branch information
howjmay committed Jun 29, 2019
1 parent 2d71fc4 commit e2c7c7c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion accelerator/common_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, attach_to_
flex_trits_to_trytes((tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, elt,
NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION);
ret = cache_set(cache_key, cache_value);
if (ret) {
if (ret != SC_OK && ret != SC_CACHE_OFF) {
goto done;
}
}
Expand Down
8 changes: 6 additions & 2 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ status_t cli_config_set(ta_config_t* const info, iota_config_t* const tangle, ta
case SEED_CLI:
tangle->seed = value;
break;
case CACHE:
cache->cache_state = !(strncmp(value, "T", 1));
}
return SC_OK;
}
Expand All @@ -91,6 +93,7 @@ status_t ta_config_default_init(ta_config_t* const info, iota_config_t* const ta
log_info(logger_id, "Initializing Redis information\n");
cache->host = REDIS_HOST;
cache->port = REDIS_PORT;
cache->cache_state = false;

log_info(logger_id, "Initializing IRI configuration\n");
tangle->milestone_depth = MILESTONE_DEPTH;
Expand Down Expand Up @@ -155,8 +158,9 @@ status_t ta_config_set(ta_cache_t* const cache, iota_client_service_t* const ser
log_info(logger_id, "Initializing PoW implementation context\n");
pow_init();

log_info(logger_id, "Initializing cache connection\n");
cache_init(cache->host, cache->port);
log_info(logger_id, "Initializing cache state\n");
cache_init(cache->cache_state, cache->host, cache->port);

return ret;
}

Expand Down
5 changes: 3 additions & 2 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ typedef struct ta_config_s {

/** struct type of accelerator cache */
typedef struct ta_cache_s {
char* host; /**< Binding address of redis server */
uint16_t port; /**< Binding port of redis server */
bool cache_state; /** set it true to turn on cache server */
char* host; /**< Binding address of redis server */
uint16_t port; /**< Binding port of redis server */
} ta_cache_t;

/** struct type of accelerator core */
Expand Down
2 changes: 2 additions & 0 deletions accelerator/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ typedef enum {
/**< NULL parameters in cache */
SC_CACHE_FAILED_RESPONSE = 0x02 | SC_MODULE_CACHE | SC_SEVERITY_FATAL,
/**< Fail in cache operations */
SC_CACHE_OFF = 0x03 | SC_MODULE_CACHE | SC_SEVERITY_MINOR,
/**< Cache server doesn't turn on */

// MAM module
SC_MAM_OOM = 0x01 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
Expand Down
4 changes: 3 additions & 1 deletion accelerator/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef enum ta_cli_arg_value_e {
MILESTONE_DEPTH_CLI,
MWM_CLI,
SEED_CLI,
CACHE,
} ta_cli_arg_value_t;

typedef enum ta_cli_arg_requirement_e { NO_ARG, REQUIRED_ARG, OPTIONAL_ARG } ta_cli_arg_requirement_t;
Expand All @@ -56,7 +57,8 @@ static struct ta_cli_argument_s {
{"redis_port", REDIS_PORT_CLI, "Redis server listening port", REQUIRED_ARG},
{"milestone_depth", MILESTONE_DEPTH_CLI, "IRI milestone depth", OPTIONAL_ARG},
{"mwm", MWM_CLI, "minimum weight magnitude", OPTIONAL_ARG},
{"seed", SEED_CLI, "IOTA seed", OPTIONAL_ARG}};
{"seed", SEED_CLI, "IOTA seed", OPTIONAL_ARG},
{"cache", CACHE, "Enable cache server with Y", REQUIRED_ARG}};

static const int cli_cmd_num = sizeof(ta_cli_arguments_g) / sizeof(struct ta_cli_argument_s);
void ta_usage();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void test_cache_set(void) {

int main(void) {
UNITY_BEGIN();
cache_init(REDIS_HOST, REDIS_PORT);
cache_init(true, REDIS_HOST, REDIS_PORT);
RUN_TEST(test_cache_set);
RUN_TEST(test_cache_get);
RUN_TEST(test_cache_del);
Expand Down
4 changes: 2 additions & 2 deletions tests/test_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "accelerator/common_core.h"
#include "iota_api_mock.hh"

using ::testing::_;
using ::testing::AtLeast;
using ::testing::ElementsAreArray;
using ::testing::_;

APIMock APIMockObj;
iota_config_t tangle;
Expand Down Expand Up @@ -183,7 +183,7 @@ TEST(SendTrytesTest, SendTrytesTest) {

int main(int argc, char** argv) {
// GTest manage to cleanup after testing, so only need to initialize here
cache_init(REDIS_HOST, REDIS_PORT);
cache_init(true, REDIS_HOST, REDIS_PORT);
::testing::GTEST_FLAG(throw_on_failure) = true;
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
35 changes: 26 additions & 9 deletions utils/backend_redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ typedef struct {
#define CONN(c) ((connection_private*)(c.conn))

static cache_t cache;

static bool cache_state;
/*
* Private functions
*/

static status_t redis_del(redisContext* c, const char* const key) {
status_t ret = SC_OK;

if (key == NULL) {
return SC_CACHE_NULL;
}
Expand All @@ -39,7 +38,6 @@ static status_t redis_del(redisContext* c, const char* const key) {

static status_t redis_get(redisContext* c, const char* const key, char* res) {
status_t ret = SC_OK;

if (key == NULL || res[0] != '\0') {
return SC_CACHE_NULL;
}
Expand All @@ -57,7 +55,6 @@ static status_t redis_get(redisContext* c, const char* const key, char* res) {

static status_t redis_set(redisContext* c, const char* const key, const char* const value) {
status_t ret = SC_OK;

if (key == NULL || value == NULL) {
return SC_CACHE_NULL;
}
Expand All @@ -75,7 +72,12 @@ static status_t redis_set(redisContext* c, const char* const key, const char* co
* Public functions
*/

bool cache_init(const char* host, int port) {
bool cache_init(bool state, const char* host, int port) {
cache_state = state;
if (!state) {
return false;
}

cache.conn = (connection_private*)malloc(sizeof(connection_private));
CONN(cache)->rc = redisConnect(host, port);
if (CONN(cache)->rc) {
Expand All @@ -85,7 +87,7 @@ bool cache_init(const char* host, int port) {
}

void cache_stop() {
if (CONN(cache)->rc) {
if (cache_state == true && CONN(cache)->rc) {
redisFree(CONN(cache)->rc);

if (CONN(cache)) {
Expand All @@ -94,8 +96,23 @@ void cache_stop() {
}
}

status_t cache_del(const char* const key) { return redis_del(CONN(cache)->rc, key); }
status_t cache_del(const char* const key) {
if (!cache_state) {
return SC_CACHE_OFF;
}
return redis_del(CONN(cache)->rc, key);
}

status_t cache_get(const char* const key, char* res) { return redis_get(CONN(cache)->rc, key, res); }
status_t cache_get(const char* const key, char* res) {
if (!cache_state) {
return SC_CACHE_OFF;
}
return redis_get(CONN(cache)->rc, key, res);
}

status_t cache_set(const char* const key, const char* const value) { return redis_set(CONN(cache)->rc, key, value); }
status_t cache_set(const char* const key, const char* const value) {
if (!cache_state) {
return SC_CACHE_OFF;
}
return redis_set(CONN(cache)->rc, key, value);
}
4 changes: 2 additions & 2 deletions utils/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ typedef struct {
/**
* Initiate cache module
*
* @param[in] state if cache server should open
* @param[in] host cache server host
* @param[in] port cache server port
*
* @return
* - True on success
* - False on error
*/
bool cache_init(const char* host, int port);
bool cache_init(bool state, const char* host, int port);

/**
* Stop interacting with cache module
Expand Down

0 comments on commit e2c7c7c

Please sign in to comment.