Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-65:move appInfo no-static #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions src/app_demo/AppStateMachine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/************************************************************************
Copyright 2019-2022 eBay Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/

#include "AppStateMachine.h"
#include "should_be_generated/domain/common_types.h"

namespace gringofts::demo {

AppStateMachine::AppStateMachine() {
registerCommandProcessor(INCREASE_COMMAND, [this](const gringofts::Command &command,
std::vector<std::shared_ptr<gringofts::Event>> *events) {
return this->process(dynamic_cast<const IncreaseCommand&>(command), events);
});

registerEventApplier(PROCESSED_EVENT, [this](const gringofts::Event &event) -> gringofts::StateMachine & {
return this->apply(dynamic_cast<const ProcessedEvent&>(event));
});
}

ProcessHint AppStateMachine::processCommandAndApply(
const Command &command, std::vector<std::shared_ptr<Event>> *events) {
auto hint = processCommand(command, events);
if (events->empty()) {
return hint;
}

for (const auto &event : *events) {
applyEvent(*event);
}
return hint;
}
} // namespace gringofts::demo
15 changes: 10 additions & 5 deletions src/app_demo/AppStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ limitations under the License.

#include "execution/IncreaseApplier.h"
#include "execution/IncreaseHandler.h"
#include "should_be_generated/domain/AppStateMachine_internal.h"
#include "should_be_generated/domain/IncreaseCommand.h"
#include "should_be_generated/domain/ProcessedEvent.h"
#include "../app_util/AppStateMachine.h"

namespace gringofts {
namespace demo {

class AppStateMachine : public demo::AppStateMachine_internal {
class AppStateMachine : public gringofts::app::AppStateMachine {
public:
AppStateMachine();
~AppStateMachine() override = default;

ProcessHint processCommandAndApply(const Command &command, std::vector<std::shared_ptr<Event>> *events) override;

/**
* define getter() and setter()
*/
Expand All @@ -41,12 +46,12 @@ class AppStateMachine : public demo::AppStateMachine_internal {
* getter() and setter() to manipulate state.
*/
ProcessHint process(const IncreaseCommand &command,
std::vector<std::shared_ptr<Event>> *events) const override {
IncreaseHandler handler;
std::vector<std::shared_ptr<Event>> *events) const {
IncreaseHandler handler(*mAppInfo);
return handler.process(*this, command, events);
}

StateMachine &apply(const ProcessedEvent &event) override {
StateMachine &apply(const ProcessedEvent &event) {
IncreaseApplier applier;
applier.apply(event, this);
return *this;
Expand Down
10 changes: 7 additions & 3 deletions src/app_demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ ADD_PROTO_SET(demo_proto_library "${proto_file_list}" ${proto_generated_dir})
## src list
# app_demo
set(APP_DEMO_AUTOGEN
AppStateMachine.cpp
should_be_generated/app/App.cpp
should_be_generated/app/RequestCallData.cpp
should_be_generated/app/RequestReceiver.cpp
should_be_generated/domain/AppStateMachine_internal.cpp
should_be_generated/domain/CommandDecoderImpl.cpp
should_be_generated/domain/EventDecoderImpl.cpp
should_be_generated/domain/IncreaseCommand.cpp
Expand Down Expand Up @@ -55,7 +54,12 @@ target_link_libraries(app_demo demo_proto_library)
# executables
add_executable(DemoApp
should_be_generated/app/Main.cpp)
target_link_libraries(DemoApp app_demo gringofts_app_util gringofts_infra -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
#for macos
target_link_libraries(DemoApp app_demo gringofts_app_util gringofts_infra -Wl -lgrpc++_reflection)
elseif()
target_link_libraries(DemoApp app_demo gringofts_app_util gringofts_infra -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed)
endif()

#################################################################################################
#
Expand Down
6 changes: 3 additions & 3 deletions src/app_demo/execution/IncreaseHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ ProcessHint IncreaseHandler::process(const AppStateMachine &appStateMachine,
events->push_back(std::make_shared<ProcessedEvent>(TimeUtil::currentTimeInNanos(), increaseCommand.getRequest()));

for (auto &eventPtr : *events) {
eventPtr->setCreatorId(app::AppInfo::subsystemId());
eventPtr->setGroupId(app::AppInfo::groupId());
eventPtr->setGroupVersion(app::AppInfo::groupVersion());
eventPtr->setCreatorId(mAppInfo.subsystemId());
eventPtr->setGroupId(mAppInfo.groupId());
eventPtr->setGroupVersion(mAppInfo.groupVersion());
}

return ProcessHint{200, "Success"};
Expand Down
4 changes: 4 additions & 0 deletions src/app_demo/execution/IncreaseHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.

#include <string>

#include "../../app_util/AppInfo.h"
#include "../../infra/es/Event.h"
#include "../should_be_generated/domain/IncreaseCommand.h"

Expand All @@ -27,9 +28,12 @@ class AppStateMachine;

class IncreaseHandler {
public:
explicit IncreaseHandler(const app::AppInfo &appInfo) : mAppInfo(appInfo) {}
ProcessHint process(const AppStateMachine &appStateMachine,
const IncreaseCommand &increaseCommand,
std::vector<std::shared_ptr<Event>> *);
private:
const app::AppInfo &mAppInfo;
};

} /// namespace demo
Expand Down
38 changes: 20 additions & 18 deletions src/app_demo/should_be_generated/app/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ App::App(const char *configPath) : mIsShutdown(false) {
throw std::runtime_error("Cannot load config file");
}

app::AppInfo::init(reader);
mAppInfo = std::make_shared<app::AppInfo>(reader);

mCrypto = std::make_shared<gringofts::CryptoUtil>();
mCrypto->init(reader);
Expand All @@ -54,11 +54,11 @@ App::App(const char *configPath) : mIsShutdown(false) {

auto commandEventDecoder = std::make_shared<app::CommandEventDecoderImpl<EventDecoderImpl, CommandDecoderImpl>>();

const auto &appVersion = app::AppInfo::appVersion();
const auto &appVersion = mAppInfo->appVersion();
if (appVersion == "v2") {
mEventApplyLoop =
std::make_shared<app::EventApplyLoop<v2::RocksDBBackedAppStateMachine>>(
reader, commandEventDecoder, std::move(mReadonlyCommandEventStoreForEventApplyLoop), snapshotDir);
reader, commandEventDecoder, std::move(mReadonlyCommandEventStoreForEventApplyLoop), snapshotDir, mAppInfo);
mCommandProcessLoop = std::make_unique<CommandProcessLoop<v2::MemoryBackedAppStateMachine>>(
reader,
commandEventDecoder,
Expand All @@ -68,15 +68,17 @@ App::App(const char *configPath) : mIsShutdown(false) {
std::move(mReadonlyCommandEventStoreForCommandProcessLoop),
mCommandEventStore,
snapshotDir,
mFactory);
mFactory,
mAppInfo);
} else {
SPDLOG_ERROR("App version {} is not supported. Exiting...", appVersion);
assert(0);
}

mRequestReceiver = ::std::make_unique<RequestReceiver>(reader, mCommandQueue);
mNetAdminServer = ::std::make_unique<app::NetAdminServer>(reader, mEventApplyLoop);
mPostServer = std::make_unique<BundleExposePublisher>(reader, std::move(mReadonlyCommandEventStoreForPostServer));
mRequestReceiver = ::std::make_unique<RequestReceiver>(reader, mAppInfo->gateWayPort(), *mAppInfo, mCommandQueue);
mNetAdminServer = ::std::make_unique<app::NetAdminServer>(reader, mEventApplyLoop, mAppInfo);
mPostServer = std::make_unique<BundleExposePublisher>(reader, mAppInfo->fetchPort(),
std::move(mReadonlyCommandEventStoreForPostServer));
}

App::~App() {
Expand Down Expand Up @@ -123,11 +125,14 @@ void App::initMonitor(const INIReader &reader) {
void App::initMemoryPool(const INIReader &reader) {
if (gringofts::PerfConfig::getInstance().getMemoryPoolType() == "monotonic") {
mFactory = std::make_shared<gringofts::PMRContainerFactory>("PMRFactory",
std::make_unique<gringofts::MonotonicPMRMemoryPool>(
"monotonicPool", gringofts::PerfConfig::getInstance().getMaxMemoryPoolSizeInMB()));
std::make_unique<gringofts::MonotonicPMRMemoryPool>(
"monotonicPool",
gringofts::PerfConfig::getInstance()
.getMaxMemoryPoolSizeInMB()));
} else {
mFactory = std::make_shared<gringofts::PMRContainerFactory>("PMRFactory",
std::make_unique<gringofts::NewDeleteMemoryPool>("newDeletePool"));
std::make_unique<gringofts::NewDeleteMemoryPool>(
"newDeletePool"));
}
}

Expand Down Expand Up @@ -172,9 +177,9 @@ void App::initCommandEventStore(const INIReader &reader) {

std::shared_ptr<app::CommandEventDecoderImpl<EventDecoderImpl, CommandDecoderImpl>> commandEventDecoder =
std::make_shared<app::CommandEventDecoderImpl<EventDecoderImpl, CommandDecoderImpl>>();
auto myNodeId = gringofts::app::AppInfo::getMyNodeId();
auto myClusterInfo = gringofts::app::AppInfo::getMyClusterInfo();
auto raftImpl = raft::buildRaftImpl(configPath.c_str(), myNodeId, myClusterInfo);
auto myNodeId = mAppInfo->getMyNodeId();
auto myCluster = mAppInfo->getMyCluster();
auto raftImpl = raft::buildRaftImpl(configPath.c_str(), myNodeId, myCluster);
auto metricsAdaptor = std::make_shared<RaftMonitorAdaptor>(raftImpl);
enableMonitorable(metricsAdaptor);
mCommandEventStore = std::make_shared<RaftCommandEventStore>(raftImpl, mCrypto);
Expand All @@ -193,10 +198,7 @@ void App::initCommandEventStore(const INIReader &reader) {
}

void App::startRequestReceiver() {
mServerThread = std::thread([this]() {
pthread_setname_np(pthread_self(), "ReqReceiver");
mRequestReceiver->run();
});
mRequestReceiver->start();
}

void App::startNetAdminServer() {
Expand Down Expand Up @@ -275,7 +277,7 @@ void App::shutdown() {

// shutdown all threads
mCommandQueue.shutdown();
mRequestReceiver->shutdown();
mRequestReceiver->stop();
mNetAdminServer->shutdown();
mCommandProcessLoop->shutdown();
mEventApplyLoop->shutdown();
Expand Down
1 change: 1 addition & 0 deletions src/app_demo/should_be_generated/app/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class App final {
void startPostServerLoop();

private:
std::shared_ptr<app::AppInfo> mAppInfo;
DeploymentMode mDeploymentMode = DeploymentMode::Standalone;

/// the factory to create container from a memory pool
Expand Down
106 changes: 0 additions & 106 deletions src/app_demo/should_be_generated/app/RequestCallData.cpp

This file was deleted.

Loading