You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by GitBox <gi...@apache.org> on 2021/04/19 06:53:34 UTC

[GitHub] [qpid-proton] jiridanek commented on a change in pull request #309: PROTON-2370: [cpp] An accessor for the delivery tag

jiridanek commented on a change in pull request #309:
URL: https://github.com/apache/qpid-proton/pull/309#discussion_r615578902



##########
File path: cpp/src/delivery_test.cpp
##########
@@ -0,0 +1,132 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include <proton/connection.hpp>
+#include <proton/connection_options.hpp>
+#include <proton/container.hpp>
+#include <proton/delivery.h>
+#include <proton/delivery.hpp>
+#include <proton/link.hpp>
+#include <proton/listen_handler.hpp>
+#include <proton/listener.hpp>
+#include <proton/message.hpp>
+#include <proton/message_id.hpp>
+#include <proton/messaging_handler.hpp>
+#include <proton/tracker.hpp>
+#include <proton/types.h>
+#include <proton/types.hpp>
+#include <proton/value.hpp>
+
+#include "proton/error_condition.hpp"
+#include "proton/internal/pn_unique_ptr.hpp"
+#include "proton/receiver_options.hpp"
+#include "proton/transport.hpp"
+#include "proton/work_queue.hpp"
+#include "test_bits.hpp"
+
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+
+namespace {
+std::mutex m;
+std::condition_variable cv;
+bool listener_ready = false;
+int listener_port;
+} // namespace
+
+class test_recv : public proton::messaging_handler {
+  private:
+    class listener_ready_handler : public proton::listen_handler {
+        void on_open(proton::listener &l) PN_CPP_OVERRIDE {
+            std::cout << "listening on " << l.port() << std::endl;
+            listener_port = l.port();
+            listener_ready = true;
+            cv.notify_one();
+        }
+    };
+
+    std::string url;
+    proton::listener listener;
+    listener_ready_handler listen_handler;
+
+  public:
+    test_recv(const std::string &s) : url(s) {}
+
+    void on_container_start(proton::container &c) PN_CPP_OVERRIDE {
+        listener = c.listen(url, listen_handler);
+    }
+
+    void on_message(proton::delivery &d, proton::message &msg) PN_CPP_OVERRIDE {
+        d.receiver().close();
+        d.connection().close();
+        listener.stop();
+        proton::binary test_tag_recv("TESTTAG");
+        ASSERT_EQUAL(test_tag_recv, d.tag());
+    }
+};
+
+class test_send : public proton::messaging_handler {
+  private:
+    std::string url;
+    proton::sender sender;
+
+  public:
+    test_send(const std::string &s) : url(s) {}
+
+    void on_container_start(proton::container &c) PN_CPP_OVERRIDE {
+        proton::connection_options co;
+        sender = c.open_sender(url, co);
+    }
+
+    void on_sendable(proton::sender &s) PN_CPP_OVERRIDE {
+        proton::message msg;
+        msg.body("message");
+        proton::binary test_tag_send("TESTTAG");
+        s.send(msg, test_tag_send);
+    }

Review comment:
       You do not have a handler for when the delivery is settled, where you'd also check the tag value. As far as I can tell, you cannot access the tag from a `tracker`. If you think that making tag accessible there should be done in a new Jira, that is ok with me too.
   
   ```
       // called in the receiver
       void on_delivery_settle(proton::delivery &delivery) override {
   
       }
   
       // called in the sender
       void on_tracker_accept(proton::tracker &tracker) override {
   
       }
   
       void on_tracker_settle(proton::tracker &tracker) override {
   
       }
   ```

##########
File path: cpp/src/delivery_test.cpp
##########
@@ -0,0 +1,132 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include <proton/connection.hpp>
+#include <proton/connection_options.hpp>
+#include <proton/container.hpp>
+#include <proton/delivery.h>
+#include <proton/delivery.hpp>
+#include <proton/link.hpp>
+#include <proton/listen_handler.hpp>
+#include <proton/listener.hpp>
+#include <proton/message.hpp>
+#include <proton/message_id.hpp>
+#include <proton/messaging_handler.hpp>
+#include <proton/tracker.hpp>
+#include <proton/types.h>
+#include <proton/types.hpp>
+#include <proton/value.hpp>
+
+#include "proton/error_condition.hpp"
+#include "proton/internal/pn_unique_ptr.hpp"
+#include "proton/receiver_options.hpp"
+#include "proton/transport.hpp"
+#include "proton/work_queue.hpp"
+#include "test_bits.hpp"
+
+#include <condition_variable>
+#include <mutex>
+#include <thread>

Review comment:
       Good thing Proton Cpp is moving towards C++11 for the next release. Otherwise you wouldn't be able to use these. Now it's fine!

##########
File path: cpp/src/delivery_test.cpp
##########
@@ -0,0 +1,132 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include <proton/connection.hpp>
+#include <proton/connection_options.hpp>
+#include <proton/container.hpp>
+#include <proton/delivery.h>
+#include <proton/delivery.hpp>
+#include <proton/link.hpp>
+#include <proton/listen_handler.hpp>
+#include <proton/listener.hpp>
+#include <proton/message.hpp>
+#include <proton/message_id.hpp>
+#include <proton/messaging_handler.hpp>
+#include <proton/tracker.hpp>
+#include <proton/types.h>
+#include <proton/types.hpp>
+#include <proton/value.hpp>
+
+#include "proton/error_condition.hpp"
+#include "proton/internal/pn_unique_ptr.hpp"
+#include "proton/receiver_options.hpp"
+#include "proton/transport.hpp"
+#include "proton/work_queue.hpp"
+#include "test_bits.hpp"
+
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+
+namespace {
+std::mutex m;
+std::condition_variable cv;
+bool listener_ready = false;
+int listener_port;
+} // namespace
+
+class test_recv : public proton::messaging_handler {
+  private:
+    class listener_ready_handler : public proton::listen_handler {
+        void on_open(proton::listener &l) PN_CPP_OVERRIDE {
+            std::cout << "listening on " << l.port() << std::endl;
+            listener_port = l.port();
+            listener_ready = true;

Review comment:
       https://travis-ci.com/github/apache/qpid-proton/jobs/499340056#L5205
   
   ```
   23: Test command: /opt/pyenv/shims/python3 "/home/travis/build/apache/qpid-proton/scripts/env.py" "--" "TSAN_OPTIONS=second_deadlock_stack=1 suppressions=/home/travis/build/apache/qpid-proton/tests/tsan.supp" "PN_SASL_CONFIG_PATH=/home/travis/build/apache/qpid-proton/build/cpp/testdata/sasl-conf" "/home/travis/build/apache/qpid-proton/build/cpp/delivery_test"
   23: Test timeout computed to be: 360
   23: TEST: test_delivery_tag()
   23: listening on 43505
   23: ==================
   23: WARNING: ThreadSanitizer: data race (pid=14832)
   23:   Write of size 1 at 0x555ab72fc164 by thread T1:
   23:     #0 test_recv::listener_ready_handler::on_open(proton::listener&) /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:62 (delivery_test+0x7fee)
   23:     #1 proton::container::impl::dispatch(pn_event_t*) /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:602 (libqpid-proton-cpp.so.12+0x4d02d)
   23:     #2 proton::container::impl::thread() /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:765 (libqpid-proton-cpp.so.12+0x4d567)
   23:     #3 proton::container::impl::run(int) /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:812 (libqpid-proton-cpp.so.12+0x4db6b)
   23:     #4 proton::container::run() /home/travis/build/apache/qpid-proton/cpp/src/container.cpp:92 (libqpid-proton-cpp.so.12+0x3587b)
   23:     #5 operator() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:113 (delivery_test+0x6628)
   23:     #6 __invoke_impl<void, test_delivery_tag()::<lambda()> > /usr/include/c++/10/bits/invoke.h:60 (delivery_test+0x6628)
   23:     #7 __invoke<test_delivery_tag()::<lambda()> > /usr/include/c++/10/bits/invoke.h:95 (delivery_test+0x6628)
   23:     #8 _M_invoke<0> /usr/include/c++/10/thread:264 (delivery_test+0x6628)
   23:     #9 operator() /usr/include/c++/10/thread:271 (delivery_test+0x6628)
   23:     #10 _M_run /usr/include/c++/10/thread:215 (delivery_test+0x6628)
   23:     #11 <null> <null> (libstdc++.so.6+0xd6d83)
   23: 
   23:   Previous read of size 1 at 0x555ab72fc164 by main thread (mutexes: write M50):
   23:     #0 operator() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:117 (delivery_test+0x6c14)
   23:     #1 wait<test_delivery_tag()::<lambda()> > /usr/include/c++/10/condition_variable:110 (delivery_test+0x6c14)
   23:     #2 test_delivery_tag() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:117 (delivery_test+0x6c14)
   23:     #3 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23:   Location is global '(anonymous namespace)::listener_ready' of size 1 at 0x555ab72fc164 (delivery_test+0x00000000c164)
   23: 
   23:   Mutex M50 (0x555ab72fc1c0) created at:
   23:     #0 pthread_mutex_lock <null> (libtsan.so.0+0x5271c)
   23:     #1 __gthread_mutex_lock /usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:749 (delivery_test+0x6be6)
   23:     #2 std::mutex::lock() /usr/include/c++/10/bits/std_mutex.h:100 (delivery_test+0x6be6)
   23:     #3 std::unique_lock<std::mutex>::lock() /usr/include/c++/10/bits/unique_lock.h:138 (delivery_test+0x6be6)
   23:     #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&) /usr/include/c++/10/bits/unique_lock.h:68 (delivery_test+0x6be6)
   23:     #5 test_delivery_tag() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:116 (delivery_test+0x6be6)
   23:     #6 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23:   Thread T1 (tid=14834, running) created by main thread at:
   23:     #0 pthread_create <null> (libtsan.so.0+0x5ea99)
   23:     #1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xd7048)
   23:     #2 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23: SUMMARY: ThreadSanitizer: data race /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:62 in test_recv::listener_ready_handler::on_open(proton::listener&)
   23: ==================
   23: ==================
   23: WARNING: ThreadSanitizer: data race (pid=14832)
   23:   Read of size 4 at 0x555ab72fc160 by main thread (mutexes: write M50):
   23:     #0 test_delivery_tag() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:120 (delivery_test+0x6c4b)
   23:     #1 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23:   Previous write of size 4 at 0x555ab72fc160 by thread T1:
   23:     #0 test_recv::listener_ready_handler::on_open(proton::listener&) /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:61 (delivery_test+0x7fdb)
   23:     #1 proton::container::impl::dispatch(pn_event_t*) /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:602 (libqpid-proton-cpp.so.12+0x4d02d)
   23:     #2 proton::container::impl::thread() /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:765 (libqpid-proton-cpp.so.12+0x4d567)
   23:     #3 proton::container::impl::run(int) /home/travis/build/apache/qpid-proton/cpp/src/proactor_container_impl.cpp:812 (libqpid-proton-cpp.so.12+0x4db6b)
   23:     #4 proton::container::run() /home/travis/build/apache/qpid-proton/cpp/src/container.cpp:92 (libqpid-proton-cpp.so.12+0x3587b)
   23:     #5 operator() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:113 (delivery_test+0x6628)
   23:     #6 __invoke_impl<void, test_delivery_tag()::<lambda()> > /usr/include/c++/10/bits/invoke.h:60 (delivery_test+0x6628)
   23:     #7 __invoke<test_delivery_tag()::<lambda()> > /usr/include/c++/10/bits/invoke.h:95 (delivery_test+0x6628)
   23:     #8 _M_invoke<0> /usr/include/c++/10/thread:264 (delivery_test+0x6628)
   23:     #9 operator() /usr/include/c++/10/thread:271 (delivery_test+0x6628)
   23:     #10 _M_run /usr/include/c++/10/thread:215 (delivery_test+0x6628)
   23:     #11 <null> <null> (libstdc++.so.6+0xd6d83)
   23: 
   23:   Location is global '(anonymous namespace)::listener_port' of size 4 at 0x555ab72fc160 (delivery_test+0x00000000c160)
   23: 
   23:   Mutex M50 (0x555ab72fc1c0) created at:
   23:     #0 pthread_mutex_lock <null> (libtsan.so.0+0x5271c)
   23:     #1 __gthread_mutex_lock /usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:749 (delivery_test+0x6be6)
   23:     #2 std::mutex::lock() /usr/include/c++/10/bits/std_mutex.h:100 (delivery_test+0x6be6)
   23:     #3 std::unique_lock<std::mutex>::lock() /usr/include/c++/10/bits/unique_lock.h:138 (delivery_test+0x6be6)
   23:     #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&) /usr/include/c++/10/bits/unique_lock.h:68 (delivery_test+0x6be6)
   23:     #5 test_delivery_tag() /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:116 (delivery_test+0x6be6)
   23:     #6 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23:   Thread T1 (tid=14834, running) created by main thread at:
   23:     #0 pthread_create <null> (libtsan.so.0+0x5ea99)
   23:     #1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xd7048)
   23:     #2 main /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:130 (delivery_test+0x646c)
   23: 
   23: SUMMARY: ThreadSanitizer: data race /home/travis/build/apache/qpid-proton/cpp/src/delivery_test.cpp:120 in test_delivery_tag()
   23: ==================
   23: ThreadSanitizer: reported 2 warnings
   23/46 Test #23: cpp-delivery_test ................***Failed    0.15 sec
   ```
   
   CI caught something about this like. Do you see what's wrong/different from the cppreference.com usage example :P ?

##########
File path: cpp/src/sender.cpp
##########
@@ -64,10 +64,18 @@ namespace {
 uint64_t tag_counter = 0;

Review comment:
       I am not aware and I could not find anything in Jira. If you can't find it either, then please file a new one.  We need to ensure that a single sender will not send a message with a duplicate tag.
   
   btw, I found this docs jira about auto settlement, https://issues.apache.org/jira/browse/PROTON-2060; it might become relevant here later.
   
   I tried to make sure that the `tag_counter` idea really is as broken as I thought it is, so here is my test program
   
   ```
   $ cat increment.cpp
   #include <thread>
   #include <cstdio>
   #include <cstdlib>
   
   int count = 0;
   
   void func1()
   {
     for ( int i =0 ; i < 1000000; ++i )
       count = count + 1;
   }
   
   void func2()
   {
     int prev = 0;
     for ( int i =0 ; i < 1000000; ++i ) {
        int neww = ++count;
        if (prev == neww) {
           printf("failure func2: prev %d, neww %d\n", prev, neww);
          abort();
           }
        prev = neww;
     }
   }
   
   void func3()
   {
     int prev = 0;
     for ( int i =0 ; i < 1000000; ++i ) {
       int neww = ++count;
       if (prev == neww) {
         printf("failure func3: prev %d neww %d\n", prev, neww);
         abort();
       }
       prev = neww;
     }
   }
   
   int main()
   {
     auto t1 = std::thread(func1);
     auto t2 = std::thread(func2);
     auto t3 = std::thread(func3);
   
     //joining all the threads
     t1.join();
     t2.join();
     t3.join();
   
     printf("total: %d \n", count);
   
     return 0;
   }
   ```
   
   and it looks that yes, it is possible to get the same tag twice in the same thread. So yes, it apparently can give you duplicate tags.
   
   ```
   % g++ increment.cpp -lpthread && ./a.out
   failure func3: prev 349602 neww 349602
   [1]    9584 abort (core dumped)  ./a.out
   % g++ increment.cpp -lpthread && ./a.out
   failure func2: prev 188384, neww 188384
   
   ```




-- 
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@qpid.apache.org
For additional commands, e-mail: dev-help@qpid.apache.org