Skip to content

Commit

Permalink
Merge pull request datastax#265 from EasyPost:log_in_protocol
Browse files Browse the repository at this point in the history
pass through logger to CqlProtocolHandler; log information about heartbeats; send heartbeats even if one fails

Co-authored-by: Danilo Caballero Chinchilla <danilo@easypost.com>
  • Loading branch information
Roguelazer and dcaballeroc authored Oct 12, 2021
1 parent 8fdcfd2 commit e1b8dcb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/cassandra/cluster/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def do_connect(host)
@connection_options.nodelay? ? 1 : 0)

Protocol::CqlProtocolHandler.new(connection,
host,
@reactor,
@logger,
@connection_options.protocol_version,
@connection_options.compressor,
@connection_options.heartbeat_interval,
Expand Down
8 changes: 7 additions & 1 deletion lib/cassandra/protocol/cql_protocol_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class CqlProtocolHandler
attr_reader :protocol_version

def initialize(connection,
host,
scheduler,
logger,
protocol_version,
compressor = nil,
heartbeat_interval = 30,
Expand All @@ -50,10 +52,12 @@ def initialize(connection,
custom_type_handlers = {})
@protocol_version = protocol_version
@connection = connection
@host = host
@scheduler = scheduler
@compressor = compressor
@connection.on_data(&method(:receive_data))
@connection.on_closed(&method(:socket_closed))
@logger = logger

@streams = Array.new(requests_per_connection) {|i| i}

Expand Down Expand Up @@ -414,7 +418,8 @@ def schedule_heartbeat
end

timer.on_value do
send_request(HEARTBEAT, nil, false).on_value do
@logger.debug("sending heartbeat to #{@host}")
send_request(HEARTBEAT, nil, false).on_complete do
schedule_heartbeat
end
end
Expand All @@ -432,6 +437,7 @@ def reschedule_termination
end

timer.on_value do
@logger.info("#{@host} has had no activity in the last #{@idle_timeout}s; marking as failed")
@terminate = nil
@connection.close(TERMINATED)
end
Expand Down
15 changes: 11 additions & 4 deletions spec/cassandra/protocol/cql_protocol_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
module Cassandra
module Protocol
describe CqlProtocolHandler do
let :host do
'example.com'
end
let :logger do
Cassandra::NullLogger.new
end
let :protocol_handler do
described_class.new(connection, scheduler, 1, nil, 30, 60, 36)
described_class.new(connection, host, scheduler, 1, logger, nil, 30, 60, 36)
end

let :connection do
Expand Down Expand Up @@ -52,6 +58,8 @@ module Protocol
connection.stub(:on_connected) do |&h|
connection.stub(:connected_listener).and_return(h)
end
connection.stub(:host).and_return(host)

protocol_handler
end

Expand Down Expand Up @@ -87,7 +95,6 @@ module Protocol

describe '#host' do
it 'delegates to the connection' do
connection.stub(:host).and_return('example.com')
protocol_handler.host.should == 'example.com'
end
end
Expand Down Expand Up @@ -175,7 +182,7 @@ module Protocol

context 'when a compressor is specified' do
let :protocol_handler do
described_class.new(connection, scheduler, 1, compressor)
described_class.new(connection, host, scheduler, 1, logger, compressor)
end

let :compressor do
Expand Down Expand Up @@ -215,7 +222,7 @@ module Protocol

context 'when a protocol version is specified' do
let :protocol_handler do
described_class.new(connection, scheduler, 7)
described_class.new(connection, host, scheduler, 7, logger)
end

it 'sets the protocol version in the header' do
Expand Down

0 comments on commit e1b8dcb

Please sign in to comment.