private void Commit(bool startNewTransaction) { log.LogDebug($"{logPrefix}Comitting"); if (state == TaskState.CLOSED) { throw new IllegalStateException($"Illegal state {state} while committing active task {Id}"); } else if (state == TaskState.SUSPENDED || state == TaskState.CREATED || state == TaskState.RUNNING || state == TaskState.RESTORING) { FlushState(); if (eosEnabled) { producer.SendOffsetsToTransaction(GetPartitionsWithOffset(), GroupMetadata, configuration.TransactionTimeout); producer.CommitTransaction(configuration.TransactionTimeout); transactionInFlight = false; if (startNewTransaction) { producer.BeginTransaction(); transactionInFlight = true; } consumedOffsets.Clear(); } else { try { consumer.Commit(GetPartitionsWithOffset()); consumedOffsets.Clear(); } catch (TopicPartitionOffsetException e) { log.LogInformation($"{logPrefix}Committing failed with a non-fatal error: {e.Message}, we can ignore this since commit may succeed still"); } catch (KafkaException e) { // TODO : get info about offset committing log.LogError($"{logPrefix}Error during committing offset ......", e); } } commitNeeded = false; commitRequested = false; commitSensor.Record(); } else { throw new IllegalStateException($"Unknown state {state} while committing active task {Id}"); } }
/// <summary> /// 事务版本 貌似没必要 要求 配置TransactionalId,Acks必须等于all /// </summary> /// <param name="topic"></param> /// <param name="message"></param> /// <returns></returns> public async Task <DeliveryResult <TKey, TValue> > ProduceAsync2(string topic, Message <TKey, TValue> message) { DeliveryResult <TKey, TValue> result = null; try { _producer.InitTransactions(DefaultTimeout); _producer.BeginTransaction(); result = await this._producer.ProduceAsync(topic, message); _producer.CommitTransaction(DefaultTimeout); } catch (Exception) { _producer.AbortTransaction(DefaultTimeout); throw; } return(result); }
private void SendMessages(IProducer <Null, byte[]> producer, KafkaSendMessageArgs args, byte[] data, byte[] dataGuid, string guid) { producer.InitTransactions(TimeSpan.FromSeconds(10)); producer.BeginTransaction(); for (int i = 0; i < args.MessageCount - 1; i++) { producer.Produce(args.Topic, new Message <Null, byte[]> { Value = data }); } producer.Produce(args.Topic, new Message <Null, byte[]> { Value = dataGuid }); producer.CommitTransaction(); var now = DateTimeOffset.Now.ToUnixTimeMilliseconds(); SendInitialTestResult(args, guid, now); _logger.LogInformation($"Kafka sender: Guid = {guid} - Sent at: {DateTimeOffset.FromUnixTimeMilliseconds(now).AddHours(3).ToString("yyyy-MM-dd hh:mm:ss.fff tt")}"); //cuz im in +0300 atm }
private void Commit(bool startNewTransaction) { log.Debug($"{logPrefix}Comitting"); FlushState(); if (eosEnabled) { producer.SendOffsetsToTransaction(GetPartitionsWithOffset(), GroupMetadata, configuration.TransactionTimeout); producer.CommitTransaction(configuration.TransactionTimeout); transactionInFlight = false; if (startNewTransaction) { producer.BeginTransaction(); transactionInFlight = true; } consumedOffsets.Clear(); } else { try { consumer.Commit(GetPartitionsWithOffset()); consumedOffsets.Clear(); } catch (TopicPartitionOffsetException e) { log.Info($"{logPrefix}Committing failed with a non-fatal error: {e.Message}, we can ignore this since commit may succeed still"); } catch (KafkaException e) { // TODO : get info about offset committing log.Error($"{logPrefix}Error during committing offset ......", e); } } commitNeeded = false; commitRequested = false; }
public void CommitTransaction(TimeSpan timeout) { _producer.CommitTransaction(timeout); }