-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(momento-proxy): add delete support (#102)
Adds support for delete/del commands for memcahce and redis protocols.
- Loading branch information
Showing
7 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,90 @@ | ||
use crate::klog::{klog_1, Status}; | ||
use crate::{Error, *}; | ||
use ::net::*; | ||
use protocol_memcache::*; | ||
|
||
pub async fn delete( | ||
client: &mut SimpleCacheClient, | ||
cache_name: &str, | ||
socket: &mut tokio::net::TcpStream, | ||
request: &protocol_memcache::Delete, | ||
) -> Result<(), Error> { | ||
let key = request.key(); | ||
|
||
// check if the key is invalid before sending the requests to the backend | ||
if std::str::from_utf8(key).is_err() { | ||
GET_EX.increment(); | ||
|
||
// invalid key | ||
let _ = socket.write_all(b"ERROR\r\n").await; | ||
return Err(Error::from(ErrorKind::InvalidInput)); | ||
} | ||
|
||
BACKEND_REQUEST.increment(); | ||
|
||
match timeout(Duration::from_millis(200), client.delete(cache_name, key)).await { | ||
Ok(Ok(_result)) => { | ||
// it appears we can't tell deleted from not found in the momento | ||
// protocol, so we treat all non-error responses as if the key has | ||
// been deleted | ||
|
||
DELETE_DELETED.increment(); | ||
|
||
if request.noreply() { | ||
klog_1(&"delete", &key, Status::Deleted, 0); | ||
} else { | ||
klog_1(&"delete", &key, Status::Deleted, 8); | ||
SESSION_SEND.increment(); | ||
SESSION_SEND_BYTE.add(8); | ||
TCP_SEND_BYTE.add(8); | ||
if let Err(e) = socket.write_all(b"DELETED\r\n").await { | ||
SESSION_SEND_EX.increment(); | ||
// hangup if we can't send a response back | ||
return Err(e); | ||
} | ||
} | ||
} | ||
Ok(Err(e)) => { | ||
BACKEND_EX.increment(); | ||
|
||
DELETE_EX.increment(); | ||
SESSION_SEND.increment(); | ||
|
||
klog_1(&"delete", &key, Status::ServerError, 0); | ||
|
||
let message = format!("SERVER_ERROR {e}\r\n"); | ||
|
||
SESSION_SEND_BYTE.add(message.len() as _); | ||
TCP_SEND_BYTE.add(message.len() as _); | ||
|
||
if let Err(e) = socket.write_all(message.as_bytes()).await { | ||
SESSION_SEND_EX.increment(); | ||
// hangup if we can't send a response back | ||
return Err(e); | ||
} | ||
} | ||
Err(_) => { | ||
// timeout | ||
BACKEND_EX.increment(); | ||
BACKEND_EX_TIMEOUT.increment(); | ||
|
||
DELETE_EX.increment(); | ||
SESSION_SEND.increment(); | ||
|
||
klog_1(&"delete", &key, Status::Timeout, 0); | ||
|
||
let message = "SERVER_ERROR backend timeout\r\n"; | ||
|
||
SESSION_SEND_BYTE.add(message.len() as _); | ||
TCP_SEND_BYTE.add(message.len() as _); | ||
|
||
if let Err(e) = socket.write_all(message.as_bytes()).await { | ||
SESSION_SEND_EX.increment(); | ||
// hangup if we can't send a response back | ||
return Err(e); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
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,46 @@ | ||
use crate::klog::*; | ||
use crate::*; | ||
use protocol_resp::*; | ||
use std::io::Write; | ||
|
||
use super::update_method_metrics; | ||
|
||
pub async fn del( | ||
client: &mut SimpleCacheClient, | ||
cache_name: &str, | ||
response_buf: &mut Vec<u8>, | ||
req: &Del, | ||
) -> ProxyResult { | ||
let keys: Vec<&[u8]> = req.keys().iter().map(|k| &**k).collect(); | ||
|
||
for key in keys { | ||
let mut client = client.clone(); | ||
|
||
update_method_metrics(&DEL, &DEL_EX, async move { | ||
match timeout(Duration::from_millis(200), client.delete(cache_name, key)).await { | ||
Ok(Ok(_)) => {} | ||
Ok(Err(e)) => { | ||
klog_1(&"hdel", &key, Status::ServerError, 0); | ||
return Err(ProxyError::from(e)); | ||
} | ||
Err(e) => { | ||
klog_1(&"hdel", &key, Status::Timeout, 0); | ||
return Err(ProxyError::from(e)); | ||
} | ||
} | ||
|
||
Ok(()) | ||
}) | ||
.await?; | ||
} | ||
|
||
// NOTE: the Momento protocol does not inform us of how many keys are | ||
// deleted. We lie to the client and say that they all were deleted. | ||
write!(response_buf, ":{}\r\n", req.keys().len())?; | ||
|
||
for key in req.keys() { | ||
klog_1(&"del", &key, Status::Deleted, 0); | ||
} | ||
|
||
Ok(()) | ||
} |
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