예제 #1
0
        public async Task CheckHeartBeat_TimerSetting_Valid()
        {
            var instanceDataStore = new DummyInstanceStore();
            var lockProviderMock  = new Mock <ILockProvider>();

            lockProviderMock.Setup(loc => loc.TryGetLockAsync("abc", LockType.Write, CancellationToken.None)).ReturnsAsync(true);
            var configProviderMock = new Mock <IConfigurationProvider>();

            _settings.Add("reset_time_sec", "5");
            configProviderMock.Setup(cp => cp.GetGlobalConfigurationAsNameValueCollection("montone_id_generator", "webfarm_settings", "heartbeat_settings")).Returns(_settings);

            var webFarm = new WebFarmConfiguration(instanceDataStore, lockProviderMock.Object, configProviderMock.Object);

            webFarm.Intialize();

            var instanceInfo = new InstanceInfo(webFarm.GetRegionId(), webFarm.GetZoneId(), webFarm.GetInstanceId(), DateTime.UtcNow);

            var datetime1 = instanceDataStore.InstanceList[instanceInfo.GetUniqueId()].UpdateTimestamp;

            Thread.Sleep(6000);

            var datetime2 = instanceDataStore.InstanceList[instanceInfo.GetUniqueId()].UpdateTimestamp;

            Assert.IsTrue(datetime2 > datetime1);
        }
예제 #2
0
        public async Task GetInstanceId_IncrementedId_Valid()
        {
            var instanceDataStoreMock = new Mock <IInstanceDataStore>();
            var lockProviderMock      = new Mock <ILockProvider>();

            lockProviderMock.Setup(loc => loc.TryGetLockAsync("abc", LockType.Write, CancellationToken.None)).ReturnsAsync(true);
            var configProviderMock = new Mock <IConfigurationProvider>();

            configProviderMock.Setup(cp => cp.GetGlobalConfiguration <int>("", "")).Returns(1800);
            var instanceList = new Dictionary <string, InstanceInfo>();

            var instance1 = new InstanceInfo(0, 0, 0, DateTime.UtcNow.Subtract(new TimeSpan(0, 30, 0)));

            var instance2 = new InstanceInfo(0, 0, 1, DateTime.UtcNow.Subtract(new TimeSpan(0, 15, 0)));

            instanceList.Add(instance1.GetUniqueId(), instance1);
            instanceList.Add(instance2.GetUniqueId(), instance2);

            instanceDataStoreMock.Setup(ds => ds.GetInstanceList()).Returns(instanceList);

            var webFarm = new WebFarmConfiguration(instanceDataStoreMock.Object, lockProviderMock.Object, configProviderMock.Object);

            webFarm.Intialize();
            var instanceId = webFarm.GetInstanceId();

            Assert.AreEqual(instanceId, (UInt32)2);
        }
예제 #3
0
        public async Task MultiRegion_AddSameInstanceId_Valid()
        {
            var instanceDataStoreMock = new Mock <IInstanceDataStore>();
            var lockProviderMock      = new Mock <ILockProvider>();

            lockProviderMock.Setup(loc => loc.TryGetLockAsync("abc", LockType.Write, CancellationToken.None)).ReturnsAsync(true);
            var configProviderMock = new Mock <IConfigurationProvider>();

            configProviderMock.Setup(cp => cp.GetGlobalConfiguration <int>("", "", "")).Returns(1800);

            var instanceList = new Dictionary <string, InstanceInfo>();

            var instance1 = new InstanceInfo(0, 0, 0, DateTime.UtcNow);
            var instance2 = new InstanceInfo(1, 0, 0, DateTime.UtcNow);

            instanceList.Add(instance1.GetUniqueId(), instance1);
            instanceList.Add(instance2.GetUniqueId(), instance2);

            configProviderMock.Setup(cp => cp.GetGlobalConfiguration <uint>("montone_id_generator", "webfarm_settings", "webfarm_regionid")).Returns(2);

            instanceDataStoreMock.Setup(ds => ds.GetInstanceList()).Returns(instanceList);

            var webFarm = new WebFarmConfiguration(instanceDataStoreMock.Object, lockProviderMock.Object, configProviderMock.Object);

            webFarm.Intialize();
            var instanceId = webFarm.GetInstanceId();

            Assert.AreEqual(instanceId, (UInt32)0);
        }
예제 #4
0
        public async Task Aerospike_AddOrUpdate_TimeStamp_Valid()
        {
            var instanceDS = new AerospikeInstanceDataStore(new ConfigurationProvider("montone_id_generator"));

            var timestamp = DateTime.UtcNow;
            var instance  = new InstanceInfo(10, 11, 23, timestamp);

            instanceDS.AddOrUpdate(instance);

            var instanceList = instanceDS.GetInstanceList();

            Assert.IsNotNull(instanceList);

            Assert.IsTrue(instanceList.Keys.Count > 0);

            Assert.AreEqual(timestamp.Hour, instanceList[instance.GetUniqueId()].UpdateTimestamp.Hour);
            Assert.AreEqual(timestamp.Minute, instanceList[instance.GetUniqueId()].UpdateTimestamp.Minute);
            Assert.AreEqual(timestamp.Second, instanceList[instance.GetUniqueId()].UpdateTimestamp.Second);
        }
예제 #5
0
        public Dictionary <string, InstanceInfo> GetInstanceList()
        {
            Dictionary <string, InstanceInfo> instancesInfo = new Dictionary <string, InstanceInfo>();

            var statement = new Statement()
            {
                Namespace = _settings[Keystore.AerospikeKeys.InstanceStore.Namespace],
                SetName   = _settings[Keystore.AerospikeKeys.InstanceStore.Set]
            };

            using (AerospikeClient client = GetClient())
            {
                var records = client.Query(null, statement);

                if (records == null)
                {
                    return(instancesInfo);
                }

                try
                {
                    while (records.Next())
                    {
                        Key    key          = records.Key;
                        Record record       = records.Record;
                        var    instanceInfo = new InstanceInfo()
                        {
                            RegionId        = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.RegionIdBin),
                            ZoneId          = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.ZoneIdBin),
                            InstanceId      = record.GetUInt(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin),
                            UpdateTimestamp = GetTimestamp(record.GetString(Keystore.AerospikeKeys.InstanceStore.TimestampBin))
                        };


                        instancesInfo[instanceInfo.GetUniqueId()] = instanceInfo;
                    }
                }
                finally
                {
                    records.Close();
                }

                return(instancesInfo);
            }
        }
예제 #6
0
 public void AddOrUpdate(InstanceInfo instanceinfo)
 {
     InstanceList[instanceinfo.GetUniqueId()] = instanceinfo;
 }
예제 #7
0
        public void AddOrUpdate(InstanceInfo instanceInfo)
        {
            if (instanceInfo == null)
            {
                return;
            }

            var policy = new WritePolicy()
            {
                recordExistsAction = RecordExistsAction.REPLACE
            };

            var key = new Key(_settings[Keystore.AerospikeKeys.InstanceStore.Namespace], _settings[Keystore.AerospikeKeys.InstanceStore.Set], instanceInfo.GetUniqueId());

            var regionBin   = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.RegionId);
            var zoneBin     = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.ZoneId);
            var instanceBin = new Bin(Keystore.AerospikeKeys.InstanceStore.InstanceIdBin, instanceInfo.InstanceId);

            var timestampBin = new Bin(Keystore.AerospikeKeys.InstanceStore.TimestampBin, instanceInfo.UpdateTimestamp.ToString("T"));

            using (AerospikeClient client = GetClient())
            {
                client.Put(policy, key, regionBin, zoneBin, instanceBin, timestampBin);
            }
        }