|
| 1 | +/** |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <memory> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include "Session.h" |
| 26 | +#include "SessionBuilder.h" |
| 27 | +#include "SessionDataSet.h" |
| 28 | +#include "TableSessionBuilder.h" |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +void RunTreeExample() { |
| 33 | + try { |
| 34 | + std::vector<std::string> node_urls = { |
| 35 | + "127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; |
| 36 | + |
| 37 | + auto builder = std::make_shared<SessionBuilder>(); |
| 38 | + auto session = std::shared_ptr<Session>( |
| 39 | + builder->username("root") |
| 40 | + ->password("root") |
| 41 | + ->nodeUrls(node_urls) |
| 42 | + ->build()); |
| 43 | + |
| 44 | + session->open(); |
| 45 | + if (!session->checkTimeseriesExists("root.test.d1.s1")) { |
| 46 | + session->createTimeseries("root.test.d1.s1", TSDataType::INT64, |
| 47 | + TSEncoding::RLE, CompressionType::SNAPPY); |
| 48 | + } |
| 49 | + session->deleteTimeseries("root.test.d1.s1"); |
| 50 | + session->close(); |
| 51 | + } catch (const std::exception& e) { |
| 52 | + std::cout << "Caught exception: " << e.what() << std::endl; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void RunTableExample() { |
| 57 | + try { |
| 58 | + std::vector<std::string> node_urls = { |
| 59 | + "127.0.0.1:6669", "127.0.0.1:6668", "127.0.0.1:6667"}; |
| 60 | + |
| 61 | + auto builder = std::make_shared<TableSessionBuilder>(); |
| 62 | + auto session = std::shared_ptr<TableSession>( |
| 63 | + builder->username("root") |
| 64 | + ->password("root") |
| 65 | + ->nodeUrls(node_urls) |
| 66 | + ->build()); |
| 67 | + |
| 68 | + session->open(); |
| 69 | + |
| 70 | + session->executeNonQueryStatement("DROP DATABASE IF EXISTS db1"); |
| 71 | + session->executeNonQueryStatement("CREATE DATABASE db1"); |
| 72 | + session->executeNonQueryStatement("DROP DATABASE IF EXISTS db2"); |
| 73 | + session->executeNonQueryStatement("CREATE DATABASE db2"); |
| 74 | + |
| 75 | + session->close(); |
| 76 | + } catch (const std::exception& e) { |
| 77 | + std::cout << "Caught exception: " << e.what() << std::endl; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// Example: continuously write/query data so you can manually stop a node |
| 83 | +// to test client failover behavior. |
| 84 | +void RunResilienceExample() { |
| 85 | + try { |
| 86 | + std::vector<std::string> node_urls = { |
| 87 | + "127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; |
| 88 | + |
| 89 | + auto builder = std::make_shared<SessionBuilder>(); |
| 90 | + auto session = std::shared_ptr<Session>( |
| 91 | + builder->username("root") |
| 92 | + ->password("root") |
| 93 | + ->nodeUrls(node_urls) |
| 94 | + ->build()); |
| 95 | + |
| 96 | + session->open(); |
| 97 | + |
| 98 | + if (!session->checkTimeseriesExists("root.resilience.d1.s1")) { |
| 99 | + session->createTimeseries("root.resilience.d1.s1", TSDataType::INT64, |
| 100 | + TSEncoding::RLE, CompressionType::SNAPPY); |
| 101 | + } |
| 102 | + |
| 103 | + std::cout << "Starting resilience test. " |
| 104 | + "Stop one node manually to see failover handling..." |
| 105 | + << std::endl; |
| 106 | + |
| 107 | + for (int i = 0; i < 60; ++i) { // run ~60 seconds |
| 108 | + int64_t timestamp = std::chrono::system_clock::now().time_since_epoch() / |
| 109 | + std::chrono::milliseconds(1); |
| 110 | + std::string value = to_string(i); |
| 111 | + const char* value_cstr = value.c_str(); |
| 112 | + |
| 113 | + try { |
| 114 | + session->insertRecord("root.resilience.d1", timestamp, |
| 115 | + {"s1"}, {TSDataType::INT64}, |
| 116 | + {const_cast<char*>(value_cstr)}); |
| 117 | + std::cout << "[Insert] ts=" << timestamp << ", value=" << value |
| 118 | + << std::endl; |
| 119 | + |
| 120 | + auto dataset = session->executeQueryStatement( |
| 121 | + "SELECT s1 FROM root.resilience.d1 LIMIT 1"); |
| 122 | + std::cout << "[Query] Got dataset: " |
| 123 | + << (dataset ? "Success" : "Null") << std::endl; |
| 124 | + |
| 125 | + } catch (const std::exception& e) { |
| 126 | + std::cout << "Caught exception during resilience loop: " << e.what() |
| 127 | + << std::endl; |
| 128 | + } |
| 129 | + |
| 130 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 131 | + } |
| 132 | + |
| 133 | + session->close(); |
| 134 | + } catch (const std::exception& e) { |
| 135 | + std::cout << "Caught exception in RunResilienceExample: " << e.what() |
| 136 | + << std::endl; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace |
| 141 | + |
| 142 | +int main() { |
| 143 | + //RunTreeExample(); |
| 144 | + //RunTableExample(); |
| 145 | + RunResilienceExample(); |
| 146 | + return 0; |
| 147 | +} |
0 commit comments