Skip to content

Commit 040fc1a

Browse files
committed
Fix ramcloud problems
Signed-off-by: Haomai Wang <[email protected]>
1 parent 04b76c3 commit 040fc1a

File tree

3 files changed

+67
-43
lines changed

3 files changed

+67
-43
lines changed

src/app/ramcloud/app_ramcloud.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ static void ramcloudIncr(struct ramcloudData *d, wstr key, uint64_t num, int pos
238238
value[actual_len-FLAG_SIZE] = '\0';
239239
long long n = atoll(value);
240240
if (n == 0) {
241-
sliceTo(&d->storage_response, (uint8_t*)CLIENT_ERROR, sizeof(CLIENT_ERROR)-1);
242-
return ;
241+
if (value[0] != '0') {
242+
sliceTo(&d->storage_response, (uint8_t*)CLIENT_ERROR, sizeof(CLIENT_ERROR)-1);
243+
return ;
244+
}
243245
}
244246
if (positive) {
245247
n += num;
@@ -294,7 +296,7 @@ static void ramcloudAppend(struct ramcloudData *d, wstr key, struct array *vals,
294296
wheatLog(WHEAT_WARNING, " failed to read %s: %s", key, statusToString(s));
295297
sliceTo(&d->storage_response, (uint8_t*)SERVER_ERROR, sizeof(SERVER_ERROR)-1);
296298
} else {
297-
sliceTo(&d->storage_response, (uint8_t*)NOT_FOUND, sizeof(NOT_FOUND)-1);
299+
sliceTo(&d->storage_response, (uint8_t*)NOT_STORED, sizeof(NOT_STORED)-1);
298300
}
299301
wstrFree(origin_val);
300302
return ;
@@ -316,7 +318,7 @@ static void ramcloudAppend(struct ramcloudData *d, wstr key, struct array *vals,
316318
wheatLog(WHEAT_WARNING, " failed to read %s: %s", key, statusToString(s));
317319
sliceTo(&d->storage_response, (uint8_t*)SERVER_ERROR, sizeof(SERVER_ERROR)-1);
318320
} else {
319-
sliceTo(&d->storage_response, (uint8_t*)NOT_FOUND, sizeof(NOT_FOUND)-1);
321+
sliceTo(&d->storage_response, (uint8_t*)NOT_STORED, sizeof(NOT_STORED)-1);
320322
}
321323
wstrFree(origin_val);
322324
return ;
@@ -356,7 +358,7 @@ static void ramcloudAppend(struct ramcloudData *d, wstr key, struct array *vals,
356358
wstrFree(val);
357359
if (s != STATUS_OK) {
358360
if (s == STATUS_OBJECT_DOESNT_EXIST) {
359-
sliceTo(&d->storage_response, (uint8_t*)NOT_FOUND, sizeof(NOT_FOUND)-1);
361+
sliceTo(&d->storage_response, (uint8_t*)NOT_STORED, sizeof(NOT_STORED)-1);
360362
return ;
361363
} else if (s == STATUS_WRONG_VERSION) {
362364
goto again;
@@ -464,6 +466,8 @@ int initRamcloud(struct protocol *p)
464466
wheatLog(WHEAT_WARNING, "%s connect to %s: %s", __func__, locator, statusToString(s));
465467
goto err;
466468
}
469+
rc_createTable(global.client, "1234", 1);
470+
rc_getTableId(global.client, "1234", &table_id);
467471

468472
return WHEAT_OK;
469473

src/protocol/memcache/proto_memcache.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* control characters or whitespace.
1919
*/
2020
#define MEMCACHE_MAX_KEY_LENGTH 250
21-
#define MEMCACHE_FINISHED(r) (((r)->stage) == SW_ALMOST_DONE)
21+
#define MEMCACHE_FINISHED(r) (((r)->stage) == SW_DONE)
2222
#define ERROR "CLIENT_ERROR bad request\r\n"
2323

2424
#define CR (uint8_t)13
@@ -65,6 +65,7 @@ enum reqStage {
6565
SW_NOREPLY,
6666
SW_AFTER_NOREPLY,
6767
SW_ALMOST_DONE,
68+
SW_DONE,
6869
SW_SENTINEL
6970
};
7071

@@ -639,6 +640,7 @@ static ssize_t memcacheParseReq(struct memcacheProcData *r, struct slice *s)
639640
case LF:
640641
/* req_end <- p */
641642
pos++;
643+
state = SW_DONE;
642644
goto done;
643645

644646
default:

tests/test_memcache.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections
22
import random
33
import socket
4+
import time
45

56
from wheatserver_test import WheatServer, PROJECT_PATH, server_socket
67
import memcache
@@ -190,7 +191,7 @@ def test_memcache_basic_response():
190191

191192

192193
class MemcacheSynthetic(object):
193-
def __ini__(self):
194+
def __init__(self):
194195
self.data = collections.defaultdict(str)
195196
self.c = memcache.Client(["127.0.0.1:10828"])
196197
self.num_key = set()
@@ -203,86 +204,99 @@ def get_str(self, n):
203204
return s
204205

205206
def get(self):
206-
k = ramdom.choice(self.data.keys())
207-
assert(self.data[k] == self.c.get(k))
207+
if not self.data:
208+
return
209+
k = random.choice(self.data.keys())
210+
assert(str(self.data[k]) == str(self.c.get(k)))
208211

209212
def set(self):
210213
k = self.get_str(random.randint(10, 100))
211214
if k not in self.data:
212215
if len(k) % 3 == 0:
213216
v = random.randint(0, 1000000)
214-
self.num_key.insert(k)
217+
self.num_key.add(k)
215218
else:
216219
v = self.get_str(random.randint(100, 4096))
217-
break
220+
if k in self.num_key:
221+
self.num_key.remove(k)
222+
223+
assert(self.c.set(k, v))
224+
self.data[k] = str(v)
218225

219-
self.c.set(k, v)
220-
self.data[k] = v
226+
def delete(self):
227+
if not self.data:
228+
return
229+
k = random.choice(self.data.keys())
230+
assert(self.c.delete(k))
231+
if k in self.num_key:
232+
self.num_key.remove(k)
233+
del self.data[k]
221234

222235
def append(self):
223-
if random.randint(0, 1):
224-
k = ramdom.choice(self.data.keys())
225-
else:
226-
k = self.get_str(random.randint(10, 100))
236+
if not self.data:
237+
return
227238

239+
k = random.choice(self.data.keys())
228240
v = self.get_str(random.randint(100, 1024))
229-
self.c.append(k, v)
230-
self.data[k] += v
241+
assert(self.c.append(k, v))
242+
self.data[k] += str(v)
231243
if k in self.num_key:
232244
self.num_key.remove(k)
233245

234246
def prepend(self):
235-
if random.randint(0, 1):
236-
k = ramdom.choice(self.data.keys())
237-
else:
238-
k = self.get_str(random.randint(10, 100))
247+
if not self.data:
248+
return
239249

250+
k = random.choice(self.data.keys())
240251
v = self.get_str(random.randint(100, 1024))
241-
242-
self.c.prepend(k, v)
243-
self.data[k] = v + self.data[k]
252+
assert(self.c.prepend(k, v))
253+
self.data[k] = str(v) + str(self.data[k])
244254
if k in self.num_key:
245255
self.num_key.remove(k)
246256

247257
def incr(self):
248258
if not self.num_key:
249259
return
250-
k = ramdom.choice(self.num_key)
260+
k = random.sample(self.num_key, 1)[0]
251261
v = random.randint(0, 1000000)
252-
self.c.incr(k, n)
253-
self.data[k] = int(self.data[k]) + n
262+
self.data[k] = str(int(self.data[k]) + v)
263+
assert(str(self.c.incr(k, v)) == self.data[k])
254264

255265
def decr(self):
256266
if not self.num_key:
257267
return
258-
k = ramdom.choice(self.num_key)
268+
k = random.sample(self.num_key, 1)[0]
259269
v = random.randint(0, 1000000)
260-
self.c.decr(k, n)
261-
self.data[k] = int(self.data[k]) - n
262-
if self.data[k] < 0:
263-
self.data[k] = 0
270+
self.data[k] = str(int(self.data[k]) - v)
271+
if int(self.data[k]) < 0:
272+
self.data[k] = "0"
273+
assert(str(self.c.decr(k, v)) == self.data[k])
264274

265-
def add(self, k, v):
275+
def add(self):
266276
while True:
267277
k = self.get_str(random.randint(10, 100))
268278
if k not in self.data:
269279
if len(k) % 3 == 0:
270280
v = random.randint(0, 1000000)
271-
self.num_key.insert(k)
281+
self.num_key.add(k)
272282
else:
273283
v = self.get_str(random.randint(100, 4096))
274284
break
275285

276-
self.c.add(k, v)
277-
self.data[k] = v
286+
assert(self.c.add(k, v))
287+
self.data[k] = str(v)
288+
if k in self.num_key:
289+
self.num_key.remove(k)
278290

279291
def replace(self):
280292
if not self.data:
281293
return
282-
k = ramdom.choice(self.data.keys())
294+
k = random.choice(self.data.keys())
283295
v = self.get_str(random.randint(100, 4096))
284-
self.c.replace(k, v)
285-
self.data[k] = v
296+
assert(self.c.replace(k, v))
297+
self.data[k] = str(v)
298+
if k in self.num_key:
299+
self.num_key.remove(k)
286300

287301
def run(self, num):
288302
while num:
@@ -295,16 +309,20 @@ def run(self, num):
295309
self.incr()
296310
elif n > 65:
297311
self.decr()
298-
elif n > 55:
312+
elif n > 60:
299313
self.prepend()
300-
elif n > 50:
314+
elif n > 55:
301315
self.append()
316+
elif n > 45:
317+
self.delete()
302318
elif n > 30:
303319
self.set()
304320
else:
305321
self.get()
322+
num -= 1
306323

307324

308325
def test_memcache_synthetic():
309326
s = MemcacheSynthetic()
327+
random.seed(time.time())
310328
s.run(10000)

0 commit comments

Comments
 (0)