Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/index corruption with clippy fixes and rename #2275

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bitcoin = { version = "0.29.1", features = ["rand"] }
boilerplate = { version = "0.2.3", features = ["axum"] }
chrono = "0.4.19"
clap = { version = "3.2.18", features = ["derive", "deprecated"] }
ctrlc = "3.2.1"
ctrlc = { version = "3.2.1", features = ["termination"] }
derive_more = "0.99.17"
dirs = "5.0.0"
env_logger = "0.10.0"
Expand Down
8 changes: 3 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ impl Index {
blocks_indexed: wtx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
Expand Down Expand Up @@ -739,8 +738,7 @@ impl Index {
let current = tx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| height)
.map(|x| x.value())
Expand Down Expand Up @@ -811,7 +809,7 @@ impl Index {
let inscription_number_to_inscription_id =
rtx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;

let latest = match inscription_number_to_inscription_id.iter()?.rev().next() {
let latest = match inscription_number_to_inscription_id.iter()?.next_back() {
Some(Ok((number, _id))) => number.value(),
Some(Err(_)) => return Ok(Default::default()),
None => return Ok(Default::default()),
Expand Down
9 changes: 3 additions & 6 deletions src/index/rtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ impl Rtx<'_> {
.0
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| Height(height.value())),
)
Expand All @@ -22,8 +21,7 @@ impl Rtx<'_> {
.0
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0),
Expand All @@ -44,8 +42,7 @@ impl Rtx<'_> {
.0
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(_height, hash)| BlockHash::load(*hash.value())),
),
Expand Down
6 changes: 2 additions & 4 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ impl Updater {
let height = wtx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);
Expand Down Expand Up @@ -140,8 +139,7 @@ impl Updater {
let height = wtx
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(height, _hash)| height.value() + 1)
.unwrap_or(0);
Expand Down
6 changes: 2 additions & 4 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {

let next_number = number_to_id
.iter()?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(number, _id)| number.value() + 1)
.unwrap_or(0);
Expand Down Expand Up @@ -173,8 +172,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
let seq_num = self
.reinscription_id_to_seq_num
.iter()?
.rev()
.next()
.next_back()
.and_then(|result| result.ok())
.map(|(_id, number)| number.value() + 1)
.unwrap_or(0);
Expand Down
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const CYCLE_EPOCHS: u64 = 6;

static SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
static LISTENERS: Mutex<Vec<axum_server::Handle>> = Mutex::new(Vec::new());
static INDEXER: Mutex<Option<thread::JoinHandle<()>>> = Mutex::new(Option::None);

fn integration_test() -> bool {
env::var_os("ORD_INTEGRATION_TEST")
Expand All @@ -149,21 +150,32 @@ fn unbound_outpoint() -> OutPoint {
}
}

fn gracefully_shutdown_indexer() {
if let Some(indexer) = INDEXER.lock().unwrap().take() {
// We explicitly set this to true to notify the thread to not take on new work
SHUTTING_DOWN.store(true, atomic::Ordering::Relaxed);
log::info!("Waiting for index thread to finish...");
if indexer.join().is_err() {
log::warn!("Index thread panicked; join failed");
}
}
}

pub fn main() {
env_logger::init();

ctrlc::set_handler(move || {
if SHUTTING_DOWN.fetch_or(true, atomic::Ordering::Relaxed) {
process::exit(1);
}

println!("Shutting down gracefully. Press <CTRL-C> again to shutdown immediately.");

LISTENERS
.lock()
.unwrap()
.iter()
.for_each(|handle| handle.graceful_shutdown(Some(Duration::from_millis(100))));

println!("Shutting down gracefully. Press <CTRL-C> again to shutdown immediately.");

if SHUTTING_DOWN.fetch_or(true, atomic::Ordering::Relaxed) {
process::exit(1);
}
})
.expect("Error setting <CTRL-C> handler");

Expand All @@ -179,6 +191,11 @@ pub fn main() {
{
eprintln!("{}", err.backtrace());
}

gracefully_shutdown_indexer();

process::exit(1);
}

gracefully_shutdown_indexer();
}
10 changes: 7 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ pub(crate) struct Server {
impl Server {
pub(crate) fn run(self, options: Options, index: Arc<Index>, handle: Handle) -> Result {
Runtime::new()?.block_on(async {
let clone = index.clone();
thread::spawn(move || loop {
if let Err(error) = clone.update() {
let index_clone = index.clone();
let index_thread = thread::spawn(move || loop {
if SHUTTING_DOWN.load(atomic::Ordering::Relaxed) {
break;
}
if let Err(error) = index_clone.update() {
log::warn!("{error}");
}
thread::sleep(Duration::from_millis(5000));
});
INDEXER.lock().unwrap().replace(index_thread);

let config = options.load_config()?;
let acme_domains = self.acme_domains()?;
Expand Down