-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alternator, db: move the tag code to db/tags
Tags are a useful mechanism that could be used outside of alternator namespace. My motivation to move tags_extension and other utilities to db/tags/ was that I wanted to use them to mark "synchronous mode" views. I have extracted `get_tags_of_table`, `find_tag` and `update_tags` method to db/tags/utils.cc and moved alternator/tags_extension.hh to db/tags/. The signature of `get_tags_of_table` was changed from `const std::map<sstring, sstring>&` to `const std::map<sstring, sstring>*` Original behavior of this function was to throw an `alternator::api_error` exception. This was undesirable, as it introduced a dependency on the alternator module. I chose to change it to return a potentially null value, and added a wrapper function to the alternator module - `get_tags_of_table_or_throw` to keep the previous throwing behavior.
- Loading branch information
Showing
11 changed files
with
128 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ resources | |
.pytest_cache | ||
/expressions.tokens | ||
tags | ||
!db/tags/ | ||
testlog | ||
test/*/*.reject | ||
.vscode | ||
|
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
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,56 @@ | ||
/* | ||
* Copyright 2022-present ScyllaDB | ||
*/ | ||
|
||
/* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
#include "db/tags/utils.hh" | ||
|
||
#include "db/tags/extension.hh" | ||
#include "schema_builder.hh" | ||
#include "schema_registry.hh" | ||
|
||
namespace db { | ||
|
||
const std::map<sstring, sstring>* get_tags_of_table(schema_ptr schema) { | ||
auto it = schema->extensions().find(tags_extension::NAME); | ||
if (it == schema->extensions().end()) { | ||
return nullptr; | ||
} | ||
auto tags_ext = static_pointer_cast<tags_extension>(it->second); | ||
return &tags_ext->tags(); | ||
} | ||
|
||
std::optional<std::string> find_tag(const schema& s, const sstring& tag) { | ||
auto it1 = s.extensions().find(tags_extension::NAME); | ||
if (it1 == s.extensions().end()) { | ||
return std::nullopt; | ||
} | ||
const std::map<sstring, sstring>& tags_map = | ||
static_pointer_cast<tags_extension>(it1->second)->tags(); | ||
auto it2 = tags_map.find(tag); | ||
if (it2 == tags_map.end()) { | ||
return std::nullopt; | ||
} else { | ||
return it2->second; | ||
} | ||
} | ||
|
||
future<> update_tags(service::migration_manager& mm, schema_ptr schema, std::map<sstring, sstring>&& tags_map) { | ||
co_await mm.container().invoke_on(0, [s = global_schema_ptr(std::move(schema)), tags_map = std::move(tags_map)] (service::migration_manager& mm) -> future<> { | ||
// FIXME: the following needs to be in a loop. If mm.announce() below | ||
// fails, we need to retry the whole thing. | ||
auto group0_guard = co_await mm.start_group0_operation(); | ||
|
||
schema_builder builder(s); | ||
builder.add_extension(tags_extension::NAME, ::make_shared<tags_extension>(tags_map)); | ||
|
||
auto m = co_await mm.prepare_column_family_update_announcement(builder.build(), false, std::vector<view_ptr>(), group0_guard.write_timestamp()); | ||
|
||
co_await mm.announce(std::move(m), std::move(group0_guard)); | ||
}); | ||
} | ||
|
||
} |
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,41 @@ | ||
/* | ||
* Copyright 2022-present ScyllaDB | ||
*/ | ||
|
||
/* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <map> | ||
#include <optional> | ||
#include <seastar/core/future.hh> | ||
#include <seastar/core/sstring.hh> | ||
#include "seastarx.hh" | ||
|
||
#include "schema.hh" | ||
#include "service/client_state.hh" | ||
#include "service/migration_manager.hh" | ||
|
||
namespace db { | ||
|
||
// get_tags_of_table() returns all tags associated with the given table, or | ||
// nullptr if the table is missing the tags extension. | ||
// Returned value is a non-owning pointer, which refers to the inner fields of the | ||
// schema. It should not outlive the schema_ptr argument. | ||
const std::map<sstring, sstring>* get_tags_of_table(schema_ptr schema); | ||
|
||
// find_tag() returns the value of a specific tag, or nothing if it doesn't | ||
// exist. If the table is missing the tags extension (e.g., is not an | ||
// Alternator table) it's not an error - we return nothing, as in the case that | ||
// tags exist but not this tag. | ||
std::optional<std::string> find_tag(const schema& s, const sstring& tag); | ||
|
||
// FIXME: Updating tags currently relies on updating schema, which may be subject | ||
// to races during concurrent updates of the same table. Once Scylla schema updates | ||
// are fixed, this issue will automatically get fixed as well. | ||
future<> update_tags(service::migration_manager& mm, schema_ptr schema, std::map<sstring, sstring>&& tags_map); | ||
|
||
} |
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