Skip to content

Instantly share code, notes, and snippets.

@lrita
Created Jan 29, 2019
Embed
What would you like to do?
分布式存储系统可靠性-注解
package main
import (
"fmt"
"math"
)
func C(N, K int64) float64 {
a, b := 1.0, 1.0
if N == 0 {
panic(fmt.Sprint("N ", N))
}
if K == 0 {
return 1.0
}
for i := N; i > N-K; i-- {
a *= float64(i)
}
for i := int64(1); i <= K; i++ {
b = b * float64(i)
}
return a / b
}
// 在N块盘的系统内,坏K块盘时,命中copyset的概率
func pK(N, K, R, S int64) float64 {
var (
sum float64
n = K / R
)
for i := int64(1); i <= n; i++ {
sum += C(S, i) * C(N-i*R, K-i*R)
}
return sum / C(N, K)
}
// returns n!
func Factorial(n int64) float64 {
sum := 1.0
for i := int64(1); i < n+1; i++ {
sum *= float64(i)
}
return sum
}
// 计算泊松分布概率
func Poisson(lambda, T float64, K int64) float64 {
lambda *= T
return math.Pow(lambda, float64(K)) *
math.Pow(math.E, -1*lambda) / Factorial(K)
}
// 将N块磁盘的年故障率转换为每小时内的故障概率
func ARF2Lambda(N int64, ARF float64) float64 {
return float64(N) * ARF / (365.0 * 24.0)
}
// P(N(T)=K) : N 个磁盘存储系统中T时间同时损坏K块盘的概率,年故障率ARF
func DiskFailRateOfK(N, T, K int64, ARF float64) float64 {
return Poisson(ARF2Lambda(N, ARF), float64(T), K)
}
// 副本数R的N 个磁盘存储系统中T时间内造成数据丢失的概率
// 只统计 [R,2R-1]个副本情况下的丢失数据概率(大于R个情况下,在一遍情况下对结果影响比较小)
func LossDataInT(N, S, R, T int64, ARF float64) float64 {
sum := 0.0
for K := R; K <= R*2; K++ {
sum += DiskFailRateOfK(N, T, K, ARF) * pK(N, K, R, S)
}
return sum
}
// 恢复故障时间为T时(单位:小时),一年内丢数据的概率
func LossDataRate(N, S, R, T int64, ARF float64) float64 {
return 1.0 - math.Pow(1.0-LossDataInT(N, S, R, T, ARF), 365.0*24.0/float64(T))
}
func main() {
println(LossDataInT(7200, 1376256, 3, 1, 0.04)) // -> 1.310834e-010 一小时内丢数据的概率
println(LossDataRate(7200, 1376256, 3, 1, 0.04)) // -> 1.148291e-006 一年内丢数据的概率
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment