Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-store
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Samarth Joshi
key-value-store
Commits
d39129da
Commit
d39129da
authored
Nov 10, 2020
by
Samarth Joshi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client Thruput calculation
parent
4fbaf0a8
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
196 additions
and
83 deletions
+196
-83
Client
Client
+0
-0
File0.txt
File0.txt
+0
-0
KVClient.c
KVClient.c
+90
-22
KVServer.c
KVServer.c
+13
-6
Makefile
Makefile
+3
-3
Server
Server
+0
-0
StorageHandler.c
StorageHandler.c
+85
-49
StorageHandler.h
StorageHandler.h
+5
-3
temp
temp
+0
-0
No files found.
Client
View file @
d39129da
No preview for this file type
File0.txt
View file @
d39129da
No preview for this file type
KVClient.c
View file @
d39129da
...
...
@@ -2,11 +2,13 @@
#define PORT 8000
#define SA struct sockaddr
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include "KVClientLibrary.h"
#include <string.h>
...
...
@@ -63,12 +65,23 @@ void interactive (int sock) {
}
while
(
command
[
0
]
!=
'q'
);
}
int
main
(
int
argc
,
char
const
*
argv
[])
{
int
sock
=
0
,
valread
;
void
gen_random
(
char
*
s
,
const
int
len
)
{
static
const
char
alphanum
[]
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
;
for
(
int
i
=
0
;
i
<
len
;
++
i
)
{
s
[
i
]
=
alphanum
[
rand
()
%
(
sizeof
(
alphanum
)
-
1
)];
}
s
[
len
]
=
0
;
}
int
client_init
()
{
int
valread
;
struct
sockaddr_in
serv_addr
;
char
*
hello
=
"Hello from client"
;
char
buffer
[
1024
]
=
{
0
};
char
buffer
[
1024
]
=
{
0
};
int
sock
=
0
;
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
<
0
)
{
printf
(
"
\n
Socket creation error
\n
"
);
...
...
@@ -90,22 +103,77 @@ int main(int argc, char const *argv[])
printf
(
"
\n
Connection Failed
\n
"
);
return
-
1
;
}
char
key1
[
256
]
=
"abc"
;
char
value1
[
256
]
=
"cde"
;
char
error1
[
256
];
char
key2
[
256
]
=
"test"
;
char
value2
[
256
]
=
"cdde"
;
char
error2
[
256
];
char
value3
[
256
];
//printf("%d",(int)put(sock,key, value,error));
//printf("%d",(int)del(sock,key,error));
//printf("%d",(int)get(sock,key, value,error));
//while (1) {
//printf("%d",(int)put(sock, key1, value1, error1));
//printf("%d",(int)put(sock, key2, value2, error2));
//printf("%d\n",(int)get(sock, key1, value3, error2));
//printf("%s", value3);
interactive
(
sock
);
return
0
;
return
sock
;
}
void
*
seige
(
void
*
args
)
{
int
*
rcompleted
=
(
int
*
)
args
;
*
rcompleted
=
0
;
int
status_code
;
char
key
[
256
];
char
value
[
256
];
char
error
[
256
];
int
sock
;
int
r
;
sock
=
client_init
();
while
(
1
)
{
gen_random
(
key
,
10
);
gen_random
(
value
,
10
);
r
=
rand
()
%
3
;
switch
(
r
)
{
case
0
:
put
(
sock
,
key
,
value
,
error
);
break
;
case
1
:
get
(
sock
,
key
,
value
,
error
);
break
;
case
2
:
get
(
sock
,
key
,
value
,
error
);
break
;
}
*
rcompleted
=
*
rcompleted
+
1
;
}
}
int
main
(
int
argc
,
char
const
*
argv
[])
{
int
sock
;
int
max_concurrent
=
100
;
int
n
=
5
;
pthread_t
*
seige_threads
;
int
*
requestcompleted
;
int
*
requestsent
;
int
sum
;
int
seconds
=
1
;
int
i
;
if
(
1
)
{
sock
=
client_init
();
interactive
(
sock
);
return
0
;
}
else
{
seige_threads
=
malloc
(
max_concurrent
*
sizeof
(
pthread_t
));
requestcompleted
=
(
int
*
)
malloc
(
max_concurrent
*
sizeof
(
int
));
for
(
i
=
0
;
i
<
n
;
i
++
)
{
pthread_create
(
&
seige_threads
[
i
],
NULL
,
seige
,
requestcompleted
+
i
);
}
while
(
1
)
{
sleep
(
1
);
sum
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
sum
=
sum
+
requestcompleted
[
i
];
}
if
(
seconds
%
5
==
0
)
{
printf
(
"%d, %f
\n
"
,
i
,
((
float
)
sum
/
seconds
));
//pthread_create( &seige_threads[i], NULL, seige, requestcompleted+i );
//i++;
}
if
(
i
==
max_concurrent
)
{
break
;
}
seconds
++
;
}
}
}
\ No newline at end of file
KVServer.c
View file @
d39129da
...
...
@@ -102,17 +102,24 @@ void *worker(void *args) {
if
(
status
)
{
requestMessage
->
status
=
200
;
}
else
{
requestMessage
->
status
=
240
;
status
=
file_get
(
requestMessage
->
key
,
requestMessage
->
value
);
if
(
status
)
{
requestMessage
->
status
=
200
;
}
else
{
requestMessage
->
status
=
240
;
}
}
break
;
case
STATUS_PUT
:
if
DEBUG
printf
(
"[%s] PUT
\n
"
,
name
);
cache_put
(
requestMessage
->
key
,
requestMessage
->
value
);
file_put
(
requestMessage
->
key
,
requestMessage
->
value
);
requestMessage
->
status
=
200
;
break
;
case
STATUS_DEL
:
if
DEBUG
printf
(
"[%s] DEL
\n
"
,
name
);
status
=
cache_del
(
requestMessage
->
key
);
file_del
(
requestMessage
->
key
);
if
(
status
==
0
)
{
requestMessage
->
status
=
240
;
}
else
{
...
...
@@ -139,14 +146,14 @@ void *worker(void *args) {
int
main
(
int
argc
,
int
argv
)
{
int
i
;
int
next_thread_to_assign
=
0
;
pthread_t
*
worker_threads
;
int
pool_thread_size
=
1
;
// TODO: get pool thread size from config file
int
next_thread_to_assign
=
0
;
pthread_t
*
worker_threads
;
int
pool_thread_size
=
1
0
;
// TODO: get pool thread size from config file
int
sockfd
,
newsockfd
,
portno
,
clilen
,
n
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int
pipes
[
1
][
2
];
// TODO: initialize pipes dynamically
int
pipes
[
1
0
][
2
];
// TODO: initialize pipes dynamically
worker_threads
=
malloc
(
pool_thread_size
*
sizeof
(
pthread_t
));
for
(
i
=
0
;
i
<
pool_thread_size
;
i
++
)
{
...
...
@@ -175,7 +182,7 @@ int main (int argc, int argv) {
clilen
=
sizeof
(
cli_addr
);
init_cache
();
init_storage
()
storage_init
();
while
(
1
)
{
...
...
Makefile
View file @
d39129da
CC
=
gcc
SFLAGS
=
-pthread
-g
SRV_SRC
=
LRU.c KVServer.c
SRV_HED
=
KVMessageFormat.h LRU.h
SRV_SRC
=
LRU.c
StorageHandler.c
KVServer.c
SRV_HED
=
KVMessageFormat.h LRU.h
StorageHandler.h
STARGET
=
Server
CFLAGS
=
-g
CFLAGS
=
-
pthread
-
g
CLI_SRC
=
KVClientLibrary.c KVClient.c
CLI_HED
=
KVMessageFormat.h KVClientLibrary.h
CTARGET
=
Client
...
...
Server
View file @
d39129da
No preview for this file type
StorageHandler.c
View file @
d39129da
...
...
@@ -21,66 +21,70 @@ pthread_t tid;
pthread_t
writerthreads
[
100
],
readerthreads
[
100
];
int
readercount
=
0
;
int
modulus
(
char
*
num
,
int
size
,
int
divisor
)
{
int
rem
=
0
;
unsigned
modulus
(
unsigned
char
*
num
,
size_t
size
,
unsigned
divisor
)
{
unsigned
rem
=
0
;
while
(
size
--
>
0
)
{
rem
=
((
UCHAR_MAX
+
1
)
*
rem
+
*
num
)
%
divisor
;
rem
=
((
UCHAR_MAX
+
1
ULL
)
*
rem
+
*
num
)
%
divisor
;
num
++
;
}
return
rem
;
}
void
file_del
(
off_t
offset
,
char
*
key
)
void
file_del
(
char
*
key
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
fflush
(
stdout
);
int
length
=
0
;
sem_wait
(
&
mutex
[
index
]);
char
blankspace
[
512
];
char
ch
;
int
k
=-
1
;
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
length
++
;
memset
(
blankspace
,
0
,
512
);
char
*
line
;
char
temp
[
514
];
size_t
len
=
0
;
off_t
position
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
memcpy
(
temp
,
line
,
strlen
(
line
)
-
1
);
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
lseek
(
fds
[
index
],
position
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
strlen
(
temp
));
puts
(
temp
);
break
;
}
position
=
ftell
(
fp
);
}
printf
(
"length %d"
,
length
);
memset
(
blankspace
,
0
,
length
);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
length
);
sem_post
(
&
mutex
[
index
]);
}
void
file_get
(
off_t
offset
,
char
*
key
,
char
*
value
)
int
file_get
(
char
*
key
,
char
*
value
)
{
/* Gets the value stored at offset */
/* Does not depend on key argument */
int
found
=
0
;
int
index
=
modulus
(
key
,
256
,
setSize
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+=
1
;
if
(
readCounters
[
index
]
==
1
)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
char
line
[
10
];
//FILE *fp =fdopen(fds[index],"r+");
// fseek(fp, offset,SEEK_SET);
char
*
line
;
size_t
len
=
0
;
char
ch
;
int
k
=-
1
;
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
if
(
k
>=
0
)
{
value
[
k
++
]
=
ch
;
}
if
(
ch
==
':'
)
{
k
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
memcpy
(
value
,
fvalue
,
strlen
(
fvalue
)
-
1
);
found
=
1
;
break
;
}
}
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
-=
1
;
...
...
@@ -89,27 +93,60 @@ void file_get(off_t offset, char *key, char *value)
sem_post
(
&
mutex
[
index
]);
}
sem_post
(
&
readerLocks
[
index
]);
return
found
;
}
off_t
file_put
(
char
*
key
,
char
*
value
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
int
bytes
,
rembytes
,
i
;
off_t
file_put
(
char
*
key
,
char
*
value
)
{
/*
if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned
int
index
=
modulus
(
key
,
256
,
setSize
);
off_t
position
;
sem_wait
(
&
mutex
[
index
]);
//sem_wait(&mutex[index]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
position
=
lseek
(
fds
[
index
],
0
,
SEEK_END
);
char
line
[
514
];
snprintf
(
line
,
sizeof
(
line
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
line
,
strlen
(
line
))
<
0
)
char
*
line
;
char
lin
[
514
];
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
position
=
ftell
(
fp
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
if
(
strcmp
(
value
,
strtok
(
fvalue
,
"
\n
"
))
==
0
)
{
printf
(
"(%s)(%s)
\n
"
,
fkey
,
key
);
///everything same then why insert?
return
0
;
}
else
{
printf
(
"key:(%s)(%s)
\n
"
,
fkey
,
key
);
printf
(
"value:(%s)(%s)
\n
"
,
fvalue
,
value
);
fflush
(
stdout
);
fseek
(
fp
,
position
,
SEEK_SET
);
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
fputs
(
lin
,
fp
)
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
return
0
;
}
}
position
=
ftell
(
fp
);
}
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
lin
,
strlen
(
lin
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
sem_post
(
&
mutex
[
index
]);
// sem_post(&mutex[index]);
return
position
;
}
int
init_storage
()
{
int
storage_init
()
{
/*
define the array of file descriptors depending on the prefix
define the array of readCount as well as the semaphore (read x and write y) for the same
...
...
@@ -136,5 +173,4 @@ int init_storage() {
readCounters
[
i
]
=
0
;
}
return
0
;
}
\ No newline at end of file
}
StorageHandler.h
View file @
d39129da
#include <sys/types.h>
void
*
file_del
(
off_t
offset
,
char
*
key
);
int
storage_init
(
);
void
file_
get
(
off_t
offset
,
char
*
key
,
char
*
value
);
void
file_
del
(
char
*
key
);
off_t
file_put
(
char
*
key
,
char
*
value
);
int
init_storage
();
int
file_get
(
char
*
key
,
char
*
value
);
void
file_del
(
char
*
key
);
temp
View file @
d39129da
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment