|
| 1 | +use super::record_result; |
| 2 | +use crate::clients::ResponseError; |
| 3 | +use crate::config::Config; |
| 4 | +use crate::metrics::*; |
| 5 | +use crate::workload::{ClientWorkItemKind, LeaderboardClientRequest}; |
| 6 | +use crate::{workload, RUNNING}; |
| 7 | +use paste::paste; |
| 8 | + |
| 9 | +use async_channel::Receiver; |
| 10 | +use momento::leaderboard::{configurations, LeaderboardClient}; |
| 11 | +use momento::CredentialProvider; |
| 12 | +use ringlog::debug; |
| 13 | +use tokio::runtime::Runtime; |
| 14 | +use tokio::time::timeout; |
| 15 | + |
| 16 | +use std::io::{Error, Result}; |
| 17 | +use std::sync::atomic::Ordering; |
| 18 | +use std::time::Instant; |
| 19 | + |
| 20 | +/// Launch tasks with one channel per task as gRPC is mux-enabled. |
| 21 | +pub fn launch_tasks( |
| 22 | + runtime: &mut Runtime, |
| 23 | + config: Config, |
| 24 | + work_receiver: Receiver<ClientWorkItemKind<LeaderboardClientRequest>>, |
| 25 | +) { |
| 26 | + debug!("launching momento protocol tasks"); |
| 27 | + |
| 28 | + let cache_name = config |
| 29 | + .target() |
| 30 | + .cache_name() |
| 31 | + .unwrap_or_else(|| { |
| 32 | + eprintln!("cache name is not specified in the `target` section"); |
| 33 | + std::process::exit(1); |
| 34 | + }) |
| 35 | + .to_string(); |
| 36 | + |
| 37 | + for _ in 0..config.leaderboard().unwrap().poolsize() { |
| 38 | + let client = { |
| 39 | + let _guard = runtime.enter(); |
| 40 | + |
| 41 | + // initialize the Momento cache client |
| 42 | + if std::env::var("MOMENTO_API_KEY").is_err() { |
| 43 | + eprintln!("environment variable `MOMENTO_API_KEY` is not set"); |
| 44 | + std::process::exit(1); |
| 45 | + } |
| 46 | + |
| 47 | + let credential_provider = |
| 48 | + match CredentialProvider::from_env_var("MOMENTO_API_KEY".to_string()) { |
| 49 | + Ok(v) => v, |
| 50 | + Err(e) => { |
| 51 | + eprintln!("MOMENTO_API_KEY key should be valid: {e}"); |
| 52 | + std::process::exit(1); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + match LeaderboardClient::builder() |
| 57 | + .configuration(configurations::LowLatency::v1()) |
| 58 | + .credential_provider(credential_provider) |
| 59 | + .build() |
| 60 | + { |
| 61 | + Ok(c) => c, |
| 62 | + Err(e) => { |
| 63 | + eprintln!("could not create leaderboard client: {}", e); |
| 64 | + std::process::exit(1); |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + LEADERBOARD_CONNECT.increment(); |
| 70 | + LEADERBOARD_CONNECT_CURR.increment(); |
| 71 | + |
| 72 | + // create one task per channel |
| 73 | + for _ in 0..config.leaderboard().unwrap().concurrency() { |
| 74 | + runtime.spawn(task( |
| 75 | + config.clone(), |
| 76 | + client.clone(), |
| 77 | + cache_name.clone(), |
| 78 | + work_receiver.clone(), |
| 79 | + )); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +async fn task( |
| 85 | + config: Config, |
| 86 | + mut client: LeaderboardClient, |
| 87 | + cache_name: String, |
| 88 | + work_receiver: Receiver<ClientWorkItemKind<LeaderboardClientRequest>>, |
| 89 | +) -> Result<()> { |
| 90 | + while RUNNING.load(Ordering::Relaxed) { |
| 91 | + let work_item = work_receiver |
| 92 | + .recv() |
| 93 | + .await |
| 94 | + .map_err(|_| Error::other("channel closed"))?; |
| 95 | + |
| 96 | + LEADERBOARD_REQUEST.increment(); |
| 97 | + let start = Instant::now(); |
| 98 | + let result = match work_item { |
| 99 | + ClientWorkItemKind::Request { request, .. } => match request { |
| 100 | + LeaderboardClientRequest::Upsert(r) => { |
| 101 | + upsert(&mut client, &config, cache_name.clone(), r).await |
| 102 | + } |
| 103 | + LeaderboardClientRequest::GetCompetitionRank(r) => { |
| 104 | + get_competition_rank(&mut client, &config, cache_name.clone(), r).await |
| 105 | + } |
| 106 | + _ => { |
| 107 | + LEADERBOARD_REQUEST_UNSUPPORTED.increment(); |
| 108 | + continue; |
| 109 | + } |
| 110 | + }, |
| 111 | + ClientWorkItemKind::Reconnect => { |
| 112 | + continue; |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + LEADERBOARD_REQUEST_OK.increment(); |
| 117 | + |
| 118 | + let stop = Instant::now(); |
| 119 | + |
| 120 | + match result { |
| 121 | + Ok(_) => { |
| 122 | + LEADERBOARD_RESPONSE_OK.increment(); |
| 123 | + |
| 124 | + let latency = stop.duration_since(start).as_nanos() as u64; |
| 125 | + |
| 126 | + let _ = LEADERBOARD_RESPONSE_LATENCY.increment(latency); |
| 127 | + } |
| 128 | + Err(ResponseError::Exception) => { |
| 129 | + LEADERBOARD_RESPONSE_EX.increment(); |
| 130 | + } |
| 131 | + Err(ResponseError::Timeout) => { |
| 132 | + LEADERBOARD_RESPONSE_TIMEOUT.increment(); |
| 133 | + } |
| 134 | + Err(ResponseError::Ratelimited) => { |
| 135 | + LEADERBOARD_RESPONSE_RATELIMITED.increment(); |
| 136 | + } |
| 137 | + Err(ResponseError::BackendTimeout) => { |
| 138 | + LEADERBOARD_RESPONSE_BACKEND_TIMEOUT.increment(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + Ok(()) |
| 144 | +} |
| 145 | + |
| 146 | +/// Insert or update id-score pairs in the leaderboard. |
| 147 | +pub async fn upsert( |
| 148 | + client: &mut LeaderboardClient, |
| 149 | + config: &Config, |
| 150 | + cache_name: String, |
| 151 | + request: workload::leaderboard::Upsert, |
| 152 | +) -> std::result::Result<(), ResponseError> { |
| 153 | + LEADERBOARD_UPSERT.increment(); |
| 154 | + |
| 155 | + let leaderboard = client.leaderboard(cache_name, request.leaderboard.as_ref().clone()); |
| 156 | + let result = timeout( |
| 157 | + config.leaderboard().unwrap().request_timeout(), |
| 158 | + leaderboard.upsert(request.elements), |
| 159 | + ) |
| 160 | + .await; |
| 161 | + |
| 162 | + record_result!(result, LEADERBOARD_UPSERT) |
| 163 | +} |
| 164 | + |
| 165 | +/// Get the competition rank of a list of ids in a leaderboard. |
| 166 | +pub async fn get_competition_rank( |
| 167 | + client: &mut LeaderboardClient, |
| 168 | + config: &Config, |
| 169 | + cache_name: String, |
| 170 | + request: workload::leaderboard::GetCompetitionRank, |
| 171 | +) -> std::result::Result<(), ResponseError> { |
| 172 | + LEADERBOARD_GET_COMPETITION_RANK.increment(); |
| 173 | + |
| 174 | + let leaderboard = client.leaderboard(cache_name, request.leaderboard.as_ref().clone()); |
| 175 | + let ids = request.ids.as_ref().to_vec(); |
| 176 | + let result = timeout( |
| 177 | + config.leaderboard().unwrap().request_timeout(), |
| 178 | + leaderboard.get_competition_rank(ids), |
| 179 | + ) |
| 180 | + .await; |
| 181 | + |
| 182 | + record_result!(result, LEADERBOARD_GET_COMPETITION_RANK) |
| 183 | +} |
0 commit comments