Fix blue-green migration might be stuck due to an existing reconnection (#406)

Fixes https://github.com/apache/pulsar-client-cpp/issues/405

### Motivation

After triggering a blue-green migration, the socket will be disconnected
and then schedule a reconnection to the blue cluster. However, the blue
cluster could never respond with a response for Producer or Subscribe
commands. Take producer as example, it means `connectionOpened` will not
complete and `reconnectionPending_` will not become false.

Then, after receiving a `CommandProducerClose` command from the blue
cluster, a new reconnection will be scheduled to the green cluster but
it will be skipped because `reconnectionPending_` is true, which means
the previous `connectionOpened` future is not completed until the 30s
timeout is reached.

```
2024-02-26 06:09:30.251 INFO  [139737465607744] HandlerBase:101 | [persistent://public/unload-test/topic-1708927732, sub, 0] Ignoring reconnection attempt since there's already a pending reconnection
2024-02-26 06:10:00.035 WARN  [139737859880512] ProducerImpl:291 | [persistent://public/unload-test/topic-1708927732, cluster-a-0-0] Failed to reconnect producer: TimeOut
```

### Modifications

When receiving the `TOPIC_MIGRATED` command, cancel the pending
`Producer` and `Subscribe` commands so that `connectionOpened` will fail
with a retryable error. In the next time of reconnection, the green
cluster will be connected.

Fix the `ExtensibleLoadManagerTest` with a more strict timeout check.
After this change, it will pass in about 3 seconds locally, while in CI
even if it passed, it takes about 70 seconds before.

Besides, fix the possible crash on macOS when closing the client, see
https://github.com/apache/pulsar-client-cpp/issues/405#issuecomment-1963969215
9 files changed
tree: 1ac79c971e25cc7fdc1710720016b809a7e5ba29
  1. .github/
  2. build-support/
  3. cmake_modules/
  4. examples/
  5. include/
  6. lib/
  7. perf/
  8. pkg/
  9. proto/
  10. templates/
  11. test-conf/
  12. tests/
  13. vcpkg-example/
  14. win-examples/
  15. wireshark/
  16. .asf.yaml
  17. .clang-format
  18. .gitignore
  19. .gitmodules
  20. CMakeLists.txt
  21. CONTRIBUTING.md
  22. dependencies.yaml
  23. Doxyfile
  24. eclipse-formatter.xml
  25. LEGACY_BUILD.md
  26. LegacyFindPackages.cmake
  27. LICENSE
  28. log4cxx.conf
  29. NOTICE
  30. pulsar-test-service-start.sh
  31. pulsar-test-service-stop.sh
  32. README.md
  33. run-unit-tests.sh
  34. SECURITY.md
  35. vcpkg.json
  36. version.txt
README.md

Pulsar C++ client library

Pulsar C++ clients support a variety of Pulsar features to enable building applications connecting to your Pulsar cluster.

For the supported Pulsar features, see Client Feature Matrix.

For how to use APIs to publish and consume messages, see examples.

Import the library into your project

CMake with vcpkg integration

Navigate to vcpkg-example for how to import the pulsar-client-cpp into your project via vcpkg.

Download pre-built binaries

For non-vcpkg projects, you can download pre-built binaries from the official release page.

Generate the API documents

Pulsar C++ client uses doxygen to build API documents. After installing doxygen, you only need to run doxygen to generate the API documents whose main page is under the doxygen/html/index.html path.

Build with vcpkg

Since it's integrated with vcpkg, see vcpkg#README for the requirements. See LEGACY_BUILD if you want to manage dependencies by yourself or you cannot install vcpkg in your own environment.

How to build from source

git clone https://github.com/apache/pulsar-client-cpp.git
cd pulsar-client-cpp
git submodule update --init --recursive
cmake -B build -DINTEGRATE_VCPKG=ON
cmake --build build -j8

The 1st step will download vcpkg and then install all dependencies according to the version description in vcpkg.json. The 2nd step will build the Pulsar C++ libraries under ./build/lib/, where ./build is the CMake build directory.

After the build, the hierarchy of the build directory will be:

build/
  include/   -- extra C++ headers
  lib/       -- libraries
  tests/     -- test executables
  examples/  -- example executables
  generated/
    lib/     -- protobuf source files for PulsarApi.proto
    tests/   -- protobuf source files for *.proto used in tests

How to install

To install the C++ headers and libraries into a specific path, e.g. /tmp/pulsar, run the following commands:

cmake -B build -DINTEGRATE_VCPKG=ON -DCMAKE_INSTALL_PREFIX=/tmp/pulsar
cmake --build build -j8 --target install

For example, on macOS you will see:

/tmp/pulsar/
  include/pulsar     -- C/C++ headers
  lib/
    libpulsar.a      -- Static library
    libpulsar.dylib  -- Dynamic library

Tests

Tests are built by default. You should execute run-unit-tests.sh to run tests locally.

If you don't want to build the tests, disable the BUILD_TESTS option:

cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_TESTS=OFF
cmake --build build -j8

Build perf tools

If you want to build the perf tools, enable the BUILD_PERF_TOOLS option:

cmake -B build -DINTEGRATE_VCPKG=ON -DBUILD_PERF_TOOLS=ON
cmake --build build -j8

Then the perf tools will be built under ./build/perf/.

Platforms

Pulsar C++ Client Library has been tested on:

  • Linux
  • Mac OS X
  • Windows x64

Wireshark Dissector

See the wireshark directory for details.

Requirements for Contributors

It's required to install LLVM for clang-tidy and clang-format. Pulsar C++ client use clang-format 11 to format files. make format automatically formats the files.

For Ubuntu users, you can install clang-format-11 via apt install clang-format-11. For other users, run ./build-support/docker-format.sh if you have Docker installed.

We welcome contributions from the open source community, kindly make sure your changes are backward compatible with GCC 4.8 and Boost 1.53.

If your contribution adds Pulsar features for C++ clients, you need to update both the Pulsar docs and the Client Feature Matrix. See Contribution Guide for more details.