Esempio n. 1
0
        protected ProducerActorBase(IActorRef client, IActorRef lookup, IActorRef cnxPool, string topic, ProducerConfigurationData conf, ISchema <T> schema, ProducerInterceptors <T> interceptors, ClientConfigurationData configurationData)
        {
            ClientConfiguration = configurationData;
            Client = client;
            _topic = topic;

            if (conf.BatchingEnabled && conf.AckReceivedListerner == null)
            {
                conf.AckReceivedListerner = (acked) =>
                {
                    Context.System.Log.Info($"AckReceived(ledger-id:{acked.LedgerId}, entery-id:{acked.EntryId}, sequence-id:{acked.SequenceId}, highest-sequence-id:{acked.HighestSequenceId})");
                };
            }
            Conf         = conf;
            Schema       = schema;
            Interceptors = interceptors;
            SchemaCache  = new Dictionary <SchemaHash, byte[]>();
            if (!conf.MultiSchema)
            {
                _multiSchemaMode = MultiSchemaMode.Disabled;
            }
            var pName = ProducerName().GetAwaiter().GetResult();

            State = new HandlerState(lookup, cnxPool, topic, Context.System, pName);
        }
Esempio n. 2
0
        public PartitionedProducer(IActorRef client, IActorRef lookup, IActorRef cnxPool, IActorRef idGenerator, string topic, ProducerConfigurationData conf, int numPartitions, ISchema <T> schema, ProducerInterceptors <T> interceptors, ClientConfigurationData clientConfiguration) : base(client, lookup, cnxPool, topic, conf, schema, interceptors, clientConfiguration)
        {
            _cnxPool       = cnxPool;
            _lookup        = lookup;
            _self          = Self;
            _producers     = new List <IActorRef>();
            _generator     = idGenerator;
            _context       = Context;
            _producers     = new List <IActorRef>(numPartitions);
            _topicMetadata = new TopicMetadata(numPartitions);
            _stats         = clientConfiguration.StatsIntervalSeconds > 0 ? new ProducerStatsRecorder(Context.System, "PartitionedProducer", topic, conf.MaxPendingMessages) : null;
            _log           = Context.GetLogger();
            var maxPendingMessages = Math.Min(conf.MaxPendingMessages, conf.MaxPendingMessagesAcrossPartitions / numPartitions);

            conf.MaxPendingMessages = maxPendingMessages;

            switch (conf.MessageRoutingMode)
            {
            case MessageRoutingMode.ConsistentHashingMode:
                _router = Context.System.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup()), $"Partition{DateTimeHelper.CurrentUnixTimeMillis()}");
                break;

            case MessageRoutingMode.BroadcastMode:
                _router = Context.System.ActorOf(Props.Empty.WithRouter(new BroadcastGroup()), $"Partition{DateTimeHelper.CurrentUnixTimeMillis()}");
                break;

            case MessageRoutingMode.RandomMode:
                _router = Context.System.ActorOf(Props.Empty.WithRouter(new RandomGroup()), $"Partition{DateTimeHelper.CurrentUnixTimeMillis()}");
                break;

            default:
                _router = Context.System.ActorOf(Props.Empty.WithRouter(new RoundRobinGroup()), $"Partition{DateTimeHelper.CurrentUnixTimeMillis()}");
                break;
            }

            // start track and auto subscribe partition increasement
            if (conf.AutoUpdatePartitions)
            {
                _partitionsAutoUpdateTimeout = _context.System.Scheduler.ScheduleTellRepeatedlyCancelable(TimeSpan.FromSeconds(conf.AutoUpdatePartitionsIntervalSeconds), TimeSpan.FromSeconds(conf.AutoUpdatePartitionsIntervalSeconds), Self, ExtendTopics.Instance, ActorRefs.NoSender);
            }
            Receive <Flush>(_ => {
                Flush();
            });
            ReceiveAsync <Connect>(async _ =>
            {
                await Start().ConfigureAwait(false);
            });
            Receive <TriggerFlush>(_ => {
                TriggerFlush();
            });
            ReceiveAsync <ExtendTopics>(async _ =>
            {
                var t = topic;
                await OnTopicsExtended(new List <string> {
                    t
                });
            });

            ReceiveAsync <InternalSend <T> >(async m =>
            {
                try
                {
                    //get excepyion vai out
                    await InternalSend(m.Message);
                }
                catch (Exception ex)
                {
                    Sender.Tell(ex);
                    _log.Error(ex.ToString());
                }
            });
            Receive <InternalSendWithTxn <T> >(m =>
            {
                try
                {
                    InternalSendWithTxn(m.Message, m.Txn);
                }
                catch (Exception ex)
                {
                    Sender.Tell(ex);
                    _log.Error(ex.ToString());
                }
            });
            ReceiveAny(any => _router.Forward(any));
        }