聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

7次阅读

共计 2999 个字符,预计需要花费 8 分钟才能阅读完成。

本文主要研究一下 Elasticsearch 的 ExponentiallyWeightedMovingAverage

ExponentiallyWeightedMovingAverage

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverage.java

public class ExponentiallyWeightedMovingAverage {

    private final double alpha;
    private final AtomicLong averageBits;

    /**
     * Create a new EWMA with a given {@code alpha} and {@code initialAvg}. A smaller alpha means
     * that new data points will have less weight, where a high alpha means older data points will
     * have a lower influence.
     */
    public ExponentiallyWeightedMovingAverage(double alpha, double initialAvg) {if (alpha < 0 || alpha > 1) {throw new IllegalArgumentException("alpha must be greater or equal to 0 and less than or equal to 1");
        }
        this.alpha = alpha;
        this.averageBits = new AtomicLong(Double.doubleToLongBits(initialAvg));
    }

    public double getAverage() {return Double.longBitsToDouble(this.averageBits.get());
    }

    public void addValue(double newValue) {
        boolean successful = false;
        do {final long currentBits = this.averageBits.get();
            final double currentAvg = getAverage();
            final double newAvg = (alpha * newValue) + ((1 - alpha) * currentAvg);
            final long newBits = Double.doubleToLongBits(newAvg);
            successful = averageBits.compareAndSet(currentBits, newBits);
        } while (successful == false);
    }
}
  • ExponentiallyWeightedMovingAverage 实现了 EWMA,它是线程安全的;其构造器要求输入 alpha 及 initialAvg;alpha 越大表示新数据权重越大旧数据权重越小
  • getAverage 返回的是 averageBits 的值,不过它存储的是 double 的 bit 形式,返回的时候使用 Double.longBitsToDouble 转换会 double
  • addValue 方法使用 (alpha * newValue) + ((1 - alpha) * currentAvg) 计算新值,然后使用 averageBits.compareAndSet 方法来实现原子更新

实例

elasticsearch-7.0.1/server/src/test/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverageTests.java

public class ExponentiallyWeightedMovingAverageTests extends ESTestCase {public void testEWMA() {final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10);
        ewma.addValue(12);
        assertThat(ewma.getAverage(), equalTo(11.0));
        ewma.addValue(10);
        ewma.addValue(15);
        ewma.addValue(13);
        assertThat(ewma.getAverage(), equalTo(12.875));
    }

    public void testInvalidAlpha() {IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new ExponentiallyWeightedMovingAverage(-0.5, 10));
        assertThat(ex.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1"));

        ex = expectThrows(IllegalArgumentException.class, () -> new ExponentiallyWeightedMovingAverage(1.5, 10));
        assertThat(ex.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1"));
    }

    public void testConvergingToValue() {final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10000);
        for (int i = 0; i < 100000; i++) {ewma.addValue(1);
        }
        assertThat(ewma.getAverage(), lessThan(2.0));
    }
}
  • testEWMA 方法测试算法的计算逻辑;testInvalidAlpha 测试 alpha 参数的校验;testConvergingToValue 则测试 ewma 值的收敛

小结

  • ExponentiallyWeightedMovingAverage 实现了 EWMA,它是线程安全的;其构造器要求输入 alpha 及 initialAvg;alpha 越大表示新数据权重越大旧数据权重越小
  • getAverage 返回的是 averageBits 的值,不过它存储的是 double 的 bit 形式,返回的时候使用 Double.longBitsToDouble 转换会 double
  • addValue 方法使用 (alpha * newValue) + ((1 - alpha) * currentAvg) 计算新值,然后使用 averageBits.compareAndSet 方法来实现原子更新

doc

  • 聊聊 rsocket load balancer 的 Ewma
  • ExponentiallyWeightedMovingAverage
  • ExponentiallyWeightedMovingAverageTests

正文完
 0