Skip to content

Commit

Permalink
get_drones_for_pool() should return all drones + add index on drone(l…
Browse files Browse the repository at this point in the history
…ast_heartbeat) (#781)
  • Loading branch information
rolyatmax committed Jul 9, 2024
1 parent 75bb4c7 commit 730fac7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions plane/schema/derived_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,13 @@ CREATE INDEX idx_backend_static_token ON public.backend USING btree (static_toke
CREATE UNIQUE INDEX idx_cluster_name ON public.node USING btree (cluster, name);


--
-- Name: idx_drone_last_heartbeat; Type: INDEX; Schema: public; Owner: postgres
--

CREATE INDEX idx_drone_last_heartbeat ON public.drone USING btree (last_heartbeat);


--
-- Name: idx_event_created_at; Type: INDEX; Schema: public; Owner: postgres
--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create index idx_drone_last_heartbeat on drone(last_heartbeat);
14 changes: 11 additions & 3 deletions plane/src/database/drone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,23 @@ impl<'a> DroneDatabase<'a> {
Ok(result.pool.into())
}

/// Returns a list of drones that are ready and have been seen in the last `seen_in_last` duration.
/// Returns a list of drones that have been seen in the last `seen_in_last` duration.
/// Limits the result to the most recent 100 drones.
pub async fn get_drones_for_pool(
&self,
cluster: &ClusterName,
pool: &DronePoolName,
seen_in_last: Duration,
) -> sqlx::Result<Vec<DroneWithMetadata>> {
// Converting to a PgInterval fails if the duration has nanosecond granularity.
// So let's round up to the nearest microsecond.
let seen_in_last = if seen_in_last.subsec_nanos() % 1000 == 0 {
seen_in_last
} else {
let nanos = (seen_in_last.subsec_micros() + 1) * 1000;
Duration::new(seen_in_last.as_secs(), nanos)
};

let Ok(seen_in_last) = PgInterval::try_from(seen_in_last) else {
return Err(sqlx::Error::Protocol("invalid interval".to_string()));
};
Expand All @@ -124,8 +133,7 @@ impl<'a> DroneDatabase<'a> {
from node
left join drone on node.id = drone.id
where
drone.ready = true
and controller is not null
controller is not null
and cluster = $1
and now() - drone.last_heartbeat < $2
and pool = $3
Expand Down

0 comments on commit 730fac7

Please sign in to comment.