Experiment with Role.Instances Collection and creating all Role Instances Order

When there are multiple instance in a web role, I was trying to check if Role.Instances collection ordered consistently on all endpoints. I also wanted to be sure when instance count is increased by modifying service configuration, the new instances be added to the end of the collection or will it be random.

 

So I created a web role and changed instance count to 10. I also modified OnStart() function as below:

  public override bool OnStart()
 {
 foreach (RoleInstance roleInst in RoleEnvironment.CurrentRoleInstance.Role.Instances)
 {
 Trace.WriteLine("Current Time: " + DateTime.Now.TimeOfDay);
 Trace.WriteLine("Instance ID: " + roleInst.Id);
 Trace.WriteLine("Instance Role Name: " + roleInst.Role.Name);
 Trace.WriteLine("Instance Count: " + roleInst.Role.Instances.Count);
 Trace.WriteLine("Instance EndPoint Count: " + roleInst.InstanceEndpoints.Values.Count);
 Trace.WriteLine("IP : " + RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["EndPoint1"].IPEndpoint.Address);
 int port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["EndPoint1"].IPEndpoint.Port;
 Trace.WriteLine("Port: " + port);
 
 }
 return base.OnStart();
 }
 
 

I started application in the Compute Emulator so I can look the start time for each instance in my Web Role:

I wanted to check the start time of each instance so I collected the following info for each instance:

Role[#]

Role.Instance.ID

Start Order

Role[0]

Current Time: 15:07:05.9445984

Instance ID: deployment(341).WebRoleTest.ASPWebRole.0

IP : 127.0.0.1

Port: 5145

2

Role[1]

Current Time: 15:07:06.3676226

Instance ID: deployment(341).WebRoleTest.ASPWebRole.1

IP : 127.0.0.1

Port: 5146

5

Role[2]

Current Time: 15:07:06.4216257

Instance ID: deployment(341).WebRoleTest.ASPWebRole.2

IP : 127.0.0.1

Port: 5147

7

Role[3]

Current Time: 15:07:06.2376152

Instance ID: deployment(341).WebRoleTest.ASPWebRole.3

IP : 127.0.0.1

Port: 5148

4

Role[4]

Current Time: 15:07:05.9345979

Instance ID: deployment(341).WebRoleTest.ASPWebRole.4

IP : 127.0.0.1

Port: 5149

1

Role[5]

Current Time: 15:07:06.5746345

Instance ID: deployment(341).WebRoleTest.ASPWebRole.5

IP : 127.0.0.1

Port: 5150

9

Role[6]

Current Time: 15:07:06.2106137

Instance ID: deployment(341).WebRoleTest.ASPWebRole.6

IP : 127.0.0.1

Port: 5151

3

Role[7]

Current Time: 15:07:06.3846236

Instance ID: deployment(341).WebRoleTest.ASPWebRole.7

IP : 127.0.0.1

Port: 5152

6

Role[8]

Current Time: 15:07:06.5906354

Instance ID: deployment(341).WebRoleTest.ASPWebRole.8

IP : 127.0.0.1

Port: 5153

10

Role[9]

Current Time: 15:07:06.4746288

Instance ID: deployment(341).WebRoleTest.ASPWebRole.9

IP : 127.0.0.1

Port: 5154

8

 

Based on above I could create the following Instance order:

 

  • 1 deployment(341).WebRoleTest.ASPWebRole.4
  • 2 deployment(341).WebRoleTest.ASPWebRole.0
  • 3 deployment(341).WebRoleTest.ASPWebRole.6
  • 4 deployment(341).WebRoleTest.ASPWebRole.3
  • 5 deployment(341).WebRoleTest.ASPWebRole.1
  • 6 deployment(341).WebRoleTest.ASPWebRole.7
  • 7 deployment(341).WebRoleTest.ASPWebRole.2
  • 8 deployment(341).WebRoleTest.ASPWebRole.9
  • 9 deployment(341).WebRoleTest.ASPWebRole.5
  • 10 deployment(341).WebRoleTest.ASPWebRole.8

 

When I ran the same application a few times, the order was not consistent. Adding new instances was also changing the order. So you can see the current role instance is always first in the list however the ordering is very different.

 

So If you would need to create instance order list, the best would be to have each instance write an entity to a table when it starts up (with a timestamp or a row key that represents the current time). Then you’ll be able to construct an ordered list.