Skip to content

Commit

Permalink
Use a directory that the vespa user has control over.
Browse files Browse the repository at this point in the history
Also add vespa version to the assert key file name.
  • Loading branch information
baldersheim committed Dec 7, 2018
1 parent 66b2121 commit 5ebc571
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 35 deletions.
19 changes: 14 additions & 5 deletions staging_vespalib/src/tests/assert/assert_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/slaveproc.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/fastos/file.h>
#include <vespa/vespalib/util/assert.h>
#include <vespa/vespalib/io/fileutil.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vespa/defaults.h>

using namespace vespalib;

TEST("that it borks the first time.") {
vespalib::string assertName = make_string("tmp/myassert.assert.%s", vespa::Defaults::vespaUser());
FastOS_File::EmptyAndRemoveDirectory("tmp");
ASSERT_EQUAL(0, mkdir("tmp", 0755));
std::string assertName;
const char * assertDir = "var/db/vespa/tmp";
vespalib::rmdir("var", true);
ASSERT_TRUE(vespalib::mkdir(assertDir, true));
{
SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000");
proc.wait();
ASSERT_EQUAL(proc.getExitCode() & 0x7f, 6);
}
{
SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000");
proc.readLine(assertName);
proc.wait();
ASSERT_EQUAL(proc.getExitCode() & 0x7f, 0);
}
ASSERT_EQUAL(0, unlink(assertName.c_str()));
ASSERT_EQUAL(0, rmdir("tmp"));
{
SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000");
proc.wait();
ASSERT_EQUAL(proc.getExitCode() & 0x7f, 6);
}
ASSERT_EQUAL(0, unlink(assertName.c_str()));
ASSERT_TRUE(vespalib::rmdir("var", true));
}

TEST_MAIN_WITH_PROCESS_PROXY() { TEST_RUN_ALL(); }
9 changes: 9 additions & 0 deletions staging_vespalib/src/tests/assert/asserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <vespa/vespalib/util/assert.h>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <string>

int main(int argc, char *argv[]) {
assert(argc == 3);
Expand All @@ -11,6 +13,13 @@ int main(int argc, char *argv[]) {
ASSERT_ONCE_OR_LOG(true, assertKey, 100);
ASSERT_ONCE_OR_LOG(false, assertKey, 100);
}
std::string filename = vespalib::assert::getAssertLogFileName(assertKey);
std::ifstream is(filename.c_str());
assert(is);
std::string line;
std::getline(is, line);
printf("%s\n", filename.c_str());
assert(line.find(assertKey) != std::string::npos);
assert(assertCount == vespalib::assert::getNumAsserts(assertKey));
return 0;
}
63 changes: 39 additions & 24 deletions staging_vespalib/src/vespa/vespalib/util/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "assert.h"
#include <vespa/defaults.h>
#include <vespa/vespalib/util/backtrace.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/component/vtag.h>
#include <fstream>
#include <map>
#include <mutex>
Expand All @@ -21,41 +23,54 @@ std::map<std::string, size_t> _G_assertMap;

}

size_t getNumAsserts(const char *key)
size_t
getNumAsserts(const char *key)
{
std::lock_guard guard(_G_lock);
return _G_assertMap[key];
}

void assertOnceOrLog(const char *expr, const char *key, size_t freq)
vespalib::string
getAssertLogFileName(const char *key)
{
std::string relativePath("tmp/");
relativePath += key;
relativePath += ".assert.";
relativePath += vespa::Defaults::vespaUser();
std::string rememberAssert = vespa::Defaults::underVespaHome(relativePath.c_str());
std::ifstream prevAssertFile(rememberAssert.c_str());
if (prevAssertFile) {
size_t count(0);
{
std::lock_guard guard(_G_lock);
count = _G_assertMap[key]++;
}
vespalib::string relative = make_string("var/db/vespa/tmp/%s.%s.assert", key, Vtag::currentVersion.toString().c_str());
return vespa::Defaults::underVespaHome(relative.c_str());
}

void
assertOnceOrLog(const char *expr, const char *key, size_t freq)
{
size_t count(0);
{
std::lock_guard guard(_G_lock);
count = _G_assertMap[key]++;
}
if (count) {
if ((count % freq) == 0) {
LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s",
expr, key, count+1, vespalib::getStackTrace(0).c_str());
expr, key, count+1, vespalib::getStackTrace(0).c_str());
}
} else {
{
LOG(error, "assert(%s) named '%s' failed first time. Stacktrace = %s",
expr, key, vespalib::getStackTrace(0).c_str());
std::ofstream assertStream(rememberAssert.c_str());
std::chrono::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
assertStream << std::put_time(std::gmtime(&now_c), "%F %T") << " assert(" << expr << ") failed" << std::endl;
assertStream.close();
std::string rememberAssert = getAssertLogFileName(key);
std::ifstream prevAssertFile(rememberAssert.c_str());
if (prevAssertFile) {
if ((count % freq) == 0) {
LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s",
expr, key, count + 1, vespalib::getStackTrace(0).c_str());
}
} else {
{
LOG(error, "assert(%s) named '%s' failed first time. Stacktrace = %s",
expr, key, vespalib::getStackTrace(0).c_str());
std::ofstream assertStream(rememberAssert.c_str());
std::chrono::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
assertStream << std::put_time(std::gmtime(&now_c), "%F %T") << " assert(" << expr
<< ") named " << key << " failed" << std::endl;
assertStream.close();
}
abort();
}
abort();
}
}

Expand Down
9 changes: 8 additions & 1 deletion staging_vespalib/src/vespa/vespalib/util/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma once

#include <cstddef>
#include <vespa/vespalib/stllike/string.h>

namespace vespalib::assert {

Expand All @@ -13,6 +13,13 @@ namespace vespalib::assert {
*/
size_t getNumAsserts(const char *key);

/**
* Get the filename that will be used for remembering asserts.
* @param key
* @return
*/
vespalib::string getAssertLogFileName(const char *key);

/**
* If there is no record on file that this assert has failed, it will be recorded and aborted.
* However if there is a record of it, it will merely be logged the first and then every #freq time.
Expand Down
3 changes: 1 addition & 2 deletions vespalib/src/vespa/vespalib/component/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Version::Version(int major, int minor, int micro, const string & qualifier)

Version::Version(const Version &) = default;
Version & Version::operator = (const Version &) = default;
Version::~Version() { }
Version::~Version() = default;

void
Version::initialize()
Expand Down Expand Up @@ -151,5 +151,4 @@ Version::compareTo(const Version& other) const
return getQualifier().compare(other.getQualifier());
}


} // namespace vespalib
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ VersionSpecification::VersionSpecification(int major, int minor, int micro, cons

VersionSpecification::VersionSpecification(const VersionSpecification &) = default;

VersionSpecification::~VersionSpecification() { }
VersionSpecification::~VersionSpecification() = default;

void
VersionSpecification::initialize()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/component/version.h>
#include "version.h"

namespace vespalib {

Expand Down
2 changes: 1 addition & 1 deletion vespalib/src/vespa/vespalib/component/vtag.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <cstdio>
#include "vtag.h"
#include <cstdio>

#ifndef V_TAG
#define V_TAG "NOTAG"
Expand Down

0 comments on commit 5ebc571

Please sign in to comment.