forked from PikaLabs/floyd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,4 @@ floyd/example/kv/sdk.pb.h | |
*t2 | ||
|
||
floyd/src/build_version.cc | ||
floyd/example/redis/deploy.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
sudo: required | ||
dist: trusty | ||
language: cpp | ||
|
||
os: | ||
- linux | ||
|
||
addons: | ||
apt: | ||
packages: ['zlib1g-dev', 'libbz2-dev', 'libsnappy-dev', 'curl', 'libprotobuf-dev', 'protobuf-compiler'] | ||
|
||
compiler: | ||
- gcc | ||
|
||
language: cpp | ||
|
||
script: | ||
- cd pink && ./build.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <sys/time.h> | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
#include "floyd/include/floyd.h" | ||
#include "slash/include/testutil.h" | ||
|
||
using namespace floyd; | ||
uint64_t NowMicros() { | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; | ||
} | ||
|
||
int main() | ||
{ | ||
Options op("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4311, "./data1/"); | ||
op.Dump(); | ||
|
||
Floyd *f1, *f2, *f3, *f4, *f5; | ||
|
||
slash::Status s; | ||
s = Floyd::Open(op, &f1); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op2("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4312, "./data2/"); | ||
s = Floyd::Open(op2, &f2); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op3("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4313, "./data3/"); | ||
s = Floyd::Open(op3, &f3); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op4("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4314, "./data4/"); | ||
s = Floyd::Open(op4, &f4); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op5("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4315, "./data5/"); | ||
s = Floyd::Open(op5, &f5); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
std::string msg; | ||
uint64_t st = NowMicros(), ed; | ||
|
||
while (1) { | ||
if (f1->HasLeader()) { | ||
break; | ||
} | ||
printf("electing leader... sleep 2s\n"); | ||
sleep(2); | ||
} | ||
|
||
int cnt; | ||
cnt = 5; | ||
while (cnt--) { | ||
f2->GetServerStatus(&msg); | ||
for (int j = 0; j < 100; j++) { | ||
f2->Write("zz2" + char(j), "value2" + char(j)); | ||
} | ||
printf("%s\n", msg.c_str()); | ||
} | ||
|
||
// two thread | ||
cnt = 100; | ||
while (cnt--) { | ||
uint64_t session; | ||
s = f1->TryLock("baotiao", &session); | ||
printf("TryLock status %s session %ld\n", s.ToString().c_str(), session); | ||
s = f1->UnLock("baotiao", session); | ||
printf("TryUnLock status %s session %ld\n", s.ToString().c_str(), session); | ||
} | ||
|
||
getchar(); | ||
delete f2; | ||
delete f3; | ||
delete f4; | ||
delete f5; | ||
delete f1; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <sys/time.h> | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
#include "floyd/include/floyd.h" | ||
#include "slash/include/testutil.h" | ||
|
||
using namespace floyd; | ||
uint64_t NowMicros() { | ||
struct timeval tv; | ||
gettimeofday(&tv, NULL); | ||
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; | ||
} | ||
|
||
Floyd *f1, *f2, *f3, *f4, *f5; | ||
slash::Status s; | ||
|
||
/* | ||
* in this case, thread1 get the lock first, and then sleep 6 second | ||
* during this time, thread2 try to get lock every second, it will failed every time. | ||
* at the 7 second, thread2 may get the lock | ||
*/ | ||
|
||
void *thread1_fun(void *arg) { | ||
uint64_t session; | ||
s = f1->TryLock("baotiao", &session); | ||
printf("thread1 TryLock status %s session %ld\n", s.ToString().c_str(), session); | ||
sleep(6); | ||
s = f1->UnLock("baotiao", session); | ||
printf("thread2 TryUnLock status %s session %ld\n", s.ToString().c_str(), session); | ||
} | ||
|
||
void *thread2_fun(void *arg) { | ||
while (1) { | ||
uint64_t session; | ||
s = f1->TryLock("baotiao", &session); | ||
printf("thread2 TryLock status %s session %ld\n", s.ToString().c_str(), session); | ||
s = f1->UnLock("baotiao", session); | ||
printf("thread2 TryUnLock status %s session %ld\n", s.ToString().c_str(), session); | ||
sleep(1); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
Options op("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4311, "./data1/"); | ||
op.Dump(); | ||
|
||
|
||
s = Floyd::Open(op, &f1); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op2("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4312, "./data2/"); | ||
s = Floyd::Open(op2, &f2); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op3("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4313, "./data3/"); | ||
s = Floyd::Open(op3, &f3); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op4("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4314, "./data4/"); | ||
s = Floyd::Open(op4, &f4); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
Options op5("127.0.0.1:4311,127.0.0.1:4312,127.0.0.1:4313,127.0.0.1:4314,127.0.0.1:4315", "127.0.0.1", 4315, "./data5/"); | ||
s = Floyd::Open(op5, &f5); | ||
printf("%s\n", s.ToString().c_str()); | ||
|
||
std::string msg; | ||
uint64_t st = NowMicros(), ed; | ||
|
||
while (1) { | ||
if (f1->HasLeader()) { | ||
break; | ||
} | ||
printf("electing leader... sleep 2s\n"); | ||
sleep(2); | ||
} | ||
|
||
int cnt; | ||
cnt = 5; | ||
while (cnt--) { | ||
f2->GetServerStatus(&msg); | ||
for (int j = 0; j < 100; j++) { | ||
f2->Write("zz2" + char(j), "value2" + char(j)); | ||
} | ||
printf("%s\n", msg.c_str()); | ||
} | ||
|
||
pthread_t t1, t2; | ||
pthread_create(&t1, NULL, thread1_fun, NULL); | ||
sleep(1); | ||
pthread_create(&t2, NULL, thread2_fun, NULL); | ||
|
||
pthread_join(t1, NULL); | ||
pthread_join(t2, NULL); | ||
|
||
getchar(); | ||
delete f2; | ||
delete f3; | ||
delete f4; | ||
delete f5; | ||
delete f1; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
#!/bin/sh | ||
set -x | ||
|
||
################### | ||
# floyd.proto | ||
################### | ||
FILE1="floyd" | ||
for file in $FILE1 ; do | ||
protoc -I=./ --cpp_out=./ ./$file.proto | ||
cp $file.pb.h $file.pb.cc ../src | ||
done | ||
protoc -I=./ --cpp_out=../src/ ./floyd.proto | ||
|
||
echo "run protoc success, go, go, go..."; |