public virtual void SwitchToSlave() { _globalState = IdGeneratorState.Slave; foreach (HaIdGenerator generator in _generators.Values) { generator.SwitchToSlave(_master.cement()); } }
public virtual void SwitchToMaster() { _globalState = IdGeneratorState.Master; foreach (HaIdGenerator generator in _generators.Values) { generator.SwitchToMaster(); } }
private static IdGeneratorState CopyState(IdGeneratorState idGeneratorState) { return(new IdGeneratorState( idGeneratorState.DataType, idGeneratorState.LastId, idGeneratorState.IsLocked, idGeneratorState.LockTime, idGeneratorState.LockSessionId)); }
internal HaIdGenerator(HaIdGeneratorFactory outerInstance, IdGenerator initialDelegate, File fileName, int grabSize, IdType idType, IdGeneratorState initialState, long maxId) { this._outerInstance = outerInstance; Delegate = initialDelegate; this.FileName = fileName; this.GrabSize = grabSize; this.IdType = idType; this.State = initialState; this.MaxId = maxId; outerInstance.log.Debug("Instantiated HaIdGenerator for " + initialDelegate + " " + idType + ", " + initialState); }
internal virtual void SwitchToSlave(Master master) { string previousDelegate = Delegate.ToString(); long highId = Delegate.HighId; // The .id file is open and marked DIRTY Delegate.delete(); // The .id file underneath is now gone Delegate = new SlaveIdGenerator(IdType, highId, master, outerInstance.log, outerInstance.requestContextFactory); outerInstance.log.Debug("Instantiated slave delegate " + Delegate + " of type " + IdType + " with highid " + highId + " uniquetempvar."); State = IdGeneratorState.Slave; }
public void ExpiredLockIsReleased() { var idGeneratorState = new IdGeneratorState("Sample", 2, true, DateTime.UtcNow - TimeSpan.FromMinutes(3), Guid.NewGuid()); var collection = CreateIdGeneratorStateRepository(idGeneratorState); var sut = new IntegerIdGenerator(collection.Object) { MaxTryTime = TimeSpan.FromSeconds(5) }; sut.GetIdsAsync(DataType, 1).Wait(); idGeneratorState = collection.Object.GetForDataTypeAsync(DataType).Result; Assert.That(idGeneratorState.IsLocked, Is.False); Assert.That(idGeneratorState.LockTime, Is.Null); Assert.That(idGeneratorState.LockSessionId, Is.Null); Assert.That(idGeneratorState.LastId, Is.EqualTo(3)); }
internal virtual void SwitchToMaster() { if (State == IdGeneratorState.Slave) { string previousDelegate = Delegate.ToString(); long highId = Delegate.HighId; Delegate.delete(); outerInstance.localFactory.Create(FileName, highId, false); Delegate = outerInstance.localFactory.Open(FileName, GrabSize, IdType, () => highId, MaxId); outerInstance.log.Debug("Instantiated master delegate " + Delegate + " of type " + IdType + " with highid " + highId + " uniquetempvar."); } else { outerInstance.log.Debug("Keeps " + Delegate); } State = IdGeneratorState.Master; }
private static Mock <IIdGeneratorStateRepository> CreateIdGeneratorStateRepository(IdGeneratorState initialState) { var idGeneratorState = initialState; var collection = new Mock <IIdGeneratorStateRepository>(); collection.Setup(x => x.GetForDataTypeAsync(DataType)).Returns(Task.FromResult(CopyState(idGeneratorState))); collection .Setup(x => x.TryGetLockAsync(It.IsAny <IdGeneratorState>())) .Returns <IdGeneratorState>( newState => { if (idGeneratorState.IsLocked) { return(Task.FromResult(false)); } idGeneratorState = CopyState(newState); return(Task.FromResult(true)); }); collection .Setup(x => x.ReleaseLockAsync(It.IsAny <IdGeneratorState>(), It.IsAny <Guid>())) .Callback <IdGeneratorState, Guid>( (newState, guid) => { if (idGeneratorState.LockSessionId == guid) { idGeneratorState = CopyState(newState); } }); return(collection); }