Open Kilda Java Documentation
OrderAwareWorker.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.kafka.producer;
17 
18 import org.apache.kafka.clients.producer.Callback;
19 import org.apache.kafka.clients.producer.Producer;
20 import org.apache.kafka.clients.producer.ProducerRecord;
21 import org.apache.kafka.clients.producer.RecordMetadata;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 
25 import java.util.concurrent.Future;
26 
27 public class OrderAwareWorker extends AbstractWorker {
28  private static final Logger log = LoggerFactory.getLogger(OrderAwareWorker.class);
29 
30  private int deep = 1;
31  private long expireAt = 0;
32  private Integer partition;
33 
35  super(worker.getKafkaProducer(), worker.getTopic());
36 
37  if (worker instanceof OrderAwareWorker) {
38  OrderAwareWorker other = (OrderAwareWorker) worker;
39  this.deep += other.deep;
40  this.partition = other.partition;
41  }
42  }
43 
44  public OrderAwareWorker(Producer<String, String> kafkaProducer, String topic) {
45  super(kafkaProducer, topic);
46  }
47 
48  @Override
49  protected synchronized SendStatus send(String payload, Callback callback) {
50  ProducerRecord<String, String> record;
51  if (partition == null) {
52  record = new ProducerRecord<>(getTopic(), payload);
53  } else {
54  record = new ProducerRecord<>(getTopic(), partition, null, payload);
55  }
56 
57  Future<RecordMetadata> promise = getKafkaProducer().send(record, callback);
58  if (partition == null) {
59  try {
60  partition = promise.get().partition();
61  } catch (Exception e) {
62  log.error("Can't determine kafka topic partition for order aware writing, due to send error "
63  + "(will retry on next send attempt). Error: {}", e.toString());
64  }
65  }
66 
67  return new SendStatus(promise);
68  }
69 
70  @Override
71  void deactivate(long transitionPeriod) {
72  if (deep == 0) {
73  throw new IllegalStateException("Number of .diable() calls have overcome number of .enable() calls");
74  }
75 
76  deep -= 1;
77  if (deep == 0) {
78  expireAt = System.currentTimeMillis() + transitionPeriod;
79  }
80  }
81 
82  @Override
83  boolean isActive() {
84  if (0 < deep) {
85  return true;
86  }
87  return System.currentTimeMillis() < expireAt;
88  }
89 }
synchronized SendStatus send(String payload, Callback callback)
OrderAwareWorker(Producer< String, String > kafkaProducer, String topic)