Repository navigation
convert worker threads to async functions - #200
Conversation
|
Is this line no longer needed? pulsar-client-node/src/Client.h Line 40 in 571aa7a |
639c778 to
81609e6
Compare
8f2fd59 to
6c6a865
Compare
6c6a865 to
27074a6
Compare
27074a6 to
3713040
Compare
|
I managed to track down the segfault with valgrind and found an appropriate workaround (issue with the node-addon-api double freeing Ref'd instances nodejs/node-addon-api#1153). Valgrind shows no remaining memory access issues on shutdown. This is likely to be an existing latent bug and it wasn't reproducible for me locally but it was reproducible consistently in github actions. |
equanz
left a comment
There was a problem hiding this comment.
Thank you for interesting PR. I left some comments.
(Honestly, I'm not familiar with C++ and N-API.)
| this->Cleanup(); | ||
| while (this->Unref() != 0) { | ||
| // If Ref() > 0 then the process is shutting down. We must unref to prevent | ||
| // double free (once for the env shutdown and once for non-zero refs) | ||
| } |
There was a problem hiding this comment.
If the refcount is already 0, the Reference#Unref might throw an error.
https://github.com/nodejs/node-addon-api/blob/v4.3.0/napi-inl.h#L2940-L2946
https://github.com/nodejs/node/blob/v16.14.2/src/js_native_api_v8.cc#L2549-L2572
Are these lines okay?
There was a problem hiding this comment.
I'm glad you pointed that out because I think there is some inconsistency between versions. I've added a ref prior to the unref to ensure that the first unref doesn't throw. We really shouldn't need to do this at all but it's clearly a bug in the node api. The first destructor should reset the references. The reset method also frees so that doesn't help here.
There was a problem hiding this comment.
Note:
I noticed Reference#Unref throws the Error at the other lines (backtraces as below) in the 3def7b8 commit.
(gdb) run perf/perf_producer.js --url pulsar://host.docker.internal:6650 --topic persistent://public/default/topic -i 30
Starting program: /usr/local/bin/node perf/perf_producer.js --url pulsar://host.docker.internal:6650 --topic persistent://public/default/topic -i 30
warning: Error disabling address space randomization: Operation not permitted
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7f14709ed700 (LWP 2166)]
[New Thread 0x7f146bfff700 (LWP 2167)]
[New Thread 0x7f146b7fe700 (LWP 2168)]
[New Thread 0x7f146affd700 (LWP 2169)]
[New Thread 0x7f146a7fc700 (LWP 2170)]
[New Thread 0x7f1471c1b700 (LWP 2171)]
----------------------
size: 1024
url: pulsar://host.docker.internal:6650
topic: persistent://public/default/topic
iteration: 30
messages: 1000
----------------------
[New Thread 0x7f1468f00700 (LWP 2172)]
[New Thread 0x7f145bfff700 (LWP 2173)]
Throughput produced: 6277.019 msg/s --- 49.039 Mbit/s --- Latency: mean: 92.55816686251468 ms - med: 95 - 95pct: 149 - 99pct: 156 - 99.9pct: 158 - 99.99pct: 158 - Max: 158.00080001354218
Throughput produced: 6735.213 msg/s --- 52.619 Mbit/s --- Latency: mean: 79.87486515641855 ms - med: 80 - 95pct: 140 - 99pct: 146 - 99.9pct: 148 - 99.99pct: 148 - Max: 148.17530000209808
Throughput produced: 7529.627 msg/s --- 58.825 Mbit/s --- Latency: mean: 75.30654420206659 ms - med: 77 - 95pct: 126 - 99pct: 131 - 99.9pct: 132 - 99.99pct: 132 - Max: 132.66699999570847
Throughput produced: 4261.913 msg/s --- 33.296 Mbit/s --- Latency: mean: 127.84177869700103 ms - med: 122 - 95pct: 222 - 99pct: 227 - 99.9pct: 229 - 99.99pct: 229 - Max: 229.04490000009537
Throughput produced: 4543.716 msg/s --- 35.498 Mbit/s --- Latency: mean: 142.1572192513369 ms - med: 150 - 95pct: 214 - 99pct: 218 - 99.9pct: 220 - 99.99pct: 220 - Max: 220.01279997825623
Throughput produced: 5914.056 msg/s --- 46.204 Mbit/s --- Latency: mean: 96.91391509433963 ms - med: 93 - 95pct: 163 - 99pct: 166 - 99.9pct: 169 - 99.99pct: 169 - Max: 169.00579994916916
Throughput produced: 7065.959 msg/s --- 55.203 Mbit/s --- Latency: mean: 83.79047619047618 ms - med: 85 - 95pct: 135 - 99pct: 139 - 99.9pct: 141 - 99.99pct: 141 - Max: 141.43660002946854
Throughput produced: 8951.213 msg/s --- 69.931 Mbit/s --- Latency: mean: 61.14530892448513 ms - med: 62 - 95pct: 105 - 99pct: 110 - 99.9pct: 111 - 99.99pct: 111 - Max: 111.6453999876976
Throughput produced: 7331.771 msg/s --- 57.279 Mbit/s --- Latency: mean: 76.43560606060606 ms - med: 75 - 95pct: 131 - 99pct: 135 - 99.9pct: 136 - 99.99pct: 136 - Max: 136.28170001506805
Throughput produced: 5457.243 msg/s --- 42.635 Mbit/s --- Latency: mean: 100.08841099163679 ms - med: 100 - 95pct: 174 - 99pct: 180 - 99.9pct: 182 - 99.99pct: 182 - Max: 182.2026999592781
Throughput produced: 7964.142 msg/s --- 62.22 Mbit/s --- Latency: mean: 75.52533992583436 ms - med: 77 - 95pct: 119 - 99pct: 123 - 99.9pct: 125 - 99.99pct: 125 - Max: 125.44050002098083
Throughput produced: 7403.996 msg/s --- 57.844 Mbit/s --- Latency: mean: 74.31855309218203 ms - med: 77 - 95pct: 127 - 99pct: 132 - 99.9pct: 134 - 99.99pct: 134 - Max: 134.95059996843338
Throughput produced: 6052.195 msg/s --- 47.283 Mbit/s --- Latency: mean: 98.21065375302663 ms - med: 96 - 95pct: 157 - 99pct: 163 - 99.9pct: 165 - 99.99pct: 165 - Max: 165.1327999830246
Throughput produced: 6790.83 msg/s --- 53.053 Mbit/s --- Latency: mean: 81.64018691588785 ms - med: 81 - 95pct: 142 - 99pct: 145 - 99.9pct: 147 - 99.99pct: 147 - Max: 147.17070001363754
Throughput produced: 7530.325 msg/s --- 58.831 Mbit/s --- Latency: mean: 73.41789215686275 ms - med: 72 - 95pct: 125 - 99pct: 131 - 99.9pct: 132 - 99.99pct: 132 - Max: 132.66470003128052
Throughput produced: 6098.937 msg/s --- 47.648 Mbit/s --- Latency: mean: 0 ms - med: 0 - 95pct: 0 - 99pct: 0 - 99.9pct: 0 - 99.99pct: 0 - Max: 0
Throughput produced: 8248.756 msg/s --- 64.443 Mbit/s --- Latency: mean: 66.27010804321729 ms - med: 65 - 95pct: 115 - 99pct: 119 - 99.9pct: 121 - 99.99pct: 121 - Max: 121.02969998121262
Throughput produced: 6061.877 msg/s --- 47.358 Mbit/s --- Latency: mean: 85.33713692946058 ms - med: 82 - 95pct: 158 - 99pct: 163 - 99.9pct: 164 - 99.99pct: 164 - Max: 164.81310003995895
Throughput produced: 4276.394 msg/s --- 33.409 Mbit/s --- Latency: mean: 143.57420675537358 ms - med: 156 - 95pct: 227 - 99pct: 232 - 99.9pct: 233 - 99.99pct: 233 - Max: 233.78890001773834
Throughput produced: 4568.025 msg/s --- 35.688 Mbit/s --- Latency: mean: 133.0496957403651 ms - med: 139 - 95pct: 213 - 99pct: 217 - 99.9pct: 218 - 99.99pct: 218 - Max: 218.86809998750687
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7f1468f00700 (LWP 2172)]
0x0000000000bbe3d0 in v8::Exception::Error(v8::Local<v8::String>) ()
Missing separate debuginfos, use: debuginfo-install apache-pulsar-client-2.9.1-1.x86_64 glibc-2.17-307.el7.1.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
(gdb) backtrace
#0 0x0000000000bbe3d0 in v8::Exception::Error(v8::Local<v8::String>) ()
#1 0x00000000009debc2 in napi_create_error ()
#2 0x00007f1469d8e34a in Napi::Error::New (env=0x5f0c760) at /pulsar-client-node/node_modules/node-addon-api/napi-inl.h:2570
#3 0x00007f1469da2e71 in Napi::Reference<Napi::Object>::Unref (this=0x5de1b38) at /pulsar-client-node/node_modules/node-addon-api/napi-inl.h:2944
#4 0x00007f1469da73e4 in Producer::__lambda32::operator() (__closure=0x0, result=pulsar_result_Ok, ctx=0x5f1ce40) at ../src/Producer.cc:165
#5 0x00007f1469da747e in Producer::__lambda32::_FUN (result=pulsar_result_Ok, ctx=0x5f1ce40) at ../src/Producer.cc:166
#6 0x00007f14694cef77 in pulsar::ProducerImpl::ackReceived(unsigned long, pulsar::MessageId&) () from /lib/libpulsar.so.2.9.1
#7 0x00007f1469423bde in pulsar::ClientConnection::handleIncomingCommand() () from /lib/libpulsar.so.2.9.1
#8 0x00007f1469426a6a in pulsar::ClientConnection::processIncomingBuffer() () from /lib/libpulsar.so.2.9.1
#9 0x00007f1469427808 in pulsar::ClientConnection::handleRead(boost::system::error_code const&, unsigned long, unsigned int) () from /lib/libpulsar.so.2.9.1
#10 0x00007f1469436a90 in boost::asio::detail::reactive_socket_recv_op<boost::asio::mutable_buffers_1, AllocHandler<std::_Bind<std::_Mem_fn<void (pulsar::ClientConnection::*)(boost::system::error_code const&, unsigned long, unsigned int)> (std::shared_ptr<pulsar::ClientConnection>, std::_Placeholder<1>, std::_Placeholder<2>, unsigned int)> > >::do_complete(boost::asio::detail::task_io_service*, boost::asio::detail::task_io_service_operation*, boost::system::error_code const&, unsigned long) () from /lib/libpulsar.so.2.9.1
#11 0x00007f146942c671 in boost::asio::detail::task_io_service::run(boost::system::error_code&) () from /lib/libpulsar.so.2.9.1
#12 0x00007f146947f596 in pulsar::ExecutorService::startWorker(std::shared_ptr<boost::asio::io_service>) () from /lib/libpulsar.so.2.9.1
#13 0x00007f1469481a42 in std::thread::_Impl<std::_Bind_simple<std::_Bind<std::_Mem_fn<void (pulsar::ExecutorService::*)(std::shared_ptr<boost::asio::io_service>)> (pulsar::ExecutorService*, std::shared_ptr<boost::asio::io_service>)> ()> >::_M_run() () from /lib/libpulsar.so.2.9.1
#14 0x00007f14715a5330 in ?? () from /lib64/libstdc++.so.6
#15 0x00007f1470dc3ea5 in start_thread () from /lib64/libpthread.so.0
#16 0x00007f1470aec8dd in clone () from /lib64/libc.so.6
This issue was fixed in the 94cb050 commit.
There was a problem hiding this comment.
For future reference - the wrapped object should be held by an object reference if we want to keep it alive - see nodejs/node-addon-api#1153 (comment)
5f66596 to
3def7b8
Compare
|
Tests are passing: https://github.com/Matt-Esch/pulsar-client-node/actions/runs/2033472853 Comments have been addressed. Are we ready to accept this pr and merge? |
|
What about this comment? |
Yes this is redundant, I've removed it. |
|
When will this be versioned and published to npm? |
|
The last release was done last month, so I think the next version will be released around June. |
|
Hi, these changes seem great, but I'm now using current master HEAD and I think I'm seeing an issue that is related to this change: #230 I'm not saying it worked well before, but... looks like something related to operation ordering. |
I have replaced the use of async workers with direct async functions by utilising a thread safe deferred (promise). The need for this comes from the fact that with workers, calling producer.send in order results in non-deterministic ordering, and awaiting every send would diminish the utility of having a batched mode. I also found that closing the client while worker threads were ready to execute resulted in segfaults, so this change ensures that most operations execute in the order they are called on the js side.
I've also found several places lacking in good memory management discipline and have patched a few leaks. I've opted to use shared pointers with custom destructors to wrap all of the returned pointers, referring the pulsar library code to check who owns these pointers. (I did not change Auth but this could also benefit). I have avoided excessively adding std::move manually as modern compilers are quite good at optimising this themselves, but I would welcome suggestions where appropriate.
I've changed the logger so that it is set globally. Currently the logger is supplied by the client configuration, but this gives the impression that the logger can be set per client. This is not true and the logger can only be set once for the lifetime of the process. The logger change commandeers the logger by default, avoiding unexpected logging to stdout and allows for the js log function to be switched out at runtime.
I have replaced most of the workers with async functions, the only ones I haven't replaced are the ones for which async functions are not exposed to the c api. I've raised an issue for this apache/pulsar#14452, but it should be noted that the library is still prone to segfaulting if the read workers execute after the client close call.
This is my first time contributing to this library and I'm not particularly experienced with C++, but I have written node addons before. I have run the tests and checked that the linting and formatter have been run. If there are concerns around the style (such as the use of auto) I would welcome the feedback.