Exemplo n.º 1
0
 /// <summary>
 /// All the election related heartbeat tasks begin here.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="shard"></param>
 public void BeginHeartbeatTasks(NodeContext context, IShard shard, IConnectionRestoration connectionRestoration)
 {
     if (_heartbeatManager != null)
     {
         _heartbeatManager.BeginTasks(context, shard, this, connectionRestoration, _clusterConfigMgr);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Provides O(n) linear weighted rendezvous routing algorithm
        /// </summary>
        /// <param name="shards">Shard set out of which one shard is selected</param>
        /// <param name="shardKey">Sharding key - argument of shard mapping</param>
        /// <returns>IShard chosen from the shard set</returns>
        public static IShard RendezvouzRoute(this IEnumerable <IShard> shards, ShardKey shardKey)
        {
            //[W]eighted [R]endezvous [H]ashing Algorithm
            //https://tools.ietf.org/html/draft-mohanty-bess-weighted-hrw-01
            var keyHash = shardKey.Hash;//the hash is already "avalanched"

            shards.IsTrue(s => s != null && s.Any(), "shards!=null && !empty");

            IShard best      = null;
            double bestScore = double.MinValue;

            foreach (var shard in shards)                           // O(n)
            {
                var    hash  = shard.NameHash ^ keyHash;            //both "avalanched"
                double norm  = hash / (double)ulong.MaxValue;       //[0.0 .. 1.0]
                var    score = shard.ShardWeight / -Math.Log(norm); //logarithm of real number is negative; log (0) = - infinity ; 1 / -log(0) = 0
                if (score > bestScore)
                {
                    best      = shard;
                    bestScore = score;
                }
            }

            return(best);
        }
            public Func <int> Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var query = this.shardedQuery.EstablishFor(shard);

                return(query.ExecuteUpdate);
            }
Exemplo n.º 4
0
 public KeyFrame(IShard shard)
 {
     UnkBytes = new List <byte> {
         0xff, 0xff, 0xff, 0xff,
         0xff, 0xff, 0xff, 0xff,
     };
 }
Exemplo n.º 5
0
            public System.Action Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var queryBatch = this.shardedQueryBatch.EstablishFor(shard);

                return(queryBatch.Execute);
            }
            public Func <CancellationToken, Task <int> > PrepareAsync(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var query = this.shardedQuery.EstablishFor(shard);

                return(query.ExecuteUpdateAsync);
            }
Exemplo n.º 7
0
            public Func <IEnumerable <TSource> > Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var multiQuery = this.shardedQueryBatch.EstablishFor(shard);

                return(() => multiQuery.GetResult <TSource>(this.queryIndex));
            }
Exemplo n.º 8
0
        public LocalShardCheckHeartbeatTask(IShard shard, NodeContext context, MembershipManager membershipManager, ClusterConfigurationManager clusterConfigMgr)
        {
            this._shard                  = shard;
            this._context                = context;
            this._clusterConfigMgr       = clusterConfigMgr;
            _localShardHeartbeatReporter = new LocalShardHeartbeatReporting(_context.LocalAddress);

            this._membershipManager = membershipManager;
            this._membershipManager.HeartbeatReport = _localShardHeartbeatReporter;
            ShardConfiguration sConfig = null;

            if (_clusterConfigMgr != null)
            {
                sConfig = _clusterConfigMgr.GetShardConfiguration(context.LocalShardName);
            }

            if (sConfig != null && sConfig.NodeHeartbeatInterval > 0)
            {
                this._poolingThreshold = (sConfig.NodeHeartbeatInterval * 2) * 1000;
            }
            _running     = false;
            _startSignal = new ManualResetEvent(false);


            //with the initialization of the heartbeat receiver, we start the send heart beat task.
            _sendHearbeatTask = new LocalShardSendHeartbeatTask(context, shard, _membershipManager, clusterConfigMgr);
        }
Exemplo n.º 9
0
            public Func <IEnumerable <T> > Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var criteria = this.shardedCriteria.EstablishFor(shard);

                return(criteria.List <T>);
            }
Exemplo n.º 10
0
            public Func <T> Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var criteria = shardedCriteria.EstablishFor(shard);

                return(criteria.UniqueResult <T>);
            }
Exemplo n.º 11
0
        public LocalShardSendHeartbeatTask(NodeContext nodeContext, IShard shard, MembershipManager membershipManager, ClusterConfigurationManager clusterConfigMgr)
        {
            this._nodeContext      = nodeContext;
            this._clusterConfigMgr = clusterConfigMgr;
            ShardConfiguration config = _clusterConfigMgr.GetShardConfiguration(_nodeContext.LocalShardName);//_nodeContext.ConfigurationSession.GetDatabaseClusterConfiguration(_nodeContext.ClusterName).Deployment.GetShardConfiguration(_nodeContext.LocalShardName);

            if (config == null)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("LocalShardSendHeartTask", "shard configuration is null");
                }
                throw new Alachisoft.NosDB.Common.Exceptions.DatabaseException(" shard configuration is null");
            }

            if (config.NodeHeartbeatInterval > 0)
            {
                this._poolingThreshold = (config.NodeHeartbeatInterval) * 1000;
            }
            this._shard             = shard;
            this._membershipManager = membershipManager;

            _running     = false;
            _startSignal = new System.Threading.ManualResetEvent(false);
        }
 public ElectionBasedMembershipStrategy(IShard shard, NodeContext context, LocalShardHeartbeatReporting heartbeatTable, ClusterConfigurationManager clusterConfigMgr)
 {
     this._shard            = shard;
     this._context          = context;
     this._clusterConfigMgr = clusterConfigMgr;
     Initialize();
 }
            public Func <IList> Prepare(IShard shard)
            {
                // NOTE: Establish action is not thread-safe and therefore must not be performed by returned delegate.
                var multiQuery = this.shardedMultiQuery.EstablishFor(shard);

                return(multiQuery.List);
            }
Exemplo n.º 14
0
        public KinesisStreamSubscription(KinesisStream stream, IShard shard, IObserver <IRecord> observer)
        {
            this.stream   = stream;
            this.shard    = shard;
            this.observer = observer;

            Task.Factory.StartNew(async() => await StartAsync().ConfigureAwait(false));
        }
 public bool AddResult(IList oneResult, IShard shard)
 {
     foreach (object item in oneResult)
     {
         result.Add(item);
     }
     return(false);
 }
Exemplo n.º 16
0
        public async Task RequestLogout(INetworkPlayer player, IShard shard, ulong EntityID, GamePacket packet)
        {
            var resp = new CloseConnection {
                Unk1 = 0
            };

            _ = await shard.SendTo(player, ChannelType.Control, resp);
        }
Exemplo n.º 17
0
 public ElectionManager(IShard shard, NodeContext context, ClusterConfigurationManager clusterConfigMgr)
 {
     this._shard            = shard;
     this._context          = context;
     this._clusterConfigMgr = clusterConfigMgr;
     //if (_context != null)
     //    _clusterConfig = _context.ConfigurationSession.GetDatabaseClusterConfiguration(_context.ClusterName);
 }
        /// <summary>
        /// Add the provided result and return whether or not the caller can halt
        /// processing.
        /// </summary>
        /// <param name="partialResult">The result to add</param>
        /// <param name="shard"></param>
        /// <returns>Whether or not the caller can halt processing</returns>
        public bool AddResult(IEnumerable <T> partialResult, IShard shard)
        {
            Preconditions.CheckNotNull(partialResult);

            result = result == null
                                ? partialResult
                                : result.Concat(partialResult);
            return(false);
        }
 /// <summary>
 /// Add the provided result and return whether or not the caller can halt
 /// processing.
 ///
 /// Synchronized method guarantees that only the first thread to add a result  will have its result reflected.
 /// </summary>
 /// <param name="result">The result to add</param>
 /// <param name="shard"></param>
 /// <returns>Whether or not the caller can halt processing</returns>
 public bool AddResult(T result, IShard shard)
 {
     Preconditions.CheckNotNull(result);
     this.results.Add(result);
     this.firstShard = this.results.Count == 1
         ? shard
         : null;
     return(false);
 }
        public void CountDown()
        {
            MockRepository mr = Mocks;

            int            latchSize  = 5;
            CountDownLatch startLatch = new CountDownLatch(0);
            CountDownLatch latch      = new CountDownLatch(latchSize);

            IShard shard = mr.DynamicMock <IShard>();

            IExitStrategy <object> strat = mr.CreateMock <IExitStrategy <object> >();

            ICallable <object> anotherCallable = mr.DynamicMock <ICallable <object> >();

            IShardOperation <object> operation = mr.DynamicMock <IShardOperation <object> >();

            //Expectations and results
            Expect.Call(strat.AddResult(null, shard)).Return(false);
            Expect.Call(strat.AddResult(null, shard)).Return(false);
            Expect.Call(strat.AddResult(null, shard)).Return(true);
            Expect.Call(strat.AddResult(null, shard)).Return(true);
            SetupResult.For(shard.ShardIds).Return(new HashedSet <ShardId>(new ShardId[] { (new ShardId(0)) }));

            mr.ReplayAll();

            IList <StartAwareFutureTask <object> > futureTasks = new List <StartAwareFutureTask <object> >();

            ParallelShardOperationCallable <object> callable = new ParallelShardOperationCallable <object>(
                startLatch,
                latch,
                strat,
                operation,
                shard,
                futureTasks);

            callable.Call();
            // addResult returns false so latch is only decremented by 1
            Assert.AreEqual(latchSize - 1, latch.Count);
            // addResult returns false so latch is only decremented by 1
            callable.Call();
            Assert.AreEqual(latchSize - 2, latch.Count);


            StartAwareFutureTask <object> ft = new StartAwareFutureTask_CancelReturnFalse(anotherCallable, 0);

            futureTasks.Add(ft);
            callable.Call();
            // cancelling the 1 task returns false, so latch is only decremented by 1
            Assert.AreEqual(latchSize - 3, latch.Count);

            ft = new StartAwareFutureTask_CancelReturnTrue(anotherCallable, 0);

            futureTasks.Add(ft);
            callable.Call();
            // 1 decrement for myself and 1 for the task that returned true when cancelled
            Assert.AreEqual(latchSize - 5, latch.Count);
        }
Exemplo n.º 21
0
        /// <summary>
        /// All the heartbeat tasks begin here.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="shard"></param>
        public void BeginTasks(NodeContext context, IShard shard, MembershipManager manager, IConnectionRestoration connectionRestoration, ClusterConfigurationManager clusterConfigMgr)
        {
            _nodeToCSReportingTask = new NodeToCSHeartbeatTask(context, manager, connectionRestoration, clusterConfigMgr);
            _nodeToCSReportingTask.Start();

            //Start the shard level heartbeat tasks.
            _checkHeartbeatTask = new LocalShardCheckHeartbeatTask(shard, context, manager, clusterConfigMgr);
            _checkHeartbeatTask.Start();
        }
Exemplo n.º 22
0
 public IQuery EstablishFor(IShard shard)
 {
     if (!establishedQueriesByShard.TryGetValue(shard, out var result))
     {
         result = this.queryFactory(shard.EstablishSession());
         this.establishActions?.Invoke(result);
         establishedQueriesByShard.Add(shard, result);
     }
     return(result);
 }
Exemplo n.º 23
0
        /// <summary>
        /// Add the provided result and return whether or not the caller can halt
        /// processing.
        ///
        /// Synchronized method guarantees that only the first thread to add a result  will have its result reflected.
        /// </summary>
        /// <param name="result">The result to add</param>
        /// <param name="shard"></param>
        /// <returns>Whether or not the caller can halt processing</returns>
        public bool AddResult(T result, IShard shard)
        {
            Preconditions.CheckNotNull(result);

            if (Interlocked.Increment(ref resultCount) == 1)
            {
                firstResult = result;
                firstShard  = shard;
            }
            return(true);
        }
 public bool AddResult(T result, IShard shard)
 {
     Preconditions.CheckNotNull(shard);
     if (result != null && nonNullResult == null)
     {
         nonNullResult = result;
         this.shard    = shard;
         return(true);
     }
     return(false);
 }
Exemplo n.º 25
0
        private IQuery GetOrEstablishSomeQuery()
        {
            IQuery query = SomeQuery;

            if (query == null)
            {
                IShard shard = shards[0];
                query = shard.EstablishQuery(this);
            }
            return(query);
        }
Exemplo n.º 26
0
        public IDictionary <String, IResponseCollection <T> > SendMessageToAllShards <T>(Message message, bool primaryOnly)
        {
            IDictionary <String, IResponseCollection <T> > responses    = new HashVector <String, IResponseCollection <T> >();
            IDictionary <String, RequestAsync>             asyncResults = new HashVector <String, RequestAsync>();

            foreach (String shard in _remoteShards.Keys)
            {
                IShard dest = GetShardInstance(shard);
                if (dest != null)
                {
                    try
                    {
                        if (primaryOnly)
                        {
                            ShardRequestBase <T> request = dest.CreateUnicastRequest <T>(dest.Primary, message);
                            asyncResults.Add(shard, new RequestAsync(request.BeginExecute(), request, dest.Primary));
                        }
                        else
                        {
                            ShardMulticastRequest <ResponseCollection <T>, T> multicastRequest = dest.CreateMulticastRequest <ResponseCollection <T>, T>(dest.ActiveChannelsList, message);
                            asyncResults.Add(shard, new RequestAsync(multicastRequest.BeginExecute(), multicastRequest));
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }

            foreach (KeyValuePair <String, RequestAsync> pair in asyncResults)
            {
                String shardName = pair.Key;
                IResponseCollection <T> shardResponse = new ResponseCollection <T>();

                if (primaryOnly)
                {
                    ShardRequestBase <T>          req             = pair.Value.Request;
                    IClusterResponse <T>          clusterResponse = new ClusterResponse <T>(req.EndExecute(pair.Value.AsyncResult), pair.Value.Destination);
                    IList <IClusterResponse <T> > list            = new List <IClusterResponse <T> >();
                    list.Add(clusterResponse);

                    shardResponse.Responses = list;
                }
                else
                {
                    ShardMulticastRequest <ResponseCollection <T>, T> req = pair.Value.Request;
                    shardResponse = req.EndExecute(pair.Value.AsyncResult);
                }

                responses.Add(shardName, shardResponse);
            }

            return(responses);
        }
Exemplo n.º 27
0
        /**
         * @return any Criteria.  If no Criteria has been established we establish
         * one and return it.
         */
        private ICriteria GetOrEstablishSomeCriteria()
        {
            ICriteria crit = GetSomeCriteria();

            if (crit != null)
            {
                IShard shard = shards[0];
                crit = shard.EstablishCriteria(this);
            }
            return(crit);
        }
Exemplo n.º 28
0
 /// <inheritdoc />
 public override void EstablishFor(IShard shard, IQueryBatch queryBatch, string key = null)
 {
     if (key != null)
     {
         queryBatch.Add <T>(key, this.shardedCriteria.EstablishFor(shard));
     }
     else
     {
         queryBatch.Add <T>(this.shardedCriteria.EstablishFor(shard));
     }
 }
Exemplo n.º 29
0
 public void EstablishFor(IShard shard, ICriteria parent)
 {
     if (!this.establishedSubcriteriaByShard.TryGetValue(shard, out var result))
     {
         result = this.subcriteriaFactory(parent);
         foreach (var action in this.establishActions)
         {
             action(result);
         }
         establishedSubcriteriaByShard[shard] = result;
     }
 }
Exemplo n.º 30
0
        public void Init(INetworkPlayer player, IShard shard, IPacketSender s)
        {
            Player          = player;
            NetClientStatus = Status.Connecting;
            AssignedShard   = shard;

            NetChans = Channel.GetChannels(this, s).ToImmutableDictionary();
            NetChans[ChannelType.Control].PacketAvailable       += Control_PacketAvailable;
            NetChans[ChannelType.Matrix].PacketAvailable        += Matrix_PacketAvailable;
            NetChans[ChannelType.ReliableGss].PacketAvailable   += GSS_PacketAvailable;
            NetChans[ChannelType.UnreliableGss].PacketAvailable += GSS_PacketAvailable;
        }
Exemplo n.º 31
0
 public SubcriteriaRegistrar(IShard shard)
 {
     this.shard = shard;
 }
Exemplo n.º 32
0
 public void Add(IShard shard, List<Object> list)
 {
     resultMap.Add(shard, list);
     entityList.Add(list); //.addAll
 }