Пример #1
0
        private WopiResponse ProcessGetLockRequest(GetLockRequest wopiReq)
        {
            if (!int.TryParse(wopiReq.FileId, out var contentId))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;
            if (!(Node.LoadNode(contentId) is File file))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;

            var existingLock = SharedLock.GetLock(file.Id, CancellationToken.None) ?? string.Empty;

            return(new WopiResponse
            {
                StatusCode = HttpStatusCode.OK,
                Headers = new Dictionary <string, string>
                {
                    { WopiHeader.Lock, existingLock },
                }
            });
        }
Пример #2
0
        private async Task <WopiResponse> ProcessGetLockRequestAsync(GetLockRequest wopiRequest,
                                                                     CancellationToken cancellationToken)
        {
            if (!int.TryParse(wopiRequest.FileId, out var contentId))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;
            if (!(await Node.LoadNodeAsync(contentId, cancellationToken).ConfigureAwait(false) is File file))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;

            var existingLock = SharedLock.GetLock(file.Id, CancellationToken.None) ?? string.Empty;

            return(new WopiResponse
            {
                StatusCode = HttpStatusCode.OK,
                Headers = new Dictionary <string, string>
                {
                    { WopiHeader.Lock, existingLock },
                }
            });
        }
Пример #3
0
        private IEnumerable <DataSample> PopulateBucketedDataSamples(DimensionSpecification filterDims,
                                                                     Action <DataSample, TInternal, QuerySpecification>
                                                                     sampleAction, QuerySpecification querySpec)
        {
            using (SharedLock.OpenShared(this.dataLock))
            {
                var bucketQuery = new BucketQuery(this, filterDims);
                foreach (var bucket in bucketQuery)
                {
                    foreach (var match in (querySpec.IsCrossQuery
                                               ? bucket.GetMatchesSplitByDimension(filterDims,
                                                                                   querySpec.CrossQueryDimension)
                                               : bucket.GetMatches(filterDims)))
                    {
                        if (match.DataCount == 0)
                        {
                            continue;
                        }

                        var sample = new DataSample
                        {
                            Name       = this.Name,
                            Dimensions = match.DimensionValues.Data,
                            StartTime  = bucket.StartTime.ToMillisecondTimestamp(),
                            EndTime    = bucket.EndTime.ToMillisecondTimestamp(),
                        };

                        sampleAction(sample, match.Data, querySpec);
                        yield return(sample);
                    }
                }
            }

            Events.Write.EndQueryData(this);
        }
Пример #4
0
        public bool ReleaseOldestData(bool releaseLatest)
        {
            DataBucket <TInternal> releaseBucket = null;

            // Under heavy stress we want to unblock even if we can't find data to release.
            using (SharedLock.OpenShared(this.dataLock))
            {
                for (var i = this.data.Count - 1; i >= (releaseLatest ? 0 : 1); --i)
                {
                    var bucket = this.data.Values[i];
                    if (bucket.Loaded)
                    {
                        releaseBucket = bucket;
                        break;
                    }
                }
            }

            if (releaseBucket != null)
            {
                releaseBucket.ReleaseData();
                return(true);
            }

            return(false);
        }
Пример #5
0
        public bool Serialize(DateTime startTime, DateTime endTime, Stream destination)
        {
            // When serializing, if asked to send data for multiple buckets, we simply have them all serialize one-by-
            // one into the stream. The reader fully supports this behavior. We could potentially save wire size by
            // condensing all buckets together, but the caller may not want that and the CPU cost would be quite
            // substantial to do so.

            startTime = startTime.ToUniversalTime();
            endTime   = endTime.ToUniversalTime();

            var serializeBuckets = new List <DataBucket <TInternal> >();

            using (SharedLock.OpenShared(this.dataLock))
            {
                foreach (var bucketPair in this.data)
                {
                    var bucketTimestamp = bucketPair.Key;
                    if (bucketTimestamp < startTime || bucketTimestamp >= endTime)
                    {
                        continue;
                    }

                    serializeBuckets.Add(bucketPair.Value);
                }
            }

            foreach (var bucket in serializeBuckets)
            {
                bucket.Serialize(this.Name, destination);
            }

            return(serializeBuckets.Count > 0);
        }
Пример #6
0
        public void SetLatestTimeForDataSources(SortedList <DateTimeOffset, List <string> > sourcesByTime)
        {
            Events.Write.BeginSetDataSourceTimes(this.Name);
            using (SharedLock.OpenExclusive(this.dataLock))
            {
                foreach (var kvp in sourcesByTime)
                {
                    var latestTime = kvp.Key;
                    var serverList = kvp.Value;

                    if (latestTime < this.earliestUnsealedBucketTime)
                    {
                        continue;
                    }

                    DateTimeOffset bucketTimestamp =
                        this.earliestUnsealedBucketTime == DateTime.MinValue
                            ? latestTime - this.properties.SealTime
                            : this.earliestUnsealedBucketTime;

                    while (bucketTimestamp < latestTime)
                    {
                        // we ensure above that we won't ask for a time that is too early, so bucket is guaranteed to be
                        // non-null.
                        var bucket = this.GetOrCreateDataBucket(bucketTimestamp.UtcDateTime, false);
                        bucketTimestamp += this.properties.CompactionConfiguration.Default.Interval;
                        foreach (var server in serverList)
                        {
                            bucket.AddDataSource(server);
                        }
                    }
                }
            }
            Events.Write.EndSetDataSourceTimes(this.Name);
        }
Пример #7
0
        public void UpdateFromAggregator(IPersistedDataAggregator aggregator, DateTimeOffset start, DateTimeOffset end)
        {
            DataBucket <TInternal> updateBucket = null;

            using (SharedLock.OpenShared(this.dataLock))
            {
                foreach (var bucket in this.data.Values)
                {
                    if (bucket.StartTime == start && bucket.EndTime == end)
                    {
                        updateBucket = bucket;
                        break;
                    }
                }
            }

            if (updateBucket == null)
            {
                Events.Write.UnknownBucketCannotBeUpdated(this.Name, start, end);
                return;
            }

            if (updateBucket.Sealed)
            {
                Events.Write.SealedBucketCannotBeUpdated(this.Name, start, end);
                return;
            }

            var agg = aggregator as PersistedDataAggregator <TInternal>;
            var availableSources = new List <string>();

            foreach (var source in agg.Sources)
            {
                switch (source.Status)
                {
                case PersistedDataSourceStatus.Unavailable:
                    updateBucket.SetSourceUnavailable(source.Name);
                    break;

                case PersistedDataSourceStatus.Available:
                    availableSources.Add(source.Name);
                    break;

                case PersistedDataSourceStatus.Unknown:
                    break;

                default:
                    throw new ArgumentException("Unexpected source status " + source.Status, "aggregator");
                }
            }

            if (availableSources.Count > 0)
            {
                var aggregateData = agg.AcquireData();
                updateBucket.UpdateDataFromSources(availableSources, agg.DimensionSet, aggregateData);
            }

            // XXX: Dump data back to disk for now (eases memory pressure)
            updateBucket.ReleaseData();
        }
Пример #8
0
 static EvalManager()
 {
     // ENSURE to create lock first
     SharedLock     = new SharedLock();
     Configuration  = new EvalContext();
     DefaultContext = Configuration;
     SQLNET.LoadConfiguration();
 }
Пример #9
0
 /// <summary>
 /// Seal data (preventing further modification). Does not write to permanent storage.
 /// </summary>
 public void Seal()
 {
     using (SharedLock.OpenExclusive(this.dataAccessLock))
     {
         this.Flush();
         this.Sealed = true;
     }
 }
Пример #10
0
        public void SharedLock_Unlock_Missing()
        {
            var node         = CreateTestFolder();
            var nodeId       = node.Id;
            var existingLock = "LCK_" + Guid.NewGuid();

            // ACTION
            SharedLock.Unlock(nodeId, existingLock);
        }
Пример #11
0
        public void SharedLock_RefreshLock_Missing()
        {
            var node      = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();

            // ACTION
            SharedLock.RefreshLock(nodeId, lockValue);
        }
 public void SharedLockProtectsAgainstDoubleDispose()
 {
     using (var rwSlim = new ReaderWriterLockSlim())
     {
         var testMe = SharedLock.OpenExclusive(rwSlim);
         testMe.Dispose();
         Assert.Throws <ObjectDisposedException>(testMe.Dispose);
     }
 }
Пример #13
0
        private async Task <WopiResponse> ProcessLockRequestAsync(LockRequest wopiRequest,
                                                                  CancellationToken cancellationToken)
        {
            if (!int.TryParse(wopiRequest.FileId, out var contentId))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;
            if (!(await Node.LoadNodeAsync(contentId, cancellationToken).ConfigureAwait(false) is File file))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;

            var existingLock = SharedLock.GetLock(file.Id, CancellationToken.None);

            if (existingLock == null)
            {
                if (!file.Locked)
                {
                    SharedLock.Lock(file.Id, wopiRequest.Lock, CancellationToken.None);
                    return(new WopiResponse {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                return(new WopiResponse
                {
                    StatusCode = HttpStatusCode.Conflict,
                    Headers = new Dictionary <string, string>
                    {
                        { WopiHeader.Lock, "" },
                        { WopiHeader.LockFailureReason, "CheckedOut" }
                    }
                });
            }
            if (existingLock != wopiRequest.Lock)
            {
                return(new WopiResponse
                {
                    StatusCode = HttpStatusCode.Conflict,
                    Headers = new Dictionary <string, string>
                    {
                        { WopiHeader.Lock, existingLock },
                        { WopiHeader.LockFailureReason, "LockedByAnother" }
                    }
                });
            }
            SharedLock.RefreshLock(contentId, existingLock, CancellationToken.None);
            return(new WopiResponse {
                StatusCode = HttpStatusCode.OK
            });
        }
Пример #14
0
        public void SharedLock_ModifyLock_Missing()
        {
            var node         = CreateTestFolder();
            var nodeId       = node.Id;
            var oldLockValue = Guid.NewGuid().ToString();
            var newLockValue = Guid.NewGuid().ToString();

            // ACTION
            SharedLock.ModifyLock(nodeId, oldLockValue, newLockValue);
        }
Пример #15
0
 private IEnumerable <string> GetTimestampValues(GetTimestamp getter)
 {
     using (SharedLock.OpenShared(this.dataLock))
     {
         foreach (var bucket in this.data.Values)
         {
             yield return(getter(bucket).ToString(Protocol.TimestampStringFormat));
         }
     }
 }
Пример #16
0
 public void Flush()
 {
     using (SharedLock.OpenShared(this.dataLock))
     {
         foreach (var b in this.data.Values)
         {
             b.Flush();
         }
     }
 }
Пример #17
0
        public StreamObserver(StreamReader argStream, Object argMessageSyncer = null)
        {
            if (argStream == null)
            {
                throw new ArgumentNullException("argStream");
            }

            this.stream        = argStream;
            this.messageSyncer = new SharedLock(argMessageSyncer);
        }
Пример #18
0
        private WopiResponse ProcessLockRequest(LockRequest wopiReq)
        {
            if (!int.TryParse(wopiReq.FileId, out var contentId))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;
            if (!(Node.LoadNode(contentId) is File file))
            {
                return new WopiResponse {
                           StatusCode = HttpStatusCode.NotFound
                }
            }
            ;

            var existingLock = SharedLock.GetLock(file.Id);

            if (existingLock == null)
            {
                if (!file.Locked)
                {
                    SharedLock.Lock(file.Id, wopiReq.Lock);
                    return(new WopiResponse {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                return(new WopiResponse
                {
                    StatusCode = HttpStatusCode.Conflict,
                    Headers = new Dictionary <string, string>
                    {
                        { WopiHeader.Lock, "" },
                        { WopiHeader.LockFailureReason, "CheckedOut" }
                    }
                });
            }
            if (existingLock != wopiReq.Lock)
            {
                return(new WopiResponse
                {
                    StatusCode = HttpStatusCode.Conflict,
                    Headers = new Dictionary <string, string>
                    {
                        { WopiHeader.Lock, existingLock },
                        { WopiHeader.LockFailureReason, "LockedByAnother" }
                    }
                });
            }
            SharedLock.RefreshLock(contentId, existingLock);
            return(new WopiResponse {
                StatusCode = HttpStatusCode.OK
            });
        }
Пример #19
0
        public void SharedLock_Lock_CheckedOut()
        {
            var node = CreaeTestContent();
            node.CheckOut();
            var nodeId = node.Id;
            var expectedLockValue = Guid.NewGuid().ToString();
            Assert.IsNull(SharedLock.GetLock(nodeId));

            // ACTION
            SharedLock.Lock(nodeId, expectedLockValue);
        }
Пример #20
0
        public void SharedLock_Delete_Locked_Folder()
        {
            var node      = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();

            SharedLock.Lock(nodeId, lockValue);

            // ACTION
            node.ForceDelete();
        }
Пример #21
0
        public void SharedLock_Unlock_Different()
        {
            var node = CreaeTestContent();
            var nodeId = node.Id;
            var existingLock = "LCK_" + Guid.NewGuid();
            SharedLock.Lock(nodeId, existingLock);

            // ACTION
            var actualLock = SharedLock.Unlock(nodeId, "DifferentLock");

            Assert.AreEqual(existingLock, actualLock);
        }
Пример #22
0
        public void SharedLock_Checkout()
        {
            //UNDONE: remove this line when SharedLock assert is implemented in the repo
            Assert.Inconclusive();

            var node = CreaeTestContent();
            var nodeId = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();
            SharedLock.Lock(nodeId, lockValue);

            node.CheckOut();
        }
Пример #23
0
        public void SharedLock_Lock_Different()
        {
            var node = CreaeTestContent();
            var nodeId = node.Id;
            var oldLockValue = Guid.NewGuid().ToString();
            var newLockValue = Guid.NewGuid().ToString();
            SharedLock.Lock(nodeId, oldLockValue);
            SetSharedLockCreationDate(nodeId, DateTime.UtcNow.AddMinutes(-10.0d));

            // ACTION
            SharedLock.Lock(nodeId, newLockValue);
        }
Пример #24
0
        public void SharedLock_Unlock()
        {
            var node = CreaeTestContent();
            var nodeId = node.Id;
            var existingLock = "LCK_" + Guid.NewGuid();
            SharedLock.Lock(nodeId, existingLock);

            // ACTION
            SharedLock.Unlock(nodeId, existingLock);

            Assert.IsNull(SharedLock.GetLock(nodeId));
        }
Пример #25
0
        public void SharedLock_RefreshLock_TimedOut()
        {
            var node      = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = Guid.NewGuid().ToString();

            SharedLock.Lock(nodeId, lockValue);
            SetSharedLockCreationDate(nodeId, DateTime.UtcNow.AddHours(-1.0d));

            // ACTION
            SharedLock.RefreshLock(nodeId, lockValue);
        }
Пример #26
0
        public void SharedLock_Move_Locked_Folder()
        {
            var node      = CreateTestFolder();
            var target    = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();

            SharedLock.Lock(nodeId, lockValue);

            // ACTION
            node.MoveTo(target);
        }
Пример #27
0
        public void SharedLock_RefreshLock_Different()
        {
            var node = CreaeTestContent();
            var nodeId = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();
            SharedLock.Lock(nodeId, lockValue);

            // ACTION
            var actualLock = SharedLock.RefreshLock(nodeId, "DifferentLock");

            Assert.AreEqual(lockValue, actualLock);
        }
Пример #28
0
        public void SharedLock_Rename_Locked_Folder()
        {
            var node      = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();

            SharedLock.Lock(nodeId, lockValue);

            // ACTION
            node.Name = Guid.NewGuid().ToString();
            node.Save();
        }
Пример #29
0
        public void SharedLock_Unlock_TimedOut()
        {
            var node         = CreateTestFolder();
            var nodeId       = node.Id;
            var existingLock = Guid.NewGuid().ToString();

            SharedLock.Lock(nodeId, existingLock);
            SetSharedLockCreationDate(nodeId, DateTime.UtcNow.AddHours(-1.0d));

            // ACTION
            SharedLock.Unlock(nodeId, existingLock);
        }
Пример #30
0
        public void SharedLock_Checkout_Locked_Folder()
        {
            var node      = CreateTestFolder();
            var nodeId    = node.Id;
            var lockValue = "LCK_" + Guid.NewGuid();

            SharedLock.Lock(nodeId, lockValue);

            node.CheckOut();

            Assert.IsTrue(node.Locked);
            Assert.AreEqual(lockValue, SharedLock.GetLock(nodeId));
        }
Пример #31
0
        private void button1_Click(object sender, EventArgs e)
        {
            animalList = new List<Animal>();
            threadList = new List<Thread>();

            // Create the shared variable to lock on
            SharedLock sharedLock = new SharedLock();

            animalList.Add(new Animal("frog.wav", sharedLock));
            animalList.Add(new Animal("duck.wav", sharedLock));
            animalList.Add(new Animal("meow.wav", sharedLock));

            for (int i = 0; i < animalList.Count; i++)
                threadList.Add(new Thread(animalList[i].speak));

            for (int i = 0; i < animalList.Count; i++)
                threadList[i].Start();
        }
Пример #32
0
 public Animal(String soundFileName, SharedLock sharedLock)
 {
     this.sharedLock = sharedLock;
     soundPlayer = new SoundPlayer(soundFileName);
 }