-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b2712c
commit 396d941
Showing
23 changed files
with
1,061 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** Copyright 2020 Alibaba Group Holding Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include "stdlib.h" | ||
|
||
#include "flex/engines/hqps_db/core/utils/hqps_utils.h" | ||
#include "flex/engines/http_server/service/proxy_service.h" | ||
#include "flex/utils/service_utils.h" | ||
|
||
#include <boost/program_options.hpp> | ||
|
||
#include <glog/logging.h> | ||
|
||
namespace bpo = boost::program_options; | ||
|
||
namespace gs { | ||
// Function to parse endpoints from a string | ||
bool parse_endpoints(const std::string& input_string, | ||
std::vector<std::pair<std::string, uint16_t>>& endpoints) { | ||
std::istringstream iss(input_string); | ||
std::string endpoint; | ||
|
||
while (std::getline(iss, endpoint, ',')) { | ||
// Split the endpoint into host and port using ':' | ||
size_t delimiter_pos = endpoint.find(':'); | ||
if (delimiter_pos == std::string::npos) { | ||
std::cerr << "Invalid endpoint: " << endpoint << ", missing delimiter ':'" | ||
<< std::endl; | ||
continue; | ||
} | ||
|
||
std::string host = endpoint.substr(0, delimiter_pos); | ||
std::string port_str = endpoint.substr(delimiter_pos + 1); | ||
uint16_t port; | ||
try { | ||
port = std::stoull(port_str); | ||
} catch (const std::invalid_argument& e) { | ||
LOG(ERROR) << "Invalid port: " << port_str << ", must be a number" | ||
<< std::endl; | ||
return false; | ||
} | ||
|
||
// Check for valid port range | ||
if (port < 1 || port > 65535) { | ||
LOG(ERROR) << "Invalid port: " << port << ", must be between 1 and 65535" | ||
<< std::endl; | ||
return false; | ||
} | ||
endpoints.push_back({host, port}); | ||
} | ||
return true; | ||
} | ||
} // namespace gs | ||
|
||
/** | ||
* The main entrance for ProxyServer. | ||
*/ | ||
int main(int argc, char** argv) { | ||
bpo::options_description desc("Usage:"); | ||
desc.add_options()("help,h", "Display help messages")( | ||
"endpoints,e", bpo::value<std::string>()->required(), | ||
"The endpoints of the proxy server, e.g., {ip}:{port},{ip}:{port},...")( | ||
"heartbeat-interval,i", bpo::value<int>()->default_value(1), | ||
"The interval of heartbeat check in seconds"); | ||
|
||
setenv("TZ", "Asia/Shanghai", 1); | ||
tzset(); | ||
|
||
bpo::variables_map vm; | ||
bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm); | ||
bpo::notify(vm); | ||
|
||
if (vm.count("help")) { | ||
std::cout << desc << std::endl; | ||
return 0; | ||
} | ||
|
||
if (!vm.count("endpoints")) { | ||
LOG(FATAL) << "endpoints is not specified"; | ||
return 0; | ||
} | ||
std::vector<std::pair<std::string, uint16_t>> endpoints; | ||
if (!gs::parse_endpoints(vm["endpoints"].as<std::string>(), endpoints)) { | ||
LOG(FATAL) << "Failed to parse endpoints"; | ||
return 0; | ||
} | ||
|
||
LOG(INFO) << "got endpoints of size: " << endpoints.size() | ||
<< ", :" << gs::to_string(endpoints); | ||
|
||
uint32_t shard_num = 1; | ||
uint16_t http_port = 9999; | ||
|
||
if (!server::ProxyService::get().init(shard_num, http_port, endpoints).ok()) { | ||
LOG(FATAL) << "Failed to init ProxyService"; | ||
return 0; | ||
} | ||
server::ProxyService::get().run_and_wait_for_exit(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** Copyright 2020 Alibaba Group Holding Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "flex/engines/http_server/actor/proxy_actor.act.h" | ||
#include "flex/engines/http_server/service/proxy_service.h" | ||
|
||
#include "nlohmann/json.hpp" | ||
|
||
#include <seastar/core/print.hh> | ||
|
||
namespace server { | ||
|
||
proxy_actor::~proxy_actor() { | ||
// finalization | ||
// ... | ||
} | ||
|
||
proxy_actor::proxy_actor(hiactor::actor_base* exec_ctx, | ||
const hiactor::byte_t* addr) | ||
: hiactor::actor(exec_ctx, addr) { | ||
set_max_concurrency(1); // set max concurrency for task reentrancy (stateful) | ||
// initialization | ||
// ... | ||
} | ||
|
||
seastar::future<query_result> proxy_actor::do_query( | ||
proxy_request&& request_payload) { | ||
auto& request = request_payload.content; | ||
VLOG(10) << "proxy_actor::forward_request, method: " << request->_method | ||
<< ", path: " << request->_url << ", query: " << request->content; | ||
|
||
// recover the old url with paramters in request | ||
auto& proxy_service = ProxyService::get(); | ||
auto& client = proxy_service.get_client(); | ||
return client | ||
.forward_request(request->_url, request->_method, request->content, | ||
request->_headers) | ||
.then([&proxy_service](HttpForwardingResponses&& content) { | ||
if (content.size() == 0) { | ||
return seastar::make_exception_future<query_result>( | ||
std::runtime_error("Got no responses when forwarding request " | ||
"to interactive servers.")); | ||
} | ||
// Check all responses are ok, if not ok, return error | ||
seastar::sstring res_string; | ||
for (size_t i = 0; i < content.size(); ++i) { | ||
auto& response = content[i]; | ||
if (response.first != 200) { | ||
LOG(ERROR) << "Got error response when forwarding request " | ||
"to interactive servers at index: " | ||
<< std::to_string(i) << ", endpoint: " | ||
<< proxy_service.get_endpoints()[i].first + ":" | ||
<< std::to_string( | ||
proxy_service.get_endpoints()[i].second) | ||
<< std::to_string(response.first) + ", msg:" | ||
<< response.second; | ||
} else { | ||
// Select the first/last response as the final result? or check | ||
// whether all responses are the same? | ||
res_string = response.second; | ||
break; | ||
} | ||
} | ||
return seastar::make_ready_future<query_result>( | ||
query_result{std::move(res_string)}); | ||
}); | ||
} | ||
|
||
} // namespace server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** Copyright 2020 Alibaba Group Holding Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef ENGINES_HTTP_SERVER_ACTOR_PROXY_ACTOR_H_ | ||
#define ENGINES_HTTP_SERVER_ACTOR_PROXY_ACTOR_H_ | ||
|
||
|
||
#include "flex/engines/http_server/types.h" | ||
|
||
#include <hiactor/core/actor-template.hh> | ||
#include <hiactor/util/data_type.hh> | ||
#include <seastar/http/httpd.hh> | ||
|
||
namespace server { | ||
|
||
class ANNOTATION(actor:impl) proxy_actor : public hiactor::actor { | ||
|
||
public: | ||
proxy_actor(hiactor::actor_base* exec_ctx, const hiactor::byte_t* addr); | ||
~proxy_actor() override; | ||
|
||
seastar::future<query_result> ANNOTATION(actor:method) do_query(proxy_request&& param); | ||
|
||
// DECLARE_RUN_QUERIES; | ||
/// Declare `do_work` func here, no need to implement. | ||
ACTOR_DO_WORK() | ||
|
||
private: | ||
int32_t your_private_members_ = 0; | ||
}; | ||
} | ||
|
||
#endif // ENGINES_HTTP_SERVER_ACTOR_PROXY_ACTOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.