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

Add support for registering custom circuit breaker #8795

Merged
merged 3 commits into from Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,7 +38,7 @@ public class ChildMemoryCircuitBreaker implements CircuitBreaker {
private final AtomicLong trippedCount;
private final ESLogger logger;
private final HierarchyCircuitBreakerService parent;
private final Name name;
private final String name;

/**
* Create a circuit breaker that will break if the number of estimated
Expand All @@ -49,7 +49,7 @@ public class ChildMemoryCircuitBreaker implements CircuitBreaker {
* @param name the name of the breaker
*/
public ChildMemoryCircuitBreaker(BreakerSettings settings, ESLogger logger,
HierarchyCircuitBreakerService parent, Name name) {
HierarchyCircuitBreakerService parent, String name) {
this(settings, null, logger, parent, name);
}

Expand All @@ -64,7 +64,7 @@ public ChildMemoryCircuitBreaker(BreakerSettings settings, ESLogger logger,
* @param oldBreaker the previous circuit breaker to inherit the used value from (starting offset)
*/
public ChildMemoryCircuitBreaker(BreakerSettings settings, ChildMemoryCircuitBreaker oldBreaker,
ESLogger logger, HierarchyCircuitBreakerService parent, Name name) {
ESLogger logger, HierarchyCircuitBreakerService parent, String name) {
this.name = name;
this.settings = settings;
this.memoryBytesLimit = settings.getLimit();
Expand Down Expand Up @@ -141,7 +141,7 @@ memoryBytesLimit, new ByteSizeValue(memoryBytesLimit),
newUsedWithOverhead, new ByteSizeValue(newUsedWithOverhead));
}
if (memoryBytesLimit > 0 && newUsedWithOverhead > memoryBytesLimit) {
logger.warn("[{}] New used memory {} [{}] from field [{}] would be larger than configured breaker: {} [{}], breaking",
logger.warn("[{}] New used memory {} [{}] for data of [{}] would be larger than configured breaker: {} [{}], breaking",
this.name,
newUsedWithOverhead, new ByteSizeValue(newUsedWithOverhead), label,
memoryBytesLimit, new ByteSizeValue(memoryBytesLimit));
Expand Down Expand Up @@ -220,7 +220,7 @@ public long getTrippedCount() {
/**
* @return the name of the breaker
*/
public Name getName() {
public String getName() {
return this.name;
}
}
44 changes: 4 additions & 40 deletions src/main/java/org/elasticsearch/common/breaker/CircuitBreaker.java
Expand Up @@ -20,10 +20,7 @@
package org.elasticsearch.common.breaker;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.util.Locale;

/**
Expand All @@ -32,42 +29,9 @@
*/
public interface CircuitBreaker {

/**
* Enum used for specifying different types of circuit breakers
*/
public static enum Name {
PARENT(0),
FIELDDATA(1),
REQUEST(2);

private int ordinal;

Name(int ordinal) {
this.ordinal = ordinal;
}

public int getSerializableValue() {
return this.ordinal;
}

public static Name readFrom(StreamInput in) throws IOException {
int value = in.readVInt();
switch (value) {
case 0:
return Name.PARENT;
case 1:
return Name.FIELDDATA;
case 2:
return Name.REQUEST;
default:
throw new ElasticsearchIllegalArgumentException("No CircuitBreaker with ordinal: " + value);
}
}

public static void writeTo(Name name, StreamOutput out) throws IOException {
out.writeVInt(name.getSerializableValue());
}
}
public static final String PARENT = "PARENT";
public static final String FIELDDATA = "FIELDDATA";
public static final String REQUEST = "REQUEST";
Copy link
Member

Choose a reason for hiding this comment

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

I think it's okay to make these lowercase now that they aren't enums


public static enum Type {
// A regular or child MemoryCircuitBreaker
Expand Down Expand Up @@ -135,5 +99,5 @@ public static Type parseValue(String value) {
/**
* @return the name of the breaker
*/
public Name getName();
public String getName();
}
Expand Up @@ -186,7 +186,7 @@ public long getTrippedCount() {
/**
* @return the name of the breaker
*/
public Name getName() {
return Name.FIELDDATA;
public String getName() {
return FIELDDATA;
}
}
Expand Up @@ -25,9 +25,9 @@
*/
public class NoopCircuitBreaker implements CircuitBreaker {

private final Name name;
private final String name;

public NoopCircuitBreaker(Name name) {
public NoopCircuitBreaker(String name) {
this.name = name;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ public long getTrippedCount() {
}

@Override
public Name getName() {
public String getName() {
return this.name;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/elasticsearch/common/util/BigArrays.java
Expand Up @@ -387,7 +387,7 @@ public BigArrays(Settings settings, PageCacheRecycler recycler, @Nullable final
*/
void adjustBreaker(long delta) {
if (this.breakerService != null) {
CircuitBreaker breaker = this.breakerService.getBreaker(CircuitBreaker.Name.REQUEST);
CircuitBreaker breaker = this.breakerService.getBreaker(CircuitBreaker.REQUEST);
if (this.checkBreaker == true) {
// checking breaker means potentially tripping, but it doesn't
// have to if the delta is negative
Expand Down
Expand Up @@ -53,7 +53,7 @@ public static IndexOrdinalsFieldData build(final IndexReader indexReader, IndexO
}
final OrdinalMap ordinalMap = OrdinalMap.build(null, subs, PackedInts.DEFAULT);
final long memorySizeInBytes = ordinalMap.ramBytesUsed();
breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA).addWithoutBreaking(memorySizeInBytes);
breakerService.getBreaker(CircuitBreaker.FIELDDATA).addWithoutBreaking(memorySizeInBytes);

if (logger.isDebugEnabled()) {
logger.debug(
Expand Down
Expand Up @@ -75,7 +75,7 @@ public AtomicNumericFieldData loadDirect(LeafReaderContext context) throws Excep
Terms terms = reader.terms(getFieldNames().indexName());
AtomicNumericFieldData data = null;
// TODO: Use an actual estimator to estimate before loading.
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA));
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA));
if (terms == null) {
data = AtomicDoubleFieldData.empty(reader.maxDoc());
estimator.afterLoad(null, data.ramBytesUsed());
Expand Down
Expand Up @@ -64,7 +64,7 @@ public AtomicOrdinalsFieldData loadDirect(LeafReaderContext context) throws Exce
Terms terms = reader.terms(getFieldNames().indexName());
AtomicOrdinalsFieldData data = null;
// TODO: Use an actual estimator to estimate before loading.
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA));
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA));
if (terms == null) {
data = AbstractAtomicOrdinalsFieldData.empty();
estimator.afterLoad(null, data.ramBytesUsed());
Expand Down
Expand Up @@ -73,7 +73,7 @@ public AtomicNumericFieldData loadDirect(LeafReaderContext context) throws Excep
Terms terms = reader.terms(getFieldNames().indexName());
AtomicNumericFieldData data = null;
// TODO: Use an actual estimator to estimate before loading.
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA));
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA));
if (terms == null) {
data = AtomicDoubleFieldData.empty(reader.maxDoc());
estimator.afterLoad(null, data.ramBytesUsed());
Expand Down
Expand Up @@ -83,7 +83,7 @@ public AtomicGeoPointFieldData loadDirect(LeafReaderContext context) throws Exce
Terms terms = reader.terms(getFieldNames().indexName());
AtomicGeoPointFieldData data = null;
// TODO: Use an actual estimator to estimate before loading.
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA));
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA));
if (terms == null) {
data = AbstractAtomicGeoPointFieldData.empty(reader.maxDoc());
estimator.afterLoad(null, data.ramBytesUsed());
Expand Down
Expand Up @@ -65,7 +65,7 @@ public AtomicGeoPointFieldData loadDirect(LeafReaderContext context) throws Exce
Terms terms = reader.terms(getFieldNames().indexName());
AtomicGeoPointFieldData data = null;
// TODO: Use an actual estimator to estimate before loading.
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA));
NonEstimatingEstimator estimator = new NonEstimatingEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA));
if (terms == null) {
data = AbstractAtomicGeoPointFieldData.empty(reader.maxDoc());
estimator.afterLoad(null, data.ramBytesUsed());
Expand Down
Expand Up @@ -90,7 +90,7 @@ public AtomicNumericFieldData loadDirect(LeafReaderContext context) throws Excep
final LeafReader reader = context.reader();
Terms terms = reader.terms(getFieldNames().indexName());
AtomicNumericFieldData data = null;
PackedArrayEstimator estimator = new PackedArrayEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA), getNumericType(), getFieldNames().fullName());
PackedArrayEstimator estimator = new PackedArrayEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA), getNumericType(), getFieldNames().fullName());
if (terms == null) {
data = AtomicLongFieldData.empty(reader.maxDoc());
estimator.adjustForNoTerms(data.ramBytesUsed());
Expand Down
Expand Up @@ -62,7 +62,7 @@ public AtomicOrdinalsFieldData loadDirect(LeafReaderContext context) throws Exce
LeafReader reader = context.reader();
AtomicOrdinalsFieldData data = null;

PagedBytesEstimator estimator = new PagedBytesEstimator(context, breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA), getFieldNames().fullName());
PagedBytesEstimator estimator = new PagedBytesEstimator(context, breakerService.getBreaker(CircuitBreaker.FIELDDATA), getFieldNames().fullName());
Terms terms = reader.terms(getFieldNames().indexName());
if (terms == null) {
data = AbstractAtomicOrdinalsFieldData.empty();
Expand Down
Expand Up @@ -102,7 +102,7 @@ public ParentChildAtomicFieldData loadDirect(LeafReaderContext context) throws E
new ParentChildIntersectTermsEnum(reader, UidFieldMapper.NAME, ParentFieldMapper.NAME),
parentTypes
);
ParentChildEstimator estimator = new ParentChildEstimator(breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA), termsEnum);
ParentChildEstimator estimator = new ParentChildEstimator(breakerService.getBreaker(CircuitBreaker.FIELDDATA), termsEnum);
TermsEnum estimatedTermsEnum = estimator.beforeLoad(null);
ObjectObjectOpenHashMap<String, TypeBuilder> typeBuilders = ObjectObjectOpenHashMap.newInstance();
try {
Expand Down Expand Up @@ -338,7 +338,7 @@ public int getOrd(int docID) {
}
}

breakerService.getBreaker(CircuitBreaker.Name.FIELDDATA).addWithoutBreaking(ramBytesUsed);
breakerService.getBreaker(CircuitBreaker.FIELDDATA).addWithoutBreaking(ramBytesUsed);
if (logger.isDebugEnabled()) {
logger.debug(
"Global-ordinals[_parent] took {}",
Expand Down
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.indices.breaker;

import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand Down Expand Up @@ -48,9 +47,9 @@ public CircuitBreakerStats[] getAllStats() {
return this.allStats;
}

public CircuitBreakerStats getStats(CircuitBreaker.Name name) {
public CircuitBreakerStats getStats(String name) {
for (CircuitBreakerStats stats : allStats) {
if (stats.getName() == name) {
if (stats.getName().equals(name)) {
return stats;
}
}
Expand Down
Expand Up @@ -27,23 +27,23 @@
*/
public class BreakerSettings {

private final CircuitBreaker.Name name;
private final String name;
private final long limitBytes;
private final double overhead;
private final CircuitBreaker.Type type;

public BreakerSettings(CircuitBreaker.Name name, long limitBytes, double overhead) {
public BreakerSettings(String name, long limitBytes, double overhead) {
this(name, limitBytes, overhead, CircuitBreaker.Type.MEMORY);
}

public BreakerSettings(CircuitBreaker.Name name, long limitBytes, double overhead, CircuitBreaker.Type type) {
public BreakerSettings(String name, long limitBytes, double overhead, CircuitBreaker.Type type) {
this.name = name;
this.limitBytes = limitBytes;
this.overhead = overhead;
this.type = type;
}

public CircuitBreaker.Name getName() {
public String getName() {
return this.name;
}

Expand All @@ -61,7 +61,7 @@ public CircuitBreaker.Type getType() {

@Override
public String toString() {
return "[" + this.name.toString() +
return "[" + this.name +
",type=" + this.type.toString() +
",limit=" + this.limitBytes + "/" + new ByteSizeValue(this.limitBytes) +
",overhead=" + this.overhead + "]";
Expand Down
Expand Up @@ -34,10 +34,17 @@ protected CircuitBreakerService(Settings settings) {
super(settings);
}

/**
* Allows to register of a custom circuit breaker.
*
* @param breakerSettings
*/
public abstract void registerBreaker(BreakerSettings breakerSettings);

/**
* @return the breaker that can be used to register estimates against
*/
public abstract CircuitBreaker getBreaker(CircuitBreaker.Name type);
public abstract CircuitBreaker getBreaker(String name);

/**
* @return stats about all breakers
Expand All @@ -47,7 +54,7 @@ protected CircuitBreakerService(Settings settings) {
/**
* @return stats about a specific breaker
*/
public abstract CircuitBreakerStats stats(CircuitBreaker.Name name);
public abstract CircuitBreakerStats stats(String name);

protected void doStart() throws ElasticsearchException {
}
Expand Down
Expand Up @@ -19,8 +19,6 @@

package org.elasticsearch.indices.breaker;

import org.elasticsearch.Version;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand All @@ -37,7 +35,7 @@
*/
public class CircuitBreakerStats implements Streamable, ToXContent {

private CircuitBreaker.Name name;
private String name;
private long limit;
private long estimated;
private long trippedCount;
Expand All @@ -47,15 +45,15 @@ public class CircuitBreakerStats implements Streamable, ToXContent {

}

public CircuitBreakerStats(CircuitBreaker.Name name, long limit, long estimated, double overhead, long trippedCount) {
public CircuitBreakerStats(String name, long limit, long estimated, double overhead, long trippedCount) {
this.name = name;
this.limit = limit;
this.estimated = estimated;
this.trippedCount = trippedCount;
this.overhead = overhead;
}

public CircuitBreaker.Name getName() {
public String getName() {
return this.name;
}

Expand Down Expand Up @@ -87,7 +85,7 @@ public void readFrom(StreamInput in) throws IOException {
estimated = in.readLong();
overhead = in.readDouble();
this.trippedCount = in.readLong();
this.name = CircuitBreaker.Name.readFrom(in);
this.name = in.readString();
}

@Override
Expand All @@ -96,12 +94,12 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeLong(estimated);
out.writeDouble(overhead);
out.writeLong(trippedCount);
CircuitBreaker.Name.writeTo(name, out);
out.writeString(name);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name.toString().toLowerCase(Locale.ROOT));
builder.startObject(name.toLowerCase(Locale.ROOT));
builder.field(Fields.LIMIT, limit);
builder.field(Fields.LIMIT_HUMAN, new ByteSizeValue(limit));
builder.field(Fields.ESTIMATED, estimated);
Expand All @@ -114,7 +112,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public String toString() {
return "[" + this.name.toString() +
return "[" + this.name +
",limit=" + this.limit + "/" + new ByteSizeValue(this.limit) +
",estimated=" + this.estimated + "/" + new ByteSizeValue(this.estimated) +
",overhead=" + this.overhead + ",tripped=" + this.trippedCount + "]";
Expand Down