예제 #1
0
        internal void RegisterReadReplica(SharedDiscoveryReadReplicaClient client)
        {
            ReadReplicaInfo previousRR = _readReplicas.putIfAbsent(client.MemberId, client.ReadReplicainfo);

            if (previousRR == null)
            {
                NotifyCoreClients();
            }
        }
예제 #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void begin(KEY key) throws ConcurrentAccessException
        public virtual void Begin(KEY key)
        {
            VALUE instance = _factory.newInstance();
            Entry existing;

            if ((existing = _repo.putIfAbsent(key, new Entry(this, instance))) != null)
            {
                _reaper.accept(instance);                           // Need to clear up our optimistically allocated value
                throw new ConcurrentAccessException(string.Format("Cannot begin '{0}', because {1} with that key already exists.", key, existing));
            }
        }
예제 #3
0
        internal void RegisterCoreMember(SharedDiscoveryCoreClient client)
        {
            CoreServerInfo previousMember = _coreMembers.putIfAbsent(client.MemberId, client.CoreServerInfo);

            if (previousMember == null)
            {
                _listeningClients.Add(client);
                _enoughMembers.Signal();
                NotifyCoreClients();
            }
        }
예제 #4
0
        internal bool CasClusterId(ClusterId clusterId, string dbName)
        {
            ClusterId previousId = _clusterIdDbNames.putIfAbsent(dbName, clusterId);

            bool success = previousId == null || previousId.Equals(clusterId);

            if (success)
            {
                NotifyCoreClients();
            }
            return(success);
        }
예제 #5
0
        public override bool TryExclusiveLock(ResourceType resourceType, long resourceId)
        {
            _hasLocks = true;
            _stateHolder.incrementActiveClients(this);

            try
            {
                ConcurrentMap <long, ForsetiLockManager.Lock> lockMap = _lockMaps[resourceType.TypeId()];
                MutableLongIntMap heldLocks = _exclusiveLockCounts[resourceType.TypeId()];

                int heldCount = heldLocks.getIfAbsent(resourceId, -1);
                if (heldCount != -1)
                {
                    // We already have a lock on this, just increment our local reference counter.
                    heldLocks.put(resourceId, Math.incrementExact(heldCount));
                    return(true);
                }

                // Grab the global lock
                ForsetiLockManager.Lock @lock;
                if ((@lock = lockMap.putIfAbsent(resourceId, _myExclusiveLock)) != null)
                {
                    if (@lock is SharedLock && _sharedLockCounts[resourceType.TypeId()].containsKey(resourceId))
                    {
                        SharedLock sharedLock = ( SharedLock )@lock;
                        if (sharedLock.TryAcquireUpdateLock(this))
                        {
                            if (sharedLock.NumberOfHolders() == 1)
                            {
                                heldLocks.put(resourceId, 1);
                                return(true);
                            }
                            else
                            {
                                sharedLock.ReleaseUpdateLock();
                                return(false);
                            }
                        }
                    }
                    return(false);
                }

                heldLocks.put(resourceId, 1);
                return(true);
            }
            finally
            {
                _stateHolder.decrementActiveClients();
            }
        }
예제 #6
0
        //-------------------------------------------------------------------------
        public override FraConvention lookup(string name)
        {
            FraConvention value = BY_NAME.get(name);

            if (value == null)
            {
                FraConvention created = createByName(name);
                if (created != null)
                {
                    string correctName = created.Name;
                    value = BY_NAME.computeIfAbsent(correctName, k => created);
                    BY_NAME.putIfAbsent(correctName.ToUpper(Locale.ENGLISH), value);
                }
            }
            return(value);
        }
예제 #7
0
        private AuthenticationMetadata AuthMetadataFor(string username)
        {
            AuthenticationMetadata authMeta = _authenticationData.get(username);

            if (authMeta == null)
            {
                authMeta = new AuthenticationMetadata(this);
                AuthenticationMetadata preExisting = _authenticationData.putIfAbsent(username, authMeta);
                if (preExisting != null)
                {
                    authMeta = preExisting;
                }
            }

            return(authMeta);
        }
예제 #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") static <Key> void applyUpdate(ReadableState<Key> store, java.util.concurrent.ConcurrentMap<Key, ChangeEntry> changes, Key key, ValueUpdate update, boolean reset, long version) throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal static void ApplyUpdate <Key>(ReadableState <Key> store, ConcurrentMap <Key, ChangeEntry> changes, Key key, ValueUpdate update, bool reset, long version)
        {
            ChangeEntry value = changes.get(key);

            if (value == null)
            {
                ChangeEntry newEntry = ChangeEntry.Of(new sbyte[store.KeyFormat().valueSize()], version);
                lock ( newEntry )
                {
                    value = changes.putIfAbsent(key, newEntry);
                    if (value == null)
                    {
                        BigEndianByteArrayBuffer buffer = new BigEndianByteArrayBuffer(newEntry.Data);
                        if (!reset)
                        {
                            PreviousValue lookup = new PreviousValue(newEntry.Data);
                            if (!store.Lookup(key, lookup))
                            {
                                buffer.Clear();
                            }
                        }
                        update.Update(buffer);
                        return;
                    }
                }
            }
            lock ( value )
            {
                BigEndianByteArrayBuffer target = new BigEndianByteArrayBuffer(value.Data);
                value.Version = version;
                if (reset)
                {
                    target.Clear();
                }
                update.Update(target);
            }
        }
예제 #9
0
        public override bool TrySharedLock(ResourceType resourceType, long resourceId)
        {
            _hasLocks = true;
            _stateHolder.incrementActiveClients(this);

            try
            {
                ConcurrentMap <long, ForsetiLockManager.Lock> lockMap = _lockMaps[resourceType.TypeId()];
                MutableLongIntMap heldShareLocks     = _sharedLockCounts[resourceType.TypeId()];
                MutableLongIntMap heldExclusiveLocks = _exclusiveLockCounts[resourceType.TypeId()];

                int heldCount = heldShareLocks.getIfAbsent(resourceId, -1);
                if (heldCount != -1)
                {
                    // We already have a lock on this, just increment our local reference counter.
                    heldShareLocks.put(resourceId, Math.incrementExact(heldCount));
                    return(true);
                }

                if (heldExclusiveLocks.containsKey(resourceId))
                {
                    // We already have an exclusive lock, so just leave that in place. When the exclusive lock is released,
                    // it will be automatically downgraded to a shared lock, since we bumped the share lock reference count.
                    heldShareLocks.put(resourceId, 1);
                    return(true);
                }

                long waitStartMillis = _clock.millis();
                while (true)
                {
                    AssertValid(waitStartMillis, resourceType, resourceId);

                    ForsetiLockManager.Lock existingLock = lockMap.get(resourceId);
                    if (existingLock == null)
                    {
                        // Try to create a new shared lock
                        if (lockMap.putIfAbsent(resourceId, new SharedLock(this)) == null)
                        {
                            // Success!
                            break;
                        }
                    }
                    else if (existingLock is SharedLock)
                    {
                        // Note that there is a "safe" race here where someone may be releasing the last reference to a lock
                        // and thus removing that lock instance (making it unacquirable). In this case, we allow retrying,
                        // even though this is a try-lock call.
                        if ((( SharedLock )existingLock).Acquire(this))
                        {
                            // Success!
                            break;
                        }
                        else if ((( SharedLock )existingLock).UpdateLock)
                        {
                            return(false);
                        }
                    }
                    else if (existingLock is ExclusiveLock)
                    {
                        return(false);
                    }
                    else
                    {
                        throw new System.NotSupportedException("Unknown lock type: " + existingLock);
                    }
                }
                heldShareLocks.put(resourceId, 1);
                return(true);
            }
            finally
            {
                _stateHolder.decrementActiveClients();
            }
        }
예제 #10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void acquireExclusive(org.neo4j.storageengine.api.lock.LockTracer tracer, org.neo4j.storageengine.api.lock.ResourceType resourceType, long... resourceIds) throws org.neo4j.storageengine.api.lock.AcquireLockTimeoutException
        public override void AcquireExclusive(LockTracer tracer, ResourceType resourceType, params long[] resourceIds)
        {
            _hasLocks = true;
            _stateHolder.incrementActiveClients(this);
            LockWaitEvent waitEvent = null;

            try
            {
                ConcurrentMap <long, ForsetiLockManager.Lock> lockMap = _lockMaps[resourceType.TypeId()];
                MutableLongIntMap heldLocks = _exclusiveLockCounts[resourceType.TypeId()];

                foreach (long resourceId in resourceIds)
                {
                    int heldCount = heldLocks.getIfAbsent(resourceId, -1);
                    if (heldCount != -1)
                    {
                        // We already have a lock on this, just increment our local reference counter.
                        heldLocks.put(resourceId, Math.incrementExact(heldCount));
                        continue;
                    }

                    // Grab the global lock
                    ForsetiLockManager.Lock existingLock;
                    int  tries           = 0;
                    long waitStartMillis = _clock.millis();
                    while ((existingLock = lockMap.putIfAbsent(resourceId, _myExclusiveLock)) != null)
                    {
                        AssertValid(waitStartMillis, resourceType, resourceId);

                        // If this is a shared lock:
                        // Given a grace period of tries (to try and not starve readers), grab an update lock and wait
                        // for it to convert to an exclusive lock.
                        if (tries > 50 && existingLock is SharedLock)
                        {
                            // Then we should upgrade that lock
                            SharedLock sharedLock = ( SharedLock )existingLock;
                            if (TryUpgradeSharedToExclusive(tracer, waitEvent, resourceType, lockMap, resourceId, sharedLock, waitStartMillis))
                            {
                                break;
                            }
                        }

                        if (waitEvent == null)
                        {
                            waitEvent = tracer.WaitForLock(true, resourceType, resourceId);
                        }
                        WaitFor(existingLock, resourceType, resourceId, true, tries++);
                    }

                    heldLocks.put(resourceId, 1);
                }
            }
            finally
            {
                if (waitEvent != null)
                {
                    waitEvent.Close();
                }
                ClearWaitList();
                _waitingForLock = null;
                _stateHolder.decrementActiveClients();
            }
        }
예제 #11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void acquireShared(org.neo4j.storageengine.api.lock.LockTracer tracer, org.neo4j.storageengine.api.lock.ResourceType resourceType, long... resourceIds) throws org.neo4j.storageengine.api.lock.AcquireLockTimeoutException
        public override void AcquireShared(LockTracer tracer, ResourceType resourceType, params long[] resourceIds)
        {
            _hasLocks = true;
            _stateHolder.incrementActiveClients(this);
            LockWaitEvent waitEvent = null;

            try
            {
                // Grab the global lock map we will be using
                ConcurrentMap <long, ForsetiLockManager.Lock> lockMap = _lockMaps[resourceType.TypeId()];

                // And grab our local lock maps
                MutableLongIntMap heldShareLocks     = _sharedLockCounts[resourceType.TypeId()];
                MutableLongIntMap heldExclusiveLocks = _exclusiveLockCounts[resourceType.TypeId()];

                foreach (long resourceId in resourceIds)
                {
                    // First, check if we already hold this as a shared lock
                    int heldCount = heldShareLocks.getIfAbsent(resourceId, -1);
                    if (heldCount != -1)
                    {
                        // We already have a lock on this, just increment our local reference counter.
                        heldShareLocks.put(resourceId, Math.incrementExact(heldCount));
                        continue;
                    }

                    // Second, check if we hold it as an exclusive lock
                    if (heldExclusiveLocks.containsKey(resourceId))
                    {
                        // We already have an exclusive lock, so just leave that in place.
                        // When the exclusive lock is released, it will be automatically downgraded to a shared lock,
                        // since we bumped the share lock reference count.
                        heldShareLocks.put(resourceId, 1);
                        continue;
                    }

                    // We don't hold the lock, so we need to grab it via the global lock map
                    int        tries           = 0;
                    SharedLock mySharedLock    = null;
                    long       waitStartMillis = _clock.millis();

                    // Retry loop
                    while (true)
                    {
                        AssertValid(waitStartMillis, resourceType, resourceId);

                        // Check if there is a lock for this entity in the map
                        ForsetiLockManager.Lock existingLock = lockMap.get(resourceId);

                        // No lock
                        if (existingLock == null)
                        {
                            // Try to create a new shared lock
                            if (mySharedLock == null)
                            {
                                mySharedLock = new SharedLock(this);
                            }

                            if (lockMap.putIfAbsent(resourceId, mySharedLock) == null)
                            {
                                // Success, we now hold the shared lock.
                                break;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        // Someone holds shared lock on this entity, try and get in on that action
                        else if (existingLock is SharedLock)
                        {
                            if ((( SharedLock )existingLock).Acquire(this))
                            {
                                // Success!
                                break;
                            }
                        }

                        // Someone holds an exclusive lock on this entity
                        else if (existingLock is ExclusiveLock)
                        {
                            // We need to wait, just let the loop run.
                        }
                        else
                        {
                            throw new System.NotSupportedException("Unknown lock type: " + existingLock);
                        }

                        if (waitEvent == null)
                        {
                            waitEvent = tracer.WaitForLock(false, resourceType, resourceId);
                        }
                        // And take note of who we are waiting for. This is used for deadlock detection.
                        WaitFor(existingLock, resourceType, resourceId, false, tries++);
                    }

                    // Make a local note about the fact that we now hold this lock
                    heldShareLocks.put(resourceId, 1);
                }
            }
            finally
            {
                if (waitEvent != null)
                {
                    waitEvent.Close();
                }
                ClearWaitList();
                _waitingForLock = null;
                _stateHolder.decrementActiveClients();
            }
        }