Exemplo n.º 1
0
        public void RecordRead(ITransactionalResource transactionalResource, TransactionalResourceVersion readVersion, long stableVersion)
        {
            if (readVersion.TransactionId == TransactionId)
            {
                // Just reading our own write here.
                // Sanity check to see if there's a lost write.
                int resourceWriteNumber;
                if (WriteSet.TryGetValue(transactionalResource, out resourceWriteNumber) &&
                    resourceWriteNumber > readVersion.WriteNumber)
                {
                    // Context has record of more writes than we have, some writes must be lost.
                    throw new OrleansTransactionAbortedException(TransactionId, "Lost Write");
                }
            }
            else
            {
                TransactionalResourceVersion resourceReadVersion;
                if (ReadSet.TryGetValue(transactionalResource, out resourceReadVersion) &&
                    resourceReadVersion != readVersion)
                {
                    // Uh-oh. Read two different versions of the grain.
                    throw new OrleansValidationFailedException(TransactionId);
                }

                ReadSet[transactionalResource] = readVersion;

                if (readVersion.TransactionId != TransactionId &&
                    readVersion.TransactionId > stableVersion)
                {
                    DependentTransactions.Add(readVersion.TransactionId);
                }
            }
        }
Exemplo n.º 2
0
        private async Task OnSetupState(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return;
            }

            Tuple <TransactionalExtension, ITransactionalExtension> boundExtension = await this.runtime.BindExtension <TransactionalExtension, ITransactionalExtension>(() => new TransactionalExtension());

            boundExtension.Item1.Register(this.config.StateName, this);
            this.transactionalResource = boundExtension.Item2.AsTransactionalResource(this.config.StateName);

            // wire up storage provider
            IStorageProvider storageProvider = string.IsNullOrWhiteSpace(this.config.StorageName)
                ? this.context.ActivationServices.GetRequiredService <IStorageProvider>()
                : this.context.ActivationServices.GetServiceByKey <string, IStorageProvider>(this.config.StorageName);

            this.storage = new StateStorageBridge <TransactionalStateRecord <TState> >(StoredName(), this.context.GrainInstance.GrainReference, storageProvider);

            // load inital state
            await this.storage.ReadStateAsync();

            // recover state
            DoRecovery();
            this.validState = true;
        }
Exemplo n.º 3
0
        public void RecordWrite(ITransactionalResource transactionalResource, TransactionalResourceVersion latestVersion, long stableVersion)
        {
            int writeNumber;

            WriteSet.TryGetValue(transactionalResource, out writeNumber);
            WriteSet[transactionalResource] = writeNumber + 1;

            if (latestVersion.TransactionId != TransactionId && latestVersion.TransactionId > stableVersion)
            {
                DependentTransactions.Add(latestVersion.TransactionId);
            }
        }
Exemplo n.º 4
0
            public async Task BindAsync(Grain containerGrain, Logger logger, IServiceProvider services, ITransactionOrchestrationResultGrain resultGrain)
            {
                this.grain = containerGrain;
                this.resultGrain = resultGrain;
                this.logger = logger.GetSubLogger(nameof(TransactionalResource));

                // bind extension to grain
                IProviderRuntime runtime = services.GetRequiredService<IProviderRuntime>();
                Tuple<TransactionalExtension, ITransactionalExtension> boundExtension = await runtime.BindExtension<TransactionalExtension, ITransactionalExtension>(() => new TransactionalExtension());
                boundExtension.Item1.Register(nameof(TransactionalResource), this);
                this.transactionalResource = boundExtension.Item2.AsTransactionalResource(nameof(TransactionalResource));
            }
Exemplo n.º 5
0
 private static void NotifyResourceOfCommit(ITransactionalResource resource, long transaction, MultiCompletionSource completionSource)
 {
     resource.Commit(transaction)
     .ContinueWith(
         (result, state) =>
     {
         var completion = (MultiCompletionSource)state;
         if (result.Exception != null)
         {
             completion.SetException(result.Exception);
         }
         else
         {
             completion.SetOneResult();
         }
     },
         completionSource,
         CancellationToken.None,
         TaskContinuationOptions.ExecuteSynchronously,
         TaskScheduler.Default);
 }
Exemplo n.º 6
0
        private async Task OnSetupState(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return;
            }

            Tuple <TransactionalExtension, ITransactionalExtension> boundExtension = await this.runtime.BindExtension <TransactionalExtension, ITransactionalExtension>(() => new TransactionalExtension());

            boundExtension.Item1.Register(this.config.StateName, this);
            this.transactionalResource = boundExtension.Item2.AsTransactionalResource(this.config.StateName);

            INamedTransactionalStateStorageFactory storageFactory = this.context.ActivationServices.GetRequiredService <INamedTransactionalStateStorageFactory>();

            this.storage = storageFactory.Create <TState>(this.config.StorageName);

            // recover state
            await DoLoad();

            this.validState = true;
        }
Exemplo n.º 7
0
 public bool Equals(ITransactionalResource other)
 {
     return(transactionalResource.Equals(other));
 }
Exemplo n.º 8
0
 public void Register(string resourceId, ITransactionalResource localTransactionalResource)
 {
     this.transactionalResources.Add(resourceId, localTransactionalResource);
 }
Exemplo n.º 9
0
 public static string ToShortString(this ITransactionalResource resource)
 {
     // Meant to help humans when debugging or reading traces
     return(resource.GetHashCode().ToString("x4").Substring(0, 4));
 }
 public bool Equals(ITransactionalResource other)
 {
     return(Object.Equals(this, other));
 }
Exemplo n.º 11
0
 public bool Equals(ITransactionalResource other)
 {
     return(Equals((object)other));
 }