<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mostafa Motahari]]></title><description><![CDATA[Python Programmer | Back-end Developer]]></description><link>https://blog.mousiol.ir</link><generator>RSS for Node</generator><lastBuildDate>Thu, 30 Apr 2026 12:03:20 GMT</lastBuildDate><atom:link href="https://blog.mousiol.ir/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Solving One of the Most Frustrating NFS Issues in Longhorn]]></title><description><![CDATA[One of the must painful confusions of Longhorn is communication with NFS server.Longhorn needs NFS server to able creating volumes with RWX (ReadWriteMany) access mode. Since this problem is rare and Devops operators who currently work with Longhorn ...]]></description><link>https://blog.mousiol.ir/solving-one-of-the-most-frustrating-nfs-issues-in-longhorn</link><guid isPermaLink="true">https://blog.mousiol.ir/solving-one-of-the-most-frustrating-nfs-issues-in-longhorn</guid><category><![CDATA[Kubernetes]]></category><category><![CDATA[longhorn]]></category><category><![CDATA[NFS Server]]></category><dc:creator><![CDATA[Mostafa Motahari]]></dc:creator><pubDate>Fri, 06 Jun 2025 12:35:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1749203449919/c41c3ec2-20c6-4ad9-bd0a-61ac8ca7b6b0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One of the must painful confusions of Longhorn is communication with NFS server.<br />Longhorn needs NFS server to able creating volumes with RWX (ReadWriteMany) access mode. Since this problem is rare and Devops operators who currently work with Longhorn may be faced with this problem, we’re straightly going to discuss about the root of this issue.</p>
<ul>
<li><p><a target="_blank" href="https://github.com/longhorn/longhorn?tab=readme-ov-file#----">What is Longhorn?</a></p>
</li>
<li><p><a target="_blank" href="https://en.wikipedia.org/wiki/Network_File_System">What is NFS server?</a></p>
</li>
<li><p><a target="_blank" href="https://longhorn.io/docs/1.8.1/nodes-and-volumes/volumes/rwx-volumes/">How to enable RWX access mode in Longhorn.</a></p>
</li>
</ul>
<h3 id="heading-issue-description">Issue Description:</h3>
<p>NFS server itself has much problems with some versions of Linux kernels. <a target="_blank" href="https://longhorn.io/kb/troubleshooting-rwx-volume-fails-to-attached-caused-by-protocol-not-supported/?utm_source=chatgpt.com">In this link</a> of official Longhorn docs you can find which kernel versions are not suggested to use longhorn on them. But apart of confusing with kernels, NFS may have some issues those cause disconnecting Longhorn from NFS server. And since most of this kind of problems are so difficult to solve the Devops operator may be confused about finding the root of problem.</p>
<p>Recently I needed to replace the entire of my Kubernetes cluster so I should took two types of backups:<br />1. Velero full cluster backup to store resources of cluster and,<br />2. Longhorn volumes backup to keep the persistent data safe.</p>
<p>When I restored the Longhorn volumes in the new cluster and Velero resources next, I realized there is a problem with volumes those access mode is rwx: The pods cant read or write on their volumes and a simple “ls” command would cause the pod hang out and freeze.<br />I checked the entire Longhorn system but I couldn’t find any problem with Longhorn share manager pods or any other related resources, and everything was completely OK. All the share manager pods were running and connected to NFS server (based on their logs).</p>
<p>After lots of searching and checking the pods I realized that not all pods have this problem. About 3 pods from 58 total pods are OK and can access to their rwx volumes; so the issue was not pervasive.<br />If I hadn’t notice that, I might thought that the problem is with kernel and downgrade my cluster kernel to earlier version.</p>
<h3 id="heading-solution">Solution:</h3>
<p>So i guessed there is kind of limitations with NFS servers. On the other hand the output of <code>dmesg | grep nfs</code> showed that some IP’s tried connect to the NFS server but timeout error occurred:</p>
<pre><code class="lang-bash">[334579.017116] nfs: server 10.233.14.62 not responding, timed out
[334579.528673] nfs: server 10.233.58.28 not responding, timed out
[334579.528825] nfs: server 10.233.48.185 not responding, timed out
[334579.528894] nfs: server 10.233.22.75 not responding, timed out
[334583.112813] nfs: server 10.233.46.135 not responding, timed out
...
</code></pre>
<p>All systemd services showed that NFS server is correctly run. After lots of research and surfing in Longhorn related documentations finally I noticed that NFS server has a max threads count limitation that causes this problem and prevents of connecting other pods to the NFS server.<br />The default value of max threads is 8. So I immediately increased that to 128 by doing such this:</p>
<pre><code class="lang-bash">$ sudo systemctl edit nfs-server

<span class="hljs-comment"># Find this line and add 128 to end of the line.</span>
...
[Service]
ExecStart=/usr/sbin/rpc.nfsd 128
...

<span class="hljs-comment"># Save and exit.</span>
$ sudo systemctl daemon-reexec
$ sudo systemctl restart nfs-server
</code></pre>
<p>By applying above changes, your NFS server max threads limitation will increase to 128 temporary and will be reverted after system reboot.<br />To increase the max threads count permanently, do this:</p>
<pre><code class="lang-bash">$ <span class="hljs-built_in">echo</span> 128 &gt; /proc/fs/nfsd/threads
</code></pre>
<p>At the end, restart all problematic pods and you can see that issue’s gone.</p>
]]></content:encoded></item><item><title><![CDATA[DB Race Condition Errors, Silent Killer of DB Consistency]]></title><description><![CDATA[If you are a web-developer that works on simple and soft web projects, then probably you have never noticed about Database race conditions.
First of all, let me talk a little about race conditions. What are they, how they happen and what should we do...]]></description><link>https://blog.mousiol.ir/db-race-condition-errors-silent-killer-of-db-consistency</link><guid isPermaLink="true">https://blog.mousiol.ir/db-race-condition-errors-silent-killer-of-db-consistency</guid><category><![CDATA[Django]]></category><category><![CDATA[Databases]]></category><category><![CDATA[race-condition]]></category><category><![CDATA[data-consistency]]></category><dc:creator><![CDATA[Mostafa Motahari]]></dc:creator><pubDate>Tue, 21 Jan 2025 22:18:19 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737497791581/28404da6-01ee-43e6-80cb-107cca5bae21.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you are a web-developer that works on simple and soft web projects, then probably you have never noticed about Database race conditions.</p>
<p>First of all, let me talk a little about race conditions. What are they, how they happen and what should we do about them.</p>
<h3 id="heading-race-conditions">Race conditions</h3>
<p>Suppose a DB row that holds data of a random user of your website. you wanna add some budgets to its balance. In normal situation, you may do something like this: (Note that this code snippet is just for example. Using magic tricks and clean coding are not goals of this content.)</p>
<pre><code class="lang-python">user = models.UserProfile.objects.get(username=<span class="hljs-string">'Adam'</span>)
user.budget += <span class="hljs-number">1000</span>
user.save()
</code></pre>
<p>In the first look, everything looks normal. Now we wanna add some more complex processes right before saving the new budget of user. For example checking the available budget with user’s bank account balance through an API. Then if there was any problem with verifying user’s balance, the budget amount that was supposed to be added, will change. OK?</p>
<p>For handling such processes we have to do some actions in our web-app that takes a bit of time.<br />Now what will be happen if during verifying the user’s budget by bank API, another process override the content of the budget field?</p>
<p>In this situation two processes write on a specific field and race condition error occurs.<br />Race condition error may cause critical problems in such web-app those a great probability of overriding a row data by two parallel processes exists in them. Like bank apps, financials or etc.</p>
<p>In a bank transaction, the transaction must not cost any balance of user account until the transaction is over completely successful. That if it is not so, a user can exchange a single amount of money to other accounts more than one time.</p>
<p>The process of exchanging is as follows: When the bank received the exchange request from user, it checks the balance of account and if there were enough amounts of money, then submits the request.<br />Now exactly after the balance check process and before submitting the request, user can apply more requests to exchange money, and since no money has been deducted from balance yet (Because the process is not submitted yet and balance will deducted after the process has been submitted), the bank program will verify other request also. (If the bank doesn’t care about race conditions).</p>
<h3 id="heading-ways-to-prevent">Ways to prevent</h3>
<p>Now, that we now what is race condition and how it may occur, It’s time to know what can we do against these critical errors.</p>
<ol>
<li><h3 id="heading-using-db-transactions">Using DB transactions</h3>
<p> The first step and must important one, is using Database transaction. although it does not prevent RC errors singly, but it’s the foundation of building such apps like bank applications that have parallel processes.</p>
</li>
<li><h3 id="heading-row-blocking">Row blocking</h3>
<p> The main task to do is designing a logic that blocks the DB row when we start to Updating or changing that.<br /> You can design this logic yourself. But frameworks like Django, provide enhanced solutions to apply this logic when you need.</p>
</li>
</ol>
<h3 id="heading-django-selectforupdate-method">Django <code>select_for_update()</code> method</h3>
<p>Like other powerful web frameworks, Django provides lots of functionalities to apply complex database related logics in your app. One of them is <code>select_for_update()</code> method.<br />In short, when you use this method inside of an atomic transaction, It blocks the row data until the transaction closes.(Committed or rolled back)<br />To do this, we can just calling it from <code>objects</code> manager of a Model:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.db <span class="hljs-keyword">import</span> transaction

<span class="hljs-keyword">with</span> transaction.atomic():
    row = models.SomeModel.objects.select_for_update().get(pk=some_pk)
</code></pre>
<p>In above code, the row of some_model table will be blocked until the transaction context manager ends.</p>
<p>At the end, you can also implement this logic yourself. You can define a new field in your model named <code>is_locked = models.BooleanField(default=False)</code> or something like that and when you want to start editing the object you’ll make it True. (Don’t forget to change the is_blocked back to False after your changes committed, Otherwise the row will stay blocked forever!)</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>In summary, race conditions in web applications, especially those involving database transactions, can lead to severe issues, such as data inconsistency or even financial mistakes. Understanding how race conditions occur is key to preventing them, particularly in applications where critical processes, such as financial transactions, are involved.</p>
<p>By employing techniques like database transactions and row locking, such as Django's <code>select_for_update()</code>, you can prevent database inconsistencies.</p>
]]></content:encoded></item><item><title><![CDATA[Elegant Trick to Swap Variables Without Extra Memory]]></title><description><![CDATA[Imagine you are in C++ programming language exam session. You relaxed and answering the questions one after another. After a while you solved all the quests and now it’s time to solve the last one.Question Title:

We have two variables like below:int...]]></description><link>https://blog.mousiol.ir/elegant-tricks-to-swap-variables-without-extra-memory</link><guid isPermaLink="true">https://blog.mousiol.ir/elegant-tricks-to-swap-variables-without-extra-memory</guid><category><![CDATA[#cpp #guide]]></category><category><![CDATA[tricks]]></category><category><![CDATA[variables]]></category><category><![CDATA[memory-management]]></category><dc:creator><![CDATA[Mostafa Motahari]]></dc:creator><pubDate>Fri, 10 Jan 2025 22:46:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1736544741389/adfb05c2-c413-43da-bd81-b7af23e77642.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Imagine you are in C++ programming language exam session. You relaxed and answering the questions one after another. After a while you solved all the quests and now it’s time to solve the last one.<br />Question Title:</p>
<blockquote>
<p><strong>We have two variables like below:</strong><br /><code>int a = 10; int b = 12;</code></p>
<p>Swap variable’s content and print the result.</p>
</blockquote>
<p>At first, you think, wow! what a simple question. Let’s get free scores. But suddenly you notice something at the bottom of the question:</p>
<blockquote>
<p>(Note: you can not use a any third variable!)</p>
</blockquote>
<p>Wait! What? Without any third variable? How it’s even possible?</p>
<h2 id="heading-here-is-how">Here is how:</h2>
<p>You may know that in compiled language like C++ everything will be converted to binary (Zero and One).<br />So you can convert the variable’s content to binaries also. By doing that and replacing the content of the variables with the binary one and printing them to output, You’ll see something like this:</p>
<pre><code class="lang-cpp">...
<span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"A contains: "</span>&lt;&lt;a&lt;&lt;<span class="hljs-built_in">endl</span>;
str::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"B contains: "</span>&lt;&lt;b;
...

Output:
A contains: <span class="hljs-number">1010</span>
B contains: <span class="hljs-number">1100</span>
</code></pre>
<p>And looks normal.</p>
<h3 id="heading-step-2">Step 2:</h3>
<p>Now it’s time to play the magic trick. If we apply the XOR operation three times in each digits column of the binaries content, we could see that the variables just swapped automatically!<br />Don’t you believe it? Let’s give it a try!</p>
<blockquote>
<p>“<a target="_blank" href="https://www.analog.com/en/resources/glossary/xor-gate.html">XOR</a>” <strong>an abbreviation for “Exclusively-OR.”</strong> The simplest XOR gate is a two-input digital circuit that outputs a logical “1” if the two input values differ, i.e., its output is a logical “1” if either of its inputs are 1, but not at the same time (exclusively).</p>
</blockquote>
<p>The first column of digits are two number ones. If we apply XOR on them, it will be 0. right?<br />Now we’ll put the operation result in place of the first A’s digit and A looks like <code>0010</code>. Great. Then we need do the same process two more times and each time put the operation result in the opposite variable name (we put the result of XOR between A &amp; B in A, So in the same way B &amp; A in B. The order is important).</p>
<pre><code class="lang-cpp">A: <span class="hljs-number">1010</span> <span class="hljs-comment">// Zero step.</span>
B: <span class="hljs-number">1100</span>
A: <span class="hljs-number">0010</span> <span class="hljs-comment">// First step for the first colume of digits.</span>
B: <span class="hljs-number">1100</span> <span class="hljs-comment">// Second step for the first colume of digits.</span>
A: <span class="hljs-number">1010</span> <span class="hljs-comment">// Third step for the first colume of digits.</span>
</code></pre>
<h3 id="heading-step-3-final">Step 3 (Final):</h3>
<p>Great! now it’s enough to just do the same process for the 3 remaining columns of digits. (As done below)<br />Note that in the below codes, the process were applied at a same time for all columns to prevent boring calculations:</p>
<pre><code class="lang-cpp">A: <span class="hljs-number">1010</span> <span class="hljs-comment">// Zero step. definition.</span>
B: <span class="hljs-number">1100</span>
A: <span class="hljs-number">0110</span> <span class="hljs-comment">// First step</span>
B: <span class="hljs-number">1010</span> <span class="hljs-comment">// Second step</span>
A: <span class="hljs-number">1100</span> <span class="hljs-comment">// Last step. just look at the result!</span>
</code></pre>
<p>Did you see that? At the end of the third step, the content of our two variables are completely swapped!<br />The strange thing is that the whole process will work for any binary values and there are no any exceptions!<br />You can try it by your self with different values and see that it works fine with any binary value.</p>
<h3 id="heading-one-more-note">One more note:</h3>
<p>At the end, if you want to test on any other datatype apart from int datatype, It will work fine. BUT the important note is that you should convert your data into binary before you start the process, and after swapping the values convert them back to your considered datatype.</p>
<h3 id="heading-conclusion">Conclusion:</h3>
<p>In this article, we demonstrated a clever way to swap two variables without using a third variable in C++ by applying the XOR bitwise operation. This technique works by manipulating the binary representation of the variables through a series of XOR steps, resulting in the values being swapped.<br />Here, we used C++ language to explain our subject. But it works fine with all programming languages those you’re able to convert datatypes into binaries by them; Because as you know, binary isn’t depend on programming languages.</p>
]]></content:encoded></item></channel></rss>