Skip to content

Commit e4eb9a5

Browse files
committed
adjust find_sectors_by_expiration for not re-quantizing
1 parent 9c8517a commit e4eb9a5

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

actors/miner/src/expiration_queue.rs

+51-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::convert::TryInto;
66

77
use anyhow::{anyhow, Context};
88
use cid::Cid;
9+
use fil_actors_runtime::network::EPOCHS_IN_DAY;
910
use fil_actors_runtime::runtime::Policy;
1011
use fil_actors_runtime::{ActorDowncast, Array};
1112
use fvm_ipld_amt::{Error as AmtError, ValueMut};
@@ -776,47 +777,63 @@ impl<'db, BS: Blockstore> ExpirationQueue<'db, BS> {
776777
sector_size: SectorSize,
777778
sectors: &[SectorOnChainInfo],
778779
) -> anyhow::Result<Vec<SectorExpirationSet>> {
779-
let mut declared_expirations = BTreeMap::<ChainEpoch, bool>::new();
780+
if sectors.is_empty() {
781+
return Ok(Vec::new());
782+
}
783+
780784
let mut sectors_by_number = BTreeMap::<u64, &SectorOnChainInfo>::new();
781785
let mut all_remaining = BTreeSet::<u64>::new();
782786

787+
let mut min_expire = i64::MAX;
788+
let mut max_expire = i64::MIN;
783789
for sector in sectors {
784-
let q_expiration = self.quant.quantize_up(sector.expiration);
785-
declared_expirations.insert(q_expiration, true);
790+
if sector.expiration < min_expire {
791+
min_expire = sector.expiration;
792+
}
793+
if sector.expiration > max_expire {
794+
max_expire = sector.expiration;
795+
}
786796
all_remaining.insert(sector.sector_number);
787797
sectors_by_number.insert(sector.sector_number, sector);
788798
}
789799

790-
let mut expiration_groups =
791-
Vec::<SectorExpirationSet>::with_capacity(declared_expirations.len());
800+
let mut expiration_groups = Vec::<SectorExpirationSet>::with_capacity(sectors.len());
792801

793-
for (&expiration, _) in declared_expirations.iter() {
794-
let es = self.may_get(expiration)?;
802+
let end_epoch = (max_expire + EPOCHS_IN_DAY) as u64;
803+
self.amt.for_each_while_ranged(Some(min_expire as u64), None, |epoch, es| {
804+
if epoch > end_epoch {
805+
return Ok(false);
806+
}
795807

796808
let group = group_expiration_set(
797809
sector_size,
798810
&sectors_by_number,
799811
&mut all_remaining,
800812
es,
801-
expiration,
813+
epoch as ChainEpoch,
802814
);
803815
if !group.sector_epoch_set.sectors.is_empty() {
804816
expiration_groups.push(group);
805817
}
806-
}
818+
819+
Ok(!all_remaining.is_empty())
820+
})?;
807821

808822
// If sectors remain, traverse next in epoch order. Remaining sectors should be
809823
// rescheduled to expire soon, so this traversal should exit early.
810824
if !all_remaining.is_empty() {
825+
// avoid type cast in the for_each_while below
826+
let min_expire = min_expire as u64;
811827
self.amt.for_each_while(|epoch, es| {
812-
let epoch = epoch as ChainEpoch;
813-
// If this set's epoch is one of our declared epochs, we've already processed it
828+
// If this set's epoch is in range [min_expire, end_epoch], we've already processed it
814829
// in the loop above, so skip processing here. Sectors rescheduled to this epoch
815830
// would have been included in the earlier processing.
816-
if declared_expirations.contains_key(&epoch) {
831+
if epoch >= min_expire && epoch <= end_epoch {
817832
return Ok(true);
818833
}
819834

835+
let epoch = epoch as ChainEpoch;
836+
820837
// Sector should not be found in EarlyExpirations which holds faults. An implicit assumption
821838
// of grouping is that it only returns sectors with active power. ExpirationQueue should not
822839
// provide operations that allow this to happen.
@@ -826,7 +843,7 @@ impl<'db, BS: Blockstore> ExpirationQueue<'db, BS> {
826843
sector_size,
827844
&sectors_by_number,
828845
&mut all_remaining,
829-
es.clone(),
846+
es,
830847
epoch,
831848
);
832849

@@ -911,7 +928,7 @@ fn group_expiration_set(
911928
sector_size: SectorSize,
912929
sectors: &BTreeMap<u64, &SectorOnChainInfo>,
913930
include_set: &mut BTreeSet<u64>,
914-
es: ExpirationSet,
931+
es: &ExpirationSet,
915932
expiration: ChainEpoch,
916933
) -> SectorExpirationSet {
917934
let mut sector_numbers = Vec::new();
@@ -927,14 +944,26 @@ fn group_expiration_set(
927944
}
928945
}
929946

930-
SectorExpirationSet {
931-
sector_epoch_set: SectorEpochSet {
932-
epoch: expiration,
933-
sectors: sector_numbers,
934-
power: total_power,
935-
pledge: total_pledge,
936-
},
937-
expiration_set: es,
947+
if sector_numbers.is_empty() {
948+
SectorExpirationSet {
949+
sector_epoch_set: SectorEpochSet {
950+
epoch: expiration,
951+
sectors: sector_numbers,
952+
power: total_power,
953+
pledge: total_pledge,
954+
},
955+
expiration_set: ExpirationSet::default(),
956+
}
957+
} else {
958+
SectorExpirationSet {
959+
sector_epoch_set: SectorEpochSet {
960+
epoch: expiration,
961+
sectors: sector_numbers,
962+
power: total_power,
963+
pledge: total_pledge,
964+
},
965+
expiration_set: es.clone(), // lazy clone
966+
}
938967
}
939968
}
940969

0 commit comments

Comments
 (0)