Skip to content

Commit

Permalink
Improve memory watermark detection (#2885)
Browse files Browse the repository at this point in the history
* Add MemoryUtils to compute available memory

* Improve file reader

* Benchmark fstream and popen

* Replace Memory with MemoryUtils

* Add sysinfo bench

* Need not to close fstream

* Cleanup staled codes

* Use bg thread to check memory watermark in query engine

* Replace stringPrintf with sformat

* Refactor storage detail

* Reuse env of job

* Fix insert test

* Refactor bg thread for updating memory watermark

* check memory watermark in executor start

* format Makefile

* Use literal string

* Update code coverage badge

* Cleanup Makefile

* Check memory in iterator and executor

* Fix compile error

* Cleanup Makefile

* Fix test and todo

* Fix test

* Fix tck tests

* Fix containerized test

* Refactor nebula test script

* Optimize check

* Try to fix workflow

* Restore volumes of container

* Fix compile error

* Fix compile

* Address comments

* Fix error

* Fix ci
  • Loading branch information
yixinglu authored Oct 8, 2021
1 parent 9462d35 commit e642c05
Show file tree
Hide file tree
Showing 63 changed files with 584 additions and 312 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jobs:
timeout-minutes: 20
- name: Setup cluster
run: |
make ENABLE_SSL=true CA_SIGNED=true up
make CONTAINERIZED=true ENABLE_SSL=true CA_SIGNED=true up
working-directory: tests/
timeout-minutes: 2
- name: Pytest
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ jobs:
exclude:
- os: centos7
compiler: clang-10
env:
CCACHE_DIR: /tmp/ccache/nebula/${{ matrix.os }}-${{ matrix.compiler }}
CCACHE_MAXSIZE: 8G
container:
image: vesoft/nebula-dev:${{ matrix.os }}
env:
CCACHE_DIR: /tmp/ccache/nebula/${{ matrix.os }}-${{ matrix.compiler }}
CCACHE_MAXSIZE: 8G
volumes:
- /tmp/ccache/nebula/${{ matrix.os }}-${{ matrix.compiler }}:/tmp/ccache/nebula/${{ matrix.os }}-${{ matrix.compiler }}
options: --cap-add=SYS_PTRACE
Expand Down Expand Up @@ -132,17 +132,17 @@ jobs:
case ${{ matrix.os }} in
centos7)
# normal cluster
make up
make CONTAINERIZED=true up
;;
ubuntu2004)
# ssl cluster
make ENABLE_SSL=true CA_SIGNED=true up
make CONTAINERIZED=true ENABLE_SSL=true CA_SIGNED=true up
;;
esac
;;
clang-*)
# graph ssl only cluster
make ENABLE_SSL=false ENABLE_GRAPH_SSL=true up
make CONTAINERIZED=true ENABLE_SSL=false ENABLE_GRAPH_SSL=true up
;;
esac
working-directory: tests/
Expand Down
20 changes: 14 additions & 6 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
</p>

<p align="center">
<a href="https://user-images.githubusercontent.com/38887077/67449282-4362b300-f64c-11e9-878f-7efc373e5e55.jpg"><img src="https://img.shields.io/badge/WeChat-%E5%BE%AE%E4%BF%A1-brightgreen" alt="WeiXin"></a>
<a href="https://www.zhihu.com/org/nebulagraph/activities"><img src="https://img.shields.io/badge/Zhihu-%E7%9F%A5%E4%B9%8E-blue" alt="Zhihu"></a>
<a href="https://segmentfault.com/t/nebula"><img src="https://img.shields.io/badge/SegmentFault-%E6%80%9D%E5%90%A6-green" alt="SegmentFault"></a>
<a href="https://weibo.com/p/1006067122684542/home?from=page_100606&mod=TAB#place"><img src="https://img.shields.io/badge/Weibo-%E5%BE%AE%E5%8D%9A-red" alt="Sina Weibo"></a>
<a href="http://githubbadges.com/star.svg?user=vesoft-inc&repo=nebula&style=default">
<a href="https://user-images.githubusercontent.com/38887077/67449282-4362b300-f64c-11e9-878f-7efc373e5e55.jpg">
<img src="https://img.shields.io/badge/WeChat-%E5%BE%AE%E4%BF%A1-brightgreen" alt="WeiXin">
</a>
<a href="https://www.zhihu.com/org/nebulagraph/activities">
<img src="https://img.shields.io/badge/Zhihu-%E7%9F%A5%E4%B9%8E-blue" alt="Zhihu">
</a>
<a href="https://segmentfault.com/t/nebula">
<img src="https://img.shields.io/badge/SegmentFault-%E6%80%9D%E5%90%A6-green" alt="SegmentFault">
</a>
<a href="https://weibo.com/p/1006067122684542/home?from=page_100606&mod=TAB#place">
<img src="https://img.shields.io/badge/Weibo-%E5%BE%AE%E5%8D%9A-red" alt="Sina Weibo">
</a>
<a href="https://github.com/vesoft-inc/nebula/stargazers">
<img src="http://githubbadges.com/star.svg?user=vesoft-inc&repo=nebula&style=default" alt="nebula star"/>
</a>
<a href="http://githubbadges.com/fork.svg?user=vesoft-inc&repo=nebula&style=default">
<a href="https://github.com/vesoft-inc/nebula/network/members">
<img src="http://githubbadges.com/fork.svg?user=vesoft-inc&repo=nebula&style=default" alt="nebula fork"/>
</a>
</p>
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
<br>A distributed, scalable, lightning-fast graph database<br>
</p>
<p align="center">
<a href="https://app.codecov.io/gh/vesoft-inc/nebula">
<img src="https://codecov.io/github/vesoft-inc/nebula/coverage.svg?branch=master" alt="code coverage"/>
</a>
<a href="https://github.com/vesoft-inc/nebula/actions?workflow=nightly">
<img src="https://github.com/vesoft-inc/nebula/workflows/nightly/badge.svg" alt="nightly build"/>
</a>
<a href="http://githubbadges.com/star.svg?user=vesoft-inc&repo=nebula&style=default">
<a href="https://github.com/vesoft-inc/nebula/stargazers">
<img src="http://githubbadges.com/star.svg?user=vesoft-inc&repo=nebula&style=default" alt="nebula star"/>
</a>
<a href="http://githubbadges.com/fork.svg?user=vesoft-inc&repo=nebula&style=default">
<a href="https://github.com/vesoft-inc/nebula/network/members">
<img src="http://githubbadges.com/fork.svg?user=vesoft-inc&repo=nebula&style=default" alt="nebula fork"/>
</a>
<br>
Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN rpm -ivh *.rpm \

EXPOSE 9669 19669 19670

ENTRYPOINT ["/usr/local/nebula/bin/nebula-graphd", "--flagfile=/usr/local/nebula/etc/nebula-graphd.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-graphd", "--flagfile=/usr/local/nebula/etc/nebula-graphd.conf", "--daemonize=false", "--containerized=true"]

FROM centos:7 as metad

Expand All @@ -35,7 +35,7 @@ RUN rpm -ivh *.rpm \

EXPOSE 9559 9560 19559 19560

ENTRYPOINT ["/usr/local/nebula/bin/nebula-metad", "--flagfile=/usr/local/nebula/etc/nebula-metad.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-metad", "--flagfile=/usr/local/nebula/etc/nebula-metad.conf", "--daemonize=false", "--containerized=true"]

FROM centos:7 as storaged

Expand All @@ -50,7 +50,7 @@ RUN rpm -ivh *.rpm \

EXPOSE 9777 9778 9779 9780 19779 19780

ENTRYPOINT ["/usr/local/nebula/bin/nebula-storaged", "--flagfile=/usr/local/nebula/etc/nebula-storaged.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-storaged", "--flagfile=/usr/local/nebula/etc/nebula-storaged.conf", "--daemonize=false", "--containerized=true"]

FROM centos:7 as tools

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.graphd
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUN rpm -ivh *.rpm \

EXPOSE 9669 19669 19670

ENTRYPOINT ["/usr/local/nebula/bin/nebula-graphd", "--flagfile=/usr/local/nebula/etc/nebula-graphd.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-graphd", "--flagfile=/usr/local/nebula/etc/nebula-graphd.conf", "--daemonize=false", "--containerized=true"]
2 changes: 1 addition & 1 deletion docker/Dockerfile.metad
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUN rpm -ivh *.rpm \

EXPOSE 9559 9560 19559 19560

ENTRYPOINT ["/usr/local/nebula/bin/nebula-metad", "--flagfile=/usr/local/nebula/etc/nebula-metad.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-metad", "--flagfile=/usr/local/nebula/etc/nebula-metad.conf", "--daemonize=false", "--containerized=true"]
2 changes: 1 addition & 1 deletion docker/Dockerfile.storaged
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUN rpm -ivh *.rpm \

EXPOSE 9777 9778 9779 9780 19779 19780

ENTRYPOINT ["/usr/local/nebula/bin/nebula-storaged", "--flagfile=/usr/local/nebula/etc/nebula-storaged.conf", "--daemonize=false"]
ENTRYPOINT ["/usr/local/nebula/bin/nebula-storaged", "--flagfile=/usr/local/nebula/etc/nebula-storaged.conf", "--daemonize=false", "--containerized=true"]
1 change: 1 addition & 0 deletions resources/gflags.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"custom_filter_interval_secs",
"accept_partial_success",
"system_memory_high_watermark_ratio",
"num_rows_to_check_memory",
"session_idle_timeout_secs",
"session_reclaim_interval_secs",
"max_allowed_connections",
Expand Down
2 changes: 1 addition & 1 deletion src/clients/storage/StorageClientBase-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ folly::SemiFuture<StorageRpcResponse<Response>> StorageClientBase<ClientType>::c
// then-callback will be executed on the same IO thread
.via(evb)
.then([this, context, host, spaceId, start](folly::Try<Response>&& val) {
auto& r = context->findRequest(host);
if (val.hasException()) {
auto& r = context->findRequest(host);
LOG(ERROR) << "Request to " << host << " failed: " << val.exception().what();
auto parts = getReqPartsId(r);
context->resp.appendFailedParts(parts, nebula::cpp2::ErrorCode::E_RPC_FAILURE);
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ nebula_add_subdirectory(plugin)
nebula_add_subdirectory(utils)
nebula_add_subdirectory(ssl)
nebula_add_subdirectory(geo)
nebula_add_subdirectory(memory)
1 change: 0 additions & 1 deletion src/common/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ nebula_add_library(
SignalHandler.cpp
SlowOpTracker.cpp
StringValue.cpp
Memory.cpp
${gdb_debug_script}
)

Expand Down
31 changes: 0 additions & 31 deletions src/common/base/Memory.cpp

This file was deleted.

44 changes: 0 additions & 44 deletions src/common/base/Memory.h

This file was deleted.

7 changes: 0 additions & 7 deletions src/common/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ nebula_add_test(
LIBRARIES gtest gtest_main
)

nebula_add_test(
NAME memory_test
SOURCES MemoryTest.cpp
OBJECTS $<TARGET_OBJECTS:base_obj>
LIBRARIES gtest gtest_main
)

nebula_add_executable(
NAME range_vs_transform_bm
SOURCES RangeVsTransformBenchmark.cpp
Expand Down
29 changes: 0 additions & 29 deletions src/common/base/test/MemoryTest.cpp

This file was deleted.

1 change: 1 addition & 0 deletions src/common/geo/io/wkt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(WKT_PARSER_TEST_LIBS
$<TARGET_OBJECTS:idgenerator_obj>
$<TARGET_OBJECTS:graph_obj>
$<TARGET_OBJECTS:ssl_obj>
$<TARGET_OBJECTS:memory_obj>
)

nebula_add_test(
Expand Down
12 changes: 12 additions & 0 deletions src/common/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License,
# attached with Common Clause Condition 1.0, found in the LICENSES directory.


nebula_add_library(
memory_obj OBJECT
MemoryUtils.cpp
)

nebula_add_subdirectory(test)
83 changes: 83 additions & 0 deletions src/common/memory/MemoryUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "common/memory/MemoryUtils.h"

#include <folly/String.h>
#include <gflags/gflags.h>

#include <cstdio>
#include <fstream>
#include <regex>

#include "common/fs/FileUtils.h"

DEFINE_bool(containerized, false, "Whether run this process inside the docker container");
DEFINE_double(system_memory_high_watermark_ratio, 0.8, "high watermark ratio of system memory");

using nebula::fs::FileUtils;

namespace nebula {

static const std::regex reMemAvailable(R"(^Mem(Available|Total):\s+(\d+)\skB$)");
static const std::regex reTotalCache(R"(^total_(cache|inactive_file)\s+(\d+)$)");

std::atomic_bool MemoryUtils::kHitMemoryHighWatermark{false};

StatusOr<bool> MemoryUtils::hitsHighWatermark() {
double available = 0.0, total = 0.0;
if (FLAGS_containerized) {
FileUtils::FileLineIterator iter("/sys/fs/cgroup/memory/memory.stat", &reTotalCache);
uint64_t cacheSize = 0;
for (; iter.valid(); ++iter) {
auto& sm = iter.matched();
cacheSize += std::stoul(sm[2].str(), NULL);
}

auto limitStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.limit_in_bytes");
NG_RETURN_IF_ERROR(limitStatus);
uint64_t limitInBytes = std::move(limitStatus).value();

auto usageStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.usage_in_bytes");
NG_RETURN_IF_ERROR(usageStatus);
uint64_t usageInBytes = std::move(usageStatus).value();

total = static_cast<double>(limitInBytes);
available = static_cast<double>(limitInBytes - usageInBytes + cacheSize);
} else {
FileUtils::FileLineIterator iter("/proc/meminfo", &reMemAvailable);
std::vector<uint64_t> memorySize;
for (; iter.valid(); ++iter) {
auto& sm = iter.matched();
memorySize.emplace_back(std::stoul(sm[2].str(), NULL) << 10);
}
CHECK_EQ(memorySize.size(), 2U);
size_t i = 0, j = 1;
if (memorySize[0] < memorySize[1]) {
std::swap(i, j);
}
total = memorySize[i];
available = memorySize[j];
}

auto hits = (1 - available / total) > FLAGS_system_memory_high_watermark_ratio;
LOG_IF_EVERY_N(WARNING, hits, 100)
<< "Memory usage has hit the high watermark of system, available: " << available
<< " vs. total: " << total << " in bytes.";
return hits;
}

StatusOr<uint64_t> MemoryUtils::readSysContents(const std::string& path) {
std::ifstream ifs(path);
if (!ifs) {
return Status::Error("Could not open the file: %s", path.c_str());
}
uint64_t value = 0;
ifs >> value;
return value;
}

} // namespace nebula
Loading

0 comments on commit e642c05

Please sign in to comment.