Esempio n. 1
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;
            }
        }
Esempio n. 2
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;
            }
        }
            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();
            }
            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();
            }