forked from aparo/opensearch-learning-to-rank
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented circuit breaker (#71)
Signed-off-by: Johannes Peter <[email protected]>
- Loading branch information
1 parent
b84bc5e
commit 45e743f
Showing
12 changed files
with
487 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.opensearch.ltr.breaker; | ||
|
||
public enum BreakerName { | ||
|
||
MEM("memory"), | ||
CPU("cpu"); | ||
|
||
private String name; | ||
|
||
BreakerName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/opensearch/ltr/breaker/CircuitBreaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.opensearch.ltr.breaker; | ||
|
||
/** | ||
* An interface for circuit breaker. | ||
* | ||
* We use circuit breaker to protect a certain system resource like memory, cpu etc. | ||
*/ | ||
public interface CircuitBreaker { | ||
|
||
boolean isOpen(); | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/opensearch/ltr/breaker/LTRCircuitBreakerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.opensearch.ltr.breaker; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.monitor.jvm.JvmService; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* Class {@code LTRCircuitBreakerService} provide storing, retrieving circuit breakers functions. | ||
* | ||
* This service registers internal system breakers and provide API for users to register their own breakers. | ||
*/ | ||
public class LTRCircuitBreakerService { | ||
|
||
private final ConcurrentMap<String, CircuitBreaker> breakers = new ConcurrentHashMap<>(); | ||
private final JvmService jvmService; | ||
|
||
private static final Logger logger = LogManager.getLogger(LTRCircuitBreakerService.class); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param jvmService jvm info | ||
*/ | ||
public LTRCircuitBreakerService(JvmService jvmService) { | ||
this.jvmService = jvmService; | ||
} | ||
|
||
public void registerBreaker(String name, CircuitBreaker breaker) { | ||
breakers.putIfAbsent(name, breaker); | ||
} | ||
|
||
public void unregisterBreaker(String name) { | ||
if (name == null) { | ||
return; | ||
} | ||
|
||
breakers.remove(name); | ||
} | ||
|
||
public void clearBreakers() { | ||
breakers.clear(); | ||
} | ||
|
||
public CircuitBreaker getBreaker(String name) { | ||
return breakers.get(name); | ||
} | ||
|
||
/** | ||
* Initialize circuit breaker service. | ||
* | ||
* Register memory breaker by default. | ||
* | ||
* @return LTRCircuitBreakerService | ||
*/ | ||
public LTRCircuitBreakerService init() { | ||
// Register memory circuit breaker | ||
registerBreaker(BreakerName.MEM.getName(), new MemoryCircuitBreaker(this.jvmService)); | ||
logger.info("Registered memory breaker."); | ||
|
||
return this; | ||
} | ||
|
||
public Boolean isOpen() { | ||
for (CircuitBreaker breaker : breakers.values()) { | ||
if (breaker.isOpen()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/opensearch/ltr/breaker/MemoryCircuitBreaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.opensearch.ltr.breaker; | ||
|
||
import org.opensearch.monitor.jvm.JvmService; | ||
|
||
/** | ||
* A circuit breaker for memory usage. | ||
*/ | ||
public class MemoryCircuitBreaker extends ThresholdCircuitBreaker<Short> { | ||
|
||
private static final short defaultThreshold = 85; | ||
private final JvmService jvmService; | ||
|
||
public MemoryCircuitBreaker(JvmService jvmService) { | ||
super(defaultThreshold); | ||
this.jvmService = jvmService; | ||
} | ||
|
||
public MemoryCircuitBreaker(short threshold, JvmService jvmService) { | ||
super(threshold); | ||
this.jvmService = jvmService; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return jvmService.stats().getMem().getHeapUsedPercent() > this.getThreshold(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/opensearch/ltr/breaker/ThresholdCircuitBreaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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.opensearch.ltr.breaker; | ||
|
||
/** | ||
* An abstract class for all breakers with threshold. | ||
* @param <T> data type of threshold | ||
*/ | ||
public abstract class ThresholdCircuitBreaker<T> implements CircuitBreaker { | ||
|
||
private T threshold; | ||
|
||
public ThresholdCircuitBreaker(T threshold) { | ||
this.threshold = threshold; | ||
} | ||
|
||
public T getThreshold() { | ||
return threshold; | ||
} | ||
|
||
@Override | ||
public abstract boolean isOpen(); | ||
} |
Oops, something went wrong.