コード例 #1
0
        async Task UpdateFromSyncStoreAsync(string scopeName, ScopeState state)
        {
            var writesAttempted = 0;

            while (writesAttempted < maxWriteAttempts)
            {
                var data = await optimisticDataStore.GetDataAsync(scopeName);

                if (!long.TryParse(data, out long nextId))
                {
                    throw new UniqueIdGenerationException(string.Format(
                                                              "The id seed returned from storage for scope '{0}' was corrupt, and could not be parsed as a long. The data returned was: {1}",
                                                              scopeName,
                                                              data));
                }

                state.LastId = nextId - 1;
                state.HighestIdAvailableInBatch = nextId - 1 + batchSize;
                var firstIdInNextBatch = state.HighestIdAvailableInBatch + 1;

                var written = await optimisticDataStore.TryOptimisticWriteAsync(scopeName, firstIdInNextBatch.ToString(CultureInfo.InvariantCulture));

                if (written)
                {
                    return;
                }

                writesAttempted++;
            }

            throw new UniqueIdGenerationException(string.Format(
                                                      "Failed to update the data store after {0} attempts. This likely represents too much contention against the store. Increase the batch size to a value more appropriate to your generation load.",
                                                      writesAttempted));
        }
コード例 #2
0
        void UpdateFromSyncStore(string scopeName, ScopeState state)
        {
            var writesAttempted = 0;

            while (writesAttempted < maxWriteAttempts)
            {
                var data = optimisticDataStore.GetData(scopeName);

                long nextId;
                if (!long.TryParse(data, out nextId))
                    throw new UniqueIdGenerationException(string.Format(
                       "The id seed returned from storage for scope '{0}' was corrupt, and could not be parsed as a long. The data returned was: {1}",
                       scopeName,
                       data));

                state.LastId = nextId - 1;
                state.HighestIdAvailableInBatch = nextId - 1 + batchSize;
                var firstIdInNextBatch = state.HighestIdAvailableInBatch + 1;

                if (optimisticDataStore.TryOptimisticWrite(scopeName, firstIdInNextBatch.ToString()))
                    return;

                writesAttempted++;
            }

            throw new UniqueIdGenerationException(string.Format(
                "Failed to update the data store after {0} attempts. This likely represents too much contention against the store. Increase the batch size to a value more appropriate to your generation load.",
                writesAttempted));
        }
コード例 #3
0
        void UpdateFromSyncStore(string scopeName, ScopeState state)
        {
            var writesAttempted = 0;

            while (writesAttempted < maxWriteAttempts)
            {
                long id = optimisticDataStore.GetNextBatch(scopeName, batchSize);
                if (id != -1)
                {
                    state.LastId = id - 1;
                    state.HighestIdAvailableInBatch = id - 1 + batchSize;
                    return;
                }
                writesAttempted++;
            }

            throw new UniqueIdGenerationException(string.Format(
                "Failed to update the data store after {0} attempts. This likely represents too much contention against the store. Increase the batch size to a value more appropriate to your generation load.",
                writesAttempted));
        }
コード例 #4
0
        private void FetchFromSyncStore(string scopeName, ScopeState state, int batchSize)
        {
            if (state.PrefetchTask != null) {
                try {
                    state.PrefetchTask.Wait();
                }
                finally {
                    state.PrefetchTask = null;
                }
            }
            else {
                UpdateFromSyncStore(scopeName, state, batchSize);
            }

            state.LastId = state.NextBatchMin;
            state.HighestIdAvailableInBatch = state.NextBatchMax;

            state.NextBatchMin = 0;
            state.NextBatchMax = 0;
        }
コード例 #5
0
 private void PrefetchFromSyncStore(string scopeName, ScopeState state, int batchSize)
 {
     if (state.PrefetchTask != null)
         return;
     state.PrefetchTask = Task.Run(() => UpdateFromSyncStore(scopeName, state, batchSize));
 }