예제 #1
0
        /// <summary>
        /// The setup kafka consumer.
        /// </summary>
        /// <param name="databaseConsumerParameters">
        /// The database consumer parameters.
        /// </param>
        /// <returns>
        /// The <see cref="KafkaConsumer{T1,T2}"/>.
        /// </returns>
        private static IKafkaConsumer <string, string> SetupKafkaConsumer(VotingDatabaseParameters databaseConsumerParameters)
        {
            var kafkaConsumerProperties = GetKafkaConsumerProperties(databaseConsumerParameters);

            return(new KafkaConsumer <string, string>(
                       kafkaConsumerProperties,
                       new StringDeserializer(Encoding.UTF8),
                       new StringDeserializer(Encoding.UTF8)));
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VotingDatabase"/> class.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="votingDatabaseMessageHandler">Voting database message handler.</param>
 /// <param name="votingDatabaseParameters">The database consumer parameters.</param>
 /// <param name="kafkaConsumer">The kafka consumer.</param>
 public VotingDatabase(StatelessServiceContext context,
                       IVotingDatabaseMessageHandler votingDatabaseMessageHandler,
                       VotingDatabaseParameters votingDatabaseParameters,
                       IKafkaConsumer <string, string> kafkaConsumer)
     : base(context)
 {
     this.votingDatabaseParameters     = votingDatabaseParameters;
     this.kafkaConsumer                = kafkaConsumer;
     this.votingDatabaseMessageHandler = votingDatabaseMessageHandler;
 }
예제 #3
0
        public static void Main(string[] args)
        {
            try
            {
                var builder = new ContainerBuilder();

                // Register any regular dependencies.
                var databaseParams = VotingDatabaseParameters.GetDatabaseConsumerParameters(FabricRuntime.GetActivationContext());
                builder.Register(c => databaseParams).As <VotingDatabaseParameters>().SingleInstance();
                builder.RegisterType <KafkaConsumer <string, string> >().As <IKafkaConsumer <string, string> >().SingleInstance();
                builder.RegisterType <VotingDatabaseMessageHandler>().As <IVotingDatabaseMessageHandler>().SingleInstance();
                builder.RegisterType <OtpHandler>().As <IDataHandler>().Keyed <IDataHandler>(Enums.EventType.SendOtp);
                builder.RegisterType <CastVoteHandler>().As <IDataHandler>().Keyed <IDataHandler>(Enums.EventType.CastVote);
                builder.Register(c => GetKafkaConsumerProperties(c.Resolve <VotingDatabaseParameters>())).As <KafkaConsumerProperties>().SingleInstance();
                builder.Register(c => SetupKafkaConsumer(c.Resolve <VotingDatabaseParameters>())).As <IKafkaConsumer <string, string> >().SingleInstance();

                builder.Register <Func <Enums.EventType, IDataHandler> >(c =>
                {
                    var componentContext = c.Resolve <IComponentContext>();
                    return(eventType => componentContext.ResolveKeyed <IDataHandler>(eventType));
                });

                // Register the Autofac for Service Fabric support.
                builder.RegisterServiceFabricSupport();

                // Register a stateless service.
                builder.RegisterStatelessService <VotingDatabase>("VotingDatabaseType");

                using (builder.Build())
                {
                    ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(VotingDatabase).Name);

                    // Prevents this host process from terminating so services keep running.
                    Thread.Sleep(Timeout.Infinite);
                }
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
예제 #4
0
        private static KafkaConsumerProperties GetKafkaConsumerProperties(VotingDatabaseParameters databaseConsumerParameters)
        {
            var kafkaConsumerProperties = new KafkaConsumerProperties
            {
                AutoCommitIntervalInMilliseconds = databaseConsumerParameters.KafkaAutoCommitIntervalInMilliseconds,
                AutoOffsetReset              = databaseConsumerParameters.KafkaAutoOffsetReset,
                ServerAddresses              = databaseConsumerParameters.KafkaListenerServerAddress,
                TopicName                    = databaseConsumerParameters.KafkaListenerTopicName,
                ConsumerGroupId              = databaseConsumerParameters.KafkaConsumerGroupId,
                MaximumParititionFetchBytes  = databaseConsumerParameters.KafkaMaxPartitionFetchBytes,
                UseSecureKafka               = databaseConsumerParameters.UseSecureKafka,
                SecurityProtocol             = databaseConsumerParameters.SecurityProtocol,
                CertificateLocation          = databaseConsumerParameters.CertificateLocation,
                KeyLocation                  = databaseConsumerParameters.KeyLocation,
                KeyPassword                  = databaseConsumerParameters.KeyPassword,
                CertificateAuthorityLocation = databaseConsumerParameters.CertificateAuthorityLocation,
            };

            return(kafkaConsumerProperties);
        }
예제 #5
0
        /// <summary>
        /// Gets the application parameters from the activation context.   Note that we cannot continue if there is an error here
        /// so the exception will cause the service to be unable to start, and that is the desired result.
        /// </summary>
        /// <param name="activationContext">The activation Context.</param>
        /// <returns>Application Parameters</returns>
        public static VotingDatabaseParameters GetDatabaseConsumerParameters(ICodePackageActivationContext activationContext)
        {
            var configurationPackage = activationContext.GetConfigurationPackageObject(ServiceFabricConfig);
            var consumerTopicName    = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                       .Parameters[nameof(KafkaListenerTopicName)].Value;
            var listenerServerAddressList = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                            .Parameters[nameof(KafkaListenerServerAddress)].Value.Split(',').ToList();
            var producerServerAddressList = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                            .Parameters[nameof(KafkaProducerServerAddress)].Value.Split(',').ToList();
            var producerTopicName = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                    .Parameters[nameof(KafkaProducerTopicName)].Value;
            var consumerGroupId = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(KafkaConsumerGroupId)].Value;
            var autoCommitIntervalInMilliseconds = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                                   .Parameters[nameof(KafkaAutoCommitIntervalInMilliseconds)].Value;
            var autoOffsetReset = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(KafkaAutoOffsetReset)].Value;
            var maxPartitionFetchByteSize = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                            .Parameters[nameof(KafkaMaxPartitionFetchBytes)].Value;
            var messagePollIntervalInMilliseconds = configurationPackage.Settings
                                                    .Sections[DatabaseConsumerConfigSection].Parameters[nameof(MessagePollIntervalInMilliseconds)].Value;
            var maxNumberOfConcurrentMessageProcessors = configurationPackage.Settings
                                                         .Sections[DatabaseConsumerConfigSection].Parameters[nameof(MaxNumberOfConcurrentMessageProcessors)]
                                                         .Value;
            var sampleBatchSize = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(SampleBatchSize)].Value;
            var sampleWaitTimeInMinutes = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                          .Parameters[nameof(SampleWaitTimeInMinutes)].Value;

            var databaseConnection = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                     .Parameters[nameof(DatabaseConnectionString)];
            var databaseConnectionString = databaseConnection.Value;

            if (databaseConnection.IsEncrypted)
            {
                // databaseConnectionString = databaseConnection.DecryptValue().ConvertToUnsecureString();
            }

            var waitTimeInSeconds = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                    .Parameters[nameof(WaitTimeinSeconds)].Value;
            var certificateLocation = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                      .Parameters[nameof(CertificateLocation)].Value;
            var keyLocation = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                              .Parameters[nameof(KeyLocation)].Value;
            var securityProtocol = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                   .Parameters[nameof(SecurityProtocol)].Value;

            var keyPasswordProp = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(KeyPassword)];
            var keyPassword = keyPasswordProp.Value;

            if (keyPasswordProp.IsEncrypted)
            {
                //keyPassword = keyPasswordProp.DecryptValue().ConvertToUnsecureString();
            }

            var certificateAuthorityLocation = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                               .Parameters[nameof(CertificateAuthorityLocation)].Value;
            var useSecureKafkaValue = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                      .Parameters[nameof(UseSecureKafka)].Value;

            var environmentName = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(EnvironmentName)].Value;

            var loggingPath = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                              .Parameters[nameof(LoggingPath)].Value;

            if (!int.TryParse(autoCommitIntervalInMilliseconds, out int autoCommitInterval))
            {
                autoCommitInterval = DefaultAutoCommitInterval;
            }

            if (!long.TryParse(maxPartitionFetchByteSize, out long maxFetchByteSize))
            {
                maxFetchByteSize = DefaultMaxFetchByteSize;
            }

            if (!int.TryParse(messagePollIntervalInMilliseconds, out int pollIntervalInMilliseconds))
            {
                pollIntervalInMilliseconds = DefaultPollIntervalInMilliseconds;
            }

            if (!int.TryParse(maxNumberOfConcurrentMessageProcessors, out int maxConcurrentProcessors))
            {
                maxConcurrentProcessors = DefaultMaxConcurrentProcessors;
            }

            if (!int.TryParse(sampleBatchSize, out int tempSampleBatchSize))
            {
                tempSampleBatchSize = DefaultSampleBatchSize;
            }

            if (!int.TryParse(waitTimeInSeconds, out int tempWaitTimeInSeconds))
            {
                tempWaitTimeInSeconds = DefaultWaitTimeInSeconds;
            }

            if (!int.TryParse(sampleWaitTimeInMinutes, out int tempSampleWaitTimeInMinutes))
            {
                tempSampleWaitTimeInMinutes = DefaultSampleWaitTimeInMinutes;
            }

            if (!bool.TryParse(useSecureKafkaValue, out bool useSecureKafka))
            {
                useSecureKafka = DefaultUseSecureKafka;
            }

            string loggingLevel = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                  .Parameters[nameof(LoggingLevel)].Value;

            if (!Enum.TryParse(loggingLevel, true, out SourceLevels sourceLevels))
            {
                sourceLevels = SourceLevels.Information;
            }

            string logFileRollSize = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                     .Parameters[nameof(LogFileRollSize)].Value;

            if (!int.TryParse(logFileRollSize, out int tempLogFileRollSize))
            {
                tempLogFileRollSize = DefaultTempLogFileRollSize;
            }

            var isClientLoggingEnabledValue = configurationPackage.Settings.Sections[DatabaseConsumerConfigSection]
                                              .Parameters[nameof(IsKafkaClientLoggingEnabled)].Value;

            if (!bool.TryParse(isClientLoggingEnabledValue, out bool isClientLoggingEnabled))
            {
                isClientLoggingEnabled = DefaultIsClientLoggingEnabled;
            }

            var result = new VotingDatabaseParameters
            {
                KafkaAutoCommitIntervalInMilliseconds = autoCommitInterval,
                KafkaAutoOffsetReset                   = autoOffsetReset,
                KafkaListenerServerAddress             = listenerServerAddressList,
                KafkaListenerTopicName                 = consumerTopicName,
                KafkaConsumerGroupId                   = consumerGroupId,
                KafkaMaxPartitionFetchBytes            = maxFetchByteSize,
                MessagePollIntervalInMilliseconds      = new TimeSpan(0, 0, 0, 0, pollIntervalInMilliseconds),
                MaxNumberOfConcurrentMessageProcessors = maxConcurrentProcessors,
                DatabaseConnectionString               = databaseConnectionString,
                SampleWaitTimeInMinutes                = new TimeSpan(0, tempSampleWaitTimeInMinutes, 0),
                WaitTimeinSeconds          = new TimeSpan(0, 0, tempWaitTimeInSeconds),
                SampleBatchSize            = tempSampleBatchSize,
                KafkaProducerServerAddress = producerServerAddressList,
                KafkaProducerTopicName     = producerTopicName,
                CertificateLocation        = certificateLocation,
                KeyLocation                  = keyLocation,
                SecurityProtocol             = securityProtocol,
                KeyPassword                  = keyPassword,
                CertificateAuthorityLocation = certificateAuthorityLocation,
                UseSecureKafka               = useSecureKafka,
                EnvironmentName              = environmentName,
                LoggingPath                  = loggingPath,
                LoggingLevel                 = sourceLevels,
                LogFileRollSize              = tempLogFileRollSize,
                IsKafkaClientLoggingEnabled  = isClientLoggingEnabled
            };

            return(result);
        }