Monitoring with God

Posted by Linus Mon, 26 May 2008 20:06:00 GMT

This week I discovered a great (though very poorly named) program called God. It is a ruby monitoring gem that, as the author describes, “is like monit, only awesome”. It actually is pretty awesome because it makes monitoring scripts SO much easier to write versus monit. Plus it’s all in ruby so we can use all the regular ruby shortcuts without learning how to do them in monit.

The only drawback however is that God doesn’t not support monitoring. If you read the message boards and the blogs out there you can see that this is a feature that many have requested. However since the purpose of God is to simply start/stop/restart processes, the author of the gem has neglated to build in this feature.

I however have a work around to this problem. What you do is make a transition condition that monitors when the process is up and transitions it to a restart. The KEY here is to make sure that your restart command does nothing. For me I just have it sleep 0 which is close to a noop. See the code snippet below:

God.watch do |w|

 w.name = “FOO”
 w.interval = 5.minutes
 w.start = “sudo /usr/bin/ruby FOO”
 w.stop = “sleep 0” # stop command - HACK
 w.restart = “sleep 0” # restart command - HACK
 w.pid_file = “/tmp/FOO.pid”

 w.start_grace = 5.minutes
 w.restart_grace = 5.minutes

 w.start_if do |start|
   start.condition(:process_running) do |c|
    c.running = false
   end
 end

# Hack: GOD doesn’t currently do monitoring. Therefore we need to run a
# restart command that doesn’t do anything. The w.restart_grace variable
# is the time in between checks, since after it does this transition, it
# will requery the state of the rails app
#
 w.transition(:up, :restart) do |on|
   on.condition(:memory_usage) do |c|
    c.above = 1.megabytes
   end
 end

Posted in  | no comments

Bay to Breakers

Posted by Jeff Wed, 21 May 2008 03:39:00 GMT

Have you ever been to a Bay to Breakers? This is the first year that I’ve gone, and I’ll for sure be there next year.

For those B2B Virgins out there - the closest thing that bears any resemblence is a gigantuous frat party - on the streets - for what seems to be the entire population of San Francisco.

If you plan to go next year, there is quite the check list before you go.

Parts of it that I’d give mad credits to:
A) Be in a large group
B) Have a cool theme (ours’ was Team Zissou)
C) Bring lots of beer - we brought kegs in shopping carts
D) Get there early –> it’s MADNESS!

The ‘race’ - this word is definitely not accurate at all … almost everyone is walking or crawling or napping along the way when they are not doing keg stands.

Surely, you’ve heard of people completely naked and running through the streets. Let me assure you that is certainly true. There’s also tons of people on the side of the streets squirting liquids of who-knows-what into the crowd.

So I didn’t actually finish the race - and neither did most of the crowd. I ended up at some park, ran out of booze, and split from the rest of the Zissous to mark the end of a great party.


no comments

Think Weekend

Posted by Jeff Tue, 13 May 2008 20:08:00 GMT

Just got back from my Think Weekend. It was fantastic!

What is a Think Weekend? Every 6 months, I reflect on what has happened in the past half year, set goals for the next couple of decades, and reshuffle short-term priorities to align with long term goals. This is something that was originally recommended to me by my graduate advisor David Patterson. Here’s rationale behind it: In normal everyday life, we have to make day-to-day decisions such as ‘Should I go work out?’, ‘Should I go to the movies with friends?’, ‘When am I going to leave work today’. It’s very easy to get lost in the woods and loose sight of the bigger picture. Although true in school and big-company environments, it’s even easier to forget and loose sight of the mission as no one governs CLZ in starting companies to changing the world.

Think Weekends remind me of these goals and reinforces them.

As an interesting side note - a friend of mine recently pointed out that BIll Gates is someone else who also shares this ritual. Based on the WSJ, it seems that Gates does this for a whole week and reads everything about Microsoft. Needless to say, mine is much less formal.

Here’s a peek into what I did over the weekend:

I booked a quiet and cozy room in Santa Cruz where I spent most of my weekend organizing thoughts on my Mac, in the morning and afternoon, I would pick out certain topics and go to my favorite view point at the Sea and Sands Inn.

It’s gorgeous:

Agenda for the weekend looks like:

Friday Night:

-Read over notes from the last Think Weekend
-Come up with core topics to think through

Saturday/Sunday

-Reach conclusions or action items on core topics

Sunday

-Write down notes from the weekend

What are the long term goals?

1) Start a Public company by 2018
2) Save 1 million lives and reduce global CO2 emissions by 20% by 2023

After every Think Weekend, I always come back much more motivated and focused on long term goals. I would highly recommend it to everyone.

no comments

Stored Procedures in MySQL

Posted by James Tue, 13 May 2008 00:44:00 GMT

A few days ago I wrote my first MySQL stored procedure.

For one of our applications, we had to make a big database schema change. As a part of this process, we needed to populate a new table with some existing data. However, there needs to be a bit of custom logic because the underlying data representation changed.

The algorithm for this operation is O(kn), where n is the number of rows in the original table and k is a small constant due to the way the data is stored in the original table. The unfortunate fact is that the original table has 3.5 million rows. So, I chose to do everything in a stored procedure.

Writing a stored procedure in MySQL is easy enough. However, the available tools suck. It took me forever to figure out that I have to change the delimiter from ‘;’ to something like ‘$$’ for everything to work. There is no debugger and no print statements. If you write something really complicated, it’s almost impossible to debug.

But the performance of stored procedures is amazing! I calculated that if I didn’t use a stored procedure it would take about 8 hours. When we ran the stored procedure, the whole thing finished in 15 minutes. It peaked at more than 13,000 queries per second! What’s more amazing, was that the application was still live and serving requests normally during the data migration.

mysql query graph

Well, needless to say, I’ve become a fan of stored procedures, even though it was a pain to edit and debug.

Posted in  | Tags  | 1 comment

Making Custom Strikethroughs with Trac

Posted by Linus Mon, 12 May 2008 21:41:00 GMT

I am going to make another technical post this week. We are using Trac as a way track bugs in our applications. It is extremely popular which as a result makes it a pain to find information on Google (trust me, try searching and you will get hundreds of irrelevant results).

The other day I spent a considerable amount of time trying to customize the reports to the way I want them. One problem was how to create a strikethrough for bugs that were closed. Several people have asked for this on various forums but no one had a solution. I decided to write my own.

My solution was to create a new style in the stylesheet and its corresponding tag. Though I could have overridden the existing styles but I like to keep this as OEM as possible.

In /htdocs/trac.css Add the lines:

table.listing tbody tr.even_strike td {
  background-color: #fcfcfc;
  text-decoration: line-through;
}
table.listing tbody tr.odd_strike td {
  background-color: #f7f7f7;
  text-decoration: line-through;
}

In /template/report.cs add the lines:

<?cs if row.__strikethrough__ ?>
         <?cs if idx % #2 ?>
           <?cs set row_class=$rstem+'even_strike' ?>
         <?cs else ?>
           <?cs set row_class=$rstem+'odd_strike' ?>
         <?cs /if ?>
       <?cs /if ?>

Finally in our customer report:

SELECT
  id AS ticket, 
  summary,
  priority,
  (CASE status WHEN 'closed' THEN 'true' ELSE '' END) AS __strikethrough__,
  (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner,
  datetime(changetime, 'unixepoch') AS Closed, description AS _description,
  reporter AS _reporter
FROM
 ticket t
LEFT JOIN
 enum p ON p.name = t.priority AND p.type = 'priority'
ORDER BY owner, priority, status

If you do this right, you should be able to see something like below:

Posted in  | Tags , , , , , ,  | no comments

How to do fast SQL Deletes

Posted by Linus Thu, 08 May 2008 18:35:00 GMT

This week, I’ll change it up and make a technical post. We ran into this problem when we tried to delete 20 million rows without bringing down our live site. A simple ‘delete from FOO where id < 20000000’ would have taken too long because the database would have to go through those rows, recreate the indices, and do all the internal SQL cleanup. Given these problems we had to do it a better way. The general idea is to:

  1. create a temp table
  2. insert the rows we want into the temp table
  3. recreate the indices
  4. switch over the tables.

The actual SQL statements is:


CREATE TABLE foo_temp (
  `id` int(11) NOT NULL auto_increment,
  `my_data` text NOT NULL,
);

INSERT INTO foo_temp (SELECT * FROM foo WHERE id > 20000000);

CREATE INDEX `foo_index` ON foo_temp(`my_data`);

DROP TABLE foo;

RENAME TABLE foo_temp TO foo;

Posted in  | no comments

Apple and Mr. Market

Posted by Jeff Tue, 06 May 2008 22:48:00 GMT

It was more than 50 years ago (1949) that Benjamin Graham published the Intelligent Investor.

I am happy to report that - as far as I can see, despite the invention of TVs, the Internet, and real-time trading, Human nature in the Market has not changed at all in 50 years and people are just as emotional and impulsive as they were in 1949. Oh - and Efficient Market Theory? - needless to say, I’m not a fan.

What Graham said: Imagine that “one of your partners, named Mr. Market, is very obliging indeed. Every day he tells you that he thinks your interest is worth and furthermore offers either to buy you out or to sell you an additional interest on that basis. Sometimes his idea of value appears plausible and justified by business developments and prospects as you know them. Often, on the other hand, Mr. Market lets his enthusiasm or his fears run away with him, and the value he proposes seems to you a little short of silly.” (Intelligent Investor p.204)

Irrational pricing of stocks? Let’s talk about Apple (Disclaimer: I own Apple). Near the end of last year, hype of the iPhone and iPod Nano launches led to an Apple ROCKET. We saw the stock go up from $122 (Aug 17, 2007) to $198 (Dec 31, 2007) in just 4 months. This was based on great sales combined with expectations that Apple will come up with a 3G iPhone and maybe snazzier Macs in MacWorld in January.

And then what happened? 1) NO 3G iPhone, and 2) the country is going to be in a recession is going to affect sales. Stock TANKs to $119 (Feb 22, 2008)

OK - so let’s take a look at these items a little closer. The issue of the ‘3G iPhone’ - it is clear to me that there is no doubt that Apple is going to come out with a second generation iPhone with 3G technology and probably GPS also. This will probably happen in June or December 08. Is Apple going to loose their competitive edge because they didn’t come out with it in ‘07? Have you played with the iPhone? That thing is an innovation freak of nature: I think it’s going to take companies at least 18 months to come up with a comparable product, the iPhone is just way ahead of its time. What about earnings? Sure, earnings will be lower than if Apple came out with the 3G iPhone, but that will only be temporary, until they get the 3G version made.

Now let’s take a look at talks of a recession. Last time I checked people who buy Apple products are well-off individuals who are buying electronic gadgets. My argument is that most people who are considering buying Apple products are not going to be impacted very much. Even if there is going to be some slipping of sales, a 40% reduction to stock price is still very much uncalled for.

I bought at 160 and kept buying as the stock tanked in January and February. Being a long term investor, I’m looking at at least a 2-3 year hold for Apple. They have the most innovative hardware personal electronics on the planet and arguably the most innovative operating system as well.

Who knows if there will be more growth to the stock price of Apple, but I’m certain that the company will continue to innovate in the decades to come.

Enjoy the Ride.

Posted in  | no comments

Life Lessons - Part 1

Posted by Linus Tue, 29 Apr 2008 17:41:00 GMT

This weekend I attended a friend’s birthday and it made me realize just how old I am getting. In the twenty six years that I have roamed the earth I have learned many important life lessons which I think I should start sharing. These aren’t going to be philosophical life lessons but instead a series of practical tips that have helped me so far. So without further ado…

Life Lesson #1 – Public toilets

We all know the situation; we’re out in public and then have a terrible stomach ache. Being nowhere close to the comfort of your own home, you need to find a clean public toilet.

Most people think the best place is to find a restaurant. However, often times you can’t find a clean one where you can just walk right without getting accosted by a host/hostess. Instead what you should look for is a hotel. Hotels always have great bathrooms and you can shamelessly walk right in, pretend you are a guest, and do your business.

Now I do agree that hotels are less abundant than restaurants but if you spend an extra five minutes in the car looking for one, you’d be surprised, there are more than you think.

Okay so once you get into the bathroom, I always have this problem. The way the toilet seat covers are designed it makes it so that the bottom flap (see picture below) always touches the water.

As the water soaks up the paper, it drags the whole cover into the bowl. This means you probably have at most 30 seconds to take off your pants and sit down.

The way to avoid this problem is to simply tear off either the bottom half of the flap so that it doesn’t touch the water. I can’t stress this enough because it will save you so much time in life setting a new cover.

That’s it for now, stay tuned for other life lessons…

Posted in  | no comments

Coolest Wine Opener Ever

Posted by Jeff Tue, 29 Apr 2008 17:07:00 GMT

OK - so I have to admit. I like wine. But something hit me over the weekend: how come we haven’t really improved the process of opening wine bottles?

I mean - we have such intricate systems for making wine - involving large amounts of chemical testing, big vats, and wine ‘scientists’ going in there to make sure everything is just right. But - I guess opening the bottle is just something that we omitted.

A year prior, after I landed a gig at IBM Research, I took my parents to The First Cabin at Balboa Bay Club. Really awesome views, really awesome food. We got a bottle for like $100 and while the waitress was poping the cork “CRACK” she cracked the bottle. Now c’mon, there got to be room for improvement there.

Let’s take a look at some of the old fashion openers:


So these openers go from pure stupid to a little more sophisticated. Take the one on the left, you have to PULL the cork out with all your might. Can you imagine doing that while hammered? You’d probably drop the bottle while pulling or spill half of it.

Now, the second one is the what the waitress used to crack our bottle at First Cabin. It uses the bottle top as a hinge - GLASS as a hinge??? That’s got to be bad. The last one is the one I currently have in our apartment. But just too big to carry around.

I FOUND something this weekend that’s REALLY Cool! It’s called a Cork Pop. Here you go:

This thing is awesome. First thing you notice is that there is no cork screw, just a needle. The idea is that instead of ‘pulling’ the cork of of the bottle, you ‘push’ it out with air.

There a small canister of air on the top - you simple jab that needle-like thing into the cork and then push the button on the top. Air gets pumped into the bottle and pushes the cork out with a POP.

That thing was amazing. Definitely going to buy one for mom for Mother’s Day in 2 weeks!

Posted in  | no comments

Krav Maga

Posted by James Sun, 27 Apr 2008 16:54:00 GMT

On Saturday, I went to my first Krav Maga class.

I really like Krav Maga. It’s one of the few martial arts that was invented after guns. It teaches you practical self-defense against realistic threats such as chokes, knifes, and assault rifles. It was designed to be easily learned, but difficult to forget. The movements are natural and for the most part instinctive.

In my first class, we did bunch of warm up drills, lots of punching, palm strikes, kicks, then for the last part, I learned a easy choke defense. I am completely sore… my back is killing me, but it feels good. I’m going to try to do Krav Maga twice a week… and see how far I can get with it.

I found an awesome Krav Maga video on youtube… enjoy.

Posted in  | 1 comment

Older posts: 1 2 3