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

时间:2014-02-27 10:18:14

标签: c++ distributed distributed-computing clock

我为即将推出的分布式NoSQL数据库系统制作了一个内部分布式时间服务器(没有主服务器)。只要分布式系统中2/3的时钟正确,它就应该处理Byzantium时钟和时钟偏移问题。

我想看看其他人是如何实现这种模式的(对基于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()));
        }

1 个答案:

答案 0 :(得分:5)

时间同步协议中有大量文献,特别是对于无线传感器网络,其中部署环境不适合时间主控。关于this page的主题有一个很好的介绍。似乎最受关注的协议是洪水时间同步协议(FTSP),由Maróti,Kusy,Simon和Lédeczi以paper为名。我在其wiki上描述了TinyOS的实现,它具有您正在寻找的那种代码。

对于任何无主系统都有一个警告:没有“正确”时间的概念。您可以获得的最佳结果是将节点收敛到公共时间参考。这是一个共识时间,但不应被视为具有权威性的“正确”时间。