Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions include/dsn/cpp/callocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
# pragma once

# include <dsn/service_api_c.h>
# include <limits>
# include <memory>
# include <new>

namespace dsn {

Expand All @@ -49,7 +51,12 @@ namespace dsn {
public:
void* operator new(size_t size)
{
return a(uint32_t(size));
void* ptr = a(uint32_t(size));
if (ptr == nullptr)
{
throw std::bad_alloc();
}
return ptr;
}

void operator delete(void* p)
Expand All @@ -59,7 +66,12 @@ namespace dsn {

void* operator new[](size_t size)
{
return a((uint32_t)size);
void* ptr = a((uint32_t)size);
if (ptr == nullptr)
{
throw std::bad_alloc();
}
return ptr;
}

void operator delete[](void* p)
Expand All @@ -86,7 +98,23 @@ namespace dsn {

T* allocate(size_type n, const void *hint = 0)
{
return static_cast<T*>(a(uint32_t(n * sizeof(T))));
if (n == 0)
{
return nullptr;
}

// The underlying rDSN allocation API takes uint32_t bytes.
if (n > (std::numeric_limits<uint32_t>::max)() / sizeof(T))
{
throw std::bad_alloc();
}

T* ptr = static_cast<T*>(a(uint32_t(n * sizeof(T))));
if (ptr == nullptr)
{
throw std::bad_alloc();
}
return ptr;
}

void deallocate(T* p, size_type n)
Expand Down
2 changes: 1 addition & 1 deletion include/dsn/tool-api/task_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class task_spec : public extensible_object<task_spec, 4>
{
public:
DSN_API static task_spec* get(int ec);
DSN_API static void register_task_code(dsn_task_code_t code, dsn_task_type_t type, dsn_task_priority_t pri, dsn_threadpool_code_t pool);
DSN_API static bool register_task_code(dsn_task_code_t code, dsn_task_type_t type, dsn_task_priority_t pri, dsn_threadpool_code_t pool);

public:
// not configurable [
Expand Down
23 changes: 15 additions & 8 deletions include/dsn/utility/customizable_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,23 @@ int customized_id_mgr<T>::register_id(const char* name)
return -1;
}

int id = get_id(name);
if (-1 != id)
try
{
return id;
int id = get_id(name);
if (-1 != id)
{
return id;
}

int code = static_cast<int>(_names.size());
_names[std::string(name)] = code;
_names2.push_back(std::string(name));
return code;
}
catch (...)
{
return -1;
}

int code = static_cast<int>(_names.size());
_names[std::string(name)] = code;
_names2.push_back(std::string(name));
return code;
}

}} // end namespace dsn::utils
8 changes: 4 additions & 4 deletions src/core/src/coredump.win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace dsn {
s_dump_dir = dump_dir;

if (::GetModuleBaseNameA(::GetCurrentProcess(),
::GetModuleHandleA(NULL),
::GetModuleHandleA(nullptr),
s_app_name,
256
) == 0)
Expand Down Expand Up @@ -128,16 +128,16 @@ namespace dsn {
s_dump_dir.c_str(),
s_app_name,
::GetCurrentProcessId(),
(int64_t)time(NULL));
(int64_t)time(nullptr));
if (len < 0 || static_cast<size_t>(len) >= sizeof(szDumpPath))
{
szResult = "failed to format dump file path";
goto out_error;
}

// create the file
fh = ::CreateFileA(szDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
fh = ::CreateFileA(szDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, nullptr);
if (fh == INVALID_HANDLE_VALUE)
{
len = snprintf(szScratch,
Expand Down
79 changes: 55 additions & 24 deletions src/core/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
# include "transient_memory.h"
# include "library_utils.h"
# include <cstdio>
# include <exception>
# include <fstream>

# if defined(_WIN32)
Expand Down Expand Up @@ -305,9 +306,9 @@ DSN_API void dsn_config_dump(const char* file)
extern bool dsn_log_init();

// load all modules: local components, tools, frameworks, apps
static void load_all_modules(::dsn::configuration_ptr config)
static bool load_all_modules(::dsn::configuration_ptr config)
{
std::vector< std::pair<std::string, std::string> > modules;
std::vector<std::pair<std::string, std::string>> modules;
std::map<std::string, std::size_t> module_map; // name -> index in modules

// load local components, toollets, and tools
Expand Down Expand Up @@ -373,9 +374,8 @@ static void load_all_modules(::dsn::configuration_ptr config)
auto hmod = ::dsn::utils::load_dynamic_library(m.first.c_str(), search_dirs);
if (nullptr == hmod)
{
dassert(false, "cannot load shared library '%s' specified in config file",
m.first.c_str());
break;
derror("cannot load shared library '%s' specified in config file", m.first.c_str());
return false;
}
else
{
Expand All @@ -386,22 +386,26 @@ static void load_all_modules(::dsn::configuration_ptr config)
# if !defined(_WIN32)
typedef void(*dsn_module_init_fn)();
dsn_module_init_fn init_fn = (dsn_module_init_fn)::dsn::utils::load_symbol(hmod, "dsn_module_init");
dassert(init_fn != nullptr,
"dsn_module_init is not present (%s), use MODULE_INIT_BEGIN/END to define it",
m.first.c_str()
);
if (init_fn == nullptr)
{
derror("dsn_module_init is not present (%s), use MODULE_INIT_BEGIN/END to define it",
m.first.c_str());
return false;
}
init_fn();
# endif // ! _WIN32

// have dmodule_bridge_arguments?
if (m.second.length() > 0)
{
dsn_app_bridge_t bridge_ptr = (dsn_app_bridge_t)::dsn::utils::load_symbol(hmod, "dsn_app_bridge");
dassert(bridge_ptr != nullptr,
"when dmodule_bridge_arguments is present (%s), function dsn_app_bridge must be implemented in module %s",
m.second.c_str(),
m.first.c_str()
);
if (bridge_ptr == nullptr)
{
derror("when dmodule_bridge_arguments is present (%s), function dsn_app_bridge must be implemented in module %s",
m.second.c_str(),
m.first.c_str());
return false;
}

ddebug("call %s.dsn_app_bridge(...%s...)",
m.first.c_str(),
Expand All @@ -420,6 +424,7 @@ static void load_all_modules(::dsn::configuration_ptr config)
bridge_ptr((int)args_ptr.size(), &args_ptr[0]);
}
}
return true;
}

void run_all_unit_tests_prepare_when_necessary();
Expand Down Expand Up @@ -469,7 +474,10 @@ bool run(
}

// load plugged modules
load_all_modules(dsn_all.config);
if (!load_all_modules(dsn_all.config))
{
return false;
}

// prepare unit test run if necessary
run_all_unit_tests_prepare_when_necessary();
Expand Down Expand Up @@ -619,7 +627,11 @@ bool run(

if (create_it)
{
::dsn::service_engine::fast_instance().start_node(sp);
if (::dsn::service_engine::fast_instance().start_node(sp) == nullptr)
{
fprintf(stderr, "Fail to start app %s.\n", sp.name.c_str());
return false;
}
}
}

Expand Down Expand Up @@ -758,12 +770,22 @@ DSN_API void dsn_run(int argc, char** argv, bool sleep_after_init)
}
}

if (!run(config,
config_args.size() > 0 ? config_args.c_str() : nullptr,
overwrites.size() > 0 ? overwrites.c_str() : nullptr,
sleep_after_init,
app_list
))
bool run_succeeded = false;
try
{
run_succeeded = run(config,
config_args.size() > 0 ? config_args.c_str() : nullptr,
overwrites.size() > 0 ? overwrites.c_str() : nullptr,
sleep_after_init,
app_list
);
}
catch (const std::exception& err)
{
fprintf(stderr, "run the system failed due to exception: %s\n", err.what());
}

if (!run_succeeded)
{
fprintf(stderr, "run the system failed\n");
dsn_exit(-1);
Expand All @@ -779,8 +801,17 @@ DSN_API bool dsn_run_config(const char* config, bool sleep_after_init)
return false;
}

std::string name;
return run(config, nullptr, nullptr, sleep_after_init, name);
try
{
std::string name;
return run(config, nullptr, nullptr, sleep_after_init, name);
}
catch (const std::exception& err)
{
fprintf(stderr, "run the system failed due to exception: %s\n", err.what());
}

return false;
}

DSN_API int dsn_get_all_apps(dsn_app_info* info_buffer, int count)
Expand Down
3 changes: 1 addition & 2 deletions src/core/src/rpc_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ namespace dsn {
}
else
{
// mem leak, don't care as it halts the program
dassert(false, "create network failed, error_code: %s", ret.to_string());
derror("create network failed, error_code: %s", ret.to_string());
return nullptr;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/src/service_api_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ DSN_API dsn_task_code_t dsn_task_code_register(
return static_cast<dsn_task_code_t>(::dsn::TASK_CODE_INVALID);
}

auto r = static_cast<dsn_task_code_t>(::dsn::utils::customized_id_mgr<task_code_placeholder>::instance().register_id(name));
::dsn::task_spec::register_task_code(r, type, pri, pool);
auto r = static_cast<dsn_task_code_t>(
::dsn::utils::customized_id_mgr<task_code_placeholder>::instance().register_id(name));
if (!::dsn::task_spec::register_task_code(r, type, pri, pool))
{
return static_cast<dsn_task_code_t>(::dsn::TASK_CODE_INVALID);
}
return r;
}

Expand Down Expand Up @@ -1731,7 +1735,7 @@ static BOOL SuspendAllThreads()
threads.find(ti.th32ThreadID) == threads.end())
{
HANDLE hThread = ::OpenThread(THREAD_ALL_ACCESS, FALSE, ti.th32ThreadID);
if (hThread == NULL)
if (hThread == nullptr)
{
derror("OpenThread failed, err = %d", ::GetLastError());
goto err;
Expand Down
12 changes: 10 additions & 2 deletions src/core/src/service_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ error_code service_node::start()
// start io engines (only timer, disk and rpc), others are started in app start task
for (auto& io : _ios)
{
start_io_engine_in_main(io);
err = start_io_engine_in_main(io);
if (err != ERR_OK)
{
return err;
}
}

// start task engine
Expand Down Expand Up @@ -673,7 +677,11 @@ service_node* service_engine::start_node(service_app_spec& app_spec)

auto node = new service_node(app_spec);
error_code err = node->start();
dassert (err == ERR_OK, "service node start failed, err = %s", err.to_string());
if (err != ERR_OK)
{
derror("service node start failed, err = %s", err.to_string());
return nullptr;
}

_nodes_by_app_id[node->id()] = node;
for (auto p1 : node->spec().ports)
Expand Down
Loading
Loading