Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fix http2 async import.

kishorenc030dc5ca3572
diff --git a/cmake/H2O.cmake b/cmake/H2O.cmake
index 572e51d7..bcf14f5e 100644
--- a/cmake/H2O.cmake
+++ b/cmake/H2O.cmake
@@ -1,12 +1,12 @@
# Download and build H2O
-set(H2O_VERSION 2.3.0-beta2)
+set(H2O_VERSION 5accc794404996fb056f1826f5a172693de95f22)
set(H2O_NAME h2o-${H2O_VERSION})
-set(H2O_TAR_PATH ${DEP_ROOT_DIR}/v${H2O_NAME}.tar.gz)
+set(H2O_TAR_PATH ${DEP_ROOT_DIR}/${H2O_NAME}.tar.gz)
if(NOT EXISTS ${H2O_TAR_PATH})
- message(STATUS "Downloading ${H2O_NAME}...")
- file(DOWNLOAD https://github.com/h2o/h2o/archive/v${H2O_VERSION}.tar.gz ${H2O_TAR_PATH})
+ message(STATUS "Downloading https://github.com/h2o/h2o/archive/${H2O_VERSION}.tar.gz")
+ file(DOWNLOAD https://github.com/h2o/h2o/archive/${H2O_VERSION}.tar.gz ${H2O_TAR_PATH})
endif()
if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME})
@@ -14,9 +14,13 @@ if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME})
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${H2O_TAR_PATH} WORKING_DIRECTORY ${DEP_ROOT_DIR}/)
endif()
-if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME}/build/h2o)
+if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME}/build/libh2o-evloop.a)
message("Configuring ${H2O_NAME}...")
file(MAKE_DIRECTORY ${DEP_ROOT_DIR}/${H2O_NAME}/build)
+
+ execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory deps/picotls/include include/h2o
+ WORKING_DIRECTORY ${DEP_ROOT_DIR}/${H2O_NAME})
+
execute_process(COMMAND ${CMAKE_COMMAND}
"-DCMAKE_FIND_LIBRARY_SUFFIXES=.a"
"-DWITH_MRUBY=off"
@@ -32,6 +36,7 @@ if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME}/build/h2o)
message("Building ${H2O_NAME} locally...")
execute_process(COMMAND ${CMAKE_COMMAND} --build
"${DEP_ROOT_DIR}/${H2O_NAME}/build"
+ --target libh2o-evloop
RESULT_VARIABLE
H2O_BUILD)
if(NOT H2O_BUILD EQUAL 0)
diff --git a/include/http_data.h b/include/http_data.h
index 2aa7f1b6..b4e4ac69 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -31,6 +31,9 @@ struct http_res {
std::string body;
bool final;
+ // fulfilled by an async response handler to pass control back to raft replica apply thread
+ std::promise<bool>* promise = nullptr;
+
h2o_generator_t* generator = nullptr;
http_res(): status_code(501), content_type_header("application/json; charset=utf-8"), final(true) {
@@ -130,8 +133,9 @@ struct http_req {
uint64_t route_hash;
std::map<std::string, std::string> params;
- std::string stream_state;
- size_t chunk_length;
+ bool first_chunk_aggregate;
+ bool last_chunk_aggregate;
+ size_t chunk_len;
std::string body;
size_t body_index;
@@ -142,15 +146,17 @@ struct http_req {
// for deffered processing of async handlers
h2o_custom_timer_t defer_timer;
- http_req(): _req(nullptr), route_hash(1), chunk_length(0), body_index(0), data(nullptr) {
+ http_req(): _req(nullptr), route_hash(1),
+ first_chunk_aggregate(true), last_chunk_aggregate(false),
+ chunk_len(0), body_index(0), data(nullptr) {
}
http_req(h2o_req_t* _req, const std::string & http_method, uint64_t route_hash,
const std::map<std::string, std::string> & params, const std::string& body):
_req(_req), http_method(http_method), route_hash(route_hash), params(params),
- stream_state("NON_STREAMING"), chunk_length(0), body(body), body_index(0),
- data(nullptr) {
+ first_chunk_aggregate(true), last_chunk_aggregate(false),
+ chunk_len(0), body(body), body_index(0), data(nullptr) {
}
@@ -167,8 +173,8 @@ struct http_req {
}
metadata = content.count("metadata") != 0 ? content["metadata"] : "";
- stream_state = content.count("stream_state") != 0 ? content["stream_state"] : "";
-
+ first_chunk_aggregate = content.count("first_chunk_aggregate") != 0 ? content["first_chunk_aggregate"].get<bool>() : true;
+ last_chunk_aggregate = content.count("last_chunk_aggregate") != 0 ? content["last_chunk_aggregate"].get<bool>() : false;
_req = nullptr;
}
@@ -176,19 +182,16 @@ struct http_req {
nlohmann::json content;
content["route_hash"] = route_hash;
content["params"] = params;
- content["stream_state"] = stream_state;
+ content["first_chunk_aggregate"] = first_chunk_aggregate;
+ content["last_chunk_aggregate"] = last_chunk_aggregate;
content["body"] = body;
content["metadata"] = metadata;
return content.dump();
}
- bool is_ending() {
- return stream_state == "END" || stream_state == "NON_STREAMING";
- }
-
- bool is_starting() {
- return stream_state == "START" || stream_state == "NON_STREAMING";
+ bool is_http_v1() {
+ return (_req->version < 0x200);
}
};
diff --git a/include/http_server.h b/include/http_server.h
index e3ea3273..51e0e6f0 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -46,6 +46,8 @@ private:
h2o_hostconf_t *hostconf;
h2o_socket_t* listener_socket;
+ static const size_t ACTIVE_STREAM_WINDOW_SIZE = 196605;
+
static const uint64_t SSL_REFRESH_INTERVAL_MS = 8 * 60 * 60 * 1000;
h2o_custom_timer_t ssl_refresh_timer;
@@ -155,4 +157,4 @@ public:
static void on_deferred_process_request(h2o_timer_t *entry);
void defer_processing(http_req& req, http_res& res, size_t timeout_ms);
-};
\ No newline at end of file
+};
diff --git a/src/collection.cpp b/src/collection.cpp
index 3a12f0c2..5bdf17d1 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -201,7 +201,6 @@ nlohmann::json Collection::add_many(std::vector<std::string>& json_lines) {
for(size_t i=0; i < json_lines.size(); i++) {
const std::string & json_line = json_lines[i];
-
nlohmann::json document;
Option<uint32_t> doc_seq_id_op = to_doc(json_line, document);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 4ffa9059..26f830cb 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -524,7 +524,7 @@ bool get_export_documents(http_req & req, http_res & res) {
Collection* collection = collectionManager.get_collection(req.params["collection"]);
if(collection == nullptr) {
- req.stream_state = "NON_STREAMING";
+ req.last_chunk_aggregate = true;
res.set_404();
HttpServer::stream_response(req, res);
return false;
@@ -541,19 +541,6 @@ bool get_export_documents(http_req & req, http_res & res) {
it = static_cast<rocksdb::Iterator*>(req.data);
}
- if(req.stream_state == "DISPOSE") {
- LOG(INFO) << "Disposing export iterator";
- delete it;
- req.data = nullptr;
- return true;
- }
-
- if(req.stream_state == "NON_STREAMING") {
- req.stream_state = "START";
- } else if(req.stream_state == "START") {
- req.stream_state = "IN_PROGRESS";
- }
-
if(it->Valid() && it->key().ToString().compare(0, seq_id_prefix.size(), seq_id_prefix) == 0) {
res.body = it->value().ToString();
it->Next();
@@ -561,8 +548,11 @@ bool get_export_documents(http_req & req, http_res & res) {
// append a new line character if there is going to be one more record to send
if(it->Valid() && it->key().ToString().compare(0, seq_id_prefix.size(), seq_id_prefix) == 0) {
res.body += "\n";
+ req.last_chunk_aggregate = false;
} else {
- req.stream_state = "END";
+ req.last_chunk_aggregate = true;
+ delete it;
+ req.data = nullptr;
}
}
@@ -574,19 +564,16 @@ bool get_export_documents(http_req & req, http_res & res) {
}
bool post_import_documents(http_req& req, http_res& res) {
+ //LOG(INFO) << "post_import_documents";
+ //LOG(INFO) << "req.first_chunk=" << req.first_chunk_aggregate << ", last_chunk=" << req.last_chunk_aggregate;
const char *BATCH_SIZE = "batch_size";
- if(req.stream_state == "DISPOSE") {
- // we don't cache any state across iterations, so just return
- return true;
- }
-
if(req.params.count(BATCH_SIZE) == 0) {
req.params[BATCH_SIZE] = "40";
}
if(!StringUtils::is_uint32_t(req.params[BATCH_SIZE])) {
- req.stream_state = "NON_STREAMING";
+ req.last_chunk_aggregate = true;
res.set_400("Parameter `" + std::string(BATCH_SIZE) + "` must be a positive integer.");
HttpServer::stream_response(req, res);
return false;
@@ -599,36 +586,35 @@ bool post_import_documents(http_req& req, http_res& res) {
return false;
}
- if(req.stream_state == "START" && req.body_index == 0) {
- LOG(INFO) << "Import batch size: " << IMPORT_BATCH_SIZE;
+ if(req.body_index == 0) {
+ // will log for every major chunk of request body
+ LOG(INFO) << "Import, req.body.size=" << req.body.size() << ", batch_size=" << IMPORT_BATCH_SIZE;
}
CollectionManager & collectionManager = CollectionManager::get_instance();
Collection* collection = collectionManager.get_collection(req.params["collection"]);
if(collection == nullptr) {
- req.stream_state = (req.stream_state == "IN_PROGRESS") ? "END" : "NON_STREAMING";
+ req.last_chunk_aggregate = true;
res.set_404();
HttpServer::stream_response(req, res);
return false;
}
- //LOG(INFO) << "Import, req.body.size: " << req.body.size() << ", state: " << req.stream_state;
+ //LOG(INFO) << "Import, " << "req.body_index=" << req.body_index << ", req.body.size: " << req.body.size();
+ //LOG(INFO) << "req body %: " << (float(req.body_index)/req.body.size())*100;
std::vector<std::string> json_lines;
req.body_index = StringUtils::split(req.body, json_lines, "\n", false, req.body_index, IMPORT_BATCH_SIZE);
bool stream_proceed = false; // default state
- //LOG(INFO) << "req.body_index=" << req.body_index << ", req.body.size=" << req.body.size();
- //LOG(INFO) << "req body percentage: " << (float(req.body_index)/req.body.size())*100;
-
if(req.body_index == req.body.size()) {
// body has been consumed fully, see whether we can fetch more request body
req.body_index = 0;
stream_proceed = true;
- if(req.stream_state == "END" || req.stream_state == "NON_STREAMING") {
+ if(req.last_chunk_aggregate) {
req.body = "";
} else {
if(!json_lines.empty()) {
@@ -637,6 +623,8 @@ bool post_import_documents(http_req& req, http_res& res) {
// eject partial record
req.body = json_lines.back();
json_lines.pop_back();
+ } else {
+ req.body = "";
}
}
}
@@ -654,7 +642,7 @@ bool post_import_documents(http_req& req, http_res& res) {
//ss << import_summary_json << "\n";
for (size_t i = 0; i < json_lines.size(); i++) {
- if(i == json_lines.size()-1 && req.stream_state == "END" && req.body_index == req.body.size()) {
+ if(i == json_lines.size()-1 && req.body_index == req.body.size() && req.last_chunk_aggregate) {
// indicates last record of last batch
ss << json_lines[i];
} else {
@@ -1073,9 +1061,14 @@ bool raft_write_send_response(void *data) {
route_path* found_rpath = nullptr;
bool route_found = server->get_route(index_arg->req->route_hash, &found_rpath);
if(route_found) {
- // call request handler
- found_rpath->handler(*index_arg->req, *index_arg->res);
+ // for an async response handler, we need to assign the promise
async_res = found_rpath->async_res;
+ if(async_res) {
+ index_arg->res->promise = index_arg->promise;
+ }
+
+ // now we can call the request handler
+ found_rpath->handler(*index_arg->req, *index_arg->res);
} else {
index_arg->res->set_404();
}
@@ -1084,10 +1077,10 @@ bool raft_write_send_response(void *data) {
if(!async_res) {
// only handle synchronous responses as async ones are handled by their handlers
server->send_response(index_arg->req, index_arg->res);
- }
- if(index_arg->promise != nullptr) {
- index_arg->promise->set_value(true); // returns control back to caller
+ if(index_arg->promise != nullptr) {
+ index_arg->promise->set_value(true); // returns control back to raft replication thread
+ }
}
return true;
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 31bf29e3..5036d6c8 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -127,6 +127,7 @@ int HttpServer::create_listener() {
}
ctx.globalconf->server_name = h2o_strdup(nullptr, "", SIZE_MAX);
+ ctx.globalconf->http2.active_stream_window_size = ACTIVE_STREAM_WINDOW_SIZE;
accept_ctx->ctx = &ctx;
accept_ctx->hosts = config.hosts;
@@ -270,13 +271,6 @@ uint64_t HttpServer::find_route(const std::vector<std::string> & path_parts, con
void HttpServer::on_res_generator_dispose(void *self) {
//LOG(INFO) << "on_res_generator_dispose fires";
h2o_custom_generator_t* res_generator = static_cast<h2o_custom_generator_t*>(self);
-
- if(res_generator->rpath->async_res) {
- // for the handler to free any cached resources like an iterator
- res_generator->request->stream_state = "DISPOSE";
- res_generator->rpath->handler(*res_generator->request, *res_generator->response);
- }
-
destroy_request_response(res_generator->request, res_generator->response);
}
@@ -402,6 +396,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
if(req->proceed_req == nullptr) {
// Full request body is already available, so we don't care if handler is async or not
+ request->last_chunk_aggregate = true;
return process_request(request, response, rpath, h2o_handler);
} else {
// Only partial request body is available.
@@ -411,54 +406,76 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
req->write_req.cb = async_req_cb;
req->write_req.ctx = custom_generator;
- int is_end_entity = 0;
- req->proceed_req(req, req->entity.len, is_end_entity);
+ req->proceed_req(req, req->entity.len, H2O_SEND_STATE_IN_PROGRESS);
}
return 0;
}
+
int HttpServer::async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream) {
+ // NOTE: this callback is triggered multiple times by HTTP 2 but only once by HTTP 1
+ // This quirk is because of the underlying buffer/window sizes. We will have to deal with both cases.
h2o_custom_generator_t* custom_generator = static_cast<h2o_custom_generator_t*>(ctx);
http_req* request = custom_generator->request;
http_res* response = custom_generator->response;
- //LOG(INFO) << "async_req_cb, chunk.len: " << chunk.len << ", is_end_stream: " << is_end_stream;
-
- request->chunk_length = chunk.len;
-
std::string chunk_str(chunk.base, chunk.len);
+
request->body += chunk_str;
+ request->chunk_len += chunk.len;
+
+ LOG(INFO) << "async_req_cb, chunk.len=" << chunk.len << ", aggr_chunk_len=" << request->chunk_len
+ << ", is_end_stream=" << is_end_stream;
+
+ //LOG(INFO) << "request->body.size(): " << request->body.size() << ", request->chunk_len=" << request->chunk_len;
+
+ size_t CHUNK_LIMIT = 0;
+ if(request->is_http_v1()) {
+ CHUNK_LIMIT = ACTIVE_STREAM_WINDOW_SIZE;
+ } else {
+ // For http v2, first window will include both initial request entity and as well as chunks.
+ CHUNK_LIMIT = request->first_chunk_aggregate ?
+ (ACTIVE_STREAM_WINDOW_SIZE - request->_req->entity.len) : ACTIVE_STREAM_WINDOW_SIZE;
+ }
bool async_req = custom_generator->rpath->async_req;
+ bool can_process_async = async_req && (request->chunk_len >= CHUNK_LIMIT);
+
+ /*if(is_end_stream == 1) {
+ LOG(INFO) << "is_end_stream=1";
+ }*/
- if(async_req || is_end_stream) {
- // Handler should be invoked for every chunk for async streaming requests
+ // first let's handle the case where we are ready to fire the request handler
+ if(can_process_async || is_end_stream) {
+ // For async streaming requests, handler should be invoked for every aggregated chunk
// For a non streaming request, buffer body and invoke only at the end
- // default value for response is NON_STREAMING
- // for streaming requests, we have to set: START, IN_PROGRESS or END
- if(async_req) {
- if(is_end_stream) {
- request->stream_state = "END";
- } else if(request->stream_state == "NON_STREAMING") {
- // `NON_STREAMING` default value indicates first chunk of streaming body
- request->stream_state = "START";
- } else {
- request->stream_state = "IN_PROGRESS";
- }
+ if(request->first_chunk_aggregate) {
+ request->first_chunk_aggregate = false;
}
+ // default value for last_chunk_aggregate is false
+ request->last_chunk_aggregate = (is_end_stream == 1);
process_request(request, response, custom_generator->rpath, custom_generator->h2o_handler);
+ return 0;
+ }
+
+ // we are not ready to fire the request handler, so that means we need to buffer the request further
+ // this could be because we are a) dealing with a HTTP v1 request or b) a synchronous request
+
+ if(request->is_http_v1()) {
+ // http v1 callbacks fire on small chunk sizes, so fetch more to match window size of http v2 buffer
+ size_t written = chunk.len;
+ request->_req->proceed_req(request->_req, written, H2O_SEND_STATE_IN_PROGRESS);
}
- if(!is_end_stream && !async_req) {
+ if(!async_req) {
+ // progress ONLY non-streaming type request body since
// streaming requests will call proceed_req in an async fashion
- // so we have to handle this only for non streaming
size_t written = chunk.len;
- int stream_ended = 0;
- request->_req->proceed_req(request->_req, written, stream_ended);
+ request->_req->proceed_req(request->_req, written, H2O_SEND_STATE_IN_PROGRESS);
}
return 0;
@@ -474,7 +491,6 @@ int HttpServer::process_request(http_req* request, http_res* response, route_pat
}
// for reads, we will invoke the request handler and handle response as well
-
(rpath->handler)(*request, *response);
if(!rpath->async_res) {
@@ -551,9 +567,12 @@ void HttpServer::response_proceed(h2o_generator_t *generator, h2o_req_t *req) {
if (custom_generator->rpath->async_req &&
custom_generator->request->_req && custom_generator->request->_req->proceed_req) {
- int is_end_stream = (custom_generator->response->final) ? 1 : 0;
- size_t written = custom_generator->request->chunk_length;
- custom_generator->request->_req->proceed_req(custom_generator->request->_req, written, is_end_stream);
+ size_t written = custom_generator->request->chunk_len;
+ custom_generator->request->chunk_len = 0;
+ auto stream_state = (custom_generator->response->final) ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;
+
+ // NOTE: `written` is ignored by http1.1 implementation, so it's meant only for http 2 and http 3
+ custom_generator->request->_req->proceed_req(custom_generator->request->_req, written, stream_state);
} else {
custom_generator->rpath->handler(*custom_generator->request, *custom_generator->response);
@@ -561,8 +580,13 @@ void HttpServer::response_proceed(h2o_generator_t *generator, h2o_req_t *req) {
}
void HttpServer::stream_response(http_req& request, http_res& response) {
+ //LOG(INFO) << "stream_response called";
if(request._req == nullptr) {
- // serialized request or underlying request is no longer available
+ // raft log replay
+ if(response.promise) {
+ // returns control back to raft replication thread
+ response.promise->set_value(true);
+ }
destroy_request_response(&request, &response);
return;
}
@@ -570,7 +594,8 @@ void HttpServer::stream_response(http_req& request, http_res& response) {
h2o_req_t* req = request._req;
h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t *>(response.generator);
- if (request.stream_state == "START" || request.stream_state == "NON_STREAMING") {
+ if (req->res.status == 0) {
+ response.status_code = (response.status_code == 0) ? 503 : response.status_code; // just to be sure
req->res.status = response.status_code;
req->res.reason = http_res::get_status_reason(response.status_code);
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL,
@@ -579,13 +604,17 @@ void HttpServer::stream_response(http_req& request, http_res& response) {
h2o_start_response(req, &custom_generator->super);
}
- custom_generator->response->final = (request.stream_state == "END" || request.stream_state == "NON_STREAMING");
-
h2o_iovec_t body = h2o_strdup(&req->pool, response.body.c_str(), SIZE_MAX);
response.body = "";
+ custom_generator->response->final = request.last_chunk_aggregate;
const h2o_send_state_t state = custom_generator->response->final ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;
h2o_send(req, &body, 1, state);
+
+ if(response.promise) {
+ // returns control back to raft replication thread
+ response.promise->set_value(true);
+ }
}
void HttpServer::destroy_request_response(http_req* request, http_res* response) {
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index e3057afc..12f271f2 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -191,7 +191,8 @@ void ReplicationState::on_apply(braft::Iterator& iter) {
request = new http_req;
request->deserialize(iter.data().to_string());
- request->_req = nullptr; // indicates remote request
+
+ //LOG(INFO) << "Parsed request from the log, body_size: " << request->body.size();
}
if(request->_req == nullptr && request->body == "INIT_SNAPSHOT") {