Skip to content

Commit 67bfb29

Browse files
kpumukJens-G
authored andcommitted
Implemented header protocol for Ruby client library
1 parent 84554fa commit 67bfb29

File tree

15 files changed

+1839
-20
lines changed

15 files changed

+1839
-20
lines changed

LANGUAGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ Thrift's core protocol is TBinary, supported by all languages except for JavaScr
315315
<!-- Language Levels -------><td>2.7.8</td><td>4.0.0</td>
316316
<!-- Field types -----------><td><img src="/doc/images/cgrn.png" alt="Yes"/></td>
317317
<!-- Low-Level Transports --><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cred.png" alt=""/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cred.png" alt=""/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td>
318-
<!-- Transport Wrappers ----><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cred.png" alt=""/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cred.png" alt=""/></td>
318+
<!-- Transport Wrappers ----><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cred.png" alt=""/></td>
319319
<!-- Protocols -------------><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td>
320320
<!-- Servers ---------------><td><img src="/doc/images/cred.png" alt=""/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td><td><img src="/doc/images/cgrn.png" alt="Yes"/></td>
321321
<td align=left><a href="https://issues.apache.org/jira/issues/?jql=project%20%3D%20THRIFT%20AND%20component%20in%20(%22Ruby%20-%20Compiler%22%2C%20%22Ruby%20-%20Library%22)%20and%20status%20not%20in%20(fixed%2C%20resolved%2C%20closed)">Ruby</a></td>

lib/rb/benchmark/benchmark.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ class Server
3535
attr_accessor :interpreter
3636
attr_accessor :host
3737
attr_accessor :port
38+
attr_accessor :protocol_type
3839

3940
def initialize(opts)
4041
@serverclass = opts.fetch(:class, Thrift::NonblockingServer)
4142
@interpreter = opts.fetch(:interpreter, "ruby")
4243
@host = opts.fetch(:host, ::HOST)
4344
@port = opts.fetch(:port, ::PORT)
45+
@protocol_type = opts.fetch(:protocol_type, 'binary')
4446
@tls = opts.fetch(:tls, false)
4547
end
4648

4749
def start
4850
return if @serverclass == Object
4951
args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "")
50-
@pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{"-tls" if @tls} #{@host} #{@port} #{@serverclass.name}", "r+")
52+
@pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{"-tls" if @tls} #{@host} #{@port} #{@serverclass.name} #{@protocol_type}", "r+")
5153
Marshal.load(@pipe) # wait until the server has started
5254
sleep 0.4 # give the server time to actually start spawning sockets
5355
end
@@ -77,6 +79,7 @@ def initialize(opts, server)
7779
@interpreter = opts.fetch(:interpreter, "ruby")
7880
@server = server
7981
@log_exceptions = opts.fetch(:log_exceptions, false)
82+
@protocol_type = opts.fetch(:protocol_type, 'binary')
8083
@tls = opts.fetch(:tls, false)
8184
end
8285

@@ -96,7 +99,7 @@ def run
9699
end
97100

98101
def spawn
99-
pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{"-tls" if @tls} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
102+
pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{"-tls" if @tls} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client} #{@protocol_type}")
100103
@pool << pipe
101104
end
102105

@@ -202,6 +205,7 @@ def report_output
202205
[["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
203206
[["Server interpreter", "%s"], @server.interpreter],
204207
[["Client interpreter", "%s"], @interpreter],
208+
[["Protocol type", "%s"], @protocol_type],
205209
[["Socket class", "%s"], socket_class],
206210
["Number of processes", @num_processes],
207211
["Clients per process", @clients_per_process],
@@ -255,12 +259,14 @@ def resolve_const(const)
255259
end
256260

257261
puts "Starting server..."
262+
protocol_type = ENV['THRIFT_PROTOCOL'] || 'binary'
258263
args = {}
259264
args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
260265
args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
261266
args[:host] = ENV['THRIFT_HOST'] || HOST
262267
args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
263268
args[:tls] = ENV['THRIFT_TLS'] == 'true'
269+
args[:protocol_type] = protocol_type
264270
server = Server.new(args)
265271
server.start
266272

@@ -273,6 +279,7 @@ def resolve_const(const)
273279
args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
274280
args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
275281
args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
282+
args[:protocol_type] = protocol_type
276283
BenchmarkManager.new(args, server).run
277284

278285
server.shutdown

lib/rb/benchmark/client.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,36 @@
2525
require 'benchmark_service'
2626

2727
class Client
28-
def initialize(host, port, clients_per_process, calls_per_client, log_exceptions, tls)
28+
def initialize(host, port, clients_per_process, calls_per_client, log_exceptions, tls, protocol_type)
2929
@host = host
3030
@port = port
3131
@clients_per_process = clients_per_process
3232
@calls_per_client = calls_per_client
3333
@log_exceptions = log_exceptions
3434
@tls = tls
35+
@protocol_type = protocol_type || 'binary'
36+
end
37+
38+
def create_protocol(socket)
39+
case @protocol_type
40+
when 'binary'
41+
transport = Thrift::FramedTransport.new(socket)
42+
Thrift::BinaryProtocol.new(transport)
43+
when 'compact'
44+
transport = Thrift::FramedTransport.new(socket)
45+
Thrift::CompactProtocol.new(transport)
46+
when 'header'
47+
Thrift::HeaderProtocol.new(socket)
48+
when 'header-compact'
49+
Thrift::HeaderProtocol.new(socket, nil, Thrift::HeaderSubprotocolID::COMPACT)
50+
when 'header-zlib'
51+
protocol = Thrift::HeaderProtocol.new(socket)
52+
protocol.add_transform(Thrift::HeaderTransformID::ZLIB)
53+
protocol
54+
else
55+
transport = Thrift::FramedTransport.new(socket)
56+
Thrift::BinaryProtocol.new(transport)
57+
end
3558
end
3659

3760
def run
@@ -53,8 +76,8 @@ def run
5376
else
5477
Thrift::Socket.new(@host, @port)
5578
end
56-
transport = Thrift::FramedTransport.new(socket)
57-
protocol = Thrift::BinaryProtocol.new(transport)
79+
protocol = create_protocol(socket)
80+
transport = protocol.trans
5881
client = ThriftBenchmark::BenchmarkService::Client.new(protocol)
5982
begin
6083
start = Time.now
@@ -89,6 +112,6 @@ def print_exception(e)
89112
log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift
90113
tls = true if ARGV[0] == '-tls' and ARGV.shift
91114

92-
host, port, clients_per_process, calls_per_client = ARGV
115+
host, port, clients_per_process, calls_per_client, protocol_type = ARGV
93116

94-
Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions, tls).run
117+
Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions, tls, protocol_type).run

lib/rb/benchmark/server.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ def fibonacci(n)
3838
end
3939
end
4040

41-
def self.start_server(host, port, serverClass, tls)
41+
def self.create_factories(protocol_type)
42+
case protocol_type
43+
when 'binary'
44+
[FramedTransportFactory.new, BinaryProtocolFactory.new]
45+
when 'compact'
46+
[FramedTransportFactory.new, CompactProtocolFactory.new]
47+
when 'header'
48+
[HeaderTransportFactory.new, HeaderProtocolFactory.new]
49+
when 'header-compact'
50+
[HeaderTransportFactory.new, HeaderProtocolFactory.new(nil, HeaderSubprotocolID::COMPACT)]
51+
when 'header-zlib'
52+
# Note: Server doesn't add transforms - it mirrors client's transforms
53+
[HeaderTransportFactory.new, HeaderProtocolFactory.new]
54+
else
55+
[FramedTransportFactory.new, BinaryProtocolFactory.new]
56+
end
57+
end
58+
59+
def self.start_server(host, port, serverClass, tls, protocol_type = nil)
4260
handler = BenchmarkHandler.new
4361
processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
4462
transport = if tls
@@ -58,8 +76,8 @@ def self.start_server(host, port, serverClass, tls)
5876
else
5977
ServerSocket.new(host, port)
6078
end
61-
transport_factory = FramedTransportFactory.new
62-
args = [processor, transport, transport_factory, nil, 20]
79+
transport_factory, protocol_factory = create_factories(protocol_type || 'binary')
80+
args = [processor, transport, transport_factory, protocol_factory, 20]
6381
if serverClass == NonblockingServer
6482
logger = Logger.new(STDERR)
6583
logger.level = Logger::WARN
@@ -88,9 +106,9 @@ def resolve_const(const)
88106

89107
tls = true if ARGV[0] == '-tls' and ARGV.shift
90108

91-
host, port, serverklass = ARGV
109+
host, port, serverklass, protocol_type = ARGV
92110

93-
Server.start_server(host, port.to_i, resolve_const(serverklass), tls)
111+
Server.start_server(host, port.to_i, resolve_const(serverklass), tls, protocol_type)
94112

95113
# let our host know that the interpreter has started
96114
# ideally we'd wait until the server was serving, but we don't have a hook for that

lib/rb/lib/thrift.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
require 'thrift/protocol/compact_protocol'
4545
require 'thrift/protocol/json_protocol'
4646
require 'thrift/protocol/multiplexed_protocol'
47+
require 'thrift/protocol/header_protocol'
4748

4849
# transport
4950
require 'thrift/transport/base_transport'
@@ -56,6 +57,7 @@
5657
require 'thrift/transport/unix_server_socket'
5758
require 'thrift/transport/buffered_transport'
5859
require 'thrift/transport/framed_transport'
60+
require 'thrift/transport/header_transport'
5961
require 'thrift/transport/http_client_transport'
6062
require 'thrift/transport/io_stream_transport'
6163
require 'thrift/transport/memory_buffer_transport'

0 commit comments

Comments
 (0)