Example #1
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);
			}
		}
Example #2
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;
			}
		}
Example #3
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);
		}
Example #4
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));
                }
            }
        }