diff --git a/src/include/pgagroal.h b/src/include/pgagroal.h index aff0d3eb..e32c2bec 100644 --- a/src/include/pgagroal.h +++ b/src/include/pgagroal.h @@ -253,6 +253,11 @@ extern void* prometheus_cache_shmem; * response cache. */ extern void* query_cache_shmem; +/** + * Shared memory used to contain the Prometheus + * response cache. + */ +extern void* client_server_shmem; /** @struct * Defines a server @@ -359,6 +364,13 @@ struct prometheus_cache size_t size; /**< size of the cache */ char data[]; /**< the payload */ } __attribute__ ((aligned (64))); +struct client_server_cache +{ + char kind; + atomic_schar lock; + size_t key_length; + char key[]; +} __attribute__ ((aligned (64))); /** @struct * Defines the Prometheus metrics @@ -424,6 +436,13 @@ struct query_cache atomic_schar lock; /**< lock to protect the cache */ size_t size; /**< size of the cache */ struct hashTable* table; + struct cachev2 + { + struct hashEntry* key; + struct hashEntry* data; + } cache[100000]; + int max_elements; + } __attribute__ ((aligned (64))); struct hashTable @@ -437,6 +456,7 @@ struct hashEntry { void* value; size_t length; + char key[]; } __attribute__ ((aligned (64))); /** @struct diff --git a/src/include/query_cache.h b/src/include/query_cache.h index cb3f5347..dcbb10a6 100644 --- a/src/include/query_cache.h +++ b/src/include/query_cache.h @@ -80,7 +80,7 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem); * * @warning The caller should ensure the validity of the 'Table' pointer and the 'key' pointer. */ -struct hashTable* +struct hashEntry* pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key); /** diff --git a/src/libpgagroal/pipeline_session.c b/src/libpgagroal/pipeline_session.c index 951ffde9..5e07f3c0 100644 --- a/src/libpgagroal/pipeline_session.c +++ b/src/libpgagroal/pipeline_session.c @@ -75,17 +75,8 @@ struct client_session time_t timestamp; /**< The last used timestamp */ }; -struct cache_key -{ - char* key; - char kind; - atomic_schar lock; - ssize_t key_length; -}; - static void client_active(int); static void client_inactive(int); -void* client_server_shmem = NULL; struct pipeline session_pipeline(void) @@ -110,22 +101,25 @@ session_initialize(void* shmem, void** pipeline_shmem, size_t* pipeline_shmem_si size_t session_shmem_size; struct client_session* client; struct configuration* config; - struct cache_key* cache_key; + struct client_server_cache* cache_key; config = (struct configuration*)shmem; + client_server_shmem = NULL; *pipeline_shmem = NULL; *pipeline_shmem_size = 0; - if (pgagroal_create_shared_memory(QUERY_KEY_SIZE, config->hugepage, (void*)&cache_key)) + if (pgagroal_create_shared_memory(sizeof(struct client_server_cache) + QUERY_KEY_SIZE, config->hugepage, (void*)&cache_key)) { return 1; } - memset(cache_key, 0, QUERY_KEY_SIZE); + memset(cache_key, 0, sizeof(struct client_server_cache) + QUERY_KEY_SIZE); + memset(cache_key->key, 0, QUERY_KEY_SIZE); - cache_key->key = NULL; cache_key->kind = '\0'; cache_key->key_length = 0; atomic_init(&cache_key->lock, STATE_FREE); + pgagroal_log_info("INIT LOCK STATE %d", cache_key->lock); + client_server_shmem = cache_key; if (config->disconnect_client > 0) @@ -204,10 +198,6 @@ session_destroy(void* pipeline_shmem, size_t pipeline_shmem_size) { pgagroal_destroy_shared_memory(pipeline_shmem, pipeline_shmem_size); } - if (client_server_shmem != NULL) - { - pgagroal_destroy_shared_memory(client_server_shmem, QUERY_KEY_SIZE); - } } static void @@ -314,15 +304,17 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents) struct worker_io* wi = NULL; struct message* msg = NULL; struct configuration* config = NULL; - struct cache_key* client_query = NULL; + struct client_server_cache* client_query = NULL; wi = (struct worker_io*)watcher; config = (struct configuration*)shmem; - client_query = (struct cache_key*)client_server_shmem; + client_query = (struct client_server_cache*)client_server_shmem; struct query_cache* cache; cache = (struct query_cache*)query_cache_shmem; + pgagroal_log_info("CACHE_SHMEM LOCK STATE %d, client slot %d", client_query->lock, wi->slot); + client_active(wi->slot); if (wi->client_ssl == NULL) @@ -339,16 +331,16 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents) if (likely(msg->kind != 'X')) { - char* query_with_semicolon = msg->data + 5; - if (msg->kind == 'Q' && query_with_semicolon[strlen(query_with_semicolon) - 1] == ';') - { - pgagroal_log_debug("Q message found"); - - signed char cache_is_free; + signed char cache_is_free; retry_cache_get: - cache_is_free = STATE_FREE; - if (atomic_compare_exchange_strong(&client_query->lock, &cache_is_free, STATE_IN_USE)) + cache_is_free = STATE_FREE; + if (atomic_compare_exchange_strong(&client_query->lock, &cache_is_free, STATE_IN_USE)) + { + // char* query_with_semicolon = msg->data + 5; + if (msg->kind == 'Q') { + pgagroal_log_debug("Q message found"); + pgagroal_log_info("Lock acquired by client %d", wi->client_fd); pgagroal_log_debug("msg: kind %c, strlen %d", msg->kind, strlen(msg->data + 5)); @@ -357,29 +349,31 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents) size_t key_length = strlen(msg->data + 5); // allocate 25 bytes for key \0 - client_query->key = malloc(key_length + 1); - if (client_query->key == NULL) - { - pgagroal_log_fatal("failed to allocate memory for key"); - client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); - atomic_store(&client_query->lock, STATE_FREE); - return; - } - // set 25 bytes to 0 - memset(client_query->key, 0, key_length + 1); + // client_query->key = malloc(key_length + 1); + // if (client_query->key == NULL) + // { + // pgagroal_log_fatal("failed to allocate memory for key"); + // client_inactive(wi->slot); + // ev_break(loop, EVBREAK_ONE); + // atomic_store(&client_query->lock, STATE_FREE); + // return; + // } + // // set 25 bytes to 0 + memset(client_query->key, 0, QUERY_KEY_SIZE); + memcpy(client_query->key, msg->data + 5, key_length); + client_query->key[key_length + 1] = '\0'; // copy key to cache_key->key - strcpy(client_query->key, msg->data + 5); + // strcpy(client_query->key, msg->data + 5); // client_query->key[strlen(client_query->key)] = '\0'; client_query->key_length = key_length; client_query->kind = msg->kind; - pgagroal_log_info("print using shmem %s, %d", client_query->key, client_query->key_length); + // pgagroal_log_info("print using shmem %s, %d", client_query->key, client_query->key_length); struct hashEntry* key = NULL; - key = (struct hashEntry*)malloc(sizeof *key); + key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + key_length + 1); if (key == NULL) { pgagroal_log_fatal("failed to allocate memory for key"); @@ -390,69 +384,70 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents) return; } // allocate 25 bytes for key->value - key->value = (char*)malloc(key_length + 1); - if (key->value == NULL) - { - pgagroal_log_fatal("failed to allocate memory for key"); - client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); + // key->value = (char*)malloc(key_length + 1); + // if (key->value == NULL) + // { + // pgagroal_log_fatal("failed to allocate memory for key"); + // client_inactive(wi->slot); + // ev_break(loop, EVBREAK_ONE); + // atomic_store(&client_query->lock, STATE_FREE); - atomic_store(&client_query->lock, STATE_FREE); - return; - } + // return; + // } // initialize 25 memory blocks properly - memset(key->value, 0, key_length + 1); + memset(key->key, 0, key_length + 1); - strcpy(key->value, client_query->key); + memcpy(key->key, msg->data + 5, key_length); + key->key[key_length + 1] = '\0'; key->length = key_length; - struct hashTable* s = pgagroal_query_cache_get(cache, &(cache->table), key); + struct hashEntry* s = pgagroal_query_cache_get(cache, &(cache->table), key); + // struct hashEntry* s = NULL; // free memory for key->value and key - free(key->value); + free(key); - if (s != NULL) + if (s != NULL && s->value != NULL) { pgagroal_log_info("USING CACHED ENTRY"); pgagroal_log_warn("CACHE_QUERY LOCK STATE %d", cache->lock); struct message* result = NULL; - pgagroal_log_warn("CACHED_ENTRY %s, %d", s->data->value, s->data->length); + pgagroal_log_warn("CACHED_ENTRY %s, %d", s->value, s->length); - int x = pgagroal_create_message(s->data->value, s->data->length, &result); + int x = pgagroal_create_message(s->value, s->length, &result); // log - pgagroal_log_info("DONE: %d", x); - pgagroal_log_info("res: %s, %d", result->data, result->length); + pgagroal_log_fatal("DONE: %d", x); + // pgagroal_log_info("res: %s, %d", result->data, result->length); status = pgagroal_write_socket_message(wi->client_fd, result); pgagroal_log_info("status %d", status); client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); - atomic_store(&client_query->lock, STATE_FREE); + pgagroal_log_debug("CACHE IS IN USE"); pgagroal_log_debug("CACHE_QUERY STATE %d", cache->lock); + //release lock + ev_break(loop, EVBREAK_ONE); + atomic_store(&client_query->lock, STATE_FREE); return; } - else - { - pgagroal_log_info("CACHE MISS"); - } - atomic_store(&client_query->lock, STATE_FREE); - } - else - { - pgagroal_log_debug("CACHE IS IN USE"); - pgagroal_log_debug("CACHE_QUERY STATE %d", cache->lock); + pgagroal_log_info("CACHE MISS"); - SLEEP_AND_GOTO(1000000L, retry_cache_get) } + atomic_store(&client_query->lock, STATE_FREE); + } + else + { + pgagroal_log_debug("CACHE IS IN USE %d", client_query->lock); + pgagroal_log_debug("CACHE_QUERY STATE %d", cache->lock); + SLEEP_AND_GOTO(1000000L, retry_cache_get); } - pgagroal_log_warn("CACHE LOCK STATE %d, shq %s, msgQ %s", client_query->lock, client_query->key, msg->data + 5); + // pgagroal_log_warn("CACHE LOCK STATE %d, shq %s, msgQ %s", client_query->lock, client_query->key, msg->data + 5); pgagroal_log_warn("OUT"); pgagroal_log_warn("CACHE_SHMEM LOCK STATE %d", client_query->lock); pgagroal_log_warn("CACHE_QUERY LOCK STATE %d", cache->lock); @@ -604,8 +599,8 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) struct worker_io* wi = NULL; struct message* msg = NULL; struct configuration* config = NULL; - struct cache_key* client_query = NULL; - client_query = (struct cache_key*)client_server_shmem; + struct client_server_cache* client_query = NULL; + client_query = (struct client_server_cache*)client_server_shmem; //Query Cache struct query_cache* cache; @@ -627,6 +622,7 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) { pgagroal_prometheus_network_received_add(msg->length); + signed char cache_is_free; retry_cache_add: @@ -637,18 +633,18 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) if (msg->kind == 'T' && client_query->kind == 'Q' && strlen(client_query->key) == client_query->key_length) { - pgagroal_log_warn("INSIDE SERVER msg %c, %s", client_query->kind, client_query->key); + // pgagroal_log_warn("INSIDE SERVER msg %c, %s", client_query->kind, client_query->key); struct message* tmp = NULL; if (!pgagroal_extract_message('Z', msg, &tmp)) { pgagroal_log_info("START"); pgagroal_log_info("MSG Size %d", msg->length); pgagroal_log_info("KEY Size %d", client_query->key_length); - pgagroal_log_info("KEY %s", client_query->key); + // pgagroal_log_info("KEY %s", client_query->key); struct hashEntry* key, * data; - key = (struct hashEntry*)malloc(sizeof *key); - data = (struct hashEntry*)malloc(sizeof *data); + key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + client_query->key_length + 1); + data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + msg->length); // Check if memory allocation was successful if (key == NULL || data == NULL) @@ -666,37 +662,28 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) } client_inactive(wi->slot); ev_break(loop, EVBREAK_ONE); + //release lock atomic_store(&client_query->lock, STATE_FREE); return; } // Use memset to initialize the memory blocks properly - memset(key, 0, sizeof *key); - memset(data, 0, sizeof *data); - pgagroal_log_info("str len, size %d, %d, %s", client_query->key_length, strlen(client_query->key), client_query->key); - key->value = (char*)malloc(client_query->key_length + 1); + // pgagroal_log_info("str len, size %d, %d, %s", client_query->key_length, strlen(client_query->key), client_query->key); + + // key->value = (char*)malloc(client_query->key_length + 1); data->value = malloc(msg->length); + memset(key->key, 0, client_query->key_length + 1); // Check if memory allocation for key->value and data->value was successful - if (key->value == NULL || data->value == NULL) + if (data->value == NULL) { pgagroal_log_fatal("memory allocaltion failed"); - if (key->value != NULL) - { - free(key->value); - key->value = NULL; - } if (data->value != NULL) { free(data->value); data->value = NULL; } - if (key != NULL) - { - free(key); - key = NULL; - } if (data != NULL) { free(data); @@ -704,12 +691,13 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) } client_inactive(wi->slot); ev_break(loop, EVBREAK_ONE); + //release lock atomic_store(&client_query->lock, STATE_FREE); return; } - memset(key->value, 0, client_query->key_length + 1); - strcpy(key->value, client_query->key); + memcpy(key->key, client_query->key, client_query->key_length); + key->key[client_query->key_length + 1] = '\0'; key->length = client_query->key_length; memset(data->value, 0, msg->length); @@ -718,17 +706,9 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) int cache_add_status = pgagroal_query_cache_add(cache, &(cache->table), data, key, 1); pgagroal_log_info("CACHE ADD STATUS: %d", cache_add_status); - if (client_query->key != NULL) - { - free(client_query->key); - client_query->key = NULL; - } - - free(key->value); - free(data->value); - free(key); - free(data); atomic_store(&client_query->lock, STATE_FREE); + free(data); + free(key); } else @@ -739,16 +719,8 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) } else { - pgagroal_log_info("MESSAGE DID NOT HAVE T (ROW DESCRIPTION), becasue query is %s", client_query->key); - - if (client_query->key != NULL) - { - free(client_query->key); - client_query->key = NULL; - } - + pgagroal_log_info("MESSAGE DID NOT HAVE T (ROW DESCRIPTION)"); atomic_store(&client_query->lock, STATE_FREE); - } } else diff --git a/src/libpgagroal/query_cache.c b/src/libpgagroal/query_cache.c index bd801602..47541dae 100644 --- a/src/libpgagroal/query_cache.c +++ b/src/libpgagroal/query_cache.c @@ -45,7 +45,7 @@ #include #define QUERY_CACHE_MAX_SIZE 1024 * 1024 * 256 -#define QUERY_CACHE_MAX_ENTRIES 1 +#define QUERY_CACHE_MAX_ENTRIES 1000 #define HASH_ENTRY_DATA_SIZE 1024 * 1024 //1MB #define HASH_ENTRY_KEY_SIZE 1024 //1KB @@ -73,10 +73,9 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem) memset(cache, 0, cache_size); cache->size = cache_size; // initalizing the hash table - struct hashTable* Table = NULL; - cache->table = Table; - atomic_init(&cache->lock, STATE_FREE); + atomic_init(&cache->lock, STATE_FREE); + cache->max_elements = 0; // success! do the memory swap *p_shmem = cache; *p_size = cache_size; @@ -91,7 +90,7 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem) return 1; } -struct hashTable* +struct hashEntry* pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key) { signed char cache_is_free; @@ -102,39 +101,32 @@ pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, st if (atomic_compare_exchange_strong(&cache->lock, &cache_is_free, STATE_IN_USE)) { + // pgagroal_log_info("START: GETTING cache entry with key: %s ", key->key); - pgagroal_log_info("GET: Key: %s", key->value); - pgagroal_log_info("GET Stats strlen %d, length %d", strlen(key->value), key->length); + for (int i = 0; i < cache->max_elements; i++) + { + // pgagroal_log_info("GET %s", cache->cache[i].key->key); - struct hashTable* tmp = NULL; + int x = strncmp(cache->cache[i].key->key, key->key, key->length); + // pgagroal_log_info("GET X %d", x); - char* qkey = malloc(strlen(key->value) + 1); - memset(qkey, 0, strlen(key->value) + 1); - strcpy(qkey, key->value); - void* n = strchr(key->value, '\0'); - if (n) - { - pgagroal_log_info("HERE GET %d", n); + if (x == 0) + { + // Key found, return the corresponding data + atomic_store(&cache->lock, STATE_FREE); + return cache->cache[i].data; + } } + atomic_store(&cache->lock, STATE_FREE); - HASH_FIND(hh, *Table, qkey, strlen(qkey), tmp); - free(qkey); - if (tmp == NULL) - { - pgagroal_log_info("CACHE MISS"); - atomic_store(&cache->lock, STATE_FREE); - return NULL; - } + return NULL; - pgagroal_log_info("FREEING GET"); - atomic_store(&cache->lock, STATE_FREE); - return tmp; } else { - pgagroal_log_debug("LOCKED GET"); - SLEEP_AND_GOTO(1000000000000L, retry_get) + // pgagroal_log_info("LOCKED GET"); + SLEEP_AND_GOTO(1000000L, retry_get) } } @@ -179,115 +171,49 @@ pgagroal_query_cache_update(struct hashTable** Table, struct hashEntry* key, str int pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, struct hashEntry* key, int flag) { - signed char cache_is_free; + if (cache->max_elements >= QUERY_CACHE_MAX_ENTRIES) + { + pgagroal_log_info("fail %d", cache->max_elements); + + return -1; // Cache is full + } + signed char cache_is_free; retry_add: cache_is_free = STATE_FREE; if (atomic_compare_exchange_strong(&cache->lock, &cache_is_free, STATE_IN_USE)) { - // Successful lock, continue with cache entry addition - pgagroal_log_info("START: Adding cache entry with key: %s and data: %s", key->value, data->value); - pgagroal_log_info("ADD stats strlen key %d, length %d", strlen(key->value), key->length); - - if (key->length <= HASH_ENTRY_KEY_SIZE && data->length <= HASH_ENTRY_DATA_SIZE) - { - struct hashTable* s = NULL; - - void* n = strchr(key->value, '\0'); - if (n) - { - pgagroal_log_info("The string is key->val null terminated %d", n); - - } - //str - HASH_FIND_STR(*Table, key->value, s); + /* + add key and data to the cache + */ + // pgagroal_log_info("START: Adding cache entry with key: %s and data: %s", key->key, data->value); - if (s) - { - pgagroal_log_fatal("FOUND DUPLICATE!"); - atomic_store(&cache->lock, STATE_FREE); - return -1; - } - - s = (struct hashTable*)malloc(sizeof(struct hashTable)); - - if (s == NULL) - { - atomic_store(&cache->lock, STATE_FREE); - return -1; - } - memset(s, 0, sizeof *s); - - s->key = (struct hashEntry*)malloc(sizeof (struct hashEntry)); - s->data = (struct hashEntry*)malloc(sizeof (struct hashEntry)); - - if (s->key == NULL || s->data == NULL) - { - // free mem - if (s->key) - { - free(s->key); - } - if (s->data) - { - free(s->data); - } - free(s); + struct hashEntry* copy_key = NULL; + struct hashEntry* copy_data = NULL; + copy_key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + key->length + 1); + copy_data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + data->length); - atomic_store(&cache->lock, STATE_FREE); - return -1; - } - - // Deep copy key and data - s->key->length = key->length; - s->data->length = data->length; + copy_data->value = malloc(data->length); + memset(copy_key->key, 0, key->length + 1); - s->key->value = (char*)malloc(key->length + 1); - s->data->value = malloc(data->length); + memcpy(copy_key->key, key->key, key->length); + copy_key->key[key->length + 1] = '\0'; + copy_key->length = key->length; - if (s->key->value == NULL || s->data->value == NULL) - { - // free mem - if (s->key->value) - { - free(s->key->value); - } - if (s->data->value) - { - free(s->data->value); - } - free(s->key); - free(s->data); - free(s); - atomic_store(&cache->lock, STATE_FREE); - return -1; - } + memset(copy_data->value, 0, data->length); + memcpy(copy_data->value, data->value, data->length); + copy_data->length = data->length; - memset(s->key->value, 0, key->length + 1); - strcpy(s->key->value, key->value); - - memset(s->data->value, 0, data->length); - memcpy(s->data->value, data->value, data->length); - pgagroal_log_info("About to ADD stats %s strlen key %d, length %d", s->key->value, strlen(s->key->value), s->key->length); - - n = strchr(s->key->value, '\0'); - if (n) - { - pgagroal_log_info("The string s->key->val is null terminated %d", n); - - } - HASH_ADD_KEYPTR(hh, *Table, s->key->value, s->key->length, s); - atomic_store(&cache->lock, STATE_FREE); + cache->cache[cache->max_elements].key = copy_key; + cache->cache[cache->max_elements].data = copy_data; + cache->max_elements = cache->max_elements + 1; - pgagroal_log_info("FREE"); - return 1; - } atomic_store(&cache->lock, STATE_FREE); - return -1; + return 1; + } else { - // Failed to acquire lock, retry after sleeping SLEEP_AND_GOTO(1000000L, retry_add); } @@ -307,115 +233,115 @@ pgagroal_query_cache_clear(struct hashTable** Table) void pgagroal_query_cache_test(void) { - struct query_cache* cache; - - cache = (struct query_cache*)query_cache_shmem; - - struct hashEntry* key, * data; - key = (struct hashEntry*)malloc(sizeof *key); - memset(key, 0, sizeof *key); - data = (struct hashEntry*)malloc(sizeof *data); - memset(key, 0, sizeof *data); - key->value = "key"; - key->length = strlen(key->value); - data->value = "data"; - data->length = strlen(data->value); - pgagroal_log_info("Add cache entry with key: key and data: data"); - int x = pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); - if (x == 0) - { - pgagroal_log_info("Key already exists"); - } - else - { - pgagroal_log_info("Key added"); - } - pgagroal_log_info("Add cache entry with key: key and data: data"); - x = pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); - if (x == 0) - { - pgagroal_log_info("Key already exists"); - } - else - { - pgagroal_log_info("Key added"); - } - pgagroal_log_info("Get cache entry with key: key"); - struct hashTable* resp = pgagroal_query_cache_get(cache, &(cache->table), key); - if (resp != NULL) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } - pgagroal_log_info("Update cache entry with key: key and data: new data"); - struct hashEntry* ndata, * nKey; - ndata = (struct hashEntry*)malloc(sizeof *ndata); - ndata->value = "new data"; - ndata->length = strlen(ndata->value); - nKey = (struct hashEntry*)malloc(sizeof *nKey); - nKey->value = "newKey"; - nKey->length = strlen(nKey->value); - pgagroal_query_cache_update(&(cache->table), key, ndata); - resp = pgagroal_query_cache_get(cache, &(cache->table), key); - if (resp) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } - pgagroal_log_info("Invalidate cache entry with key: key"); - pgagroal_query_cache_invalidate(&(cache->table), key); - pgagroal_log_info("Get cache entry with key: key"); - resp = pgagroal_query_cache_get(cache, &(cache->table), key); - if (resp) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } - pgagroal_log_info("Add cache entry with key: key and data: data"); - pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); - pgagroal_log_info("Add cache entry with key: newKey and data: new-data"); - pgagroal_query_cache_add(cache, &(cache->table), ndata, nKey, 0); - pgagroal_log_info("Get cache entry with key: newKey"); - resp = pgagroal_query_cache_get(cache, &(cache->table), nKey); - if (resp) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } - pgagroal_log_info("Clear cache"); - pgagroal_query_cache_clear(&(cache->table)); - pgagroal_log_info("Get cache entry with key: key"); - - resp = pgagroal_query_cache_get(cache, &(cache->table), nKey); - if (resp) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } - pgagroal_log_info("Get cache entry with key: newKey"); - resp = pgagroal_query_cache_get(cache, &(cache->table), key); - if (resp) - { - pgagroal_log_info("resp: %s", resp->data->value); - } - else - { - pgagroal_log_info("Key not found in cache"); - } + // struct query_cache* cache; + + // cache = (struct query_cache*)query_cache_shmem; + + // struct hashEntry* key, * data; + // key = (struct hashEntry*)malloc(sizeof *key); + // memset(key, 0, sizeof *key); + // data = (struct hashEntry*)malloc(sizeof *data); + // memset(key, 0, sizeof *data); + // key->value = "key"; + // key->length = strlen(key->value); + // data->value = "data"; + // data->length = strlen(data->value); + // pgagroal_log_info("Add cache entry with key: key and data: data"); + // int x = pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); + // if (x == 0) + // { + // pgagroal_log_info("Key already exists"); + // } + // else + // { + // pgagroal_log_info("Key added"); + // } + // pgagroal_log_info("Add cache entry with key: key and data: data"); + // x = pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); + // if (x == 0) + // { + // pgagroal_log_info("Key already exists"); + // } + // else + // { + // pgagroal_log_info("Key added"); + // } + // pgagroal_log_info("Get cache entry with key: key"); + // struct hashTable* resp = pgagroal_query_cache_get(cache, &(cache->table), key); + // if (resp != NULL) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } + // pgagroal_log_info("Update cache entry with key: key and data: new data"); + // struct hashEntry* ndata, * nKey; + // ndata = (struct hashEntry*)malloc(sizeof *ndata); + // ndata->value = "new data"; + // ndata->length = strlen(ndata->value); + // nKey = (struct hashEntry*)malloc(sizeof *nKey); + // nKey->value = "newKey"; + // nKey->length = strlen(nKey->value); + // pgagroal_query_cache_update(&(cache->table), key, ndata); + // resp = pgagroal_query_cache_get(cache, &(cache->table), key); + // if (resp) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } + // pgagroal_log_info("Invalidate cache entry with key: key"); + // pgagroal_query_cache_invalidate(&(cache->table), key); + // pgagroal_log_info("Get cache entry with key: key"); + // resp = pgagroal_query_cache_get(cache, &(cache->table), key); + // if (resp) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } + // pgagroal_log_info("Add cache entry with key: key and data: data"); + // pgagroal_query_cache_add(cache, &(cache->table), data, key, 0); + // pgagroal_log_info("Add cache entry with key: newKey and data: new-data"); + // pgagroal_query_cache_add(cache, &(cache->table), ndata, nKey, 0); + // pgagroal_log_info("Get cache entry with key: newKey"); + // resp = pgagroal_query_cache_get(cache, &(cache->table), nKey); + // if (resp) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } + // pgagroal_log_info("Clear cache"); + // pgagroal_query_cache_clear(&(cache->table)); + // pgagroal_log_info("Get cache entry with key: key"); + + // resp = pgagroal_query_cache_get(cache, &(cache->table), nKey); + // if (resp) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } + // pgagroal_log_info("Get cache entry with key: newKey"); + // resp = pgagroal_query_cache_get(cache, &(cache->table), key); + // if (resp) + // { + // pgagroal_log_info("resp: %s", resp->data->value); + // } + // else + // { + // pgagroal_log_info("Key not found in cache"); + // } } diff --git a/src/libpgagroal/shmem.c b/src/libpgagroal/shmem.c index ad37ceb5..4aa9c021 100644 --- a/src/libpgagroal/shmem.c +++ b/src/libpgagroal/shmem.c @@ -40,6 +40,8 @@ void* pipeline_shmem = NULL; void* prometheus_shmem = NULL; void* prometheus_cache_shmem = NULL; void* query_cache_shmem = NULL; +void* client_server_shmem = NULL; + int pgagroal_create_shared_memory(size_t size, unsigned char hp, void** shmem)