NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Lease Based Lifetime Concepts

Distributed Garbage Collection (DGC) controls when server applications can be deleted. Traditionally DGC uses reference counts and pinging for control. This works well when there are a few clients per service, but doesn’t scale well when there are thousands of clients per service. A Leasing Distributed Garbage Collector (LDGC) associates a lease time with each service. The service is deleted when the lease’s time expires. The LDGC can subsume the function of a traditional DGC, in addition it scales well when the numbers of clients increase per server.

The Leasing Distributed Garbage Collector (LDGC) associates a lease with a remotely activated object. When the lease expires, the object is removed. A lease can specify that an object has an infinite lifetime. An infinite server is removed by being explicitly canceled by a client or administrator.

Lifetime Policies

A LDGC supports many policies in controlling the lifetime of an object. The following are typical lifetime policies:

Few long-lived remote objects with many clients

The remote object’s life is independent of the clients’ life. The lease specifies a long lifetime that can periodically be renewed by an administrator or a client. This is the most efficient use of leases in that there is very little network traffic needed for distributed garbage collection.

Many short lived remote objects with few clients

The remote object is not used very long, and needs to be removed as soon as its function is completed so as not to tie up scarce server machine resources. In this case the object has a lease with a short lifetime. The client frequently renews the lease with a short time span. When all the clients are finished with the remote object, the service is removed. This policy substitutes increased network traffic for more efficient use of the server machines resources.

Administrative Policies

A LDGC can be administered in many ways.

Central administrator. The central administrator establishes the initial value for the remote object’s lease.

Client administrator. The client specifies the initial lease time.

Sponsorship. A client sponsors the remote object by registering itself with LDGC. When the lease expires it asks one of its sponsoring clients for more time.

This administration policy is particular effective for parallel problem solving, in that the client can give multiple remote objects a problem to solve. When a remote object runs out of time, it asks the client for more. If the client already has the solution to its problem from another remote object, it doesn’t renew the object’s lease.

This policy is also useful for migrating remote objects, in that the remote object contacts the client from its current location rather then the client having to find it.

In the case where there are many clients per remote object, it is more efficient to have the remote object ask one of the clients for a lease renewal, then have all the clients pinging the remote object.

Design

Each remote object can have a lease that controls its lifetime. All leases in an Appdomain are connected to a Lease Manager. This object is the root object that keeps the leases and remote object alive. This object is also used to query and administer the leases in the appdomain. It determines when a lease’s time has expired, and if it has, informs the lease. The lease runs in the Lease Managers thread.

A lease contains its expiration time. A lease can be renewed in three ways.

Lease Manager

Each lease appdomain contains a Lease Manager that administers the leases in its domain. Among its tasks is to periodically examine the leases for time expiration. If a lease has expired it can either be cancelled by removing its reference to the lease, or renewed by invoking one or more of the lease’s sponsors. The lease manager has a Lease List with leases sorted in order of increasing time left. The leases with the least time will be on the top.

Lease

The lease consists of a collection of properties that determine its policies, and methods that renew the lease time. The lease exposes the ILease interface. The Lease contains the information that is detailed in the following sections.

Renewal Policies

RenewOnCall – Each time an invocation is made on the service, the lease time is set to the maximum of the current LeaseTime or the current time plus the RenewOnCall time. This value supports the client transparently setting the lease time. If the value is null, then the object will not be pinged on call.

SponsorshipTimeOut – When the LeaseTime is elapses a sponsor is asked to renew the lease. The sponsorshipTimeout specifies the amount of time to wait for a sponsor to reply. If the value is null, then the lease will not take sponsors.

CurrentLeaseTime – The time that the lease will expire. If the value is zero, the lease will not expire and will be infinite. Specifies that this is an infinite lease that will not expire. Shutdown of the service occurs with the use of the Cancel method. This value is set if no lease time is given in the constructor.

Sponsors

SponsorList – A list of the sponsors ISponsor callback object sorted in order of decreasing sponsorship time. When a sponsor is needed for renewing the lease’s time, one or more sponsors from the top of the list will be asked to renew the time. The top of the list represents the sponsor that previously requested the largest lease renewal time. If a sponsor doesn’t respond in by the SponsorshipTimeOut time span, it will be removed from the list.

Links to lease manager and managed object

LeaseManager – The leaseManager object.

ManagedObject – Object which lease manages.

State

State – The current state of the lease.

ILease properties

LeaseTime - Initial time for lease. If leaseTime is null, then isInfinite is set to true.

RenewOnCall – The amount of time that a call to the remote object increases the leaseTime. If RenewOnCall is not null, then a sync is created to renew the lease by the RenewOnCall amount on each call.

SponsorTimeOut – The about of time to wait for a sponsor to return with a lease renewal time. If the sponsorshipTimeOut is null, then this lease will not take sponsors.

ILease Methods

Register – registers the sponsor’s callback object with the lease. A renewal time can also be sent with the Register method.

UnRegister – removes a sponsor from the lease.

Renew – This method is used to renew a lease. The lease time is set to the maximum of the current leaseTime or the current time plus the renewal time. The leases expiration time is returned to the caller.

Obtain the lease

The lease has various methods that can be called either locally or remotely. The lease is obtained by calling the static method RemotingServices. GetLifetimeService..

public class RemotingServices
{
  public static Object GetLifetimeService(MarshalByRefObject obj);
}

The GetLifetimeService method parameter is the reference to the object.

Object providing their own leases

Object can provide their own lease and so control their own lifetime. They do this by overriding the InitializeLifetimeService method provided on MarshalByRefObject.

public class Foo : MarshalByRefObject
{
  public override Object InitializeLifetimeService()
  {
   ILease lease = (ILease)base.InitializeLifetimeService();
   if (lease.CurrentState == LeaseState.Initial)
   {
       lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
       lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
        lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
   }
     return lease;
  }
}

Property sets will only work when a lease is in the initial state. A lease is in the initial state which it is created by InitializeLifetimeService. It is placed in the Active state when the object is marshaled.

When the ILease lease = (ILease)base.InitializeLifetimeService(); is called within InitializeLifetimeService, either an existing lease is returned for this object, or if no lease exists, a new lease is returned. Only if a new lease is returned can the lease properties be set. An exception will occur if an attempt is made to set an existing lease. A new lease has the state LeaseState.Initial, an existing lease has the state LeaseState.Active or LeaseState.Expired.

The only call that counts for the Lifetime service is the call to InitializeLifetimeService from the Remoting Lifetime Lease infrastructure (implemented as an AppDomain property), this the call that acivates the lease. Someone else can call InitializeLifetimeService and create a lease, but that lease will stay in its initial state until it is returned to the Remoting Lease Infrastructure. An existing lease that is not in the initial state cannot be set with new values, although it can be returned to Remoting Lifetime Lease infrastructure for use use by the new sink for RenewOnCall when an invocation on the object occurs. This allows multiple sinks for an object to point to the same lease.

No lease will be created if the leasetime is zero or a null lease is returned lease.InitialLeaseTime = TimeSpan.Zero; or return null. No sink will be created if the RenewOnCallTime is Zero, but a lease will be created. lease.RenewOnCallTime = TimeSpan.Zero;

Initializing a Lease

A lease is created when activating an object. The values for fields’ RenewOnCall, sponsorship, and leaseTime are supplied in the lease’s constructor. These values can come from multiple places:

The above list also gives the order in which the values are found.

The Activation call can also supply a list of sponsors for the lease. Additional sponsors can be added any time the lease is executing.

The leaseTime can be extended while the object’s clients are executing.

Explicit client Lease Renewal (Renew)

The client of the remote object can explicitly renew the lease. This is done as follows:

Implicit client Lease Renewal (RenewOnCall)

A client can implicitly renew a lease each time a method on the object is invoked. This is done as follows:

Initializing the object

Calling the object

Explicit Remote Object Renewal Request using Sponsorship

A Lease on a remote object can explicitly ask a sponsor to renew a lease. This is done as follows:

Registering a Sponsor

The sponsor is registered during activation or

The sponsor registers itself

Invoking the sponsor

The Lease Manager has a thread that periodically examines all the leases it manages. When a leases time has expired and the lease has sponsors it performs the following:

Terminating the Object

When a lease is in the Expired state, the LeaseManager will terminate it. The lease’s reference is removed from the LeaseManager and also the object reference is removed from the ID table. The Garbage Collector will then remove the lease, the object, and the remoting pipeline.

Once a lease is in the Expired state, no further lease messages or sponsor returns will be accepted.

Reachability of Sponsors

In order for the remote object to call back to the sponsor, it must be reachable by the remote object. The Web and firewalls can make it difficult to establish a client/server relationship from the remote object back to the client. The sponsor doesn’t need to be in the same location as the client, so it can be in a part of the network reachable by the remote object.

Automatic Lease Renewal (Not in PDC)

A possible renewal policy is to have a renewal agent rather then the client renew the lease. The client can specify a policy to keep alive the remote object for as long as the client is alive. The renewal agent will periodically renew the lease on behalf of the client.

Automatic Lease Sponsorship (Not in PDC)

A possible sponsorship policy is to have a sponsor agent automatically supply sponsors for one or more clients. The sponsor will renew the lease when asked until the clients are no longer alive.

Performance Improvements over DCOM

DCOM uses reference counting and pinging to keep a remote object alive. LDGC can support higher performance then DCOM because of the following:

Cycle Elimination (Not in PDC)

LDGC as currently described will not detect cycles. That is the clients and sponsors will renew the lease, but none of the clients or sponsors are rooted in an actively executing application. With the help of the garbage collector at each network node, cycle detection can be added to LDGC.