internal ClusteredServiceAgent(ClusteredServiceContainer.Context ctx)
        {
            this.ctx = ctx;

            archiveCtx   = ctx.ArchiveContext();
            aeron        = ctx.Aeron();
            service      = ctx.ClusteredService();
            idleStrategy = ctx.IdleStrategy();
            serviceId    = ctx.ServiceId();
            epochClock   = ctx.EpochClock();
            markFile     = ctx.MarkFile();


            var channel = ctx.ServiceControlChannel();

            _consensusModuleProxy = new ConsensusModuleProxy(aeron.AddPublication(channel, ctx.ConsensusModuleStreamId()));
            _serviceAdapter       = new ServiceAdapter(aeron.AddSubscription(channel, ctx.ServiceStreamId()), this);

            UnsafeBuffer headerBuffer = new UnsafeBuffer(new byte[SESSION_HEADER_LENGTH]);

            _egressMessageHeaderEncoder.WrapAndApplyHeader(headerBuffer, 0, new MessageHeaderEncoder());

            _vectors[0] = new DirectBufferVector(headerBuffer, 0, SESSION_HEADER_LENGTH);
            _vectors[1] = _messageVector;
        }
예제 #2
0
        internal ClusteredServiceAgent(ClusteredServiceContainer.Context ctx)
        {
            this.ctx = ctx;

            headerVector = new DirectBufferVector(headerBuffer, 0, headerBuffer.Capacity);

            abortHandler      = Abort;
            archiveCtx        = ctx.ArchiveContext();
            aeron             = ctx.Aeron();
            aeronAgentInvoker = ctx.Aeron().ConductorAgentInvoker;
            service           = ctx.ClusteredService();
            idleStrategy      = ctx.IdleStrategy();
            serviceId         = ctx.ServiceId();
            epochClock        = ctx.EpochClock();
            markFile          = ctx.MarkFile();


            var channel = ctx.ServiceControlChannel();

            _consensusModuleProxy =
                new ConsensusModuleProxy(aeron.AddPublication(channel, ctx.ConsensusModuleStreamId()));
            _serviceAdapter = new ServiceAdapter(aeron.AddSubscription(channel, ctx.ServiceStreamId()), this);
            _sessionMessageHeaderEncoder.WrapAndApplyHeader(headerBuffer, 0, new MessageHeaderEncoder());
            aeron.AddCloseHandler(abortHandler);
        }
예제 #3
0
        private void CheckForReplay(CountersReader counters, int recoveryCounterId)
        {
            long replayTermCount = RecoveryState.GetReplayTermCount(counters, recoveryCounterId);

            if (0 == replayTermCount)
            {
                return;
            }

            service.OnReplayBegin();

            for (int i = 0; i < replayTermCount; i++)
            {
                AwaitActiveLog();
                int counterId = newActiveLogEvent.commitPositionId;
                leadershipTermId    = CommitPos.GetLeadershipTermId(counters, counterId);
                termBaseLogPosition = CommitPos.GetTermBaseLogPosition(counters, counterId); // TODO MARK

                if (CommitPos.GetLeadershipTermLength(counters, counterId) > 0)
                {
                    using (Subscription subscription = aeron.AddSubscription(newActiveLogEvent.channel, newActiveLogEvent.streamId))
                    {
                        serviceControlPublisher.AckAction(termBaseLogPosition, leadershipTermId, serviceId, ClusterAction.READY);

                        Image             image   = AwaitImage(newActiveLogEvent.sessionId, subscription);
                        ReadableCounter   limit   = new ReadableCounter(counters, counterId);
                        BoundedLogAdapter adapter = new BoundedLogAdapter(image, limit, this);

                        ConsumeImage(image, adapter);

                        termBaseLogPosition += image.Position();
                    }
                }

                newActiveLogEvent = null;
                serviceControlPublisher.AckAction(termBaseLogPosition, leadershipTermId, serviceId, ClusterAction.REPLAY);
            }

            service.OnReplayEnd();
        }
예제 #4
0
        private void CheckForReplay(CountersReader counters, int recoveryCounterId)
        {
            if (RecoveryState.HasReplay(counters, recoveryCounterId))
            {
                AwaitActiveLog();

                int counterId = _activeLogEvent.commitPositionId;

                using (Subscription subscription = aeron.AddSubscription(_activeLogEvent.channel, _activeLogEvent.streamId))
                {
                    _consensusModuleProxy.Ack(CommitPos.GetLogPosition(counters, counterId), ackId++, serviceId);

                    Image             image   = AwaitImage(_activeLogEvent.sessionId, subscription);
                    ReadableCounter   limit   = new ReadableCounter(counters, counterId);
                    BoundedLogAdapter adapter = new BoundedLogAdapter(image, limit, this);

                    ConsumeImage(image, adapter);
                }

                _activeLogEvent = null;
                heartbeatCounter.SetOrdered(epochClock.Time());
            }
        }
예제 #5
0
        private AeronCluster(Context ctx)
        {
            _ctx = ctx;


            Subscription subscription = null;

            try
            {
                ctx.Conclude();

                _aeron        = ctx.Aeron();
                _idleStrategy = ctx.IdleStrategy();
                _nanoClock    = _aeron.Ctx().NanoClock();
                _isUnicast    = ctx.ClusterMemberEndpoints() != null;
                UpdateMemberEndpoints(ctx.ClusterMemberEndpoints());

                subscription  = _aeron.AddSubscription(ctx.EgressChannel(), ctx.EgressStreamId());
                _subscription = subscription;

                _publication      = ConnectToCluster();
                _clusterSessionId = OpenSession();

                UnsafeBuffer headerBuffer = new UnsafeBuffer(new byte[SessionDecorator.SESSION_HEADER_LENGTH]);
                _sessionHeaderEncoder
                .WrapAndApplyHeader(headerBuffer, 0, _messageHeaderEncoder)
                .ClusterSessionId(_clusterSessionId)
                .Timestamp(Aeron.Aeron.NULL_VALUE);

                _vectors[0] = new DirectBufferVector(headerBuffer, 0, SessionDecorator.SESSION_HEADER_LENGTH);
                _vectors[1] = _messageBuffer;

                _poller            = new Poller(ctx.SessionMessageListener(), _clusterSessionId, this);
                _fragmentAssembler = new FragmentAssembler(_poller);
            }
            catch (Exception)
            {
                if (!ctx.OwnsAeronClient())
                {
                    CloseHelper.QuietDispose(_publication);
                    CloseHelper.QuietDispose(subscription);
                }

                CloseHelper.QuietDispose(ctx);
                throw;
            }
        }
예제 #6
0
        internal ClusteredServiceAgent(ClusteredServiceContainer.Context ctx)
        {
            this.ctx = ctx;

            archiveCtx   = ctx.ArchiveContext();
            aeron        = ctx.Aeron();
            service      = ctx.ClusteredService();
            idleStrategy = ctx.IdleStrategy();
            serviceId    = ctx.ServiceId();
            epochClock   = ctx.EpochClock();
            markFile     = ctx.MarkFile();


            var channel = ctx.ServiceControlChannel();

            _consensusModuleProxy = new ConsensusModuleProxy(aeron.AddPublication(channel, ctx.ConsensusModuleStreamId()));
            _serviceAdapter       = new ServiceAdapter(aeron.AddSubscription(channel, ctx.ServiceStreamId()), this);
        }
예제 #7
0
        internal ClusteredServiceAgent(ClusteredServiceContainer.Context ctx)
        {
            this.ctx = ctx;

            archiveCtx           = ctx.ArchiveContext();
            aeron                = ctx.Aeron();
            shouldCloseResources = ctx.OwnsAeronClient();
            service              = ctx.ClusteredService();
            recordingLog         = ctx.RecordingLog();
            idleStrategy         = ctx.IdleStrategy();
            serviceId            = ctx.ServiceId();
            epochClock           = ctx.EpochClock();
            markFile             = ctx.MarkFile();


            var channel  = ctx.ServiceControlChannel();
            var streamId = ctx.ServiceControlStreamId();

            serviceControlPublisher = new ServiceControlPublisher(aeron.AddPublication(channel, streamId));
            serviceControlAdapter   = new ServiceControlAdapter(aeron.AddSubscription(channel, streamId), this);
        }
예제 #8
0
        private AeronCluster(Context ctx)
        {
            _ctx = ctx;

            Subscription subscription = null;
            Publication  publication  = null;

            try
            {
                ctx.Conclude();

                _aeron        = ctx.Aeron();
                _lock         = ctx.Lock();
                _idleStrategy = ctx.IdleStrategy();
                _nanoClock    = _aeron.Ctx().NanoClock();
                _isUnicast    = ctx.ClusterMemberEndpoints() != null;

                subscription  = _aeron.AddSubscription(ctx.EgressChannel(), ctx.EgressStreamId());
                _subscription = subscription;

                publication  = ConnectToCluster();
                _publication = publication;

                _clusterSessionId = OpenSession();
            }
            catch (Exception)
            {
                if (!ctx.OwnsAeronClient())
                {
                    CloseHelper.QuietDispose(publication);
                    CloseHelper.QuietDispose(subscription);
                }

                CloseHelper.QuietDispose(ctx);
                throw;
            }
        }