Why I switched from Rackspace to Amazon EC2

Feb 22, 2012 | No Comments

I have been a happy rackspace customer for a long time (almost 3 years by my estimation). I’ve had a number of various sized servers, mostly for hosting source code and managing this and some small web sites / blogs. I never really had any complaints with Rackspace, until recently when I had huge a billing scare. Although it was ultimately resolved with no problems, I decided to take a look at the competition, mainly Amazon EC2.

There are a bunch of articles which already exist about this. So I thought I’d give my two cents on the big differences:

Amazon Pros

  • Far greater choice in machine customization. Generally rackspace boxes have better cpu on the low end, but amazon scales to a higher degree and their boxes have substantially more memory for similar price points.
  • External Firewall. This is a nice feature, since it forces you to be security aware. They also encourage key access to your servers which tends to be much more secure.
  • Attachable Extra Storage. Elastic Block Storage volumes can be attached to any instance, providing a hard backup to your servers, or adding extra volume for data intensive tasks. This is contrary to rackspace where your disk volume is fixed.
  • Better Pricing and More Flexible Schedules. Amazon has many selections for pricing. You can get spot instances in which you bid for unused capacity at lower rates and reserve instances, where you essentially pay some cost up front and reserve the instance at a lower rate for 1-3 years. This alone makes it much better for a single server customer hosting a small blog or site, as it makes bigger boxes available at lower prices. Data transfer prices for outbound bandwidth are also cheaper.
  • Better Image Support. While rackspace always had my favorite linux distributions, Amazon EC2 has a wealth of server images provided by amazon and the community.
  • Great Command Line Tool Support
  • Better Performance. You are running on the glorious cloud infrastructure that is Amazon. The evidence speaks for itself.

Rackspace Pros

  • Simpler Interface. Rackspace wins in ease of startup. The terminology is intuitive (no laundry list of wacky terms like EBS, EC2, Route 53, RDS, ECU, which seem to exist only to confuse first starters).
  • Resize Instances On The Fly. You can resize any instance on the fly. If you realize your server is too small for what its grown into, you can resize it from 256 mb to 1024 mb. If you realize its too big, you can scale it down to 512gb. I have used this feature at like half a dozen times and it was painless. On amazon, you’ll need to make an image of your server and restore, or detach an EBS volume … it works but not nearly as easy as rackspace.
  • 24/7 Customer Support. Rackspace prides itself on customer service and it is excellent. I’ve found for serious problems its always best to call them, rather than go through the live chat. Phone responders are extremely helpful and polite.
  • Backups Included. You don’t pay for daily and weekly backups on rackspace, they are included with your server instance.

I won’t say which is better because they have different strengths. They provide similar products with different levels of service. Ultimately, it depends on what your needs are. Do your research and make a decision! This is actually my first blog post after having switched this site from rackspace to amazon. Overall I feel like I am getting a better value.

It’s been great rackspace, but I must bid you adieu.

Litecoin (LTC): An alternative to Bitcoin (BTC)

Litecoin (LTC): An alternative to Bitcoin (BTC)

Dec 12, 2011 | No Comments

A few months ago I found myself enchanted with the idea of bitcoin; which as you probably already know, is a digital currency. The interesting part about it is that there is actually a market for bitcoin where real cash can be exchange for the virtual currency and back. Basically what happened is that the popularity of bitcoin exploded, sending the price up to almost 30$/BTC. Early adopters who held their coins suddenly had fortunes worth $100k+!

My biggest criticism of bitcoin was always that it was pretty wasteful. Essentially throwing away GPU and electricity in order to enforce the security of the bitcoin system. The waste was in some sense required as the system functioned by assuming that the majority of the computing power of the network would never be under control of a single entity or group capable of exploiting the system (basically the larger it became the more secure it became).

Ultimately the market brought bitcoin into balance. The number of people with powerful GPU cards increased the hashing rate of the network, produced enough bitcoins to satisfy demand which along with waning interest brought the price of the currency down about 10 fold to $3 or so currently. I’m a big proponent of digital currencies, but I would really like to see a little less waste in terms of computing resources. That is where litecoin comes in.

Litecoin is basically a fork of bitcoin which uses a modified hashing algorithm that benefits more from CPU than GPU computing power. In a sense, this makes litecoin more easily accessible, and generally less wasteful as CPU can consume substantially less power. While I think that crypto-currencies are generally pretty wasteful, I think Litecoin is a step in the right direction. You know I take that back, as wasteful seems to be a loaded word here — perhaps a better term is “resource intensive”.

I would love to eventually see some sort of currency which allows useful work to be done in addition to managing a distributed currency. This would give the currency some sort of intrinsic value aside from maintaining its own integrity.

Anyway, if you are like me and feel like you can’t adequately contribute to bitcoin because of the size of the network, you should head over to Litecoin’s website and give them a try. You can even run bitcoin alongside litecoin since it runs completely independently of your GPU.

[Boost.Python] Calling back into python from native C++ threads

[Boost.Python] Calling back into python from native C++ threads

Dec 11, 2011 | No Comments

So I came across a particularly frustrating problem dealing with a boost python wrapper around a library which is calling python functions from native c++ threads (boost threads in this case). The problem was that some calls to release the global interpreter lock would cause python to through fatal errors and segmentation fault, despite seeming to acquire the GIL properly. This is how I solved the problem.

As a quick overview, you need to make sure that any native threads acquire the GIL (global interpreter lock) before any sort of python resources are touched. You may not use any C Python API functions or call into python until the GIL is acquired. A good practice is to follow the RIAA method for acquiring the GIL like so:

 

class ScopedGILRelease
{
public:
   inline ScopedGILRelease()
   {
      d_gstate = PyGILState_Ensure();
   }

   inline ~ScopedGILRelease()
   {
      PyGILState_Release(d_gstate);
   }

private:
   PyGILState_STATE  d_gstate;
};

void onData(const boost::shared_ptr<Data>::P & data, const void * closure)
    {
       ScopedGILRelease gil_lock;
       // invoke call_method to python
    }

In the above example we call the lock which invokes PyGILState_Ensure() to acquire the lock. Additionally we call PyGILState_Release() on the same thread state object when we go out of scope.

What bit me when developing this solution was that the api was not being properly initialized. Originally I was calling two functions in my one of my modules objects which I assumed was going to be called before any threads were made.

PyEval_InitThreads();
PyEval_ReleaseLock();

Now according to the python documentation, the first thread should:

Initialize and acquire the global interpreter lock. It should be called in the main thread before creating a second thread or engaging in any other thread operations such as PyEval_ReleaseLock() or PyEval_ReleaseThread(tstate). It is not needed before calling PyEval_SaveThread() or PyEval_RestoreThread().

However, that documentation is either incorrect or something very subtle was going on in my library, since PyEval_InitThreads() was being called before any thread operations and the object was being created in the main thread of the python interpreter. Perhaps I couldn’t guarantee that python wasn’t spawning some sub-thread to handle that part of the script. Anyway I found that I had to instead call PyEval_InitThreads() once in the module definition macro for boost python. Looking something like this:

BOOST_PYTHON_MODULE(ModuleName)
{
    PyEval_InitThreads();

    ...
}

As always a super simple solution to a super frustrating problem. This information was extremely hard to find, so hopefully this helps someone out there from losing hours of time. Also, here is a link to the stack overflow question I eventually posted and quickly subsequently answered.

Enjoy.

The Ultimate Home Media Center Solution [Spoiler: Plex!]

The Ultimate Home Media Center Solution [Spoiler: Plex!]

Nov 5, 2011 | One Comment

In this article, I cover my opinion on currently the best media center option for managing local content libraries, and my thoughts on the state of digital video distribution.

I do not condone any mention of piracy in the article and any references are merely for illustrative purposes regarding this rather opinionated article .

Feel free to jump ahead to the guide, but first some background:

How I Discovered Plex

I can say that I have been searching for the “perfect” home media center for a long time. I have a rather large collection of TV Shows and DVDs that have been ripped. For years I struggled with getting that media on devices I wanted. At first I converted them to PS3 compatible formats and streamed them over DLNA using TwonkeyMedia. That was great except that conversions took a long time. Then if I wanted them on my iPhone or iPod I need to convert them yet again, to a different format. Then I needed space for all those duplicate files (and managing them was a nightmare). There seemed to be a lot of disjoint solutions which required manual management. A friend of mine suggested XBMC, which is a great home media center front end with tons of plug-in support. It actually eventually led to my discovery of what I believe to be clearly the best product out there for managing a local collection of content and getting it on your devices, and it is called Plex.

What Is Plex?

First, let me explain what Plex does. Plex is a media center front end for Plex Media Server. Basically, Plex Media Server manages a library of media on a computer for you, and serves it up to multiple devices. So you can have a drive with all your TV Shows, Movies, Music, Photos, etc and Plex Media Server (PMS — an unfortunate abbreviation) will deliver it over your home network or the internet. Plex is a UI which runs on a computer, mobile phone or TV and gives you access to that content. It looks something like this:

Plex for Samsung


Basically gives you all your episodes (with metadata) automatically. Lets you browse by tons of different categories, like unwatched or recently aired/added. Did I mention it automatically organize your media assuming you have everything reasonable named? Basically it is pretty close to media center nirvana, and the basic application and server are free to use.

So what makes this better than any other solution out there, especially XBMC (from which Plex was originally development from)?

Well it’s all about Plex Media Server. Basically you can setup one machine in your network with media center, and serve up content to every client device running plex. The Plex client is available for Mac, Roku, Samsung and LG TVs, and jailbroken Apple TVs and more — so theres plenty of choice on the set top box front. Additionally there are native iPhone, iPad, and Android apps. Also, Plex Media Server automatically live transcodes videos which are unsupported on client devices, and almost all video formats are supported. This solves the problem I had originally in storing many copies of files. Also, since everything is on a common server, you can stop a show or movie on your TV, then watch it on your iPad in bed. The server keeps track of partially watch shows on all devices. Also, you may want to search for an application called sickbeard, as it can integrate with plex to enhance your experience (that is all I can say ).

If you like what you hear, I suggest trying it out. I can also recommend a setup which I’ve found useful, not everyone needs what I have, you can make due with less, but my setup fits with my other server needs.

Setting Up Plex Media Center

EDIT: I just noticed that as of the latest update, Plex is also available for Windows. So you can use any machine you want.

You Will Need:

  • Mac Mini (Alternative: Samsung or LG TV with Apps, Roku, Google TV, Apple TV)
  • Dedicated Server of any OS (Mac Mini will do, or any other machine so long as it is on while you watch videos)
  • Plex for iOS / Android (Last I checked it was about $3-5 in the app store but definitely the best purchase I’ve made)
  • Plex for Mac
  • Plex Media Server
Rough Setup and Reasoning:
  1. First, you want to setup your Mac Mini or other plex box. I recommend a mac mini because it is small and provides the best looking, fastest Plex experience. You also get a complete client implementation. It is also the most expensive option. There are Plex clients for TVs, Roku, Google TV or Apple TV (jailbroken), so you can also try one of those if you already have a different box.
  2. You should install and run Plex Media Server on a dedicated box you can always count on being on when you want to watch videos. This should also be the server all the media is stored on. Advantage of an always on server is that you can access your content from any time on the network (or on the internet if you setup myPlex which is available as of Plex 0.9.5).
  3. Plex for iOS is only necessary if you want to watch you media on mobile devices. The apps cost a few bucks but they are totally worth it and highly polished.
  4. Once you setup PMS you just need to run plex and it should auto-detect your local server, exposing all your media. If you run an alternative plex client you may need to point it to the address of your server.

Why People Want Plex

I asked myself why people want home media centers. I don’t think it is necessarily due to wanting to own copies of movies, tv shows and music. I think that Plex and XBMC have succeeded because simply put, the providers and TV device manufacturers have failed consumers. There is no single product out there that can give you everything you want, despite having tons of companies dedicated to this purpose.

Netflix

Netflix is cheap and great, but it has a terrible selection. I mean 8$ a month isn’t much but it seems like half the content on there are national geographic documentaries. They have great coverage for client devices (all mobile phone platforms, tablets, set tops, tvs) but their coverage is just so spotty. It seems like they are always losing providers or having content negotiation problems. Even so, most shows only come out on Netflix the same time they come out on DVD. Maybe I don’t want to wait 8 months to watch a TV show.

Amazon Instant Video

The next best service is probably Amazon Instant Video. It has a decent selection of TV shows and if you are an Amazon Prime member, the deal is great (I recently get addicted to Doctor Who thanks to Amazon Instant Video and am now I lover of the series). The problem with Amazon is that the client support is simply abysmal. I don’t understand what they are doing. Amazon is perhaps my favorite internet company but I think whoever is running their video distribution strategy is a buffoon. Amazon Instant Video has clients for Roku and some TVs. However, theres no iOS support (no iPad — come on!), no Android support, no apple tv support. If you sell a streaming service you need to make it accessible. Also, same criticism applies to the waiting period as netflix.

iTunes and Apple TV

Apple TV provides a wide selection of TV Shows and Movies. They have their own dedicated box which is simple and cheap. Unfortunately, Apple TV has serious, serious flaws. The deal breaker being that its 720p, as is most of the content on the iTunes store. Also, theres no subscription option, you have to actually buy all the episodes of every show. Its almost a good alternative if you don’t want cable.

I think what the world really needs is a digital form of on demand cable. Where you choose your box or client, and subscribe through a cable provider to certain shows or subsets of channels and have optionally commercial free streaming of content. Then you have access to all their networks contents from all time. The issue is that content providers and cable companies don’t want to stray from a profitable traditional business model. They like charging you 100$ a month for digital cable, when you watch about 25% of the shows and channels. Then they want to sell people box set DVDs they will watch once if ever after the original airing date. They think this is the best way to make money. What they don’t realize is that if they provide customers with choice, such as choosing which channels they watch, choosing whether or not to watch with commercials, they will make more money. You can lend a box set of DVDs to your friend. You can’t share DRM digital rental, subscription or purchase, and you can’t resell it when you are finish (this is why the video game industry loves digital downloads — they lose millions to the resale game market). This is not even to mention the possibility for targeted advertisements or exposing people to shows directly.

So how does Plex solve this? Well let’s be honest, a large number of people who want XBMC or Plex are downloading TV Shows or ripping their own bought discs. They are making this complete service for themselves, for the subset of shows they want to watch. The sad thing is that the piracy is actually a better solution then what is legally available. Using the right tools, it can become completely automated, commercial free, and instant available after airing — you could get full quality 720 TV shows and 1080p Movies as soon as they air or become commercially available without lifting a finger. Get with the program providers, if people are willing to spend thousands on custom servers, software and services, they will pay a little extra for complete, high quality streaming access that is guaranteed and legal.

Hopefully Google, Microsoft or Apple will get their act together and release a good solution. Apple has a good track record with provider industries in getting content on digital devices. Maybe we will see an Apple TV running iOS with native third party app support soon. That could mean an app store that supports channels from providers, or a common subscription model. There is a lot of money to be made here.

I’d like to hear what anyone else thinks. What sort of setup do you guys have, what do you want to see in a streaming service that doesn’t exist?

Implementing jQuery tablesorter natural sort plugin as text sort

Implementing jQuery tablesorter natural sort plugin as text sort

Nov 4, 2011 | One Comment

I have been doing quite a bit of web programming lately and was recently trying to figure out how to get the jquery plugin ‘tablesorter‘ to perform a natural sort.

For those not aware of what natural sort is, it is the way in which human beings tend to sort things intuitively (hence the prefix ‘natural’). In fact, coding horror as a pretty good article about natural sort and how tough it can be. To cut the story short, it ensures that word19 comes after word2 in a sorted list (a typical sort would place word2 after, since the ascii characters in the 5th position define the sort order).

Now, normally implementing a natural sort is not so difficult depending on the language, you basically split up the areas of the string that represent digits, and then sort each substring piece in order (string order if two strings, numeric order if two digits). Unfortunately, the javascript tablesorter plugin allows you to sort by providing a function that takes an element to be sorted and transforms it into a different string or number that must be sorted EITHER numerical OR textually, but not both.

Looking around online didn’t yield any good solutions aside from, “implement a jquery plugin”. So I got to wondering what how it could be done. These plugins could take the either table or cell as an argument but then you’d have to remain stateless and re-perform the entire natural sort over again. It may be possible to also use the jquery data api to store data about a table to be sorted and then fetch it on each call (performing the natural sort once and assigning ids to each cell or something).

It got me wondering about formulating a natural sorting algorithm as a numeric or textual sort. So I tried to prove if it could be done. I tend to get carried away and engrossed by certain little challenges like this:

1.) Natural Sort As Numeric Sort

This should become clear shortly that natural sort cannot be easily implemented as a stateless numerical sort. Since you are ultimately converting the string to a number, you can simply make the string longer to produce a bigger number and mess up the sort order.

2.) Natural Sort As Textual Sort

This seemed like it could work. A textual sort is just a sequential comparison of ascii coded bytes, each with a numeric value. Natural sort is also a set of sequential comparison operations that ultimately break down to numeric comparisons. One simply needs to convert the numeric pieces into a string of characters preserving the same order as the numeric sort, then create a new string with these numeric characters replaced.

I decided to follow the original idea of ascii code comparisons, and I decided to convert each number to a fixed length string in which each digit is a base 127 or 256 number. Why 127 or 256? They represent standard and extended ascii code length and ensure that the values always fall without the proper range. String needs to be fixed length so that all numerals are compared equally (longer numbers won’t be incorrectly compared against smaller numbers). The length of your string determines the maximum size of any numeric segment. That means that using a N digit base 127 string gives you 127^(N-1) numbers. At 10 digits, thats 3×10^18 numbers. If you use base 256 you get 21 decimal places. Technically you can go even higher with UTF8.

Anyway, here is my solution (excuse my probably ugly javascript, truthfully its one of the first javascript functions I’ve written from scratch).

 ts.addParser({
    id: "natural",
    is: function(s) {
        return false;
    },
    format: function(s) {
      var ret = "";
      var num_str = "";
      var base_num = 256;
      var fixed_len = 10;
      var num_to_ascii_len_str = function (base, max_len, num)
      {
        var tmp = num;
        var ret = "";

        for (var i = 0; i < max_len; i++)
        {
          if (tmp > base)
          {
            var div = tmp / base;
            tmp = tmp - div * base;
            ret = String.fromCharCode(div) + ret;
          }
          else
          {
            ret = String.fromCharCode(tmp) + ret;
            tmp = 0;
          }
        }

        return ret;
      };

      for(var i = 0; i < s.length; i++)
      {
        code = s.charCodeAt(i);
        if (code > 47 && code < 58) {
          num_str = num_str + s.charAt(i);
        }
        else{
          if (num_str.length > 0)
          {
            ret = ret + num_to_ascii_len_str(base_num, fixed_len, parseInt(num_str));
            num_str = "";
          }
          else
          {
            ret = ret + s.charAt(i);
          }
        }
      }

      if (num_str.length > 0)
      {
        ret = ret + num_to_ascii_len_str(base_num, fixed_len, parseInt(num_str));
      }
      return ret;
    },
    type: "text"
  });

Hope this helps someone out there, as this kind of thing is useful but not easily found. Oh and if anyone can figure out if its possible for formulate this as a numeric sort or knows a better way to do this in the context of the tablesorter plugin, please share!