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

Employ etcd for distributed locking #148

Merged
merged 1 commit into from
Feb 26, 2021
Merged

Employ etcd for distributed locking #148

merged 1 commit into from
Feb 26, 2021

Conversation

hackerwins
Copy link
Member

@hackerwins hackerwins commented Feb 12, 2021

What this PR does / why we need it:

Employ etcd for distributed locking

In order to allow multiple agents to process client requests, etcd is
employed. In standalone mode with only one agent, memory-based locker is
used, not etcd.

Which issue(s) this PR fixes:

Address #11

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

@hackerwins
Copy link
Member Author

hackerwins commented Feb 12, 2021

Stress test for distributed locking based on etcd

Description of test conditions

  • Multiple routines access the same key at the same time.
  • etcd is also run on the machine running the test.
  • Machine info
    • MacBook Air (Retina, 13-inch, 2020)
    • Processor 1.1 GHz Quad-Core Intel Core i5
    • Memory 16 GB 3733 MHz LPDDR4X
start := time.Now()

size := 500
sum := 0
var wg gosync.WaitGroup
for i := 0; i < size; i++ {
	wg.Add(1)
	go func() {
		defer wg.Done()

		ctx := context.Background()
		locker := cli.NewLocker(ctx, sync.Key(t.Name()))
		locker.Lock()
		sum += 1
		locker.Unlock()
	}()
}
wg.Wait()
assert.Equal(t, size, sum)

log.Logger.Infof("lock count: %d, elapsed: %s", size, time.Since(start))

Results

// Performance of locker.Locker (Memory, moby.locker)
2021-02-12T23:11:44.384+0900	INFO	lock count: 10, elapsed: 177.267µs
2021-02-12T23:11:57.554+0900	INFO	lock count: 100, elapsed: 331.794µs
2021-02-12T23:06:24.930+0900	INFO	lock count: 1000, elapsed: 2.271639ms

// Performance of concurrency.Mutex (Distributed locking, etcd)
2021-02-12T23:11:27.319+0900	INFO	lock count: 10, elapsed: 199.070746ms
2021-02-12T23:10:52.406+0900	INFO	lock count: 100, elapsed: 1.133782094s
2021-02-12T23:14:37.825+0900	INFO	lock count: 200, elapsed: 3.849113763s
2021-02-12T23:15:32.183+0900	INFO	lock count: 500, elapsed: 14.822853146s
2021-02-12T23:01:29.410+0900	INFO	lock count: 1000, elapsed: 48.902339709s

// Performance of concurrency.Mutex (etcd, When accessing different keys) 
2021-02-12T23:21:36.212+0900	INFO	lock count: 500, elapsed: 997.036022ms

@codecov
Copy link

codecov bot commented Feb 12, 2021

Codecov Report

Merging #148 (b7e966e) into main (cf19e73) will decrease coverage by 0.01%.
The diff coverage is 55.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #148      +/-   ##
==========================================
- Coverage   57.05%   57.03%   -0.02%     
==========================================
  Files          33       34       +1     
  Lines        3106     3191      +85     
==========================================
+ Hits         1772     1820      +48     
- Misses       1154     1187      +33     
- Partials      180      184       +4     
Impacted Files Coverage Δ
pkg/document/json/rga_tree_list.go 53.28% <ø> (ø)
pkg/document/json/rht.go 53.44% <ø> (ø)
pkg/document/json/rht_pq_map.go 59.15% <ø> (ø)
pkg/llrb/llrb.go 81.60% <ø> (ø)
yorkie/backend/db/db.go 66.66% <ø> (ø)
yorkie/config.go 81.25% <ø> (ø)
yorkie/yorkie.go 0.00% <0.00%> (ø)
yorkie/backend/sync/etcd/client.go 34.78% <34.78%> (ø)
yorkie/rpc/server.go 53.49% <66.66%> (+4.50%) ⬆️
api/converter/from_bytes.go 64.42% <100.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf19e73...540d2b7. Read the comment docs.

Copy link
Member

@ppeeou ppeeou left a comment

Choose a reason for hiding this comment

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

LGTM 🔥

@ppeeou ppeeou removed their assignment Feb 14, 2021
@dc7303
Copy link
Member

dc7303 commented Feb 15, 2021

Sorry, I am not familiar with ETCD, so there is a delay in reviewing it. 😭
I will take a closer look and give you an opinion.

@mojosoeun
Copy link
Member

@hackerwins Thank you for your PR! Could you elaborate on this PR in the weekly meeting? Because im also not used to etcd, it would be great to hear about this task!

Copy link
Member

@dc7303 dc7303 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 the wonderful work. Thanks to you, I am learning a lot. 🙇‍♂️

I am trying to fully understand this feature and have made a few comments along the way. Please check.

test/stress/etcd_stress_test.go Outdated Show resolved Hide resolved
yorkie/backend/sync/etcd/client.go Outdated Show resolved Hide resolved
yorkie/rpc/server.go Outdated Show resolved Hide resolved
@hackerwins hackerwins force-pushed the distributed-locking branch 3 times, most recently from 1e80e29 to 5bf1ad7 Compare February 17, 2021 05:37
@hackerwins hackerwins added the enhancement 🌟 New feature or request label Feb 17, 2021
@hackerwins
Copy link
Member Author

hackerwins commented Feb 19, 2021

Since we are using lease based lock, we need to introduce fencing token.
Lease lock and fencing token are described in the article by Martin Kleppmann.

@hackerwins hackerwins force-pushed the distributed-locking branch 2 times, most recently from 2f071bf to 9eee471 Compare February 21, 2021 07:36
@hackerwins hackerwins force-pushed the distributed-locking branch 10 times, most recently from a7ea019 to 80ea20b Compare February 22, 2021 08:03
@hackerwins hackerwins force-pushed the distributed-locking branch 2 times, most recently from d42cbf1 to f943bd5 Compare February 22, 2021 17:13
Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

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

I have a few inquiries and suggestions!
Please answer after checking. :)

yorkie/backend/sync/etcd/client.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
In order to allow multiple agents to process client requests, etcd is
employed. In standalone mode with only one agent, memory-based locker is
used, not etcd.
Copy link
Member

@dc7303 dc7303 left a comment

Choose a reason for hiding this comment

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

LGTM 👍 I learned a lot while reviewing. Thank you! :)

@hackerwins hackerwins merged commit 8bdd310 into main Feb 26, 2021
@hackerwins hackerwins deleted the distributed-locking branch February 26, 2021 14:57
jeonjonghyeok pushed a commit to jeonjonghyeok/yorkie that referenced this pull request Aug 4, 2022
In order to allow multiple agents to process client requests, etcd is
employed. In standalone mode with only one agent, memory-based locker is
used, not etcd.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🌟 New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants