Transactions and Locks in MySQL

Refer: https://stackoverflow.com/questions/61212438/why-do-we-need-to-lock-a-mysql-table-during-select-when-beginning-a-transaction/

TL;DR:

  1. MySQL docs are wrong
  2. Transactions act like a try-catch block. They don’t provide protection against concurrency. If something fails in a transaction, it can be rolled back to undo pending operations that were not committed.
  3. To handle concurrency you need to create locks.
  4. SELECT … LOCK IN SHARE MODE is a Reader-Writer lock that allows multiple threads to read and only 1 to write. https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
  5. SELECT … FOR UPDATE is a normal lock aka Mutex – mutual exclusion. https://en.wikipedia.org/wiki/Lock_(computer_science)

The statement

Beginning a transaction causes any pending transaction to be committed is extremely misleading because what it fails to mention is that The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement. What this means is that if in our code you have a START TRANSACTION and you forget to explicitly COMMIT it, then later on whenever another START TRANSACTION is encountered, it will cause the running transaction to be committed before a new one can begin. This is likely not what you thought it does when you read  Beginning a transaction causes any pending transaction to be committed.
Readers–writer lock – Wikipedia
In computer science, a readers–writer (single-writer lock, a multi-reader lock, a push lock, or an MRSW lock) is a synchronization primitive that solves one of the readers–writers problems.An RW lock allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is …
en.wikipedia.org
Posted in Software | Tagged | Leave a comment

A note on the dbDelta function

The dbDelta function documented at https://codex.wordpress.org/Creating_Tables_with_Plugins:

The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary, so it can be very handy for updates (see wp-admin/upgrade-schema.php for more examples of how to use dbDelta)

except hat it does not work. Here is a real example when I tried to use it. I created a table using following command:

the table got created. But then I tried to update it using dbDelta

this is the result:

so $result = Array
(

[visitor_log(

] => Created table visitor_log(

)

whereas $wpdb->last_error = Table ‘visitor_log’ already exists

When I checked in mysql, the table is not modified in any way.

Posted in Software | Tagged | Leave a comment

Writing Your First WordPress Plugin

This post describes how to develop a WordPress plugin that can be used to count visits to a WordPress site. The basic idea is that we will create a table visitor_log in which we will store the timestamp, url_visited, ip_address, and user details of every visit. To do that, we need to:

  1. Create our plugin file (can name it anything ending with a .php) that will go under our plugin folder. The plugin folder itself is stored under wp-content/plugins. WordPress scans all folders under this directory and displays them in admin plugins dashboard. The plugin file should start with a preamble.

    Its also a good idea to declare a namespace to avoid conflicting function names. PHP uses the character in namespace instead of the more common and readable . character used in other languages. So com.foo.myclass becomes comfoomyclass in PHP

  2. Create the visitor_log table when plugin is activated
  3. Insert rows into the table when a page is visited

WP provides a register_activation_hook that can be used to do tasks when a plugin gets activated. We can use this hook as follows:

and then in our init_site_counter function, we can create a table like so

Finally for logging visits to the site, we subscribe to the template_redirect event

and add a row as shown below:

Exercise: The row_id variable is declared as 8 byte int which means the mysql table will be able to store

visits before the row_id variable overflows and resets to 1. In reality the disk is more likely to become full before the overflow happens. It would be good if we can auto-purge data from the table to prevent a disk-space full error. One elegant way to do this is to reduce the length of the row_id variable to lets say 4 bytes:

Now when the row_id overflows it will start overwriting the oldest records and prevent the table from growing in size which is exactly what we want except that we see an error when trying to insert a row with an id that already exists.

and if we remove the PRIMARY KEY from the table definition

there is an error when trying to create the table. It expects a primary key to be defined

Posted in Software | Tagged | 2 Comments

Backing up WordPress db

It is very important to backup wordpress db. This is done using mysqldump command. In the basic form:

mysqldump -u wpreader --password='password' wordpress > backup.sql

But we need to automate this and protect the password. This is done by following the steps at https://www.linode.com/docs/databases/mysql/use-mysqldump-to-back-up-mysql-or-mariadb/.

First create a user backupagent that has necessary privileges (read and lock tables) to all the databases

mysql> CREATE USER 'backupagent'@'localhost' IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> grant select on *.* to 'backupagent'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> grant lock tables on *.* to 'backupagent'@'localhost'; Query OK, 0 rows affected (0.00 sec)

Create a file .my.cnf under $HOME directory that stores

[client] user=backupagent password=backup agent's password (put it in quotes to be safe)

chmod this file to 600. Write a bash script backup.sh

and a cronjobs.txt

and create a crontab:

$ crontab cronjobs.txt

The cron job will run under your credentials but it will only get a handful of environment variables. To see the env variable it gets, I added an entry to the cron file as explained in https://askubuntu.com/a/23438/393311

* * * * * env > /tmp/env.output

Every minute of every day of every week of every month, that command runs. https://serverfault.com/a/162389/77118

with this, I get:

$ cat /tmp/env.output HOME=/home/siddjain LOGNAME=siddjain PATH=/usr/bin:/bin LANG=C.UTF-8 SHELL=/bin/sh PWD=/home/siddjain

Somewhere I read that cron job will inherit all environment variables defined in /etc/environment but that does not seen to be the case above:

$ cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
Posted in Software | Tagged | 1 Comment

Fabric CA Step by Step – Part 2

Restart Fabric CA server. This time we don’t use -b switch since we have a fabric-ca-server-config.yaml from previous run. Even if we used -b switch it will be ignored.

note that it says

2020/03/27 10:38:13 [INFO] The CA key and certificate already exist

so its picking up the key and certificate from previous run.

Register a new user tom

The register command can only be executed by a registrar – only a registrar can register new users.

when fabric-ca-client is run, it runs under identity of a user (except when enrolling a user). This identity is picked from from $FABRIC_CA_CLIENT_HOME/msp by default. This can be overridden using -M switch.

Enroll tom. his credentials will be saved under $PWD/tom. During this time the -M switch acts differently. This time -M switch is used to specify the location where tom’s credentials should be stored.

Next the registrar can check if a user already exists in the CA db using fabric-ca-client identity list command

and retrieve his X.509 certificate if needed using fabric-ca-client certificate list command

if you run above commands with tom’s identity they will fail as tom is not a registrar.

Posted in Software | Tagged | Leave a comment

Step by Step with Fabric CA

fabric-ca-server (v. 1.4.5) can be started without giving it any config or its own self-identity (public cert and private key)
from an empty directory

run below command. in below bob is the bootstrap user and bobpw is his password. we need a user to bootstrap the CA. this user automatically becomes an admin user.

it will automatically generate config file and identity for itself

if you open this config observe following section in it that stores bob and bobpw

if you start the CA and give it a config file, it will ignore the -b switch and use the identities section for bootstrap user and password. try it.

enroll the bootstrap user. from the client directory which is also empty

run

note that it is the client who is generating the private key (and also the CSR) not the server and the client never sends the private key to the server. now have several files in client directory

server logs show

to test if a given public private key-pair match use

lets look inside the sqlite3 db on the server

log in to the sqlite3 db

list tables

list users

the second entry is bob’s hashed password.

list certificates

the server has a copy of the public cert but it does not have bob’s private key.

credentials table is empty

to see the schema of a table (note no semi-colon at end of command)

to log out of sqlite3 use Ctrl+D

Posted in Software | Tagged | Leave a comment

Steps to add Like button to your custom installation of wordpress

WordPress (WP for short) does not come with any like button functionality built-in. You have two choices to add the like button. Use a 3rd party plugin. The caveat with these is that they will send your data to a remote server. Of course, you also have to pay for these services and create an account. The other option is to do-it-yourself (DIY). This post describes the second option.

I used following code https://github.com/JonMasterson/WordPress-Post-Like-System/. So first read through it.

Steps I followed to get it working (with the twentynineteen theme):

  1. Add content of https://raw.githubusercontent.com/JonMasterson/WordPress-Post-Like-System/master/css/simple-likes-public.css to the end of wp-content/themes/twentynineteen/style.css – this defines the styles for button but not the heart SVG.
  2. Add new javascript file simple-likes-public.js under wp-content/themes/twentynineteen/js– this makes the AJAX call to backend when button is clicked.
  3. Add the content of https://raw.githubusercontent.com/JonMasterson/WordPress-Post-Like-System/0.5.3/post-like.php to bottom of wp-content/themes/twentynineteen/functions.phpThis file is really doing most of the work. It also defines the heart SVG in get_liked_icon(). I suggest using font-awesome in this function to display the heart shape instead of a custom SVG. To use font-awesome, add it to the list of downloads:

    data/wordpress/wp-content/themes/twentynineteen/functions.php: wp_enqueue_style( ‘font-awesome‘, ‘https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css’ );

    and modify the get_liked_icon and get_unliked_icon as follows:

    function get_liked_icon() {

    $icon = ‘<span class=”sl-icon”><i class=”fa fa-heart”></i></span>’;

    return $icon;

    } // get_liked_icon()

    function get_unliked_icon() {

    $icon = ‘<span class=”sl-icon”><i class=”fa fa-heart-o”></i></span>’;

    return $icon;

    } // get_unliked_icon()

  4. Another change I made is that I replaced all occurrences of "YourThemeTextDomain" with wp_get_theme()->get( 'TextDomain' ) as this user has commented ref. The other important thing was to comment out all the code that allowed anonymous user to like.
  1. The final step is to edit wp-content/themes/twentynineteen/inc/template-tags.php and add the line echo get_simple_likes_button( get_the_ID() ); in the function twentynineteen_entry_footer(). This will add the button to the posts. I did not add the like button to comments.

Re: shortcode, these are codes like , that you add when authoring your posts. There was no need to add shortcode in php. The [jmliker] shortcode does exactly what echo get_simple_likes_button( get_the_ID() ); does and is defined in function sl_shortcode() in functions.php.

The like button code creates some fields in the wp_postmeta table:

mysql> select meta_key from wp_postmeta; +
Posted in Software | Tagged | Leave a comment

What is Quantum Computing – Part II

1. Summary

  • Definition (from Jack Hidary’s book): A quantum computer is a device that leverages specific properties described by quantum mechanics to perform computation.
  • A quantum computer is not a parallel computer.
  • Any quantum computation is described by a series of matrix multiplications.
  • Not any matrix is permitted. The matrix has to be reversible plus there are other constraints as well imposed by laws of quantum physics.
  • So the whole computation or circuit as it is called – just like we have a Boolean circuit in classical computing – can be reduced to a matrix operation known as the unitary matrix of the circuit denoted by U.
  • The input to the circuit is a familiar bit string of n bits and output is also a n bit string. E.g., with n=4, example input: 0101 and example output: 1100.
  • However, the output of a quantum circuit is not deterministic. A quantum circuit can put the bits into superposition and entanglement with the result that the output bit string is described by a joint probability distribution function ||Ψ||2. So there will be a ||Ψ0000||2 that gives the joint distribution of the output bits when input is 0000 and a different ||Ψ0001||2 that gives the joint distribution of output bits when input is 0001. Keeping the input fixed at 0000 if we make repeated measurements, we will get different outputs (e.g., in one measurement it can be 0011, in another measurement it can be 1010) whose frequency of occurrence will obey ||Ψ0000||2. Take a pause and let this sink in. Now we can see if we have nbits, there are total 2n possible outputs and so ||Ψ||2 for a given input is a real-valued vector of length 2n. And if we consider all the inputs, we get a matrix of order 2n × 2n. This matrix is nothing but ||𝑈||2. The ij entry in this matrix ||𝑈||2𝑖𝑗 is the probability that when the circuit is given input j, the output will be i.
  • We can see from above that it becomes exponentially difficult to simluate the outcome of a quantum computer using a classical computer. That fact in itself provides a somewhat contrived use-case where quantum computers would outperform classical computers and is at the heart of Google’s Quantum Supremacy achievement.
  • Given an input, if the joint probability distribution of output bits can be factored out into a multiplication of individual probabilities i.e., if ||Ψ||2=Π𝑖||𝜓𝑖||2 or taking an example of n=4 bits, if following holds ||Ψ||2=||𝜓𝑎||2⋅||𝜓𝑏||2⋅||𝜓𝑐||2⋅||𝜓𝑑||2. Stated in English we are saying that if the probability we will get an output of abcd – where each of a, b etc. is a 0 or 1 – is equal to the probability that the 0 bit is equal to d which we denote by ||𝜓𝑑||2 and further this number ||𝜓𝑑||2 doesn’t depend on what the other bits are, multiplied by the probability that the 1st bit is equal to c given by ||𝜓𝑐||2 and so on, then this corresponds to the case when there is no entanglement. So the definition of entanglement is a joint pdf that cannot be factored out. If there is no entanglement then we only need to store 2𝑛 numbers for the individual probabilities (0 or 1) of the n output bits to calculate ||Ψ||2 for a given input. But this case does not make for any interesting applications.
  • Superposition is the process that puts a classical bit (a classical bit is a definite 0 or 1) into a quantum state when it is no longer a 0 or 1 but is simultaneously both with some probability. We call this a qubit. What does it mean physically? The spin of an electron is a good candidate for a qubit. Whenever we measure the spin, it is either up or down but before measurement it can be in a superposition of both up and down states. A classical bit is put in a superposition state by application of a Hadamard gate. The application of a Hadamard followed by a controlled not CNOT puts two bits into entanglement. We can see why. The CNOT gate takes two bits as input – one is a control bit and the other is a target bit. The CNOT gate will flip the target bit depending on whether the control bit is set. And the control bit itself is neither 0 or 1. Its in a superposition of the two states. That entangles the two bits as their fate is entertwined now.
  • Superposition and entanglement are the essence of quantum physics.

2. An example: putting it all together

Install the Cirq library by following the steps at https://github.com/quantumlib/Cirq/blob/master/docs/install.md#installing-on-docker

docker pull quantumlib/cirq

Check it works

docker run -it quantumlib/cirq
 python -c "import cirq; print(cirq.google.Foxtail)"
 # should print:
 # (0, 0)───(0, 1)───(0, 2)───(0, 3)───(0, 4)───(0, 5)───(0, 6)───(0, 7)───(0, 8)───(0, 9)───(0, 10)
# │ │ │ │ │ │ │ │ │ │ │
 # │ │ │ │ │ │ │ │ │ │ │
 # (1, 0)───(1, 1)───(1, 2)───(1, 3)───(1, 4)───(1, 5)───(1, 6)───(1, 7)───(1, 8)───(1, 9)───(1, 10)

Run the image

docker run -it quantumlib/cirq

Create a random circuit of 4×4 qubits arranged in a grid. The code is from the book Quantum Computing by Jack Hidary. It uses the cirq.experiments.generate_supremacy_circuit_google_v2_grid method which is no longer available in latest version of cirq but is there in the version of cirq in the Docker image (as of this writing):

root@eb880b341bec:/examples# python Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import cirq
 >>> nrows=4
 >>> ncols=4
>>> depth=5
>>> supremacy_circuit=cirq.experiments.generate_supremacy_circuit_google_v2_grid(nrows, ncols, depth, seed=123)
 >>> print(supremacy_circuit)
 ┌──────┐ (0, 0): ───H───@───X^0.5─────────T───────@────────X^0.5───H─── │ │ (0, 1): ───H───@───X^0.5─────────@───────┼X^0.5───T───────H─── │ │ (0, 2): ───H───T─────────────────@───────┼@───────@───────H─── ││ │ (0, 3): ───H───T─────────────────────────┼┼───────@───────H─── ││ (1, 0): ───H───T───@─────────────Y^0.5───@┼───────@───────H─── │ │ │ (1, 1): ───H───T───┼──────────────────────┼───────@───────H─── │ │ (1, 2): ───H───@───┼────@────────X^0.5────@───────X^0.5───H─── │ │ │ (1, 3): ───H───@───┼────┼X^0.5───T────────────────────────H─── │ │ (2, 0): ───H───@───@────┼────────Y^0.5───T────────────────H─── │ │ (2, 1): ───H───@───X^0.5┼────────@───────@────────X^0.5───H─── │ │ │ (2, 2): ───H───T────────@────────@───────┼X^0.5───@───────H─── │ │ (2, 3): ───H───T─────────────────────────┼@───────@───────H─── ││ (3, 0): ───H───T─────────────────────────┼┼───────@───────H─── ││ │ (3, 1): ───H───T─────────────────────────@┼───────@───────H─── │ (3, 2): ───H───@───Y^0.5─────────T────────┼───────────────H─── │ │ (3, 3): ───H───@───X^0.5─────────T────────@───────X^0.5───H─── └──────┘

Increasing the depth of the circuit will expand it in the horizontal direction (i.e., more to the right) and insert more quantum gates. Let’s calculate the unitary of this circuit:

>>> U = cirq.unitary(supremacy_circuit)
 Killed

Oops! well, we have 16 qubits, meaning we are trying to calculate a matrix of 216 x 216 complex numbers. This will take total

>>> import math
>>> a=math.pow(2,16)
 >>> a*a*2*4 34359738368.0

bytes (this is 34GB in case you missed it) assuming each real number is stored as 4 byte float. So we can see its very difficult for classical computers to simulate what we will get from a quantum computer.

Let’s reduce the circuit to 3×3 qubits and try again

>>> import cirq
 >>> nrows=3
>>> ncols=3
 >>> depth=5
>>> supremacy_circuit=cirq.experiments.generate_supremacy_circuit_google_v2_grid(nrows, ncols, depth, seed=123)
 >>> print(supremacy_circuit)
 (0, 0): ───H───@───X^0.5────T───────@────────────X^0.5───H─── │ │ (0, 1): ───H───@───X^0.5────@───────┼────Y^0.5───T───────H─── │ │ (0, 2): ───H───T────────────@───────┼────@───────X^0.5───H─── │ │ (1, 0): ───H───T───@────────X^0.5───@────┼───────@───────H─── │ │ │ (1, 1): ───H───T───┼─────────────────────┼───────@───────H─── │ │ (1, 2): ───H───T───┼────@───Y^0.5────────@───────X^0.5───H─── │ │ (2, 0): ───H───@───@────┼───X^0.5───T────────────────────H─── │ │ (2, 1): ───H───@───X^0.5┼───@───────X^0.5────────T───────H─── │ │ (2, 2): ───H───T────────@───@───────Y^0.5────────T───────H───

Ready to calculate unitary? Try it:

>>> U=cirq.unitary(supremacy_circuit)
 >>> import numpy
 >>> numpy.shape(U) (512, 512)

Let’s see Ψ0 – the wave function when input to the circuit is 000000000 (remember we have 3×3 or 9 qubits). Its a complex valued vector. The first 3 elements are shown:

>>> psi_0 = U[:,0]
 >>> psi_0[0:3]
 array([-0.00457646-0.02209709j, -0.03772209-0.00457646j,
-0.05334709-0.03314563j])

Measuring the circuit will collapse the wave function to a definite state and the probabilities of getting 000000000, 000000001, 000000010, 000000011 and so on in the output will be given by the square of the modulus (also known asamplitude) of the complex numbers. We can calculate it like this

>>> prob = numpy.square(numpy.abs(psi_0))

Verify

>>> numpy.sum(prob)
 1.0000000000000018

Let’s take a random entry in the matrix U

>>> U[45,33]
 (-0.06897208691207966+0.03772208691207965j)

What does this entry mean? It means that if this circuit is fed an input of

>>> "{0:09b}".format(33)
 '000100001'

the output will be

>>> "{0:09b}".format(45)
 '000101101'

with a probability

>>> numpy.square(numpy.abs(U[45,33]))
 0.006180104614009962

Google’s quantum supremacy experiment generated a random circuit of 𝑛×𝑛 qubits. They then keep the input fixed at 000⋅⋅⋅000 and sample the output of the circuit many times. The results from sampling the output are used to create a pdf which is then verified against the expected pdf calculated using a classical computer. This tells them that the quantum circuit is behaving correctly. They slowly increase n until they reach a point (n=52) where calculation of expected pdf using a classical computer becomes infeasible – the moment of quantum supremacy.

One can see that one natural application of quantum computers will be to generate truly random numbers. This corresponds to a wave function where each ||𝜓𝑖||2=𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 corresponding to a uniform pdf.

There are two challenges in quantum computing: One is that keeping the qubits entangled and superimposed becomes very difficult as the number of bits and the depth of the circuit increases. This is the hardware challenge and this is what Google accomplished with their recent results on quantum supremacy. The other is finding problems that can be expressed as quantum computations – where quantum computing can outperform classical computing. This is the software challenge. The famous example is Shor’s factorization algorithm which can be used to break RSA cryptography that forms the backbone of secure communications between computers today, but it will take sometime for it to become practical since the number of qubits it requires is outside what today’s quantum computers can provide.

Posted in Software | Leave a comment

Fabric JIRA queries and links to uncommon resources

To see the issues that I am watching (https://stackoverflow.com/a/45876107/147530)

https://jira.hyperledger.org/browse/FAB-17584?jql=key%20in%20watchedIssues()%20ORDER%20BY%20created%20DESC

or navigate to Issues -> Search and type

key in watchedIssues() ORDER BY created DESC

To see the issues I have filed

https://jira.hyperledger.org/browse/FABN-1234?jql=reporter%20%3D%20siddjain

or type reporter = siddjain in search bar

Questions I have asked on Fabric DL: https://lists.hyperledger.org/g/fabric/search?ev=false&q=posterid%3A1540551&ct=1

list open issues of node-sdk

labels = node-SDK and status != Complete

https://jira.hyperledger.org/issues/?jql=labels%20%3D%20node-SDK%20%20and%20status%20!%3D%20Complete%20

Community Calls: https://wiki.hyperledger.org/display/fabric/Fabric+Application+Developer+Community+Calls

Posted in Software | Tagged | Leave a comment

A note on how environment variables are handled in Bash

Say you h​ave a bash script that looks like


​#!/bin/bash
SOME_VARIABLE=hello
some_program

where some_program ​is just some executable. Further this script is executed like

$ FOO=bar ./my_script.sh

Observation: the variable FOO=bar ​will make it to some_program ​whereas SOME_VARIABLE=hello ​will not.

To pass SOME_VARIABLE=hello to some_program turn on following flag in your script:

set -a

or put SOME_VARIABLE=hello on the same line as some_program in the script as shown below:

SOME_VARIABLE=hello some_program

It can be tested as follows

  1. Create a file JavaEnvironmentVariables.java with this code
  2. Compile this program by running
    ​$ javac JavaEnvironmentVariables.java
  3. ​Write test script as follows
    #!/bin/bash

    SPAM=eggs

    java JavaEnvironmentVariables

  4. Chmod755 the script and execute it
    $ FOO=bar ./my_script.sh
  5. Observe that FOO=bar makes it to the java program but SPAM=eggs does not
    Now re-run the script again but this time add set -a to the script

    #!/bin/bash

    set -a

    SPAM=eggs

    java JavaEnvironmentVariables

  6. Verify

    $ ./my_script.sh | grep SPAM

    SPAM=eggs

Posted in Software | Leave a comment