From 2f4e7a00f63b3c56cb578aa76ed20c019cb9cbbb Mon Sep 17 00:00:00 2001 From: Calle Wilund Date: Wed, 29 Apr 2015 10:24:17 +0200 Subject: [PATCH] Use db/config object in main, database etc * Uses config object to augument/impl options parsing * Database now holds config obj * Commitlog can now be inited with global config obj. --- database.cc | 19 ++++++++++++------- database.hh | 9 ++++++--- db/commitlog/commitlog.cc | 9 +++++++++ db/commitlog/commitlog.hh | 3 +++ db/legacy_schema_tables.cc | 6 +++--- db/legacy_schema_tables.hh | 6 ++++-- main.cc | 35 ++++++++++++++++++++++++----------- 7 files changed, 61 insertions(+), 26 deletions(-) diff --git a/database.cc b/database.cc index 32cb066797..0149bd82d1 100644 --- a/database.cc +++ b/database.cc @@ -10,6 +10,7 @@ #include "db/consistency_level.hh" #include "db/serializer.hh" #include "db/commitlog/commitlog.hh" +#include "db/config.hh" #include "to_string.hh" #include "query-result-writer.hh" @@ -181,7 +182,11 @@ future<> column_family::populate(sstring sstdir) { } -database::database() { +database::database() : database(db::config()) +{} + +database::database(const db::config& cfg) : _cfg(std::make_unique(cfg)) +{ db::system_keyspace::make(*this); } @@ -224,15 +229,15 @@ future<> database::populate(sstring datadir) { } future<> -database::init_from_data_directory(sstring datadir) { - return populate(datadir).then([this, datadir]() { - return init_commitlog(datadir); +database::init_from_data_directory() { + return populate(_cfg->data_file_directories()).then([this]() { + return init_commitlog(); }); } future<> -database::init_commitlog(sstring datadir) { - auto logdir = datadir + "/work" + std::to_string(engine().cpu_id()); +database::init_commitlog() { + auto logdir = _cfg->commitlog_directory() + "/work" + std::to_string(engine().cpu_id()); return engine().file_type(logdir).then([this, logdir](auto type) { if (type && type.value() != directory_entry_type::directory) { @@ -242,7 +247,7 @@ database::init_commitlog(sstring datadir) { throw std::runtime_error("Could not create directory " + logdir); } - db::commitlog::config cfg; + db::commitlog::config cfg(*_cfg); cfg.commit_log_location = logdir; // TODO: real config. Real logging. // Right now we just set this up to use a single segment diff --git a/database.hh b/database.hh index cc64628b8b..b2996afe46 100644 --- a/database.hh +++ b/database.hh @@ -50,6 +50,7 @@ template class serializer; class commitlog; +class config; } struct column_family { @@ -126,11 +127,14 @@ class database { std::unordered_map _column_families; std::unordered_map, utils::UUID, utils::tuple_hash> _ks_cf_to_uuid; std::unique_ptr _commitlog; + std::unique_ptr _cfg; - future<> init_commitlog(sstring datadir); + future<> init_commitlog(); future<> apply_in_memory(const mutation&); + future<> populate(sstring datadir); public: database(); + database(const db::config&); database(database&&) = default; ~database(); @@ -138,8 +142,7 @@ public: return _commitlog.get(); } - future<> init_from_data_directory(sstring datadir); - future<> populate(sstring datadir); + future<> init_from_data_directory(); keyspace& add_keyspace(sstring name, keyspace k); /** Adds cf with auto-generated UUID. */ diff --git a/db/commitlog/commitlog.cc b/db/commitlog/commitlog.cc index a526e75572..5ae05d1903 100644 --- a/db/commitlog/commitlog.cc +++ b/db/commitlog/commitlog.cc @@ -35,6 +35,15 @@ #include "core/rwlock.hh" #include "net/byteorder.hh" #include "commitlog.hh" +#include "db/config.hh" + +db::commitlog::config::config(const db::config& cfg) + : commit_log_location(cfg.commitlog_directory()) + , commitlog_total_space_in_mb(cfg.commitlog_total_space_in_mb()) + , commitlog_segment_size_in_mb(cfg.commitlog_segment_size_in_mb()) + , commitlog_sync_period_in_ms(cfg.commitlog_sync_batch_window_in_ms()) + , mode(cfg.commitlog_sync() == "batch" ? sync_mode::BATCH : sync_mode::PERIODIC) +{} class db::commitlog::descriptor { public: diff --git a/db/commitlog/commitlog.hh b/db/commitlog/commitlog.hh index 87cd5e683b..491f7eee0e 100644 --- a/db/commitlog/commitlog.hh +++ b/db/commitlog/commitlog.hh @@ -33,6 +33,8 @@ namespace db { +class config; + typedef uint64_t segment_id_type; typedef uint64_t position_type; typedef utils::UUID cf_id_type; @@ -91,6 +93,7 @@ public: struct config { config() = default; config(const config&) = default; + config(const db::config&); sstring commit_log_location; uint64_t commitlog_total_space_in_mb = 0; // TODO: not respected yet. diff --git a/db/legacy_schema_tables.cc b/db/legacy_schema_tables.cc index b8eb0c9c72..b552d2101f 100644 --- a/db/legacy_schema_tables.cc +++ b/db/legacy_schema_tables.cc @@ -804,7 +804,7 @@ std::vector ALL { KEYSPACES, COLUMNFAMILIES, COLUMNS, TRIGGERS, USE * Keyspace metadata serialization/deserialization. */ - std::vector make_create_keyspace_mutations(lw_shared_ptr keyspace, api::timestamp_type timestamp, bool with_tables_and_types_and_functions) + std::vector make_create_keyspace_mutations(lw_shared_ptr<::config::ks_meta_data> keyspace, api::timestamp_type timestamp, bool with_tables_and_types_and_functions) { std::vector mutations; schema_ptr s = keyspaces(); @@ -863,7 +863,7 @@ std::vector ALL { KEYSPACES, COLUMNFAMILIES, COLUMNS, TRIGGERS, USE * * @param partition Keyspace attributes in serialized form */ - lw_shared_ptr create_keyspace_from_schema_partition(const std::pair>>& result) + lw_shared_ptr<::config::ks_meta_data> create_keyspace_from_schema_partition(const std::pair>>& result) { auto&& rs = result.second; if (rs->empty()) { @@ -874,7 +874,7 @@ std::vector ALL { KEYSPACES, COLUMNFAMILIES, COLUMNS, TRIGGERS, USE auto strategy_name = row.get_nonnull("strategy_class"); std::unordered_map strategy_options; bool durable_writes = row.get_nonnull("durable_writes"); - return make_lw_shared(keyspace_name, strategy_name, strategy_options, durable_writes); + return make_lw_shared<::config::ks_meta_data>(keyspace_name, strategy_name, strategy_options, durable_writes); } #if 0 diff --git a/db/legacy_schema_tables.hh b/db/legacy_schema_tables.hh index 8d0539ab76..91dec955f5 100644 --- a/db/legacy_schema_tables.hh +++ b/db/legacy_schema_tables.hh @@ -65,9 +65,11 @@ future<> merge_schema(service::storage_proxy& proxy, std::vector mutat future> merge_keyspaces(service::storage_proxy& proxy, schema_result&& before, schema_result&& after); -std::vector make_create_keyspace_mutations(lw_shared_ptr keyspace, api::timestamp_type timestamp, bool with_tables_and_types_and_functions = true); +std::vector make_create_keyspace_mutations(lw_shared_ptr<::config::ks_meta_data> keyspace, api::timestamp_type timestamp, bool with_tables_and_types_and_functions = true); -lw_shared_ptr create_keyspace_from_schema_partition(const std::pair>>& partition); +lw_shared_ptr<::config::ks_meta_data> create_keyspace_from_schema_partition(const std::pair>>& partition); + +mutation make_create_keyspace_mutation(lw_shared_ptr<::config::ks_meta_data> keyspace, api::timestamp_type timestamp, bool with_tables_and_types_and_functions = true); void add_table_to_schema_mutation(schema_ptr table, api::timestamp_type timestamp, bool with_columns_and_triggers, const partition_key& pkey, std::vector& mutations); diff --git a/main.cc b/main.cc index 93d7ad881a..ba1fa107f9 100644 --- a/main.cc +++ b/main.cc @@ -10,16 +10,28 @@ #include "transport/server.hh" #include "http/httpd.hh" #include "api/api.hh" +#include "db/config.hh" namespace bpo = boost::program_options; +static future<> +read_config(bpo::variables_map& opts, db::config& cfg) { + if (opts.count("options-file") == 0) { + return make_ready_future<>(); + } + return cfg.read_from_file(opts["options-file"].as()); +} + int main(int ac, char** av) { app_template app; - app.add_options() - ("cql-port", bpo::value()->default_value(9042), "CQL port") - ("thrift-port", bpo::value()->default_value(9160), "Thrift port") + auto opt_add = app.add_options(); + + auto cfg = make_lw_shared(); + cfg->add_options(opt_add) ("api-port", bpo::value()->default_value(10000), "Http Rest API port") - ("datadir", bpo::value()->default_value("/var/lib/cassandra/data"), "data directory"); + // TODO : default, always read? + ("options-file", bpo::value(), "cassandra.yaml file to read options from") + ; auto server = std::make_unique>(); distributed db; @@ -28,15 +40,16 @@ int main(int ac, char** av) { api::http_context ctx; return app.run(ac, av, [&] { - auto&& config = app.configuration(); - uint16_t thrift_port = config["thrift-port"].as(); - uint16_t cql_port = config["cql-port"].as(); - sstring datadir = config["datadir"].as(); - uint16_t api_port = config["api-port"].as(); + auto&& opts = app.configuration(); + uint16_t thrift_port = cfg->rpc_port(); + uint16_t cql_port = cfg->native_transport_port(); + uint16_t api_port = opts["api-port"].as(); - return db.start().then([datadir, &db] { + return read_config(opts, *cfg).then([cfg, &db]() { + return db.start(std::move(*cfg)); + }).then([&db] { engine().at_exit([&db] { return db.stop(); }); - return db.invoke_on_all(&database::init_from_data_directory, datadir); + return db.invoke_on_all(&database::init_from_data_directory); }).then([&db, &proxy, &qp] { return qp.start(std::ref(proxy), std::ref(db)).then([&qp] { engine().at_exit([&qp] { return qp.stop(); });