use of com.ibm.streamsx.kafka.clients.consumer.ControlPortAction in project streamsx.kafka by IBMStreams.
the class AbstractKafkaConsumerOperator method process.
@Override
public void process(StreamingInput<Tuple> stream, Tuple tuple) throws Exception {
synchronized (monitor) {
logger.info("process >>> ENTRY");
boolean interrupted = false;
try {
final ConsumerClient consumer = consumerRef.get();
logger.info("current consumer implementation: " + consumer);
ControlPortAction actn = ControlPortAction.fromJSON(tuple.getString(0));
final ControlPortActionType action = actn.getActionType();
if (consumer.supports(actn)) {
logger.info("consumer implementation supports " + action);
consumer.onControlPortAction(actn);
} else {
if ((consumer instanceof DummyConsumerClient) && (action == ControlPortActionType.ADD_ASSIGNMENT || action == ControlPortActionType.ADD_SUBSCRIPTION)) {
logger.info("replacing ConsumerClient by a version that supports " + action);
// we can change the client implementation
if (consumer.isProcessing()) {
consumer.onShutdown(SHUTDOWN_TIMEOUT, SHUTDOWN_TIMEOUT_TIMEUNIT);
}
final ConsumerClientBuilder builder;
if (action == ControlPortActionType.ADD_SUBSCRIPTION) {
if (crContext != null) {
logger.error("topic subscription via control port is not supported when the operator is used in a consistent region. Ignoring " + actn.getJson());
nFailedControlTuples.increment();
logger.info("process <<< EXIT");
return;
}
builder = this.groupEnabledClientBuilder;
} else {
if (this.groupIdSpecified) {
logger.warn(MsgFormatter.format("A group.id is specified. The ''{0}'' operator " + "will NOT participate in a consumer group because the operator assigns partitions.", getOperatorContext().getName()));
}
builder = this.staticAssignClientBuilder;
}
logger.info("Using client builder: " + builder);
final ConsumerClient newClient = builder.build();
logger.info(MsgFormatter.format("consumer client {0} created", newClient.getClass().getName()));
try {
newClient.startConsumer();
if (consumerRef.compareAndSet(consumer, newClient)) {
logger.info(MsgFormatter.format("consumer client implementation {0} replaced by {1}", consumer.getClass().getName(), newClient.getClass().getName()));
newClient.onControlPortAction(actn);
} else {
logger.warn(MsgFormatter.format("consumer client replacement failed"));
newClient.onShutdown(SHUTDOWN_TIMEOUT, SHUTDOWN_TIMEOUT_TIMEUNIT);
nFailedControlTuples.increment();
}
} catch (KafkaClientInitializationException e) {
logger.error(e.getLocalizedMessage(), e);
logger.error("root cause: " + e.getRootCause());
nFailedControlTuples.increment();
throw e;
}
} else {
// unsupported action
logger.error("Could not process control tuple. Action " + action + " is not supported by the '" + consumer.getClass().getName() + "' ConsumerClient implementation. Tuple: '" + tuple + "'");
nFailedControlTuples.increment();
}
}
} catch (ControlPortJsonParseException e) {
logger.error("Could not process control tuple. Parsing JSON '" + e.getJson() + "' failed.");
logger.error(e.getLocalizedMessage(), e);
nFailedControlTuples.increment();
} catch (InterruptedException e) {
// interrupted during shutdown
interrupted = true;
nFailedControlTuples.increment();
} catch (Exception e) {
e.printStackTrace();
logger.error("Could not process control tuple: '" + tuple + "':" + e);
logger.error(e.getLocalizedMessage(), e);
nFailedControlTuples.increment();
} finally {
final ConsumerClient consumer = consumerRef.get();
if (!interrupted && consumer.isSubscribedOrAssigned()) {
logger.info("sendStartPollingEvent ...");
consumer.sendStartPollingEvent();
}
logger.info("process <<< EXIT");
}
}
}
Aggregations