helium / blockchain-etl

Blockchain follower that follows and stores the Helium blockchain
Apache License 2.0
64 stars 37 forks source link

ara/implement-copy-method #310

Closed anthonyra closed 11 months ago

anthonyra commented 2 years ago

The pull request implements the COPY method for sending data to postgres. It also has the needed changes to use this copy method with transaction_actors.

anthonyra commented 2 years ago

When adding a block to the database the ETL uses BEGIN to batch all of the transactions into one. The COPY method however appears (I haven't seen any examples or documentation proving otherwise) to not be compatible with BEGIN. Meaning the COPY is initiated before the COMMIT is called on BEGIN. This creates a race condition that causes the transaction to be ROLLBACK due to foreign_key constraints.

It however works if the copies are after the initial COMMIT of a block. The copies are atomic in nature so if one fails we don't have a partial insert of data. With this in mind we could monitor said copies and ensure they are committed to the DB. If they fail we could crash like what is done with BEGIN.

These options have compromises though, so the real question is "does the performance improvements warrant such compromises"...

Some performance numbers with using COPY for the transactions and transaction_actors tables;

**Normal Block**
be_db_follower:maybe_log_duration:123 be_db_block took 6895 ms
be_db_block:q_copy_transactions:247 Txns copy list took 1507 ms. CopyLists (1596)
be_db_follower:maybe_log_duration:123 be_db_txn_actor took 17243 ms
q_copy_transaction_actors:127 Txn actors copy list took 5883 ms. CopyLists (7525)
...
be_db_follower:load_block:107 Stored block: 1199717 took: 33563 ms

**Reward Block**
be_db_follower:maybe_log_duration:123 be_db_block took 46886 ms
be_db_block:q_copy_transactions:247 Txns copy list took 11469 ms. CopyLists (2)
be_db_follower:maybe_log_duration:123 be_db_txn_actor took 330198 ms
be_db_txn_actor:q_copy_transaction_actors:127 Txn actors copy list took 164007 ms. CopyLists (168938)
...
be_db_follower:load_block:107 Stored block: 1199698 took: 792255 ms