예제 #1
0
        /// <summary>
        ///     Will check for a predefined keyspace and table to see if there is a lock.
        ///     Otherwise, always returns true, because the lock is granted at table level.
        /// </summary>
        public override bool TryAcquireApplicationLock()
        {
            string clusterLockKeyspaceName = "cluster_lock";
            string clusterLockTableName    = "lock";

            if (Configuration.ConfigurationFileExists())
            {
                var clusterLock = JSON.Parse(Configuration.GetConfiguration()).Linq
                                  .SingleOrDefault(x => x.Key.Equals("clusterLock", StringComparison.OrdinalIgnoreCase)).Value?.Linq
                                  .ToDictionary(x => x.Key, x => x.Value.Value, StringComparer.OrdinalIgnoreCase);

                clusterLockKeyspaceName = clusterLock !.GetValue("defaultClusterLockKeyspace", clusterLockKeyspaceName) !;
                clusterLockTableName    = clusterLock !.GetValue("defaultClusterLockTable", clusterLockTableName) !;
            }

            try
            {
                return(WrappedConnection.QueryForLong($"select count(locked) from {clusterLockKeyspaceName}.{clusterLockTableName}") == 0);
            }
            catch (EvolveException ex)
                when(ex?.InnerException?.GetType().ToString() == "Cassandra.InvalidQueryException")
                {
                    //These error messages are very specific to Cassandra's drivers and could change
                    return(ex.Message.StartsWith($"keyspace {clusterLockKeyspaceName} does not exist", StringComparison.OrdinalIgnoreCase) ||
                           ex.Message.StartsWith($"unconfigured table {clusterLockTableName}", StringComparison.OrdinalIgnoreCase) ||
                           ex.Message.StartsWith($"table {clusterLockTableName} does not exist", StringComparison.OrdinalIgnoreCase));
                }
        }
예제 #2
0
        /// <summary>
        ///     Will chekc for a predefined keyspace and table to see if there is a lock.
        ///     Otherwise, always returns true, because the lock is granted at table level.
        ///     <see cref="CassandraMetadataTable.TryLock"/>
        /// </summary>
        public override bool TryAcquireApplicationLock()
        {
            const string DefaultClusterLockKeyspaceName = "cluster_lock";
            const string DefaultClusterLockTableName    = "lock";

            var clusterLockKeyspaceName = DefaultClusterLockKeyspaceName;
            var clusterLockTableName    = DefaultClusterLockTableName;

            if (Configuration.ConfigurationFileExists())
            {
                var configuration = Configuration.GetConfiguration()
                                    .FromJson <Dictionary <string, Dictionary <string, object> > >()
                                    .GetValue("clusterLock", new Dictionary <string, object>());

                clusterLockKeyspaceName = configuration.GetValue("defaultClusterLockKeyspace", DefaultClusterLockKeyspaceName) as string;
                clusterLockTableName    = configuration.GetValue("defaultClusterLockTable", DefaultClusterLockTableName) as string;
            }

            try
            {
                return(WrappedConnection.QueryForLong($"select count(locked) from {clusterLockKeyspaceName}.{clusterLockTableName}") == 0);
            }
            catch (EvolveException ex)
                when(ex?.InnerException.GetType().ToString() == "Cassandra.InvalidQueryException")
                {
                    //These error messages are very specific to Cassandra's drivers and could change
                    if (ex.Message.StartsWith($"Keyspace {clusterLockKeyspaceName} does not exist") ||
                        ex.Message.StartsWith($"unconfigured table {clusterLockTableName}"))
                    {
                        return(true);
                    }
                    else
                    {
                        return(true);
                    }
                }
        }
예제 #3
0
 public override bool ReleaseApplicationLock() => WrappedConnection.QueryForLong($"SELECT RELEASE_LOCK('{LOCK_ID}');") == 1;
예제 #4
0
 public override bool TryAcquireApplicationLock() => WrappedConnection.QueryForLong($"SELECT GET_LOCK('{LOCK_ID}', 0);") == 1;