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

Remove panic from crdt primitive #636

Merged
merged 8 commits into from
Sep 29, 2023

Conversation

fourjae
Copy link
Contributor

@fourjae fourjae commented Sep 5, 2023

What this PR does / why we need it:

Action to remove panic method from crdt/primitive.go

I am leaving a question because I encountered a problem while uninstalling crdt/primitive.go Panic.

In the case of the func (p *Primitive) Marshal() method inside the crdt/primitive.go package, Marshal() of the type Element interface is implemented and used.

However, if you modify the interface to Marshal() (string, error), the related implementations must be modified.

At this time, Marshal() is called when calling other String() methods, and I am asking because I think it will be difficult to change this part of String().

func (n *RGATreeListNode) String() string {

Please let me know if I'm missing something or if there's a better way. thank you

Which issue(s) this PR fixes:

Fixes #497 crdt/primitive

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

@krapie krapie requested review from hackerwins and krapie September 5, 2023 16:29
@krapie krapie added the cleanup 🧹 Paying off technical debt label Sep 5, 2023
@krapie krapie removed their request for review September 5, 2023 16:33
@krapie
Copy link
Member

krapie commented Sep 6, 2023

@fourjae I think we need to change Marshal() string interface to Marshal() (string, error), just like when we removed panic() in crdt/text.

In #522, we changed DeepCopy() Element signature to DeepCopy() (Element, error), and updated all related codes.

Changing Marshal() will affect many other related codes including String(), but I think we need to update them all. It will take some time.

@krapie krapie self-requested a review September 6, 2023 10:28
@fourjae
Copy link
Contributor Author

fourjae commented Sep 6, 2023

As we progressed, we encountered this problem. What do you think?

I'm trying to fix crdt/primitive marshal(), but I have the following problem.

  1. Marshal() implements Marshal() of type Element interface. Therefore, the return format has been modified to (String, error) and is in progress.
  2. Among the implemented Marshal()s, Marshal(), which receives the *Object structure as an argument, is used when calling from String() in crdt/rga_tree_list.go.
  3. The String() is used in pkg/llrb.go and pkg/splay.go and implements the type Value interface. String() is implemented and used in approximately 12,000 places.

As a result, all String() methods must be changed to handle errors in marshal().

Do you have any good troubleshooting methods?

@krapie
Copy link
Member

krapie commented Sep 12, 2023

As we progressed, we encountered this problem. What do you think?

I'm trying to fix crdt/primitive marshal(), but I have the following problem.

  1. Marshal() implements Marshal() of type Element interface. Therefore, the return format has been modified to (String, error) and is in progress.
  2. Among the implemented Marshal()s, Marshal(), which receives the *Object structure as an argument, is used when calling from String() in crdt/rga_tree_list.go.
  3. The String() is used in pkg/llrb.go and pkg/splay.go and implements the type Value interface. String() is implemented and used in approximately 12,000 places.

As a result, all String() methods must be changed to handle errors in marshal().

Do you have any good troubleshooting methods?

@hackerwins Any ideas on this issue?

Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

For discussion record: It will be nice to move panic() to the json layer so that we can catch errors at the json level and change types in the crdt layer to never emit errors on the crdt layer. This will prevent interface changes on crdt layer.

@fourjae fourjae force-pushed the remove-panic-from-crdt-primitive branch from 5b78e7d to bb79fcd Compare September 20, 2023 13:15
@codecov
Copy link

codecov bot commented Sep 20, 2023

Codecov Report

Attention: 34 lines in your changes are missing coverage. Please review.

Comparison is base (940941a) 49.47% compared to head (7aad26d) 49.31%.
Report is 1 commits behind head on main.

❗ Current head 7aad26d differs from pull request most recent head b5f4740. Consider uploading reports for the commit b5f4740 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #636      +/-   ##
==========================================
- Coverage   49.47%   49.31%   -0.16%     
==========================================
  Files          69       69              
  Lines        9951     9973      +22     
==========================================
- Hits         4923     4918       -5     
- Misses       4512     4532      +20     
- Partials      516      523       +7     
Files Coverage Δ
api/converter/to_bytes.go 57.37% <ø> (ø)
api/converter/to_pb.go 52.75% <ø> (ø)
pkg/document/crdt/rga_tree_list.go 76.68% <50.00%> (-1.22%) ⬇️
pkg/document/crdt/array.go 32.25% <0.00%> (-0.71%) ⬇️
api/converter/from_bytes.go 43.47% <25.00%> (-1.88%) ⬇️
api/converter/from_pb.go 41.28% <35.71%> (-0.81%) ⬇️
pkg/document/crdt/primitive.go 91.57% <70.96%> (-2.11%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fourjae
Copy link
Contributor Author

fourjae commented Sep 20, 2023

There were things I didn't know about, and after analyzing them, I came to the following conclusion and changed it to PR.

  1. (p *Primitive) As a result of checking the primitive type of the Marshal() function, there are 8 types (Null, Boolean, Integer, Long, Double, String, Bytes, Date) already declared as constants. Only the above 8 types could be executed.
    Therefore, the panic itself could not occur.

    // Primitive can have the following types:

  2. In the NewPrimitive function, the exception type (float) is also set to Double and values ​​other than those declared in the constant cannot be entered.

  3. (p *Primitive) Marshal() function is Since we are not calling from json layer, we don't need to use any method to catch error at json level.

Therefore, the (p *Primitive) Marshal() function only erased the panic from the crdt layer.

@fourjae fourjae marked this pull request as ready for review September 20, 2023 13:50
@krapie krapie self-requested a review September 21, 2023 03:19
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.
I left some comments below.

pkg/document/crdt/primitive.go Outdated Show resolved Hide resolved
pkg/document/crdt/object.go Outdated Show resolved Hide resolved
pkg/document/crdt/object.go Outdated Show resolved Hide resolved
pkg/document/crdt/element_rht.go Outdated Show resolved Hide resolved
pkg/document/crdt/array.go Outdated Show resolved Hide resolved
pkg/document/json/counter.go Outdated Show resolved Hide resolved
pkg/splay/splay.go Outdated Show resolved Hide resolved
pkg/splay/splay.go Outdated Show resolved Hide resolved
test/integration/array_test.go Outdated Show resolved Hide resolved
pkg/document/json/object.go Outdated Show resolved Hide resolved
Changing mismatched code conventions and changing err handling
@fourjae
Copy link
Contributor Author

fourjae commented Sep 21, 2023

Thanks for your comment!

Fixed code conventions that did not fit.

The hard-coded err message has been changed to return an err variable.

Please let me know if you need any additional changes.

@fourjae fourjae requested a review from krapie September 26, 2023 13:15
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

Thank you for reflecting my review.
I left just few more comments 😄

@@ -194,7 +227,6 @@ func (p *Array) addInternal(
) crdt.Element {
return p.insertAfterInternal(p.Array.LastCreatedAt(), creator)
}

Copy link
Member

Choose a reason for hiding this comment

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

It will be nice to have space between functions

@@ -120,34 +124,47 @@ func (p *Object) SetNewTree(k string, initialRoot ...*TreeNode) *Tree {
// SetNull sets the null for the given key.
func (p *Object) SetNull(k string) *Object {
p.setInternal(k, func(ticket *time.Ticket) crdt.Element {
return crdt.NewPrimitive(nil, ticket)
Copy link
Member

Choose a reason for hiding this comment

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

It will be nice to keep space convention of spacing before return in this file. I will leave only one comment for this, so check all the return codes in this file.

}

// Marshal returns the JSON encoding of the value.
func (p *Primitive) Marshal() string {
switch p.valueType {
case Null:
return "null"
return ""
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason for this change? I will be better to keep the previous code.

@@ -34,7 +34,7 @@ func TestPrimitive(t *testing.T) {
valueType crdt.ValueType
marshal string
}{
{nil, crdt.Null, "null"},
{nil, crdt.Null, ""},
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason for this change? I will be better to keep the previous code.

It will be better to revert this code too.

@@ -213,9 +213,10 @@ func (p *Primitive) Marshal() string {
return fmt.Sprintf(`"%s"`, p.value)
case Date:
return fmt.Sprintf(`"%s"`, p.value.(gotime.Time).Format(gotime.RFC3339))
// when Null or default
Copy link
Member

Choose a reason for hiding this comment

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

It will be nice to remove comment.

@fourjae fourjae requested a review from krapie September 28, 2023 15:29
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Could you review this PR @hackerwins?

})
}

Copy link
Member

Choose a reason for hiding this comment

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

Also check all the return codes for spacing convention in this file.

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution. 👍

@hackerwins hackerwins merged commit 34c8505 into yorkie-team:main Sep 29, 2023
1 check passed
hackerwins pushed a commit that referenced this pull request Sep 29, 2023
hackerwins pushed a commit that referenced this pull request Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup 🧹 Paying off technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't Panic in the Server
3 participants