Niraj Bhatt – Architect’s Blog

Ruminations on .NET, Architecture & Design

Loading 2 Versions of the same .NET Assembly

This can be an interview question :) . Let’s say I have a class

public class Class1 //1.0.0.0 – in C:
{
public string SayHello()
{
return “Hello World”;
}
}

and I version it to 2.0.0.0

public class Class1 // in C:\2 folder
{
public string SayHello()
{
return “Hello India”;
}
}

So you have 2 assemblies. Now, write a program in .NET to print Hello World & Hello India. Simple – use Assembly.LoadFrom (LoadFrom takes a path while Load goes through Assembly Resolver & Loader).

class Program
{
static void Main(string[] args)
{
Assembly assm = Assembly.LoadFrom(@”C:\ClassLibrary1.dll”);
Type[] types = assm.GetTypes();
foreach (var t in types)
{
Object obj = assm.CreateInstance(t.FullName);
Console.WriteLine(t.GetMethod(”SayHello”).Invoke(obj, null));
}

Assembly assm2 = Assembly.LoadFrom(@”C:\2\ClassLibrary1.dll”);
Type[] types2 = assm2.GetTypes();
foreach (var t in types2)
{
Object obj = assm2.CreateInstance(t.FullName);

Console.WriteLine(t.GetMethod(”SayHello”).Invoke(obj, null));
}
}
}

But to your suprise the output remains – “Hello World” & “Hello World”. If you take the second block first, output will change to “Hello India” & “Hello India”. Neither of them is what we expected. Conclusion – It’s not possible to load 2 assemblies by same name inside a single AppDomain. So you will have to create a seperate AppDomain & load the second version of assembly into it as shown below:

class Program
{
static void Main(string[] args)
{
Assembly assm = Assembly.LoadFrom(@”C:\ClassLibrary1.dll”);
Type[] types = assm.GetTypes();
foreach (var t in types)
{
Object obj = assm.CreateInstance(t.FullName);
Console.WriteLine(t.GetMethod(”SayHello”).Invoke(obj, null));
}
AppDomain app = AppDomain.CreateDomain(”Child”, AppDomain.CurrentDomain.Evidence, new AppDomainSetup { ApplicationBase = @”C:\2″ });
Assembly assm2 = app.Load(@”ClassLibrary1″);
Type[] types2 = assm2.GetTypes();
foreach (var t in types2)
{
Object obj = assm2.CreateInstance(t.FullName);

Console.WriteLine(t.GetMethod(”SayHello”).Invoke(obj, null));
}
}
}

Do you have any better ways?

July 10, 2009 Posted by nirajrules | .NET | | No Comments Yet

Tower Servers vs. Rack Servers vs. Blade Servers

Continuing my hardware series let me try to put the basic differences between above three.

Tower Servers are the normal boxes (in appearance) you would have seen in Visio Diagrams. Of course they are very powerful & have bundled software tools to manage them. The problem with Tower Severs is the space they occupy, management personnel they require, and cost of operating them (power, network, etc.).

Rack Servers are servers mounted inside a Rack (something like we normally use to manage our letters, office files, etc.) Major Racks available out there adhere to an IEEE standard and are measured in rack units or “U’s” (each U is 19” wide and 1.75” tall). So a rack server size is typically in multiplication of these “U’s”. Motivation here is to scale vertically than horizontally with more compact physical servers. In addition to this, there are many other electronic devices which adhere to this IEEE standard for instance – Rack Consoles, SAN devices, Power Backup devices, etc. Advantage being that you can fix them into rack as well along with your servers. Not to mention that the hardware vendors (Dell, HP, IBM, etc.) provide additional software tools that help you effectively manage these servers and in some cases the supported devices also.

Blade Servers are an additional level of innovation on top of Rack Servers. Blade Servers are typically placed inside a blade enclosure, and together they form a blade system. A Blade system normally meets the IEEE standard of Rack Units, which means that the entire Blade system can be placed inside the rack along with other electronic equipments. The benefits of blade enclosure includes hot plugging (normally blade servers have a handle attached to them, for transferring them in and out of the blade enclosure – it’s an easy way of identifying them) and stripped modular design (e.g. shared network ports, power connections, switches, etc.). For instance the hardware we ordered allows us to pack 16 blade servers inside a 10U space. I remember meeting an Oracle Consultant few months back where he was touting about a server with no disk. Such scenarios are possible (and are cheaper) by coupling a blade system and SAN storage. All these boils down to further space reduction, cost savings (power, administration staff) & easy management. Bundle this with Virtualization and you have a very powerful infrastructure at your disposal.

So any guesses as to what the next generation of servers would be :) ?

July 10, 2009 Posted by nirajrules | Hardware | , , | No Comments Yet

Passing Optional & Unordered Parameters via ODP.NET – PLS-00306

Times change. Recently I was interacting with a team that was using ODP.NET. And they had this stored procedure written in SQL server which they were moving to Oracle DB. They had some challenges in porting their data access code to support Oracle. One of them was – ODP.NET requires all parameters (even if they have default values) to be passed to the Stored Procedure and also expects them in the same order. But with SqlClient the same is not required. Solution to this is use BindByName property of OracleCommand class. Let me elaborate through a small sample code for better understanding:

Dummy

Consider the following dummy procedure using the above Dummy Table
CREATE PROCEDURE [dbo].[DummyProc]
@Age As Int,
@Name As varchar(50) = ‘AA’ –default value
AS
BEGIN
SET NOCOUNT ON;
DECLARE @noOfRows as INT

SELECT * from Dummy d where d.Age = @Age AND d.Name = @Name

SELECT @noofrows = @@rowcount –tSql is not case sensitive
RETURN @noofrows
END
Now the C# code to pass parameters to this would be:

SqlConnection conn = new SqlConnection(@”ConnString”);
conn.Open();
SqlCommand command = conn.CreateCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = “DummyProc”;
command.Parameters.Add(new SqlParameter(”@Age”, 2)); //Order doesn’t matter
//command.Parameters.Add(new SqlParameter(”@Name”, “AA”)); – This is not required
SqlParameter paramReturnCode = command.CreateParameter();
paramReturnCode.ParameterName = “ReturnCode”;
paramReturnCode.DbType = DbType.Int32;
paramReturnCode.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(paramReturnCode);
command.ExecuteNonQuery();
Console.WriteLine(command.Parameters[@"ReturnCode"].Value);
conn.Close();

//Output
1

Pretty simple. Let’s move the above to oracle now & access it from ODP.NET. First let’s create a stored procedure in Oracle.

create or replace
PROCEDURE DUMMYPROC
( p_Age IN NUMBER
, p_Name IN VARCHAR2 DEFAULT ‘AA’
, p_NoOfRows OUT NUMBER
) AS
BEGIN
select count(*) into p_noofrows from dummy d where d.age = p_Age AND d.name = p_name;
–N.B. In above ‘into’ is required, for select returning a recordset you need a RefCursor
END DUMMYPROC;

Now let’s write some code using ODP.NET (Oracle.DataAccess.dll)

OracleConnection conn = new OracleConnection(@”ConnString”);
conn.Open();
OracleCommand command = conn.CreateCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = “DummyProc”;
//command.BindByName = true;
command.Parameters.Add(new OracleParameter(”p_Age”, 2));
//command.Parameters.Add(new OracleParameter(”p_Name”, “AA”)); // This is not required
OracleParameter paramReturnCode = command.CreateParameter();
paramReturnCode.ParameterName = “p_NoOfRows”;
paramReturnCode.OracleDbType = OracleDbType.Int32;
paramReturnCode.Direction = ParameterDirection.Output;
command.Parameters.Add(paramReturnCode);
command.ExecuteNonQuery();
Console.WriteLine(command.Parameters[@"p_NoOfRows"].Value);
conn.Close();

The above code on execution throws an error – PLS-00306: wrong number or types of arguments in call to ‘DUMMYPROC’. The issue here is oracle expects all parameters to be passed in same order for faster execution. But as it turns out that it’s not always the case. Run the same Oracle code above but uncomment line no. 7 – command.BindByName = true

And here you are with the expected output.
//Output
1

Hope this helps :) .

July 9, 2009 Posted by nirajrules | Oracle.NET | , | No Comments Yet

I am back :)

mvp

What a way to celebrate a year of blogging. Yesterday not only marked a year of this blog (started in June 2008) but I also got my MVP status renewed into a more fitting category of – “Connected Systems Developer”. I wanted to write a detailed blog post about this, explaning the ins & outs of the MVP program but then thought otherwise. As usual, I owe the award to my family (for allowing me to stay away whenever it was required), friends @ Microsoft for providing opportunities to contribute & knowledge to share, and my MVP lead Abhishek. And last but not the least, you guys who make communities such a passionate place. A humble thank you to everyone :) .

July 3, 2009 Posted by nirajrules | Most Valuable Professional | | No Comments Yet

Types of Virtualizations – Server Virtualization vs. Desktop Virtualization vs. Application Virtualization

Praveen convinced me yesterday to have a look at Virtualization. I am jotting down few thoughts on the same. The word “Virtual” means “Existing in essence though not in fact”. Below I have outlined the basics of common Virtualization techniques out there, hope you find them useful (though it’s mainly from Microsoft Perspective).

Server Virtualization: I guess this is the most used one. Every time you want a new server you have to go your IT department, and they will put you in queue – either due to budgets, servers occupied, time to setup, etc. Challenge for IT department is most of the allocated servers are not fully used, and nobody is willing to share their Server with others. This is where Server Virtualization comes in. Server Virtualization gives you the power of creating Virtual machines which in turn can share the physical resources of the underlying Server (adding saving of power, space, coolant and professionals). Every virtual machine runs in isolation and has its own OS and applications as required. Technology that enables this is called Hypervisor which sits in between the underlying hardware & virtual machines (for instance Hyper-V available for Windows Server 2008 64 bit editions). Apart from virtualzing the underlying hardware for Virtual Machines, Hypervisor also supports live migration (only with 2008 R2) of Virtual machines (in case there is a failure / up gradation of underlying host hardware) which results in higher up times, Load balancing (moving workloads to less utilized host Server) and power management (turn of machines which have no workloads). Understanding Server Virtualization is necessary for anybody to understand about how Azure (Microsoft cloud) operates.

(N.B. Microsoft Hyper-V Server 2008 which a variant of “Windows 2008 Core” comes with CLI only interface, helping you getting speed up with your Server Virtualization efforts. Also you can migrate your .VHD created by Virtual Server to use Hyper-V).

So will my existing hardware suffice? It can but it may not scale enough. Lot of hardware innovation is currently going on to leverage on Server Virtualization. For instance, Intel VT-x processors ensure better scalability, performance & reliability for Server Virtualization efforts.

Desktop Virtualization: This consists of 2 parts – Client hosted desktops & Server hosted desktops. Client hosted desktops help creating a virtual environment for running legacy applications (for e.g. you have an application running only on Windows XP but your current OS happens to be Windows 7). The way we normally get this working is by using Virtual PC or Windows Virtual PC (bundled with Windows 7). Microsoft Enterprise Desktop Virtualization (MED-V) makes management of these Virtual PCs (vmc / vhd) a breeze. Coming to Server hosted desktops, here the client OS, Applications & Data are kept on Severs in data centers benefits being better utilization of hardware, easy maintenance& better accessibility (for instance you can access the desktops from your home). The technology which enables this is called Virtual Desktop Infrastructure (VDI) leveraging protocols like Remote Desktop. The technology is still evolving so it’s recommended you do a thorough check on it before proceeding.

Application Virtualization: Here the Applications are virtual (they are never installed physically on boxes either due to conflicts with it’s previous version / other applications or for security reasons). Only thing required for installation on client machine is App-V client. Software is either streamed or cached locally on demand and then executed (sandbox execution). The Microsoft product which enables this magic is called App-V (previously known as SoftGrid). Other benefits of App-V include centralized management, easy scalability, easy availability, easy deployment, etc. There is also another technique for Application Virtualization supported by few vendors – Presentation Virtualization (Screen Scraping). Here the screen travels all the way to your terminal while application is running on server & your input gestures are transferred back (we had proposed this to one of our clients who was insisting on a web based application while we had a windows application).

Other Virtualization Products: XenApp, XenServer, XenDesktop – Citrix; VMware, Vmotion – VMware

I would look forward to read your thoughts on above :) .

June 22, 2009 Posted by nirajrules | Virtualization | | 1 Comment

Tech Ed On Road Demos & PPTs – Azure Services

Dear All, as promised here is the complete pack of slides & demos. It is really difficult to cover all Azure Services with Demos in just 45 minutes and that too with a machine reboot it’s a mission impossible. But thanks to Jani who kept going despite the issues, allowing me to bring things up again. By the way am I sort of impressed by the theme of demos we came up with – a paper less feedback for an event, guess that’s the way to go. If you have any doubts on demos or presentation (though we took care that they are no more than few lines each), feel free to put it across.

A special thanks to platinum sponsors of the event – Telerik and Infragistics without whom event wouldn’t have been possible.

Last but the not the least – Jai Ho – Tata Indicom :) . Love you all.

June 21, 2009 Posted by nirajrules | Windows Azure | | 3 Comments

Entering Cloud with Tech Ed On Road

I will be doing a session on “Azure Services Platform” at this upcoming – Tech Ed On Road. It’s a joint event organized by BDOTNET & BITPRO user groups. Cloud for me is the most promising upcoming technology & recently I have been investing lot of my time in it. My co-speaker for the session is Janakiram (I would be surpised if you are following Azure and haven’t heard about him), who is about to hit a silver jubilee on Azure sessions. I have started enjoying combo talks after the one I did with Ramnish back at Tech Ed, I am quite hopeful that this too would be worth your time & petrol. Registrations are already closed but if I can still try helping out (after all I need to give some benefits to my readers :) , though no promises are implied). Do drop a line to me on the same, and I will try my best. I will also post the samples & presentation slides here after the event. Looking forward to meet you all. Keep Rocking.

June 19, 2009 Posted by nirajrules | Speaking Engagements, Virtual TechEd | | No Comments Yet

RAID 0 vs. RAID 1 vs. RAID 4 vs. RAID 5 vs. RAID 10

My company had ordered some hardware recently. As usual, the buying process brought in lot of insights which I thought of sharing with you guys. I will cover them all in parts, starting with basics. First let’s see a fundamental concept called RAID which becomes critical when you start planning your storage.

RAID (Redundant Array of Independent Disks) – This technology is mainly used for hard disks to provide protection against hard drive failures and also to improvise performance (of course you can select either of failure protection / performance). RAID also gives an implicit way to scale your disks (after all it’s an Array). RAID builds on 2 primary concepts – Striping (storing data across multiple disks sectors of multiple drives so that disk head can read more data in a single move) and Mirroring(replication of drives). RAID requires minimum of 2 disks & can have maximum of 32 disks. It’s also important to note here that RAID controllers typically abstract out the striping & mirroring details giving end user a feel of only a single disk available at disposal. Below are some of the popular RAID configurations out there:

RAID 0 – Striping
Pros: Better performance – data replicated across drives, no storage overhead as drives are utilized 100%
Cons: Possibility of losing entire data on failure of a single disk
Minimum Disks Required – 2

RAID 1 – Mirroring
Pros – Guard against disk failure as data is replicated across disk drives
Cons – Replication creates storage overhead as the same data is copied across drives
Minimum Disks Required – 2

RAID 4 – A Mirroring variation. It actually uses the concept of ‘Parity’ (based on XOR operation – Sum of all bits across drives – if sum is even parity set to 0 else parity set to 1) to retrive data in case of a disk failure, which reduces storage overhead – an issue with RAID 1.
Pros – Reduced storage overhead (actually we need only a single disk here to store parity). E.g. if you have 3 disks, parity can be stored on 3rd. So your overhead is only 33% in terms of storage.
Cons – Still suffers from a performance prespective
Minimum Disks Required – 3 (anyways 2 doesn’t make sense and the more no. of disks you have lesser would be your storage overhead)

RAID 5 – Striped Data & Striped Parity – most widely used. Taking RAID 4 to next level by striping the parity (parity gets stored across disks instead of getting stored on single disk) and also striping data (that anyways doesn’t matter as far as parity is concerned)
Pros – Good Performance, Good failure protection
Cons – Not as good when your requirement is only performance or only failure protection (parity doesn’t come for free).
Minimum Disks Required – 3

RAID 10 (1 + 0) – Combination of Stripe & Mirror with no parity. You stripe the data across drives & mirror the entire set of drives.
Pros: Best in terms of performance & guards against potential failures.
Cons: Costly in terms of storage overhead
Minimum Disks Required – 4 (and for > 4 you must have even number of disks)

There are few other RAID levels which I would leave for the reader to digg into, as they are comparatively not that widely used. I will continue exploring hardware aspects further in next thread.

June 18, 2009 Posted by nirajrules | Hardware | , | 2 Comments

What is .LDF – SQL Server?

I was quite surprised to see search engines throwing very less information on this one, yet it’s a frequently asked question. LDF acronym stands for Log Data File (though could have been called – Transactional Log Data File). This is a must to have file, and is created at the same time the database file (.mdf) is created by SQL Server.

So why do you need it? For keeping .mdf consistent. To cut short I will copy these lines straight from wiki :
“SQL Server ensures that any change to the data is ACID-compliant, i.e., it uses transactions to ensure that any operation either totally completes or is undone if fails, but never leaves the database in an intermediate state. Using transactions, a sequence of actions can be grouped together, with the guarantee that either all actions will succeed or none will. SQL Server implements transactions using a write-ahead log. Any changes made to any page will update the in-memory cache of the page, simultaneously all the operations performed will be written to a log, along with the transaction ID which the operation was a part of. Each log entry is identified by an increasing Log Sequence Number (LSN) which ensure that no event overwrites another. SQL Server ensures that the log will be written onto the disc before the actual page is written back. This enables SQL Server to ensure integrity of the data, even if the system fails. If both the log and the page were written before the failure, the entire data is on persistent storage and integrity is ensured. If only the log was written (the page was either not written or not written completely), then the actions can be read from the log and repeated to restore integrity. If the log wasn’t written then integrity is also maintained although the database state remains unchanged as if the transaction never occurred. If it was only partially written, then the actions associated with the unfinished transaction are discarded. Since the log was only partially written, the page is guaranteed to have not been written, again ensuring data integrity. Removing the unfinished log entries effectively undoes the transaction. SQL Server ensures consistency between the log and the data every time an instance is restarted.”

Are there any other uses of .LDF? Yes they can be used for Log Shipping, a high availability solution.

What are the recommended optimizations for LDF file? LDF could be stored on a separate disk having a dedicated disk controller in high volume scenario. Growth of LDF should be managed as daily maintenance activity as it can easily eat up the available storage capacity. A 2 disk RAID 1 is also a good trade off for LDF is terms of performance vs. failure protection (if you are able to invest money on disks RAID 10 is good for MDF (faster read)).

Hope that helps you get started :) .

June 14, 2009 Posted by nirajrules | SQL SERVER | , | No Comments Yet

Cost Based Optimization (CBO) vs. Rule Based Optimization (RBO)

These terms were brought up in a recent meeting. I decided to dig them out. These are the optimization strategies used by Database engines for executing a query or a stored procedure. They come into picture after a query or Stored Procedure is compiled and is just about to execute (most databases also cache these generated execution plans). Topic of optimization strategies & their differences can be huge one (guess one can write a book on that) but in this post I will try to keep things simple at a definition level. (An analogy here could be you want to travel from destination A to B, & you have several routes to pick up from.)

Rule Based Optimization: This is an old technique. Basically, the RBO used a set of rules to determine how to execute a query. E.g. If an index is available on a table, the RBO rules can be to always use that index (a RBO for our travel analogy can be avoid all routes with speed brakers). As it turns out that this is simpler to implement but not the best strategy always and can backfire. A Classic example of indexing a gender column is shown here in a similar post. RBO was supported in earlier versions of Oracle. (SQL Server supports table hints which in a way can be compared to RBO, as they force optimizer to follow certain path).

Cost Based Optimization: Motivation behind CBO is to come up with the cheapest execution plan available for each SQL statement. The cheapest plan is the one that will use the least amount of resources (CPU, Memory, I/O, etc.) to get the desired output (in relation to our travel analogy this can be Petrol, time, etc.). This can be a daunting task for DB engine as complex queries can thousands of possible execution paths, and selecting the best one can be quite expensive. For more information on CBO I suggest you go through “Inside MS SQL Server 2005 T-SQL Querying”. CBO is supported by most of databases including Oracle, SQL Server, etc.

(N.B. If you find execution plan selected by DB engine is not the optimal one you can try breaking your query into smaller chunks or changing the query logic)

As a programmer you should strive to ensure that cached query plans are used as much as possible. One of the techniques which can get you going is using parameterized queries & this turns out to be important even if you are using an O/R mapper like NHibernate as shown in this post.

I will look forward to hear your thoughts on above.

(P.S. TOAD from Quest is very useful tool if you want a deep dive into execution plans, just feed your query / SP to it and it will provide many alternatives plans suggesting optimizations & indexes).

June 10, 2009 Posted by nirajrules | Performance Tuning, SQL SERVER | | No Comments Yet