事件相关的矢量时钟比较

时间:2012-11-26 01:16:12

标签: logging event-log distributed-system vector-clock

我有一堆日志文件,包含事件日志及其中记录的矢量时钟。现在在比较任意两个事件的向量时钟时,取矢量时钟的每个分量的平方和的根,并使用结果与另一个的比较,然后得出结论较小的值先于另一个值?

1 个答案:

答案 0 :(得分:3)

不,如果有办法将其降低到一个值,我们将使用它而不是矢量!

要比较矢量时钟,您需要分段比较整个矢量。

class VectorClock {
    private long[] clocks;
    ...
    /**
     * This is before other iff both conditions are met:
     * - each process's clock is less-than-or-equal-to its own clock in other; and
     * - there is at least one process's clock which is strictly less-than its
     *   own clock in other
     */
    public boolean isBefore(VectorClock other) {
        boolean isBefore = false;
        for (int i = 0; i < clocks.length; i++) {
            int cmp = Long.compare(clocks[i], other.clocks[i]);
            if (cmp > 0)
              return false; // note, could return false even if isBefore is true
            else if (cmp < 0)
              isBefore = true;
        }
        return isBefore;
    }
}

你可以只用min和max来做一个不太准确的传球:

class VectorClockSummary {
    private long min, max;
    ...
    public tribool isBefore(VectorClockSummary other) {
        if (max < other.min)
            return true;
        else if (min > other.max)
            return false;
        else
            return maybe;
    }
}