Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #544: Kamelet component - optimize as we did for direct component #553

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class KameletEndpointConfigurer extends PropertyConfigurerSupport impleme
map.put("exceptionHandler", org.apache.camel.spi.ExceptionHandler.class);
map.put("exchangePattern", org.apache.camel.ExchangePattern.class);
map.put("block", boolean.class);
map.put("failIfNoConsumers", boolean.class);
map.put("kameletProperties", java.util.Map.class);
map.put("lazyStartProducer", boolean.class);
map.put("timeout", long.class);
Expand All @@ -45,6 +46,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj
case "exceptionHandler": target.setExceptionHandler(property(camelContext, org.apache.camel.spi.ExceptionHandler.class, value)); return true;
case "exchangepattern":
case "exchangePattern": target.setExchangePattern(property(camelContext, org.apache.camel.ExchangePattern.class, value)); return true;
case "failifnoconsumers":
case "failIfNoConsumers": target.setFailIfNoConsumers(property(camelContext, boolean.class, value)); return true;
case "kameletproperties":
case "kameletProperties": target.setKameletProperties(property(camelContext, java.util.Map.class, value)); return true;
case "lazystartproducer":
Expand Down Expand Up @@ -73,6 +76,8 @@ public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
case "exceptionHandler": return target.getExceptionHandler();
case "exchangepattern":
case "exchangePattern": return target.getExchangePattern();
case "failifnoconsumers":
case "failIfNoConsumers": return target.isFailIfNoConsumers();
case "kameletproperties":
case "kameletProperties": return target.getKameletProperties();
case "lazystartproducer":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"exceptionHandler": { "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored." },
"exchangePattern": { "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut", "InOptionalOut" ], "deprecated": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." },
"block": { "kind": "parameter", "displayName": "Block", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "If sending a message to a direct endpoint which has no active consumer, then we can tell the producer to block and wait for the consumer to become active." },
"kameletProperties": { "kind": "parameter", "displayName": "Kamelet Properties", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "deprecated": false, "secret": false, "defaultValue": "true", "description": "Custom properties for kamelet" },
"failIfNoConsumers": { "kind": "parameter", "displayName": "Fail If No Consumers", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": true, "description": "Whether the producer should fail by throwing an exception, when sending to a kamelet endpoint with no active consumers." },
"kameletProperties": { "kind": "parameter", "displayName": "Kamelet Properties", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "deprecated": false, "secret": false, "description": "Custom properties for kamelet" },
"lazyStartProducer": { "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing." },
"timeout": { "kind": "parameter", "displayName": "Timeout", "group": "producer", "label": "producer", "required": false, "type": "integer", "javaType": "long", "deprecated": false, "secret": false, "defaultValue": 30000, "description": "The timeout value to use if block is enabled." },
"basicPropertyBinding": { "kind": "parameter", "displayName": "Basic Property Binding", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -38,6 +39,7 @@
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.URISupport;
import org.apache.camel.util.UnsafeUriCharactersEncoder;
import org.slf4j.Logger;
Expand All @@ -54,17 +56,22 @@
public class KameletComponent extends DefaultComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(KameletComponent.class);

private final Map<String, KameletConsumer> consumers;
private final LifecycleHandler lifecycleHandler;
// active consumers
private final Map<String, KameletConsumer> consumers = new HashMap<>();
// counter that is used for producers to keep track if any consumer was added/removed since they last checked
// this is used for optimization to avoid each producer to get consumer for each message processed
// (locking via synchronized, and then lookup in the map as the cost)
// consumers and producers are only added/removed during startup/shutdown or if routes is manually controlled
private volatile int stateCounter;

private final LifecycleHandler lifecycleHandler = new LifecycleHandler();

@Metadata(label = "producer", defaultValue = "true")
private boolean block = true;
@Metadata(label = "producer", defaultValue = "30000")
private long timeout = 30000L;

public KameletComponent() {
this.lifecycleHandler = new LifecycleHandler();
this.consumers = new ConcurrentHashMap<>();
}

@Override
Expand Down Expand Up @@ -194,7 +201,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
// Note that at the moment, there's no enforcement around `source`
// and `sink' to be defined on the right side (producer or consumer)
//
endpoint = new KameletEndpoint(uri, this, templateId, routeId, consumers);
endpoint = new KameletEndpoint(uri, this, templateId, routeId);

// forward component properties
endpoint.setBlock(block);
Expand All @@ -203,7 +210,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
// set endpoint specific properties
setProperties(endpoint, parameters);
} else {
endpoint = new KameletEndpoint(uri, this, templateId, routeId, consumers) {
endpoint = new KameletEndpoint(uri, this, templateId, routeId) {
@Override
protected void doInit() throws Exception {
super.doInit();
Expand Down Expand Up @@ -266,6 +273,53 @@ public void setTimeout(long timeout) {
this.timeout = timeout;
}

int getStateCounter() {
return stateCounter;
}

public void addConsumer(String key, KameletConsumer consumer) {
synchronized (consumers) {
if (consumers.putIfAbsent(key, consumer) != null) {
throw new IllegalArgumentException(
"Cannot add a 2nd consumer to the same endpoint: " + key
+ ". KameletEndpoint only allows one consumer.");
}
// state changed so inc counter
stateCounter++;
consumers.notifyAll();
}
}

public void removeConsumer(String key, KameletConsumer consumer) {
synchronized (consumers) {
consumers.remove(key, consumer);
// state changed so inc counter
stateCounter++;
consumers.notifyAll();
}
}

protected KameletConsumer getConsumer(String key, boolean block, long timeout) throws InterruptedException {
synchronized (consumers) {
KameletConsumer answer = consumers.get(key);
if (answer == null && block) {
StopWatch watch = new StopWatch();
for (;;) {
answer = consumers.get(key);
if (answer != null) {
break;
}
long rem = timeout - watch.taken();
if (rem <= 0) {
break;
}
consumers.wait(rem);
}
}
return answer;
}
}

@Override
protected void doInit() throws Exception {
getCamelContext().addLifecycleStrategy(lifecycleHandler);
Expand All @@ -278,13 +332,12 @@ protected void doInit() throws Exception {
}

@Override
protected void doStop() throws Exception {
protected void doShutdown() throws Exception {
getCamelContext().getLifecycleStrategies().remove(lifecycleHandler);

ServiceHelper.stopService(consumers.values());
ServiceHelper.stopAndShutdownService(consumers);
consumers.clear();

super.doStop();
super.doShutdown();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@
import org.apache.camel.support.DefaultConsumer;

final class KameletConsumer extends DefaultConsumer implements ShutdownAware, Suspendable {
public KameletConsumer(KameletEndpoint endpoint, Processor processor) {

private final KameletComponent component;
private final String key;

public KameletConsumer(KameletEndpoint endpoint, Processor processor, String key) {
super(endpoint, processor);
this.component = endpoint.getComponent();
this.key = key;
}

@Override
Expand All @@ -34,22 +40,25 @@ public KameletEndpoint getEndpoint() {

@Override
protected void doStart() throws Exception {
getEndpoint().addConsumer(this);
super.doStart();
component.addConsumer(key, this);
}

@Override
protected void doStop() throws Exception {
getEndpoint().removeConsumer(this);
component.removeConsumer(key, this);
super.doStop();
}

@Override
protected void doSuspend() throws Exception {
getEndpoint().removeConsumer(this);
component.removeConsumer(key, this);
}

@Override
protected void doResume() throws Exception {
getEndpoint().addConsumer(this);
// resume by using the start logic
component.addConsumer(key, this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.kamelet;

import org.apache.camel.CamelExchangeException;
import org.apache.camel.Exchange;

public class KameletConsumerNotAvailableException extends CamelExchangeException {

public KameletConsumerNotAvailableException(String message, Exchange exchange) {
super(message, exchange);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@
import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StopWatch;

@UriEndpoint(
firstVersion = "3.5.0",
scheme = "kamelet",
syntax = "kamelet:templateId/routeId",
title = "Kamelet",
lenientProperties = true,
category = Category.CORE)
firstVersion = "3.5.0",
scheme = "kamelet",
syntax = "kamelet:templateId/routeId",
title = "Kamelet",
lenientProperties = true,
category = Category.CORE)
public class KameletEndpoint extends DefaultEndpoint {

private final String key;

@Metadata(required = true)
@UriPath(description = "The Route Template ID")
private final String templateId;
@Metadata(required = false)
@Metadata
@UriPath(description = "The Route ID", defaultValueNote = "The ID will be auto-generated if not provided")
private final String routeId;

@UriParam(label = "producer", defaultValue = "true")
private boolean block = true;
@UriParam(label = "producer", defaultValue = "30000")
private long timeout = 30000L;
@UriParam(label = "producer", defaultValue = "true")

@UriParam(label = "producer")
private final Map<String, Object> kameletProperties;
private final Map<String, KameletConsumer> consumers;
private final String key;
@UriParam(label = "producer", defaultValue = "true")
private boolean failIfNoConsumers = true;

public KameletEndpoint(
String uri,
KameletComponent component,
String templateId,
String routeId,
Map<String, KameletConsumer> consumers) {
String routeId) {

super(uri, component);

Expand All @@ -73,7 +73,6 @@ public KameletEndpoint(
this.routeId = routeId;
this.key = templateId + "/" + routeId;
this.kameletProperties = new HashMap<>();
this.consumers = consumers;
}

public boolean isBlock() {
Expand Down Expand Up @@ -101,6 +100,18 @@ public void setTimeout(long timeout) {
this.timeout = timeout;
}

public boolean isFailIfNoConsumers() {
return failIfNoConsumers;
}

/**
* Whether the producer should fail by throwing an exception, when sending to a kamelet endpoint with no active
* consumers.
*/
public void setFailIfNoConsumers(boolean failIfNoConsumers) {
this.failIfNoConsumers = failIfNoConsumers;
}

@Override
public KameletComponent getComponent() {
return (KameletComponent) super.getComponent();
Expand Down Expand Up @@ -140,58 +151,14 @@ public Map<String, Object> getKameletProperties() {

@Override
public Producer createProducer() throws Exception {
return new KameletProducer(this);
return new KameletProducer(this, key);
}

@Override
public Consumer createConsumer(Processor processor) throws Exception {
Consumer answer = new KameletConsumer(this, processor);
Consumer answer = new KameletConsumer(this, processor, key);
configureConsumer(answer);
return answer;
}

// *********************************
//
// Helpers
//
// *********************************

void addConsumer(KameletConsumer consumer) {
synchronized (consumers) {
if (consumers.putIfAbsent(key, consumer) != null) {
throw new IllegalArgumentException(
"Cannot add a 2nd consumer to the same endpoint. Endpoint " + this + " only allows one consumer.");
}
consumers.notifyAll();
}
}

void removeConsumer(KameletConsumer consumer) {
synchronized (consumers) {
consumers.remove(key, consumer);
consumers.notifyAll();
}
}

KameletConsumer getConsumer() throws InterruptedException {
synchronized (consumers) {
KameletConsumer answer = consumers.get(key);
if (answer == null && block) {
StopWatch watch = new StopWatch();
for (; ; ) {
answer =consumers.get(key);
if (answer != null) {
break;
}
long rem = timeout - watch.taken();
if (rem <= 0) {
break;
}
consumers.wait(rem);
}
}

return answer;
}
}
}
Loading