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 21 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/stddev.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 math {
Copy link
Member

Choose a reason for hiding this comment

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

那这里还不如改为dsn::utils 名字空间,和其他工具函数保持一致。
另外文件名改为math.h/math.cpp,这样以后有其他数学相关的函数也都放在这里。


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

} // namespace math
} // 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/stddev.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/stddev.h>

namespace dsn {
namespace math {

double mean_stddev(const std::vector<unsigned> &result_set, bool partial_sample)
{
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;
return stddev;
}

} // namespace math
} // 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