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

add cluster balance indicator #214

Merged
merged 28 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from 22 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
27 changes: 25 additions & 2 deletions include/dsn/dist/replication/replication_types.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions include/dsn/utility/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#pragma once

#include <vector>

namespace dsn {
namespace utils {

double mean_stddev(const std::vector<unsigned> &result_set, bool partial_sample);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数没有一点注释?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该还好吧,函数名一看就明白,公式也很简单

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsigned 要改成 uint32_t(当然你提前要 include )这是 cpplint 标准写法。


} // namespace utils
} // namespace dsn
2 changes: 2 additions & 0 deletions include/dsn/utility/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ char *trim_string(char *s);

// calculate the md5 checksum of buffer
std::string string_md5(const char *buffer, unsigned int length);

std::string to_string_with_precision(const double double_val, const int precision);
}
}
34 changes: 34 additions & 0 deletions src/core/core/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <numeric>
#include <algorithm>
#include <math.h>
#include <dsn/utility/math.h>

namespace dsn {
namespace utils {

double mean_stddev(const std::vector<unsigned> &result_set, bool partial_sample)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以加个assert(result_set.size() > 1)

double sum = std::accumulate(result_set.begin(), result_set.end(), 0.0);
double mean = sum / result_set.size();

double accum = 0.0;
std::for_each(result_set.begin(), result_set.end(), [&](const double d) {
accum += (d - mean) * (d - mean);
});

double stddev;
if (partial_sample)
stddev = sqrt(accum / (result_set.size() - 1));
else
stddev = sqrt(accum / (result_set.size()));

stddev = ((double)((int)((stddev + 0.005) * 100))) / 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只是为了做四舍五入吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

四舍五入保留小数点后两位

return stddev;
}

} // namespace utils
} // namespace dsn
8 changes: 8 additions & 0 deletions src/core/core/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,13 @@ std::string string_md5(const char *buffer, unsigned length)

return result;
}

std::string to_string_with_precision(const double double_val, const int precision)
Copy link
Contributor

@neverchanje neverchanje Jan 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 fmtlib,这个函数没必要写,http://fmtlib.net/latest/syntax.html
image

{
std::ostringstream out;
out.precision(precision);
out << std::fixed << double_val;
return out.str();
}
}
}
Loading