Skip to content

Commit

Permalink
FEATURE: add sop forced insert
Browse files Browse the repository at this point in the history
  • Loading branch information
computerphilosopher committed Jul 18, 2019
1 parent cb9cd9a commit af82de4
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 12 deletions.
5 changes: 3 additions & 2 deletions engines/default/default_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,16 @@ default_set_elem_release(ENGINE_HANDLE* handle, const void *cookie,
static ENGINE_ERROR_CODE
default_set_elem_insert(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey, eitem *eitem,
item_attr *attrp, bool *created, uint16_t vbucket)
item_attr *attrp, bool *created, uint16_t vbucket,
bool force)
{
struct default_engine *engine = get_handle(handle);
ENGINE_ERROR_CODE ret;
VBUCKET_GUARD(engine, vbucket);

ACTION_BEFORE_WRITE(cookie, key, nkey);
ret = set_elem_insert(engine, key, nkey, (set_elem_item*)eitem,
attrp, created, cookie);
attrp, created, cookie, force);
ACTION_AFTER_WRITE(cookie, ret);
return ret;
}
Expand Down
12 changes: 8 additions & 4 deletions engines/default/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ static ENGINE_ERROR_CODE do_set_elem_get(struct default_engine *engine,

static ENGINE_ERROR_CODE do_set_elem_insert(struct default_engine *engine,
hash_item *it, set_elem_item *elem,
const void *cookie)
const void *cookie, bool force)
{
set_meta_info *info = (set_meta_info *)item_get_meta(it);
int32_t real_mcnt = (info->mcnt == -1 ? max_set_size : info->mcnt);
Expand All @@ -2251,7 +2251,10 @@ static ENGINE_ERROR_CODE do_set_elem_insert(struct default_engine *engine,
/* overflow check */
assert(info->ovflact == OVFL_ERROR);
if (info->ccnt >= real_mcnt) {
return ENGINE_EOVERFLOW;
if (!force || info->ccnt >= max_set_size) {
return ENGINE_EOVERFLOW;
}
info->mcnt = info->ccnt+1;
}

/* create the root hash node if it does not exist */
Expand Down Expand Up @@ -6675,7 +6678,8 @@ void set_elem_release(struct default_engine *engine, set_elem_item **elem_array,
}

ENGINE_ERROR_CODE set_elem_insert(struct default_engine *engine, const char *key, const size_t nkey,
set_elem_item *elem, item_attr *attrp, bool *created, const void *cookie)
set_elem_item *elem, item_attr *attrp, bool *created, const void *cookie,
bool force)
{
hash_item *it = NULL;
ENGINE_ERROR_CODE ret;
Expand All @@ -6698,7 +6702,7 @@ ENGINE_ERROR_CODE set_elem_insert(struct default_engine *engine, const char *key
}
}
if (ret == ENGINE_SUCCESS) {
ret = do_set_elem_insert(engine, it, elem, cookie);
ret = do_set_elem_insert(engine, it, elem, cookie, force);
if (ret != ENGINE_SUCCESS && *created) {
do_item_unlink(engine, it, ITEM_UNLINK_NORMAL);
}
Expand Down
3 changes: 2 additions & 1 deletion engines/default/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ ENGINE_ERROR_CODE set_elem_insert(struct default_engine *engine,
const char *key, const size_t nkey,
set_elem_item *elem,
item_attr *attrp,
bool *created, const void *cookie);
bool *created, const void *cookie,
bool force);

ENGINE_ERROR_CODE set_elem_delete(struct default_engine *engine,
const char *key, const size_t nkey,
Expand Down
3 changes: 2 additions & 1 deletion engines/demo/demo_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ Demo_set_elem_release(ENGINE_HANDLE* handle, const void *cookie,
static ENGINE_ERROR_CODE
Demo_set_elem_insert(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey, eitem *eitem,
item_attr *attrp, bool *created, uint16_t vbucket)
item_attr *attrp, bool *created, uint16_t vbucket,
bool force)
{
return ENGINE_ENOTSUP;
}
Expand Down
2 changes: 1 addition & 1 deletion include/memcached/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ extern "C" {
ENGINE_ERROR_CODE (*set_elem_insert)(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey, eitem *eitem,
item_attr *attrp, bool *created,
uint16_t vbucket);
uint16_t vbucket, bool force);

ENGINE_ERROR_CODE (*set_elem_delete)(ENGINE_HANDLE* handle, const void* cookie,
const void* key, const int nkey,
Expand Down
8 changes: 5 additions & 3 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ static void process_sop_insert_complete(conn *c) {

ret = mc_engine.v1->set_elem_insert(mc_engine.v0, c,
c->coll_key, c->coll_nkey, elem,
c->coll_attrp, &created, 0);
c->coll_attrp, &created, 0, c->force);
if (ret == ENGINE_EWOULDBLOCK) {
c->ewouldblock = true;
ret = ENGINE_SUCCESS;
Expand Down Expand Up @@ -5072,7 +5072,7 @@ static void process_bin_sop_insert_complete(conn *c) {
ret = mc_engine.v1->set_elem_insert(mc_engine.v0, c,
c->coll_key, c->coll_nkey, elem,
c->coll_attrp, &created,
c->binary_header.request.vbucket);
c->binary_header.request.vbucket, c->force);
if (ret == ENGINE_EWOULDBLOCK) {
c->ewouldblock = true;
ret = ENGINE_SUCCESS;
Expand Down Expand Up @@ -10586,8 +10586,10 @@ static void process_sop_command(conn *c, token_t *tokens, const size_t ntokens)
}
vlen += 2;

c->force = (strcmp(tokens[ntokens - 2 - (c->noreply ? 1 : 0)].value, "force") == 0);

int read_ntokens = SOP_KEY_TOKEN + 2;
int post_ntokens = 1 + (c->noreply ? 1 : 0);
int post_ntokens = 1 + (c->noreply ? 1 : 0) + (c->force ? 1: 0);
int rest_ntokens = ntokens - read_ntokens - post_ntokens;

if (rest_ntokens >= 2) {
Expand Down
1 change: 1 addition & 0 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ struct conn {
char client_ip[16];

bool noreply; /* True if the reply should not be sent. */
bool force;
/* current stats command */

struct {
Expand Down
60 changes: 60 additions & 0 deletions t/coll_sop_forced_insert.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/perl

use strict;
use Test::More tests => 51001;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;

my $engine = shift;
my $server = get_memcached($engine);
my $sock = $server->sock;

my $flags = 13;
my $default_set_size = 4000;
my $maximum_set_size = 50000;
my $cnt;

sub sop_forced_insert_over_maxcount {
my ($key, $create) = @_;
my $index;

for ($index = 0; $index < $maximum_set_size; $index++) {
my $val = "datum$index";
my $vleng = length($val);
my $cmd;
my $rst;

if ($index == 0) {
$cmd = "sop insert $key $vleng $create force";
$rst = "CREATED_STORED";
}

else {
$cmd = "sop insert $key $vleng force";
$rst = "STORED";
}
mem_cmd_is($sock, $cmd, $val, $rst);
}
}


sub sop_forced_insert_over_hardlimit{
my ($key, $from, $to) = @_;
my $index;

for($index = $from; $index <= $to; $index++){
my $val = "datum$index";
my $vleng = length($val);
my $cmd = "sop insert $key $vleng force";
my $rst = "OVERFLOWED";

mem_cmd_is($sock, $cmd, $val, $rst);
}
}

sop_forced_insert_over_maxcount("skey", "create $flags 0 1");
sop_forced_insert_over_hardlimit("skey", $maximum_set_size, $maximum_set_size+1000);

# after test
release_memcached($engine, $server);
1 change: 1 addition & 0 deletions t/tlist/engine_default_s.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
./t/coll_pipeline_sop_exist.t
./t/coll_readable_attr.t
./t/coll_sop_segfault_p012611.t
./t/coll_sop_forced_insert.t
./t/coll_sop_unittest.t
./t/daemonize.t
./t/dash-M.t
Expand Down

0 comments on commit af82de4

Please sign in to comment.