Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimise sort #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 70 additions & 7 deletions hash_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,18 @@ int hash_ring_add_items(hash_ring_t *ring, hash_ring_node_t *node) {
uint64_t keyInt;

// Resize the items array
void *resized = realloc(ring->items, (sizeof(hash_ring_item_t*) * ring->numNodes * ring->numReplicas));
if(resized == NULL) {
//void *resized = realloc(ring->items, (sizeof(hash_ring_item_t*) * ring->numNodes * ring->numReplicas));
//if(resized == NULL) {
// return HASH_RING_ERR;
//}
//ring->items = (hash_ring_item_t**)resized;

//optimise: sort new items,then merge two part
hash_ring_item_t **adds = (hash_ring_item_t **)malloc((sizeof(hash_ring_item_t*) * ring->numReplicas));
if(adds == NULL) {
return HASH_RING_ERR;
}
ring->items = (hash_ring_item_t**)resized;

for(x = 0; x < ring->numReplicas; x++) {
if(ring->mode == HASH_RING_MODE_LIBMEMCACHED_COMPAT) {
concat_len = snprintf(concat_buf, sizeof(concat_buf), "-%d", x);
Expand All @@ -197,9 +204,49 @@ int hash_ring_add_items(hash_ring_t *ring, hash_ring_node_t *node) {
item->node = node;
item->number = keyInt;

ring->items[(ring->numNodes - 1) * ring->numReplicas + x] = item;
adds[x] = item;
}

quicksort((void**)adds, ring->numReplicas, item_sort);
//ring->numNodes * ring->numReplicas
if(ring->items == NULL || ring-> numNodes == 1) {
ring->items = adds;
} else {
size_t size_new = sizeof(hash_ring_item_t*) * ring->numNodes * ring->numReplicas;
hash_ring_item_t **news = (hash_ring_item_t **)malloc(size_new);
hash_ring_item_t **olds = ring->items;
if(news == NULL) {
return HASH_RING_ERR;
}
int oldlen = (ring->numNodes - 1) * ring->numReplicas;
int addlen = ring->numReplicas;
int i=0, j=0, k=0;
while(1) {
if(i == oldlen && j == addlen) {
break;
}
if(j == addlen) {
news[k++] = olds[i++];
continue;
}
if(i == oldlen) {
news[k++] = adds[j++];
continue;
}
int ret = item_sort(olds[i], adds[j]);
if(ret == 0) {
news[k++] = olds[i++];
news[k++] = adds[j++];
} else if (ret < 0) {
news[k++] = olds[i++];
} else {
news[k++] = adds[j++];
}
};
free(adds);
free(olds);
ring->items = news;
}
ring->numItems += ring->numReplicas;
return HASH_RING_OK;
}
Expand Down Expand Up @@ -259,7 +306,7 @@ int hash_ring_add_node(hash_ring_t *ring, uint8_t *name, uint32_t nameLen) {
}

// Sort the items
quicksort((void**)ring->items, ring->numItems, item_sort);
// quicksort((void**)ring->items, ring->numItems, item_sort);

return HASH_RING_OK;
}
Expand Down Expand Up @@ -294,10 +341,26 @@ int hash_ring_remove_node(hash_ring_t *ring, uint8_t *name, uint32_t nameLen) {
ring->items[x] = NULL;
}
}

// By re-sorting, all the NULLs will be at the end of the array
// Then the numItems is reset and that memory is no longer used
quicksort((void**)ring->items, ring->numItems, item_sort);
// quicksort((void**)ring->items, ring->numItems, item_sort);

// optimise: just remove NULL items, needn't sort
int start = -1;
for(x = 0; x < ring->numItems; x++) {
if(start == -1) {
if(ring->items[x] == NULL) {
start = x;
}
}
else {
if(ring->items[x] != NULL) {
ring->items[start++] = ring->items[x];
ring->items[x] = NULL;
}
}
}
ring->numItems -= ring->numReplicas;

free(node);
Expand Down
11 changes: 8 additions & 3 deletions src/hash_ring.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
remove_node/2,
find_node/2,
set_mode/2,
stop/0
stop/0,
set_priority_high/0
]).

-define(SERVER, ?MODULE).
Expand Down Expand Up @@ -92,6 +93,8 @@ find_node(Ring, Key) when is_binary(Key) ->
set_mode(Ring, Mode) when is_integer(Mode) ->
gen_server:call(?SERVER, {set_mode, {Ring, Mode}}).

set_priority_high() ->
gen_server:call(?SERVER, set_priority_high).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Internal functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -102,14 +105,16 @@ set_mode(Ring, Mode) when is_integer(Mode) ->
}).

init(Drv) ->
process_flag(priority, high),
Port = open_port({spawn, Drv}, [binary]),
{ok, #state{
port = Port,
rings = dict:new(),
queue = queue:new() }}.



handle_call(set_priority_high, _From, State) ->
process_flag(priority, high),
{reply, ok, State};
handle_call({create_ring, {Ring, NumReplicas, HashFunction}}, _From, #state{ port = Port, rings = Rings } = State) ->
Port ! {self(), {command, <<1:8, NumReplicas:32, HashFunction:8>>}},
receive
Expand Down