内部分布式时间服务器实现

Internal Distributed Time Server implementation

本文关键字:实现 服务器 时间 分布式 内部      更新时间:2023-10-16

我已经为我们即将推出的分布式NoSQL数据库系统制作了一个内部分布式时间服务器(没有母版)。它应该处理拜占庭时钟和时钟偏差问题,只要分布式系统中 2/3 的时钟是正确的。

然而,我想看看其他人如何实现这种模式(对基于 IEEE 1588 的主/从模式实现不感兴趣)——最好是一些已经在使用的开源代码——断言我已经正确实现了它,因为很难为它编写单元测试。

有谁知道这样的开源实现?我们使用的编程语言C++所以我更喜欢 C/C++ 引用,尽管只要代码是人类可读的,它可能并不那么重要。

以下是到目前为止我的实现的代码(为了简单起见,部分伪代码):

/*!
brief Maximum allowed clock skew in milliseconds
details A network node that has a clock skew greater than this value will be ignore
* and an error message will be logged
note Maximum divergence (drift) between two clocks on the network nodes will be 3x this amount if we 
* have a worst case Byzantium clock issue
*/
#define MAX_ALLOWED_CLOCK_SCEW_MS 333
/*!
class CTimeServer
brief Internal distributed time server
details The time server frequently recieves the time from all the other master server nodes
* in the DBMS and calculates the current time by averaging all of the recieves timestamps.
note If a server node has a greater clock skew than c MAX_ALLOWED_CLOCK_SCEW_MS then it its
* timestamp is ignored and an error message is logged
note Clocks are accurately synchronized until more than 1/3 of the nodes have Byzantium clock issues
author Inge Eivind Henriksen
date February 2014
*/
class CTimeServer
{
    private:
        /** System offset in milliseconds */
        std::atomic<int> offsetAverageMs;
        /*!
        brief Node offsets type
        par key Node ID
        par value Offset in milliseconds
        */
        typedef std::map<int, int> nodeOffset_t;
        /*!
        brief Iterator type for c nodeOffset_t
        relates nodeOffset_t
        */
        typedef nodeOffset_t::iterator nodeOffsetIter_t;
        /** Node offsets */
        nodeOffset_t nodeOffsets;
        /*!
        brief Calculates the offset time in milliseconds between all the nodes in the distributed system
        */
        int CalculateOffsetMs() {
            bool exists;
            nodeOffsetIter_t offsets_iter(&nodeOffsets);
            int offsetMs = offsets_iter.first(&exists);
            int averageMs = 0;
            while (exists)
            {
                averageMs += offsetMs;
                averageMs /= 2;
                // Get the next file manager in the map
                offsetMs = offsets_iter.next(&exists);
            }
            return averageMs;
        }
    public:
        CTimeServer() {
            offsetAverageMs = 0;
        }
        /*!
        brief Register the time of a node
        param nodeHostName [in] Network node host name or IP address
        param nodeId [in] Network node ID
        param timestamp [in] Network node timestamp
        */
        void RegisterNodeTime(const wchar_t *nodeHostName, int nodeId, time_t timestamp) {
            int now = (int)time(NULL);
            int offset = (int)timestamp - now;
            // Make sure the node clock is within the permitted values
            if (abs(offset) > MAX_ALLOWED_CLOCK_SCEW_MS)
            {
                // Node clock skew was outside the permitted limit, so remove it from the list of valid time offsets
                nodeOffsets.erase(nodeId);
                // Throw an error
                std::wstringstream err;
                err << L"Network node " << nodeHostName << L" exceeded the maximum allowed clock skew of " 
                    << MAX_ALLOWED_CLOCK_SCEW_MS << L" ms by " << offset << " ms. Set the clock to correct this problem.";
                throw err.str().c_str();
            }
            nodeOffsets.update(nodeId, offset);
            // Recalculate the offset average
            offsetAverageMs.store(CalculateOffsetMs());
        }
        /*!
        brief Get the distributed system time
        returns The distributed system time
        */
        time_t GetTime() {
            int now = (int)time(NULL);
            return (time_t)(now + offsetAverageMs.load()));
        }

在时间同步协议方面有相当多的文献,特别是对于无线传感器网络,其中部署环境不适合时间主控。此页面上对该主题进行了不错的介绍。似乎最受关注的协议是洪水时间同步协议(FTSP),来自Maróti,Kusy,Simon和Lédeczi的同名论文。我在其wiki上发现了TinyOS的实现,其中包含您正在寻找的代码类型。

对于任何无主系统,都有一个警告:没有"正确"时间的概念。你能得到的最好的结果是将节点收敛到一个公共时间参考。这是一个共识时间,但它不应该被认为是权威的"正确"时间。