尝试使用Mongoid 4.0.2 gem测试自动故障转移并使用MongoDB 2.4.3
为了模拟这个,我正在使用这个测试代码:
require 'mongoid'
class TestClass
include Mongoid::Document
store_in collection: "test", database: "test"
field :uuid, type: String
end
Mongoid.load!("config/mongoid.yml", :test)
batch = (1..100).map { |x| TestClass.new({ uuid: x }) }
batch.each_with_index { |x, i|
begin
x.save
sleep(5.seconds)
puts "Saved #{i} records" if i%10 == 0
rescue Exception => e
puts e.message
end
}
在两次保存之间,我跳过我的MongoDB并在Mongo集群的主节点上执行 rs.stepDown(),遗憾的是,这导致我的测试应用程序出现以下错误:
See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.
Moped::Errors::OperationFailure
The operation: #<Moped::Protocol::Command
@length=68
@request_id=192
@response_to=0
@op_code=2004
@flags=[]
@full_collection_name="test.$cmd"
@skip=0
@limit=-1
@selector={:getlasterror=>1, :w=>1}
@fields=nil>
failed with error 10058: "not master"
我的Mongoid配置如此:
test:
sessions:
default:
database: test_db
hosts:
- 192.168.1.10:27017
- 192.168.1.11:27017
options:
max_retries: 10
retry_interval: 1
知道我在这里做错了吗?我认为Mongoid驱动程序会自动检测集群中的更改并在客户端/ Ruby端更新集群状态后自动重试请求吗?