[ 
https://issues.apache.org/jira/browse/GEODE-8874?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17273079#comment-17273079
 ] 

ASF GitHub Bot commented on GEODE-8874:
---------------------------------------

pivotal-jbarrett commented on a change in pull request #731:
URL: https://github.com/apache/geode-native/pull/731#discussion_r565524041



##########
File path: cppcache/src/TXId.cpp
##########
@@ -27,15 +27,27 @@ namespace apache {
 namespace geode {
 namespace client {
 
-std::atomic<int32_t> TXId::m_transactionId(1);
+std::atomic<int32_t> TXId::m_transactionId(0);
 
-TXId::TXId() : m_TXId(m_transactionId++) {}
+TXId::TXId() {
+  // If m_transactionId has reached maximum value, then set
+  // it to zero in order to avoid overflow to negative value.
+  auto maxValueTXId = INT32_MAX;

Review comment:
       ```c++
   auto maxValueTXId = std::numeric_limits<int>::max();
   ```
   https://en.cppreference.com/w/cpp/types/numeric_limits

##########
File path: cppcache/src/TXId.cpp
##########
@@ -27,15 +27,27 @@ namespace apache {
 namespace geode {
 namespace client {
 
-std::atomic<int32_t> TXId::m_transactionId(1);
+std::atomic<int32_t> TXId::m_transactionId(0);
 
-TXId::TXId() : m_TXId(m_transactionId++) {}
+TXId::TXId() {
+  // If m_transactionId has reached maximum value, then set
+  // it to zero in order to avoid overflow to negative value.
+  auto maxValueTXId = INT32_MAX;
+  m_transactionId.compare_exchange_strong(maxValueTXId, 0);
+  m_TXId = ++m_transactionId;

Review comment:
       While highly unlikely it is possible for `m_transactionId` to be at max 
value again between lines 36 and 37. This whole operation should be done in a 
loop where you fetch the current, increment if less than max or reset to 0, 
then compare and set the new value. If failure to set, then try again.
   
   ```c++
   auto current = m_transactionId.load();
   decltype(current) next;
   do {
       next = std::numeric_limits<decltype(current)>::max() == current ? 1 : 
current + 1;
   } while(!m_transactionId.compare_exchange_strong(current, next)); 
   
   m_TXId = next;
   ```




----------------------------------------------------------------
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:
[email protected]


> Transaction commit fail when m_transactionId integer overflow to negative 
> value
> -------------------------------------------------------------------------------
>
>                 Key: GEODE-8874
>                 URL: https://issues.apache.org/jira/browse/GEODE-8874
>             Project: Geode
>          Issue Type: Bug
>          Components: native client
>            Reporter: Jakov Varenina
>            Assignee: Jakov Varenina
>            Priority: Major
>              Labels: pull-request-available
>
> When native client increments *m_transactionId* above INT32_MAX(2147483647) 
> then due to memory overflow the *m_transactionId* is set to negative value 
> INT32_MIN(-2147483648). TransactionID (m_transactionId) is sent to the server 
> as a part of transaction message.
> {code:cpp}
> std::atomic<int32_t> TXId::m_transactionId(1);
> TXId::TXId() : m_TXId(m_transactionId++) {}
> {code}
> Currently server will interpret any negative value of transactionID as 
> non-transactional traffic.
> {code:java}
> /**
>  * checks to see if this thread needs to masquerade as a transactional 
> thread. clients after
>  * GFE_66 should be able to start a transaction.
>  *
>  * @return true if thread should masquerade as a transactional thread.
>  */
> protected boolean shouldMasqueradeForTx(Message clientMessage,
>     ServerConnection serverConnection) {
>   return 
> serverConnection.getClientVersion().isNotOlderThan(KnownVersion.GFE_66)
>       && clientMessage.getTransactionId() > TXManagerImpl.NOTX; // ---> NOTX 
> is equal -1
> }
> {code}
> After overflow happens all subsequent commit actions from the client are 
> rejected with exception:
> [vm0] [fatal 2021/01/14 15:28:41.967 CET <ServerConnection on port 39093 
> Thread 2> tid=0x52] Server connection from 
> [identity(192.168.90.23(29826:loner):48210:6c694c01,connection=1; port=48212] 
> : Unexpected Error on server
>  [vm0] org.apache.geode.InternalGemFireError
>  [vm0] at org.apache.geode.internal.Assert.throwError(Assert.java:91)
>  [vm0] at org.apache.geode.internal.Assert.assertTrue(Assert.java:55)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.command.CommitCommand.cmdExecute(CommitCommand.java:82)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.BaseCommand.execute(BaseCommand.java:183)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.ServerConnection.doNormalMessage(ServerConnection.java:848)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.OriginalServerConnection.doOneMessage(OriginalServerConnection.java:72)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.ServerConnection.run(ServerConnection.java:1214)
>  [vm0] at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
>  [vm0] at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
>  [vm0] at 
> org.apache.geode.internal.cache.tier.sockets.AcceptorImpl.lambda$initializeServerConnectionThreadPool$3(AcceptorImpl.java:691)
>  [vm0] at 
> org.apache.geode.logging.internal.executors.LoggingThreadFactory.lambda$newThread$0(LoggingThreadFactory.java:120)
>  [vm0] at java.lang.Thread.run(Thread.java:748)



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to