Example #1
0
        private async Task <IKeyValueStore> InitializeKeyValueStoreAsync(
            [NotNull] IEnumerable <IManagedProcess> managedProcesses,
            [NotNull] ClusterHeartBeat heartbeat)
        {
            string etcdAgentType = WellKnownAgentType.KeyValueStore.ToString();

            IEnumerable <IManagedProcess> kvsProcesses =
                managedProcesses.Where(m => m.AgentType.Equals(etcdAgentType));

            // For all well-known KVS agent processes...
            foreach (var kvsProcess in kvsProcesses)
            {
                // Check if they live (reviving them, if necessary):
                bool started = await heartbeat.CheckHeartBeatAsync(kvsProcess);

                if (started)
                {
                    // ... and try making contact
                    IServerProcess serverProcess = (IServerProcess)kvsProcess;

                    var keyValueStore = await EtcdKeyValueStore.TryConnectAsync(
                        serverProcess.HostName, serverProcess.Port, serverProcess.UseTls);

                    if (keyValueStore != null)
                    {
                        _logger.LogInformation("Using etcd based key-value store...");
                        return(keyValueStore);
                    }
                }
            }

            // None is configured or none is running or none is responding:
            _logger.LogInformation("Using in-memory key-value store...");
            return(new LocalKeyValueStore());
        }
Example #2
0
        public async Task CanPutTwice()
        {
            EtcdKeyValueStore etcdStore = await GetEtcdKeyValueStore();

            TestUtils.SinglePutGet(etcdStore, "same_key", "value1");
            TestUtils.SinglePutGet(etcdStore, "same_key", "value2");
        }
Example #3
0
        public async Task CannotPutEmptyStringKey()
        {
            EtcdKeyValueStore store = await GetEtcdKeyValueStore();

            Assert.Throws <ArgumentException>(() =>
            {
                TestUtils.SinglePutGet(store, string.Empty, "value");
            });
        }
Example #4
0
        public async Task CanPutEmptyStringValue()
        {
            EtcdKeyValueStore etcdStore = await GetEtcdKeyValueStore();

            TestUtils.SinglePutGet(etcdStore, "key_with_empty_value", string.Empty);

            Assert.Throws <ArgumentNullException>(() =>
            {
                TestUtils.SinglePutGet(etcdStore, "key_with_null_value", null);
            });
        }
Example #5
0
        public async Task CanPutAndRetrieve()
        {
            EtcdKeyValueStore store = await GetEtcdKeyValueStore();

            TestUtils.SinglePutGet(store, "key1", "value1");
            TestUtils.SinglePutGet(store, "key2", "value2");

            // Keys are case-sensitive:
            string notFound = string.Empty;

            TestUtils.AssertGet(store, "KEY1", notFound);
        }
Example #6
0
        private static async Task <IKeyValueStore> TryGetEtcdKeyValueStore()
        {
            foreach (var kvsAgent in KnownAgents.Get(WellKnownAgentType.KeyValueStore))
            {
                IKeyValueStore keyValueStore = await EtcdKeyValueStore.TryConnectAsync(kvsAgent);

                if (keyValueStore != null)
                {
                    return(keyValueStore);
                }
            }

            // Do not just try the default address - Etcd might be running with a different purpose

            return(null);
        }
Example #7
0
        public async Task CanDeleteKey()
        {
            EtcdKeyValueStore store = await GetEtcdKeyValueStore();

            const string key = "key_to_be_deleted";

            TestUtils.SinglePutGet(store, key, "value");

            store.Delete(key);

            TestUtils.AssertGet(store, key, string.Empty);

            // Delete non-existent key:
            store.Delete(key);

            TestUtils.AssertGet(store, key, string.Empty);
        }
Example #8
0
        private static async Task <EtcdKeyValueStore> GetEtcdKeyValueStore()
        {
            EtcdClient etcdClient =
                new EtcdClient("http://localhost:2379");

            var etcdStore = new EtcdKeyValueStore(etcdClient);

            // Warm up:
            Stopwatch watch     = Stopwatch.StartNew();
            bool      connected = await etcdStore.ConnectAsync();

            watch.Stop();

            Assert.IsTrue(connected);

            Console.WriteLine("Initial connect: {0}ms", watch.ElapsedMilliseconds);
            return(etcdStore);
        }
Example #9
0
        public async Task CanGetNonExistingKey()
        {
            EtcdKeyValueStore store = await GetEtcdKeyValueStore();

            TestUtils.AssertGet(store, "nonexistent_key", string.Empty);
        }
Example #10
0
        public async Task CanGetRange()
        {
            EtcdKeyValueStore store = await GetEtcdKeyValueStore();

            TestUtils.AssertCanGetRange(store);
        }