Skip to main content

Programming Epiphany

I hate doing tasks that are repetitive or that someone else should do but its a waste of my time. Earlier we used to store user and customer data on LDAP and there were all sort of BS requests from marketing like tell me "all customers that are on PlanY and are buy domain with >5 users".  Problem is there were 40 ldaps and each one of these would require writing a custom script. Teaching other programmers ldap was not reasonable.  One of my goals at my employer is to "Empower people to retrieve information themselves". I don’t want to be bottleneck in their chain of thoughts and they need not be bottleneck in what I want to do. I don’t want to involve humans if at all possible.

So when we migrated to Ldap->Mysql, first thing I did was consolidated 40 ldaps per data centre to 4 Mysql server per datacentre.  I could have consolidated to 1 also but that would suffer from noisy neighbour issue.  Problem is that there are 3 Data centres so in total 12 mysql servers.  Still people would come to me for data just because I architected Ldap2Mysql.  People need to understand that my job is to "Empower people by writing tools/frameworks so that they can do their job themselves".

Two weeks back I got a custom request where someone wanted all emails of all users in 11 thousand customer accounts. I was like its BS either I can give you all or I can give you none. It would waste 2-3 hours of my time if I had to do it. I even tried creating a SQL query with 11K domains in IN clause and   Mysql would run it by python program to query 12 Mysql servers would bomb due to command line argument limitation.  So I refused to do it and it was given to someone else in the team who would split the list into 1K domains and combine the CSV.  But then I realized the issue for me was not splitting the result into 1K domains and constructing IN clause as that I can do programatically or in excel in 10-20 min. Problem to me was that my program was generating output as python tuple format so who would combine the 11 log files and do all that data scrubbing to generate final CSV.

Today again the developer attached to internal apps team was asked to produce a list of all admin users of all customers who want to recieve our newsletter.  The developer sent email to me that I should do it. I was like wth its not my job, the marketing team should hire someone with SQL skills to do it and I can give him pointers on how to retrieve information.  Problem is that today one more developer whom I can delegate was on medical leave so the thing came back to me. I had to go to my son's school in 20 min so I was in time crunch. I thought I have everything, I have a program that given a template query would execute it on all Mysql databases and print it in tuple format and this is a common requirement that they want it in CSV so why not change program to do both.

within 5 min I changed code like this
        for row in pc_db_conn.execute(query):
            print row
        for row in pc_db_conn.execute(query):
            print row
            #I know this is BS and I should use csv module but this is internal shit
            csv_file.write(','.join(['"'+str(s)+'"' for s in row])) 

and tada the job was done. Now it seems for this kind of queries I eliminated the need for that developer. So I eliminated a human for one more task and this gave me a programming epiphany for the day.

I was whole day battling with newrelic, appdynamics and vivid cortex to find performance anomalies but this 5 min change gave me more joy than other things I found using those tools.

This blog post is dedicated to a fellow developer who once asked me how do I came up with all these ideas. So just trying to describe the thought process of eliminating myself from doing grunt work forces me to think these things.


Popular posts from this blog

RabbitMQ java clients for beginners

Here is a sample of a consumer and producer example for RabbitMQ. The steps are
Download ErlangDownload Rabbit MQ ServerDownload Rabbit MQ Java client jarsCompile and run the below two class and you are done.
This sample create a Durable Exchange, Queue and a Message. You will have to start the consumer first before you start the for the first time.

For more information on AMQP, Exchanges, Queues, read this excellent tutorial
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; import com.rabbitmq.client.*; public class RabbitMQProducer { public static void main(String []args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("guest"); factory.setPassword("guest"); factory.setVirtualHost("/"); factory.setHost(""); factory.setPort(5672); Conne…

Spring query timeout or transaction timeout

If you are using spring to manage transactions then you can specify default transaction timeout using

    <bean id="transactionManager"
        <property name="dataSource" ref="dataSource" />
        <property name="defaultTimeout" value="30" /> <!--30 sec--->             

or you can override the timeout in the annotation

    @Transactional(readOnly = false, timeout=30)

or if you are doing it programatic transactions then you can do

DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);

 or you can override the timeout for one particular transaction

TransactionTemplate transactionTemplate = new TransactionTemplate();

Python adding pid file

I have a thumbnail generator that launches multiple processes and the correct way to shut it down is to send kill -HUP to the parent process. To automate I had to write a pid file from python, it was a piece of cake
def writePidFile(): pid = str(os.getpid()) f = open('', 'w') f.write(pid) f.close()