问题发现:
往数据库做插入操作,发现数据全部集中在一个分片,其它分片没有数据。 并且插入效率超级慢!
查看日志发现mongs.log 报出如下的异常描述:
Tue Jul 29 10:14:54.602 [Balancer] caught exception while doing balance: error checking clock skew of cluster xx02:21000,xx03:21000,xx04:21000 :: caused by :: 13650 clock skew of the cluster xx02:21000,xx03:21000,xx04:21000 is too far out of bounds to allow distributed locking.
解决办法:
断定为是由于服务器时间不同步引起的问题,在每台机器运行:ntpdate asia.pool.ntp.org 同步系统时间.
问题深究:
以上问题可以通过mongodb源码进行探源:
报错的信息在client/distlock.cpp的DistributedLock这个类中.
DistributedLock(分布锁)这个类为configdb提供了同步整个集群环境任务状态的方法。每个任务在集群中都必须有一个唯一的名字,比如数据平衡任务'balancer'。
这个锁的信息记录在configdb的locks集合中。每个锁生效都必须在一个预先规定的时间范围内,在初始化类的时候这个类都会自动去维护这个时间,判断是否超时
DistributedLock的got函数
string got( DistributedLock& lock, unsigned long long sleepTime ) { .... // Check our clock skew try { if( lock.isRemoteTimeSkewed() ) { throw LockException( str::stream() << "clock skew of the cluster " << conn.toString() << " is too far out of bounds to allow distributed locking." , 13650 ); } } catch( LockException& e) { throw LockException( str::stream() << "error checking clock skew of cluster " << conn.toString() << causedBy( e ) , 13651); } .... } bool DistributedLock::isRemoteTimeSkewed() { return !DistributedLock::checkSkew( _conn, NUM_LOCK_SKEW_CHECKS, _maxClockSkew, _maxNetSkew ); } /** * Check the skew between a cluster of servers */ static bool checkSkew( const ConnectionString& cluster, unsigned skewChecks = NUM_LOCK_SKEW_CHECKS, unsigned long long maxClockSkew = MAX_LOCK_CLOCK_SKEW, unsigned long long maxNetSkew = MAX_LOCK_NET_SKEW );
checkSkew就是判断server之间时间差的函数,此函数有几个参数
1、skewChecks 检查次数
2、maxClockSkew 最大的时间差
3、maxNetSkew 检查时网络的最大时间差
.每个参数初始化的时候都有默认值,此默认值在distlock.h头文件中
#define LOCK_TIMEOUT (15 * 60 * 1000) #define LOCK_SKEW_FACTOR (30) #define LOCK_PING (LOCK_TIMEOUT / LOCK_SKEW_FACTOR) #define MAX_LOCK_NET_SKEW (LOCK_TIMEOUT / LOCK_SKEW_FACTOR) #define MAX_LOCK_CLOCK_SKEW (LOCK_TIMEOUT / LOCK_SKEW_FACTOR) #define NUM_LOCK_SKEW_CHECKS (3)
maxNetSkew 是从检查机器到被检查机器,执行serverStatus命令返回的最大时间
Date_t then = jsTime(); bool success = conn->get()->runCommand( string("admin"),BSON( "serverStatus" << 1 ), result ); delay = jsTime() - then;
checkSkew通过相互比较集群中server的时间3次,得到集群中差值最大的时间间隔,如果大于maxClockSkew ,那么报出异常
// Make sure our max skew is not more than our pre-set limit if(totalSkew > (long long) maxClockSkew) { log( logLvl + 1 ) << "total clock skew of " << totalSkew << "ms for servers " << cluster << " is out of " << maxClockSkew << "ms bounds." << endl; return false; }