示例#1
0
        /// <summary>
        /// ReadAll
        /// </summary>
        /// <returns></returns>
        public async Task <MembershipTableData> ReadAll()
        {
            try
            {
                using (var scope = _services.CreateScope())
                {
                    var db = scope
                             .ServiceProvider
                             .GetService <OrleansEFContext>();

                    var rows = await db
                               .Memberships
                               .AsNoTracking()
                               .Where(a =>
                                      a.DeploymentId == _clusterOptions.ClusterId
                                      )
                               .ToListAsync();

                    return(OrleansEFMapper.Map(rows));
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadAll), e);
                throw;
            }
        }
示例#2
0
        /// <summary>
        /// GetGateways
        /// </summary>
        /// <returns></returns>
        public async Task <IList <Uri> > GetGateways()
        {
            try
            {
                var siloData = await _db
                               .Memberships
                               .Where(membership =>
                                      membership.Status == (int)SiloStatus.Active &&
                                      membership.DeploymentId == _clusterOptions.ClusterId
                                      )
                               .Select(membership => new {
                    membership.Address,
                    membership.ProxyPort,
                    membership.Generation
                })
                               .ToListAsync();

                var gatewayUris = siloData.Select(a =>
                                                  OrleansEFMapper.Map(
                                                      a.Address,
                                                      (int)a.ProxyPort,
                                                      a.Generation
                                                      ).ToGatewayUri()
                                                  ).ToList();

                return(gatewayUris);
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(GetGateways), e);
                throw;
            }
        }
示例#3
0
        /// <summary>
        /// UpdateRow
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="etag"></param>
        /// <param name="tableVersion"></param>
        /// <returns></returns>
        public async Task <bool> UpdateRow(
            MembershipEntry entry,
            string etag,
            TableVersion tableVersion
            )
        {
            try
            {
                if (entry == null)
                {
                    throw new ArgumentNullException(nameof(entry));
                }

                if (string.IsNullOrWhiteSpace(etag))
                {
                    throw new ArgumentNullException(nameof(etag));
                }

                if (tableVersion == null)
                {
                    throw new ArgumentNullException(nameof(tableVersion));
                }

                using (await _semaphore.DisposableWaitAsync())
                {
                    var row = await _orleansEfContext
                              .Memberships
                              .FirstOrDefaultAsync(a =>
                                                   a.DeploymentId == _clusterOptions.ClusterId &&
                                                   a.Address == entry.SiloAddress.Endpoint.Address.ToString() &&
                                                   a.Port == (uint)entry.SiloAddress.Endpoint.Port &&
                                                   a.Generation == entry.SiloAddress.Generation
                                                   );

                    if (row == null)
                    {
                        throw new OrleansEFMembershipException.RowNotFound(
                                  entry.SiloAddress
                                  );
                    }

                    OrleansEFMapper.Map(entry, row);

                    await _orleansEfContext.SaveChangesAsync();

                    _logger.Info(
                        0, "{0}: {1}", nameof(UpdateRow),
                        $"updated silo {entry.SiloAddress.Endpoint.ToString()}"
                        );

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(UpdateRow), e);
                throw;
            }
        }
        public async Task <string> UpsertRow(
            ReminderEntry entry
            )
        {
            var row = await _db
                      .Reminders
                      .FirstOrDefaultAsync(a =>
                                           a.ServiceId == _clusterOptions.Value.ServiceId &&
                                           a.GrainId == entry.GrainRef.ToKeyString() &&
                                           a.ReminderName == entry.ReminderName
                                           );

            var nullRow =
                row == null;

            if (nullRow)
            {
                row = new OrleansEFReminder
                {
                    Id   = Guid.NewGuid(),
                    ETag = entry.ETag,
                };

                _db.Reminders.Add(row);
            }

            if (entry.ETag != row.ETag)
            {
                throw new OrleansEFReminderException.EtagMismatch(
                          $"etag mismatch. " +
                          $"grainId: {entry.GrainRef.ToKeyString()} " +
                          $"reminderName: {entry.ReminderName}"
                          );
            }

            var serviceId = _clusterOptions
                            .Value
                            .ServiceId;

            row = OrleansEFMapper
                  .Map(entry, row, serviceId: serviceId);

            row.ETag = Guid
                       .NewGuid()
                       .ToString();

            await _db.SaveChangesAsync();

            return(row.ETag);
        }
示例#5
0
        /// <summary>
        /// InsertRow
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="tableVersion"></param>
        /// <returns></returns>
        public async Task <bool> InsertRow(MembershipEntry entry, TableVersion tableVersion)
        {
            try
            {
                if (entry == null)
                {
                    throw new ArgumentNullException(nameof(entry));
                }

                if (tableVersion == null)
                {
                    throw new ArgumentNullException(nameof(tableVersion));
                }

                using (var scope = _services.CreateScope())
                {
                    var db = scope
                             .ServiceProvider
                             .GetService <OrleansEFContext>();

                    var newRow = OrleansEFMapper.Map(entry);

                    newRow.Id           = Guid.NewGuid();
                    newRow.DeploymentId = _clusterOptions.ClusterId;
                    newRow.Generation   = entry.SiloAddress.Generation;
                    newRow.Address      = entry.SiloAddress.Endpoint.Address.ToString();
                    newRow.Port         = entry.SiloAddress.Endpoint.Port;

                    db.Memberships.Add(newRow);

                    await db.SaveChangesAsync();

                    _logger.Info(
                        0, "{0}: {1}", nameof(InsertRow),
                        $"inserted silo with address {entry.SiloAddress.Endpoint.ToString()} to membership table"
                        );

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(InsertRow), e);
                throw;
            }
        }
        public async Task <ReminderTableData> ReadRows(
            GrainReference key
            )
        {
            var rows = await _db
                       .Reminders
                       .Where(a =>
                              a.ServiceId == _clusterOptions.Value.ServiceId &&
                              a.GrainId == key.ToKeyString()
                              )
                       .ToListAsync();

            return(OrleansEFMapper.Map(
                       _grainReferenceConverter,
                       rows
                       ));
        }
示例#7
0
        /// <summary>
        /// ReadRow
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <MembershipTableData> ReadRow(SiloAddress key)
        {
            try
            {
                if (key == null)
                {
                    throw new ArgumentNullException(nameof(key));
                }

                using (var scope = _services.CreateScope())
                {
                    var db = scope
                             .ServiceProvider
                             .GetService <OrleansEFContext>();

                    var rows = await db
                               .Memberships
                               .AsNoTracking()
                               .Where(a =>
                                      a.DeploymentId == _clusterOptions.ClusterId &&
                                      a.Address == key.Endpoint.Address.ToString() &&
                                      a.Port == (uint)key.Endpoint.Port &&
                                      a.Generation == key.Generation
                                      )
                               .ToListAsync();

                    if (rows.Count == 0)
                    {
                        _logger.Warn(
                            0, "{0}: {1}", nameof(ReadRow),
                            $"no rows with silo address {key.Endpoint.ToString()} found"
                            );
                    }

                    return(OrleansEFMapper.Map(rows));
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadRow), e);
                throw;
            }
        }
示例#8
0
        /// <summary>
        /// InsertRow
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="tableVersion"></param>
        /// <returns></returns>
        public async Task <bool> InsertRow(MembershipEntry entry, TableVersion tableVersion)
        {
            await contextThreadLock.WaitAsync();

            try
            {
                if (entry == null)
                {
                    throw new ArgumentNullException(nameof(entry));
                }

                if (tableVersion == null)
                {
                    throw new ArgumentNullException(nameof(tableVersion));
                }

                var newRow = OrleansEFMapper.Map(entry);
                newRow.DeploymentId = _clusterOptions.ClusterId;
                newRow.Generation   = entry.SiloAddress.Generation;
                newRow.Address      = entry.SiloAddress.Endpoint.Address.ToString();
                newRow.Port         = entry.SiloAddress.Endpoint.Port;

                _db.Memberships.Add(newRow);

                await _db.SaveChangesAsync();

                _logger.Info(
                    0, "{0}: {1}", nameof(InsertRow),
                    $"inserted silo with address {entry.SiloAddress.Endpoint.ToString()} to membership table"
                    );

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(InsertRow), e);
                throw;
            }
            finally
            {
                contextThreadLock.Release();
            }
        }
示例#9
0
        /// <summary>
        /// ReadAll
        /// </summary>
        /// <returns></returns>
        public async Task <MembershipTableData> ReadAll()
        {
            await contextThreadLock.WaitAsync();

            try
            {
                return(OrleansEFMapper.Map(
                           await _db.Memberships.Where(a =>
                                                       a.DeploymentId == _clusterOptions.ClusterId
                                                       ).ToListAsync()
                           ));
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadAll), e);
                throw;
            }
            finally
            {
                contextThreadLock.Release();
            }
        }
示例#10
0
        /// <summary>
        /// ReadRow
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <MembershipTableData> ReadRow(SiloAddress key)
        {
            await contextThreadLock.WaitAsync();

            try
            {
                if (key == null)
                {
                    throw new ArgumentNullException(nameof(key));
                }

                var rows = await _db.Memberships.Where(a =>
                                                       a.DeploymentId == _clusterOptions.ClusterId &&
                                                       a.Address == key.Endpoint.Address.ToString() &&
                                                       a.Port == (uint)key.Endpoint.Port &&
                                                       a.Generation == key.Generation
                                                       ).ToListAsync();

                if (rows.Count == 0)
                {
                    _logger.Warn(
                        0, "{0}: {1}", nameof(ReadRow),
                        $"no rows with silo address {key.Endpoint.ToString()} found"
                        );
                }

                return(OrleansEFMapper.Map(rows));
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadRow), e);
                throw;
            }
            finally
            {
                contextThreadLock.Release();
            }
        }
示例#11
0
        /// <summary>
        /// Return all rows that have their GrainReference's.GetUniformHashCode() in the range (start, end]
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task <ReminderTableData> ReadRows(
            uint begin,
            uint end
            )
        {
            try
            {
                var iBegin = (int)begin;
                var iEnd   = (int)end;

                using (var scope = _services.CreateScope())
                {
                    var db = scope
                             .ServiceProvider
                             .GetService <OrleansEFContext>();

                    var rows = await db
                               .Reminders
                               .AsNoTracking()
                               .Where(a =>
                                      a.ServiceId == _clusterOptions.ServiceId &&
                                      a.GrainHash >= iBegin &&
                                      a.GrainHash <= iEnd
                                      )
                               .ToListAsync();

                    return(OrleansEFMapper.Map(
                               _grainReferenceConverter,
                               rows
                               ));
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadRows), e);
                throw;
            }
        }
示例#12
0
        /// <inheritdoc />
        /// <summary>
        /// ReadAll
        /// </summary>
        /// <returns></returns>
        public async Task <MembershipTableData> ReadAll()
        {
            try
            {
                using (await _semaphore.DisposableWaitAsync())
                {
                    var rows = await _orleansEfContext
                               .Memberships
                               .AsNoTracking()
                               .Where(a =>
                                      a.DeploymentId == _clusterOptions.ClusterId
                                      )
                               .ToListAsync();

                    return(OrleansEFMapper.Map(rows));
                }
            }
            catch (Exception e)
            {
                _logger.Error(0, nameof(ReadAll), e);
                throw;
            }
        }
        public async Task <ReminderEntry> ReadRow(
            GrainReference grainRef,
            string reminderName
            )
        {
            var row = await _db
                      .Reminders
                      .FirstOrDefaultAsync(a =>
                                           a.ServiceId == _clusterOptions.Value.ServiceId &&
                                           a.GrainId == grainRef.ToKeyString() &&
                                           a.ReminderName == reminderName
                                           );

            if (row == null)
            {
                return(null);
            }

            return(OrleansEFMapper.Map(
                       _grainReferenceConverter,
                       row
                       ));
        }