Friday, 18 June 2010

Why its good to understand FileShare semantics

I got caught out by the same issue twice in a long period of time and judging by the Google results I was getting searching for the issue, its either not very common or I missed the plot completely.

The start of the story is my implementing log4net in an ASP.NET application hosted on godaddy. Now godaddy.com has the speciality of running ASP.NET apps under medium trust IIS. That poses some restrictions but its pretty straightforward to get log4net running using by using the log4net documentation. For a rolling log file appender you must remember that access is limited to folders under your application root. You'll have to set permissions to read/write for that folder in the godaddy.com hosting control panel.

The stumbling block for me came when I tried to use FileZilla to download the log files. FileZilla was erroring out with a "file in use, access denied error" no matter what. I went back to the log4net appender config and set up MinimalLock. Which means a lock on the file is held only for the period of a write by log4net. I thought great that would be the end of that, but I was still getting issues trying to download with FileZilla.

I then decided to switch to take a different tack and wrote an ASP.NET webpage that displayed the contents of the log file. My thinking was that it would be the same process accessing the file so it should be all good.

Now my code to access the log file from the webpage essentially boiled down to

File.ReadAllText(path)

I still got the access denied problem. I now decided to eliminate godaddy from the equation. I thought it would be a simple exercise initially, famous last words, so I ran the logging code directly on godaddy.com at first. To my surprise I could reproduce this locally.

It then occurred to me that I didnt really know in what mode File.ReadAllText was opening the file, though I had hoped it was with minimal restrictions; "read only". I took the file access into my own hands by doing something akin to:

new StreamReader(new FileStream(path, FileAccess, FileShare))

keeping to my idea of a minimalist thread I put in FileShare.Read. Even with the IntelliSense help it seems like a good idea -> all I want is to read, write ?

The problem still did not go away. I then decided to pull out the big guns and see whats really going on cause I was getting frustrated by this point. So I reached for procmon and monitored the file access calls. After looking at the output for a bit it dawned on me something I had cause to encounter a couple of years back.

The semantics of FileShare are that it defines how else you allow another component to open the file you are working with. Now log4net, the first open of the file, opened it with FileShare.ReadWrite. This means the file can be opened a subsequent time for reading or writing. The issue arose because of my paranoiac Read only view which was my initial knee jerk reaction to the access denied errors.

I was trying to open the file with FileShare.Read which meant that I tried (from the perspective of the OS) to mandate others could only open the file read only, which of course clashes with what is already set on the file initially by log4net i.e. FileShare.ReadWrite.


To summarize, if you get access denied errors accessing files from code that you are the author of then do not forget your sharing semantics.

Sunday, 13 June 2010

Germany over everything

Definitely over the Socceroos. After England's unconvincing performance, this was the first of the heavyweights coming out in emphatic fashion.

It was a very entertaining match that could have ended with an even higher tally for the Germans than the 4-0 it was. The Australians let Germany play free flowing attacking football with some very very nice combinations. Though they scored 4 goals it looked like they could still tweak some nuts and bolts especially when finishing. The "other" number 13, Mueller, would surely have been more clinical.

There are probably a million and one reports from the match, so I'll leave with one thought: it struck me as strange, though comforting, that the German national team at the World Cup had players called: Podolski, Miroslav, Oezil, Khedira, Cacau, Gomez and Marko. We have all come a long way.

In search of childhood - strudel

I sometimes try to recreate experiences from my childhood. Its not always easy and most times the experience is not the same. In one area though I do have consistent success and that is food. Trying to recreate what I saw my parents and grandparents make is something that has gone well.

It seems the farther I am from the places I grew up in the more I am inclined to bring back the tastes of those places, especially the way I remember them growing up. This time I take a stab at making a traditional apple strudel the way my grandmother used to make it. I do have my grandma's recipe at my disposal but the most important ingredient, flour, is so different here in California that I will hack the recipe. This recipe has been adapted to fit your screen in California.

Also if you are not comfortable flying by the seat of your pants, then this recipe is not for you as there are few exact measurements.

Ingredients

  • 8 sheets ready made filo pastry - I used the Safeway brand
  • a handful of raisins
  • 3 cooking apples - granny smith, bramley etc
  • 2 eating apples - fuji for example
  • plain breadcrumbs - this soaks up the apple juice, use more if you are using juicier apples
  • butter - about 1 stick, melted
  • raisins - soak in brandy or rum
  • walnuts or pecan nuts
  • sugar - brown or white use depending on how sweet your apple mixture is
  • cinnamon - 1-2 tbsp

Execution

  • Set your oven to 350F.
  • Peel, core and grate the apples. Use a coarse enough grate, you don't want apple mush at the start.
  • Add breadcrumbs, sugar, nuts, cinnamon and raisins to the grated apples. For me the other ingredients act as a complement and I will add them in appropriately small quantities. Its above all an apple strudel after all.
  • Follow the preparation instructions on your ready made filo pastry to get 4 sheets. Roll up half the apple mixture in the first 4 sheets and use the rest for the next.
  • Bake at 350 for 35 to 40 minutes.

Result

The end result I found was a good enough approximation of my grandma's apple strudel. The original 'pulled' pastry required for the strudel is a butter/margarine based thin flaky pastry much like filo. the last part of its preparation entails rolling it out and pulling at it by hand to make it thing enough. I recall vividly helping my grandma with this. It was always interesting to find out how thin I could make it go without cracking. Also the original would always call for walnuts, but I didn't have any at hand when I set off to make this.

Filo pastry makes the strudel a bit harder and more brittle than the original. I found that the strudel was better the next day as it had absorbed some moisture and the flakiness was reduced.

Sunday, 23 May 2010

Database deployment projects in a non model environment

The setup

A development server running SQL Express 2008. A production server running SQL Server 2005 standard edition. A web application with a database store
in the development environment ready for a first test on production - call it staging for this purposes. Our development database had grown organically with schema changes on the fly driven by incremental features on the other tiers. At a certain point in time we made use of databasescripter to help with recreating the database schema as we are a distributed team (of two). The databasescripter served its purpose, but taking into consideration that the database schema was not the core focus of our project, it required too much TLC. The migration to production also posed a problem as that server was running SQL 2005.

The requirements
  • Have a simple way to manually kick off the deployment of our production database to SQL 2005.
  • The database settings should not be modified nor should the database itself be recreated (thats a limitation of our hosting provider).
  • The method should drop existing schema objects before recreating them.
  • Data need not be preserved.
  • This process should be triggered from inside the Visual Studio 2010 environment.

My solution

My first try was using the Package/Publish SQL feature of the web project in Visual Studio 2010. An overview of its use is here "Database Deployment with Visual Studio 10", though its pretty clear from the property sheet itself how to use it. After setting up everything I continuously hit the problem that the database did not get deployed. The output window didn't even register any activity at all - blank. Finally I think I tracked it down to the deployment method I was using. Our hosting provider doesn't allow for any 1-click or IIS package deployment, its a shared hosting setup. This means that whilst the website itself was published (i.e. copied) fine using ftp, there wasn't anything after that. I haven't confirmed in the documentation whether this is as designed, but I can well imagine it to be (i.e. I am almost certain it is so). Though this was inadequate for our purposes I do like the fact that you essentially define a "from" database and a "to" database using connection strings and the database schema is transferred across. I don't know for sure but I suspect (i.e. I am almost certain it is so) this leverages the same infrastructure as the Database Deployment projects which is what I ended up using. For dedicated hosting solutions I can imagine this would be great.

Second try was using "Declarative Database Development" as I had a full installation of Visual Studio 2010 at my disposal. This was in the end really easy:
  • Create a new SQL Server 2005 Database Project.
  • Bootstrap the project contents from you "from" database by using Properties -> Import Database Objects and Settings.
  • Fill in your "to" database in the the Target Database Settings of the Deploy tab in the project settings.
  • Hit "Deploy" from the Project context menu.
  • Great success!.
Now to be completely accurate I did encounter some quirks along the way mostly cause of my head first dive into this. After importing from my development database the project detected that the schema was from SQL 2008 i.e. SQL version 10.0.x. This meant that the DatabaseSchemaProvider (DSP tag in the project file) was set to something like Sql100.*. And trying to deploy to SQL 2005 resulted in an error. Now since I knew there wasn't anything SQL 2008 specific about our schema I hand edited the project xml file and changed the DSP to Sql90.*. Also due to the restrictions of our hosting provider I had to delete some of the imported SQL objects: setting up a dedicated user for example.

All in this was a surprisingly painless experience - as it should be for requirements so simple and basic.

Sunday, 21 February 2010

The good, the bad and the Kindle

I jumped on the e-reader bandwagon thanks to a well timed Christmas 2009 gift. I write well timed because of an incident on my daily commute a few weeks earlier. That day found me lugging a couple of things: IBM Thinkpad T61, Programming WPF and Against a dark background. The two books almost equaling the laptop in volume and weight and so I said to myself how handy it would be if the two tomes were in electronic form.
I must mention that I spend a significant part of my day looking into computer screens in some form or the other. After experiencing a lot of eye fatigue from backlit displays any respite I can get from them is very welcome. If the e-ink displays proved to be as tiring as a backlit display for me then the e-reader would be worthless. Its been more than 4 weeks of Kindle use, so without further ado my thoughts.


The good

I cant say how much of the display can be attributed to e-ink and how much is due to the Kindle implementation (as I haven't seen other e-readers), but for me it does exactly what "it says on the tin". The single most important criteria to me is readability
; Readability under different light sources sunlight, light bulbs, office lights, headlamps in that order, is paramount. The 2 factors that affect readability are: high contrast and zero glare. Other reviewers have mentioned the black on gray as not being enough contrast, I disagree. You definitely can't crank the contrast up to be as high as white on black but I find it more than adequate. The second point is where the Kindle excels, reducing glare to practically zero. I for example try to do a lot of reading on my commute. Seeing as my train runs a north-south route I need to get a seat on the left side of the train in the morning and the same on the return trip. Well me and all the other laptop/electronic device users. With the Kindle I don't really care where I sit cause the sunlight has zero impact on my reading ability. Reading outside on the porch is also a no go with my laptop or my iPhone. Its touted IPS technology doesn't work under my sun (I suspect it'll be the same for the iPad).
If you do get some glare when reading the Kindle you can always tilt it and because of its ridiculously wide viewing angle you'll definitely find comfortable reading.


The battery life is great, unbelievable for an electronic device and takes me back to the old Nokia 7360 mobile phone I used to own. That would just keep on going - to borrow a phrase. That stated the battery suffers from one very annoying flaw and its not so much with the battery but with the software. The default mode for the wireless connection is on and this drains the battery relatively quickly. I don't see the point of having the wirel
ess connection always on with a device that is primarily for offline reading. I can only assume Amazon is aware of such a bad choice and hence the "Turn Wireless Off" menu item features so prominently (its the first menu item).

@kindle.com; This dedicated email address allows you to send attachments in different formats which are downloaded to your Kindle and appear under the "Personal Docs" category. Aside from making Kindle a good general reader it allows you to read your existing books if you have any already in electronic format.


The bad

Yep as well as being the strong point this display is one of the Kindle's biggest shortcomings. I'll keep it short: it is very SLOWWWWWW. For me tha
t translates to very noticeable lag when turning pages (you get used to it) and instances where you do not know what is happening, like when ordering from the store from the Kindle (this catches me off guard every time). Now I am going out on a limb and attributing the slow speed to the display cause thats the only technical component that makes sense. I cant imagine its the processors cause there really isn't a whole lotta of processing going on.


The Kindle



A conclusion
I very much recommend getting the Kindle as the closest thing to reading a book that's out there now. Until we get that direct jack into our brains and can "know jujitsu" in the blink of an eye.