public Task <IStatefulGrain> GetGrain() { string primaryKey = null; bool renewlease = true; if (State.numInstances >= Parameters.NUM_REPLICAS) { var stubReplicas = State.replicas.Where(x => DateTime.Now - x.Value > Parameters.LEASE_DURATION); if (stubReplicas.Count() > 0) { primaryKey = stubReplicas.ElementAt(rnd.Next(0, stubReplicas.Count())).Key; } else { primaryKey = State.replicas.ElementAt(rnd.Next(0, Parameters.NUM_REPLICAS)).Key; renewlease = false; } } else { primaryKey = WITWDMetadataGrain.WITWD_REPLICA + State.numInstances++; } IWhereInTheWorldData grain = GrainFactory.GetGrain <IWhereInTheWorldData>(primaryKey); if (renewlease) { var leaseStartTime = DateTime.Now; grain.StartLease(leaseStartTime); State.replicas.Add(primaryKey, leaseStartTime); } return(Task.FromResult <IStatefulGrain>(grain)); }
public Task <IWhereInTheWorldData> GetNearestFullReplica() { // Adding some offset so that we do not pick replica whose lease is soon to expire var fullReplicas = State.replicas.Where(x => DateTime.Now - x.Value <= Parameters.LEASE_DURATION - new TimeSpan(0, 3, 0)); if (fullReplicas.Count() > 0) { string primaryKey = fullReplicas.ElementAt(rnd.Next(0, fullReplicas.Count())).Key; IWhereInTheWorldData grain = GrainFactory.GetGrain <IWhereInTheWorldData>(primaryKey); return(Task.FromResult <IWhereInTheWorldData>(grain)); } return(null); }