private static async Task RunAsync(IHazelcastClient client, CancellationToken cancellationToken) { // 'await using' ensure that both the client and the map will be disposed before the method returns await using var c = client; await using var map = await client.GetDictionaryAsync <string, int>("test-map"); // loop while not canceled while (!cancellationToken.IsCancellationRequested) { // pretend to do some work var i = await map.GetAsync("foo"); i += 1; await map.SetAsync("foo", i); Console.WriteLine(i); try { await Task.Delay(1000, cancellationToken); } catch (OperationCanceledException) { // expected } } await client.DestroyAsync(map); }
public async Task TearDown() { if (_client != null) { await _client.DisposeAsync(); } _client = null; }
public async Task Init() { _client = await CreateAndStartClientAsync(); var client = _client as HazelcastClient; Assert.That(client, Is.Not.Null); SerializationService = client.SerializationService; }
private static async Task RunTask(IHazelcastClient client, CancellationToken token, int id, int entryCount, ILogger logger) { logger.LogInformation($"Thread {id}: start"); IHMap <string, string> map = null; while (!token.IsCancellationRequested) { try { map = await client.GetMapAsync <string, string>("soak1").ConfigureAwait(false); break; } catch (Exception ex) { logger.LogError($"Thread {id} failed to acquire the map ({ex.GetType().Name}: {ex.Message}), abort"); return; } } logger.LogInformation($"Thread {id}: acquired the map"); while (!token.IsCancellationRequested) { try { var key = RandomProvider.Random.Next(0, entryCount).ToString(); var operation = RandomProvider.Random.Next(0, 100); if (operation < 30) { await map.GetAsync(key).ConfigureAwait(false); } else if (operation < 60) { await map.PutAsync(key, RandomProvider.Random.Next().ToString()).ConfigureAwait(false); } else if (operation < 80) { await map.GetValuesAsync(Predicates.Value().IsBetween(0, 10)).ConfigureAwait(false); } else { await map.ExecuteAsync(new UpdateEntryProcessor(key), key).ConfigureAwait(false); } _reports[id] += 1; } catch (Exception ex) { logger.LogError($"Thead {id} caught {ex.GetType().Name}: {ex.Message}"); } } }
public async Task TearDown() { if (_dictionary != null) { await _dictionary.DestroyAsync(); } _dictionary = null; if (_client != null) { await _client.DisposeAsync(); } _client = null; }
/// <summary> /// Generates a random key that maps to a partition. /// </summary> /// <param name="client">The client.</param> /// <param name="partitionId">The identifier of the partition.</param> /// <returns>A random key that maps to the specified partition.</returns> protected object GenerateKeyForPartition(IHazelcastClient client, int partitionId) { var clientInternal = (HazelcastClient)client; while (true) { var randomKey = TestUtils.RandomString(); var randomKeyData = clientInternal.SerializationService.ToData(randomKey); if (clientInternal.Cluster.Partitioner.GetPartitionId(randomKeyData.PartitionHash) == partitionId) { return(randomKey); } } }
public async Task TearDown() { if (_map != null) { await _map.DestroyAsync(); } _map = null; if (_client != null) { await _client.DisposeAsync(); } _client = null; }
public async Task Init() { _client = await CreateAndStartClientAsync(); var client = _client as HazelcastClient; Assert.That(client, Is.Not.Null); SerializationService = client.SerializationService; _valuePut = _assertionViolationCount = 0; _stop = false; _name = "nc-" + TestUtils.RandomString(); }
public async Task Init() { _client = await CreateAndStartClientAsync(); var client = _client as HazelcastClient; Assert.That(client, Is.Not.Null); SerializationService = client.SerializationService; _dictionary = await _client.GetDictionaryAsync <object, object>("nc-" + TestUtils.RandomString()); var nearCache = GetNearCache(_dictionary); Assert.That(nearCache, Is.InstanceOf <NearCaching.NearCache>()); }
/// <inheritdoc /> public async Task SubscribeAsync(IHazelcastClient hazelcastClient, CancellationToken cancellationToken) { if (_subscribeAsync != null) { await _subscribeAsync(hazelcastClient, cancellationToken).CfAwait(); } else { var subscriber = _subscriber ?? (_type == null ? ServiceFactory.CreateInstance <IHazelcastClientEventSubscriber>(_typename, null) : ServiceFactory.CreateInstance <IHazelcastClientEventSubscriber>(_type, null)); await subscriber.SubscribeAsync(hazelcastClient, cancellationToken).CfAwait(); } }
/// <summary> /// Shuts a member down and wait until its connection closes. /// </summary> /// <param name="rc">The remote controller.</param> /// <param name="client">The Hazelcast client.</param> /// <param name="cluster">The cluster.</param> /// <param name="member">The member.</param> /// <remarks> /// <para>This works even if the member is the last member of the cluster.</para> /// </remarks> public static async Task StopMemberWaitClosedAsync(this IRemoteControllerClient rc, IHazelcastClient client, Cluster cluster, Member member) { var clientInternal = (HazelcastClient)client; var closed = new SemaphoreSlim(0); var memberId = new Guid(member.Uuid); var subscriptionId = await clientInternal.SubscribeAsync(on => on .ConnectionClosed((sender, args) => { // we don't have this yet, so just trust it's the ok connection //if (args.Connection.MemberId == memberId) closed.Release(); closed.Release(); })) .CfAwait(); await rc.StopMemberAsync(cluster, member).CfAwait(); await closed.WaitAsync(TimeSpan.FromSeconds(120)).CfAwait(); await clientInternal.UnsubscribeAsync(subscriptionId).CfAwait(); }
public async Task TearDown() { await _client.DisposeAsync(); _client = null; }
public ValueTask HandleAsync(IHazelcastClient hazelcastClient, TArgs args) => _handler(hazelcastClient, args);
/// <summary> /// Shuts a member down and wait until it is removed. /// </summary> /// <param name="rc">The remote controller.</param> /// <param name="client">The Hazelcast client.</param> /// <param name="cluster">The cluster.</param> /// <param name="member">The member.</param> public static async Task StopMemberWaitRemovedAsync(this IRemoteControllerClient rc, IHazelcastClient client, Cluster cluster, Member member) { var clientInternal = (HazelcastClient)client; var removed = new SemaphoreSlim(0); var subscriptionId = await clientInternal.SubscribeAsync(on => on .MemberRemoved((sender, args) => { removed.Release(); })) .CAF(); await rc.StopMemberAsync(cluster, member).CAF(); await removed.WaitAsync(TimeSpan.FromSeconds(120)).CAF(); await clientInternal.UnsubscribeAsync(subscriptionId).CAF(); }
public Task SubscribeAsync(IHazelcastClient client, CancellationToken cancellationToken) { Ctored = true; return(Task.CompletedTask); }
public Worker(IHazelcastClient client, ILogger <Worker> logger) { _client = client; _logger = logger; }
/// <summary> /// Starts a new member and wait until it is added. /// </summary> /// <param name="rc">The remote controller.</param> /// <param name="client">The Hazelcast client.</param> /// <param name="cluster">The cluster.</param> /// <param name="expectedPartitionOwnersCount">The expected number of partition owners.</param> /// <returns>The new member.</returns> public static async Task <Member> StartMemberWaitAddedAsync(this IRemoteControllerClient rc, IHazelcastClient client, Cluster cluster, int expectedPartitionOwnersCount) { var clientInternal = (HazelcastClient)client; var added = new SemaphoreSlim(0); var partitions = new SemaphoreSlim(0); var subscriptionId = await clientInternal.SubscribeAsync(on => on .MemberAdded((sender, args) => { added.Release(); }) .PartitionsUpdated((sender, args) => { partitions.Release(); })) .CAF(); var member = await rc.StartMemberAsync(cluster).CAF(); await added.WaitAsync(TimeSpan.FromSeconds(120)).CAF(); // trigger the partition table creation var map = await client.GetDictionaryAsync <object, object>("default").CAF(); _ = map.GetAsync(new object()); await partitions.WaitAsync(TimeSpan.FromSeconds(120)).CAF(); await clientInternal.UnsubscribeAsync(subscriptionId).CAF(); var partitioner = clientInternal.Cluster.Partitioner; var partitionsCount = partitioner.Count; var owners = new HashSet <Guid>(); for (var i = 0; i < partitionsCount; i++) { var owner = partitioner.GetPartitionOwner(i); if (owner != default) { owners.Add(owner); } } Assert.AreEqual(expectedPartitionOwnersCount, owners.Count); return(member); }