Skip to content

Commit

Permalink
修复IOManager事件触发的bug,增加NameServer
Browse files Browse the repository at this point in the history
Signed-off-by: sylar-yin <564628276@qq.com>
  • Loading branch information
sylar-yin committed Jul 25, 2019
1 parent 3acc7df commit 702889d
Show file tree
Hide file tree
Showing 27 changed files with 388 additions and 53 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ include_directories(/apps/sylar/include)
link_directories(/apps/sylar/lib)
link_directories(/apps/sylar/lib64)

option(TEST "ON for complile test" OFF)
option(BUILD_TEST "ON for complile test" OFF)

find_package(Boost REQUIRED)
if(Boost_FOUND)
Expand Down Expand Up @@ -79,6 +79,9 @@ set(LIB_SRC
sylar/log.cc
sylar/module.cc
sylar/mutex.cc
sylar/ns/name_server_module.cc
sylar/ns/ns_client.cc
sylar/ns/ns_protocol.cc
sylar/protocol.cc
sylar/rock/rock_protocol.cc
sylar/rock/rock_server.cc
Expand All @@ -105,7 +108,9 @@ ragelmaker(sylar/http/http11_parser.rl LIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sylar
ragelmaker(sylar/http/httpclient_parser.rl LIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sylar/http)
ragelmaker(sylar/uri.rl LIB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/sylar)

#PROTOBUF_GENERATE_CPP(PB_SRCS PB_HDRS sylar/test.proto)
protobufmaker(sylar/ns/ns_protobuf.proto LIB_SRC ${CMAKE_CURRENT_SOURCE_DIR})
#PROTOBUF_GENERATE_CPP(PB_SRCS PB_HDRS sylar/ns/ns_protobuf.proto)
#message("****${PB_SRCS}***${PB_HDRS}****")
#list(APPEND LIB_SRC ${PB_SRCS})
#
#message(STATUS ${LIB_SRC})
Expand Down Expand Up @@ -135,7 +140,7 @@ set(LIBS
sqlite3
tinyxml2
)
if(TEST)
if(BUILD_TEST)
sylar_add_executable(test1 "tests/test.cc" sylar "${LIBS}")
sylar_add_executable(test_config "tests/test_config.cc" sylar "${LIBS}")
sylar_add_executable(test_thread "tests/test_thread.cc" sylar "${LIBS}")
Expand Down Expand Up @@ -176,6 +181,7 @@ sylar_add_executable(test_sqlite3 "tests/test_sqlite3.cc" sylar "${LIBS}")
sylar_add_executable(test_rock "tests/test_rock.cc" sylar "${LIBS}")
sylar_add_executable(test_email "tests/test_email.cc" sylar "${LIBS}")
sylar_add_executable(test_mysql "tests/test_mysql.cc" sylar "${LIBS}")
sylar_add_executable(test_nameserver "tests/test_nameserver.cc" sylar "${LIBS}")

set(ORM_SRCS
sylar/orm/table.cc
Expand Down
9 changes: 6 additions & 3 deletions bin/conf/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ servers:
timeout: 1000
name: sylar/1.1
accept_worker: accept
process_worker: io
io_worker: http_io
process_worker: http_io
type: http
- address: ["0.0.0.0:8072", "localhost:8071"]
keepalive: 1
timeout: 1000
name: sylar/2.1
accept_worker: accept
process_worker: io
type: ws
io_worker: io
process_worker: worker
type: nameserver
- address: ["0.0.0.0:8062", "localhost:8061"]
timeout: 1000
name: sylar-rock/1.0
accept_worker: accept
io_worker: io
process_worker: io
type: rock
8 changes: 7 additions & 1 deletion bin/conf/worker.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
workers:
io:
thread_num: 4
thread_num: 8
http_io:
thread_num: 1
accept:
thread_num: 1
worker:
thread_num: 8
notify:
thread_num: 8
22 changes: 22 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ function(ragelmaker src_rl outputlist outputdir)
set_source_files_properties(${rl_out} PROPERTIES GENERATED TRUE)
endfunction(ragelmaker)

function(protobufmaker src_proto outputlist outputdir)
#Create a custom build step that will call ragel on the provided src_rl file.
#The output .cpp file will be appended to the variable name passed in outputlist.

get_filename_component(src_file ${src_proto} NAME_WE)
get_filename_component(src_path ${src_proto} PATH)

set(protobuf_out ${outputdir}/${src_path}/${src_file}.pb.cc)

#adding to the list inside a function takes special care, we cannot use list(APPEND...)
#because the results are local scope only
set(${outputlist} ${${outputlist}} ${protobuf_out} PARENT_SCOPE)

add_custom_command(
OUTPUT ${protobuf_out}
COMMAND protoc --cpp_out=${outputdir} -I${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${src_proto}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src_proto}
)
set_source_files_properties(${protobuf_out} PROPERTIES GENERATED TRUE)
endfunction(protobufmaker)


function(sylar_add_executable targetname srcs depends libs)
add_executable(${targetname} ${srcs})
add_dependencies(${targetname} ${depends})
Expand Down
22 changes: 18 additions & 4 deletions sylar/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "sylar/worker.h"
#include "sylar/http/ws_server.h"
#include "sylar/rock/rock_server.h"
#include "sylar/ns/name_server_module.h"

namespace sylar {

Expand Down Expand Up @@ -193,6 +194,7 @@ int Application::run_fiber() {
_exit(0);
}
IOManager* accept_worker = sylar::IOManager::GetThis();
IOManager* io_worker = sylar::IOManager::GetThis();
IOManager* process_worker = sylar::IOManager::GetThis();
if(!i.accept_worker.empty()) {
accept_worker = sylar::WorkerMgr::GetInstance()->getAsIOManager(i.accept_worker).get();
Expand All @@ -202,6 +204,14 @@ int Application::run_fiber() {
_exit(0);
}
}
if(!i.io_worker.empty()) {
io_worker = sylar::WorkerMgr::GetInstance()->getAsIOManager(i.io_worker).get();
if(!io_worker) {
SYLAR_LOG_ERROR(g_logger) << "io_worker: " << i.io_worker
<< " not exists";
_exit(0);
}
}
if(!i.process_worker.empty()) {
process_worker = sylar::WorkerMgr::GetInstance()->getAsIOManager(i.process_worker).get();
if(!process_worker) {
Expand All @@ -214,13 +224,17 @@ int Application::run_fiber() {
TcpServer::ptr server;
if(i.type == "http") {
server.reset(new sylar::http::HttpServer(i.keepalive,
process_worker, accept_worker));
process_worker, io_worker, accept_worker));
} else if(i.type == "ws") {
server.reset(new sylar::http::WSServer(
process_worker, accept_worker));
process_worker, io_worker, accept_worker));
} else if(i.type == "rock") {
server.reset(new sylar::RockServer(
process_worker, accept_worker));
server.reset(new sylar::RockServer("rock",
process_worker, io_worker, accept_worker));
} else if(i.type == "nameserver") {
server.reset(new sylar::RockServer("nameserver",
process_worker, io_worker, accept_worker));
ModuleMgr::GetInstance()->add(std::make_shared<sylar::ns::NameServerModule>());
} else {
SYLAR_LOG_ERROR(g_logger) << "invalid server type=" << i.type
<< LexicalCast<TcpServerConf, std::string>()(i);
Expand Down
2 changes: 1 addition & 1 deletion sylar/bytearray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void ByteArray::addCapacity(size_t size) {
}

size = size - old_cap;
size_t count = (size / m_baseSize) + (((size % m_baseSize) > old_cap) ? 1 : 0);
size_t count = ceil(1.0 * size / m_baseSize);
Node* tmp = m_root;
while(tmp->next) {
tmp = tmp->next;
Expand Down
9 changes: 5 additions & 4 deletions sylar/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "thread.h"
#include "log.h"
#include "util.h"

namespace sylar {

Expand Down Expand Up @@ -357,7 +358,7 @@ class ConfigVar : public ConfigVarBase {
return ToStr()(m_val);
} catch (std::exception& e) {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "ConfigVar::toString exception "
<< e.what() << " convert: " << typeid(m_val).name() << " to string"
<< e.what() << " convert: " << TypeToName<T>() << " to string"
<< " name=" << m_name;
}
return "";
Expand All @@ -372,7 +373,7 @@ class ConfigVar : public ConfigVarBase {
setValue(FromStr()(val));
} catch (std::exception& e) {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "ConfigVar::fromString exception "
<< e.what() << " convert: string to " << typeid(m_val).name()
<< e.what() << " convert: string to " << TypeToName<T>()
<< " name=" << m_name
<< " - " << val;
}
Expand Down Expand Up @@ -408,7 +409,7 @@ class ConfigVar : public ConfigVarBase {
/**
* @brief 返回参数值的类型名称(typeinfo)
*/
std::string getTypeName() const override { return typeid(T).name();}
std::string getTypeName() const override { return TypeToName<T>();}

/**
* @brief 添加变化回调函数
Expand Down Expand Up @@ -487,7 +488,7 @@ class Config {
return tmp;
} else {
SYLAR_LOG_ERROR(SYLAR_LOG_ROOT()) << "Lookup name=" << name << " exists but type not "
<< typeid(T).name() << " real_type=" << it->second->getTypeName()
<< TypeToName<T>() << " real_type=" << it->second->getTypeName()
<< " " << it->second->toString();
return nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions sylar/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ int start_daemon(int argc, char** argv
, std::function<int(int argc, char** argv)> main_cb
, bool is_daemon) {
if(!is_daemon) {
ProcessInfoMgr::GetInstance()->parent_id = getpid();
ProcessInfoMgr::GetInstance()->parent_start_time = time(0);
return real_start(argc, argv, main_cb);
}
return real_daemon(argc, argv, main_cb);
Expand Down
7 changes: 4 additions & 3 deletions sylar/fiber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static thread_local Fiber* t_fiber = nullptr;
static thread_local Fiber::ptr t_threadFiber = nullptr;

static ConfigVar<uint32_t>::ptr g_fiber_stack_size =
Config::Lookup<uint32_t>("fiber.stack_size", 1024 * 1024, "fiber stack size");
Config::Lookup<uint32_t>("fiber.stack_size", 128 * 1024, "fiber stack size");

class MallocStackAllocator {
public:
Expand Down Expand Up @@ -48,7 +48,7 @@ Fiber::Fiber() {

++s_fiber_count;

SYLAR_LOG_DEBUG(g_logger) << "Fiber::Fiber";
SYLAR_LOG_DEBUG(g_logger) << "Fiber::Fiber main";
}

Fiber::Fiber(std::function<void()> cb, size_t stacksize, bool use_caller)
Expand Down Expand Up @@ -91,7 +91,8 @@ Fiber::~Fiber() {
SetThis(nullptr);
}
}
SYLAR_LOG_DEBUG(g_logger) << "Fiber::~Fiber id=" << m_id;
SYLAR_LOG_DEBUG(g_logger) << "Fiber::~Fiber id=" << m_id
<< " total=" << s_fiber_count;
}

//重置协程函数,并重置状态
Expand Down
3 changes: 2 additions & 1 deletion sylar/http/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ static sylar::Logger::ptr g_logger = SYLAR_LOG_NAME("system");

HttpServer::HttpServer(bool keepalive
,sylar::IOManager* worker
,sylar::IOManager* io_worker
,sylar::IOManager* accept_worker)
:TcpServer(worker, accept_worker)
:TcpServer(worker, io_worker, accept_worker)
,m_isKeepalive(keepalive) {
m_dispatch.reset(new ServletDispatch);

Expand Down
1 change: 1 addition & 0 deletions sylar/http/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class HttpServer : public TcpServer {
*/
HttpServer(bool keepalive = false
,sylar::IOManager* worker = sylar::IOManager::GetThis()
,sylar::IOManager* io_worker = sylar::IOManager::GetThis()
,sylar::IOManager* accept_worker = sylar::IOManager::GetThis());

/**
Expand Down
4 changes: 2 additions & 2 deletions sylar/http/ws_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace http {

static sylar::Logger::ptr g_logger = SYLAR_LOG_NAME("system");

WSServer::WSServer(sylar::IOManager* worker, sylar::IOManager* accept_worker)
:TcpServer(worker, accept_worker) {
WSServer::WSServer(sylar::IOManager* worker, sylar::IOManager* io_worker, sylar::IOManager* accept_worker)
:TcpServer(worker, io_worker, accept_worker) {
m_dispatch.reset(new WSServletDispatch);
m_type = "websocket_server";
}
Expand Down
1 change: 1 addition & 0 deletions sylar/http/ws_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class WSServer : public TcpServer {
typedef std::shared_ptr<WSServer> ptr;

WSServer(sylar::IOManager* worker = sylar::IOManager::GetThis()
, sylar::IOManager* io_worker = sylar::IOManager::GetThis()
, sylar::IOManager* accept_worker = sylar::IOManager::GetThis());

WSServletDispatch::ptr getWSServletDispatch() const { return m_dispatch;}
Expand Down
Loading

0 comments on commit 702889d

Please sign in to comment.