Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Raft implementation as a standalone interface dependent only…
… on I/O and Cluster.
- Loading branch information
Showing
91 changed files
with
1,465 additions
and
1,016 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* 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 net.kuujo.copycat.raft; | |||
|
|||
import java.lang.annotation.ElementType; | |||
import java.lang.annotation.Retention; | |||
import java.lang.annotation.RetentionPolicy; | |||
import java.lang.annotation.Target; | |||
|
|||
/** | |||
* Annotation for applying commands to the state machine. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
@Target(ElementType.METHOD) | |||
@Retention(RetentionPolicy.RUNTIME) | |||
public @interface Apply { | |||
|
|||
/** | |||
* The command types to apply. | |||
*/ | |||
Class<? extends Command>[] value(); | |||
|
|||
/** | |||
* Indicates that all commands should be applied. | |||
*/ | |||
static class All implements Command { | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,71 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* 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 net.kuujo.copycat.raft; | |||
|
|||
/** | |||
* Protocol commit. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public class Commit<T extends Operation> { | |||
private final long index; | |||
private final long timestamp; | |||
private final T operation; | |||
|
|||
public Commit(long index, long timestamp, T operation) { | |||
this.index = index; | |||
this.timestamp = timestamp; | |||
this.operation = operation; | |||
} | |||
|
|||
/** | |||
* Returns the commit index. | |||
* | |||
* @return The commit index. | |||
*/ | |||
public long index() { | |||
return index; | |||
} | |||
|
|||
/** | |||
* Returns the commit timestamp. | |||
* | |||
* @return the commit timestamp. | |||
*/ | |||
public long timestamp() { | |||
return timestamp; | |||
} | |||
|
|||
/** | |||
* Returns the commit type. | |||
* | |||
* @return The commit type. | |||
*/ | |||
@SuppressWarnings("unchecked") | |||
public Class<T> type() { | |||
return (Class<T>) operation.getClass(); | |||
} | |||
|
|||
/** | |||
* Returns the operation. | |||
* | |||
* @return The operation. | |||
*/ | |||
public T operation() { | |||
return operation; | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* 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 net.kuujo.copycat.raft; | |||
|
|||
import java.io.Serializable; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
/** | |||
* State machine operation. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public interface Operation<T> extends Serializable { | |||
|
|||
/** | |||
* Returns a cached instance of the given builder. | |||
* | |||
* @param type The builder type. | |||
* @return The builder. | |||
*/ | |||
@SuppressWarnings("unchecked") | |||
static <T extends Builder<U>, U extends Operation<?>> T builder(Class<T> type) { | |||
return (T) Builder.BUILDERS.get().computeIfAbsent(type, t -> { | |||
try { | |||
return type.newInstance(); | |||
} catch (InstantiationException | IllegalAccessException e) { | |||
throw new IllegalArgumentException("failed to instantiate builder: " + type, e); | |||
} | |||
}); | |||
} | |||
|
|||
/** | |||
* Operation builder. | |||
*/ | |||
static abstract class Builder<T extends Operation<?>> implements net.kuujo.copycat.Builder<T> { | |||
private static final ThreadLocal<Map<Class<? extends Builder>, Builder>> BUILDERS = new ThreadLocal<Map<Class<? extends Builder>, Builder>>() { | |||
@Override | |||
protected Map<Class<? extends Builder>, Builder> initialValue() { | |||
return new HashMap<>(); | |||
} | |||
}; | |||
|
|||
private final T operation; | |||
|
|||
protected Builder(T operation) { | |||
this.operation = operation; | |||
} | |||
|
|||
@Override | |||
public T build() { | |||
return operation; | |||
} | |||
} | |||
|
|||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* 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 net.kuujo.copycat.raft; | |||
|
|||
/** | |||
* Query operation. | |||
* | |||
* @author <a href="http://github.com/kuujo">Jordan Halterman</a> | |||
*/ | |||
public interface Query<T> extends Operation<T> { | |||
|
|||
/** | |||
* Returns the query consistency. | |||
* | |||
* @return The query consistency. | |||
*/ | |||
Consistency consistency(); | |||
|
|||
/** | |||
* Query consistency. | |||
*/ | |||
static enum Consistency { | |||
|
|||
/** | |||
* Provides serializable consistency. | |||
*/ | |||
SERIALIZABLE, | |||
|
|||
/** | |||
* Provides linearizable consistency based on a leader lease. | |||
*/ | |||
LINEARIZABLE_LEASE, | |||
|
|||
/** | |||
* Provides strict linearizable consistency. | |||
*/ | |||
LINEARIZABLE_STRICT | |||
|
|||
} | |||
|
|||
/** | |||
* Query builder. | |||
*/ | |||
static class Builder<T extends Builder<T, U>, U extends Query<?>> extends Operation.Builder<U> { | |||
protected final U query; | |||
|
|||
public Builder(U query) { | |||
super(query); | |||
this.query = query; | |||
} | |||
} | |||
|
|||
} |
Oops, something went wrong.