Esempio n. 1
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);
        }
 /// <summary>
 /// Set the <seealso cref="Service.RecordingLog"/> for the  log terms and snapshots.
 /// </summary>
 /// <param name="recordingLog"> to use. </param>
 /// <returns> this for a fluent API. </returns>
 public Context RecordingLog(RecordingLog recordingLog)
 {
     this.recordingLog = recordingLog;
     return(this);
 }
            public void Conclude()
            {
                if (null == threadFactory)
                {
                    threadFactory = new DefaultThreadFactory();
                }

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

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

                if (null == errorHandler)
                {
                    throw new InvalidOperationException("Error handler must be supplied");
                }


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

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

                    ownsAeronClient = true;
                }

                if (null == errorCounter)
                {
                    throw new InvalidOperationException("Error counter must be supplied");
                }

                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 (deleteDirOnStart)
                {
                    if (null != clusteredServiceDir)
                    {
                        IoUtil.Delete(clusteredServiceDir, true);
                    }
                    else
                    {
                        IoUtil.Delete(new DirectoryInfo(Configuration.ClusteredServiceDirName()), true);
                    }
                }

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

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

                if (null == recordingLog)
                {
                    recordingLog = new RecordingLog(clusteredServiceDir);
                }

                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 InvalidOperationException("Either a ClusteredService instance or class name for the service must be provided");
                    }

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

                ConcludeMarkFile();
            }