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

add util for transaction table #528

Merged
merged 1 commit into from
Nov 15, 2018

Conversation

freehan
Copy link
Contributor

@freehan freehan commented Oct 25, 2018

util needed for transaction-syncer

cc @agau4779

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Oct 25, 2018
detachTransaction = transactionOperation("DETACH")
)

type transactionOperation string
Copy link
Contributor

Choose a reason for hiding this comment

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

can this be int consts, e.g. with iota ? https://splice.com/blog/iota-elegant-constants-golang/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The transaction operation string will be used in error message though.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want, you could use int costs w/ iota and then implement a ToString() function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay.


// Insert entries into transaction table
for i := 0; i < testNum; i++ {
key := fmt.Sprintf("%s%d", keyPrefix, i)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - can we use values that look more like real values? I'm assuming the real values are NEG names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are test on the user side of the transaction table that does this. This unit test focused on the util.

// It uses the encoded endpoint as key and associate attributes of an transaction as value
type transactionTable struct {
data map[string]transactionEntry
lock sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

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

golint is angry because it wants this to be *sync.Mutex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am removing as golint is angry as the syncer using this util also uses another mutex

keyPrefix := "key"
zonePrefix := "zone"
testKeyMap := map[string]transactionEntry{}
verifyTable := func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like it'd be cleaner and more usable for future tests if this were its own function, e.g. func verifyTable(t *testing.T, expectedKeys []string). But since we're only using it in this test case, this can be a later change - up to you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


verifyTable()

// Update half of the entries in the transaction table
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have tests for detachTransaction also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@freehan
Copy link
Contributor Author

freehan commented Oct 31, 2018

Ready for another round.

@freehan freehan mentioned this pull request Nov 2, 2018
@freehan
Copy link
Contributor Author

freehan commented Nov 5, 2018

Ping


// transactionTable records ongoing NEG API operation per endpoint
// It uses the encoded endpoint as key and associate attributes of an transaction as value
// WARNING: transactionTable is not thread safe
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on our offline discussion, you need to make this thread-safe right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The user of transaction table will guarantee thread-safty.

I remove the RWlock in the util mostly because golint complains if you have a mutex wrap around another mutex.

detachTransaction = transactionOperation("DETACH")
)

type transactionOperation string
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want, you could use int costs w/ iota and then implement a ToString() function.

@freehan freehan force-pushed the transaction-table branch 2 times, most recently from 90276d5 to 6ddf88a Compare November 13, 2018 19:32
@freehan
Copy link
Contributor Author

freehan commented Nov 13, 2018

Fixed the comments

@freehan
Copy link
Contributor Author

freehan commented Nov 13, 2018

Ready for another round

attachTransaction = iota
detachTransaction
attachOpertaion = "Attach"
detachOpertaion = "Detach"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Fix spelling of "operation"

const (
attachTransaction = iota
detachTransaction
attachOpertaion = "Attach"
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need an extra const here

Just inline "Attach" in String()

package syncers

const (
attachTransaction = iota
Copy link
Contributor

Choose a reason for hiding this comment

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

I would call this attachOp. Same for detach

unknownOperation = "UnknownOperation"
)

type transactionOperation int
Copy link
Contributor

Choose a reason for hiding this comment

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

For brevity, maybe just call this transactionOp

@rramkumar1
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 15, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: freehan, rramkumar1

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 8a9f200 into kubernetes:master Nov 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants