/**
java
demo for phi failure detector
*/
import
java.util.ArrayDeque;
import
java.util.Iterator;
import
java.util.concurrent.locks.Lock;
import
java.util.concurrent.locks.ReentrantLock;
public
class
PhiAccrualFailureDetector {
private
static
final
int
sampleWindowSize
= 1000;
private
static
int
phiSuspectThreshold
= 8;
private
SamplingWindow simpleingWindow
= new
SamplingWindow(sampleWindowSize);
public
PhiAccrualFailureDetector() {
}
public
void
addSample() {
simpleingWindow.add(System.currentTimeMillis());
}
public
void
addSample(double
sample) {
simpleingWindow.add(sample);
}
public
void
interpret() {
double
phi = simpleingWindow.phi(System.currentTimeMillis());
System.out.println("PHI
= " + phi);
if
(phi > phiSuspectThreshold)
{
System.out.println("We
are assuming the moniored machine is down!");
}
else
{
System.out.println("We
are assuming the moniored machine is still running!");
}
}
/**
* @param
args
* the command line arguments
*/
public
static
void
main(String[] args) {
PhiAccrualFailureDetector
pafd = new
PhiAccrualFailureDetector();
//
first try with phi
< phiSuspectThreshold
for
(int
i = 0; i < 10; i++) {
pafd.addSample();
try
{
Thread.sleep(10L);
}
catch
(InterruptedException ex) {
//
no op
}
}
try
{
Thread.sleep(500L);
}
catch
(InterruptedException ex) {
//
no op
}
System.out.println(pafd.simpleingWindow.toString());
pafd.interpret();
//
second try result phi
> phiSuspectThreshold
for
(int
i = 0; i < 10; i++) {
pafd.addSample();
try
{
Thread.sleep(10L);
}
catch
(InterruptedException ex) {
//
no op
}
}
try
{
Thread.sleep(1500L);
}
catch
(InterruptedException ex) {
//
no op
}
System.out.println(pafd.simpleingWindow.toString());
pafd.interpret();
}
static
class
SamplingWindow {
private
final
Lock lock
= new
ReentrantLock();
private
double
lastTimeStamp
= 0L;
private
StatisticDeque arrivalIntervals;
SamplingWindow(int
size) {
arrivalIntervals
= new
StatisticDeque(size);
}
void
add(double
value) {
lock.lock();
try
{
double
interval;
if
(lastTimeStamp
> 0L) {
interval
= (value - lastTimeStamp);
}
else
{
interval
= 1000 / 2;
}
lastTimeStamp
= value;
arrivalIntervals.add(interval);
}
finally
{
lock.unlock();
}
}
double
sum() {
lock.lock();
try
{
return
arrivalIntervals.sum();
}
finally
{
lock.unlock();
}
}
double
sumOfDeviations() {
lock.lock();
try
{
return
arrivalIntervals.sumOfDeviations();
}
finally
{
lock.unlock();
}
}
double
mean() {
lock.lock();
try
{
return
arrivalIntervals.mean();
}
finally
{
lock.unlock();
}
}
double
variance() {
lock.lock();
try
{
return
arrivalIntervals.variance();
}
finally
{
lock.unlock();
}
}
double
stdev() {
lock.lock();
try
{
return
arrivalIntervals.stdev();
}
finally
{
lock.unlock();
}
}
void
clear() {
lock.lock();
try
{
arrivalIntervals.clear();
}
finally
{
lock.unlock();
}
}
/**
*
* p = E ^ (-1
* (tnow
-
lastTimeStamp) / mean)
*/
double
p(double
t) {
double
mean = mean();
double
exponent = (-1) * (t) / mean;
return
Math.pow(Math.E,
exponent);
}
double
phi(long
tnow) {
int
size = arrivalIntervals.size();
double
log = 0d;
if
(size > 0) {
double
t = tnow - lastTimeStamp;
double
probability = p(t);
log
= (-1) * Math.log10(probability);
}
return
log;
}
@Override
public
String toString() {
StringBuilder
s = new
StringBuilder();
for
(Iterator<Double> it = arrivalIntervals.iterator();
it.hasNext();) {
s.append(it.next()).append("
");
}
return
s.toString();
}
}
static
class
StatisticDeque implements
Iterable<Double> {
private
final
int
size;
protected
final
ArrayDeque<Double> queue;
public
StatisticDeque(int
size) {
this.size
= size;
queue
= new
ArrayDeque<Double>(size);
}
public
Iterator<Double> iterator() {
return
queue.iterator();
}
public
int
size() {
return
queue.size();
}
public
void
clear() {
queue.clear();
}
public
void
add(double
o) {
if
(size ==
queue.size())
{
queue.remove();
}
queue.add(o);
}
public
double
sum() {
double
sum = 0D;
for
(Double interval : this)
{
sum
+= interval;
}
return
sum;
}
public
double
sumOfDeviations() {
double
sumOfDeviations = 0D;
double
mean = mean();
for
(Double interval : this)
{
double
d = interval - mean;
sumOfDeviations
+= d * d;
}
return
sumOfDeviations;
}
public
double
mean() {
return
sum() / size();
}
public
double
variance() {
return
sumOfDeviations() / size();
}
public
double
stdev() {
return
Math.sqrt(variance());
}
}
}
|
相关推荐
Cassandra技术详解 操作与测试报告 基于nosql实现集群
63页。参照了众多其他的cassandra的资料~
本文档详细介绍了Cassandra,DataStax的原理,以及在各种环境下的安装方法。
1.CAP定理理与Cassandra 1.1 Cassandra优势 2.Cassandra ⼀一致性实现 2.1 CAS 2.2 Quorum读写 2.3 不不⼀一致产⽣生原因 2.4 Hinted handoff 2.5 Read repair 2.6 Manual repair 3.Cassandra应⽤用场景 ...
2_1_Cassandra配置文件中相关配置项详解
了解一个软件的配置项的意义是使用这个软件的前提,这里介绍 Cassandra 的配置文件(storage-config.xml)中各个配置项的意义,这其中包含有很多配置参数,我们可以对其进行调整以达到理想的性能。
2019云栖大会Cassandra一致性详解-201909.pdf
Cassandra关键技术详解[整理].pdf
cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战cassandra 实战...
cassandra中文版权威指南,官网下载
模式灵活 :使用Cassandra,像文档存储,你不必提前解决记录中的字段。你可以在系统运行时随意的添加或移除字段。这是一个惊人的效率提升,特别是在大型部署上。 真正的可扩展性 :Cassandra是纯粹意义上的水平扩展...
1、cassandra的安装、维护使用 2、java操作cassandra实例 3、cql使用详解
The rising popularity of Apache Cassandra rests on its ability to handle very large data sets that include hundreds of terabytes -- and that's why this distributed database has been chosen by ...
DevCenter cassandra客户端 DevCenter cassandra客户端 DevCenter cassandra客户端
Cassandra(apache-cassandra-4.0.1-bin.tar.gz)是一套开源分布式NoSQL数据库系统。它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身...
docker-cassandra, 在 Docker的快速启动中,Cassandra Docker 中的Cassandra这个库提供了在 Docker 中运行Cassandra所需的一切,并为快速的容器启动而调整。为什么?虽然天真的Cassandra图像大约需要 30秒的时间,...
Cassandra(apache-cassandra-3.11.11-bin.tar.gz)是一套开源分布式NoSQL数据库系统。它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身...
Cassandra权威指南【中文版】
Cassandra文档
cassandra介绍