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

Adding batch wrapper on query builder #239

Merged
merged 1 commit into from
Mar 9, 2023
Merged

Conversation

mariosttass
Copy link

Hello,

In the following PR we are adding a Batch wrapper on gocqlx library for binding the object instead of using named columns.

Please have a look and let me know for any questions.

Thanks !

@tehsphinx
Copy link
Contributor

tehsphinx commented Jul 22, 2022

In case you are not interested in adding this to the library, let us know. Then we'll keep it internally. (which would be harder because of missing access to private gocqlx functionality)

@tehsphinx
Copy link
Contributor

@mmatczuk: Any update for us? What do you think about this MR?

Copy link
Contributor

@tehsphinx tehsphinx left a comment

Choose a reason for hiding this comment

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

Just had another closer look at the MR.

  • We should try to get rid of changes to go.mod.
  • Also wondering about the commit history. Would be nice to have a clean rebase on top of scylladb/gocqlx:master.

go.mod Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
go.mod Outdated
github.com/google/go-cmp v0.5.4
github.com/maraino/go-mock v0.0.0-20180321183845-4c74c434cd3a
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible also this extra dependency: github.com/maraino/go-mock

Copy link
Author

Choose a reason for hiding this comment

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

This dependency is added because I am using a the go-mock library to mock the session on unit tests

Copy link
Author

Choose a reason for hiding this comment

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

If I get rid of it then I am not sure about the way to mock the session on unit tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

I removed the tests as they were not really testing anything anyway.

All parts that are used are tested in other unit tests already. We are not really adding something worth testing.

Copy link
Contributor

@tehsphinx tehsphinx left a comment

Choose a reason for hiding this comment

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

LGTM

@tehsphinx
Copy link
Contributor

tehsphinx commented Oct 19, 2022

@tzach can you have a look? (this is the PR I mentioned on P99 Conference)

@mmatczuk
Copy link
Contributor

Can you squash your commits to one and add a test please.

@tehsphinx
Copy link
Contributor

tehsphinx commented Oct 20, 2022

Thx for taking a look @mmatczuk!

Can you give us a pointer on how to test this?

We had some tests but they basically mocked everything away not really testing anything in the end. So I removed them. Also note, that we are only using existing functionality that has been tested already. gocql.Batch
(incl. *batch.Query method) is tested in the underlying repository, Queryx and bindStructArgs is tested in query_test.go.

@tehsphinx
Copy link
Contributor

Hey @mmatczuk, we have rebased and squashed the commits 🙂

@mmatczuk
Copy link
Contributor

Ok would be nice to add some docs so that the users know what it does and why.
I'd ask again to add that to a test. Feel free to copy existing test and replace batch handling with your code.

@tehsphinx
Copy link
Contributor

@mmatczuk Sry for the wait. I added some tests now. Also realized the execution functions for batching needed an overwrite to complete the user experience.

Let me know if there is anything else to change 🙂

@tehsphinx
Copy link
Contributor

@tzach: can you plan this in to be checked?

@tzach
Copy link

tzach commented Nov 30, 2022

@avelanarius can you please advise?

@tehsphinx
Copy link
Contributor

tehsphinx commented Jan 4, 2023

@tzach Happy new year 🥳!

Can I draw your attention to this once more? Currently we are using this change with a go.mod replace statement. That is not ideal especially since more one more of our microservices need to do that replace.

I'll give this another 2 to 3 weeks and then I'm going to use our clone permanently, changing it's project path so we can import it from our clone without a replace statement.

Copy link

@kaatinga kaatinga left a comment

Choose a reason for hiding this comment

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

added a nit

batchx.go Show resolved Hide resolved
@tehsphinx
Copy link
Contributor

@tzach: on the last scylla conference you said, that PRs are always welcome. Seeing all the PRs here waiting for months without any attention paints a very different picture. 😞 😞

@mykaul
Copy link

mykaul commented Feb 21, 2023

@tzach: on the last scylla conference you said, that PRs are always welcome. Seeing all the PRs here waiting for months without any attention paints a very different picture. disappointed disappointed

You are right and we should strive to do a better job here - we are slowly going over the backlog and trying to get stuff in. We wish to keep high quality bar so trying not to blindly merge stuff without fully reviewing the changes. That takes time and resources.

batchx.go Outdated
}

// NewBatch creates a new batch operation using defaults defined in the cluster.
func (s *Session) NewBatch(typ gocql.BatchType) *Batch {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func (s *Session) NewBatch(typ gocql.BatchType) *Batch {
func (s *Session) NewBatch(bt gocql.BatchType) *Batch {

batchx.go Outdated
// BindStruct binds query named parameters to values from arg using mapper. If
// value cannot be found error is reported.
func (b *Batch) BindStruct(qry *Queryx, arg interface{}) error {
arglist, err := qry.bindStructArgs(arg, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
arglist, err := qry.bindStructArgs(arg, nil)
args, err := qry.bindStructArgs(arg, nil)

batchx.go Outdated
Comment on lines 18 to 19
// BindStruct binds query named parameters to values from arg using mapper. If
// value cannot be found error is reported.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// BindStruct binds query named parameters to values from arg using mapper. If
// value cannot be found error is reported.
// BindStruct binds query named parameters to values from arg using a mapper.
// If value cannot be found an error is reported.

batchx.go Outdated
}

// ExecuteBatch executes a batch operation and returns nil if successful
// otherwise an error is returned describing the failure.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// otherwise an error is returned describing the failure.
// otherwise an error describing the failure.

batchx_test.go Outdated
Comment on lines 20 to 22
// Running examples locally:
// make run-scylla
// make run-examples
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove it, it's not the right place to state it

// make run-scylla
// make run-examples
func TestBatch(t *testing.T) {
cluster := gocqlxtest.CreateCluster()
Copy link
Collaborator

Choose a reason for hiding this comment

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

t.Parallel() if possible

selectPlaylist := qb.Select("batch_test.playlists").Where(qb.Eq("id")).Query(session)

t.Run("batch inserts", func(t *testing.T) {
qrys := []batchQry{
Copy link
Collaborator

Choose a reason for hiding this comment

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

t.Parallel() if possible

batchx_test.go Outdated

b := session.NewBatch(gocql.LoggedBatch)
for _, qry := range qrys {
if r := b.BindStruct(qry.qry, qry.arg); r != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if r := b.BindStruct(qry.qry, qry.arg); r != nil {
if err := b.BindStruct(qry.qry, qry.arg); 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.

applies in entire function

batchx_test.go Outdated
t.Fatal("select song:", r)
}
if diff := cmp.Diff(gotSong, song); diff != "" {
t.Error("song mismatch", diff)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
t.Error("song mismatch", diff)
t.Errorf("expected %v song, got %v, diff: %q", song, gotSong, diff)

batchx_test.go Outdated
t.Fatal("select song:", r)
}
if diff := cmp.Diff(gotPlayList, playlist); diff != "" {
t.Error("playList mismatch", diff)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

@zimnx
Copy link
Collaborator

zimnx commented Feb 22, 2023

Please squash commits into single one with descriptive commit message. Thanks

@tehsphinx
Copy link
Contributor

@zimnx thx for the review. I applied the requested changes :)

@tehsphinx
Copy link
Contributor

tehsphinx commented Mar 3, 2023

Note: adding t.Parallel() to the sub-test in batchx_test.go will close the session before the sub-test is done (defer session.Close() executes too early).

EDIT: adjusted to use t.Cleanup instead of defer.

Copy link
Collaborator

@zimnx zimnx left a comment

Choose a reason for hiding this comment

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

last two nits, thanks

example_test.go Outdated
if err != nil {
t.Fatal("create table:", err)
}

playlistMetadata := table.Metadata{
Name: "examples.playlists",
Name: keyspace + ".playlists",
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's be consistent and use fmt.Sprintf here

Copy link
Contributor

Choose a reason for hiding this comment

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

right, missed those :)

example_test.go Outdated
Columns: []string{"id", "title", "album", "artist", "song_id"},
PartKey: []string{"id"},
SortKey: []string{"title", "album", "artist", "song_id"},
}
playlistTable := table.New(playlistMetadata)

// Insert song using query builder.
insertSong := qb.Insert("examples.songs").
insertSong := qb.Insert(keyspace+".songs").
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's be consistent and use fmt.Sprintf here

@tehsphinx
Copy link
Contributor

@zimnx Thanks! Adjusted 😄

Copy link
Collaborator

@zimnx zimnx left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@zimnx zimnx merged commit dec046b into scylladb:master Mar 9, 2023
@tehsphinx tehsphinx deleted the DT-5886 branch March 9, 2023 13:44
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.

7 participants