-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add block processing queue to watcher info #1560
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
968ecb2
[WIP] initial queuing work
2ec0263
refactor: queue processing
cc8950f
catch db timeouts
3abe795
update existing tests
8df1604
add tests
a0f1960
continue with tests
bdd9af5
finish tests
8e0835e
Fix integration test
dbe8594
refactor
5c0d14b
refactor
3e2bee1
Merge branch 'master' into mederic-p/watcher-info-block-processing
3476c71
refactor tests
6293606
remove retry_count
8df12f2
remove on exist genserver shutdown
20b7404
add telemetry queue length event
93b4d05
Merge branch 'master' into mederic-p/watcher-info-block-processing
a602b73
sobelow skip BinToTerm
098be67
remove pending block status
7f85dea
Merge branch 'master' into mederic-p/watcher-info-block-processing
0cecf68
naming
8c337cd
fix tests
aee4bf7
fix tests
1813caf
rename config
0ee6d37
remove unused alias
28c95fe
Merge remote-tracking branch 'origin/master' into mederic-p/watcher-i…
1a299a3
fix PR minor comments
277e574
missing file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
apps/omg_watcher_info/lib/omg_watcher_info/configuration.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.WatcherInfo.Configuration do | ||
@moduledoc """ | ||
Provides access to applications configuration | ||
""" | ||
@app :omg_watcher_info | ||
|
||
def pending_block_processing_interval() do | ||
Application.fetch_env!(@app, :pending_block_processing_interval) | ||
end | ||
|
||
def block_queue_check_interval() do | ||
Application.fetch_env!(@app, :block_queue_check_interval) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ defmodule OMG.WatcherInfo.DB.Block do | |
alias OMG.Utils.Paginator | ||
alias OMG.WatcherInfo.DB | ||
alias OMG.WatcherInfo.DB.Block.Chunk | ||
alias OMG.WatcherInfo.DB.PendingBlock | ||
|
||
import Ecto.Query, only: [from: 2] | ||
|
||
|
@@ -142,16 +143,19 @@ defmodule OMG.WatcherInfo.DB.Block do | |
end | ||
|
||
@doc """ | ||
Inserts complete and sorted enumerable of transactions for particular block number | ||
Takes a pending block, decode its data and inserts its content in the database. | ||
""" | ||
@spec insert_with_transactions(mined_block()) :: {:ok, %__MODULE__{}} | ||
def insert_with_transactions(%{ | ||
transactions: transactions, | ||
blknum: block_number, | ||
blkhash: blkhash, | ||
timestamp: timestamp, | ||
eth_height: eth_height | ||
}) do | ||
# sobelow_skip ["Misc.BinToTerm"] | ||
mederic-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@spec insert_from_pending_block(PendingBlock.t()) :: {:ok, %__MODULE__{}} | {:error, any()} | ||
def insert_from_pending_block(pending_block) do | ||
%{ | ||
transactions: transactions, | ||
blknum: block_number, | ||
blkhash: blkhash, | ||
timestamp: timestamp, | ||
eth_height: eth_height | ||
} = :erlang.binary_to_term(pending_block.data) | ||
|
||
{db_txs, db_outputs, db_inputs} = prepare_db_transactions(transactions, block_number) | ||
|
||
current_block = %{ | ||
|
@@ -170,6 +174,7 @@ defmodule OMG.WatcherInfo.DB.Block do | |
|> prepare_inserts(db_txs_stream, "db_txs_", DB.Transaction) | ||
|> prepare_inserts(db_outputs_stream, "db_outputs_", DB.TxOutput) | ||
|> DB.TxOutput.spend_utxos(db_inputs) | ||
|> Ecto.Multi.delete("pending_block", pending_block, []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :mindblown: Interesting approach |
||
|
||
{insert_duration, result} = :timer.tc(DB.Repo, :transaction, [multi]) | ||
|
||
|
66 changes: 66 additions & 0 deletions
66
apps/omg_watcher_info/lib/omg_watcher_info/db/pending_block.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.WatcherInfo.DB.PendingBlock do | ||
@moduledoc """ | ||
Ecto schema for pending block data | ||
These are valid block data received by the internal bus that should be stored in the database. | ||
This intermediate table is needed as the messages received by the bus are not persisted | ||
and if for some reason the writing to the database fails, we would lose these data. | ||
""" | ||
use Ecto.Schema | ||
use OMG.Utils.LoggerExt | ||
|
||
import Ecto.Changeset | ||
import Ecto.Query, only: [from: 2] | ||
|
||
alias OMG.WatcherInfo.DB.Repo | ||
|
||
@type t() :: %{ | ||
blknum: pos_integer(), | ||
data: binary() | ||
} | ||
|
||
@primary_key {:blknum, :integer, []} | ||
|
||
schema "pending_blocks" do | ||
field(:data, :binary) | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
@spec insert(map()) :: {:ok, %__MODULE__{}} | {:error, Ecto.Changeset.t()} | ||
def insert(params) do | ||
params | ||
|> insert_changeset() | ||
|> Repo.insert() | ||
end | ||
|
||
@spec get_next_to_process() :: nil | %__MODULE__{} | ||
def get_next_to_process() do | ||
(pending_block in __MODULE__) | ||
|> from(order_by: :blknum, limit: 1) | ||
|> Repo.one() | ||
end | ||
|
||
@spec get_count() :: non_neg_integer() | ||
def get_count(), do: Repo.aggregate(__MODULE__, :count) | ||
|
||
defp insert_changeset(params) do | ||
%__MODULE__{} | ||
|> cast(params, [:blknum, :data]) | ||
|> validate_required([:blknum, :data]) | ||
|> unique_constraint(:blknum, name: :pending_blocks_pkey) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2019-2020 OmiseGO Pte Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
defmodule OMG.WatcherInfo.Measure do | ||
@moduledoc """ | ||
Counting business metrics sent to Datadog. | ||
""" | ||
import OMG.Status.Metric.Event, only: [name: 1] | ||
|
||
alias OMG.Status.Metric.Datadog | ||
alias OMG.WatcherInfo.PendingBlockQueueLengthChecker | ||
|
||
@supported_events [ | ||
[:pending_block_queue_length, PendingBlockQueueLengthChecker] | ||
] | ||
|
||
def supported_events(), do: @supported_events | ||
|
||
def handle_event([:pending_block_queue_length, PendingBlockQueueLengthChecker], %{length: length}, _state, _config) do | ||
_ = Datadog.gauge(name(:pending_block_queue_length), length) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PendingBlock
is somewhat different naming then I would expect. the worldqueue
is not used anywhere :)))