Skip to content

Commit

Permalink
local update
Browse files Browse the repository at this point in the history
  • Loading branch information
baotiao committed Sep 19, 2017
1 parent daa4814 commit 38e3d09
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ floyd/example/kv/sdk.pb.h
*t2

floyd/src/build_version.cc
floyd/example/redis/deploy.sh
18 changes: 18 additions & 0 deletions .travis.yml
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
83 changes: 83 additions & 0 deletions floyd/example/simple/test_lock.cc
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;
}
108 changes: 108 additions & 0 deletions floyd/example/simple/test_lock1.cc
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;
}
11 changes: 3 additions & 8 deletions floyd/proto/pr.sh
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...";

0 comments on commit 38e3d09

Please sign in to comment.