You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "RobertIndie (via GitHub)" <gi...@apache.org> on 2024/01/31 08:56:02 UTC

[PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

RobertIndie opened a new pull request, #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164

   <!--
   ### Contribution Checklist
     
     - Name the pull request in the form "[Issue XYZ][component] Title of the pull request", where *XYZ* should be replaced by the actual issue number.
       Skip *Issue XYZ* if there is no associated github issue for this pull request.
       Skip *component* if you are unsure about which is the best component. E.g. `[docs] Fix typo in produce method`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   Fixes #1163
   
   Thanks @0x4500 for reporting this issue and providing the analysis. 
   
   ### Motivation
   
   https://github.com/apache/pulsar-client-go/pull/1121 introduces a regression but. The compression logic has been moved from `internalSend` to `internalSendAsync` which leads to the compression being executed concurrently.
   
   The zstd_cgo doesn't allow compressed data concurrently. We need to add a mutex to avoid it.
   
   ### Modifications
   
   - Add mutex for zstd_cgo
   
   
   ### Verifying this change
   
   I have run the benchmark and shows that this change has a minimal impact on performance.
   
   Before this PR(I have also provided other compression provider benchmark for reference.):
   ```
   BenchmarkCompression
   BenchmarkCompression/zlib
   BenchmarkCompression/zlib-12         	     388	   2904783 ns/op	  34.63 MB/s
   BenchmarkCompression/lz4
   BenchmarkCompression/lz4-12          	    4832	    239997 ns/op	 419.20 MB/s
   BenchmarkCompression/zstd-pure-go-fastest
   BenchmarkCompression/zstd-pure-go-fastest-12         	    2618	    428303 ns/op	 234.89 MB/s
   BenchmarkCompression/zstd-pure-go-default
   BenchmarkCompression/zstd-pure-go-default-12         	    1794	    633533 ns/op	 158.80 MB/s
   BenchmarkCompression/zstd-pure-go-best
   BenchmarkCompression/zstd-pure-go-best-12            	    1299	    905872 ns/op	 111.06 MB/s
   BenchmarkCompression/zstd-cgo-level-fastest
   BenchmarkCompression/zstd-cgo-level-fastest-12       	    7480	    176366 ns/op	 570.44 MB/s
   BenchmarkCompression/zstd-cgo-level-default
   BenchmarkCompression/zstd-cgo-level-default-12       	    1867	    587183 ns/op	 171.34 MB/s
   BenchmarkCompression/zstd-cgo-level-best
   BenchmarkCompression/zstd-cgo-level-best-12          	     620	   1855183 ns/op	  54.23 MB/s
   PASS
   ```
   
   After this PR:
   ```
   BenchmarkCompression
   BenchmarkCompression/zlib
   BenchmarkCompression/zlib-12         	     364	   2988984 ns/op	  33.66 MB/s
   BenchmarkCompression/lz4
   BenchmarkCompression/lz4-12          	    4396	    263103 ns/op	 382.38 MB/s
   BenchmarkCompression/zstd-pure-go-fastest
   BenchmarkCompression/zstd-pure-go-fastest-12         	    2572	    480822 ns/op	 209.24 MB/s
   BenchmarkCompression/zstd-pure-go-default
   BenchmarkCompression/zstd-pure-go-default-12         	    1748	    633217 ns/op	 158.88 MB/s
   BenchmarkCompression/zstd-pure-go-best
   BenchmarkCompression/zstd-pure-go-best-12            	    1206	    891240 ns/op	 112.88 MB/s
   BenchmarkCompression/zstd-cgo-level-fastest
   BenchmarkCompression/zstd-cgo-level-fastest-12       	    7033	    172643 ns/op	 582.74 MB/s
   BenchmarkCompression/zstd-cgo-level-default
   BenchmarkCompression/zstd-cgo-level-default-12       	    1900	    602368 ns/op	 167.02 MB/s
   BenchmarkCompression/zstd-cgo-level-best
   BenchmarkCompression/zstd-cgo-level-best-12          	     511	   2036597 ns/op	  49.40 MB/s
   PASS
   ```
   
   This change added tests.
   
   ### Does this pull request potentially affect one of the following parts:
   
   *If `yes` was chosen, please highlight the changes*
   
     - Dependencies (does it add or upgrade a dependency): (yes / no)
     - The public API: (yes / no)
     - The schema: (yes / no / don't know)
     - The default values of configurations: (yes / no)
     - The wire protocol: (yes / no)
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / GoDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a followup issue for adding the documentation
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "RobertIndie (via GitHub)" <gi...@apache.org>.
RobertIndie commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1473750268


##########
pulsar/internal/compression/zstd_cgo.go:
##########
@@ -61,6 +64,8 @@ func (z *zstdCGoProvider) CompressMaxSize(originalSize int) int {
 }
 
 func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
+	z.mu.Lock()
+	defer z.mu.Unlock()

Review Comment:
   Good idea! Thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "0x4500 (via GitHub)" <gi...@apache.org>.
0x4500 commented on PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#issuecomment-1921181279

   The updated version works for us -- thanks again @RobertIndie.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat merged PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1473216384


##########
pulsar/internal/compression/zstd_cgo.go:
##########
@@ -61,6 +64,8 @@ func (z *zstdCGoProvider) CompressMaxSize(originalSize int) int {
 }
 
 func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
+	z.mu.Lock()
+	defer z.mu.Unlock()

Review Comment:
   Good point, the `sync.Pool` is already doing the thread-local caching internally.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "0x4500 (via GitHub)" <gi...@apache.org>.
0x4500 commented on PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#issuecomment-1918711500

   Confirmed with a manual test that this PR fixes the issue in our code. Thanks @RobertIndie!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1472555275


##########
pulsar/internal/compression/zstd_cgo.go:
##########
@@ -61,6 +64,8 @@ func (z *zstdCGoProvider) CompressMaxSize(originalSize int) int {
 }
 
 func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
+	z.mu.Lock()
+	defer z.mu.Unlock()

Review Comment:
   If there are many routine publishing, we could get into a contention situation. Maybe we could try some gorouting-local access? eg: https://github.com/timandy/routine?tab=readme-ov-file#use-threadlocal 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "0x4500 (via GitHub)" <gi...@apache.org>.
0x4500 commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1473198414


##########
pulsar/internal/compression/zstd_cgo.go:
##########
@@ -61,6 +64,8 @@ func (z *zstdCGoProvider) CompressMaxSize(originalSize int) int {
 }
 
 func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
+	z.mu.Lock()
+	defer z.mu.Unlock()

Review Comment:
   Alternatively, golang provides [sync.Pool](https://pkg.go.dev/sync#Pool) which could potentially be used to create and cache zstd contexts as required.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "0x4500 (via GitHub)" <gi...@apache.org>.
0x4500 commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1474351750


##########
pulsar/internal/utils.go:
##########
@@ -40,7 +40,7 @@ func TimestampMillis(t time.Time) uint64 {
 // GetAndAdd perform atomic read and update
 func GetAndAdd(n *uint64, diff uint64) uint64 {
 	for {
-		v := *n
+		v := atomic.LoadUint64(n)
 		if atomic.CompareAndSwapUint64(n, v, v+diff) {

Review Comment:
   I'm not familiar enough with the pulsar codebase to understand what this is trying to achieve, but the usual way to perform an atomic get-and-add on a uint64 in golang is to use `atomic.AddUint64()`. Also, is this change related to the issue at hand, or is it an unrelated change?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix SIGSEGV with zstd compression enabled [pulsar-client-go]

Posted by "RobertIndie (via GitHub)" <gi...@apache.org>.
RobertIndie commented on code in PR #1164:
URL: https://github.com/apache/pulsar-client-go/pull/1164#discussion_r1474702429


##########
pulsar/internal/utils.go:
##########
@@ -40,7 +40,7 @@ func TimestampMillis(t time.Time) uint64 {
 // GetAndAdd perform atomic read and update
 func GetAndAdd(n *uint64, diff uint64) uint64 {
 	for {
-		v := *n
+		v := atomic.LoadUint64(n)
 		if atomic.CompareAndSwapUint64(n, v, v+diff) {

Review Comment:
   This change is to fix a race issue in the CI discovered by the new test case `TestSendConcurrently`.
   The difference between them is that atomic.AddUint64() returns the new value but `GetAndAdd ` here returns the old value.
   Actually I was trying to [use atomic.AddUint64()](https://github.com/apache/pulsar-client-go/pull/1164/commits/51a62c212fa1d860b162c076606484f47e69e951), but if failed in the CI because [there are other cases](https://github.com/apache/pulsar-client-go/actions/runs/7736056342/job/21092758357#step:5:9550) that need the old values.
   And yes, this is to fix another regression bug introduced in 0.12.0.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org