Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "repo.hpp" |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 21 | #include "storage/sqlite-storage.hpp" |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 22 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 23 | #include <ndn-cxx/util/logger.hpp> |
| 24 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 25 | namespace repo { |
| 26 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 27 | NDN_LOG_INIT(repo); |
| 28 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 29 | RepoConfig |
| 30 | parseConfig(const std::string& configPath) |
| 31 | { |
| 32 | if (configPath.empty()) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 33 | NDN_LOG_DEBUG("configuration file path is empty"); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | std::ifstream fin(configPath.c_str()); |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 37 | if (!fin.is_open()) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 38 | BOOST_THROW_EXCEPTION(Repo::Error("failed to open configuration file '" + configPath + "'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 39 | |
| 40 | using namespace boost::property_tree; |
| 41 | ptree propertyTree; |
| 42 | try { |
| 43 | read_info(fin, propertyTree); |
| 44 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 45 | catch (const ptree_error& e) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 46 | BOOST_THROW_EXCEPTION(Repo::Error("failed to read configuration file '" + configPath + "'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ptree repoConf = propertyTree.get_child("repo"); |
| 50 | |
| 51 | RepoConfig repoConfig; |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 52 | repoConfig.repoConfigPath = configPath; |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 53 | |
| 54 | ptree dataConf = repoConf.get_child("data"); |
Alexander Afanasyev | 81203b9 | 2017-11-09 13:53:56 -0500 | [diff] [blame] | 55 | for (const auto& section : dataConf) { |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 56 | if (section.first == "prefix") |
| 57 | repoConfig.dataPrefixes.push_back(Name(section.second.get_value<std::string>())); |
| 58 | else if (section.first == "registration-subset") |
| 59 | repoConfig.registrationSubset = section.second.get_value<int>(); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 60 | else |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 61 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + section.first + "' option in 'data' section in " |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 62 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | ptree commandConf = repoConf.get_child("command"); |
Alexander Afanasyev | 81203b9 | 2017-11-09 13:53:56 -0500 | [diff] [blame] | 66 | for (const auto& section : commandConf) { |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 67 | if (section.first == "prefix") |
| 68 | repoConfig.repoPrefixes.push_back(Name(section.second.get_value<std::string>())); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 69 | else |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 70 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + section.first + "' option in 'command' section in " |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 71 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alexander Afanasyev | 81203b9 | 2017-11-09 13:53:56 -0500 | [diff] [blame] | 74 | auto tcpBulkInsert = repoConf.get_child_optional("tcp_bulk_insert"); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 75 | bool isTcpBulkEnabled = false; |
| 76 | std::string host = "localhost"; |
| 77 | std::string port = "7376"; |
Alexander Afanasyev | 81203b9 | 2017-11-09 13:53:56 -0500 | [diff] [blame] | 78 | if (tcpBulkInsert) { |
| 79 | for (const auto& section : *tcpBulkInsert) { |
| 80 | isTcpBulkEnabled = true; |
Alexander Afanasyev | 81203b9 | 2017-11-09 13:53:56 -0500 | [diff] [blame] | 81 | if (section.first == "host") { |
| 82 | host = section.second.get_value<std::string>(); |
| 83 | } |
| 84 | else if (section.first == "port") { |
| 85 | port = section.second.get_value<std::string>(); |
| 86 | } |
| 87 | else |
| 88 | BOOST_THROW_EXCEPTION(Repo::Error("Unrecognized '" + section.first + "' option in 'tcp_bulk_insert' section in " |
| 89 | "configuration file '"+ configPath +"'")); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 90 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 91 | } |
| 92 | if (isTcpBulkEnabled) { |
| 93 | repoConfig.tcpBulkInsertEndpoints.push_back(std::make_pair(host, port)); |
| 94 | } |
| 95 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 96 | if (repoConf.get<std::string>("storage.method") != "sqlite") { |
| 97 | BOOST_THROW_EXCEPTION(Repo::Error("Only 'sqlite' storage method is supported")); |
| 98 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 99 | |
| 100 | repoConfig.dbPath = repoConf.get<std::string>("storage.path"); |
| 101 | |
| 102 | repoConfig.validatorNode = repoConf.get_child("validator"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 103 | |
Shuo Chen | 6e81812 | 2015-05-04 10:40:03 +0800 | [diff] [blame] | 104 | repoConfig.nMaxPackets = repoConf.get<uint64_t>("storage.max-packets"); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 105 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 106 | return repoConfig; |
| 107 | } |
| 108 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 109 | Repo::Repo(boost::asio::io_service& ioService, const RepoConfig& config) |
| 110 | : m_config(config) |
| 111 | , m_scheduler(ioService) |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 112 | , m_face(ioService) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 113 | , m_dispatcher(m_face, m_keyChain) |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 114 | , m_store(std::make_shared<SqliteStorage>(config.dbPath)) |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 115 | , m_storageHandle(*m_store) |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 116 | , m_validator(m_face) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 117 | , m_readHandle(m_face, m_storageHandle, m_config.registrationSubset) |
| 118 | , m_writeHandle(m_face, m_storageHandle, m_dispatcher, m_scheduler, m_validator) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 119 | , m_deleteHandle(m_face, m_storageHandle, m_dispatcher, m_scheduler, m_validator) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 120 | , m_tcpBulkInsertHandle(ioService, m_storageHandle) |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 121 | { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 122 | this->enableValidation(); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Shuo Chen | a12f528 | 2014-08-01 15:18:30 +0800 | [diff] [blame] | 126 | Repo::initializeStorage() |
| 127 | { |
| 128 | // Rebuild storage if storage checkpoin exists |
| 129 | ndn::time::steady_clock::TimePoint start = ndn::time::steady_clock::now(); |
Shuo Chen | a12f528 | 2014-08-01 15:18:30 +0800 | [diff] [blame] | 130 | ndn::time::steady_clock::TimePoint end = ndn::time::steady_clock::now(); |
| 131 | ndn::time::milliseconds cost = ndn::time::duration_cast<ndn::time::milliseconds>(end - start); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 132 | NDN_LOG_DEBUG("initialize storage cost: " << cost << "ms"); |
Shuo Chen | a12f528 | 2014-08-01 15:18:30 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 136 | Repo::enableListening() |
| 137 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 138 | for (const ndn::Name& dataPrefix : m_config.dataPrefixes) { |
| 139 | // ReadHandle performs prefix registration internally. |
| 140 | m_readHandle.listen(dataPrefix); |
| 141 | } |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 142 | for (const ndn::Name& cmdPrefix : m_config.repoPrefixes) { |
| 143 | m_face.registerPrefix(cmdPrefix, nullptr, |
| 144 | [] (const Name& cmdPrefix, const std::string& reason) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 145 | NDN_LOG_DEBUG("Command prefix " << cmdPrefix << " registration error: " << reason); |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 146 | BOOST_THROW_EXCEPTION(Error("Command prefix registration failed")); |
| 147 | }); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 148 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 149 | m_dispatcher.addTopPrefix(cmdPrefix); |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | for (const auto& ep : m_config.tcpBulkInsertEndpoints) { |
| 153 | m_tcpBulkInsertHandle.listen(ep.first, ep.second); |
| 154 | } |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 157 | void |
| 158 | Repo::enableValidation() |
| 159 | { |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 160 | m_validator.load(m_config.validatorNode, m_config.repoConfigPath); |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 163 | } // namespace repo |