Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Willemt Raft
Commits
79033d25
Commit
79033d25
authored
Oct 24, 2013
by
willem
Browse files
Raft log ADT
parent
c8ab5e03
Changes
4
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
79033d25
...
@@ -37,7 +37,7 @@ main_test.c:
...
@@ -37,7 +37,7 @@ main_test.c:
fi
fi
sh make-tests.sh
"test_*.c"
>
main_test.c
sh make-tests.sh
"test_*.c"
>
main_test.c
tests_main
:
main_test.c raft_server.c raft_candidate.c raft_follower.c raft_leader.c raft_peer.c test_server.c test_server_request_vote.c test_peer.c mock_send_functions.c CuTest.c $(HASHMAP_DIR)/linked_list_hashmap.c $(LLQUEUE_DIR)/linked_list_queue.c $(AQUEUE_DIR)/arrayqueue.c
tests_main
:
main_test.c raft_server.c
raft_log.c
raft_candidate.c raft_follower.c raft_leader.c raft_peer.c test_server.c test_server_request_vote.c test_peer.c mock_send_functions.c CuTest.c $(HASHMAP_DIR)/linked_list_hashmap.c $(LLQUEUE_DIR)/linked_list_queue.c $(AQUEUE_DIR)/arrayqueue.c
$(CC)
$(CCFLAGS)
-o
$@
$^
$(CC)
$(CCFLAGS)
-o
$@
$^
./tests_main
./tests_main
...
...
raft_log.c
View file @
79033d25
...
@@ -18,4 +18,46 @@
...
@@ -18,4 +18,46 @@
#include "linked_list_hashmap.h"
#include "linked_list_hashmap.h"
#include "arrayqueue.h"
#include "arrayqueue.h"
#include "raft.h"
#include "raft.h"
#include "raft_log.h"
typedef
struct
{
arrayqueue_t
*
log
;
}
raft_log_private_t
;
static
unsigned
long
__entry_hash
(
const
void
*
obj
)
{
return
(
unsigned
long
)
obj
;
}
static
long
__entry_compare
(
const
void
*
obj
,
const
void
*
other
)
{
return
obj
-
other
;
}
raft_log_t
*
raft_log_new
()
{
raft_log_private_t
*
me
;
me
=
calloc
(
1
,
sizeof
(
raft_log_private_t
));
me
->
log
=
arrayqueue_new
();
// me->log_map = hashmap_new(__entry_hash, __entry_compare, 100);
return
(
void
*
)
me
;
}
void
raft_log_append_entry
(
raft_log_t
*
me_
,
raft_command_t
*
c
)
{
raft_log_private_t
*
me
=
(
void
*
)
me_
;
arrayqueue_offer
(
me
->
log
,
c
);
}
int
raft_log_count
(
raft_log_t
*
me_
)
{
raft_log_private_t
*
me
=
(
void
*
)
me_
;
return
arrayqueue_count
(
me
->
log
);
}
raft_log.h
View file @
79033d25
typedef
void
*
raft_log_t
;
raft_log_t
*
raft_log_new
();
void
raft_log_append_entry
(
raft_log_t
*
me_
,
raft_command_t
*
c
);
int
raft_log_count
(
raft_log_t
*
me_
);
raft_server.c
View file @
79033d25
...
@@ -32,7 +32,7 @@ typedef struct {
...
@@ -32,7 +32,7 @@ typedef struct {
/* the log which is replicated */
/* the log which is replicated */
//void* log;
//void* log;
arrayqueue
_t
*
log
;
raft_log
_t
*
log
;
/* Volatile state: */
/* Volatile state: */
...
@@ -96,12 +96,10 @@ raft_server_t* raft_new()
...
@@ -96,12 +96,10 @@ raft_server_t* raft_new()
me
->
voted_for
=
-
1
;
me
->
voted_for
=
-
1
;
me
->
current_index
=
0
;
me
->
current_index
=
0
;
me
->
timeout_elapased
=
0
;
me
->
timeout_elapased
=
0
;
me
->
log
=
arrayqueue
_new
();
me
->
log
=
raft_log
_new
();
raft_set_state
((
void
*
)
me
,
RAFT_STATE_FOLLOWER
);
raft_set_state
((
void
*
)
me
,
RAFT_STATE_FOLLOWER
);
raft_set_request_timeout
((
void
*
)
me
,
500
);
raft_set_request_timeout
((
void
*
)
me
,
500
);
raft_set_election_timeout
((
void
*
)
me
,
1000
);
raft_set_election_timeout
((
void
*
)
me
,
1000
);
// me->log_map = hashmap_new(__log_hash, __log_compare, 100);
// me->peers = hashmap_new(__peer_hash, __peer_compare, 100);
return
(
void
*
)
me
;
return
(
void
*
)
me
;
}
}
...
@@ -431,7 +429,7 @@ int raft_recv_command(raft_server_t* me_, int peer, msg_command_t* cmd)
...
@@ -431,7 +429,7 @@ int raft_recv_command(raft_server_t* me_, int peer, msg_command_t* cmd)
int
raft_get_log_count
(
raft_server_t
*
me_
)
int
raft_get_log_count
(
raft_server_t
*
me_
)
{
{
raft_server_private_t
*
me
=
(
void
*
)
me_
;
raft_server_private_t
*
me
=
(
void
*
)
me_
;
return
arrayqueue
_count
(
me
->
log
);
return
raft_log
_count
(
me
->
log
);
}
}
int
raft_get_voted_for
(
raft_server_t
*
me_
)
int
raft_get_voted_for
(
raft_server_t
*
me_
)
...
@@ -505,7 +503,7 @@ int raft_append_entry(raft_server_t* me_, raft_command_t* c)
...
@@ -505,7 +503,7 @@ int raft_append_entry(raft_server_t* me_, raft_command_t* c)
{
{
raft_server_private_t
*
me
=
(
void
*
)
me_
;
raft_server_private_t
*
me
=
(
void
*
)
me_
;
arrayqueue_offer
(
me
->
log
,
c
);
raft_log_append_entry
(
me
->
log
,
c
);
me
->
current_index
+=
1
;
me
->
current_index
+=
1
;
return
1
;
return
1
;
}
}
...
...
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