Skip to content

Commit aa530c6

Browse files
authored
[1/n] [installinator] add PeerAddress wrapper (#8035)
1 parent 81bfe57 commit aa530c6

3 files changed

Lines changed: 126 additions & 73 deletions

File tree

‎installinator/src/dispatch.rs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use update_engine::StepResult;
2424

2525
use crate::{
2626
artifact::ArtifactIdOpts,
27-
peers::{DiscoveryMechanism, FetchedArtifact, Peers},
27+
peers::{DiscoveryMechanism, FetchedArtifact, HttpPeers, Peers},
2828
reporter::ProgressReporter,
2929
write::{ArtifactWriter, WriteDestination},
3030
};
@@ -92,7 +92,10 @@ impl DebugDiscoverOpts {
9292
async fn exec(self, log: &slog::Logger) -> Result<()> {
9393
let peers = Peers::new(
9494
log,
95-
self.opts.mechanism.discover_peers(log).await?,
95+
Box::new(HttpPeers::new(
96+
log,
97+
self.opts.mechanism.discover_peers(log).await?,
98+
)),
9699
Duration::from_secs(10),
97100
);
98101
println!("discovered peers: {}", peers.display());
@@ -190,7 +193,10 @@ impl InstallOpts {
190193
async move {
191194
Ok(Peers::new(
192195
&log,
193-
discovery.discover_peers(&log).await?,
196+
Box::new(HttpPeers::new(
197+
&log,
198+
discovery.discover_peers(&log).await?,
199+
)),
194200
Duration::from_secs(10),
195201
))
196202
}
@@ -234,7 +240,7 @@ impl InstallOpts {
234240
)
235241
.await?;
236242

237-
let address = host_phase_2_artifact.addr;
243+
let address = host_phase_2_artifact.peer.address();
238244

239245
StepSuccess::new(host_phase_2_artifact)
240246
.with_metadata(
@@ -273,7 +279,7 @@ impl InstallOpts {
273279
)
274280
.await?;
275281

276-
let address = control_plane_artifact.addr;
282+
let address = control_plane_artifact.peer.address();
277283

278284
StepSuccess::new(control_plane_artifact)
279285
.with_metadata(
@@ -495,7 +501,10 @@ async fn fetch_artifact(
495501
|| async {
496502
Ok(Peers::new(
497503
&log,
498-
discovery.discover_peers(&log).await?,
504+
Box::new(HttpPeers::new(
505+
&log,
506+
discovery.discover_peers(&log).await?,
507+
)),
499508
Duration::from_secs(10),
500509
))
501510
},
@@ -508,7 +517,7 @@ async fn fetch_artifact(
508517
log,
509518
"fetched {} bytes from {}",
510519
artifact.artifact.num_bytes(),
511-
artifact.addr,
520+
artifact.peer,
512521
);
513522

514523
Ok(artifact)

‎installinator/src/mock_peers.rs‎

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ use uuid::Uuid;
2727

2828
use crate::{
2929
errors::HttpError,
30-
peers::{FetchReceiver, PeersImpl},
30+
peers::{FetchReceiver, PeerAddress, PeersImpl},
3131
};
3232

3333
struct MockPeersUniverse {
3434
artifact: Bytes,
35-
peers: BTreeMap<SocketAddr, MockPeer>,
35+
peers: BTreeMap<PeerAddress, MockPeer>,
3636
attempt_bitmaps: Vec<AttemptBitmap>,
3737
}
3838

@@ -49,7 +49,7 @@ impl fmt::Debug for MockPeersUniverse {
4949
impl MockPeersUniverse {
5050
fn new(
5151
artifact: Bytes,
52-
peers: BTreeMap<SocketAddr, MockPeer>,
52+
peers: BTreeMap<PeerAddress, MockPeer>,
5353
attempt_bitmaps: Vec<AttemptBitmap>,
5454
) -> Self {
5555
assert!(peers.len() <= 32, "this test only supports up to 32 peers");
@@ -70,7 +70,7 @@ impl MockPeersUniverse {
7070
// being unique identifiers. This means that this code can use a BTreeMap rather than a
7171
// fancier structure like an IndexMap.
7272
let peers_strategy = prop::collection::btree_map(
73-
any::<SocketAddr>(),
73+
any::<PeerAddress>(),
7474
any::<MockResponse_>(),
7575
0..max_peer_count,
7676
);
@@ -82,7 +82,7 @@ impl MockPeersUniverse {
8282
(artifact_strategy, peers_strategy, attempt_bitmaps_strategy).prop_map(
8383
|(artifact, peers, attempt_bitmaps): (
8484
Vec<u8>,
85-
BTreeMap<SocketAddr, MockResponse_>,
85+
BTreeMap<PeerAddress, MockResponse_>,
8686
Vec<AttemptBitmap>,
8787
)| {
8888
let artifact = Bytes::from(artifact);
@@ -107,7 +107,7 @@ impl MockPeersUniverse {
107107
fn expected_result(
108108
&self,
109109
timeout: Duration,
110-
) -> Result<(usize, SocketAddr), usize> {
110+
) -> Result<(usize, PeerAddress), usize> {
111111
self.attempts()
112112
.enumerate()
113113
.filter_map(|(attempt, peers)| {
@@ -174,20 +174,20 @@ enum AttemptBitmap {
174174
struct MockPeers {
175175
artifact: Bytes,
176176
// Peers within the universe that have been selected
177-
selected_peers: BTreeMap<SocketAddr, MockPeer>,
177+
selected_peers: BTreeMap<PeerAddress, MockPeer>,
178178
}
179179

180180
impl MockPeers {
181-
fn get(&self, addr: SocketAddr) -> Option<&MockPeer> {
182-
self.selected_peers.get(&addr)
181+
fn get(&self, peer: PeerAddress) -> Option<&MockPeer> {
182+
self.selected_peers.get(&peer)
183183
}
184184

185-
fn peers(&self) -> impl Iterator<Item = (&SocketAddr, &MockPeer)> + '_ {
185+
fn peers(&self) -> impl Iterator<Item = (&PeerAddress, &MockPeer)> + '_ {
186186
self.selected_peers.iter()
187187
}
188188

189189
/// Returns the peer that can return the entire dataset within the timeout.
190-
fn successful_peer(&self, timeout: Duration) -> Option<SocketAddr> {
190+
fn successful_peer(&self, timeout: Duration) -> Option<PeerAddress> {
191191
self.peers()
192192
.filter_map(|(addr, peer)| {
193193
if peer.artifact != self.artifact {
@@ -233,7 +233,7 @@ impl MockPeers {
233233

234234
#[async_trait]
235235
impl PeersImpl for MockPeers {
236-
fn peers(&self) -> Box<dyn Iterator<Item = SocketAddr> + Send + '_> {
236+
fn peers(&self) -> Box<dyn Iterator<Item = PeerAddress> + Send + '_> {
237237
Box::new(self.selected_peers.keys().copied())
238238
}
239239

@@ -243,7 +243,7 @@ impl PeersImpl for MockPeers {
243243

244244
async fn fetch_from_peer_impl(
245245
&self,
246-
peer: SocketAddr,
246+
peer: PeerAddress,
247247
// We don't (yet) use the artifact ID in MockPeers
248248
_artifact_hash_id: ArtifactHashId,
249249
) -> Result<(u64, FetchReceiver), HttpError> {
@@ -261,7 +261,7 @@ impl PeersImpl for MockPeers {
261261

262262
async fn report_progress_impl(
263263
&self,
264-
_peer: SocketAddr,
264+
_peer: PeerAddress,
265265
_update_id: Uuid,
266266
_report: EventReport,
267267
) -> Result<(), ClientError> {
@@ -469,26 +469,31 @@ struct MockReportPeers {
469469

470470
impl MockReportPeers {
471471
// SocketAddr::new is not a const fn in stable Rust as of this writing
472-
fn valid_peer() -> SocketAddr {
473-
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 2000)
472+
fn valid_peer() -> PeerAddress {
473+
PeerAddress::new(SocketAddr::new(
474+
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
475+
2000,
476+
))
474477
}
475478

476-
fn invalid_peer() -> SocketAddr {
477-
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)), 2000)
479+
fn invalid_peer() -> PeerAddress {
480+
PeerAddress::new(SocketAddr::new(
481+
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2)),
482+
2000,
483+
))
478484
}
479485

480-
fn unresponsive_peer() -> SocketAddr {
481-
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)), 2000)
482-
}
483-
484-
fn new(update_id: Uuid, report_sender: mpsc::Sender<EventReport>) -> Self {
485-
Self { update_id, report_sender }
486+
fn unresponsive_peer() -> PeerAddress {
487+
PeerAddress::new(SocketAddr::new(
488+
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3)),
489+
2000,
490+
))
486491
}
487492
}
488493

489494
#[async_trait]
490495
impl PeersImpl for MockReportPeers {
491-
fn peers(&self) -> Box<dyn Iterator<Item = SocketAddr> + Send + '_> {
496+
fn peers(&self) -> Box<dyn Iterator<Item = PeerAddress> + Send + '_> {
492497
Box::new(
493498
[
494499
Self::valid_peer(),
@@ -505,7 +510,7 @@ impl PeersImpl for MockReportPeers {
505510

506511
async fn fetch_from_peer_impl(
507512
&self,
508-
_peer: SocketAddr,
513+
_peer: PeerAddress,
509514
_artifact_hash_id: ArtifactHashId,
510515
) -> Result<(u64, FetchReceiver), HttpError> {
511516
unimplemented!(
@@ -516,7 +521,7 @@ impl PeersImpl for MockReportPeers {
516521

517522
async fn report_progress_impl(
518523
&self,
519-
peer: SocketAddr,
524+
peer: PeerAddress,
520525
update_id: Uuid,
521526
report: EventReport,
522527
) -> Result<(), ClientError> {
@@ -598,10 +603,10 @@ mod tests {
598603
async move {
599604
Ok(Peers::new(
600605
&reporter_log,
601-
Box::new(MockReportPeers::new(
606+
Box::new(MockReportPeers {
602607
update_id,
603608
report_sender,
604-
)),
609+
}),
605610
// The timeout is currently unused by broadcast_report.
606611
Duration::from_secs(10),
607612
))
@@ -620,7 +625,7 @@ mod tests {
620625
let artifact =
621626
fetch_artifact(&cx, &log, attempts, timeout)
622627
.await?;
623-
let address = artifact.addr;
628+
let address = artifact.peer.address();
624629
StepSuccess::new(artifact)
625630
.with_metadata(
626631
InstallinatorCompletionMetadata::Download {
@@ -652,21 +657,21 @@ mod tests {
652657
match (expected_result, fetched_artifact) {
653658
(
654659
Ok((expected_attempt, expected_addr)),
655-
Ok(FetchedArtifact { attempt, addr, mut artifact }),
660+
Ok(FetchedArtifact { attempt, peer, mut artifact }),
656661
) => {
657662
assert_eq!(
658663
expected_attempt, attempt,
659664
"expected successful attempt is the same as actual attempt"
660665
);
661666
assert_eq!(
662-
expected_addr, addr,
667+
expected_addr, peer,
663668
"expected successful peer is the same as actual peer"
664669
);
665670
let artifact = artifact.copy_to_bytes(artifact.num_bytes());
666671
assert_eq!(
667672
expected_artifact, artifact,
668673
"correct artifact fetched from peer {}",
669-
addr,
674+
peer,
670675
);
671676
}
672677
(Err(_), Err(_)) => {}
@@ -716,7 +721,7 @@ mod tests {
716721

717722
fn assert_reports(
718723
reports: &[EventReport],
719-
expected_result: Result<(usize, SocketAddr), usize>,
724+
expected_result: Result<(usize, PeerAddress), usize>,
720725
) {
721726
let all_step_events: Vec<_> =
722727
reports.iter().flat_map(|report| &report.step_events).collect();
@@ -740,7 +745,7 @@ mod tests {
740745
fn assert_success_events(
741746
all_step_events: Vec<&StepEvent>,
742747
expected_attempt: usize,
743-
expected_addr: SocketAddr,
748+
expected_peer: PeerAddress,
744749
) {
745750
let mut saw_success = false;
746751

@@ -753,7 +758,8 @@ mod tests {
753758
peer,
754759
} => {
755760
assert_ne!(
756-
*peer, expected_addr,
761+
*peer,
762+
expected_peer.address(),
757763
"peer cannot match since this is the last attempt"
758764
);
759765
}
@@ -795,7 +801,8 @@ mod tests {
795801
..
796802
} => {
797803
assert_eq!(
798-
*address, expected_addr,
804+
*address,
805+
expected_peer.address(),
799806
"address matches expected"
800807
);
801808
}

0 commit comments

Comments
 (0)