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

[db]log mptrie node #3634

Merged
merged 26 commits into from
Jan 13, 2023
Merged

[db]log mptrie node #3634

merged 26 commits into from
Jan 13, 2023

Conversation

millken
Copy link
Contributor

@millken millken commented Sep 9, 2022

Description

record mptrie node infomation to postgres DB.

Node Type Node Key Node Path Node Children
branchNode
extensionNode
leafNode
test=# select * from log_mptrie order by id asc limit 10;
 id | node_type |                             node_key                             |                          node_path                           | node_children |        last_visited        
----+-----------+------------------------------------------------------------------+--------------------------------------------------------------+---------------+----------------------------
  1 | b         |                                                                  |                                                              |               | 2022-10-11 15:13:52.755372 
  7 | l         | 0000000000000000000000000000000000000000000000000000000000000000 |                                                              |               | 2022-10-11 15:13:00.320674 
  8 | b         |                                                                  |                                                              | 00            | 2022-10-11 15:13:00.349906 
 23 | l         | 0000000000000000000000000000000000000000000000000000000000000002 |                                                              |               | 2022-10-11 15:13:00.322241 
 24 | b         |                                                                  |                                                              | 0002          | 2022-10-11 15:13:00.339253 
 25 | e         |                                                                  | 000000000000000000000000000000000000000000000000000000000000 |               | 2022-10-11 15:13:00.483378 
 36 | l         | 0000000000000000000000000000000000000000000000000000000000000003 |                                                              |               | 2022-10-11 15:13:00.340525 
 37 | b         |                                                                  |                                                              | 000203        | 2022-10-11 15:13:00.480425 
 44 | l         | 17ef568e3e12ab5b9c7254a8d58478811de00f9e6eb34345acd53bf8fd09d3ec |                                                              |               | 2022-10-11 15:13:52.794232 
 45 | b         |                                                                  |                                                              | 0017          | 2022-10-11 15:13:00.4656   
(10 rows)

Fixes #3204

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@millken millken requested a review from a team as a code owner September 9, 2022 07:12
@codecov
Copy link

codecov bot commented Sep 9, 2022

Codecov Report

Merging #3634 (1b1944d) into master (263ff82) will increase coverage by 0.60%.
The diff coverage is 81.32%.

@@            Coverage Diff             @@
##           master    #3634      +/-   ##
==========================================
+ Coverage   74.47%   75.07%   +0.60%     
==========================================
  Files         269      299      +30     
  Lines       23925    25202    +1277     
==========================================
+ Hits        17818    18921    +1103     
- Misses       5174     5302     +128     
- Partials      933      979      +46     
Impacted Files Coverage Δ
action/actctx.go 90.62% <ø> (+6.25%) ⬆️
action/action.go 82.35% <ø> (ø)
action/action_deserializer.go 57.14% <ø> (ø)
action/candidate_update.go 88.88% <0.00%> (-4.14%) ⬇️
action/consignment_transfer.go 90.41% <ø> (ø)
action/const.go 100.00% <ø> (ø)
action/execution.go 83.78% <ø> (ø)
action/grantreward.go 68.57% <ø> (ø)
action/protocol/account/protocol.go 86.90% <ø> (ø)
action/protocol/account/transfer.go 84.61% <ø> (ø)
... and 276 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Liuhaai
Copy link
Member

Liuhaai commented Sep 9, 2022

how does this pr solve #3204 ?

@millken millken marked this pull request as draft September 13, 2022 02:18
@millken millken changed the title add collector package to monitor node cpu/loadavg/memory usage events for node Sep 13, 2022
millken and others added 5 commits September 13, 2022 20:41
* check nil for response.EpochData (#3642)

* remove unused db/sql (#3645)

* [ioctl] Build Read unittest (#3635)

* build Read unittest

* fix code smells

* remove redundant argument

* fix error

* remove redundant code

* remove redundant space

* delete useless code

Co-authored-by: huof6890 <[email protected]>

* [pkg] rotate log file (#3638)

* add lumberjack

* add lograte conf in Dockerfile

* del space line

* [ioctl] Build did geturi command line into new ioctl (#3650)

* [ioctl] build did geturi command line into new ioctl

* add test data

* move abi reader to init

* build unittest to cover the modification

* move global var _didABI to did.go

* move abi reader to did

* alter did string with did prefix

* rename did string

Co-authored-by: huof6890 <[email protected]>

* [ioctl] build did gethash command line into new ioctl (#3639)

* [ioctl] build did gethash command line into new ioctl

* remove redundant code

* build unittest to cover the modification

* remove redundant args

* add command description

* modify test value

* remove func encodeGet

* remove func encodeGet

* build unittest to cover the modification

* reset default gas limit

* use fault address from identityset

* remove redundant space

* update command name

* move abi reader to init

* move abi reader to did

* alter did string with did prefix

* rename did string

Co-authored-by: huofei <[email protected]>

* [factory] add error details (#3657)

Co-authored-by: huofei <[email protected]>
Co-authored-by: Jeremy Chou <[email protected]>
Co-authored-by: CoderZhi <[email protected]>
@millken millken changed the title events for node [db]log mptrie node Oct 11, 2022
@millken millken marked this pull request as ready for review October 11, 2022 07:38
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
5.0% 5.0% Duplication

}

// ParseNodeEvent parse the node event
func ParseNodeEvent(b []byte) (NodeEvent, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to unit test and rename to private function

return logFile.Close()
}

func logNode(n node) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameters:

  1. node type, leaf, extension, and branch
  2. action type, e.g., read and write, or search, delete, and new
  3. hashvalue, creating via cacheNode.Hash()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create hashvalue inside of logNode function. Other than that, it looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment on lines +148 to +152
defer func() {
if err = mptrie.CloseLogDB(); err != nil {
log.L().Error("Failed to close mptrie log DB.", zap.Error(err))
}
}()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function will be called right away. It seems that the PR has not been fully tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has been tested OK

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know. I have wrong understanding of defer

@millken millken requested a review from dustinxie as a code owner December 29, 2022 02:44
@@ -55,15 +54,14 @@ func newExtensionNodeFromProtoPb(pb *triepb.ExtendPb, hashVal []byte) *extension
child: newHashNode(pb.Value),
}
e.cacheNode.serializable = e
if err := logNode(_nodeTypeExtension, _actionTypeNew, hashVal, e); err != nil {
if err := logNode(_nodeTypeExtension, _actionTypeNew, e, nil); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not working here

@@ -93,7 +91,7 @@ func newBranchNodeFromProtoPb(pb *triepb.BranchPb, hashVal []byte) *branchNode {
}
bnode.indices = NewSortedList(bnode.children)
bnode.cacheNode.serializable = bnode
if err := logNode(_nodeTypeBranch, _actionTypeNew, hashVal, bnode); err != nil {
if err := logNode(_nodeTypeBranch, _actionTypeNew, bnode, nil); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not working here

@@ -55,7 +54,7 @@ func newLeafNodeFromProtoPb(pb *triepb.LeafPb, hashVal []byte) *leafNode {
value: pb.Value,
}
l.cacheNode.serializable = l
if err := logNode(_nodeTypeLeaf, _actionTypeNew, hashVal, l); err != nil {
if err := logNode(_nodeTypeLeaf, _actionTypeNew, l, nil); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not working here

Copy link
Contributor Author

@millken millken Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too at first.
see the following code, first, it will check hashVal, only use cli if hashVal is not set.

func (cn *cacheNode) hash(cli client, flush bool) ([]byte, error) {
if len(cn.hashVal) != 0 {
return cn.hashVal, nil
}
if cli == nil {
return []byte{}, errors.New("client cannot be nil")
}
pb, err := cn.proto(cli, flush)

}
if event.KeyLen != test.event.KeyLen {
t.Errorf("unexpected key length %v", event.KeyLen)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use same test code style? like

require.NoError(err)
require.True(event.NodeType == test.event.NodeType)

@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
3.5% 3.5% Duplication

@millken millken merged commit dab026e into iotexproject:master Jan 13, 2023
@@ -85,6 +91,9 @@ func newBranchNodeFromProtoPb(pb *triepb.BranchPb, hashVal []byte) *branchNode {
}
bnode.indices = NewSortedList(bnode.children)
bnode.cacheNode.serializable = bnode
if err := logNode(_nodeTypeBranch, _actionTypeNew, bnode, nil); err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is panic used when logging fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newBranchNodeFromProtoPb does not handle errors, directly returns *branchNode, logNode should not fail here either

@@ -69,6 +72,9 @@ func newRootBranchNode(cli client, children map[byte]node, indices *SortedList,
}
}
}
if err := logNode(_nodeTypeBranch, _actionTypeNew, bnode, cli); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is logging used at the end of function instead of at the head?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait for the bnode Initialized

@@ -0,0 +1,127 @@
package mptrie
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the name lognode.go is misleading. log.go is a better choice

type actionType byte

const (
_nodeTypeLeaf nodeType = 'l'
Copy link
Member

@Liuhaai Liuhaai Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a byte array [10]byte be used here? The meaning of the data as a byte is hard to read

return err
}
// write event body
_, err = logWriter.Write(event.Bytes())
Copy link
Member

@Liuhaai Liuhaai Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to read the binary data in the db? Do we have the tool or component to print the data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A byte is used to save the size of the binrary. Binary itself is not readable and needs to be processed by tool. In the lognode_test.go, there is a parseNodeEvent that can parse the binrary.

How the tools are provided needs to be discussed (print, database)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory usage monitor/control for mptrie
4 participants