An Object s Lease on Life

An Object’s Lease on Life

.NET Remoting uses a lease-based form of distributed garbage collection to manage the lifetime of remote objects. To understand the reasoning behind this choice of lifetime management systems, consider a situation in which many clients are communicating with a server-activated Singleton-mode remote object. Non-lease-based lifetime management schemes can use a combination of pinging and reference counting to determine when an object should be garbage collected. The reference count indicates the number of connected clients, while pinging ensures that the clients are still active. In this situation, the network traffic incurred by pinging might have adverse effects on the overall operation of the distributed application. In contrast, the lease-based lifetime management system uses a combination of leases, sponsors, and a lease manager. Because the lease-based lifetime management system doesn’t use pinging, it offers an increase in overall performance. Figure 2-7 shows the distributed lifetime management architecture employed by .NET Remoting.

figure 2-7 .net remoting uses a lease-based lifetime management system to achieve distributed garbage collection.

Figure 2-7. .NET Remoting uses a lease-based lifetime management system to achieve distributed garbage collection.

In Figure 2-7, each application domain contains a lease manager. The lease manager holds references to a lease object for each server-activated Singleton or each client-activated remote object activated within the lease manager’s application domain. Each lease can have zero or more associated sponsors that are capable of renewing the lease when the lease manager determines that the lease has expired.

Leases

A lease is an object that encapsulates TimeSpan values that the .NET Remoting infrastructure uses to manage the lifetime of a remote object. The .NET Remoting infrastructure provides the ILease interface that defines this functionality. When the runtime activates an instance of either a well-known Singleton or a client-activated remote object, it asks the object for a lease by calling the object’s InitializeLifetimeServices method, inherited from System.MarshalByRefObject. You can override this method to return a lease with values other than the default. The following code listing provides an override in the SomeMBRType class of the InitializeLifetimeServices method:

class SomeMBRType : MarshalByRefObject 
{
     

    public override object InitializeLifetimeService() 
    {
        // Returning null means the lease will never expire.
        return null;
    }

     
}

We’ll show another example of overriding the InitializeLifetimeService method in Chapter 3.

The ILease interface defines the following properties that the .NET Remoting infrastructure uses to manage an object’s lifetime:

  • InitialLeaseTime

  • RenewOnCallTime

  • SponsorshipTimeout

  • CurrentLeaseTime

We’ll look at an example of manipulating a lease’s properties in Chapter 3. For now, it’s important to understand the purpose of each of the properties that ILease defines. The InitialLeaseTime property is a TimeSpan value that determines how long the lease is initially valid. When the .NET Remoting infrastructure first obtains the lease for a remote object, the lease’s CurrentLeaseTime will be equal to InitialLeaseTime. An InitialLeaseTime value of 0 indicates that the lease will never expire.

The .NET Remoting infrastructure uses the RenewOnCallTime property to renew a lease each time a client calls a method on the remote object associated with the lease. When the client calls a method on the remote object, the .NET Remoting infrastructure will determine how much time remains until the lease expires. If the time remaining is less than RenewOnCallTime, the .NET Remoting infrastructure renews the lease for the time span indicated by Renew­OnCallTime.

The SponsorshipTimeout property is essentially a timeout value that indicates how long the .NET Remoting infrastructure will wait after notifying a sponsor that the lease has expired. We’ll look at sponsors shortly.

The CurrentLeaseTime property indicates the amount of time remaining until the lease expires. This property is read-only.

Lease Manager

Each application domain contains a lease manager that manages leases for instances of remotable object types residing in the application domain. When the .NET Remoting infrastructure activates a remote object, the .NET Remoting infrastructure registers a lease for that object with the application domain’s lease manager. The lease manager maintains a System.Hashtable member that maps leases to System.DateTime instances that represent when each lease is due to expire. The lease manager periodically enumerates all the leases it’s currently managing to determine whether the current time is greater than the lease’s expiration time. By default, the lease manager wakes up every 10 seconds and checks whether any leases have expired, but this polling interval is configurable. The following code snippet changes the lease manager’s polling interval to 5 minutes:

LifetimeServices.LeaseManagerPollTime = System.TimeSpan.FromMinutes(5);

The lease manager notifies each expired lease that it has expired, at which point the lease will begin asking its sponsors to renew it. If the lease doesn’t have any sponsors or if all sponsors fail to renew the lease, the lease will cancel itself by performing the following operations:

  1. Sets its state to System.Runtime.Remoting.Lifetime.LeaseState.Expired

  2. Notifies the lease manager that it should remove this lease from its lease table

  3. Disconnects the remote object from the .NET Remoting infrastructure

  4. Disconnects the lease object from the .NET Remoting infrastructure

At this point, the .NET Remoting infrastructure will no longer reference the remote object or its lease, and both objects will be available for garbage collection. Consider what will happen if a client attempts to make a method call on a remote object whose lease has expired. The remote object’s activation mode will dictate the results. If the remote object is server activated in Singleton mode, the next method call will result in the activation of a new instance of the remote object. If the remote object is client activated, the .NET Remoting infrastructure will throw an exception because the client is attempting to reference an object that’s no longer registered with the .NET Remoting infrastructure.

Sponsors

As mentioned earlier, sponsors are objects that can renew leases for remote objects. You can define a type that can act as a sponsor by implementing the ISponsor interface. Note that because the sponsor receives a callback from the remote object’s application domain, the sponsor itself must be a type derived from System.MarshalByRefObject. Once you have a sponsor, you can register it with the lease by calling the ILease.Register method. A lease can have many sponsors.

For convenience, the .NET Framework defines the ClientSponsor class in the System.Runtime.Remoting.Lifetime namespace that you can use in your code. ClientSponsor derives from System.MarshalByRefObject and implements the ISponsor interface. The ClientSponsor class enables you to register remote object references for the class to sponsor. When you call the ClientSponsor.Register method and pass it a remote object reference, the method will register itself as a sponsor with the remote object’s lease and map the remote object reference to the lease object in an internal hash table. You set the ClientSponsor.RenewalTime property to the time span by which you want the property to renew the lease. The following listing shows how to use the ClientSponsor class:

// Use the ClientSponsor class: assumes someMBR references an
// existing instance of a MarshalByRefObject derived type.
ClientSponsor cp = new ClientSponsor(TimeSpan.FromMinutes(5));
cp.Register(someMBR);