-
Notifications
You must be signed in to change notification settings - Fork 4
add Timeplus APIs #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| #include <timeplus/timeplus.h> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| using namespace timeplus; | ||
|
|
||
| /// Stream to insert is created with DDL: | ||
| /// `CREATE STREAM insert_examples(i uint64, v string)` | ||
| const std::string TABLE_NAME = "insert_examples"; | ||
|
|
||
| int main() { | ||
| TimeplusConfig config; | ||
| config.client_options.endpoints.push_back({"localhost", 8463}); | ||
| config.max_connections = 3; | ||
| config.max_retries = 10; | ||
| config.wait_time_before_retry_ms = 1000; | ||
| config.task_executors = 1; | ||
|
|
||
| Timeplus tp{std::move(config)}; | ||
|
|
||
| auto block = std::make_shared<Block>(); | ||
|
|
||
| auto col_i = std::make_shared<ColumnUInt64>(); | ||
| col_i->Append(5); | ||
| col_i->Append(7); | ||
| block->AppendColumn("i", col_i); | ||
|
|
||
| auto col_v = std::make_shared<ColumnString>(); | ||
| col_v->Append("five"); | ||
| col_v->Append("seven"); | ||
| block->AppendColumn("v", col_v); | ||
|
|
||
| /// Use synchronous insert API. | ||
| auto insert_result = tp.Insert(TABLE_NAME, block, /*idempotent_id=*/"block-1"); | ||
| if (insert_result.ok()) { | ||
| std::cout << "Synchronous insert suceeded." << std::endl; | ||
| } else { | ||
| std::cout << "Synchronous insert failed: code=" << insert_result.err_code << " msg=" << insert_result.err_msg << std::endl; | ||
| } | ||
|
|
||
| /// Use asynchrounous insert API. | ||
| std::atomic<bool> done = false; | ||
| tp.InsertAsync(TABLE_NAME, block, /*idempotent_id=*/"block-2", [&done](const BaseResult& result) { | ||
| const auto& async_insert_result = static_cast<const InsertResult&>(result); | ||
| if (async_insert_result.ok()) { | ||
| std::cout << "Asynchronous insert suceeded." << std::endl; | ||
| } else { | ||
| std::cout << "Asynchronous insert failed: code=" << async_insert_result.err_code << " msg=" << async_insert_result.err_msg | ||
| << std::endl; | ||
| } | ||
| done = true; | ||
| }); | ||
|
|
||
| while (!done) { | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or 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,11 @@ | ||
| ADD_EXECUTABLE (insert-async-test | ||
| main.cpp | ||
| ) | ||
|
|
||
| TARGET_LINK_LIBRARIES (insert-async-test | ||
| timeplus-cpp-lib | ||
| ) | ||
|
|
||
| IF (MSVC) | ||
| TARGET_LINK_LIBRARIES (insert-async-test Crypt32) | ||
| ENDIF() |
This file contains hidden or 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,122 @@ | ||
| #include <timeplus/blocking_queue.h> | ||
| #include <timeplus/timeplus.h> | ||
|
|
||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <thread> | ||
|
|
||
| using namespace timeplus; | ||
|
|
||
| const size_t INSERT_BLOCKS = 100'000; | ||
| const size_t BLOCKS_PER_BATCH = 1000; | ||
|
|
||
| const std::vector<std::pair<std::string, uint16_t>> HOST_PORTS = { | ||
| /// Single instance | ||
| {"localhost", 8463}, | ||
| /// Cluster nodes | ||
| {"localhost", 18463}, | ||
| {"localhost", 28463}, | ||
| {"localhost", 38463}, | ||
| }; | ||
|
|
||
| void prepareTable() { | ||
| ClientOptions options; | ||
| for (const auto& [host, port] : HOST_PORTS) { | ||
|
chenziliang marked this conversation as resolved.
|
||
| options.endpoints.push_back({host, port}); | ||
| } | ||
|
|
||
| Client client{options}; | ||
| client.Execute("DROP STREAM IF EXISTS insert_async_test;"); | ||
| client.Execute("CREATE STREAM insert_async_test (i uint64, s string);"); | ||
| } | ||
|
|
||
| auto timestamp() { | ||
| auto now = std::chrono::system_clock::now(); | ||
| std::time_t now_time = std::chrono::system_clock::to_time_t(now); | ||
| std::tm* local_time = std::localtime(&now_time); | ||
| return std::put_time(local_time, "%Y-%m-%d %H:%M:%S"); | ||
| } | ||
|
|
||
| int main() { | ||
| prepareTable(); | ||
|
|
||
| TimeplusConfig config; | ||
| for (const auto& [host, port] : HOST_PORTS) { | ||
| config.client_options.endpoints.push_back({host, port}); | ||
| } | ||
| config.max_connections = 4; | ||
| config.max_retries = 5; | ||
| config.task_executors = 4; | ||
| config.task_queue_capacity = BLOCKS_PER_BATCH; /// use large input queue to avoid deadlock on retry failure | ||
|
|
||
| Timeplus tp{std::move(config)}; | ||
|
|
||
| auto block = std::make_shared<Block>(); | ||
| auto col_i = std::make_shared<ColumnUInt64>(std::vector<uint64_t>{5, 7, 4, 8}); | ||
| auto col_s = std::make_shared<ColumnString>( | ||
| std::vector<std::string>{"Before my bed, the moon is bright,", "I think that it is frost on the ground.", | ||
| "I raise my head to gaze at the bright moon,", "And lower it to think of my hometown."}); | ||
| block->AppendColumn("i", col_i); | ||
| block->AppendColumn("s", col_s); | ||
|
|
||
| /// Queue to store failed inserts which need to be resent. | ||
| BlockingQueue<std::pair<size_t, BlockPtr>> insert_failure(BLOCKS_PER_BATCH); | ||
| std::atomic<size_t> insert_success_count{0}; | ||
|
|
||
| auto handle_insert_result = [&insert_failure, &insert_success_count](size_t block_id, const InsertResult& result) { | ||
| if (result.ok()) { | ||
| insert_success_count.fetch_add(1); | ||
| } else { | ||
| std::cout << "[" << timestamp() << "]\t Failed to insert block: insert_id=" << block_id << " err=" << result.err_msg | ||
| << std::endl; | ||
| insert_failure.emplace(block_id, result.block); | ||
| } | ||
| }; | ||
|
|
||
| auto async_insert_block = [&tp, &handle_insert_result](size_t block_id, BlockPtr block) { | ||
| tp.InsertAsync(/*table_name=*/"insert_async_test", block, [block_id, &handle_insert_result](const BaseResult& result) { | ||
| const auto& insert_result = static_cast<const InsertResult&>(result); | ||
| handle_insert_result(block_id, insert_result); | ||
| }); | ||
| }; | ||
|
|
||
| auto start_time = std::chrono::high_resolution_clock::now(); | ||
| auto last_time = start_time; | ||
| for (size_t batch = 0; batch < INSERT_BLOCKS / BLOCKS_PER_BATCH; ++batch) { | ||
| insert_success_count = 0; | ||
| /// Insert blocks asynchronously. | ||
| for (size_t i = 0; i < BLOCKS_PER_BATCH; ++i) { | ||
| async_insert_block(i, block); | ||
| } | ||
|
|
||
| /// Wait for all blocks of the batch are inserted. | ||
| while (insert_success_count.load() != BLOCKS_PER_BATCH) { | ||
| if (!insert_failure.empty()) { | ||
| /// Re-insert the failed blocks | ||
| auto blocks = insert_failure.drain(); | ||
| for (auto &[i, b] : blocks) { | ||
| async_insert_block(i, b); | ||
| } | ||
| } | ||
|
|
||
| std::this_thread::yield(); | ||
| } | ||
|
|
||
| /// Print insert statistics of the batch. | ||
| auto current_time = std::chrono::high_resolution_clock::now(); | ||
| std::chrono::duration<double> elapsed = current_time - last_time; | ||
| last_time = current_time; | ||
| std::cout << "[" << timestamp() << "]\t" << (batch + 1) * BLOCKS_PER_BATCH << " blocks inserted\telapsed = " << elapsed.count() | ||
| << " sec\teps = " << static_cast<double>(BLOCKS_PER_BATCH * block->GetRowCount()) / elapsed.count() << std::endl; | ||
| } | ||
|
|
||
| /// Print summary. | ||
| auto current_time = std::chrono::high_resolution_clock::now(); | ||
| std::chrono::duration<double> elapsed = current_time - start_time; | ||
| std::cout << "\nInsert Done. Total Events = " << INSERT_BLOCKS * block->GetRowCount() << " Total Time = " << elapsed.count() << " sec" | ||
| << std::endl; | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or 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,11 @@ | ||
| ADD_EXECUTABLE (insert-test | ||
| main.cpp | ||
| ) | ||
|
|
||
| TARGET_LINK_LIBRARIES (insert-test | ||
| timeplus-cpp-lib | ||
| ) | ||
|
|
||
| IF (MSVC) | ||
| TARGET_LINK_LIBRARIES (insert-test Crypt32) | ||
| ENDIF() |
This file contains hidden or 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,85 @@ | ||
| #include <timeplus/timeplus.h> | ||
|
|
||
| #include <chrono> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
|
|
||
| using namespace timeplus; | ||
|
|
||
| const size_t INSERT_BLOCKS = 100'000; | ||
|
|
||
| const std::vector<std::pair<std::string, uint16_t>> HOST_PORTS = { | ||
| /// Single instance | ||
| {"localhost", 8463}, | ||
| /// Cluster nodes | ||
| {"localhost", 18463}, | ||
| {"localhost", 28463}, | ||
| {"localhost", 38463}, | ||
| }; | ||
|
|
||
| void prepareTable() { | ||
| ClientOptions options; | ||
| for (const auto& [host, port] : HOST_PORTS) { | ||
| options.endpoints.push_back({host, port}); | ||
| } | ||
|
|
||
| Client client{options}; | ||
| client.Execute("DROP STREAM IF EXISTS insert_test;"); | ||
| client.Execute("CREATE STREAM insert_test (i uint64, s string);"); | ||
| } | ||
|
|
||
| auto timestamp() { | ||
| auto now = std::chrono::system_clock::now(); | ||
| std::time_t now_time = std::chrono::system_clock::to_time_t(now); | ||
| std::tm* local_time = std::localtime(&now_time); | ||
| return std::put_time(local_time, "%Y-%m-%d %H:%M:%S"); | ||
| } | ||
|
|
||
| int main() { | ||
| prepareTable(); | ||
|
|
||
| TimeplusConfig config; | ||
| for (const auto& [host, port] : HOST_PORTS) { | ||
| config.client_options.endpoints.push_back({host, port}); | ||
| } | ||
| config.max_connections = 1; | ||
| config.max_retries = 5; | ||
| config.task_executors = 0; | ||
| Timeplus tp{std::move(config)}; | ||
|
|
||
| auto block = std::make_shared<Block>(); | ||
| auto col_i = std::make_shared<ColumnUInt64>(std::vector<uint64_t>{5, 7, 4, 8}); | ||
| auto col_s = std::make_shared<ColumnString>( | ||
| std::vector<std::string>{"Before my bed, the moon is bright,", "I think that it is frost on the ground.", | ||
| "I raise my head to gaze at the bright moon,", "And lower it to think of my hometown."}); | ||
| block->AppendColumn("i", col_i); | ||
| block->AppendColumn("s", col_s); | ||
|
|
||
| auto start_time = std::chrono::high_resolution_clock::now(); | ||
| auto last_time = start_time; | ||
| for (size_t i = 1; i <= INSERT_BLOCKS; ++i) { | ||
| while (true) { | ||
| try { | ||
| tp.Insert("insert_test", block); | ||
| break; | ||
| } catch (const std::exception& ex) { | ||
| std::cout << timestamp() << "\t Failed to insert block " << i << " : " << ex.what() << std::endl; | ||
| } | ||
| } | ||
|
|
||
| if (i % 1000 == 0) { | ||
| auto current_time = std::chrono::high_resolution_clock::now(); | ||
| std::chrono::duration<double> elapsed = current_time - last_time; | ||
| last_time = current_time; | ||
| std::cout << "[" << timestamp() << "]\t" << i << " blocks inserted\telapsed = " << elapsed.count() | ||
| << " sec\teps = " << 1000.0 * block->GetRowCount() / elapsed.count() << std::endl; | ||
| } | ||
| } | ||
|
|
||
| auto current_time = std::chrono::high_resolution_clock::now(); | ||
| std::chrono::duration<double> elapsed = current_time - start_time; | ||
| std::cout << "\nInsert Done. Total Events = " << INSERT_BLOCKS * block->GetRowCount() << " Total Time = " << elapsed.count() << " sec" | ||
| << std::endl; | ||
|
|
||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.