forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyspace_utils.cc
209 lines (197 loc) · 7.82 KB
/
keyspace_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (C) 2019 pengjian.uestc @ gmail.com
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include "redis/keyspace_utils.hh"
#include "schema_builder.hh"
#include "types.hh"
#include "exceptions/exceptions.hh"
#include "cql3/statements/ks_prop_defs.hh"
#include "seastar/core/future.hh"
#include <memory>
#include "log.hh"
#include "db/query_context.hh"
#include "auth/service.hh"
#include "service/migration_manager.hh"
#include "service/client_state.hh"
#include "transport/server.hh"
#include "db/system_keyspace.hh"
#include "schema.hh"
#include "database.hh"
#include "gms/gossiper.hh"
#include <seastar/core/print.hh>
using namespace seastar;
namespace redis {
static logging::logger logger("keyspace_utils");
schema_ptr strings_schema(sstring ks_name) {
schema_builder builder(make_lw_shared(schema(generate_legacy_id(ks_name, redis::STRINGs), ks_name, redis::STRINGs,
// partition key
{{"pkey", utf8_type}},
// clustering key
{},
// regular columns
{{"data", utf8_type}},
// static columns
{},
// regular column name type
utf8_type,
// comment
"save STRINGs for redis"
)));
builder.set_gc_grace_seconds(0);
builder.with(schema_builder::compact_storage::yes);
builder.with_version(db::system_keyspace::generate_schema_version(builder.uuid()));
return builder.build(schema_builder::compact_storage::yes);
}
schema_ptr lists_schema(sstring ks_name) {
schema_builder builder(make_lw_shared(schema(generate_legacy_id(ks_name, redis::LISTs), ks_name, redis::LISTs,
// partition key
{{"pkey", utf8_type}},
// clustering key
{{"ckey", bytes_type}},
// regular columns
{{"data", utf8_type}},
// static columns
{},
// regular column name type
utf8_type,
// comment
"save LISTs for redis"
)));
builder.set_gc_grace_seconds(0);
builder.with(schema_builder::compact_storage::yes);
builder.with_version(db::system_keyspace::generate_schema_version(builder.uuid()));
return builder.build(schema_builder::compact_storage::yes);
}
schema_ptr hashes_schema(sstring ks_name) {
schema_builder builder(make_lw_shared(schema(generate_legacy_id(ks_name, redis::HASHes), ks_name, redis::HASHes,
// partition key
{{"pkey", utf8_type}},
// clustering key
{{"ckey", utf8_type}},
// regular columns
{{"data", utf8_type}},
// static columns
{},
// regular column name type
utf8_type,
// comment
"save HASHes for redis"
)));
builder.set_gc_grace_seconds(0);
builder.with(schema_builder::compact_storage::yes);
builder.with_version(db::system_keyspace::generate_schema_version(builder.uuid()));
return builder.build(schema_builder::compact_storage::yes);
}
schema_ptr sets_schema(sstring ks_name) {
schema_builder builder(make_lw_shared(schema(generate_legacy_id(ks_name, redis::SETs), ks_name, redis::SETs,
// partition key
{{"pkey", utf8_type}},
// clustering key
{{"ckey", utf8_type}},
// regular columns
{},
// static columns
{},
// regular column name type
utf8_type,
// comment
"save SETs for redis"
)));
builder.set_gc_grace_seconds(0);
builder.with(schema_builder::compact_storage::yes);
builder.with_version(db::system_keyspace::generate_schema_version(builder.uuid()));
return builder.build(schema_builder::compact_storage::yes);
}
schema_ptr zsets_schema(sstring ks_name) {
schema_builder builder(make_lw_shared(schema(generate_legacy_id(ks_name, redis::ZSETs), ks_name, redis::ZSETs,
// partition key
{{"pkey", utf8_type}},
// clustering key
{{"ckey", double_type}},
// regular columns
{{"data", utf8_type}},
// static columns
{},
// regular column name type
utf8_type,
// comment
"save ZSETs for redis"
)));
builder.set_gc_grace_seconds(0);
builder.with(schema_builder::compact_storage::yes);
builder.with_version(db::system_keyspace::generate_schema_version(builder.uuid()));
return builder.build(schema_builder::compact_storage::yes);
}
future<> create_keyspace_if_not_exists_impl(db::config& config, int default_replication_factor) {
auto keyspace_replication_strategy_options = config.redis_keyspace_replication_strategy_options();
if (keyspace_replication_strategy_options.count("class") == 0) {
keyspace_replication_strategy_options["class"] = "SimpleStrategy";
keyspace_replication_strategy_options["replication_factor"] = sprint("%d", default_replication_factor);
}
auto keyspace_gen = [&config, keyspace_replication_strategy_options = std::move(keyspace_replication_strategy_options)] (sstring name) {
auto& proxy = service::get_local_storage_proxy();
if (proxy.get_db().local().has_keyspace(name)) {
return make_ready_future<>();
}
auto attrs = make_shared<cql3::statements::ks_prop_defs>();
attrs->add_property(cql3::statements::ks_prop_defs::KW_DURABLE_WRITES, "true");
std::map<sstring, sstring> replication_properties;
for (auto&& option : keyspace_replication_strategy_options) {
replication_properties.emplace(option.first, option.second);
}
attrs->add_property(cql3::statements::ks_prop_defs::KW_REPLICATION, replication_properties);
attrs->validate();
const auto& tm = proxy.get_token_metadata();
return service::get_local_migration_manager().announce_new_keyspace(attrs->as_ks_metadata(name, tm), false);
};
auto table_gen = [] (sstring ks_name, sstring cf_name, schema_ptr schema) {
auto& proxy = service::get_local_storage_proxy();
if (proxy.get_db().local().has_schema(ks_name, cf_name)) {
return make_ready_future<>();
}
logger.info("Create keyspace: {}, table: {} for redis.", ks_name, cf_name);
return service::get_local_migration_manager().announce_new_column_family(schema, false);
};
// create 16 default database for redis.
return parallel_for_each(boost::irange<unsigned>(0, 16), [keyspace_gen = std::move(keyspace_gen), table_gen = std::move(table_gen)] (auto c) {
auto ks_name = sprint("REDIS_%d", c);
return keyspace_gen(ks_name).then([ks_name, table_gen] {
return when_all_succeed(
table_gen(ks_name, redis::STRINGs, strings_schema(ks_name)),
table_gen(ks_name, redis::LISTs, lists_schema(ks_name)),
table_gen(ks_name, redis::SETs, sets_schema(ks_name)),
table_gen(ks_name, redis::HASHes, hashes_schema(ks_name)),
table_gen(ks_name, redis::ZSETs, zsets_schema(ks_name))
).then([] {
return make_ready_future<>();
});
});
});
}
future<> maybe_create_keyspace(db::config& config) {
return gms::get_up_endpoint_count().then([&config] (auto live_endpoint_count) {
int replication_factor = 3;
if (live_endpoint_count < replication_factor) {
replication_factor = 1;
logger.warn("Creating keyspace for redis with unsafe, live endpoint nodes count: {}.", live_endpoint_count);
}
return create_keyspace_if_not_exists_impl(config, replication_factor);
});
}
}