-
Notifications
You must be signed in to change notification settings - Fork 151
After stepping down the primary node, queries failed with error 16550: "not authorized for query on test.users" #247
Comments
Here is my test script used in the steps above. require 'moped'
require 'pp'
#Moped.logger = Logger.new($stdout)
session = Moped::Session.new(%w(
node1, node2, node3
), refresh_interval: 60)
session.use :test
session.login("test", "password")
1000.to_i.times {|x|
begin
id = Moped::BSON::ObjectId.new
session.with(safe: false)[:users].insert({_id: id, created_at: Time.now })
sleep 1
pp x
pp session[:users].find(_id: id).one
sleep 1
rescue => e
pp e
end
} |
In moped v1.5.1, node#query method, it tried to re-auth when getting 16550 unauthorized error (you can see the comment says exactly like the scenario I reported). The problem is the "database" variable could be either String or Symbol; when it is a Symbol, that causes the def query(database, collection, selector, options = {})
operation = Protocol::Query.new(database, collection, selector, options)
process(operation) do |reply|
if reply.query_failed?
if reply.unauthorized? && auth.has_key?(database)
# If we got here, most likely this is the case of Moped
# authenticating successfully against the node originally, but the
# node has been reset or gone down and come back up. The most
# common case here is a rs.stepDown() which will reinitialize the
# connection. In this case we need to requthenticate and try again,
# otherwise we'll just raise the error to the user.
login(database, *auth[database])
reply = query(database, collection, selector, options)
else
raise Errors::QueryFailure.new(operation, reply.documents.first)
end
end
reply
end
end Converting the database variable to a String fixed the issue. However it appears that in 2.0.0-beta, the error/failover handling and auth have major refactoring and it looks like this bug doesn't happen anymore (I didn't do thorough testing with 2.0.0). To workaround this issue, just use string as a database name (e.g. session.use 'test' not session.use :test) and you won't run into this issue. |
Can you tried this against moped master? is the error still happening? |
I ran against the latest master branch; in short, yes it is still happening. But the sequence of events has changed so I'll just state what I observed below:
|
See readme for master changes - |
Thanks for the explanation, I should have read the CHANGELOG. Now with
|
Also the workaround for moped 1.5.1 (ie. use |
@ptan-iswifter are you still seeing this issue with 2.0.0? I just tried testing on 1.5.1 and 2.0.0 and it looks like this is still an issue... :/ |
was able to resolve using refresh_interval setting. |
@chrischang12 how exactly did you solve this with the refresh_interval setting? |
@duromano refresh_interval helps discover the status of nodes quicker. by default mongoid/moped caches information about a node for 300 seconds/5 minutes. if there are any changes to a node's state within these 5 minutes, that means that the driver won't know about it. lowering the refresh_interval makes the driver check the status of nodes more frequently. see source: |
@chrischang12 hmmm I see.. It makes sense! Thanks! |
@duromano i'm not too familiar with how moped creates/manages connections but one potential workaround could be simply closing the connection that throws the error after X number of times and creating a new one. i'm assuming here that you're only getting that error when the driver reuses existing connections, as opposed to newly created connections. |
We just got caught by this in production. Seems both A review of the mongodb logs suggests that moped may be losing the authentication credentials as a If this diagnosis is true, that's the real source of the problem, and not the error & trace below, which are just the natural result of the auth error. Current error, as of moped 2.0.0:
Backtrace (apologies for formatting):
|
Great @wandenberg !!! |
closing this as #324 was merged and it should solve this issue |
I'm using moped 1.5.1, mongodb 2.4.8
Steps to reproduce:
It seems to me that moped continues to use the connection to the ex-primary node for querying purpose now but mongodb requires reauth (or even reconnect, that's what mongo shell does).
Thanks in advance for looking into the issue.
-Peter
The text was updated successfully, but these errors were encountered: