Class: KafkaETLBase::Base
- Inherits:
-
Object
- Object
- KafkaETLBase::Base
- Defined in:
- lib/kafka_etl_base/base.rb
Instance Method Summary collapse
-
#initialize(zookeeper, kafka_brokers, kafka_topic, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #proccess_thread(zk, part_no) ⇒ Object
- #process ⇒ Object
- #process_messages(cons, part_no) ⇒ Object
- #process_partition(part_no) ⇒ Object
Constructor Details
#initialize(zookeeper, kafka_brokers, kafka_topic, opts = {}) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kafka_etl_base/base.rb', line 10 def initialize(zookeeper, kafka_brokers, kafka_topic, opts={}) @num_threads = opts[:num_threads] ? opts[:num_threads] : 2 @max_fetch_bytes = opts[:max_fetch_bytes] ? opts[:max_fetch_bytes] : 5_000_000 @min_fetch_bytes = opts[:min_fetch_bytes] ? opts[:min_fetch_bytes] : 0 @kafka_client_id = opts[:kafka_client_id] ? opts[:kafka_client_id] : "my_consumer" @kafka_part_num = opts[:kafka_topic_part_num] ? opts[:kafka_topic_part_num] : 2 @zookeeper = zookeeper @kafka_brokers = kafka_brokers @kafka_topic = kafka_topic $log = Logger.new(STDOUT) if $log.nil? $log.debug("zookeeper: #{@zookeeper}") $log.debug("kafka_brokers: #{@kafka_brokers}") $log.debug("kafka_topic: #{@kafka_topic}") $log.debug("kafka_client_id: #{@kafka_client_id}") @partition_shuffle = true @mutex = Mutex.new @total_procs = 0 end |
Instance Method Details
#proccess_thread(zk, part_no) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/kafka_etl_base/base.rb', line 107 def proccess_thread(zk, part_no) zk_part_node = "/part_offset_#{part_no}" num_cur_part_procs = 0 if ! zk.exists?(zk_part_node) zk.create(zk_part_node, "0") end offset = nil begin value, stat = zk.get(zk_part_node) offset = value.to_i if offset == 0 part_offset = :earliest_offset #part_offset = :latest_offset else part_offset = offset end rescue ZK::Exceptions::NoNode => e part_offset = :earliest_offset #part_offset = :latest_offset end $log.debug "part: #{part_no}, offset: #{part_offset}" cons = Poseidon::PartitionConsumer.consumer_for_partition(@kafka_client_id, @kafka_brokers, @kafka_topic, part_no, part_offset, :max_wait_ms => 0, :max_bytes => @max_fetch_bytes, :min_bytes => @min_fetch_bytes ) begin num_cur_part_procs += (cons, part_no) next_offset = cons.next_offset last_offset = cons.highwater_mark $log.info "part: #{part_no}, next_offset: #{next_offset}, last_offset: #{last_offset}, proc: #{num_cur_part_procs}" @mutex.synchronize do @total_procs += num_cur_part_procs end # set next offset to zookeper zk.set(zk_part_node, next_offset.to_s) if next_offset >= offset return last_offset - next_offset rescue Poseidon::Connection::ConnectionFailedError $log.error "kafka connection failed" return 0 rescue BackendError $log.debug "backend error" return 0 ensure cons.close if ! cons.nil? end rescue Poseidon::Errors::OffsetOutOfRange => e zk.set(zk_part_node, "0") $log.error e.inspect return 0 rescue Poseidon::Errors::NotLeaderForPartition, Poseidon::Errors::UnableToFetchMetadata => e $log.error e.inspect return 0 end |
#process ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kafka_etl_base/base.rb', line 36 def process() @total_procs = 0 zk = ZK.new(@zookeeper) begin #zk.create("/", ignore: :node_exists) if zk.exists?("/stop_etl") $log.info("zk: /stop_etl exist.") $log.info("stop message processing.") return end ensure zk.close! end seq = [ * 0 ... @kafka_part_num ] seq.shuffle! if @partition_shuffle == true r = Parallel.each(seq, :in_threads => @num_threads) do |part_no| zk_th = nil begin zk_th = ZK.new(@zookeeper) zk_lock = "lock_hdfs_part_#{part_no}" locker = zk_th.locker(zk_lock) begin if locker.lock! remain = proccess_thread(zk_th, part_no) else $log.info("part: #{part_no} is already locked skip") end ensure begin locker.unlock! rescue => e $log.error(e.inspect) $log.error(e.backtrace) end end rescue ZK::Exceptions::ConnectionLoss => e $log.error(e.inspect) $log.error(e.backtrace) ensure begin zk_th.close! if ! zk_th.nil? rescue end end end $log.info "total procs: #{@total_procs}" end |
#process_messages(cons, part_no) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/kafka_etl_base/base.rb', line 171 def (cons, part_no) = cons.fetch .each do |m| key = m.key val = m.value puts "part: #{part_no}, key: #{key}, val: #{val}" end .size end |
#process_partition(part_no) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/kafka_etl_base/base.rb', line 88 def process_partition(part_no) zk = ZK.new(@zookeeper) begin zk_lock = "lock_hdfs_part_#{part_no}" locker = zk.locker(zk_lock) begin if locker.lock! remain = proccess_thread(zk, part_no) else $log.info("part: #{part_no} is already locked skip") end ensure locker.unlock! end ensure zk.close end end |