Skip to content

Commit

Permalink
TIG-3662 Create HTTP actor (mongodb#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardsamuels authored Feb 9, 2022
1 parent 18f8f32 commit 89ce68b
Show file tree
Hide file tree
Showing 374 changed files with 73,617 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cmake/Findcppcodec.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message(${CMAKE_CURRENT_LIST_DIR})
find_path(cppcodec_INCLUDE_DIRS "cppcodec/base32_crockford.hpp"
PATHS "${CMAKE_CURRENT_LIST_DIR}/../src/third_party/cppcodec" # We allow taking the embedded cppcodec
)
mark_as_advanced(cppcodec_INCLUDE_DIRS)

find_package_handle_standard_args(cppcodec
REQUIRED_VARS cppcodec_INCLUDE_DIRS
)

if(cppcodec_FOUND)
add_library(cppcodec::cppcodec IMPORTED INTERFACE)
target_include_directories(cppcodec::cppcodec INTERFACE "${cppcodec_INCLUDE_DIRS}")
endif()
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ add_subdirectory(value_generators)
add_subdirectory(workloads)

# This makes the project importable from the build directory
export(TARGETS value_generators metrics gennylib loki cast_core poplarlib
export(TARGETS simple-beast-client value_generators metrics gennylib loki cast_core poplarlib
FILE GennyLibraryConfig.cmake)

# Below generates a cmake module so if you make install from this
Expand Down
4 changes: 4 additions & 0 deletions src/cast_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@

project(cast_core VERSION 0.0.1 LANGUAGES CXX)

find_package(cppcodec)

CreateGennyTargets(
NAME cast_core
TYPE SHARED
DEPENDS
gennylib
cppcodec::cppcodec
simple-beast-client
TEST_DEPENDS testlib
)
54 changes: 54 additions & 0 deletions src/cast_core/include/cast_core/actors/SampleHttpClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022-present MongoDB 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
//
// http://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.

#ifndef HEADER_F39B7C7C_1002_4060_AAEE_F379AEE7AB3C_INCLUDED
#define HEADER_F39B7C7C_1002_4060_AAEE_F379AEE7AB3C_INCLUDED

#include <string_view>

#include <mongocxx/pool.hpp>

#include <gennylib/Actor.hpp>
#include <gennylib/PhaseLoop.hpp>
#include <gennylib/context.hpp>

#include <metrics/metrics.hpp>

namespace genny::actor {

class SampleHttpClient : public Actor {

public:
explicit SampleHttpClient(ActorContext& context);
~SampleHttpClient() = default;

void run() override;

static std::string_view defaultName() {
return "SampleHttpClient";
}

private:
mongocxx::pool::entry _client;

genny::metrics::Operation _totalRequests;

/** @private */
struct PhaseConfig;
PhaseLoop<PhaseConfig> _loop;
};

} // namespace genny::actor

#endif // HEADER_F39B7C7C_1002_4060_AAEE_F379AEE7AB3C_INCLUDED
117 changes: 117 additions & 0 deletions src/cast_core/src/SampleHttpClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2022-present MongoDB 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
//
// http://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 <cast_core/actors/SampleHttpClient.hpp>

#include <memory>

#include <yaml-cpp/yaml.h>

#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/collection.hpp>
#include <mongocxx/database.hpp>

#include <boost/log/trivial.hpp>
#include <boost/throw_exception.hpp>

#include <gennylib/Cast.hpp>
#include <gennylib/MongoException.hpp>
#include <gennylib/context.hpp>

#include <simple-beast-client/httpclient.hpp>
#include <value_generators/DocumentGenerator.hpp>


namespace genny::actor {

struct SampleHttpClient::PhaseConfig {
mongocxx::database database;

mongocxx::collection collection;

DocumentGenerator documentExpr;

PhaseConfig(PhaseContext& phaseContext, mongocxx::database&& db, ActorId id)
: database{db},
collection{database[phaseContext["Collection"].to<std::string>()]},
documentExpr{phaseContext["Document"].to<DocumentGenerator>(phaseContext, id)} {}
};


void SampleHttpClient::run() {
for (auto&& config : _loop) {
for (const auto&& _ : config) {
auto document = config->documentExpr();

auto requests = _totalRequests.start();

BOOST_LOG_TRIVIAL(debug)
<< " SampleHttpClient Inserting " << bsoncxx::to_json(document.view());

try {
boost::asio::io_context ioContext;
auto client = std::make_shared<simple_http::get_client>(
ioContext,
[](simple_http::empty_body_request& /*req*/,
simple_http::string_body_response& resp) {
// noop for successful HTTP
});

client->setFailHandler(
[&requests](const simple_http::empty_body_request& /*req*/,
const simple_http::string_body_response& /*resp*/,
simple_http::fail_reason fr,
boost::string_view message) {
// TODO TIG-3843: this will always be triggered on macOS
// with Failure code 2, Message: Error resolving
// target: Host not found (authoritative)
BOOST_LOG_TRIVIAL(warning) << "Failure code: " << fr << std::endl;
BOOST_LOG_TRIVIAL(warning) << "Message: " << message << std::endl;
requests.failure();
});

// Run the GET request to httpbin.org
client->get(simple_http::url{
"https://user:[email protected]/digest-auth/auth/user/passwd/MD5/never"});

ioContext.run(); // blocks until requests are complete.

requests.success();
} catch (mongocxx::operation_exception& e) {
requests.failure();
//
// MongoException lets you include a "causing" bson document in the
// exception message for help debugging.
//
BOOST_THROW_EXCEPTION(MongoException(e, document.view()));
} catch (...) {
requests.failure();
throw std::current_exception();
}
}
}
}

SampleHttpClient::SampleHttpClient(genny::ActorContext& context)
: Actor{context},
_totalRequests{context.operation("Insert", SampleHttpClient::id())},
_client{context.client()},
_loop{context, (*_client)[context["Database"].to<std::string>()], SampleHttpClient::id()} {}

namespace {
auto registerSampleHttpClient = Cast::registerDefault<SampleHttpClient>();
} // namespace
} // namespace genny::actor
5 changes: 4 additions & 1 deletion src/third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ add_subdirectory(poplar)

# <Catch2>
add_subdirectory(catch2)
# </Catch2>
# </Catch2>

add_subdirectory(cppcodec)
add_subdirectory(simple-beast-client)
3 changes: 3 additions & 0 deletions src/third_party/cppcodec/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CMakeLists.txt.user
.*.swp
.DS_Store
3 changes: 3 additions & 0 deletions src/third_party/cppcodec/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "test/catch"]
path = test/catch
url = https://github.com/catchorg/Catch2.git
143 changes: 143 additions & 0 deletions src/third_party/cppcodec/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
dist: trusty
language: cpp

# sources list: https://github.com/travis-ci/apt-source-whitelist/blob/master/ubuntu.json,
# packages list: https://github.com/travis-ci/apt-package-whitelist/blob/master/ubuntu-trusty

matrix:
include:
# Newest/oldest GCC
- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-7' ]
env: BUILD_TYPE=MinSizeRel GXX=7 CXX_STD=17

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-7' ]
env: BUILD_TYPE=Release GXX=7 CXX_STD=17

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-4.8' ]
env: BUILD_TYPE=Release GXX=4.8

# Newest/oldest clang
- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-6.0' ]
packages: [ 'clang-6.0', 'libstdc++-7-dev', 'libstdc++6' ] # C++17 support in libstd++
env: BUILD_TYPE=MinSizeRel CLANGXX=6.0 CXX_STD=17

- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-6.0' ]
packages: [ 'clang-6.0', 'libstdc++-7-dev', 'libstdc++6' ] # C++17 support in libstd++
env: BUILD_TYPE=Release CLANGXX=6.0 CXX_STD=17

- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6' ]
packages: [ 'clang-3.6' ]
env: BUILD_TYPE=Release CLANGXX=3.6

# Other compiler versions
- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-7' ]
env: BUILD_TYPE=Debug GXX=7

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-6' ]
env: BUILD_TYPE=MinSizeRel GXX=6 CXX_STD=14

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-5' ]
env: BUILD_TYPE=Release GXX=5

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-4.9' ]
env: BUILD_TYPE=MinSizeRel GXX=4.9

- os: linux
compiler: gcc
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-4.8' ]
env: BUILD_TYPE=Debug GXX=4.8

- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0' ]
packages: [ 'clang-5.0' ]
env: BUILD_TYPE=Release CLANGXX=5.0

- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0' ]
packages: [ 'clang-4.0' ]
env: BUILD_TYPE=MinSizeRel CLANGXX=4.0

- os: linux
compiler: clang
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6' ]
packages: [ 'clang-3.6' ]
env: BUILD_TYPE=Debug CLANGXX=3.6 CXX_STD=14

# container-based builds
sudo: false

before_install:
- env
- export SRC_DIR="`pwd`"
- if [ "$CXX" = "g++" ]; then export CXX="g++-$GXX" CC="gcc-$GXX"; fi
- if [ "$CXX" = "clang++" ]; then export CXX="clang++-$CLANGXX" CC="clang-$CLANGXX"; fi

script:
- $CXX --version
- cmake --version
- mkdir "$TRAVIS_BUILD_DIR/build"
- cd "$TRAVIS_BUILD_DIR/build"
- pwd
- CXX_STD_ARG=""; if [ ! -z "${CXX_STD}" ]; then CXX_STD_ARG="-DCMAKE_CXX_STANDARD=${CXX_STD}"; fi
- cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} ${CXX_STD_ARG} "$SRC_DIR"
- make -j2 # cores according to https://docs.travis-ci.com/user/reference/overview/
- CTEST_OUTPUT_ON_FAILURE=1 make test
- test/benchmark_cppcodec
Loading

0 comments on commit 89ce68b

Please sign in to comment.