Exemple #1
0
        private LockAttemptResult RunBattle([NotNull] LockMetadata lockMetadata, [NotNull] string threadId, long newThreshold)
        {
            var items = baseOperationsPerformer.SearchThreads(lockMetadata.MainRowKey(), lockMetadata.PreviousThreshold);

            if (items.Length == 1)
            {
                return(items[0] == threadId?LockAttemptResult.Success() : LockAttemptResult.AnotherOwner(items[0]));
            }
            if (items.Length > 1)
            {
                if (items.Any(s => s == threadId))
                {
                    throw new Exception("Lock unknown exception");
                }
                return(LockAttemptResult.AnotherOwner(items[0]));
            }
            var beforeOurWriteShades = baseOperationsPerformer.SearchThreads(lockMetadata.ShadowRowKey(), lockMetadata.PreviousThreshold);

            if (beforeOurWriteShades.Length > 0)
            {
                return(LockAttemptResult.ConcurrentAttempt());
            }
            baseOperationsPerformer.WriteThread(lockMetadata.ShadowRowKey(), newThreshold, threadId, LockTtl);
            var shades = baseOperationsPerformer.SearchThreads(lockMetadata.ShadowRowKey(), lockMetadata.PreviousThreshold);

            if (shades.Length == 1)
            {
                items = baseOperationsPerformer.SearchThreads(lockMetadata.MainRowKey(), lockMetadata.PreviousThreshold);
                if (items.Length == 0)
                {
                    baseOperationsPerformer.WriteThread(lockMetadata.MainRowKey(), newThreshold, threadId, LockTtl);
                    baseOperationsPerformer.DeleteThread(lockMetadata.ShadowRowKey(), newThreshold, threadId);
                    return(LockAttemptResult.Success());
                }
            }
            baseOperationsPerformer.DeleteThread(lockMetadata.ShadowRowKey(), newThreshold, threadId);
            return(LockAttemptResult.ConcurrentAttempt());
        }
Exemple #2
0
 public static string MainRowKey([NotNull] this LockMetadata lockMetadata)
 {
     return("Main_" + lockMetadata.LockRowId);
 }
Exemple #3
0
 public static string ShadowRowKey([NotNull] this LockMetadata lockMetadata)
 {
     return("Shade_" + lockMetadata.LockRowId);
 }