-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip-reranking when current_first/current_last is nil #148
Conversation
My preference would be locking, perhaps with an advisory lock if you wanted to investigate that. Does this problem still occur with the latest version of ranked-model? |
Sorry, obviously it does since that's mentioned in your error :) How do we end up with no |
@brendon Here's our ranked model class JobProductionPlanSegment < ApplicationRecord
include RankedModel
ranks :sequence, with_same: :job_production_plan_id The migration for it specified t.integer :sequence, null: false The database is pg 11 Here are some logs from a recent occurrence all requests below are POST to /v1/job-production-plan-segments with the same job_production_plan_id
so maybe sequence-position 0 hadn't been persisted yet? |
lib/ranked-model/ranker.rb
Outdated
@@ -208,6 +214,11 @@ def rearrange_ranks | |||
end | |||
end | |||
|
|||
def handle_cannot_rearrange_ranks(message) | |||
logger.warn "[RANKED_MODEL] #{message}. Skipping re-ranking of #{instance.model_name.name} #{instance.id}" | |||
nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning nil as implemented will skip-reranking
alternatively, could have a strategy
fail RankedModel::Unrankable, message, instance
Or, since we're in a before_save
we can 'throw :abort' in the later rails versions, like
errors.add ranker.column, "Could not re-rank columns: #{message}"
throw :abort
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #151
73ce08e
to
a66051b
Compare
@brendon on reflection I decide it made sense to make failing the 'before-save' hook to be a first class thing, and then used that as the default strategy for handling current-first or current-last being missing. If you want to point me to where the appropriate test would go and give a few pointers, I'd be happy to add it. |
a66051b
to
fe511ed
Compare
1dea14b
to
1263b00
Compare
OMG, I wrote a failing test and made it pass. But I don't think my code style is right for the repo. |
35a1c24
to
7f6a34a
Compare
patched_duck_ranker = age_ranker.with(patched_duck) | ||
age_ranker.expects(:with).once.returns(patched_duck_ranker) | ||
patched_duck_ranker.expects(:current_first).returns(nil) | ||
patched_duck_ranker.expects(:current_at_rank).returns(:something_truthy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is otherwise nil. It's need to be truthy for rearrange_ranks
to be called
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return value never seems to need to be anything but truthy since it was introduced 8280511
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. Perhaps it was intended for future use. For it to return a boolean you'd probably want to implement a current_at_rank?
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brendon So, the reason I'm looking at current_at_rank
at all is because I wanted to reproduce the problem I experienced: creating a record, specifying a position, rearrange ranks failing due to current_first being nil
In order to do that, I discovered that in order for rearrange_ranks to be called in my situation, current_at_rank
had to be truthy. I looked at the method and determined, for mocking purposes, I didn't need to care about what it was doing or returning
I was a little curious about current_at_rank
since I didn't see it used anywhere else or directly tested. So, I looked through the version history and found as noted above that it was introduced to replace the condition (current_order.find do |rankable| rankable.rank.nil? || rankable.rank == rank end)
which confirmed that its usage in assure_unique_position
is only to return something truthy.
Interestingly, it's truthiness was replaced by finder.except( :order ).where( ranker.column => rank ).first
where 'finder' is more or less equivalent to 'current_order'
now, why would current_at_rank(rank)
be truthy when current_first
is nil, when current_first is defined as finder.first
. I'm not sure, but it could be a race condition or a query cache issue or related to the except
call (though I doubt it)
For the purpose of reproducing the failure and testing fixes, I think my test and and prs are sufficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure exactly what the intended meaning of current_at_rank is, or if it changed during the refactor. I think it is that another record exists?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's continue the discussion here: #149
Hi @bf4, this is going to take me a while to look through. At first glance it seems like a mildly ungraceful solution to the problem that seems to deal with the symptoms rather than trying to figure out the cause. Nonetheless I'll star this to look at once I've cleared my other backlog of work :D |
@brendon In that case, allow me to break this up into smaller prs
|
a3080a0
to
8b60ac5
Compare
23d89f0
to
f095445
Compare
Closing as stale |
A take on 'first do no harm' when seeing the occasional
There are various possible strategies for handling a missing 'current_x'
Related
Work done: