<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic ConcurrentAppendException in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/concurrentappendexception/m-p/81408#M3532</link>
    <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;We're dealing with a concurrency issue when we attempt to run multiple jobs at the same time, and we're still having the same problem even after using partition and liquid clustering features. Now we're making sure to have the right where condition for all updates to prevent concurrency problems.&lt;/P&gt;&lt;P&gt;Please review the options below and tell us if this is the correct way to solve the problem or if there is another way to fix it.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Option1:&lt;/STRONG&gt;&lt;BR /&gt;Having tables properly partitioned and referred to in the WHERE clause, along with unique filter criteria for each concurrent calls, is crucial.&lt;BR /&gt;Suppose you run the code concurrently for different dates or countries. Since each job is working on an independent partition on the target Delta table,&lt;BR /&gt;you don’t expect any conflicts. However, the condition is not explicit enough and can scan the entire table and can conflict with concurrent operations updating any other partitions.&lt;BR /&gt;Instead, you can rewrite your statement to add specific date and country to the merge condition, as shown in the following example.&lt;/P&gt;&lt;P&gt;// Target 'deltaTable' is partitioned by date and country&lt;BR /&gt;deltaTable.as("t").merge(&lt;BR /&gt;source.as("s"),&lt;BR /&gt;"s.user_id = t.user_id AND s.date = t.date AND s.country = t.country AND t.date = '" + &amp;lt;date&amp;gt; + "' AND t.country = '" + &amp;lt;country&amp;gt; + "'")&lt;BR /&gt;.whenMatched().updateAll()&lt;BR /&gt;.whenNotMatched().insertAll()&lt;BR /&gt;.execute()&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Option 2:&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;&lt;STRONG&gt;Whenever that ConcurrentAppendException occurs, make sure to incorporate application-specific retry logic into the code.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;retry=5&lt;BR /&gt;while (retry &amp;gt; 0):&lt;BR /&gt;try:&lt;BR /&gt;Update statement on delta table&lt;BR /&gt;break&lt;BR /&gt;except Exception as e:&lt;BR /&gt;retry = retry -1&lt;BR /&gt;delay = 20 #random.randrange(0,20)&lt;BR /&gt;time.sleep(delay)&lt;BR /&gt;print(str(retry) + " Failed , added delay " + str(delay))&lt;BR /&gt;else&lt;BR /&gt;raise "updatefailed"&lt;/P&gt;</description>
    <pubDate>Thu, 01 Aug 2024 04:48:34 GMT</pubDate>
    <dc:creator>Phani1</dc:creator>
    <dc:date>2024-08-01T04:48:34Z</dc:date>
    <item>
      <title>ConcurrentAppendException</title>
      <link>https://community.databricks.com/t5/get-started-discussions/concurrentappendexception/m-p/81408#M3532</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;We're dealing with a concurrency issue when we attempt to run multiple jobs at the same time, and we're still having the same problem even after using partition and liquid clustering features. Now we're making sure to have the right where condition for all updates to prevent concurrency problems.&lt;/P&gt;&lt;P&gt;Please review the options below and tell us if this is the correct way to solve the problem or if there is another way to fix it.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Option1:&lt;/STRONG&gt;&lt;BR /&gt;Having tables properly partitioned and referred to in the WHERE clause, along with unique filter criteria for each concurrent calls, is crucial.&lt;BR /&gt;Suppose you run the code concurrently for different dates or countries. Since each job is working on an independent partition on the target Delta table,&lt;BR /&gt;you don’t expect any conflicts. However, the condition is not explicit enough and can scan the entire table and can conflict with concurrent operations updating any other partitions.&lt;BR /&gt;Instead, you can rewrite your statement to add specific date and country to the merge condition, as shown in the following example.&lt;/P&gt;&lt;P&gt;// Target 'deltaTable' is partitioned by date and country&lt;BR /&gt;deltaTable.as("t").merge(&lt;BR /&gt;source.as("s"),&lt;BR /&gt;"s.user_id = t.user_id AND s.date = t.date AND s.country = t.country AND t.date = '" + &amp;lt;date&amp;gt; + "' AND t.country = '" + &amp;lt;country&amp;gt; + "'")&lt;BR /&gt;.whenMatched().updateAll()&lt;BR /&gt;.whenNotMatched().insertAll()&lt;BR /&gt;.execute()&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Option 2:&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;&lt;STRONG&gt;Whenever that ConcurrentAppendException occurs, make sure to incorporate application-specific retry logic into the code.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;retry=5&lt;BR /&gt;while (retry &amp;gt; 0):&lt;BR /&gt;try:&lt;BR /&gt;Update statement on delta table&lt;BR /&gt;break&lt;BR /&gt;except Exception as e:&lt;BR /&gt;retry = retry -1&lt;BR /&gt;delay = 20 #random.randrange(0,20)&lt;BR /&gt;time.sleep(delay)&lt;BR /&gt;print(str(retry) + " Failed , added delay " + str(delay))&lt;BR /&gt;else&lt;BR /&gt;raise "updatefailed"&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2024 04:48:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/concurrentappendexception/m-p/81408#M3532</guid>
      <dc:creator>Phani1</dc:creator>
      <dc:date>2024-08-01T04:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: ConcurrentAppendException</title>
      <link>https://community.databricks.com/t5/get-started-discussions/concurrentappendexception/m-p/81461#M3544</link>
      <description>&lt;P&gt;Option 3 Bit complicated process but works well:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Considering you will do Update, Insert and Delete operation on the same table at the same time by using multiple job.&lt;/P&gt;&lt;P&gt;1. Create a Dummy table with the target table schema with additional column called &lt;STRONG&gt;Operation_flag&lt;/STRONG&gt; and Insert the rows which are needs to Updated or appended or deleted in the dummy table while inserting the rows please update the&amp;nbsp;Operation_flag column with Insert, Update or Delete.&lt;BR /&gt;2. Filter the data based on&amp;nbsp;&lt;STRONG&gt;Operation_flag&lt;/STRONG&gt;&amp;nbsp;in the Dummy table and do Update, Insert and Delete one by one in the target table once everything is done clear dummy table.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; For example :&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2024 08:56:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/concurrentappendexception/m-p/81461#M3544</guid>
      <dc:creator>Kannathasan</dc:creator>
      <dc:date>2024-08-01T08:56:45Z</dc:date>
    </item>
  </channel>
</rss>

