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

yaml: add support for LoadBalancer and CircuitBreaker EIPs #183 #299

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,115 @@
/*
* 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.k.loader.yaml.parser;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonAlias;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.model.CircuitBreakerDefinition;
import org.apache.camel.model.HystrixConfigurationDefinition;
import org.apache.camel.model.OnFallbackDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.Resilience4jConfigurationCommon;
import org.apache.camel.model.Resilience4jConfigurationDefinition;
import org.apache.camel.reifier.CircuitBreakerReifier;
import org.apache.camel.reifier.OnFallbackReifier;

@YAMLStepParser("circuit-breaker")
public class CircuitBreakerStepParser implements ProcessorStepParser {
@Override
public ProcessorDefinition<?> toProcessor(Context context) {
CBDefinition definition = context.node(CBDefinition.class);

ProcessorDefinition<?> processor = StepParserSupport.convertSteps(
context,
definition.delegate,
definition.steps
);

if (definition.onFallback != null) {
StepParserSupport.convertSteps(
context,
definition.onFallback,
definition.onFallback.steps
);

definition.delegate.setOnFallback(definition.onFallback);
}

return processor;
}

@YAMLNodeDefinition(reifiers = CircuitBreakerReifier.class)
public static final class CBDefinition {
public CircuitBreakerDefinition delegate = new CircuitBreakerDefinition();
public FBDefinition onFallback;

public List<Step> steps;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When upgrading to Camel 3.3 then we also have microprofile-fault-tolerance as CB

public HystrixConfigurationDefinition getHystrixConfiguration() {
return delegate.getHystrixConfiguration();
}

public void setHystrixConfiguration(HystrixConfigurationDefinition hystrixConfiguration) {
delegate.setHystrixConfiguration(hystrixConfiguration);
}

public Resilience4jConfigurationCommon getResilience4jConfiguration() {
return delegate.getResilience4jConfiguration();
}

public void setResilience4jConfiguration(Resilience4jConfigurationDefinition resilience4jConfiguration) {
delegate.setResilience4jConfiguration(resilience4jConfiguration);
}

public String getConfigurationRef() {
return delegate.getConfigurationRef();
}

public void setConfigurationRef(String configurationRef) {
delegate.setConfigurationRef(configurationRef);
}

public FBDefinition getOnFallback() {
return onFallback;
}

public void setOnFallback(FBDefinition onFallback) {
this.onFallback = onFallback;
}
}

@YAMLNodeDefinition(reifiers = OnFallbackReifier.class)
public static final class FBDefinition extends OnFallbackDefinition {
public List<Step> steps;

@Override
public String getFallbackViaNetwork() {
return super.getFallbackViaNetwork();
}

@JsonAlias({"fallback-via-network", "via-network"})
@Override
public void setFallbackViaNetwork(String fallbackViaNetwork) {
super.setFallbackViaNetwork(fallbackViaNetwork);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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.k.loader.yaml.parser;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.camel.Expression;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.model.ExpressionSubElementDefinition;
import org.apache.camel.model.LoadBalanceDefinition;
import org.apache.camel.model.LoadBalancerDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.RandomLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.RoundRobinLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.StickyLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.TopicLoadBalancerDefinition;
import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition;
import org.apache.camel.reifier.LoadBalanceReifier;

@YAMLStepParser("load-balance")
public class LoadBalanceStepParser implements ProcessorStepParser {
@Override
public ProcessorDefinition<?> toProcessor(Context context) {
Definition definition = context.node(Definition.class);

return StepParserSupport.convertSteps(
context,
definition,
definition.steps
);
}

@YAMLNodeDefinition(reifiers = LoadBalanceReifier.class)
public static final class Definition extends LoadBalanceDefinition {
public List<Step> steps;

@JsonAlias({"load-balancer-type", "type"})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@Override
public void setLoadBalancerType(LoadBalancerDefinition loadbalancer) {
super.setLoadBalancerType(loadbalancer);
}

@Override
public LoadBalancerDefinition getLoadBalancerType() {
return super.getLoadBalancerType();
}

@JsonAlias("random")
public void setRandom(RandomLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias({"customLoadBalancer", "custom"})
public void setCustomLoadBalancer(CustomLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("failover")
public void setFailover(FailoverLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("sticky")
public void setSticky(Sticky definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("topic")
public void setTopic(TopicLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("weighted")
public void setWeighted(WeightedLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("roundRobin")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder if the alias should also have the dash variant? round-robin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jackson should be able to handle both the cases but I will add a test

public void setRoundRobin(RoundRobinLoadBalancerDefinition definition) {
if (getLoadBalancerType() != null) {
throw new IllegalArgumentException("A load-balancer has already been set");
}
setLoadBalancerType(definition);
}

@JsonAlias("custom")
public void setCustom(CustomLoadBalancerDefinition definition) {
setCustomLoadBalancer(definition);
}

public static final class Sticky extends StickyLoadBalancerDefinition implements HasExpression {
@JsonIgnore
@Override
public void setCorrelationExpression(Expression expression) {
super.setCorrelationExpression(expression);
}

@Override
public void setExpression(ExpressionDefinition expressionDefinition) {
super.setCorrelationExpression(expressionDefinition);
}

@Override
public ExpressionDefinition getExpression() {
final ExpressionSubElementDefinition expression = super.getCorrelationExpression();

return expression != null
? super.getCorrelationExpression().getExpressionType()
: null;
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import groovy.util.logging.Slf4j
import org.apache.camel.CamelContext
import org.apache.camel.component.mock.MockEndpoint
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.loader.yaml.parser.ProcessorStepParser
import org.apache.camel.k.loader.yaml.parser.StepParser
import org.apache.camel.model.ProcessorDefinition
import spock.lang.Specification

import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -99,4 +101,8 @@ class TestSupport extends Specification {

return closure.delegate
}

static <U extends ProcessorStepParser> ProcessorDefinition<?> toProcessor(Class<U> type, String content) {
return type.getConstructor().newInstance().toProcessor(stepContext(content))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.k.loader.yaml.parser

import org.apache.camel.k.loader.yaml.TestSupport
import org.apache.camel.model.CircuitBreakerDefinition
import org.apache.camel.model.ToDefinition

class CircuitBreakerTest extends TestSupport {
def "definition"() {
given:
def stepContext = stepContext('''
configuration-ref: "my-config"
resilience4j-configuration:
failure-rate-threshold: "10"
hystrix-configuration:
group-key: "my-group"
on-fallback:
fallback-via-network: "true"
steps:
- log:
message: "test"
''')
when:
def processor = new CircuitBreakerStepParser().toProcessor(stepContext)
then:
with(processor, CircuitBreakerDefinition) {
configurationRef == 'my-config'

resilience4jConfiguration != null
resilience4jConfiguration.failureRateThreshold == '10'

hystrixConfiguration != null
hystrixConfiguration.groupKey == 'my-group'

onFallback != null
onFallback.fallbackViaNetwork == "true"
}
}

def "definition with alias"() {
given:
def stepContext = stepContext('''
on-fallback:
via-network: "true"
''')
when:
def processor = new CircuitBreakerStepParser().toProcessor(stepContext)
then:
with(processor, CircuitBreakerDefinition) {
onFallback != null
onFallback.fallbackViaNetwork == "true"
}
}

def "definition with steps"() {
given:
def stepContext = stepContext('''
steps:
- to: "log:cb"
on-fallback:
steps:
- to: "log:fb"
''')
when:
def processor = new CircuitBreakerStepParser().toProcessor(stepContext)
then:
with(processor, CircuitBreakerDefinition) {
with (outputs[0], ToDefinition) {
endpointUri == "log:cb"
}
with (onFallback.outputs[0], ToDefinition) {
endpointUri == "log:fb"
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/
package org.apache.camel.k.loader.yaml.parser

import org.apache.camel.k.loader.yaml.TestSupport
import org.apache.camel.model.ClaimCheckDefinition

class ClaimCheckTest extends org.apache.camel.k.loader.yaml.TestSupport {
class ClaimCheckTest extends TestSupport {
def "definition"() {
given:
def stepContext = stepContext('''
Expand Down
Loading