attempt to fix #12, #49 and #65
[awesomized/libmemcached] / clients / ms_task.c
index 5fbd7d1302c9ffb0d1e7b41c838596e89bcf3091..f2cb8657d678bc2088aea873155fc6962cdd404a 100644 (file)
@@ -9,18 +9,14 @@
  *
  */
 
-#include "config.h"
+#include "mem_config.h"
 
-#include <inttypes.h>
-#if TIME_WITH_SYS_TIME
+#if defined(HAVE_SYS_TIME_H)
 # include <sys/time.h>
+#endif
+
+#if defined(HAVE_TIME_H)
 # include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
 #endif
 
 #include "ms_thread.h"
@@ -40,7 +36,7 @@
 static ms_task_item_t *ms_get_cur_opt_item(ms_conn_t *c);
 static ms_task_item_t *ms_get_next_get_item(ms_conn_t *c);
 static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c);
-static ms_task_item_t *ms_get_pre_set_item(ms_conn_t *c);
+static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c);
 
 
 /* select next operation to do */
@@ -54,7 +50,7 @@ static void ms_kick_out_item(ms_task_item_t *item);
 
 
 /* miss rate adjustment */
-static bool ms_need_overwirte_item(ms_task_t *task);
+static bool ms_need_overwrite_item(ms_task_t *task);
 static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task);
 
 
@@ -154,18 +150,10 @@ static ms_task_item_t *ms_get_next_set_item(ms_conn_t *c)
  * @return ms_task_item_t*, the pointer of the previous item of
  *         set operation
  */
-static ms_task_item_t *ms_get_pre_set_item(ms_conn_t *c)
+static ms_task_item_t *ms_get_random_overwrite_item(ms_conn_t *c)
 {
-  if (c->set_cursor <= 0)
-  {
-    return &c->item_win[0];
-  }
-  else
-  {
-    return &c->item_win[(int64_t)-- c->set_cursor % c->win_size];
-  }
-} /* ms_get_pre_set_item */
-
+    return ms_get_next_get_item(c);
+} /* ms_get_random_overwrite_item */
 
 /**
  * According to the proportion of operations(get or set), select
@@ -301,7 +289,7 @@ static void ms_kick_out_item(ms_task_item_t *item)
  * @return bool, if need overwrite, return true, else return
  *         false
  */
-static bool ms_need_overwirte_item(ms_task_t *task)
+static bool ms_need_overwrite_item(ms_task_t *task)
 {
   ms_task_item_t *item= task->item;
 
@@ -355,7 +343,7 @@ static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task)
     /* If the current item is not a new item, kick it out */
     if (item->value_offset != INVALID_OFFSET)
     {
-      if (ms_need_overwirte_item(task))
+      if (ms_need_overwrite_item(task))
       {
         /* overwrite */
         task->overwrite_set++;
@@ -369,17 +357,23 @@ static bool ms_adjust_opt(ms_conn_t *c, ms_task_t *task)
     else            /* it's a new item */
     {
       /* need overwrite */
-      if (ms_need_overwirte_item(task))
+      if (ms_need_overwrite_item(task))
       {
-        item= ms_get_pre_set_item(c);
+        /**
+         *  overwrite not use the item with current set cursor, revert
+         *  set cursor.
+         */
+        c->set_cursor--;
+
+        item= ms_get_random_overwrite_item(c);
         if (item->value_offset != INVALID_OFFSET)
         {
           task->item= item;
           task->overwrite_set++;
         }
-        else                /* previous set item is a new item */
+        else                /* item is a new item */
         {
-          /* select the previous item to run, and cancel overwrite */
+          /* select the item to run, and cancel overwrite */
           task->item= item;
         }
       }
@@ -601,7 +595,7 @@ static void ms_warmup_server(ms_conn_t *c)
    */
   if (c->remain_warmup_num == -1)
   {
-    ms_send_signal(&ms_global.init_lock);
+    ms_send_signal(&ms_global.warmup_lock);
     c->remain_warmup_num--;       /* never run the if branch */
   }
 } /* ms_warmup_server */
@@ -629,7 +623,7 @@ static void ms_single_getset_task_sch(ms_conn_t *c)
     else if (task->cmd == CMD_GET)
     {
       assert(task->cmd == CMD_GET);
-      ms_mcd_get(c, item, task->verify);
+      ms_mcd_get(c, item);
     }
   }
 } /* ms_single_getset_task_sch */
@@ -1055,7 +1049,7 @@ static void ms_update_task_result(ms_conn_t *c)
  *
  * @param c, pointer of the concurrency
  *
- * @return int, if success, return 0, else return -1
+ * @return int, if success, return EXIT_SUCCESS, else return -1
  */
 static int ms_run_getset_task(ms_conn_t *c)
 {
@@ -1087,7 +1081,7 @@ static int ms_run_getset_task(ms_conn_t *c)
     return -1;
   }
 
-  return 0;
+  return EXIT_SUCCESS;
 } /* ms_run_getset_task */
 
 
@@ -1096,7 +1090,7 @@ static int ms_run_getset_task(ms_conn_t *c)
  *
  * @param c, pointer of the concurrency
  *
- * @return int, if success, return 0, else return -1
+ * @return int, if success, return EXIT_SUCCESS, else return -1
  */
 int ms_exec_task(struct conn *c)
 {
@@ -1112,5 +1106,5 @@ int ms_exec_task(struct conn *c)
     }
   }
 
-  return 0;
+  return EXIT_SUCCESS;
 } /* ms_exec_task */