【问题标题】:Troubleshooting Cassandra C# driver, including seeing CQL statementsCassandra C# 驱动程序故障排除,包括查看 CQL 语句
【发布时间】:2016-04-08 14:10:42
【问题描述】:

我们有一个 ASP.NET 4.5 webapi 解决方案,它将数据写入 Cassandra。我们遇到了删除操作不起作用的问题。我们想看看引擎盖下的 C# 驱动程序发生了什么。我们已将 CassandraTraceSwitch 设置为 Verbose,但它并没有提供太多有用的数据。我想看看它针对 Cassandra 生成和执行的实际查询,以及它得到的响应。

【问题讨论】:

  • 我一直在寻找驱动程序的源代码,除了他们允许的跟踪之外,没有做任何事情。我们能够通过调试源代码来解决我们的问题。有一个已知的错误是缓存语句在键空间之间没有区别,所以我们的语句没有被执行。现在将 NoPrepare 设置为 true 可以解决我们的问题。

标签: c# cassandra cql cassandra-2.0


【解决方案1】:

没有办法输出生成的查询,但这是个好主意。

我创建了一个ticket to include the generated query in the output,当跟踪级别为Verbose时,您可以在JIRA上关注进度或发送拉取请求。

【讨论】:

  • 谢谢!我克隆了 repo 并在代码中找到了我要添加它的位置。我正在考虑为它创建一个拉取请求。也许我可以把它链接到你的票
【解决方案2】:

只是一个想法。它没有完全回答原始问题,但可能会有所帮助。您可以创建DispatchProxy 并拦截 CQL 查询。我是这样做的

using Cassandra;
using System;
using System.Reflection;
using Cassandra.Data.Linq;
using Wpfe.Logging;
using Microsoft.Extensions.Logging;
using System.Diagnostics;

namespace Wpfe.Storage {
    public class QueryTracingProxy<T> : DispatchProxy where T : class {
        private static readonly ILogger _logger = AppLogFactory.CreateLogger("Wpfe.StorageQuery");
        static Lazy<PropertyInfo> cqlPropertyInfo = new Lazy<PropertyInfo>(
            () => typeof(PreparedStatement).GetProperty("Cql", BindingFlags.NonPublic | BindingFlags.Instance));

        public T Target { get; private set; }

        public static T Decorate(T target) {
            var proxy = Create<T, QueryTracingProxy<T>>() as QueryTracingProxy<T>;
            proxy.Target = target;
            return proxy as T;
        }

        [DebuggerStepThrough]
        protected override object Invoke(MethodInfo targetMethod, object[] args) {
            try {
                if (targetMethod.Name.Equals(nameof(ISession.Execute))) {
                    if (args.Length > 0) {
                        var arg1 = args[0];
                        if (arg1 is string) {
                            var str = (string)arg1;
                            _logger.LogInformation(str);
                        }
                        else if (arg1 is CqlCommand) {
                            var cmd = (CqlCommand)arg1;
                            var values = string.Join("\n", cmd.QueryValues);
                            _logger.LogInformation(string.Concat(cmd.QueryString, "\n", values));
                        }
                    }
                }
                else if (targetMethod.Name.Equals(nameof(ISession.ExecuteAsync)) && args.Length == 1) {
                    var statement = args[0] as BoundStatement;
                    if (statement != null) {
                        var preparedStatement = statement.PreparedStatement;
                        if (preparedStatement != null) {
                            var cql = cqlPropertyInfo.Value.GetValue(preparedStatement);
                            var values = string.Join("\n", statement.QueryValues);
                            _logger.LogInformation(string.Concat(cql, "\n", values));
                        }
                    }
                }

                var result = targetMethod.Invoke(Target, args);
                return result;
            }
            catch (TargetInvocationException exc) {
                throw exc.InnerException;
            }
        }
    }

    public static class QueryTracer {
        private static readonly ILogger _logger = AppLogFactory.CreateLogger("Wpfe.StorageQuery");
        public static ISession HookUp(ISession session) {
            if (_logger.IsEnabled(LogLevel.Information) || _logger.IsEnabled(LogLevel.Debug)) {
                var proxy = QueryTracingProxy<ISession>.Decorate(session);
                return proxy;
            }
            return session;
        }
    }
}

然后在代码中我只是装饰ISession 实例,即

private static ISession createDefaultSession() {
   var cluster = EntityManagerFactory.BuildCluster(StorageGlobalOptions.Value.DatabaseEffective);
   var session = cluster.Connect();
   return QueryTracer.HookUp(session);
}

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2022-12-15
    • 2017-03-23
    • 2012-08-05
    • 2015-08-07
    相关资源
    最近更新 更多