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;
        }
            private void ConcludeMarkFile()
            {
                ClusterMarkFile.CheckHeaderLength(
                    aeron.Ctx().AeronDirectoryName(),
                    archiveContext.ControlRequestChannel(),
                    ServiceControlChannel(),
                    null,
                    serviceName,
                    null);

                var encoder = markFile.Encoder();

                encoder
                .ArchiveStreamId(archiveContext.ControlRequestStreamId())
                .ServiceStreamId(serviceStreamId)
                .ConsensusModuleStreamId(consensusModuleStreamId)
                .IngressStreamId(0)
                .MemberId(Adaptive.Aeron.Aeron.NULL_VALUE)
                .ServiceId(serviceId)
                .AeronDirectory(aeron.Ctx().AeronDirectoryName())
                .ArchiveChannel(archiveContext.ControlRequestChannel())
                .ServiceControlChannel(serviceControlChannel)
                .IngressChannel("")
                .ServiceName(serviceName)
                .Authenticator("");

                markFile.UpdateActivityTimestamp(epochClock.Time());
                markFile.SignalReady();
            }
        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);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
            private void ConcludeMarkFile()
            {
                if (null == markFile)
                {
                    int alignedTotalCncFileLength = ClusterMarkFile.AlignedTotalFileLength(
                        ClusterMarkFile.ALIGNMENT,
                        aeron.Ctx().AeronDirectoryName(),
                        archiveContext.ControlRequestChannel(),
                        ServiceControlChannel(),
                        null,
                        serviceName,
                        null);

                    markFile = new ClusterMarkFile(
                        new FileInfo(Path.Combine(clusteredServiceDir.FullName, ClusterMarkFile.FILENAME)),
                        ClusterComponentType.CONTAINER,
                        alignedTotalCncFileLength,
                        epochClock,
                        0);

                    MarkFileHeaderEncoder cncEncoder = markFile.Encoder();

                    cncEncoder
                    .ArchiveStreamId(archiveContext.ControlRequestStreamId())
                    .ServiceControlStreamId(serviceControlStreamId)
                    .IngressStreamId(0)
                    .MemberId(-1)
                    .ServiceId(serviceId)
                    .AeronDirectory(aeron.Ctx().AeronDirectoryName())
                    .ArchiveChannel(archiveContext.ControlRequestChannel())
                    .ServiceControlChannel(serviceControlChannel)
                    .IngressChannel("")
                    .ServiceName(serviceName)
                    .Authenticator("");

                    markFile.UpdateActivityTimestamp(epochClock.Time());
                    markFile.SignalReady();
                }
            }
 /// <summary>
 /// Set the <seealso cref="Cluster.ClusterMarkFile"/> in use.
 /// </summary>
 /// <param name="cncFile"> to use. </param>
 /// <returns> this for a fluent API. </returns>
 public Context MarkFile(ClusterMarkFile cncFile)
 {
     this.markFile = cncFile;
     return(this);
 }
            public void Conclude()
            {
                if (serviceId < 0)
                {
                    throw new ConfigurationException("service id must be not be negative: " + serviceId);
                }

                if (null == threadFactory)
                {
                    threadFactory = new DefaultThreadFactory();
                }

                if (null == idleStrategySupplier)
                {
                    idleStrategySupplier = Configuration.IdleStrategySupplier(null);
                }

                if (null == epochClock)
                {
                    epochClock = new SystemEpochClock();
                }

                if (null == clusterDir)
                {
                    clusterDir = new DirectoryInfo(clusteredServiceDirectoryName);
                }

                if (!clusterDir.Exists)
                {
                    Directory.CreateDirectory(clusterDir.FullName);
                }

                if (null == markFile)
                {
                    markFile = new ClusterMarkFile(
                        new FileInfo(Path.Combine(clusterDir.FullName, ClusterMarkFile.MarkFilenameForService(serviceId))),
                        ClusterComponentType.CONTAINER,
                        errorBufferLength,
                        epochClock,
                        0);
                }

                if (null == errorLog)
                {
                    errorLog = new DistinctErrorLog(markFile.ErrorBuffer, epochClock);
                }

                if (null == errorHandler)
                {
                    errorHandler = new LoggingErrorHandler(errorLog).OnError; // TODO Use interface
                }

                if (null == aeron)
                {
                    aeron = Adaptive.Aeron.Aeron.Connect(
                        new Aeron.Aeron.Context()
                        .AeronDirectoryName(aeronDirectoryName)
                        .ErrorHandler(errorHandler)
                        .EpochClock(epochClock));

                    ownsAeronClient = true;
                }

                if (null == errorCounter)
                {
                    errorCounter = aeron.AddCounter(SYSTEM_COUNTER_TYPE_ID, "Cluster errors - service " + serviceId);
                }

                if (null == countedErrorHandler)
                {
                    countedErrorHandler = new CountedErrorHandler(errorHandler, errorCounter);
                    if (ownsAeronClient)
                    {
                        aeron.Ctx().ErrorHandler(countedErrorHandler.OnError);
                    }
                }

                if (null == archiveContext)
                {
                    archiveContext = new AeronArchive.Context()
                                     .ControlRequestChannel(AeronArchive.Configuration.LocalControlChannel())
                                     .ControlResponseChannel(AeronArchive.Configuration.LocalControlChannel())
                                     .ControlRequestStreamId(AeronArchive.Configuration.LocalControlStreamId());
                }

                archiveContext
                .AeronClient(aeron)
                .OwnsAeronClient(false)
                .Lock(new NoOpLock());

                if (null == shutdownSignalBarrier)
                {
                    shutdownSignalBarrier = new ShutdownSignalBarrier();
                }

                if (null == terminationHook)
                {
                    terminationHook = () => shutdownSignalBarrier.Signal();
                }

                if (null == clusteredService)
                {
                    string className = Config.GetProperty(Configuration.SERVICE_CLASS_NAME_PROP_NAME);
                    if (null == className)
                    {
                        throw new ClusterException("either a ClusteredService instance or class name for the service must be provided");
                    }

                    clusteredService = (IClusteredService)Activator.CreateInstance(Type.GetType(className));
                }

                ConcludeMarkFile();
            }