@@ -26,7 +26,7 @@ module Selectable
2626 # @return [ Array ] tag_sets The tag sets used to select servers.
2727 attr_reader :tag_sets
2828
29- # @return [ Float ] max_staleness The maximum replication lag, in seconds, that a
29+ # @return [ Integer ] max_staleness The maximum replication lag, in seconds, that a
3030 # secondary can suffer and still be eligible for a read.
3131 #
3232 # @since 2.4.0
@@ -68,7 +68,7 @@ def ==(other)
6868 def initialize ( options = { } )
6969 @options = ( options || { } ) . freeze
7070 @tag_sets = ( options [ :tag_sets ] || [ ] ) . freeze
71- @max_staleness = options [ :max_staleness ] if options [ :max_staleness ] && options [ :max_staleness ] > 0
71+ @max_staleness = options [ :max_staleness ] unless options [ :max_staleness ] == - 1
7272 validate!
7373 end
7474
@@ -154,7 +154,7 @@ def candidates(cluster)
154154 elsif cluster . sharded?
155155 near_servers ( cluster . servers ) . each { |server | validate_max_staleness_support! ( server ) }
156156 else
157- validate_max_staleness_value! ( cluster )
157+ validate_max_staleness_value! ( cluster ) unless cluster . unknown?
158158 select ( cluster . servers )
159159 end
160160 end
@@ -230,14 +230,14 @@ def filter_stale_servers(candidates, primary = nil)
230230 validate_max_staleness_support! ( server )
231231 staleness = ( server . last_scan - server . last_write_date ) -
232232 ( primary . last_scan - primary . last_write_date ) +
233- ( server . heartbeat_frequency * 1000 )
233+ ( server . heartbeat_frequency_seconds * 1000 )
234234 staleness <= max_staleness_ms
235235 end
236236 else
237237 max_write_date = candidates . collect ( &:last_write_date ) . max
238238 candidates . select do |server |
239239 validate_max_staleness_support! ( server )
240- staleness = max_write_date - server . last_write_date + ( server . heartbeat_frequency * 1000 )
240+ staleness = max_write_date - server . last_write_date + ( server . heartbeat_frequency_seconds * 1000 )
241241 staleness <= max_staleness_ms
242242 end
243243 end
@@ -258,10 +258,12 @@ def validate_max_staleness_support!(server)
258258 end
259259
260260 def validate_max_staleness_value! ( cluster )
261- return unless @max_staleness
262- heartbeat_frequency = cluster . options [ :heartbeat_frequency ] || Server ::Monitor ::HEARTBEAT_FREQUENCY
263- if @max_staleness < heartbeat_frequency * 2
264- raise Error ::InvalidServerPreference . new ( Error ::InvalidServerPreference ::INVALID_MAX_STALENESS )
261+ if @max_staleness
262+ heartbeat_frequency_seconds = cluster . options [ :heartbeat_frequency ] || Server ::Monitor ::HEARTBEAT_FREQUENCY
263+ unless @max_staleness >= [ SMALLEST_MAX_STALENESS_SECONDS ,
264+ ( heartbeat_frequency_seconds + Cluster ::IDLE_WRITE_PERIOD_SECONDS ) ] . max
265+ raise Error ::InvalidServerPreference . new ( Error ::InvalidServerPreference ::INVALID_MAX_STALENESS )
266+ end
265267 end
266268 end
267269 end
0 commit comments