Compare commits
3 Commits
bb75a9d228
...
deafa88af5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
deafa88af5 | ||
|
|
880ad1d3fa | ||
|
|
4c0123426a |
@@ -56,8 +56,8 @@ pub fn enqueue_job(
|
||||
|
||||
/// Claim a batch of jobs for processing.
|
||||
///
|
||||
/// Atomically sets `locked_at` on the claimed jobs. Only claims jobs where
|
||||
/// `locked_at IS NULL` and `(next_retry_at IS NULL OR next_retry_at <= now)`.
|
||||
/// Atomically selects and locks jobs within a transaction. Only claims jobs
|
||||
/// where `locked_at IS NULL` and `(next_retry_at IS NULL OR next_retry_at <= now)`.
|
||||
pub fn claim_jobs(conn: &Connection, job_type: &str, batch_size: usize) -> Result<Vec<PendingJob>> {
|
||||
if batch_size == 0 {
|
||||
return Ok(Vec::new());
|
||||
@@ -65,19 +65,25 @@ pub fn claim_jobs(conn: &Connection, job_type: &str, batch_size: usize) -> Resul
|
||||
|
||||
let now = now_ms();
|
||||
|
||||
// Find available jobs
|
||||
let mut select_stmt = conn.prepare_cached(
|
||||
"SELECT id, project_id, entity_type, entity_iid, entity_local_id, job_type, payload_json, attempts
|
||||
FROM pending_dependent_fetches
|
||||
WHERE job_type = ?1
|
||||
AND locked_at IS NULL
|
||||
AND (next_retry_at IS NULL OR next_retry_at <= ?2)
|
||||
ORDER BY enqueued_at ASC
|
||||
LIMIT ?3",
|
||||
// Use UPDATE ... RETURNING to atomically select and lock in one statement.
|
||||
// This eliminates the race between SELECT and UPDATE.
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"UPDATE pending_dependent_fetches
|
||||
SET locked_at = ?1
|
||||
WHERE id IN (
|
||||
SELECT id FROM pending_dependent_fetches
|
||||
WHERE job_type = ?2
|
||||
AND locked_at IS NULL
|
||||
AND (next_retry_at IS NULL OR next_retry_at <= ?1)
|
||||
ORDER BY enqueued_at ASC
|
||||
LIMIT ?3
|
||||
)
|
||||
RETURNING id, project_id, entity_type, entity_iid, entity_local_id,
|
||||
job_type, payload_json, attempts",
|
||||
)?;
|
||||
|
||||
let jobs: Vec<PendingJob> = select_stmt
|
||||
.query_map(rusqlite::params![job_type, now, batch_size as i64], |row| {
|
||||
let jobs = stmt
|
||||
.query_map(rusqlite::params![now, job_type, batch_size as i64], |row| {
|
||||
Ok(PendingJob {
|
||||
id: row.get(0)?,
|
||||
project_id: row.get(1)?,
|
||||
@@ -91,18 +97,6 @@ pub fn claim_jobs(conn: &Connection, job_type: &str, batch_size: usize) -> Resul
|
||||
})?
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?;
|
||||
|
||||
// Lock the claimed jobs
|
||||
if !jobs.is_empty() {
|
||||
let ids: Vec<String> = jobs.iter().map(|j| j.id.to_string()).collect();
|
||||
let placeholders = ids.join(",");
|
||||
conn.execute(
|
||||
&format!(
|
||||
"UPDATE pending_dependent_fetches SET locked_at = ?1 WHERE id IN ({placeholders})"
|
||||
),
|
||||
rusqlite::params![now],
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(jobs)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ use crate::gitlab::types::{GitLabLabelEvent, GitLabMilestoneEvent, GitLabStateEv
|
||||
/// Upsert state events for an entity.
|
||||
///
|
||||
/// Uses INSERT OR REPLACE keyed on UNIQUE(gitlab_id, project_id).
|
||||
/// Wraps in a savepoint for atomicity per entity.
|
||||
/// Caller is responsible for wrapping in a transaction if atomicity is needed.
|
||||
pub fn upsert_state_events(
|
||||
conn: &mut Connection,
|
||||
conn: &Connection,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
@@ -19,9 +19,7 @@ pub fn upsert_state_events(
|
||||
) -> Result<usize> {
|
||||
let (issue_id, merge_request_id) = resolve_entity_ids(entity_type, entity_local_id)?;
|
||||
|
||||
let sp = conn.savepoint()?;
|
||||
|
||||
let mut stmt = sp.prepare_cached(
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_state_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, state,
|
||||
actor_gitlab_id, actor_username, created_at,
|
||||
@@ -51,15 +49,13 @@ pub fn upsert_state_events(
|
||||
count += 1;
|
||||
}
|
||||
|
||||
drop(stmt);
|
||||
sp.commit()?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
/// Upsert label events for an entity.
|
||||
/// Caller is responsible for wrapping in a transaction if atomicity is needed.
|
||||
pub fn upsert_label_events(
|
||||
conn: &mut Connection,
|
||||
conn: &Connection,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
@@ -67,9 +63,7 @@ pub fn upsert_label_events(
|
||||
) -> Result<usize> {
|
||||
let (issue_id, merge_request_id) = resolve_entity_ids(entity_type, entity_local_id)?;
|
||||
|
||||
let sp = conn.savepoint()?;
|
||||
|
||||
let mut stmt = sp.prepare_cached(
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_label_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, action,
|
||||
label_name, actor_gitlab_id, actor_username, created_at)
|
||||
@@ -96,15 +90,13 @@ pub fn upsert_label_events(
|
||||
count += 1;
|
||||
}
|
||||
|
||||
drop(stmt);
|
||||
sp.commit()?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
/// Upsert milestone events for an entity.
|
||||
/// Caller is responsible for wrapping in a transaction if atomicity is needed.
|
||||
pub fn upsert_milestone_events(
|
||||
conn: &mut Connection,
|
||||
conn: &Connection,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
@@ -112,9 +104,7 @@ pub fn upsert_milestone_events(
|
||||
) -> Result<usize> {
|
||||
let (issue_id, merge_request_id) = resolve_entity_ids(entity_type, entity_local_id)?;
|
||||
|
||||
let sp = conn.savepoint()?;
|
||||
|
||||
let mut stmt = sp.prepare_cached(
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_milestone_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, action,
|
||||
milestone_title, milestone_id, actor_gitlab_id, actor_username, created_at)
|
||||
@@ -142,9 +132,6 @@ pub fn upsert_milestone_events(
|
||||
count += 1;
|
||||
}
|
||||
|
||||
drop(stmt);
|
||||
sp.commit()?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
|
||||
@@ -166,12 +166,12 @@ pub fn extract_issue_document(conn: &Connection, issue_id: i64) -> Result<Option
|
||||
content.push_str(desc);
|
||||
}
|
||||
|
||||
let content_hash = compute_content_hash(&content);
|
||||
let labels_hash = compute_list_hash(&labels);
|
||||
let paths_hash = compute_list_hash(&[]); // Issues have no paths
|
||||
|
||||
// Apply hard cap truncation for safety
|
||||
// Apply hard cap truncation for safety, then hash the final stored content
|
||||
let hard_cap = truncate_hard_cap(&content);
|
||||
let content_hash = compute_content_hash(&hard_cap.content);
|
||||
|
||||
Ok(Some(DocumentData {
|
||||
source_type: SourceType::Issue,
|
||||
@@ -281,12 +281,12 @@ pub fn extract_mr_document(conn: &Connection, mr_id: i64) -> Result<Option<Docum
|
||||
content.push_str(desc);
|
||||
}
|
||||
|
||||
let content_hash = compute_content_hash(&content);
|
||||
let labels_hash = compute_list_hash(&labels);
|
||||
let paths_hash = compute_list_hash(&[]);
|
||||
|
||||
// Apply hard cap truncation for safety
|
||||
// Apply hard cap truncation for safety, then hash the final stored content
|
||||
let hard_cap = truncate_hard_cap(&content);
|
||||
let content_hash = compute_content_hash(&hard_cap.content);
|
||||
|
||||
Ok(Some(DocumentData {
|
||||
source_type: SourceType::MergeRequest,
|
||||
|
||||
@@ -662,7 +662,7 @@ impl GitLabClient {
|
||||
self.fetch_all_pages(&path).await
|
||||
}
|
||||
|
||||
/// Fetch all three event types for an entity in one call.
|
||||
/// Fetch all three event types for an entity concurrently.
|
||||
pub async fn fetch_all_resource_events(
|
||||
&self,
|
||||
gitlab_project_id: i64,
|
||||
@@ -675,23 +675,19 @@ impl GitLabClient {
|
||||
)> {
|
||||
match entity_type {
|
||||
"issue" => {
|
||||
let state = self
|
||||
.fetch_issue_state_events(gitlab_project_id, iid)
|
||||
.await?;
|
||||
let label = self
|
||||
.fetch_issue_label_events(gitlab_project_id, iid)
|
||||
.await?;
|
||||
let milestone = self
|
||||
.fetch_issue_milestone_events(gitlab_project_id, iid)
|
||||
.await?;
|
||||
let (state, label, milestone) = tokio::try_join!(
|
||||
self.fetch_issue_state_events(gitlab_project_id, iid),
|
||||
self.fetch_issue_label_events(gitlab_project_id, iid),
|
||||
self.fetch_issue_milestone_events(gitlab_project_id, iid),
|
||||
)?;
|
||||
Ok((state, label, milestone))
|
||||
}
|
||||
"merge_request" => {
|
||||
let state = self.fetch_mr_state_events(gitlab_project_id, iid).await?;
|
||||
let label = self.fetch_mr_label_events(gitlab_project_id, iid).await?;
|
||||
let milestone = self
|
||||
.fetch_mr_milestone_events(gitlab_project_id, iid)
|
||||
.await?;
|
||||
let (state, label, milestone) = tokio::try_join!(
|
||||
self.fetch_mr_state_events(gitlab_project_id, iid),
|
||||
self.fetch_mr_label_events(gitlab_project_id, iid),
|
||||
self.fetch_mr_milestone_events(gitlab_project_id, iid),
|
||||
)?;
|
||||
Ok((state, label, milestone))
|
||||
}
|
||||
_ => Err(LoreError::Other(format!(
|
||||
|
||||
@@ -540,36 +540,31 @@ async fn drain_resource_events(
|
||||
});
|
||||
|
||||
let mut processed = 0;
|
||||
|
||||
// Max iterations guard: prevent infinite loop if jobs keep failing and retrying
|
||||
// within the same drain run. Allow 2x total_pending iterations as safety margin.
|
||||
let max_iterations = total_pending * 2;
|
||||
let mut iterations = 0;
|
||||
let mut seen_job_ids = std::collections::HashSet::new();
|
||||
|
||||
loop {
|
||||
if iterations >= max_iterations {
|
||||
warn!(
|
||||
iterations,
|
||||
total_pending, "Resource events drain hit max iterations guard, stopping"
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
let jobs = claim_jobs(conn, "resource_events", batch_size)?;
|
||||
if jobs.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
for job in &jobs {
|
||||
iterations += 1;
|
||||
// Guard against re-processing a job that was failed and re-claimed
|
||||
// within the same drain run (shouldn't happen due to backoff, but
|
||||
// defensive against clock skew or zero-backoff edge cases).
|
||||
if !seen_job_ids.insert(job.id) {
|
||||
warn!(
|
||||
job_id = job.id,
|
||||
"Skipping already-processed job in same drain run"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
match client
|
||||
.fetch_all_resource_events(gitlab_project_id, &job.entity_type, job.entity_iid)
|
||||
.await
|
||||
{
|
||||
Ok((state_events, label_events, milestone_events)) => {
|
||||
// Store events - we need &mut Connection for savepoints in upsert functions.
|
||||
// Use unchecked_transaction as a workaround since we have &Connection.
|
||||
let store_result = store_resource_events(
|
||||
conn,
|
||||
job.project_id,
|
||||
@@ -635,8 +630,7 @@ async fn drain_resource_events(
|
||||
|
||||
/// Store fetched resource events in the database.
|
||||
///
|
||||
/// Uses unchecked_transaction to work with &Connection (not &mut Connection),
|
||||
/// which is safe because we're single-threaded and using WAL mode.
|
||||
/// Wraps all three event types in a single transaction for atomicity.
|
||||
fn store_resource_events(
|
||||
conn: &Connection,
|
||||
project_id: i64,
|
||||
@@ -646,23 +640,30 @@ fn store_resource_events(
|
||||
label_events: &[crate::gitlab::types::GitLabLabelEvent],
|
||||
milestone_events: &[crate::gitlab::types::GitLabMilestoneEvent],
|
||||
) -> Result<()> {
|
||||
// The upsert functions require &mut Connection for savepoints.
|
||||
// We use unchecked_transaction to wrap all three upserts atomically,
|
||||
// then call the upsert functions using the transaction's inner connection.
|
||||
let tx = conn.unchecked_transaction()?;
|
||||
|
||||
// State events - use raw SQL within transaction instead of upsert_state_events
|
||||
// which requires &mut Connection
|
||||
if !state_events.is_empty() {
|
||||
store_state_events_tx(&tx, project_id, entity_type, entity_local_id, state_events)?;
|
||||
crate::core::events_db::upsert_state_events(
|
||||
&tx,
|
||||
project_id,
|
||||
entity_type,
|
||||
entity_local_id,
|
||||
state_events,
|
||||
)?;
|
||||
}
|
||||
|
||||
if !label_events.is_empty() {
|
||||
store_label_events_tx(&tx, project_id, entity_type, entity_local_id, label_events)?;
|
||||
crate::core::events_db::upsert_label_events(
|
||||
&tx,
|
||||
project_id,
|
||||
entity_type,
|
||||
entity_local_id,
|
||||
label_events,
|
||||
)?;
|
||||
}
|
||||
|
||||
if !milestone_events.is_empty() {
|
||||
store_milestone_events_tx(
|
||||
crate::core::events_db::upsert_milestone_events(
|
||||
&tx,
|
||||
project_id,
|
||||
entity_type,
|
||||
@@ -675,139 +676,6 @@ fn store_resource_events(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store state events within an existing transaction.
|
||||
fn store_state_events_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
events: &[crate::gitlab::types::GitLabStateEvent],
|
||||
) -> Result<()> {
|
||||
let (issue_id, merge_request_id): (Option<i64>, Option<i64>) = match entity_type {
|
||||
"issue" => (Some(entity_local_id), None),
|
||||
"merge_request" => (None, Some(entity_local_id)),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
let mut stmt = tx.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_state_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, state,
|
||||
actor_gitlab_id, actor_username, created_at,
|
||||
source_commit, source_merge_request_iid)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
|
||||
)?;
|
||||
|
||||
for event in events {
|
||||
let created_at = crate::core::time::iso_to_ms_strict(&event.created_at)
|
||||
.map_err(crate::core::error::LoreError::Other)?;
|
||||
let actor_id = event.user.as_ref().map(|u| u.id);
|
||||
let actor_username = event.user.as_ref().map(|u| u.username.as_str());
|
||||
let source_mr_iid = event.source_merge_request.as_ref().map(|mr| mr.iid);
|
||||
|
||||
stmt.execute(rusqlite::params![
|
||||
event.id,
|
||||
project_id,
|
||||
issue_id,
|
||||
merge_request_id,
|
||||
event.state,
|
||||
actor_id,
|
||||
actor_username,
|
||||
created_at,
|
||||
event.source_commit,
|
||||
source_mr_iid,
|
||||
])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store label events within an existing transaction.
|
||||
fn store_label_events_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
events: &[crate::gitlab::types::GitLabLabelEvent],
|
||||
) -> Result<()> {
|
||||
let (issue_id, merge_request_id): (Option<i64>, Option<i64>) = match entity_type {
|
||||
"issue" => (Some(entity_local_id), None),
|
||||
"merge_request" => (None, Some(entity_local_id)),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
let mut stmt = tx.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_label_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, action,
|
||||
label_name, actor_gitlab_id, actor_username, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
|
||||
)?;
|
||||
|
||||
for event in events {
|
||||
let created_at = crate::core::time::iso_to_ms_strict(&event.created_at)
|
||||
.map_err(crate::core::error::LoreError::Other)?;
|
||||
let actor_id = event.user.as_ref().map(|u| u.id);
|
||||
let actor_username = event.user.as_ref().map(|u| u.username.as_str());
|
||||
|
||||
stmt.execute(rusqlite::params![
|
||||
event.id,
|
||||
project_id,
|
||||
issue_id,
|
||||
merge_request_id,
|
||||
event.action,
|
||||
event.label.name,
|
||||
actor_id,
|
||||
actor_username,
|
||||
created_at,
|
||||
])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store milestone events within an existing transaction.
|
||||
fn store_milestone_events_tx(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
project_id: i64,
|
||||
entity_type: &str,
|
||||
entity_local_id: i64,
|
||||
events: &[crate::gitlab::types::GitLabMilestoneEvent],
|
||||
) -> Result<()> {
|
||||
let (issue_id, merge_request_id): (Option<i64>, Option<i64>) = match entity_type {
|
||||
"issue" => (Some(entity_local_id), None),
|
||||
"merge_request" => (None, Some(entity_local_id)),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
let mut stmt = tx.prepare_cached(
|
||||
"INSERT OR REPLACE INTO resource_milestone_events
|
||||
(gitlab_id, project_id, issue_id, merge_request_id, action,
|
||||
milestone_title, milestone_id, actor_gitlab_id, actor_username, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
|
||||
)?;
|
||||
|
||||
for event in events {
|
||||
let created_at = crate::core::time::iso_to_ms_strict(&event.created_at)
|
||||
.map_err(crate::core::error::LoreError::Other)?;
|
||||
let actor_id = event.user.as_ref().map(|u| u.id);
|
||||
let actor_username = event.user.as_ref().map(|u| u.username.as_str());
|
||||
|
||||
stmt.execute(rusqlite::params![
|
||||
event.id,
|
||||
project_id,
|
||||
issue_id,
|
||||
merge_request_id,
|
||||
event.action,
|
||||
event.milestone.title,
|
||||
event.milestone.id,
|
||||
actor_id,
|
||||
actor_username,
|
||||
created_at,
|
||||
])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -72,8 +72,8 @@ async fn main() {
|
||||
let quiet = cli.quiet;
|
||||
|
||||
let result = match cli.command {
|
||||
Commands::Issues(args) => handle_issues(cli.config.as_deref(), args, robot_mode).await,
|
||||
Commands::Mrs(args) => handle_mrs(cli.config.as_deref(), args, robot_mode).await,
|
||||
Commands::Issues(args) => handle_issues(cli.config.as_deref(), args, robot_mode),
|
||||
Commands::Mrs(args) => handle_mrs(cli.config.as_deref(), args, robot_mode),
|
||||
Commands::Search(args) => handle_search(cli.config.as_deref(), args, robot_mode).await,
|
||||
Commands::Stats(args) => handle_stats(cli.config.as_deref(), args, robot_mode).await,
|
||||
Commands::Embed(args) => handle_embed(cli.config.as_deref(), args, robot_mode).await,
|
||||
@@ -284,7 +284,7 @@ fn handle_error(e: Box<dyn std::error::Error>, robot_mode: bool) -> ! {
|
||||
// Primary command handlers
|
||||
// ============================================================================
|
||||
|
||||
async fn handle_issues(
|
||||
fn handle_issues(
|
||||
config_override: Option<&str>,
|
||||
args: IssuesArgs,
|
||||
robot_mode: bool,
|
||||
@@ -334,7 +334,7 @@ async fn handle_issues(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_mrs(
|
||||
fn handle_mrs(
|
||||
config_override: Option<&str>,
|
||||
args: MrsArgs,
|
||||
robot_mode: bool,
|
||||
|
||||
Reference in New Issue
Block a user