Showing posts with label tech. Show all posts
Showing posts with label tech. Show all posts

Sunday, January 01, 2012

10 Wishes for a Digital 2012


Q: What's better than kickstarting a new year with a new post?
A: Kickstarting with a list post!

Here are 10 wishes for the digital space in the year 2012:
  1. May we find something better than advertising to monetize content on the Internet. (Flattr?). That, or, may ads become less annoying and unobtrusive.
  2. May everyone and their uncle STOP making mobile apps, because, it's the in-thing, you know. Mobile optimized websites will work as well, thank you.
  3. May we fix the mobile payment ecosystem [1].
  4. May e-Commerce websites stop bleeding cash.
  5. May daily deal sites graduate from hair/tattoo/nail-art/massage spas... and NOT get into the regular e-Commerce category.
  6. May we get a payment aggregator that doesn't suck. Serious, the bar is that low! (Zaakpay?)
  7. May we get a some digital marketing agencies that don't suck. Again, the bar is that low!
  8. May Google stop screwing advertisers and users over. Seriously, how many 'organic search results' do you see these days? [2]
  9. May telecom operators & TRAI figure out a way to stop SMS spam that actually works. Without crippling the SMS system completely.
  10. And finally, may we preserve and extend freedom on the Internet. And not get snooped/monitored in everything we do online. [3]
Amen.

[1] It's very easy to bring up-to-speed with the desktop Internet ecosystem. Hint: Mobile optimized 3D-Secure & Netbanking. At least http://m.hdfcbank.com seems to be moving in the right direction

[2] Tell me where to stop: hiding referrer information for free/organic traffic, but NOT paid traffic. Ad sitelinks & umpteen other 'ad extensions'. Flight meta-search widget. Hotel meta-search widget. Stuffing hotel related searches with place results. Media ads

[3] SOPA. Protect-IP. Internet censorship (or 'pre-screening'). Sec 69 of Indian IT Act 2008.

Tuesday, December 27, 2011

Android fragmentation? Don't get me started!

I've been playing around with Android development lately and it's been a fairly "interesting" ride so far. Well, apart from the horror that programming in Java is (I'll leave that rant for a separate post).

One extremely frustrating facet of designing an app for the Android platform is the plethora of devices with varying screen sizes out there. Coming from a web background, where you pick a minimum screen resolution and get going, it's a rude shock. Android's own guide on supporting multiple screens recommends having four (!) versions of bitmap resources (pictures).

I did a quick survey on Flipkart for the screen sizes & resolutions of all Android phones under Rs 18,000, which I'm assuming would be the top sellers in India. Here's the data (the first column is the screen resolution in pixels, the second column is the diagonal length of the screen in inches, and the third column is the number of handsets available in that particular combination):


So, while designing for Android, you have the mental overhead of thinking about more than 12 screen size & resolution combinations. That's right -- more than twelve. Contrast that with iPhone, where it's just two, I think -- one for normal displays and one for retina displays. Contrast that with the web, where, till some time back, you had to basically pick between designing for a minimum of 800x600 or 1024x768.

Based on the screen classification proposed by Android, even if this number can be brought do half, that would leave us with more than 6 possible outcomes! I haven't even started looking at supporting multiple API versions (2.2, 2.3, 3.0, 4.0, etc.) and multiple device capabilities (scroll wheel, D-pad, etc. etc.) I'm afraid I'll give-up even before I reach that point!

Is there any report out there which segments: (a) the amount spent on the Android market &  (b) the in-app advertising revenue, by the handset capability / screen size / etc? I'm sure app developers would be willing to pay for it, to save themselves the time spent on handsets generating a few hundred rupees, at best.

Wednesday, June 08, 2011

Google +1 vs Google Reader "Like" - Competing features?

  • Google Reader had the "like" feature since July, 2009. Why didn't they release a widget for blogs to showcase the like count on each post? This widget would've also given  a way to users not on Google Reader to showcase their appreciation for a blog post.
  • When Google finally released +1 they didn't integrate it with the Google Reader like count. So, now there are multiple ways for users on the Google ecosystem to like/recommend a piece of content. Why?
Am I missing something obvious here? What do you think?
 

Friday, March 25, 2011

3 Reasons Why You Need Test Driven Development

Having been through the initial phase of a startup, I've been responsible for fairly large projects on impossible timelines. With ever-changing specs and the pressure to ship, we never really got the time to write any automated tests.

It's not something that I'm proud of.

I took the first opportunity to experiment with "Test Driven Development" (TDD) when the support contract with a vendor ran out and I had to fix a bug myself. Based on this experience here's why I think every project needs test driven development:

Thursday, July 30, 2009

How to make your Hive cluster blazingly fast

Squeeze it! Literally.

Always, always, always - compress your tables. Most of your Hive query execution time is not going into doing complex computations. Aggregates, averages, and counts across tons of weblog data are not exactly computationally complex. It's the reading & writing of tons of data that's taking time.

In computer science parlance, it's not CPU bound, it's I/O bound.

If your data is already compressed (which in all probability, is the case), then nothing can beat the speed at which you can import it into Hive. Simply use the LOAD DATA LOCAL command to load compressed files into a TextFile table. Hive will automatically detect the compression and decompress on-the-fly when executing queries.

Leaving aside the data import times, here are some (unscientific) benchmarks for a SELECT COUNT(1) query on my raw data tables. From 106 sec down to 60 sec. At 10% of the storage space.

StorageRow countTable sizeQuery time
Uncompressed8,259,7207,686 MB106 sec
Compressed – GzipCodec (RECORD)8,259,7204,773 MB101 sec
Compressed – native gzip8,259,720736 MB60 sec


Now, each of these tables was put through a moderately complex Hive query [1], which processed the data using two custom map/reduce scripts, and inserted the procssed data into a new table. Sure, the initial ETL phase is not significantly faster, but look at the amount of space I'm saving.

StorageResultant row countResultant table sizeQuery time
Uncompressed1,561,6331,608 MB699 sec
Compressed – GzipCodec (RECORD)1,561,633N/A [2]563 sec
Compressed – native gzip1,561,63386 MB510 sec


I'll try to run some complex queries on the table with processed data to compared between compressed and uncompressed storage.

Learn how to compress your Hive tables at Hive Wiki – Compressed Storage

[1] Take a look at the section titled "Processing the raw data (ETL)" here
[2] I forgot to record the table size before dropping the table :-)

Friday, July 24, 2009

Using Hive for weblog analysis

Introduction


I've been playing around with Hadoop since the last fortnight to see how it performs with our weblog data processing jobs (Apache access logs). Right now we're using a blink-and-it-breaks system running a bunch of custom Perl scripts for log processing and MySQL for data storage. It's running on a single server class machine. It takes it about 3-4 hours to extract, transform, and load (into MySQL) one day of weblogs (this includes identifying sessions based on a 30min timeout). Another 1-2 hours to aggregate data and generate various CSV reports.

So, a total of 6-7 hours, when it's not crashing.

With Hadoop + Hive I've brought the first phase down from 3-4 hours to under an hour! Anywhere between 15 minutes to 45 minutes to copy log files into a raw Hive table (depending upon how much replication you've configured your cluster to have). After that, 12 minutes to extract, transform, and load into a new table (including session identification).

I gave Cloudbase a shot before Hive. Cloudbase was *dead* easy to setup and get off the ground (I love their simple approach to UDFs & UDTs). However, it's performance was not as good as Hive. On the other hand, I spent quite a lot of time trying to even import my custom log formats into Hive (I'm looking at you GenericUDF). But once I got started, man, was I blown away! Take a look at my Twitter updates on Cloudbase and Hive comparisons.

Cluster setup: I managed to corner 4 desktop machines in the office for my experimental cluster. They are Core-2 Duo 2.4 GHz machines with 1GB of RAM and standard SATA hard disks. All four are connected to each other using a switch.

Setting up the Hive tables


Here's how I've set up my Hive tables:

The following table (raw) stores the raw, unprocessed log files. I've partitioned the table by date so that the ETL operations can be run on bite sized chunks consisting of a day's worth of data.
create table raw(line string) partitioned by(dt string)
row format delimited fields terminated by '\t' lines terminated by '\n';


The following table (hits) contains the processed & filtered hits, partitioned by date. Within each partition the rows are clustered by Apache user-id (aid) and within each cluster they are again sorted by page view times (ts). According to Hive Wiki this clustering & sorting can improve efficiency in certain queries. Although it does lead to an increase during the ETL phase. I'm really not sure what the buckets do - and 1,000 is just a number I pulled out of thin air.
create table streamed_hits(ip_address string, aid string, uid string,
ts string, method string, uri string,
response string, session_id string,
session_start string, pv_number int,
clickstream string)
partitioned by(dt string)
clustered by (aid) sorted by (aid, ts) into 1000 buckets
row format delimited fields terminated by '\t' lines terminated by '\n';


Importing the raw data


Here's how one import data into the raw table. The import process is a simple HDFS file copy operation[1]. The more replication you have, the larger number of nodes the data needs to be copied to. Hence, the more time it takes during this operation.
load data local inpath '/weblogs/20090602-access.log'
into table raw partition(dt='2009-06-02');

load data local inpath '/weblogs/20090603-access.log'
into table raw partition(dt='2009-06-03');


Processing the raw data (ETL)


Here's the Hive query for the ETL operation. I'm making sure that uninteresting rows are discarded upfront using the WHERE clause in the inner query. I had earlier put this in the outer query (which was causing LOTS of uninteresting rows to go through the parsing logic). The (apparently, in hindsight) stupid placement of the WHERE clause was bumping up the completion time of this query from 15mins to 2+ hours!

I'm using a custom map script to read my log files, which are in a 'non-standard' format. There is another way to do this - UDFs & SerDe - but according to the discussion I had on the mailing list, these features are not fit for newbie consumption yet. The DISTRUBUTE BY & SORT BY is essential for my session & clickstream identification script to work (thanks to the Hive Wiki).
from
(from raw
select transform line using 'parse_logs.pl' as ip_address, aid, uid, ts, method, uri,
response, referer, user_agent, cookies, ptime
where lower(line) rlike '^(\\S+) (\\S+) (\\S+) \\[(.*?)\\] "(.*?)" (\\d+) (\\d+|-) "(.*?)" ".*?(mozilla|msie|opera).*?".*'
and not line rlike '^(\\S+) (\\S+) (\\S+) \\[(.*?)\\] "(\\S+) (/images.*?|/styles.*?|/javascripts.*?|/adserver.*?|.*?favicon.*?) (\\S+)".*'
and dt='2009-06-30'
distribute by aid sort by aid, ts asc) parsed
insert overwrite table streamed_hits partition(dt='2009-06-30')
select transform parsed.ip_address, parsed.aid, parsed.uid, parsed.ts, parsed.method, parsed.uri,
parsed.response, parsed.referer, parsed.user_agent, parsed.cookies, parsed.ptime
using 'identify_sessions_and_clickstream.pl' as ip_address, aid, uid, ts, method, uri,
response, session_id, session_start, pv_number, clickstream;


Custom map/reduce scripts used


Here's the parse_logs.pl Perl script. Please excuse my Perl - I'm not a Perl programmer. I was just trying to reuse the funky regular expressions that we had lying around in our current log processing system. You use plug in any script, that reads/writes from standard input/output, into Hive.

#!/usr/bin/perl

my %monthNum=(
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
);

while (defined($line = )) {
if (($host,$user,$apache,$rfc931,$method, $url, $ver, $status,$size,$referrer,$agent,$cookies,$ptime) = $line =~ m/^(\S+) (\S+) (\S+) \[(\S+ \S+)\] "(\S+) (.*?) HTTP\/([0-9\.]*)" (\d+) (\d+|-) "([^"]*)" "([^"]*)" "([^"]*)" (\d+|-)$/) {
# everything found -- nothing to be done
}elsif (($host,$user,$apache,$rfc931, $method, $url, $ver, $status,$size,$referrer,$agent,$cookies) = $line =~ m/^(\S+) (\S+) (\S+) \[(\S+ \S+)\] "(\S+) (.*?) HTTP\/([0-9\.]*)" (\d+) (\d+|-) "([^"]*)" "([^"]*)" "([^"]*)"$/) {
$ptime="";
}

$ts="";
# Converting date to yyyy-MM-dd hh:mm:ss
if (($day, $monthname, $year, $hour, $minute, $sec)= $rfc931 =~/^(\d{2})[\/-]([^\/-]+)[\/-](\d{4}):(\d{2}):(\d{2}):(\d{2})/) {
$month=$monthNum{$monthname};
$ts=sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $minute, $sec);
}


$agent=lc($agent);
$user=lc($user);
print "$host\t$apache\t$user\t$ts\t$method\t$url\t$status\t$referrer\t$agent\t$cookies\t$ptime\n"
}


Here's the identify_sessions_and_clickstream.pl Perl script. Again, same disclaimer about my Perl as above. Also, please excuse the extremely naive way in which I'm trying to construct the clickstream. That work in progress.

This script depends on the fact that the incoming data is sorted on Apache user-id and timestamp (that's what the DISTRIBUTE BY & SORT BY in the Hive query achieve). Apart from identifying 60min sessions I'm cleaning up the URLs to make them more 'generic' (removing query strings, removing trip IDs etc.)
#!/usr/bin/perl

use Date::Parse;

$session_duration=60*60; # in seconds
$prev_apache=undef;
$prev_ts=undef;
$pv_number=undef;
$session_id=undef;
$session_num=undef;
$session_start=undef;
$clickstream='';

while (defined($line = )) {
chomp($line);
($ip_address, $apache, $user, $ts, $method, $url, $status, $referrer, $agent, $cookies, $ptime) = split(/\t/, $line);
$url, $status, $referrer, $agent, $cookies, $ptime\n";
$url =~ s/(.*?)\?.*/$1/i;
$url =~ s/^\/(trains|flights)\/itinerary\/.*?\/(.*?)/\/$1\/itinerary\/itinerary-id\/$2/;
$url =~ s/^\/(trains|flights)\/itinerary\/(\d+)$/\/$1\/itinerary\/itinerary-id/;
$url =~ s/^\/(activate|reactivate|reset)\/.*/\/$1/;
$url =~ s/^\/share.*/\/share/;
$url =~ s/^\/account\/trips\/.*/\/account\/trips\/trip-id/;
$url =~ s/^\/trains\/stations\/[0-9]*/\/trains\/stations\/numeric-id/;
$url =~ s/^\/trains\/stations\/[A-Za-z]*/\/trains\/stations\/alphanumeric-id/;
$url =~ s/^\/hotels\/info\/.*/\/hotels\/info\/hotel-id/;
$url =~ s/^\/places\/hotels\/.*\/images.*/\/places\/hotels\/images/;
$url =~ s/^\/places\/hotels\/images.*/\/places\/hotels\/images/;
$url =~ s/^\/newsletters\/images\/.*/\/newsletters\/images/;
$url =~ s/^\/index.*/\//;
$url =~ s/\/$//; # remove trailing slashes

if(!defined($prev_apache) || $prev_apache ne $apache){
if($apache eq '-' or $apache eq '') {
# TODO -- use IP address & user_agent to identify sessions?
$pv_number='';
$session_start='';
$session_id='';
$clickstream='';
} else {
$pv_number=1;
$session_start=$ts;
$session_num=1;
$session_id="$apache|$session_start";
$clickstream="$url"
}
} elsif($prev_apache eq $apache) {
if((str2time($ts)-str2time($prev_ts))<=$session_duration) { $pv_number=$pv_number+1; if($pv_number<70) clickstream="$clickstream|$url" pv_number="1;" session_num="$session_num+1;" session_id="$apache|$session_start" clickstream="$url" prev_apache="$apache;" prev_ts="$ts;">



[1] A small warning here, if your Hive session is running on one of your active nodes. Beware that because of HDFS default policy your Hive node's disk is going to be filled up first. This might lead to a disk space problem if your HDFS partition is being used by other processes as well. I ran into this and had to run the HDFS balancer, which, btw, is extremely slow! (even after increasing the dfs.balance.bandwidthPerSec property).

Thursday, July 02, 2009

A SIM-card along-with the National ID card?

Taking on from Sanjay Swamy's post at Medianama, I think it's an excellent idea. Sure, there are the rough edges that one needs to smoothen out, but that's the case with any scheme of this magnitude. The kind of opportunities that open if your national ID can be part of your mobile SIM card are amazing. Especially in the mobile banking and m-commerce area.

Picture this: Your bank and telecom provider, both as part of their KYC, will need to know your national ID. Now, if you have the ability to link-up your SIM with your national ID (both smart cards), you can perform a fully authenticated financial transaction through your mobile phone. Just think of the number of services that can be made available to the rural population through such a thing!

The biggest challenge this idea will face is from people getting paranoid about
(a)a telecom operator "owning their identity", and
(b) not everyone having a mobile phone.

The National ID should be a complete in itself even without a SIM or a mobile phone. Something like the new vehicle registration smart cards you get in Maharashtra nowadays. However, it should have a detachable SIM card built right into it. If you want to use the additional features of your National ID, just remove the SIM and plug it in. You can keep your current phone number and even switch operators -- thanks to Number Portability.

The telecom operator (or anyone else on the mobile network) will NOT have access to your identity unless you authorize them during a transaction which requires such an access. It's just that if the SIM and your National ID live on the same hardware (which is, the smart card) you can link them up with ease, as and when required!


Thursday, October 30, 2008

Email archival without mailing list software?

I believe that the lack of archives in business communication is a serious enough problem to solve. Especially in these times, when a lot of brainstorming happens on email amongst team members located in different geographies.

I've tried using Basecamp for all business communication and succeeded only partially. Although Basecamp can serve as a good archive for email communication, it just doesn't beat the simplicity of adding any random email address to the CC list when you hit "Reply all". You have to first add a user to a project, then add him to the message, and then reply to the message. This, of course, will work only if the person knows how to use Basecamp. Otherwise, add the additional overhead of evangelizing Basecamp over regular email before getting him to accept this new mode of communication. [1] If something confidential is being discussed, there's also this fear of archiving your trade secrets on someone else's servers.

Probably there's a good use-case to add a new feature in SMTP servers -- an archive header and an email archiving module. If an incoming email has the archive header (X-Archive: true) the SMTP server will archive the email by storing a copy of that email and making it accessible on a web page. All replies to that email (and further replies to those emails) will automatically get archived, regardless of whether they have the archive header or not [2]. The server can also add a footer to each email in the thread to direct the user to the archive. Optionally, the archiving module may also archive email attachments and make them available on the company intranet (or the web).

Various mail clients will also need to be modified to present a user with a checkbox while composing the mail that sets/unsets the archive header.

I think this feature can be easily hacked into postfix (or sendmail) and a couple of open source email clients (like Evolution and Thunderbird).

Alternatively, this can also be achieved by having a record-keeper email ID (something like archive@somecompany.com). Any email CCed to the record-keeper will be archived and available on the company intranet (or the web). The only drawback of this is that the archival chain can break very easily if someone removes the record-keeper from the CC list. This approach can probably be an easier path towards implementation, both from the client's as well as the server's side. (Aren't such archival modules already available?)

If anyone wants to collaborate on this project with me, please drop me an email at saurabhnanda (at) gmail (dot) com. I've been wanting to work on a decent tech project for quite some time now.

[1] Given, recipients of Basecamp messages can now simply reply to messages just like normal email, it's still not perfect. All formatting (even line-breaks) are stripped. And you still have to log on to Basecamp to compose a completely new message.

[2] I think it's reasonable to assume that since the parent wanted the email thread to be archived, it must be archived in its entirety.

In Support of "Top Posting"

All over the web, on mailing lists, especially in the open source geek circles [1], top posting is frowned upon. Even while bottom posting, you're not considered cultured if you don't truncate the parent post to the exact specific part(s) your reply pertains to. I, too, used to follow this religiously until recently.

After three years of communicating with non-geeks (you know, the business and marketing types), I realized that a lot of business communication was simply not possible in the typical quote-reply-quote-reply format that is prevelant on mailing lists. This is because of a couple of reasons, which I'll try to enumerate in the following paragraphs.

One of the reasons given for not including the parent post in its entirety is that anyone wanting to refer to the thread can always look up the mailing list archives. Cultured folk should not waste bandwidth and screen real estate by repeating what is available a click away. This is not the case with typical business communication, which starts between two people (with usually their bosses on the CC list -- to be "kept in the loop"). Along the course of a reasonably sized email discussion, the CC list grows exponentially (believe me)! First the bosses' bosses are added -- if the small fry realize that they'll need to save their ass if something goes wrong. Suddenly something is under the purview of a chappy in some other department and him and his boss and boss' boss are added to the list. Later someone realizes that the legal department also needs to be "kept in the loop". Within no time you're having an orgy on email! With each new addition to the CC list you need a place where the latecomers can read-up on the discussion till then. In normal business communication there are no "archives". The latest email, itself, serves as an archive. I've had a tough time catching up on the discussion where each email was regularly truncated by the participants. I had to piece together more than 25 different emails forwarded to me as the "archive" -- believe me, it wasn't an easy task. Therefore it's much better to preserve the entire thread in the body and add your reply to the top of the email.

A side-effect of not having an archive is that you can't really follow the quote-reply-quote-reply pattern ("Comments inline...") very effectively. If five different people are communicating, you suddenly lose track of (a) who said what, (b) in reply to what, and (c) in reply to whom. With an archive, each post can easily be identified on all three parameters. Take away the archives, and within two or three replies you lose track of (a) and (c) very easily. Inline comments, even in business communication, are useful when each paragraph or point in your email can be answered independent of the others. For example, when you're seeking answers to a list of technical questions. However as soon as you want to reply to the reply of your questions you start losing track of (a) and (c). One decent solution I've noticed is prefixing your reply with your name in square brackets. So, a typical email after a couple of back and forth replies looks like:
* Can I pass the customer's shipping address instead of the billing address in the API?
[Alice] It's possible, but why would you want to do it? You already have the billing address in the DB, don't you?
[Bob] Yes, we do -- but we don't want to share it due to privacy concerns.
[Alice] Okay, then you can pass the shipping address.
I've observed a significant side effect of following the quote-reply-quote-reply model of communication -- especially on tech mailing lists. A lot of discussions regularly morph into flame-wars, mudslinging matches, or "preachers preaching the lesser mortals." Don't get me wrong -- the communication style is not the sole reason for discussions going off track. Obviously a lot depends on the attitude of the people involved in the discussion, but I feel the communication style definitely promotes the degeneration of the discussion. When you read a post you generally disagree with, it's very tempting to pick each line, quote it, and try to rebut it individually. It's very easy to get lost in the trees for the woods.

[1] These are the only circles which seem to have a respectable mailing list culture.

Wednesday, October 15, 2008

Overestimating users of technology

Most software products assume too much about their users. After reading Alan Cooper's "The Inmates Are Running The Asylum" (at Amazon and at IndiaPlaza) I realized how much I used to presume about a user's comfort level with technology while designing products. I now consciously switch off the developer/geek side of my brain while writing product specs or mocking up UI prototypes.

Here's a post by David Pogue on what everyone assumes that everyone else knows but is wrong. I thought I would know every single thing on the list but was surprised to find a keyboard shortcut that was new to me - using shift + space to scroll up in a web page.
You can tap the Space bar to scroll down on a Web page one screenful. Add the Shift key to scroll back up.
Yahoo! recently released their research findings about an OpenID usability report. They were not surprising:
None of the users had heard of OpenID before, and none of them even noticed the OpenID sign-in box displayed below the traditional email/password login form on the site. [...] Observing these tests was more than a bit frustrating for the Yahoo! OpenID team, and the test subjects may have been distracted by the sounds of the groans and head-pounding coming from the other side of the one-way mirror. Certainly there is a lot of work to be done on the OpenID UX (user experience) front.
(At the risk of over-simplification, OpenID is a common username/password using which you can login to multiple websites without creating individual username/passwords for each.)

Tuesday, September 30, 2008

Scary: CSRF and REST

The latest exploit to hit the web is CSRF. It's nothing fancy, very simple to execute and understand. In a nutshell:
  1. You're signed in to website A

  2. You open a new tab and visit website B. Website B is a malicious website (or is a trusted website which has the potential of being malicious because of allowing user generate content-UGC).

  3. Website B has an image tag (not necessarily, but the simplest to inject via UGC) which points to a predictable URL on website A. For example, <img src="http://www.a.com/account/delete?confirm=yes" /> which basically deletes your account on website A.

  4. Your browser will try to fetch the "image" from website A, merrily sending your session cookie along with the request. And BOOM! On website B you don't get the image ('cuz there is none) but your account has just been deleted from website A.

This is the simplest scenario I've explained. Read the Wikipedia article CSRF to understand how it can be executed in a variety of ways and situations.

Over the past year or so I've been going ga-ga over RESTful architecture and stateless servers. I've tried making URLs predictable and discoverable. Personally, I click on the "keep me signed in" checkbox whenever on browsing the web on my laptop. This just turns my world around!

Does it mean that we now need to make sure we're not signed-in to one site while visiting another? Or that all web applications now have to be stateful and URLs have to be non-predictable? Which means that while generating a page the webserver will have to add a random token to each URL and validate that when the browser requests the URL? The horror!

Any thoughts, anyone? This sounds like impending doom!

Saturday, April 19, 2008

Impulse Purchase

I've seen people purchase clothes on an impulse. Shoes, deodorants, shades, food, whatever. But I don't think I've seen anyone purchase a domain name on impulse!

A few days ago I was following a truck on my bike and saw the ubiquitous "Horn OK Please" written on the back. And suddenly out of nowhere I thought of "horny" please. What a cool domain name to have! I came to the office, and purchases it for Rs 599 at Net4Domains.

What the fuck do I do with it now? Any ideas?

Saurabh Nanda. Proud (confused?) owner of www.hornyplease.com!

Wednesday, April 09, 2008

Composing blog posts in Textile Markup

Why can't I compose posts in Textile Markup? It's so damn simple and guarantees conversion to clean HTML. I hate using the rich text editor for composing posts. It's too complicated and I don't like the superfluous markup it generates. Plus I've also gotten addicted to Textile, thanks to the amount of time I spend on Basecamp.

I've submitted a feature request and also posted about it on the Blogger Help Group.

Tuesday, April 08, 2008

Clueless with Computers...

... that's how non-geeks feel. I'm extremely sure of that now.

I had been spending some time planning my parents' trip to Kerala. I had every event (flight, car travel, hotel stay, etc.) added neatly to my Evolution calendar. (Given the number of meetings I now have to attend - I've been using Evolution avidly for the past month or so - but that's a different story). Finally, I sent out notifications for each event to my parents.

I called up my Mom expecting the entire travel-plan communication process to be a breeze. What's the complication, I thought. You get a bunch of meeting invites; you click "Accept" for each one and boom! you have a proper travel calendar right in front of you.

She got the mails alright, she even clicked "Accept" on each one of them. But I had one hard of a time trying to get her to the calendar.

Me: Okay, now that you've accepted them, you can see the schedule in the calendar.
Mom: How do I get to the calendar?
Me: Okay, do you use Outlook? [That's what most offices use.]
Mom: Yes.
Me: So, the left pane, sorry column, sorry left side of the screen will have a button for calendar. Click on it.
Mom: It's not there.
Me: Can you see your mail folders?
Mom: Yes. But no calendar.
Me: Okay, I'll call you back in 5 min.

Then I rushed to a guy with Microsoft Windows on his machine. Fired up Outlook and noted the exact location of the Calendar button. It was pretty visible. But then I realized, probably it's been turned off on her machine. I asked the guy with M$ installed, he showed me how to configure your left pane to show/hide various buttons. I was like, yeah right. "Mom look at the left pane, there's a teeny-weeny arrow there which will lead to a menu, which will have an option called 'configure', which will have various checkboxes...." Not happening!

I spent the next 10 mins trying to figure out how to get to the calendar in Outlook without clicking on an onscreen button or going through the configuration process. It's bloody un-intuitive. It's "Go > Calendar" if you don't already know. The shortcut for it is Ctrl+2.

Back to the phone:

Me: Okay, press Ctrl+2.
Mom: [After 30 sec] I did, nothing happened.
Me: Are you in Outlook? [Damn!]
Mom: Yes.
Me: Do you have a Menu item called "Go"? You know, right at the top where file, edit, etc. are? Can you see a "Go" there?
Mom: No, there's no "Go" option!
Me: Are you sure you're using Outlook?
Mom: Yes.
Me: Are you sure it's not Netscape?
Mom: [getting irritated] yes!
Me: okay, are you using Outlook on your own desktop? Or do you sign in to Outlook using the Internet Explorer? [Ah, probably she was on one of the M$ web mail box thingies]
Mom: No. It's on my desktop. We have two different icons. One for Internet Explorer and the other for Outlook Express. [!!]

I wanted to pull my hair out! And as it turn out, Outlook Express, indeed does not have a calendar application.

I just gave up on the entire cool iCal stuff. Took a screenshot of my Evolution Calendar screen and mailed it to her.

Sunday, March 09, 2008

How to reset an Oracle sequence at midnight everyday

Oracle, unlike MySQL, does not auto-generate values for the primary key column for you. That means, you can't "auto_increment" a column in Oracle. With each INSERT statement you have to provide the value for the primary key column yourself. Oracle has database sequences to help you generate series of numbers for this purpose.

Sometimes you end up using Oracle sequences for stuff other than as a source of values for your primary key. And sometimes you need to reset the value of the sequence back to 1 at midnight everyday. I ended up with the same problem and I couldn't find a canned solution off the Net for this. I spent a day trying to solve this and ended up learning a lot about Oracle sequences, Oracle packages and PL/SQL, the dbms_lock package, and the dbms_scheduler package.

The problem would be fairly easy to solve if resetting an Oracle sequence were a single atomic SQL statement. No, it turns out that you have to do a bit of circus involving multiple statements to reset the sequence back to 1. I figured that you basically have two options:

  1. Drop the sequence (statement #1) and recreate the sequence (statement #2)

  2. Change the INCREMENT BY property of the sequence to be negative of the CURRVAL (statement #1). Increment the seqeunce (statment #2). Reset the INCREMENT BY property back to 1 (statement #3).


In both the solutions, you run the risk of some other process coming along and doing a seq.nextval on the sequence while the reset is underway. Boom! A bit like getting caught with your pants down.

So, here's what I did:

  1. Create a PL/SQL package in Oracle which acts as a proxy for the sequence. Make sure that no application module does a seq.nextval directly. All requests to get the nextval have to go through the getNextSeq() function/procedure provided by the PL/SQL pacakge.

  2. The getNextSeq() function first acquires a lock in shared mode provided by the dbms_lock package in Oracle. Increments the sequence. Releases the lock, and returns the nextval. (Note: The dbms_lock package is a general purpose lock library. It does not mean that you acquired a lock on the sequence - that's not possible in Oracle, as some gurus told me. You just acquire a lock. Very much like a mutex lock you acquire in programming languages.)

  3. In the same PL/SQL package, you create a function resetSeq() which acquires the same lock in exclusive mode, resets the sequence using either of the solutions given above, and releases the lock.

  4. Now, you can create a scheduled job in the DB using the dbms_scheduler package to run the resetSeq() function at midnight everyday. In fact, you can execute that at whatever frequency/interval you want.


In the process I also realised that Oracle has the capability of doing a fair amount of metaprogramming through the EXECUTE IMMEDIATE statement. You can pass it any string and Oracle tries to parse + execute that as an Oracle statement, much like eval in Ruby or Lisp.

I spent most of my time breaking my head over why dbms_lock.request didn't seem to be working. Then I realized that dmbs_lock.allocate_unique worked on a per session basis. What that means is, if two separate connections called dmbs_lock.allocate_unique to create a lock with the same lock handle, both of them would end up getting lock objects that would behave independently of each other. In which case, effectively, both the processes were working on different locks and the whole purpose of using locks would be defeated. This is why, I'm calling allocate_unique just once, and "hard coding" the lock id in the package definition using Oracle "metaprogramming".

PS: I'm still not sure whether this is the best solution to this problem, but it's a solution that works. Please let me know if you have a better way of solving this.

Here's the script:

set serveroutput on;
declare
lock_handle number;
begin
dbms_lock.allocate_unique('my_lock', lock_handle, 31536000);
dbms_output.put_line('Dropping my_seq');
execute immediate 'drop sequence my_seq';

dbms_output.put_line('Creating my_seq');
execute immediate 'create sequence my_seq minvalue 1 nocache increment by 1';

dbms_output.put_line('Creating my_pkg declaration');
execute immediate 'CREATE OR REPLACE PACKAGE "my_PKG" AS
procedure reset_my_seq;
function get_my_next_seq return number;
END;';

dbms_output.put_line('Creating my_pkg definition');
execute immediate 'CREATE OR REPLACE PACKAGE BODY "my_PKG" AS
my_lock_handle number := ' || lock_handle || ';

procedure reset_my_seq is
lock_status integer;
curr_val integer;
begin
lock_status := dbms_lock.REQUEST(lockhandle => my_lock_handle, lockmode => dbms_lock.x_mode);

execute immediate ''alter sequence my_seq minvalue 0'';
execute immediate ''select my_seq.nextval from dual'' into curr_val ;
execute immediate ''alter sequence my_seq increment by -'' || curr_val ;
execute immediate ''select my_seq.nextval from dual'' into curr_val ;
execute immediate ''alter sequence my_seq increment by 1'';

lock_status := dbms_lock.release(lockhandle => my_lock_handle);
end;


function get_my_next_seq return number is
lock_status integer;
next_seq number;
begin
lock_status := dbms_lock.REQUEST(lockhandle => my_lock_handle, lockmode => dbms_lock.s_mode);
select my_seq.nextval into next_seq from dual;
lock_status := dbms_lock.release(lockhandle => my_lock_handle);
return next_seq;
end;
END;';


-- dbms_output.put_line('Dropping scheduler job');
-- dbms_scheduler.drop_job('reset_my_seq');

dbms_output.put_line('Creating scheduler job');
dbms_scheduler.create_job( job_name=> 'reset_my_seq',
job_type=>'PLSQL_BLOCK',
job_action=> 'begin my_pkg.reset_my_seq; end;',
start_date=> systimestamp,
repeat_interval=>'frequency=daily;interval=1;byhour=0; byminute=0; bysecond=0;',
enabled=>true);
commit;
end;
/

Thursday, February 07, 2008

FOSSKriti at Techkriti 2008 -- The IIT Kanpur Technical Festival

Cleartrip is supporting FOSSKriti – a 4 day mini-conference about Free/Open Source Software. It'll have technical talks, hands-on workshops, hackfests, and general geek chatter.

In an RoR workshop I'll be creating the OARS (Online Academic Registration System) website in 90 minutes – or something that can at least be called the OARS website. My friend Chaitanya will be giving a talk on Common Lisp.

If you're somewhere around, be there – it'll be fun.

FOSSKriti

Trains search by Cleartrip

We've just launched a new product – Cleartrip Trains Search It's simple. It's fast. It's comprehensive. And it rocks! Give it a spin and let us know at Cleartrip Forums

A lot of hard work put in by Piyush, Anurag, of course our rocking UI team (Hrush, Varun, Rakesh, et al).

Tuesday, June 12, 2007

Cybersquatting in India

Spicy IP informs us that the domain gmail.co.in was squatted over by a fellow named "Chen Zhaoyang". Google ultimately got it back after filing a complaint with the "National Internet Exchange of India" (NIXI)
I never knew something like that even existed. Apparently the NIXI has a Wikipedia page as well.

Google seems to have won the arbitration but it doesn't look like they've got control of the domain yet.


Surprisingly, a WHOIS query on gmail.co.in thre up the name of "Sukumaran Pillai" based in Alabama!
Domain ID:D2507759-AFIN
Domain Name:GMAIL.CO.IN
Created On:03-May-2007 20:02:52 UTC
Last Updated On:08-May-2007 15:10:14 UTC
Expiration Date:03-May-2008 20:02:52 UTC
Sponsoring Registrar:Direct Information Pvt. Ltd. dba PublicDomainRegistry.com (R5-AFIN)
Status:CLIENT TRANSFER PROHIBITED
Status:TRANSFER PROHIBITED
Registrant ID:DI_6367931
Registrant Name:Sukumaran Pillai
Registrant Organization:N/A
Registrant Street1:978 Henderson Rd NW
Registrant City:Auburn
Registrant State/Province:Alabama
Registrant Postal Code:36832
Registrant Country:US
Registrant Phone:+1.3170939374
Registrant Email:skumaranp@yahoo.com
Admin ID:DI_6367931
Admin Name:Sukumaran Pillai
Admin Organization:N/A
Admin Street1:978 Henderson Rd NW
Admin City:Auburn
Admin State/Province:Alabama
Admin Postal Code:36832
Admin Country:US
Admin Phone:+1.3170939374
Admin Email:skumaranp@yahoo.com
Tech ID:DI_6367931
Tech Name:Sukumaran Pillai
Tech Organization:N/A
Tech Street1:978 Henderson Rd NW
Tech City:Auburn
Tech State/Province:Alabama
Tech Postal Code:36832
Tech Country:US
Tech Phone:+1.3170939374
Tech Email:skumaranp@yahoo.com
Name Server:NS1.SEDOPARKING.COM
Name Server:NS2.SEDOPARKING.COM
Incidentally microsoft.co.in has also been squatted! Let's see what happens here.
Domain ID:D490943-AFIN
Domain Name:MICROSOFT.CO.IN
Created On:16-Feb-2005 06:40:20 UTC
Last Updated On:02-Aug-2006 09:31:44 UTC
Expiration Date:16-Feb-2008 06:40:20 UTC
Sponsoring Registrar:Direct Information Pvt. Ltd. dba PublicDomainRegistry.com (R5-AFIN)
Status:CLIENT DELETE PROHIBITED
Status:CLIENT RENEW PROHIBITED
Status:CLIENT TRANSFER PROHIBITED
Status:CLIENT UPDATE PROHIBITED
Registrant ID:DI_1138221
Registrant Name:Piyush Somani
Registrant Organization:eTechSupport.net
Registrant Street1:303, Rohan Heights, Model Colony
Registrant City:Nashik
Registrant State/Province:Maharashtra
Registrant Postal Code:IN
Registrant Country:IN
Registrant Phone:+91.9850552225
Registrant Phone Ext.:91
Registrant Email:piyush@etechsupport.net
Admin ID:DI_1138221
Admin Name:Piyush Somani
Admin Organization:eTechSupport.net
Admin Street1:303, Rohan Heights, Model Colony
Admin City:Nashik
Admin State/Province:Maharashtra
Admin Postal Code:IN
Admin Country:IN
Admin Phone:+91.9850552225
Admin Phone Ext.:91
Admin Email:piyush@etechsupport.net
Tech ID:DI_1138221
Tech Name:Piyush Somani
Tech Organization:eTechSupport.net
Tech Street1:303, Rohan Heights, Model Colony
Tech City:Nashik
Tech State/Province:Maharashtra
Tech Postal Code:IN
Tech Country:IN
Tech Phone:+91.9850552225
Tech Phone Ext.:91
Tech Email:piyush@etechsupport.net
Name Server:NS1.NETTOOLZ.NET
Name Server:NS2.NETTOOLZ.NET