Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
clientlet: remove class clientlet, add new class thread_access_checker
Browse files Browse the repository at this point in the history
Conflicts:
	include/dsn/cpp/serverlet.h
	include/dsn/service_api_cpp.h
	include/dsn/tool/cli/cli.client.h
	src/core/tests/async_call.cpp
	src/core/tests/rpc.cpp
	src/dist/replication/lib/replica.h
	src/dist/replication/lib/replica_chkpt.cpp
	src/dist/replication/lib/replica_stub.cpp
	src/dist/replication/zookeeper/lock_types.h
  • Loading branch information
shengofsun committed Jul 9, 2018
1 parent acf7545 commit 69512b7
Show file tree
Hide file tree
Showing 60 changed files with 257 additions and 188 deletions.
2 changes: 1 addition & 1 deletion include/dsn/cpp/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <dsn/tool-api/task_code.h>
#include <dsn/tool-api/task_tracker.h>
#include <dsn/cpp/clientlet.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/utility/chrono_literals.h>
#include <dsn/utility/apply.h>

Expand Down
1 change: 1 addition & 0 deletions include/dsn/cpp/rpc_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <dsn/c/api_common.h>
#include <dsn/c/api_layer1.h>
#include <dsn/service_api_cpp.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/tool-api/task_tracker.h>
#include <dsn/utility/smart_pointers.h>
#include <dsn/utility/chrono_literals.h>
Expand Down
4 changes: 2 additions & 2 deletions include/dsn/cpp/serverlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/
#pragma once

#include <dsn/cpp/clientlet.h>
#include <dsn/cpp/service_app.h>
#include <dsn/cpp/rpc_holder.h>
#include <dsn/cpp/serialization.h>

namespace dsn {
/*!
Expand Down Expand Up @@ -104,7 +104,7 @@ class rpc_replier
};

template <typename T> // where T : serverlet<T>
class serverlet : public virtual clientlet
class serverlet
{
public:
explicit serverlet(const char *nm);
Expand Down
4 changes: 2 additions & 2 deletions include/dsn/dist/failure_detector/fd.client.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
#include "fd.code.definition.h"
#include <iostream>
#include <dsn/utility/optional.h>
#include <dsn/cpp/clientlet.h>
#include <dsn/tool-api/async_calls.h>

namespace dsn {
namespace fd {
class failure_detector_client : public virtual ::dsn::clientlet
class failure_detector_client
{
public:
failure_detector_client(::dsn::rpc_address server) { _server = server; }
Expand Down
1 change: 0 additions & 1 deletion include/dsn/dist/failure_detector/fd.code.definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/

#pragma once
#include <dsn/cpp/clientlet.h>
#include <dsn/dist/failure_detector/fd.types.h>

namespace dsn {
Expand Down
1 change: 1 addition & 0 deletions include/dsn/dist/replication/replication_ddl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <map>
#include <dsn/dist/replication.h>
#include <dsn/tool-api/task_tracker.h>
#include <dsn/tool-api/async_calls.h>

namespace dsn {
namespace replication {
Expand Down
1 change: 0 additions & 1 deletion include/dsn/service_api_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <dsn/cpp/serialization_helper/dsn.layer2.types.h>
#include <dsn/cpp/rpc_stream.h>
#include <dsn/cpp/zlocks.h>
#include <dsn/cpp/clientlet.h>
#include <dsn/cpp/serverlet.h>
#include <dsn/cpp/service_app.h>
#include <dsn/tool-api/rpc_address.h>
Expand Down
24 changes: 0 additions & 24 deletions include/dsn/cpp/clientlet.h → include/dsn/tool-api/async_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,6 @@
#include <dsn/cpp/serialization.h>

namespace dsn {
/*
clientlet is the base class for RPC service and client
there can be multiple clientlet in the system
*/
class clientlet
{
public:
clientlet();
virtual ~clientlet();
rpc_address primary_address() { return dsn_primary_address(); }

static uint32_t random32(uint32_t min, uint32_t max) { return dsn_random32(min, max); }
static uint64_t random64(uint64_t min, uint64_t max) { return dsn_random64(min, max); }
static uint64_t now_ns() { return dsn_now_ns(); }
static uint64_t now_us() { return dsn_now_us(); }
static uint64_t now_ms() { return dsn_now_ms(); }

protected:
void check_hashed_access();

private:
int _access_thread_id;
bool _access_thread_id_inited;
};

inline void empty_rpc_handler(error_code, dsn_message_t, dsn_message_t) {}

Expand Down
47 changes: 47 additions & 0 deletions include/dsn/tool-api/thread_access_checker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace dsn {

///
/// a simple class used to check if some code is accessed by only one thread.
/// please refer to @replica.h and @lock_struct.h for a sample usage
///
class thread_access_checker
{
public:
thread_access_checker();
~thread_access_checker();

void only_one_thread_access();

private:
// TODO: the implementation is not thread safe. use atomic variable to reimplement this
int _access_thread_id;
bool _access_thread_id_inited;
};
}
1 change: 1 addition & 0 deletions include/dsn/tool/cli/cli.client.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#pragma once
#include <dsn/utility/optional.h>
#include <dsn/tool-api/task_tracker.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/tool/cli.h>
#include <iostream>

Expand Down
4 changes: 2 additions & 2 deletions src/apps/echo/echo.app.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class echo_server_app : public ::dsn::service_app
};

// client app example
class echo_client_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class echo_client_app : public ::dsn::service_app
{
public:
echo_client_app(const service_app_info *info) : ::dsn::service_app(info) {}
Expand Down Expand Up @@ -112,7 +112,7 @@ class echo_client_app : public ::dsn::service_app, public virtual ::dsn::clientl
std::unique_ptr<echo_client> _echo_client;
};

class echo_perf_test_client_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class echo_perf_test_client_app : public ::dsn::service_app
{
public:
echo_perf_test_client_app(const service_app_info *info) : ::dsn::service_app(info)
Expand Down
3 changes: 2 additions & 1 deletion src/apps/echo/echo.client.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
#include "echo.code.definition.h"
#include <iostream>
#include <dsn/utility/optional.h>
#include <dsn/tool-api/async_calls.h>

namespace dsn {
namespace example {
class echo_client : public virtual ::dsn::clientlet
class echo_client
{
public:
echo_client(::dsn::rpc_address server) { _server = server; }
Expand Down
3 changes: 2 additions & 1 deletion src/apps/nfs/nfs_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
*/
#pragma once
#include <dsn/utility/optional.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/tool/nfs.h>
#include <iostream>

namespace dsn {
namespace service {
class nfs_client : public virtual ::dsn::clientlet
class nfs_client
{
public:
nfs_client(::dsn::rpc_address server) { _server = server; }
Expand Down
4 changes: 3 additions & 1 deletion src/apps/nfs/nfs_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
* xxxx-xx-xx, author, first version
* xxxx-xx-xx, author, fix bug about xxx
*/
#include "nfs_server_impl.h"
#include <cstdlib>
#include <sys/stat.h>
#include <dsn/utility/filesystem.h>
#include <dsn/tool-api/async_calls.h>

#include "nfs_server_impl.h"

namespace dsn {
namespace service {
Expand Down
5 changes: 3 additions & 2 deletions src/apps/nfs_test/nfs_test.app.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
#pragma once
#include <dsn/tool-api/task_tracker.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/dist/replication.h>
#include <dsn/tool/nfs.h>

Expand All @@ -42,7 +43,7 @@ namespace replication {
namespace application {

// server app example
class nfs_server_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class nfs_server_app : public ::dsn::service_app
{
public:
nfs_server_app(const service_app_info *info) : ::dsn::service_app(info) {}
Expand All @@ -57,7 +58,7 @@ class nfs_server_app : public ::dsn::service_app, public virtual ::dsn::clientle
};

// client app example
class nfs_client_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class nfs_client_app : public ::dsn::service_app
{
public:
nfs_client_app(const service_app_info *info) : ::dsn::service_app(info)
Expand Down
4 changes: 2 additions & 2 deletions src/apps/skv/simple_kv.app.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace dsn {
namespace replication {
namespace application {
// client app example
class simple_kv_client_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class simple_kv_client_app : public ::dsn::service_app
{
public:
simple_kv_client_app(const service_app_info *info) : ::dsn::service_app(info) {}
Expand Down Expand Up @@ -127,7 +127,7 @@ class simple_kv_client_app : public ::dsn::service_app, public virtual ::dsn::cl
dsn::task_tracker _tracker;
};

class simple_kv_perf_test_client_app : public ::dsn::service_app, public virtual ::dsn::clientlet
class simple_kv_perf_test_client_app : public ::dsn::service_app
{
public:
simple_kv_perf_test_client_app(const service_app_info *info) : ::dsn::service_app(info) {}
Expand Down
3 changes: 2 additions & 1 deletion src/apps/skv/simple_kv.client.2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include "simple_kv.types.h"
#include <iostream>
#include <dsn/utility/optional.h>
#include <dsn/tool-api/async_calls.h>

namespace dsn {
namespace replication {
namespace application {
class simple_kv_client2 : public virtual ::dsn::clientlet
class simple_kv_client2
{
public:
simple_kv_client2(::dsn::rpc_address server) { _server = server; }
Expand Down
8 changes: 4 additions & 4 deletions src/apps/skv/simple_kv.client.perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class simple_kv_perf_test_client : public simple_kv_client2,

void send_one_read(int payload_bytes, int key_space_size)
{
auto rs = random64(0, 10000000) % key_space_size;
auto rs = dsn_random64(0, 10000000) % key_space_size;
std::stringstream ss;
ss << "key." << rs << "." << std::string(payload_bytes, 'x');

Expand All @@ -78,7 +78,7 @@ class simple_kv_perf_test_client : public simple_kv_client2,

void send_one_write(int payload_bytes, int key_space_size)
{
auto rs = random64(0, 10000000) % key_space_size;
auto rs = dsn_random64(0, 10000000) % key_space_size;
std::stringstream ss;
ss << "key." << rs;

Expand All @@ -94,7 +94,7 @@ class simple_kv_perf_test_client : public simple_kv_client2,

void send_one_append(int payload_bytes, int key_space_size)
{
auto rs = random64(0, 10000000) % key_space_size;
auto rs = dsn_random64(0, 10000000) % key_space_size;
std::stringstream ss;
ss << "key." << rs;
kv_pair req = {ss.str(), std::string(payload_bytes, 'x')};
Expand All @@ -110,4 +110,4 @@ class simple_kv_perf_test_client : public simple_kv_client2,
};
}
}
}
}
21 changes: 2 additions & 19 deletions src/core/core/clientlet.cpp → src/core/core/async_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,14 @@
* xxxx-xx-xx, author, fix bug about xxx
*/

#include <dsn/cpp/clientlet.h>
#include <dsn/cpp/service_app.h>
#include <dsn/utility/singleton.h>
#include <dsn/tool-api/task.h>
#include <dsn/tool-api/async_calls.h>
#include <iostream>
#include <map>

namespace dsn {

clientlet::clientlet() { _access_thread_id_inited = false; }

clientlet::~clientlet() { _access_thread_id_inited = false; }

void clientlet::check_hashed_access()
{
if (_access_thread_id_inited) {
dassert(::dsn::utils::get_current_tid() == _access_thread_id,
"the service is assumed to be accessed by one thread only!");
} else {
_access_thread_id = ::dsn::utils::get_current_tid();
_access_thread_id_inited = true;
}
}

namespace file {

void copy_remote_files_impl(::dsn::rpc_address remote,
Expand All @@ -83,5 +67,4 @@ void copy_remote_files_impl(::dsn::rpc_address remote,
}
}
}

} // end namespace dsn::service
}
1 change: 1 addition & 0 deletions src/core/core/partition_resolver_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "partition_resolver_simple.h"
#include <dsn/utility/utils.h>
#include <dsn/tool-api/async_calls.h>

namespace dsn {
namespace dist {
Expand Down
2 changes: 1 addition & 1 deletion src/core/core/rpc_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
#include <dsn/tool-api/group_address.h>
#include <dsn/tool-api/uri_address.h>
#include <dsn/tool-api/task_queue.h>
#include <dsn/tool-api/async_calls.h>
#include <dsn/cpp/serialization.h>
#include <dsn/cpp/clientlet.h>
#include <set>

namespace dsn {
Expand Down
21 changes: 21 additions & 0 deletions src/core/core/thread_access_checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <dsn/utility/utils.h>
#include <dsn/tool-api/thread_access_checker.h>
#include <dsn/c/api_utilities.h>

namespace dsn {

thread_access_checker::thread_access_checker() { _access_thread_id_inited = false; }

thread_access_checker::~thread_access_checker() { _access_thread_id_inited = false; }

void thread_access_checker::only_one_thread_access()
{
if (_access_thread_id_inited) {
dassert(::dsn::utils::get_current_tid() == _access_thread_id,
"the service is assumed to be accessed by one thread only!");
} else {
_access_thread_id = ::dsn::utils::get_current_tid();
_access_thread_id_inited = true;
}
}
}
Loading

0 comments on commit 69512b7

Please sign in to comment.