You need to sign in or sign up before continuing.
Commit b3f73a53 authored by Naman Dixit's avatar Naman Dixit

Fixed the bug with 256 byte keys and 256 KiB values

parent a02484f8
......@@ -133,6 +133,7 @@ int main(int argc, char** argv)
Char *xml_request_message = NULL;
// Response message will be of proper legth, since server will make sure of that.
Size response_size = KiB(300);
Char *xml_response_message_read = calloc(response_size, sizeof(*xml_response_message_read));
Char *xml_response_message = calloc(response_size, sizeof(*xml_response_message));
Size index = 0;
......@@ -142,26 +143,24 @@ int main(int argc, char** argv)
if(strcmp(query_type, "GET") == 0) {
xml_request_message = xmlCreateMessage(XML_Message_Kind_GET,
query_key, NULL, NULL);
write(sock_fd, xml_request_message, strlen(xml_request_message));
read(sock_fd, xml_response_message, response_size);
} else if(strcmp(query_type, "SET") == 0) {
} else if(strcmp(query_type, "PUT") == 0) {
Char *query_value = tokenGet(command, '\n', &index);
xml_request_message = xmlCreateMessage(XML_Message_Kind_PUT,
query_key, query_value, NULL);
sbufDelete(query_value);
write(sock_fd, xml_request_message, strlen(xml_request_message));
read(sock_fd, xml_response_message, response_size);
} else if(strcmp(query_type,"DEL") == 0) {
xml_request_message = xmlCreateMessage(XML_Message_Kind_DELETE,
query_key, NULL, NULL);
write(sock_fd, xml_request_message, strlen(xml_request_message));
}
write(sock_fd, xml_request_message, strlen(xml_request_message));
read(sock_fd, xml_response_message, response_size);
U64 read_len = 0;
while (!strsuffix(xml_response_message, "</KVMessage>\n") || (read_len >= KiB(299))) {
read_len += read(sock_fd, xml_response_message_read, response_size);
strcat(xml_response_message, xml_response_message_read);
memset(xml_response_message_read, 0, response_size);
}
sbufDelete(query_type);
......
......@@ -644,7 +644,7 @@ void* hmRemove (Hashmap *hm, void *key)
}
header_function
Size strprefix(Char *pre, Char *str)
Size strprefix(Char *str, Char *pre)
{
Size lenpre = strlen(pre);
Size lenstr = strlen(str);
......@@ -660,6 +660,21 @@ Size strprefix(Char *pre, Char *str)
}
}
header_function
B32 strsuffix (Char *str, Char *suf)
{
Char *string = strrchr(str, suf[0]);
B32 result = false;
if(string != NULL) {
if (strcmp(string, suf) == 0) {
result = true;
}
}
return result;
}
header_function
U64 nextPowerOf2(U64 v)
{
......
......@@ -26,7 +26,7 @@ XML_Message xmlParseMessage (Char *xml) {
XML_Message result = {0};
Char *error_message = "XML Error: Received unparseable message";
#define PARSE_CHAR(c) do { if (stream[0] != c) goto parse_failed; else stream++;} while (0)
#define PARSE_CHAR(c) do { if (stream[0] != c) {goto parse_failed;} else stream++;} while (0)
#define PARSE_SPACE() do { while (isspace(stream[0])) { stream++; }} while (0)
PARSE_CHAR('<');
......
......@@ -5,10 +5,10 @@ void* dump (void *arg)
XML_Dump_Metadata *xml_dump = arg;
fprintf(xml_dump->file,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KVStore>\n\n"
"<!--This XML file is generated in a lazy fashion, when the server is in a \n"
"quiscent state. This means that this file may end up being incomplete if\n"
"the server is killed forcefully before this dump is complete. -->\n\n"
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KVStore>\n");
"the server is killed forcefully before this dump is complete. -->\n\n");
fflush(xml_dump->file);
DIR *dir0 = opendir(".");
......
......@@ -48,6 +48,12 @@ typedef struct Thread_Pool_Metadata {
#define MAX_SOCKET_CONNECTIONS_REQUEST 64
#define MAX_EPOLL_EVENTS 1024
typedef struct Input_Resume {
Char *buffer;
Size buffer_len;
int fd;
} Input_Resume;
typedef struct Output_Resume {
Char *output;
Size output_pos;
......@@ -113,6 +119,7 @@ Sint main (Sint argc, Char *argv[])
}
Hashmap output_map = hmCreate(memCRT, 1024);
Hashmap input_map = hmCreate(memCRT, 1024);
Hashmap busy_map = hmCreate(memCRT, 1024);
// NOTE(naman): Create a socket for IPv4 and TCP.
......@@ -247,71 +254,91 @@ Sint main (Sint argc, Char *argv[])
char *buffer = calloc(KiB(300), sizeof(*buffer));
int len = read(fd, buffer, KiB(300) - 1);
if (len == 0) {
close(fd);
continue;
}
if (hmLookupI(&busy_map, fd)) {
B32 is_busy = hmLookupI(&busy_map, fd);
Input_Resume *ir = (Input_Resume*)hmLookupI(&input_map, fd);
if (is_busy && (ir == NULL)) {
continue;
}
hmInsertI(&busy_map, fd, true);
XML_Message msg = xmlParseMessage(buffer);
if (ir == NULL) {
ir = calloc(1, sizeof(*ir));
ir->buffer = calloc(KiB(300), sizeof(*(ir->buffer)));
ir->fd = fd;
hmInsertI(&input_map, fd, (Uptr)ir);
}
strcat(ir->buffer, buffer);
ir->buffer_len += len;
free(buffer);
switch (msg.kind) {
case XML_Message_Kind_GET: {
Command command = {.fd = fd};
if (strsuffix(ir->buffer, "</KVMessage>\n") && (ir->buffer_len <= KiB(299))) {
XML_Message msg = xmlParseMessage(ir->buffer);
command.kind = Command_Kind_GET;
command.get.key = msg.key;
switch (msg.kind) {
case XML_Message_Kind_GET: {
Command command = {.fd = fd};
commandEnqueue(command);
} break;
command.kind = Command_Kind_GET;
command.get.key = msg.key;
case XML_Message_Kind_PUT: {
Command command = {.fd = fd};
commandEnqueue(command);
} break;
command.kind = Command_Kind_PUT;
command.put.key = msg.key;
command.put.value = msg.value;
case XML_Message_Kind_PUT: {
Command command = {.fd = fd};
commandEnqueue(command);
} break;
command.kind = Command_Kind_PUT;
command.put.key = msg.key;
command.put.value = msg.value;
case XML_Message_Kind_DELETE: {
Command command = {.fd = fd};
commandEnqueue(command);
} break;
command.kind = Command_Kind_DEL;
command.del.key = msg.key;
case XML_Message_Kind_DELETE: {
Command command = {.fd = fd};
commandEnqueue(command);
} break;
command.kind = Command_Kind_DEL;
command.del.key = msg.key;
case XML_Message_Kind_SYSTEM_ERROR: {
Char *output = xmlCreateMessage(XML_Message_Kind_RESP_ERROR,
NULL, NULL, msg.error);
Size output_len = strlen(output);
commandEnqueue(command);
} break;
Output_Resume *or = calloc(1, sizeof(*or));
or->fd = fd;
or->output = output;
or->output_pos = 0;
or->output_len = output_len;
hmInsertI(&output_map, fd, (Uptr)or);
} break;
case XML_Message_Kind_SYSTEM_ERROR: {
Char *output = xmlCreateMessage(XML_Message_Kind_RESP_ERROR,
NULL, NULL, msg.error);
Size output_len = strlen(output);
default: {
} break;
}
Output_Resume *or = calloc(1, sizeof(*or));
or->fd = fd;
or->output = output;
or->output_pos = 0;
or->output_len = output_len;
hmInsertI(&output_map, fd, (Uptr)or);
} break;
struct epoll_event event = {.data.fd = fd,
.events = EPOLLIN | EPOLLOUT};
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0) {
perror("epoll_ctl EPOLL_CTL_MOD");
exit(-1);
default: {
} break;
}
hmRemoveI(&input_map, fd);
free(ir->buffer);
free(ir);
struct epoll_event event = {.data.fd = fd,
.events = EPOLLIN | EPOLLOUT};
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0) {
perror("epoll_ctl EPOLL_CTL_MOD");
exit(-1);
}
}
} else if (events[i].events & EPOLLOUT) {
// Writing into fd in which we previously were not able to finish writing to
......@@ -319,7 +346,7 @@ Sint main (Sint argc, Char *argv[])
Output_Resume *or = hmLookup(&output_map, (void*)(Uptr)fd);
if (or == NULL) {
// fprintf(stderr, "hmLookup returned NULL");
// fprintf(stderr, "hmLookup returned NULL\n");
continue;
} else {
Char *output = or->output;
......@@ -332,7 +359,7 @@ Sint main (Sint argc, Char *argv[])
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Try next time
} else {
perror("write() failed");
perror("write() failed\n");
exit(-1);
}
} else if ((Size)nsent < (output_len - output_pos)) {
......
......@@ -50,7 +50,8 @@ B32 storageSetFileData(U64 hash, Char *key, Char *value, Char **error)
strcat(path, "/");
if (!storageMakeDirectoryIfNotExist(path, error)) goto error;
strcat(path, key);
strncat(path, key, 255);
if((file = fopen(path, "w")) != NULL) {
fprintf(file, "%s", value);
fclose(file);
......@@ -102,7 +103,7 @@ Char* storageGetFileData(U64 hash, Char *key, Char **error)
strcat(path, "/");
strcat(path, byte_seven_eight);
strcat(path, "/");
strcat(path, key);
strncat(path, key, 255);
if(access(path, F_OK) == 0) {
if((file = fopen(path, "r")) != NULL) {
......@@ -201,7 +202,7 @@ B32 storageDeleteFileData(U64 hash, Char *key, Char **error)
strcat(path, "/");
strcat(path, byte_seven_eight);
strcat(path, "/");
strcat(path, key);
strncat(path, key, 255);
// FIXME(namna): This is in a race condition with the SetData
int ret = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment