Commit 745b6ee9 authored by Yuqi Zhang's avatar Yuqi Zhang
Browse files

completed connection part for dmck. bug fixes

parent 6ff74e49
......@@ -21,7 +21,7 @@ dmck: $(OBJS)
.PHONY: stop clean run
stop:
@../redis_test/shutdown.sh 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 2>/dev/null 1>&2
@../experiment/redis_test/shutdown.sh 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 2>/dev/null 1>&2
clean:
rm -rf dmck_run $(OBJS) $(INCLUDES) *.rdb *.log
......
#include <iostream>
#include "redis_connect.h"
int main()
{
std::istream::sync_with_stdio(false);
std::ostream::sync_with_stdio(false);
std::cout<<"hi"<<std::endl;
redis_connect c1("127.0.0.1", 6379, 3, 0);
redis_connect c2("127.0.0.1", 6380, 3, 1);
redis_connect c3("127.0.0.1", 6381, 3, 2);
c1.exec("rzadd s a 1");
c2.exec("rzadd s a 2");
auto r=c1.exec("rzscore s a");
print_reply(r);
r=c2.exec("rzscore s a");
print_reply(r);
r=c2.get_inner_msg();
c1.pass_inner_msg(r);
r=c1.exec("rzscore s a");
print_reply(r);
r=c3.exec("rzscore s a");
print_reply(r);
r=c1.get_inner_msg();
print_reply(r);
c3.pass_inner_msg(r);
r=c3.exec("rzscore s a");
print_reply(r);
return 0;
}
......@@ -6,12 +6,14 @@
#define DMCK_REDIS_CONNECT_H
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#if defined(__linux__)
#include <hiredis/hiredis.h>
......@@ -22,8 +24,51 @@
#endif
#define REDIS_SERVER "../redis-6.0.5/src/redis-server"
#define REDIS_CONF "../redis-6.0.5/redis.conf"
#define REDIS_CLIENT "../redis-6.0.5/src/redis-cli"
using redisReply_ptr = std::unique_ptr<redisReply, decltype(freeReplyObject) *>;
static void print_reply(redisReply *rpl, int depth)
{
for (int i = 0; i < depth - 1; ++i)
std::cout << " ";
switch (rpl->type)
{
case REDIS_REPLY_INTEGER:
std::cout << rpl->integer;
break;
case REDIS_REPLY_DOUBLE:
std::cout << rpl->dval;
if (rpl->str != nullptr) std::cout << rpl->str;
break;
case REDIS_REPLY_ERROR:
std::cout << "Error: " << rpl->str;
break;
case REDIS_REPLY_STRING:
std::cout << rpl->str;
break;
case REDIS_REPLY_VERB:
std::cout << "Verb_" << rpl->vtype << ": " << rpl->str;
break;
case REDIS_REPLY_ARRAY:
for (int i = 0; i < rpl->elements; ++i)
print_reply(rpl->element[i], depth + 1);
break;
case REDIS_REPLY_NIL:
std::cout << "NULL";
break;
}
if (rpl->type != REDIS_REPLY_ARRAY) std::cout << std::endl;
}
static void print_reply(redisReply_ptr &rpl)
{
print_reply(rpl.get(), 0);
std::cout << "----" << std::endl;
}
class redis_connect
{
private:
......@@ -118,6 +163,20 @@ public:
redis_connect(const char *ip, int port, int size, int id)
: ip(ip), port(port), size(size), id(id)
{
std::ostringstream stream;
stream << REDIS_SERVER << " " << REDIS_CONF << " "
<< "--protected-mode no "
<< "--daemonize yes "
<< "--loglevel debug "
<< "--io-threads 2 "
<< "--port " << port << " "
<< "--logfile " << port << ".log "
<< "--dbfilename " << port << ".rdb "
<< "--pidfile /var/run/redis_" << port << ".pid "
<< "1>/dev/null";
system(stream.str().c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(500));
connect_server_instruct();
connect_server_listen();
connect_client();
......@@ -143,9 +202,13 @@ public:
~redis_connect()
{
if (client != nullptr) redisFree(client);
if (server_instruct != nullptr) redisFree(server_instruct);
if (server_listen != nullptr) redisFree(server_listen);
if (client != nullptr) redisFree(client);
std::ostringstream stream;
stream << REDIS_CLIENT << " -h 127.0.0.1 -p " << port << " SHUTDOWN NOSAVE";
system(stream.str().c_str());
}
};
......
......@@ -55,6 +55,9 @@ void repltestCommand(client *c)
{
listAddNodeTail(server.replicas, c);
serverLog(LL_NOTICE, "A listening fake replica.");
c->flags |= CLIENT_REPLICA_MESSAGE;
addReply(c, shared.ok);
c->flags &= ~CLIENT_REPLICA_MESSAGE;
}
if (c->argc == 3)
......@@ -66,8 +69,8 @@ void repltestCommand(client *c)
server.p2p_count = (int)size;
server.p2p_id = (int)id;
serverLog(LL_NOTICE, "An instructing fake replica.");
addReply(c, shared.ok);
}
addReply(c, shared.ok);
}
void replicateCommand(client *c)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment