-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmemcache.rs
More file actions
341 lines (293 loc) · 11.8 KB
/
memcache.rs
File metadata and controls
341 lines (293 loc) · 11.8 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2018-2025 Progress Software Corporation and/or its subsidiaries, affiliates or applicable contributors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use protobuf::{self,
Message};
use rand::{self,
Rng};
use sha2::{Digest,
Sha512};
use std::time::Instant;
use super::metrics::Histogram;
use crate::{bldr_core::metrics::HistogramMetric,
config::MemcacheCfg,
hab_core::{package::PackageIdent,
ChannelIdent},
protocol::originsrv::Session};
pub struct MemcacheClient {
cli: memcache::Client,
ttl: u32,
}
impl MemcacheClient {
pub fn new(config: &MemcacheCfg) -> Self {
trace!("Creating memcache client, hosts: {:?}", config.hosts);
let memcache_host_strings = config.memcache_hosts();
let memcache_hosts: Vec<&str> = memcache_host_strings.iter().map(AsRef::as_ref).collect();
MemcacheClient { cli: memcache::Client::connect(memcache_hosts).unwrap(),
ttl: config.ttl, }
}
pub fn set_package(&mut self,
ident: &PackageIdent,
pkg_json: Option<&str>,
channel: &ChannelIdent,
target: &str,
opt_account_id: Option<u64>) {
let package_namespace = self.package_namespace(&ident.origin, &ident.name);
let channel_namespace = self.channel_namespace(&ident.origin, channel);
let account_str = match opt_account_id {
Some(id) => format!(":{}", id),
None => "".to_string(),
};
let body = pkg_json.unwrap_or("404");
match self.cli.set(&format!("{}/{}/{}:{}:{}{}",
target,
channel,
ident,
channel_namespace,
package_namespace,
account_str),
body,
self.ttl * 60)
{
Ok(_) => {
trace!("Saved {}/{}/{} to memcached",
target,
channel,
ident.to_string())
}
Err(e) => {
warn!("Failed to save {}/{}/{} to memcached: {:?}",
target,
channel,
ident.to_string(),
e)
}
};
}
pub fn get_package(&mut self,
ident: &PackageIdent,
channel: &ChannelIdent,
target: &str,
opt_account_id: Option<u64>)
-> (bool, Option<String>) {
let package_namespace = self.package_namespace(&ident.origin, &ident.name);
let channel_namespace = self.channel_namespace(&ident.origin, channel);
trace!("Getting {}/{}/{} from memcached for {:?}",
target,
channel,
ident.to_string(),
opt_account_id);
let account_str = match opt_account_id {
Some(id) => format!(":{}", id),
None => "".to_string(),
};
let start_time = Instant::now();
match self.get_string(&format!("{}/{}/{}:{}:{}{}",
target,
channel,
ident,
channel_namespace,
package_namespace,
account_str))
{
Some(json_body) => {
let duration_millis = start_time.elapsed().as_millis();
trace!("Memcache get_package time: {} ms", duration_millis);
Histogram::MemcacheCallTime.set(duration_millis as f64);
if json_body == "404" {
(true, None)
} else {
(true, Some(json_body))
}
}
None => (false, None),
}
}
pub fn clear_cache_for_package(&mut self, ident: &PackageIdent) {
self.reset_namespace(&package_ns_key(&ident.origin, &ident.name));
}
pub fn clear_cache_for_member_role(&mut self, origin: &str, account_id: u64) {
self.delete_role_key(&member_role_ns_key(origin, account_id));
}
pub fn clear_cache_for_channel(&mut self, origin: &str, channel: &ChannelIdent) {
self.reset_namespace(&channel_ns_key(origin, channel));
}
pub fn get_session(&mut self, token: &str) -> Option<Session> {
trace!("Getting session for token {} from memcached", token);
let start_time = Instant::now();
match self.get_bytes(&hash_key(token)) {
Some(session) => {
let duration_millis = start_time.elapsed().as_millis();
trace!("Memcache get_session time: {} ms", duration_millis);
Histogram::MemcacheCallTime.set(duration_millis as f64);
Some(protobuf::Message::parse_from_bytes(&session).unwrap())
}
None => None,
}
}
pub fn delete_role_key(&mut self, key: &str) {
match self.cli.delete(key) {
Ok(b) => {
if b {
debug!("Deleted key {}, {:?}", key, b)
} else {
debug!("Could not find key {}: {}", key, b)
}
}
Err(e) => debug!("Failed to delete key {}: {}", key, e),
};
}
pub fn delete_session_key(&mut self, key: &str) {
match self.cli.delete(&hash_key(key)) {
Ok(b) => debug!("Deleted key {}, {:?}", key, b),
Err(e) => debug!("Failed to delete key {}: {}", key, e),
};
}
pub fn set_session(&mut self, token: &str, session: &Session, ttl: Option<u32>) {
let computed_ttl = match ttl {
Some(ttl) => ttl,
None => self.ttl * 60,
};
match self.cli.set(&hash_key(token),
session.write_to_bytes().unwrap().as_slice(),
computed_ttl)
{
Ok(_) => trace!("Saved session to memcached!"),
Err(e) => warn!("Failed to save session to memcached: {}", e),
};
}
pub fn set_origin_member(&mut self, origin: &str, account_id: u64, val: bool) {
let key = format!("member:{}/{}", origin, account_id);
match self.cli.set(&key, val, self.ttl * 60) {
Ok(_) => {
trace!("Saved origin membership {}/{} to memcached!",
origin,
account_id)
}
Err(e) => warn!("Failed to save origin membership to memcached: {}", e),
}
}
pub fn get_origin_member(&mut self, origin: &str, account_id: u64) -> Option<bool> {
trace!("Getting origin membership for {} {} from memcached",
origin,
account_id);
let key = format!("member:{}/{}", origin, account_id);
let start_time = Instant::now();
let ret = self.get_bool(&key);
let duration_millis = start_time.elapsed().as_millis();
trace!("Memcache get_origin_member time: {} ms", duration_millis);
Histogram::MemcacheCallTime.set(duration_millis as f64);
ret
}
pub fn get_origin_member_role(&mut self, origin: &str, account_id: u64) -> Option<String> {
trace!("Getting origin role membership for {} {} from memcached",
origin,
account_id);
let key = member_role_ns_key(origin, account_id);
let start_time = Instant::now();
let ret = self.get_string(&key);
let duration_millis = start_time.elapsed().as_millis();
trace!("Memcache get_origin_member_role time: {} ms",
duration_millis);
Histogram::MemcacheCallTime.set(duration_millis as f64);
ret
}
pub fn set_origin_member_role(&mut self, origin: &str, account_id: u64, role: &str) {
let key = member_role_ns_key(origin, account_id);
match self.cli.set(&key, role, self.ttl * 60) {
Ok(_) => {
debug!("Saved origin role membership {}/{}/{} to memcached!",
origin, account_id, role);
}
Err(e) => warn!("Failed to save origin role membership to memcached: {}", e),
}
}
fn package_namespace(&mut self, origin: &str, name: &str) -> String {
self.get_namespace(&package_ns_key(origin, name))
}
fn channel_namespace(&mut self, origin: &str, channel: &ChannelIdent) -> String {
self.get_namespace(&channel_ns_key(origin, channel))
}
fn get_namespace(&mut self, namespace_key: &str) -> String {
match self.get_string(namespace_key) {
Some(value) => value,
None => self.reset_namespace(namespace_key),
}
}
fn reset_namespace(&mut self, namespace_key: &str) -> String {
let mut rng = rand::thread_rng();
let val: u64 = rng.gen();
trace!("Reset namespace {} to {}", namespace_key, val);
if let Err(err) = self.cli.set(namespace_key, val, self.ttl * 60) {
warn!("Failed to reset namespace {} to {}: {}",
namespace_key, val, err)
}
format!("{}", val)
}
// These are to make the compiler happy
fn get_bytes(&mut self, key: &str) -> Option<Vec<u8>> {
match self.cli.get(key) {
Ok(bytes) => bytes,
Err(e) => {
warn!("Error getting key {}: {:?}", key, e);
None
}
}
}
fn get_string(&mut self, key: &str) -> Option<String> {
match self.cli.get(key) {
Ok(string) => string,
Err(e) => {
warn!("Error getting key {}: {:?}", key, e);
None
}
}
}
fn get_bool(&mut self, key: &str) -> Option<bool> {
match self.cli.get(key) {
Ok(val) => val,
Err(e) => {
warn!("Error getting key {}: {:?}", key, e);
None
}
}
}
}
fn package_ns_key(origin: &str, name: &str) -> String { format!("package:{}/{}", origin, name) }
fn channel_ns_key(origin: &str, channel: &ChannelIdent) -> String {
format!("channel:{}/{}", origin, channel)
}
fn member_role_ns_key(origin: &str, account_id: u64) -> String {
format!("member_role:{}/{}", origin, account_id)
}
fn hash_key(key: &str) -> String {
let mut hasher = Sha512::new();
hasher.update(key);
format!("{:02x}", hasher.finalize())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn hash_key_with_empty_input() {
let expected = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e".to_string();
assert_eq!(hash_key(""), expected);
}
#[test]
fn hash_key_with_session_token() {
let token =
"CIyAhviVt/aAChIFMYz4NYETACIoZDM2NDg9ZjEzOWY0MTQ5YzZiNmNjDMBkYTA4NTAzODkaMzdiNGZlNQ==";
let expected = "33a8f10726b1ada86d9f60e4abbb1cb8726798a2303395cecace82225236cfc3d5a82815d1017a1dd6f8d34e8b77a51c30d972ba2031e1207679fb2a4db925ea".to_string();
assert_eq!(hash_key(token), expected)
}
}