Class: Pinot::JsonHttpTransport
- Inherits:
-
Object
- Object
- Pinot::JsonHttpTransport
- Defined in:
- lib/pinot/transport.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ "Content-Type" => "application/json; charset=utf-8" }.freeze
- RETRYABLE_ERRORS =
[ Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout ].freeze
- HTTP_ERROR_MAP =
HTTP status codes that map to specific error classes and are safe to retry
{ "408" => QueryTimeoutError, "429" => RateLimitError, "503" => BrokerUnavailableError, "504" => BrokerUnavailableError }.freeze
- RETRYABLE_HTTP_ERRORS =
[RateLimitError, BrokerUnavailableError].freeze
- TIMEOUT_ERROR_CODES =
Pinot exception errorCode values that indicate query timeout. 250 = ExecutionTimeoutError (server-side), 400 = BrokerTimeoutError.
[250, 400].freeze
Instance Attribute Summary collapse
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
Instance Method Summary collapse
- #execute(broker_address, request, extra_request_headers: {}) ⇒ Object
-
#initialize(http_client:, extra_headers: {}, timeout_ms: nil, logger: nil, max_retries: 0, retry_interval_ms: 200) ⇒ JsonHttpTransport
constructor
A new instance of JsonHttpTransport.
Constructor Details
#initialize(http_client:, extra_headers: {}, timeout_ms: nil, logger: nil, max_retries: 0, retry_interval_ms: 200) ⇒ JsonHttpTransport
Returns a new instance of JsonHttpTransport.
221 222 223 224 225 226 227 228 229 |
# File 'lib/pinot/transport.rb', line 221 def initialize(http_client:, extra_headers: {}, timeout_ms: nil, logger: nil, max_retries: 0, retry_interval_ms: 200) @http_client = http_client @extra_headers = extra_headers @timeout_ms = timeout_ms @logger = logger @max_retries = max_retries @retry_interval_ms = retry_interval_ms end |
Instance Attribute Details
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
219 220 221 |
# File 'lib/pinot/transport.rb', line 219 def http_client @http_client end |
Instance Method Details
#execute(broker_address, request, extra_request_headers: {}) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/pinot/transport.rb', line 231 def execute(broker_address, request, extra_request_headers: {}) logger.debug "Pinot query to #{broker_address}: #{request.query}" attempts = 0 max_attempts = (@max_retries || 0) + 1 begin attempts += 1 url = build_url(broker_address, request.query_format) body = build_body(request) headers = DEFAULT_HEADERS .merge(@extra_headers) .merge("X-Correlation-Id" => SecureRandom.uuid) .merge(extra_request_headers) resp = @http_client.post(url, body: body, headers: headers) if (error_class = HTTP_ERROR_MAP[resp.code]) logger.error "Pinot broker returned HTTP #{resp.code}" raise error_class, "http exception with HTTP status code #{resp.code}" end unless resp.code.to_i == 200 logger.error "Pinot broker returned HTTP #{resp.code}" raise TransportError, "http exception with HTTP status code #{resp.code}" end broker_response = begin BrokerResponse.from_json(resp.body) rescue JSON::ParserError => e raise e. end if (timeout_ex = broker_response.exceptions.find { |ex| TIMEOUT_ERROR_CODES.include?(ex.error_code) }) raise QueryTimeoutError, timeout_ex. end broker_response rescue *RETRYABLE_HTTP_ERRORS, *RETRYABLE_ERRORS => e if attempts < max_attempts sleep_ms = (@retry_interval_ms || 200) * (2**(attempts - 1)) sleep(sleep_ms / 1000.0) retry end raise(Net::ReadTimeout === e || Net::WriteTimeout === e ? QueryTimeoutError.new(e.) : e) end end |