Esempio n. 1
0
		public async Task KeepPollingForLeaderInfo(CancellationToken token) {
			while (!token.IsCancellationRequested) {
				try {
					var info = await LeaderInfo.Get(_storage);
					if (info == null) {
						_client = null;
						await Task.Delay(500, token);
						continue;
					}
					var newEndpoint = info.GetEndpoint();
					if (_endpoint != newEndpoint) {
						Log.Information("Detected new leader {endpoint}", newEndpoint);
						_endpoint = newEndpoint;
						var password = _storage.GetSysPassword();
						_client = new Client(_endpoint, Constants.ClusterNodeUser, password);
					}

					await Task.Delay(3500, token);
				}
				catch (StorageException ex) {
					Log.Warning(ex, "Failed to refresh leader info");
					token.WaitHandle.WaitOne(1000);
				}
			}
		}
Esempio n. 2
0
		static void Main(string[] args) {
			const string url = "http://127.0.0.1:8001";
			const string login = Constants.DefaultLogin;
			const string password = Constants.DefaultPassword;

			using (var client = new Client(url, login, password)) {
				KeepPostingForever(client).Wait();
			}
		}
Esempio n. 3
0
		public static async Task KeepPostingForever(Client client) {
			while (true) {
				var message = Message.Create("test", new byte[20]);

				var response = await client.PostMessagesAsync("test", new[] {message});
				Console.WriteLine("Wrote at position {0}", response.Position);

				await Task.Delay(1000);
			}
		}
Esempio n. 4
0
		static async Task RunNonCompetingPush(int i, int steps, int batchSize, Client client, CancellationToken token) {

			var streamName = "test" + i;
			var reader = await client.GetMessageReaderAsync(streamName);

			var starting = reader.GetPosition();

			
			for (int j = 0; j < steps; j++) {

				if (token.IsCancellationRequested) {
					return;
				}
				var messages = new List<Message>();
				for (int k = 0; k < batchSize; k++) {
					var contract = string.Format("message{0}-{1}", j, k);
					var seq = j * batchSize + k;
					messages.Add(Message.Create(contract, GenerateMessage(seq)));
				}
				var started = Stopwatch.StartNew();
				await client.PostMessagesAsync(streamName, messages);
				// time
				Interlocked.Increment(ref BatchesPosted);
				Interlocked.Add(ref MessagesPosted, batchSize);
				Interlocked.Add(ref MillisecondsPosting, started.ElapsedMilliseconds);

			}
			Console.WriteLine("Task {0} done writing", i);


			long position = starting;
			for (int j = 0; j < (steps); j++) {
				var result = await reader.GetMessagesAsync(token, position, batchSize);
				
				for (int k = 0; k < batchSize; k++) {
					var seq = j * batchSize + k;

					var msg = result.Messages[k];
					var expect = string.Format("message{0}-{1}", j, k);
					var body = GenerateMessage(seq);
					if (msg.KeyAsString() != expect) {
						throw new InvalidOperationException("Unexpected contract");
					}
					if (!body.SequenceEqual(msg.Value)) {
						throw new InvalidOperationException("Unexpected data");
					}
				}

				position = result.NextOffset;
			}
			Console.WriteLine("Task {0} done checking", i);
		}
Esempio n. 5
0
        public void Run(CancellationToken token)
        {
            var connectFailure = 0;
            while (!token.IsCancellationRequested) {
                try {
                    var localStoreIsEmpty = _store.GetCurrentVersion() == 0;
                    if (localStoreIsEmpty) {
                        token.WaitHandle.WaitOne(TimeSpan.FromSeconds(30));
                        continue;
                    }

                    _log.Information("Starting ES replication to {stream}", _streamName);

                    using (var conn = new Client(_config.Host, _config.Login, _config.Password)) {
                        connectFailure = 0;

                        var lastReplicatedEvent = _checkpoint.GetOrInitPosition();

                        while (!token.IsCancellationRequested) {
                            if (lastReplicatedEvent == _store.GetCurrentVersion()) {
                                // no work to do, so sleep and continue
                                token.WaitHandle.WaitOne(500);
                                continue;
                            }

                            var keys = _store.ReadRecords(lastReplicatedEvent, 1000).ToList();
                            var remoteEvents = keys.Select(MessageToWrite).ToList();
                            conn.PostMessagesAsync(_streamName, remoteEvents).Wait(token);

                            lastReplicatedEvent = keys.Last().StoreVersion;
                            _checkpoint.Update(lastReplicatedEvent);
                        }
                    }
                }
                catch (Exception ex) {
                    if (connectFailure == 0) {
                        _log.Error(ex, "Write connection failure");
                    }
                    connectFailure += 1;
                    token.WaitHandle.WaitOne(TimeSpan.FromMinutes(1));
                }
            }
        }
Esempio n. 6
0
		static async Task RunAsync() {
			using (var client = new Client("http://127.0.0.1:8001", Constants.DefaultLogin, Constants.DefaultPassword)) {
				
				// consumer
				var checkpoint = new MemoryCheckpointReaderWriter();
				var consumer = new ConsumerSample(checkpoint, client);

				var task = Task.Run(() => consumer.Run(CancellationToken.None));

				for (int i = 0; i < 10; i++) {
					var message = Message.Create("test", new byte[20]);

					var response = await client.PostMessagesAsync("teststream", new[] { message });
					Console.WriteLine(response.Position);

					await Task.Delay(1000);

				}

				await task;
			}
		}
Esempio n. 7
0
		static void Main(string[] args) {
			var threads = GetIntParam(args, "threads", 1);
			var url = GetStringParam(args, "url", "http://localhost:8001");
			var pass = GetStringParam(args, "pass", Constants.DefaultPassword);
			var login = GetStringParam(args, "login", Constants.DefaultLogin);
			var steps = GetIntParam(args, "steps", 100);
			var batchSize = GetIntParam(args, "batch", 100);

			Console.WriteLine(new {threads, url, pass, login, steps, batchSize});

			var client = new Client(url, login, pass);
			var list = new List<Task>();
			var cts = new CancellationTokenSource();

			for (var i = 0; i < threads; i++) {
				var taskId = i;
				
				list.Add(RunNonCompetingPush(taskId, steps, batchSize, client, cts.Token).ContinueWith(task => {
					if (task.IsFaulted) {
						Console.WriteLine(task.Exception);
					}
				}));
			}

			list.Add(Task.Factory.StartNew(() => {
				while (!cts.Token.IsCancellationRequested) {
					cts.Token.WaitHandle.WaitOne(5000);
					PrintStats();
				}
			}));

			Console.WriteLine("Waiting for tasks to stop");
			Task.WaitAll(Enumerable.ToArray(list));
			PrintStats();

		}
Esempio n. 8
0
		public ConsumerSample(ICheckpointWriter checkpoint, Client client)
		{
			_checkpoint = checkpoint;
			_client = client;
		}