Skip to content

Commit

Permalink
Issue resilience4j#201 bulkhead events corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
hexmind authored and Tomasz Skowroński committed Jan 5, 2024
1 parent 976f478 commit a4d0c7e
Show file tree
Hide file tree
Showing 29 changed files with 404 additions and 424 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ charset = utf-8
indent_style = space
indent_size = 4
continuation_indent_size = 4
ij_continuation_indent_size = 4 # continuation_indent_size is not supported by IntelliJ
end_of_line = lf
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadFullException;
import io.github.resilience4j.bulkhead.adaptive.event.*;
import io.github.resilience4j.bulkhead.adaptive.internal.AdaptiveBulkheadStateMachine;
import io.github.resilience4j.bulkhead.event.*;
import io.github.resilience4j.core.EventConsumer;
import io.github.resilience4j.core.EventPublisher;
import io.github.resilience4j.core.functions.CheckedConsumer;
Expand All @@ -30,7 +30,6 @@
import io.github.resilience4j.core.functions.CheckedSupplier;
import io.github.resilience4j.core.functions.OnceConsumer;

import java.time.Instant;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import io.github.resilience4j.core.predicate.PredicateCreator;

/**
* A {@link AdaptiveBulkheadConfig} configures a adaptation capabilities of {@link AdaptiveBulkheadStateMachine}
* A {@link AdaptiveBulkheadConfig} configures an adaptation capabilities of {@link AdaptiveBulkheadStateMachine}
*/
public class AdaptiveBulkheadConfig {

private static final int DEFAULT_MAX_CONCURRENT_CALLS = 25;
private static final int DEFAULT_MIN_CONCURRENT_CALLS = 2;
private static final int DEFAULT_INITIAL_CONCURRENT_CALLS = DEFAULT_MIN_CONCURRENT_CALLS;
private static final Duration DEFAULT_MAX_WAIT_DURATION = Duration.ofSeconds(0);

private static final float DEFAULT_FAILURE_RATE_THRESHOLD_PERCENTAGE = 50.0f;
private static final float DEFAULT_SLOW_CALL_RATE_THRESHOLD_PERCENTAGE = 50.0f;
private static final int DEFAULT_SLIDING_WINDOW_SIZE = 100;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright 2019 Mahmoud Romeh
*
* Licensed 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 speci`fic language governing permissions and
* limitations under the License.
*
*
*/
package io.github.resilience4j.bulkhead.adaptive.event;

import io.github.resilience4j.core.lang.NonNull;

import java.time.ZonedDateTime;

public abstract class AbstractAdaptiveBulkheadEvent implements AdaptiveBulkheadEvent {

private final String bulkheadName;
private final ZonedDateTime creationTime;

AbstractAdaptiveBulkheadEvent(String bulkheadName) {
this.bulkheadName = bulkheadName;
this.creationTime = ZonedDateTime.now();
}

@NonNull
@Override
public String getBulkheadName() {
return bulkheadName;
}

@Override
public ZonedDateTime getCreationTime() {
return creationTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.github.resilience4j.bulkhead.adaptive.event;

import java.time.ZonedDateTime;

/**
* adaptive bulkhead event
*/
public interface AdaptiveBulkheadEvent {
/**
* Returns the name of the bulkhead which has created the event.
*
* @return the name of the bulkhead which has created the event
*/
String getBulkheadName();

/**
* Returns the type of the bulkhead event.
*
* @return the type of the bulkhead event
*/
Type getEventType();


/**
* Returns the creation time of adaptive bulkhead event.
*
* @return the creation time of adaptive bulkhead event
*/
ZonedDateTime getCreationTime();


/**
* Event types which are created by a bulkhead.
*/
enum Type {
/**
* A AdaptiveBulkheadEvent which informs that a limit has been increased
*/
LIMIT_INCREASED(false),
/**
* A AdaptiveBulkheadEvent which informs that a limit has been decreased
*/
LIMIT_DECREASED(false),
/**
* An adaptive bulkhead event which informs that an error has been recorded
*/
ERROR(false),
/**
* An adaptive bulkhead which informs that an error has been ignored
*/
IGNORED_ERROR(false),
/**
* An adaptive bulkhead which informs that a success has been recorded
*/
SUCCESS(false),

/**
* A AdaptiveBulkheadEvent which informs the state of the AdaptiveBulkhead has been changed
*/
STATE_TRANSITION(true),
/**
* A AdaptiveBulkheadEvent which informs the AdaptiveBulkhead has been reset
*/
RESET(true),
/**
* A AdaptiveBulkheadEvent which informs the AdaptiveBulkhead has been disabled
*/
DISABLED(false);

public final boolean forcePublish;

Type(boolean forcePublish) {
this.forcePublish = forcePublish;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright 2019 Mahmoud Romeh
*
* Licensed 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 io.github.resilience4j.bulkhead.adaptive.event;

import io.github.resilience4j.core.lang.NonNull;

/**
* A BulkheadEvent which informs that a call has been failed
*/
public class BulkheadOnErrorEvent extends AbstractAdaptiveBulkheadEvent {

private final String exceptionMsg;

public BulkheadOnErrorEvent(String bulkheadName, Throwable throwable) {
super(bulkheadName);
this.exceptionMsg = throwable.getMessage();
}

@NonNull
@Override
public Type getEventType() {
return Type.ERROR;
}

@Override
public String toString() {
return String.format("%s: Bulkhead '%s' recorded an error: '%s'",
getCreationTime(),
getBulkheadName(),
exceptionMsg
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright 2019 Mahmoud Romeh
*
* Licensed 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 io.github.resilience4j.bulkhead.adaptive.event;

import io.github.resilience4j.core.lang.NonNull;

/**
* A BulkheadEvent which informs that an error has been ignored
*/
public class BulkheadOnIgnoreEvent extends AbstractAdaptiveBulkheadEvent {

private final String exceptionMsg;

public BulkheadOnIgnoreEvent(String bulkheadName, Throwable throwable) {
super(bulkheadName);
this.exceptionMsg = throwable.getMessage();
}

@NonNull
@Override
public Type getEventType() {
return Type.IGNORED_ERROR;
}

@Override
public String toString() {
return String.format("%s: Bulkhead '%s' recorded an error which has been ignored: '%s'.",
getCreationTime(),
getBulkheadName(),
exceptionMsg
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright 2019 Mahmoud Romeh
*
* Licensed 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 io.github.resilience4j.bulkhead.adaptive.event;

import io.github.resilience4j.core.lang.NonNull;

/**
* A BulkheadEvent which informs that a limit has been decreased
*/
public class BulkheadOnLimitDecreasedEvent extends AbstractAdaptiveBulkheadEvent {

private final int newMaxConcurrentCalls;

public BulkheadOnLimitDecreasedEvent(String bulkheadName, int newMaxConcurrentCalls) {
super(bulkheadName);
this.newMaxConcurrentCalls = newMaxConcurrentCalls;
}

@NonNull
@Override
public Type getEventType() {
return Type.LIMIT_DECREASED;
}

public int getNewMaxConcurrentCalls() {
return newMaxConcurrentCalls;
}

@Override
public String toString() {
return String.format("%s: Bulkhead '%s' recorded a limit decrease: %s",
getCreationTime(),
getBulkheadName(),
newMaxConcurrentCalls
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Copyright 2019 Mahmoud Romeh
*
* Licensed 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 io.github.resilience4j.bulkhead.adaptive.event;

import io.github.resilience4j.core.lang.NonNull;

/**
* A BulkheadEvent which informs that a limit has been increased
*/
public class BulkheadOnLimitIncreasedEvent extends AbstractAdaptiveBulkheadEvent {

private final int newMaxConcurrentCalls;

public BulkheadOnLimitIncreasedEvent(String bulkheadName, int newMaxConcurrentCalls) {
super(bulkheadName);
this.newMaxConcurrentCalls = newMaxConcurrentCalls;
}

@NonNull
@Override
public Type getEventType() {
return Type.LIMIT_INCREASED;
}

public int getNewMaxConcurrentCalls() {
return newMaxConcurrentCalls;
}

@Override
public String toString() {
return String.format("%s: Bulkhead '%s' recorded a limit increase: %s",
getCreationTime(),
getBulkheadName(),
newMaxConcurrentCalls
);
}

}
Loading

0 comments on commit a4d0c7e

Please sign in to comment.