Npgsql在异步环境中无限期地锁定线程

时间:2017-05-10 18:34:04

标签: c# multithreading postgresql npgsql orleans

我正在努力寻找一个问题的答案,我一直在使用C#的流行Postgres库Npgsql。我不确定它是否完全是Npgsql的问题,但我怀疑这是因为我的代码非常直接。我看到的问题是:当我有时在Npgsql上调用异步方法时,并非所有时间,线程都被锁定。从我所知道的,这个问题是随机发生的。结果,我在微软奥尔良环境中运行(可能与找到解决方案有关),就是线程无限期地锁定,从而使得奥尔良的一个人能够做到这一点。工作线程无法处理工作。当我进行更多Npgsql调用时,这些锁定的线程会堆叠起来,最终Orleans系统因线程耗尽而被阻塞。

所以我真的不知道问题是什么,但因为锁定总是在同一个方法中发生,并且因为它似乎发生在Npgsql的某个子程序中,我认为它是'公平地进一步调查Npgsql。

这是我在奥尔良存储提供程序中使用的代码(处理系统持久层的特殊类。)

public class ProtobufSQL
{
    public static List<Tuple<string, object>> FlattenToSQLColumns(IMessage message, MessageDescriptor descriptor, string prefix = null)
    {
        var fields = descriptor.Fields.InDeclarationOrder();

        var columns = new List<Tuple<string, object>>();

        for (var i = 0; i < fields.Count; i++)
        {
            var field = fields[i];
            var columnName = field.Name.ToLower();

            if (field.Name == "id")
            {
                ByteString bytes = (ByteString)field.Accessor.GetValue(message);
                var uuid = new Guid(bytes.ToByteArray());
                columns.Add(new Tuple<string, object>("id", uuid));
            }
            else if (field.FieldType == FieldType.Message)
            {
                var embeddedDescriptor = field.MessageType;
                var embeddedMessage = field.Accessor.GetValue(message);
                if (field.IsRepeated)
                {
                    throw new Exception("Repeated complex types are not supported, create a foreign key reference in a new object instead.");
                }
                else
                {
                    columns.AddRange(FlattenToSQLColumns((IMessage)embeddedMessage, embeddedDescriptor, $"{columnName}."));
                }
            }
            else if (field.FieldType == FieldType.Group)
            {
                throw new Exception("Groups are not supported by ProtobufSQL.");
            }
            else
            {
                var columnValue = field.Accessor.GetValue(message);
                var key = prefix + columnName;
                if (field.IsRepeated)
                {
                    var enumerableColumnValue = columnValue as IEnumerable;
                    Type listTypeOf = enumerableColumnValue.GetType().GetGenericArguments()[0];
                    Type listType = typeof(List<>).MakeGenericType(listTypeOf);
                    dynamic valueList = Activator.CreateInstance(listType);
                    foreach (var item in enumerableColumnValue)
                    {
                        valueList.Add((dynamic)item);
                    }
                    columns.Add(new Tuple<string, object>(key, valueList.ToArray()));
                }
                else
                {
                    columns.Add(new Tuple<string, object>(key, columnValue));
                }
            }
        }

        return columns;
    }

    public static async Task<IMessage[]> DataReaderToType(Type type, DbDataReader reader)
    {
        var descriptor = (MessageDescriptor)type.GetProperty("Descriptor").GetValue(null);

        IList<IMessage> objects = new List<IMessage>();

        while (await reader.ReadAsync())
        {
            var obj = Activator.CreateInstance(type);
            TraverseDbRow(reader, descriptor, obj);
            objects.Add((IMessage)obj);
        }

        return objects.ToArray();
    }

    private static void TraverseDbRow(DbDataReader reader, MessageDescriptor descriptor, object obj, string prefix = null)
    {
        var fields = descriptor.Fields.InFieldNumberOrder();

        for (var i = 0; i < fields.Count; i++)
        {
            var field = fields[i];
            if (field.FieldType == FieldType.Message)
            {
                if (field.IsRepeated)
                {
                    // Repeated embedded types will be broken out into a separate table,
                    // so there's no need to handle them here.
                }
                else if (field.IsMap)
                {
                    throw new Exception("Maps are not yet supported by ProtobufSQL.");
                }
                else
                {
                    var embeddedDescriptor = field.MessageType;
                    var embeddedObj = Activator.CreateInstance(embeddedDescriptor.ClrType);
                    TraverseDbRow(reader, embeddedDescriptor, embeddedObj, $"{prefix}{field.Name}.");
                }
            }
            else if (field.FieldType == FieldType.Group)
            {
                throw new Exception("Groups are not supported by ProtobufSQL.");
            }
            else
            {
                var columnName = prefix + field.Name;
                try
                {
                    var columnValue = reader[columnName];
                    var propertyInfo = obj.GetType().GetProperty(field.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                    if (field.Name == "id")
                    {
                        var guid = (Guid)columnValue;
                        ByteString bytes = ByteString.CopyFrom(guid.ToByteArray());
                        propertyInfo.SetValue(obj, bytes);
                    }
                    else if (field.IsRepeated)
                    {
                        var repeated = propertyInfo.GetValue(obj);
                        var addRange = repeated.GetType().GetMethod("AddRange");
                        addRange.Invoke(repeated, new object[] { columnValue });
                    }
                    else if (field.IsMap)
                    {
                        throw new Exception("Maps are not yet supported by ProtobufSQL.");
                    }
                    else
                    {
                        propertyInfo.SetValue(obj, Convert.ChangeType(columnValue, propertyInfo.PropertyType));
                    }
                }
                catch (IndexOutOfRangeException e)
                {
                    // columnName was not present in the response
                }
            }
        }
    }
}

这是ProtobufSQL类的源代码:

{{1}}

我还有线程堆栈锁定时的截图:thread stack

我真的不确定该怎么做。希望有人带着一些知识可以帮助我继续前进!感谢。

0 个答案:

没有答案