Skip to content

Commit

Permalink
Refactor builders to use a builder pool instead of ThreadLocal to red…
Browse files Browse the repository at this point in the history
…uce GC pressure.
  • Loading branch information
kuujo committed Jul 22, 2015
1 parent 76bce26 commit f8342ef
Show file tree
Hide file tree
Showing 59 changed files with 777 additions and 436 deletions.
45 changes: 34 additions & 11 deletions atomic/src/main/java/net/kuujo/copycat/atomic/AsyncReference.java
Expand Up @@ -20,8 +20,9 @@
import net.kuujo.alleycat.SerializeWith;
import net.kuujo.alleycat.io.BufferInput;
import net.kuujo.alleycat.io.BufferOutput;
import net.kuujo.copycat.Resource;
import net.kuujo.copycat.BuilderPool;
import net.kuujo.copycat.Mode;
import net.kuujo.copycat.Resource;
import net.kuujo.copycat.Stateful;
import net.kuujo.copycat.log.Compaction;
import net.kuujo.copycat.raft.*;
Expand Down Expand Up @@ -417,7 +418,10 @@ public void readObject(BufferInput buffer, Alleycat alleycat) {
/**
* Base reference command builder.
*/
public static abstract class Builder<T extends Builder<T, U>, U extends ReferenceCommand<?>> extends Command.Builder<T, U> {
public static abstract class Builder<T extends Builder<T, U, V>, U extends ReferenceCommand<V>, V> extends Command.Builder<T, U, V> {
protected Builder(BuilderPool<T, U> pool) {
super(pool);
}

/**
* Sets the persistence mode.
Expand Down Expand Up @@ -484,7 +488,10 @@ public void readObject(BufferInput buffer, Alleycat alleycat) {
/**
* Base reference query builder.
*/
public static abstract class Builder<T extends Builder<T, U>, U extends ReferenceQuery<?>> extends Query.Builder<T, U> {
public static abstract class Builder<T extends Builder<T, U, V>, U extends ReferenceQuery<V>, V> extends Query.Builder<T, U, V> {
protected Builder(BuilderPool<T, U> pool) {
super(pool);
}

/**
* Sets the query consistency level.
Expand Down Expand Up @@ -515,13 +522,17 @@ public static class Get<T> extends ReferenceQuery<T> {
*/
@SuppressWarnings("unchecked")
public static <T> Builder<T> builder() {
return Operation.builder(Builder.class);
return Operation.builder(Builder.class, Builder::new);
}

/**
* Get query builder.
*/
public static class Builder<T> extends ReferenceQuery.Builder<Builder<T>, Get<T>> {
public static class Builder<T> extends ReferenceQuery.Builder<Builder<T>, Get<T>, T> {
public Builder(BuilderPool<Builder<T>, Get<T>> pool) {
super(pool);
}

@Override
protected Get<T> create() {
return new Get<>();
Expand All @@ -541,7 +552,7 @@ public static class Set extends ReferenceCommand<Void> {
* @return A new set command builder.
*/
public static Builder builder() {
return Operation.builder(Builder.class);
return Operation.builder(Builder.class, Builder::new);
}

private Object value;
Expand Down Expand Up @@ -573,7 +584,11 @@ public String toString() {
/**
* Put command builder.
*/
public static class Builder extends ReferenceCommand.Builder<Builder, Set> {
public static class Builder extends ReferenceCommand.Builder<Builder, Set, Void> {
public Builder(BuilderPool<Builder, Set> pool) {
super(pool);
}

@Override
protected Set create() {
return new Set();
Expand Down Expand Up @@ -604,7 +619,7 @@ public static class CompareAndSet extends ReferenceCommand<Boolean> {
* @return A new compare and set command builder.
*/
public static Builder builder() {
return Operation.builder(Builder.class);
return Operation.builder(Builder.class, Builder::new);
}

private Object expect;
Expand Down Expand Up @@ -648,7 +663,11 @@ public String toString() {
/**
* Compare and set command builder.
*/
public static class Builder extends ReferenceCommand.Builder<Builder, CompareAndSet> {
public static class Builder extends ReferenceCommand.Builder<Builder, CompareAndSet, Boolean> {
public Builder(BuilderPool<Builder, CompareAndSet> pool) {
super(pool);
}

@Override
protected CompareAndSet create() {
return new CompareAndSet();
Expand Down Expand Up @@ -691,7 +710,7 @@ public static class GetAndSet<T> extends ReferenceCommand<T> {
*/
@SuppressWarnings("unchecked")
public static <T> Builder<T> builder() {
return Operation.builder(Builder.class);
return Operation.builder(Builder.class, Builder::new);
}

private Object value;
Expand Down Expand Up @@ -723,7 +742,11 @@ public String toString() {
/**
* Put command builder.
*/
public static class Builder<T> extends ReferenceCommand.Builder<Builder<T>, GetAndSet<T>> {
public static class Builder<T> extends ReferenceCommand.Builder<Builder<T>, GetAndSet<T>, T> {
public Builder(BuilderPool<Builder<T>, GetAndSet<T>> pool) {
super(pool);
}

@Override
protected GetAndSet<T> create() {
return new GetAndSet<>();
Expand Down
Expand Up @@ -137,7 +137,7 @@ public CompletableFuture<Void> delete() {
/**
* Raft client builder.
*/
public static class Builder implements Raft.Builder<Builder, RaftClient> {
public static class Builder extends Raft.Builder<Builder, RaftClient> {
private Transport transport;
private Alleycat serializer;
private long keepAliveInterval = 1000;
Expand All @@ -146,6 +146,14 @@ public static class Builder implements Raft.Builder<Builder, RaftClient> {
private Builder() {
}

@Override
protected void reset() {
transport = null;
serializer = null;
keepAliveInterval = 1000;
members = null;
}

@Override
public Builder withTransport(Transport transport) {
this.transport = transport;
Expand Down

0 comments on commit f8342ef

Please sign in to comment.