├── klein-jepsen ├── docker │ ├── secret │ │ └── .gitkeep │ ├── control │ │ ├── .gitignore │ │ ├── bashrc │ │ ├── Dockerfile │ │ └── init.sh │ ├── template │ │ ├── depends.yml │ │ ├── server.properties │ │ ├── db.yml │ │ └── docker-compose.yml │ ├── bin │ │ ├── console │ │ ├── web │ │ ├── readme.md │ │ └── build-docker-compose │ ├── .gitignore │ ├── docker-compose.dev.yml │ ├── node │ │ ├── setup-jepsen.sh │ │ └── Dockerfile │ └── README.md ├── readme.md ├── klein-jepsen-test │ ├── run_test.sh │ ├── .gitignore │ ├── CHANGELOG.md │ └── project.clj ├── klein-jepsen-server │ ├── deploy │ │ ├── start.sh │ │ ├── server.properties │ │ └── stop.sh │ └── src │ │ └── main │ │ ├── resources │ │ └── log4j2.xml │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── jepsen │ │ └── server │ │ └── rpc │ │ ├── GetReq.java │ │ ├── ExistsReq.java │ │ ├── InvalidateReq.java │ │ ├── BaseReq.java │ │ ├── ExistsProcessor.java │ │ ├── PutReq.java │ │ ├── InvalidateProcessor.java │ │ └── Resp.java └── pom.xml ├── klein-core ├── readme.md ├── src │ ├── test │ │ ├── java │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ ├── rpc │ │ │ │ └── facade │ │ │ │ │ └── util │ │ │ │ │ └── RpcUtilTest.java │ │ │ │ ├── KleinPropTest.java │ │ │ │ └── core │ │ │ │ └── cache │ │ │ │ └── CacheSnapTest.java │ │ └── resources │ │ │ └── log4j2.xml │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── core │ │ ├── lock │ │ ├── KleinLock.java │ │ └── LockMessage.java │ │ └── cache │ │ ├── CacheSnap.java │ │ └── MetaData.java └── pom.xml ├── klein-rpc ├── klein-rpc-grpc │ └── src │ │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── services │ │ │ │ ├── com.ofcoder.klein.rpc.facade.RpcClient │ │ │ │ ├── com.ofcoder.klein.rpc.facade.RpcEngine │ │ │ │ └── com.ofcoder.klein.rpc.facade.RpcServer │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── rpc │ │ │ └── grpc │ │ │ └── GrpcConstants.java │ │ └── test │ │ ├── resources │ │ └── log4j2.xml │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── rpc │ │ └── grpc │ │ └── ext │ │ └── HelloProcessor.java ├── klein-rpc-facade │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── rpc │ │ └── facade │ │ ├── RpcContext.java │ │ ├── InvokeCallback.java │ │ ├── exception │ │ ├── ConnectionException.java │ │ ├── InvokeTimeoutException.java │ │ └── RpcException.java │ │ ├── RpcServer.java │ │ └── RpcProcessor.java └── pom.xml ├── klein-example ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── example │ │ │ │ ├── cache │ │ │ │ └── readme.md │ │ │ │ ├── lock │ │ │ │ └── readme.md │ │ │ │ ├── nwr │ │ │ │ ├── readme.md │ │ │ │ ├── Main2.java │ │ │ │ └── Main3.java │ │ │ │ └── changemember │ │ │ │ └── readme.md │ │ └── resources │ │ │ └── log4j2.xml │ └── test │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── example │ │ └── cache │ │ └── Main2Test.java └── pom.xml ├── klein-consensus ├── klein-consensus-paxos │ ├── elect_master.png │ ├── change_member.png │ ├── elect_master_1.png │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── services │ │ │ │ │ └── com.ofcoder.klein.consensus.facade.Consensus │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── consensus │ │ │ │ └── paxos │ │ │ │ ├── rpc │ │ │ │ ├── vo │ │ │ │ │ ├── Sync.java │ │ │ │ │ ├── Pong.java │ │ │ │ │ ├── ChangeMemberRes.java │ │ │ │ │ ├── ConfirmRes.java │ │ │ │ │ ├── ElasticRes.java │ │ │ │ │ └── BaseReq.java │ │ │ │ ├── HeartbeatProcessor.java │ │ │ │ ├── SnapSyncProcessor.java │ │ │ │ └── NewMasterProcessor.java │ │ │ │ ├── core │ │ │ │ ├── sm │ │ │ │ │ └── ElectionOp.java │ │ │ │ ├── PhaseCallback.java │ │ │ │ ├── Proposer.java │ │ │ │ ├── ProposalWithDone.java │ │ │ │ └── Acceptor.java │ │ │ │ ├── ProposeProxy.java │ │ │ │ └── ProposalNoUtil.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── consensus │ │ │ └── paxos │ │ │ ├── core │ │ │ ├── ProposerTest.java │ │ │ ├── sm │ │ │ │ └── PaxosMemberConfigurationTest.java │ │ │ └── HolderTest.java │ │ │ ├── ProposalTest.java │ │ │ ├── ProposalNoUtilTest.java │ │ │ ├── QueueTest.java │ │ │ ├── LatchTest.java │ │ │ ├── PaxosNodeTest.java │ │ │ └── PropertyChangeTest.java │ └── pom.xml ├── klein-consensus-facade │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── services │ │ │ │ │ └── com.ofcoder.klein.consensus.facade.nwr.Nwr │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── consensus │ │ │ │ └── facade │ │ │ │ ├── sm │ │ │ │ ├── SystemOp.java │ │ │ │ ├── AbstractSM.java │ │ │ │ └── SM.java │ │ │ │ ├── Node.java │ │ │ │ ├── exception │ │ │ │ ├── ChangeMemberException.java │ │ │ │ ├── StateMachineException.java │ │ │ │ ├── ConsensusException.java │ │ │ │ └── SnapshotException.java │ │ │ │ ├── Command.java │ │ │ │ ├── nwr │ │ │ │ ├── FastWriteNwr.java │ │ │ │ ├── MajorityNwr.java │ │ │ │ └── Nwr.java │ │ │ │ ├── config │ │ │ │ └── SnapshotStrategy.java │ │ │ │ ├── NoopCommand.java │ │ │ │ ├── quorum │ │ │ │ └── Quorum.java │ │ │ │ ├── AbstractInvokeCallback.java │ │ │ │ ├── Cluster.java │ │ │ │ └── ConsensusEngine.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── consensus │ │ │ └── facade │ │ │ ├── nwr │ │ │ └── NwrTest.java │ │ │ └── quorum │ │ │ └── SingleQuorumTest.java │ └── pom.xml └── pom.xml ├── klein-storage ├── klein-storage-file │ ├── src │ │ ├── main │ │ │ └── resources │ │ │ │ └── META-INF │ │ │ │ └── services │ │ │ │ ├── com.ofcoder.klein.storage.facade.LogManager │ │ │ │ └── com.ofcoder.klein.storage.facade.TraceManager │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── storage │ │ │ └── file │ │ │ └── trace │ │ │ └── TracerTest.java │ └── pom.xml ├── klein-storage-facade │ ├── src │ │ ├── test │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── storage │ │ │ │ └── facade │ │ │ │ ├── SnapTest.java │ │ │ │ ├── InstanceTest.java │ │ │ │ ├── FileSnapLogManagerTest.java │ │ │ │ └── trace │ │ │ │ └── TracerTest.java │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── storage │ │ │ └── facade │ │ │ ├── exception │ │ │ ├── LockException.java │ │ │ └── StorageException.java │ │ │ ├── TraceManager.java │ │ │ └── StorageEngine.java │ └── pom.xml ├── klein-storage-h2 │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── storage │ │ └── h2 │ │ └── ConnectionPool.java └── pom.xml ├── star.md ├── klein-serializer ├── klein-serializer-hessian2 │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── services │ │ │ │ │ └── com.ofcoder.klein.serializer.Serializer │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── serializer │ │ │ │ └── hessian2 │ │ │ │ ├── KleinHessian2Output.java │ │ │ │ └── Hessian2Util.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── serializer │ │ │ └── hessian2 │ │ │ └── Hessian2UtilTest.java │ └── pom.xml ├── klein-serializer-facade │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── serializer │ │ └── Serializer.java ├── klein-serializer-protobuf │ └── pom.xml └── pom.xml ├── klein-spi ├── src │ ├── test │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── services │ │ │ │ └── com.ofcoder.klein.spi.ext.DBConnection │ │ └── java │ │ │ └── com │ │ │ └── ofcoder │ │ │ └── klein │ │ │ └── spi │ │ │ ├── ext │ │ │ ├── DBConnection.java │ │ │ ├── MysqlConnection.java │ │ │ └── ArgsConnection.java │ │ │ ├── ArgsExtensionLoaderTest.java │ │ │ └── ExtensionLoaderTest.java │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── spi │ │ ├── SpiException.java │ │ ├── ExtensionFactory.java │ │ ├── SpiExtensionFactory.java │ │ ├── Join.java │ │ └── SPI.java └── pom.xml ├── .github ├── ISSUE_TEMPLATE │ ├── custom.md │ ├── feature_request.md │ └── bug_report.md └── workflows │ ├── codecov.yml │ ├── maven-publish.yml │ └── mvn_test.yml ├── .gitignore ├── git_email.sh ├── .gitpod.yml ├── klein-common ├── src │ ├── test │ │ ├── java │ │ │ └── com │ │ │ │ └── ofcoder │ │ │ │ └── klein │ │ │ │ └── common │ │ │ │ └── util │ │ │ │ ├── ThreadExecutorTest.java │ │ │ │ └── TrueTimeTest.java │ │ └── resources │ │ │ └── log4j2.xml │ └── main │ │ └── java │ │ └── com │ │ └── ofcoder │ │ └── klein │ │ └── common │ │ ├── Role.java │ │ ├── exception │ │ ├── NoImplementationException.java │ │ ├── KleinException.java │ │ ├── StartupException.java │ │ ├── ShutdownException.java │ │ └── SerializationException.java │ │ ├── util │ │ ├── timer │ │ │ └── TimerTask.java │ │ ├── StreamUtil.java │ │ ├── TrueTime.java │ │ └── ChecksumUtil.java │ │ ├── Holder.java │ │ ├── disruptor │ │ └── DisruptorEvent.java │ │ └── OnlyForTest.java └── pom.xml └── checkstyle-header.txt /klein-jepsen/docker/secret/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /klein-jepsen/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /klein-jepsen/docker/control/.gitignore: -------------------------------------------------------------------------------- 1 | jepsen -------------------------------------------------------------------------------- /klein-core/readme.md: -------------------------------------------------------------------------------- 1 | ### 如何高效的清理过期的缓存 2 | 3 | 采用分桶策略,避免扫描全部key -------------------------------------------------------------------------------- /klein-jepsen/docker/template/depends.yml: -------------------------------------------------------------------------------- 1 | - n%%N%% 2 | -------------------------------------------------------------------------------- /klein-jepsen/docker/bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | docker exec -it jepsen-control bash 3 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/main/resources/META-INF/services/com.ofcoder.klein.rpc.facade.RpcClient: -------------------------------------------------------------------------------- 1 | grpc=com.ofcoder.klein.rpc.grpc.GrpcClient -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/main/resources/META-INF/services/com.ofcoder.klein.rpc.facade.RpcEngine: -------------------------------------------------------------------------------- 1 | grpc=com.ofcoder.klein.rpc.grpc.GrpcEngine -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/main/resources/META-INF/services/com.ofcoder.klein.rpc.facade.RpcServer: -------------------------------------------------------------------------------- 1 | grpc=com.ofcoder.klein.rpc.grpc.GrpcServer -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/cache/readme.md: -------------------------------------------------------------------------------- 1 | Please run in the following order: 2 | 1. Main2 3 | 2. Main3 4 | 3. Main1 5 | -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/lock/readme.md: -------------------------------------------------------------------------------- 1 | Please run in the following order: 2 | 1. Main2 3 | 2. Main3 4 | 3. Main1 5 | -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/nwr/readme.md: -------------------------------------------------------------------------------- 1 | Please run in the following order: 2 | 1. Main2 3 | 2. Main3 4 | 3. Main1 5 | -------------------------------------------------------------------------------- /klein-jepsen/docker/bin/web: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PORT=$(docker port jepsen-control 8080 | cut -d : -f 2) 4 | xdg-open "http://localhost:$PORT" 5 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/elect_master.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shihuili1218/klein/HEAD/klein-consensus/klein-consensus-paxos/elect_master.png -------------------------------------------------------------------------------- /klein-jepsen/docker/template/server.properties: -------------------------------------------------------------------------------- 1 | klein.id=%%N%% 2 | klein.port=1218 3 | klein.ip=n%%N%% 4 | klein.outsider=false 5 | klein.members=%%MEMBER%% 6 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-file/src/main/resources/META-INF/services/com.ofcoder.klein.storage.facade.LogManager: -------------------------------------------------------------------------------- 1 | file=com.ofcoder.klein.storage.file.FileLogManager -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/change_member.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shihuili1218/klein/HEAD/klein-consensus/klein-consensus-paxos/change_member.png -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/elect_master_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shihuili1218/klein/HEAD/klein-consensus/klein-consensus-paxos/elect_master_1.png -------------------------------------------------------------------------------- /klein-storage/klein-storage-file/src/main/resources/META-INF/services/com.ofcoder.klein.storage.facade.TraceManager: -------------------------------------------------------------------------------- 1 | file=com.ofcoder.klein.storage.file.FileTraceManager -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/resources/META-INF/services/com.ofcoder.klein.consensus.facade.Consensus: -------------------------------------------------------------------------------- 1 | paxos=com.ofcoder.klein.consensus.paxos.PaxosConsensus -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-test/run_test.sh: -------------------------------------------------------------------------------- 1 | unset SSH_AUTH_SOCK 2 | lein run test --time-limit 600 --concurrency 5 --test-count 2 --username root --password 123456 $@ 3 | -------------------------------------------------------------------------------- /star.md: -------------------------------------------------------------------------------- 1 | # Star History 2 | 3 | [![Star History Chart](https://api.star-history.com/svg?repos=shihuili1218/klein&type=Date)](https://star-history.com/#shihuili1218/klein&Date) -------------------------------------------------------------------------------- /klein-jepsen/docker/.gitignore: -------------------------------------------------------------------------------- 1 | ./docker-compose.yml 2 | ./klein-jepsen-test/ 3 | ./node/n1/ 4 | ./node/n2/ 5 | ./node/n3/ 6 | ./node/n4/ 7 | ./node/n5/ 8 | ./start.sh 9 | ./stop.sh -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-hessian2/src/main/resources/META-INF/services/com.ofcoder.klein.serializer.Serializer: -------------------------------------------------------------------------------- 1 | hessian2=com.ofcoder.klein.serializer.hessian2.Hessian2Serializer -------------------------------------------------------------------------------- /klein-spi/src/test/resources/META-INF/services/com.ofcoder.klein.spi.ext.DBConnection: -------------------------------------------------------------------------------- 1 | mysql=com.ofcoder.klein.spi.ext.MysqlConnection 2 | args=com.ofcoder.klein.spi.ext.ArgsConnection 3 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-test/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /klein-jepsen/docker/docker-compose.dev.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | services: 3 | control: 4 | volumes: 5 | - ${JEPSEN_ROOT}:/jepsen # Mounts $JEPSEN_ROOT on host to /jepsen control container 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/deploy/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | nohup java -Xmx512m -Xms512m \ 3 | -jar /klein/klein-server.jar \ 4 | -Dklein.consensus.paxos.write=false \ 5 | $@ >> klein.log 2>&1 & 6 | echo "Done!" 7 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/resources/META-INF/services/com.ofcoder.klein.consensus.facade.nwr.Nwr: -------------------------------------------------------------------------------- 1 | majority=com.ofcoder.klein.consensus.facade.nwr.MajorityNwr 2 | fastWrite=com.ofcoder.klein.consensus.facade.nwr.FastWriteNwr -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/changemember/readme.md: -------------------------------------------------------------------------------- 1 | Please run in the following order: 2 | 1. Main2 3 | 2. Main3 4 | 3. Main1 or Main0. They will automatically join the cluster and exit the cluster after shutting down 5 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/deploy/server.properties: -------------------------------------------------------------------------------- 1 | klein.id=1 2 | klein.port=1218 3 | klein.ip=127.0.0.1 4 | klein.members=1:127.0.0.1:1218:false;2:127.0.0.1:1219:false;3:127.0.0.1:1220:false;4:127.0.0.1:1221:false;5:127.0.0.1:1222:false 5 | -------------------------------------------------------------------------------- /klein-spi/src/test/java/com/ofcoder/klein/spi/ext/DBConnection.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.spi.ext; 2 | 3 | import com.ofcoder.klein.spi.SPI; 4 | 5 | /** 6 | * @author 释慧利 7 | */ 8 | @SPI 9 | public interface DBConnection { 10 | } 11 | -------------------------------------------------------------------------------- /klein-spi/src/test/java/com/ofcoder/klein/spi/ext/MysqlConnection.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.spi.ext; 2 | 3 | import com.ofcoder.klein.spi.Join; 4 | 5 | /** 6 | * @author 释慧利 7 | */ 8 | @Join 9 | public class MysqlConnection implements DBConnection { 10 | } 11 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/deploy/stop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | pid=`ps -ef |grep klein-server |grep java |awk -F' ' '{print $2}'` 3 | if [ "$pid" != "" ] 4 | then 5 | echo "kill $pid" 6 | kill $pid 7 | fi 8 | echo "Done!" 9 | 10 | rm -rf /data 11 | 12 | echo "Cleanup!" -------------------------------------------------------------------------------- /klein-core/src/test/java/com/ofcoder/klein/rpc/facade/util/RpcUtilTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.rpc.facade.util; 2 | 3 | import org.junit.Test; 4 | 5 | public class RpcUtilTest { 6 | @Test 7 | public void testGetLocalIp() { 8 | System.out.println(RpcUtil.getLocalIp()); 9 | } 10 | } -------------------------------------------------------------------------------- /klein-example/src/test/java/com/ofcoder/klein/example/cache/Main2Test.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.example.cache; 2 | 3 | import junit.framework.TestCase; 4 | 5 | /** 6 | * @author far.liu 7 | */ 8 | public class Main2Test extends TestCase { 9 | 10 | public void test() { 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/core/ProposerTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos.core; 2 | 3 | import junit.framework.TestCase; 4 | 5 | public class ProposerTest extends TestCase { 6 | 7 | public void testPropose(){ 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /klein-spi/src/test/java/com/ofcoder/klein/spi/ext/ArgsConnection.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.spi.ext; 2 | 3 | import com.ofcoder.klein.spi.Join; 4 | 5 | /** 6 | * @author 释慧利 7 | */ 8 | @Join 9 | public class ArgsConnection implements DBConnection { 10 | 11 | public ArgsConnection(String hi) { 12 | System.out.printf(hi); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /klein-jepsen/docker/bin/readme.md: -------------------------------------------------------------------------------- 1 | 2 | docker ps -aq | xargs docker rm && docker rmi shihuili1218/klein-jepsen-control shihuili1218/klein-jepsen-node 3 | 4 | 5 | mvn clean install -DskipTests=true && klein-jepsen/docker/bin/up 6 | klein-jepsen/docker/bin/up 7 | 8 | docker exec -it jepsen-control bash 9 | 10 | lein run test --time-limit 40 --concurrency 10 --test-count 10 --username root --password 123456 -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/test/java/com/ofcoder/klein/storage/facade/SnapTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.storage.facade; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class SnapTest { 8 | 9 | @Test 10 | public void testGetCheckpoint() { 11 | Snap snap = new Snap(); 12 | snap.setCheckpoint(100L); 13 | assertEquals(100L, snap.getCheckpoint()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | *.mdb.* 22 | *.filemap 23 | *.mdb 24 | 25 | # dir 26 | .idea 27 | target 28 | data 29 | 30 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 31 | hs_err_pid* 32 | -------------------------------------------------------------------------------- /klein-core/src/test/java/com/ofcoder/klein/KleinPropTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class KleinPropTest { 7 | 8 | @Test 9 | public void testGetStorage() { 10 | KleinProp prop = new KleinProp(); 11 | Assert.assertEquals("file", prop.getStorage()); 12 | prop.setStorage("leveldb"); 13 | Assert.assertEquals("leveldb", prop.getStorage()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /klein-jepsen/docker/template/db.yml: -------------------------------------------------------------------------------- 1 | n%%N%%: 2 | << : *default-node 3 | container_name: n%%N%% 4 | hostname: n%%N%% 5 | volumes: 6 | - ./node/n%%N%%/server.properties:/klein/config/server.properties:ro 7 | - ./node/n%%N%%/server.properties:/config/server.properties:ro 8 | - ./klein-server.jar:/klein/klein-server.jar 9 | - ./start.sh:/klein/start.sh 10 | - ./stop.sh:/klein/stop.sh 11 | - "jepsen-shared:/var/jepsen/shared" -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/core/sm/PaxosMemberConfigurationTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos.core.sm; 2 | 3 | import junit.framework.TestCase; 4 | 5 | public class PaxosMemberConfigurationTest extends TestCase { 6 | 7 | public void setUp() throws Exception { 8 | super.setUp(); 9 | } 10 | 11 | public void tearDown() throws Exception { 12 | } 13 | 14 | public void testChangeMaster(){ 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /klein-jepsen/docker/control/bashrc: -------------------------------------------------------------------------------- 1 | eval $(ssh-agent) &> /dev/null 2 | ssh-add /root/.ssh/id_rsa &> /dev/null 3 | 4 | cat < 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /checkstyle-header.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ -------------------------------------------------------------------------------- /klein-common/src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-test/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2018-04-25 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2018-04-25 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [Unreleased]: https://github.com/your-name/jepsen.atomic/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/jepsen.atomic/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /klein-spi/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.spi 13 | klein-spi 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/test/java/com/ofcoder/klein/rpc/grpc/ext/HelloProcessor.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.rpc.grpc.ext; 2 | 3 | import com.ofcoder.klein.rpc.facade.RpcContext; 4 | import com.ofcoder.klein.rpc.facade.RpcProcessor; 5 | import java.nio.charset.StandardCharsets; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | /** 10 | * @author 释慧利 11 | */ 12 | public class HelloProcessor implements RpcProcessor { 13 | private static final Logger LOG = LoggerFactory.getLogger(HelloProcessor.class); 14 | 15 | @Override 16 | public String service() { 17 | return String.class.getSimpleName(); 18 | } 19 | 20 | @Override 21 | public void handleRequest(byte[] request, RpcContext context) { 22 | 23 | LOG.info("receive client message: {}", new String(request, StandardCharsets.UTF_8)); 24 | context.response("hello, klein".getBytes(StandardCharsets.UTF_8)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/test/java/com/ofcoder/klein/consensus/facade/nwr/NwrTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.facade.nwr; 2 | 3 | import com.ofcoder.klein.spi.ExtensionLoader; 4 | import junit.framework.TestCase; 5 | 6 | public class NwrTest extends TestCase { 7 | 8 | public void testR() { 9 | Nwr majority = ExtensionLoader.getExtensionLoader(Nwr.class).register("majority"); 10 | Nwr fastWrite = ExtensionLoader.getExtensionLoader(Nwr.class).register("fastWrite"); 11 | 12 | assertEquals(2, majority.r(3)); 13 | assertEquals(3, fastWrite.r(3)); 14 | } 15 | 16 | public void testW() { 17 | Nwr majority = ExtensionLoader.getExtensionLoader(Nwr.class).register("majority"); 18 | Nwr fastWrite = ExtensionLoader.getExtensionLoader(Nwr.class).register("fastWrite"); 19 | 20 | assertEquals(majority.w(3), 2); 21 | assertEquals(fastWrite.w(3), 1); 22 | } 23 | } -------------------------------------------------------------------------------- /klein-spi/src/test/java/com/ofcoder/klein/spi/ExtensionLoaderTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.spi; 2 | 3 | import com.ofcoder.klein.spi.ext.DBConnection; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | import org.mockito.Mockito; 8 | 9 | import java.net.URL; 10 | import java.net.URLClassLoader; 11 | 12 | /** 13 | * @author 释慧利 14 | */ 15 | public class ExtensionLoaderTest { 16 | 17 | @Test 18 | public void testSameJoin() { 19 | DBConnection zero = 20 | ExtensionLoader.getExtensionLoader(DBConnection.class).register("mysql"); 21 | DBConnection first = 22 | ExtensionLoader.getExtensionLoader(DBConnection.class).getJoin("mysql"); 23 | DBConnection second = 24 | ExtensionLoader.getExtensionLoader(DBConnection.class).getJoin("mysql"); 25 | Assert.assertEquals(first, zero); 26 | Assert.assertEquals(first, second); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/ProposalNoUtilTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import junit.framework.TestCase; 7 | 8 | public class ProposalNoUtilTest extends TestCase { 9 | private static final Logger LOG = LoggerFactory.getLogger(ProposalNoUtilTest.class); 10 | 11 | public void testMakePno() { 12 | for (int i = 0; i < 100; i++) { 13 | for (int counter = 0; counter < 50; counter++) { 14 | long pno = ProposalNoUtil.makePno(i, counter); 15 | LOG.info("pno: {}, str: {}", pno, ProposalNoUtil.pnoToString(pno)); 16 | assertEquals(ProposalNoUtil.getCounterFromPno(pno), counter); 17 | assertEquals(ProposalNoUtil.getEpochFromPno(pno), i); 18 | } 19 | LOG.info("i: {}", i); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein-rpc 7 | com.ofcoder.klein.rpc 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.rpc.facade 13 | klein-rpc-facade 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/test/java/com/ofcoder/klein/storage/facade/InstanceTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.storage.facade; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.mockito.Mockito.mock; 5 | import static org.mockito.Mockito.when; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | public class InstanceTest { 11 | 12 | Instance instance; 13 | 14 | long testInstanceId = 100L; 15 | 16 | @Before 17 | public void setUp() { 18 | instance = mock(Instance.class); 19 | } 20 | 21 | @Test 22 | public void testGetInstanceId() { 23 | when(instance.getInstanceId()).thenReturn(testInstanceId); 24 | 25 | long result = instance.getInstanceId(); 26 | 27 | assertEquals( 28 | "getInstanceId should return the instanceId set in the instance object", 29 | testInstanceId, 30 | result); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-example/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /klein-jepsen/docker/node/setup-jepsen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # We add our hostname to the shared volume, so that control can find us 4 | echo "Adding hostname to shared volume" >> /var/log/jepsen-setup.log 5 | # We do a little dance to get our hostname (random hex), IP, then use DNS to 6 | # get a proper container name. 7 | #HOSTNAME=`hostname` 8 | #IP=`getent hosts "${HOSTNAME}" | awk '{ print $1 }'` 9 | #NAME=`dig +short -x "${IP}" | cut -f 1 -d .` 10 | #echo "${NAME}" >> /var/jepsen/shared/nodes 11 | mkdir -p /var/jepsen/shared 12 | echo `hostname` >> /var/jepsen/shared/nodes 13 | 14 | # We make sure that root's authorized keys are ready 15 | echo "Setting up root's authorized_keys" >> /var/log/jepsen-setup.log 16 | mkdir /root/.ssh 17 | chmod 700 /root/.ssh 18 | cp /run/secrets/authorized_keys /root/.ssh/ 19 | chmod 600 /root/.ssh/authorized_keys 20 | /etc/init.d/ssh start 21 | sh /klein/start.sh 22 | chmod +x /klein/start.sh \ 23 | && chmod +x /klein/stop.sh 24 | 25 | tail -f /dev/null -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ofcoder.klein.serializer 8 | klein-serializer 9 | 0.0.8 10 | 11 | 12 | klein-serializer-facade 13 | com.ofcoder.klein.serializer.facade 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-protobuf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ofcoder.klein.serializer 8 | klein-serializer 9 | 0.0.8 10 | 11 | 12 | klein-serializer-protobuf 13 | com.ofcoder.klein.serializer.protobuf 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | -------------------------------------------------------------------------------- /.github/workflows/maven-publish.yml: -------------------------------------------------------------------------------- 1 | name: Maven Publish 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | publish: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout Git Repo 12 | uses: actions/checkout@v2 13 | 14 | - name: Set up JDK 1.8 15 | uses: actions/setup-java@v1 16 | with: 17 | java-version: 1.8 18 | server-id: ossrh 19 | server-username: MAVEN_USERNAME 20 | server-password: MAVEN_PASSWORD 21 | gpg-private-key: ${{ secrets.GPG_SECRET }} # Value of the GPG private key to import 22 | gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase 23 | 24 | - name: Build with Maven 25 | run: mvn clean deploy --batch-mode -DskipTests -Pdeploy -B -U -e 26 | env: 27 | MAVEN_USERNAME: ${{ secrets.OSSRH_USER }} 28 | MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} 29 | MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSWORD }} 30 | -------------------------------------------------------------------------------- /klein-jepsen/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.jepsen 13 | klein-jepsen 14 | 0.0.8 15 | pom 16 | 17 | klein-jepsen-server 18 | 19 | 20 | 21 | 8 22 | 8 23 | UTF-8 24 | 25 | 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/workflows/mvn_test.yml: -------------------------------------------------------------------------------- 1 | name: mvn_test 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | check_format: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up JDK 1.8 15 | uses: actions/setup-java@v1 16 | with: 17 | java-version: 1.8 18 | - name: Check code style 19 | run: mvn clean compile 20 | 21 | maven_test: 22 | runs-on: ubuntu-latest 23 | steps: 24 | - uses: actions/checkout@v2 25 | - name: Set up JDK 1.8 26 | uses: actions/setup-java@v1 27 | with: 28 | java-version: 1.8 29 | 30 | - name: Package 31 | run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V 32 | 33 | - name: Run tests 34 | run: mvn -B test 35 | 36 | - name: Upload coverage to Codecov 37 | run: curl -Os https://uploader.codecov.io/latest/linux/codecov && chmod +x codecov && ./codecov -t ${CODECOV_TOKEN} -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/QueueTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos; 2 | 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | import java.util.Set; 6 | import java.util.concurrent.ConcurrentHashMap; 7 | import java.util.concurrent.PriorityBlockingQueue; 8 | 9 | import org.junit.Test; 10 | 11 | public class QueueTest { 12 | 13 | PriorityBlockingQueue learnQueue = new PriorityBlockingQueue<>(1024, Comparator.comparingLong(value -> value)); 14 | 15 | @Test 16 | public void testPeek() throws InterruptedException { 17 | final Set runningInstance = Collections.newSetFromMap(new ConcurrentHashMap<>()); 18 | System.out.println(runningInstance.add(1L)); 19 | System.out.println(runningInstance.add(2L)); 20 | System.out.println(runningInstance.add(1L)); 21 | System.out.println(runningInstance.add(3L)); 22 | System.out.println(runningInstance.add(1L)); 23 | System.out.println(runningInstance); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-file/src/test/java/com/ofcoder/klein/storage/file/trace/TracerTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.storage.file.trace; 2 | 3 | import com.ofcoder.klein.common.util.ThreadExecutor; 4 | import com.ofcoder.klein.spi.ExtensionLoader; 5 | import com.ofcoder.klein.storage.facade.TraceManager; 6 | import com.ofcoder.klein.storage.facade.config.StorageProp; 7 | import com.ofcoder.klein.storage.facade.trace.Tracer; 8 | import junit.framework.TestCase; 9 | 10 | public class TracerTest extends TestCase { 11 | 12 | public void testTrace() throws InterruptedException { 13 | StorageProp prop = new StorageProp(); 14 | prop.setTraceBlockSize(100); 15 | 16 | ExtensionLoader.getExtensionLoader(TraceManager.class).register("file", prop); 17 | Tracer tracer = new Tracer("propose", prop); 18 | for (int i = 0; i < 1024; i++) { 19 | int finalI = i; 20 | ThreadExecutor.execute(() -> tracer.trace(finalI + "zzzzzzzz")); 21 | } 22 | Thread.sleep(500L); 23 | tracer.shutdown(); 24 | Thread.sleep(500L); 25 | } 26 | } -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/GetReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | /** 20 | * cache get request. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class GetReq extends BaseReq { 25 | } 26 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/core/HolderTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos.core; 2 | 3 | import java.util.concurrent.atomic.AtomicLong; 4 | 5 | import org.junit.Assert; 6 | 7 | import com.ofcoder.klein.common.Holder; 8 | import junit.framework.TestCase; 9 | 10 | public class HolderTest extends TestCase { 11 | 12 | public void testInstanceHolder() { 13 | AtomicLong i = new AtomicLong(0); 14 | Holder instanceHolder = new Holder() { 15 | @Override 16 | protected Long create() { 17 | System.out.println("generateId"); 18 | return i.incrementAndGet(); 19 | } 20 | }; 21 | System.out.println("new"); 22 | long actual = 1; 23 | Assert.assertEquals(instanceHolder.get().longValue(), actual); 24 | Assert.assertEquals(instanceHolder.get().longValue(), actual); 25 | Assert.assertEquals(instanceHolder.get().longValue(), actual); 26 | Assert.assertEquals(instanceHolder.get().longValue(), actual); 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /klein-jepsen/docker/control/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM maven:3.6-openjdk-8 2 | MAINTAINER jake@apache.org 3 | 4 | ENV LEIN_ROOT true 5 | 6 | # 7 | # Jepsen dependencies 8 | # 9 | RUN apt-get -y -q update \ 10 | && apt-get install -qy libjna-java \ 11 | vim \ 12 | emacs \ 13 | git \ 14 | htop \ 15 | screen \ 16 | pssh \ 17 | curl \ 18 | wget \ 19 | gnuplot \ 20 | graphviz \ 21 | dos2unix 22 | 23 | # RUN apt-get remove openjdk* -y && apt-get install -qy openjdk-8-jdk 24 | 25 | RUN wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein && \ 26 | mv lein /usr/bin && \ 27 | chmod +x /usr/bin/lein && \ 28 | lein self-install 29 | 30 | # without --dev flag up.sh copies jepsen to these subfolders 31 | # with --dev flag they are empty until mounted 32 | #COPY jepsen/jepsen /jepsen/jepsen/ 33 | #RUN if [ -f /jepsen/jepsen/project.clj ]; then cd /jepsen/jepsen && lein install; fi 34 | #COPY jepsen /jepsen/ 35 | 36 | ADD ./bashrc /root/.bashrc 37 | ADD ./init.sh /init.sh 38 | RUN dos2unix /init.sh /root/.bashrc \ 39 | && chmod +x /init.sh 40 | 41 | CMD /init.sh 42 | -------------------------------------------------------------------------------- /klein-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.common 13 | klein-common 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | org.javassist 25 | javassist 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/ExistsReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | /** 20 | * cache exists request. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class ExistsReq extends BaseReq { 25 | } 26 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/vo/Sync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc.vo; 18 | 19 | /** 20 | * Sync. 21 | * 22 | * @author 释慧利 23 | */ 24 | public enum Sync { 25 | SINGLE, SNAP, NO_SUPPORT 26 | } 27 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/InvalidateReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | /** 20 | * cache invidate request. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class InvalidateReq extends BaseReq { 25 | } 26 | -------------------------------------------------------------------------------- /klein-common/src/test/java/com/ofcoder/klein/common/util/TrueTimeTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.common.util; 2 | 3 | import java.text.SimpleDateFormat; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import junit.framework.TestCase; 9 | 10 | public class TrueTimeTest extends TestCase { 11 | private static final Logger LOG = LoggerFactory.getLogger(ThreadExecutorTest.class); 12 | 13 | public void testCurrentTimeMillis() throws Exception { 14 | SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 15 | 16 | long trueTime = TrueTime.currentTimeMillis(); 17 | LOG.info(s.format(trueTime)); 18 | 19 | LOG.info(TrueTime.currentTimeMillis() - trueTime + ""); 20 | 21 | TrueTime.heartbeat(trueTime + 1000); 22 | trueTime = TrueTime.currentTimeMillis(); 23 | LOG.info(s.format(trueTime)); 24 | 25 | Thread.sleep(6L); 26 | 27 | trueTime = TrueTime.currentTimeMillis(); 28 | LOG.info(s.format(trueTime)); 29 | 30 | trueTime = TrueTime.currentTimeMillis(); 31 | LOG.info(s.format(trueTime)); 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/test/java/com/ofcoder/klein/storage/facade/FileSnapLogManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.storage.facade; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.net.URL; 6 | 7 | import org.junit.Test; 8 | 9 | import junit.framework.TestCase; 10 | 11 | public class FileSnapLogManagerTest extends TestCase { 12 | 13 | 14 | @Test 15 | public void test() throws IOException { 16 | String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "data"; 17 | System.out.println(path); 18 | 19 | path = this.getClass().getResource("/").getPath(); 20 | System.out.println(path); 21 | 22 | path = this.getClass().getResource("").getPath(); 23 | System.out.println(path); 24 | 25 | File file = new File(""); 26 | path = file.getCanonicalPath(); 27 | System.out.println(path); 28 | 29 | URL path1 = this.getClass().getResource(""); 30 | System.out.println(path1); 31 | 32 | path = System.getProperty("user.dir"); 33 | System.out.println(path); 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/vo/Pong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc.vo; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * pong. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class Pong implements Serializable { 27 | } 28 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-test/project.clj: -------------------------------------------------------------------------------- 1 | (require 'cemerick.pomegranate.aether) 2 | (cemerick.pomegranate.aether/register-wagon-factory! 3 | "http" #(org.apache.maven.wagon.providers.http.HttpWagon.)) 4 | 5 | (defproject jepsen.klein "0.1.0-SNAPSHOT" 6 | :description "klein cache jepsen test" 7 | :url "" 8 | :license {:name "Eclipse Public License" 9 | :url "http://www.eclipse.org/legal/epl-v10.html"} 10 | :main jepsen.klein 11 | :jvm-opts ["-Xms2g" "-Xmx2g" "-server"] 12 | :dependencies [ 13 | [org.clojure/clojure "1.10.0"] 14 | [jepsen "0.1.19"] 15 | ; [org.clojure/clojure "1.9.0"] 16 | ; [jepsen "0.1.11"] 17 | [clj-ssh "0.5.14"] 18 | [cider/cider-nrepl "0.17.0-SNAPSHOT"] 19 | [org.clojure/tools.nrepl "0.2.13" :exclusions [org.clojure/clojure]] 20 | [net.java.dev.jna/jna "4.5.1"] 21 | [javax.xml.bind/jaxb-api "2.3.1"] 22 | [org.glassfish.jaxb/jaxb-runtime "2.3.1"] 23 | [com.ofcoder.klein.jepsen.server/klein-jepsen-server "0.0.1"] 24 | ] 25 | ) 26 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/sm/SystemOp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.sm; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * System Operator. 23 | * 24 | * @author 释慧利 25 | */ 26 | public interface SystemOp extends Serializable { 27 | } 28 | -------------------------------------------------------------------------------- /klein-jepsen/docker/template/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | x-node: 3 | &default-node 4 | image: shihuili1218/klein-jepsen-node:latest 5 | env_file: ./secret/node.env 6 | secrets: 7 | - authorized_keys 8 | tty: true 9 | tmpfs: 10 | - /run:size=100M 11 | - /run/lock:size=100M 12 | volumes: 13 | - "jepsen-shared:/var/jepsen/shared" 14 | networks: 15 | - jepsen 16 | privileged: true 17 | cap_add: 18 | - ALL 19 | ports: 20 | - ${JEPSEN_PORT:-22} 21 | 22 | volumes: 23 | jepsen-shared: 24 | 25 | secrets: 26 | authorized_keys: 27 | file: ./secret/authorized_keys 28 | 29 | networks: 30 | jepsen: 31 | 32 | services: 33 | control: 34 | container_name: jepsen-control 35 | hostname: control 36 | depends_on: 37 | %%DEPS%% 38 | image: shihuili1218/klein-jepsen-control:latest 39 | env_file: ./secret/control.env 40 | privileged: true 41 | ports: 42 | - "22" 43 | - "1218" 44 | networks: 45 | - jepsen 46 | volumes: 47 | - ./klein-server.jar:/jepsen/klein-server.jar 48 | - "jepsen-shared:/var/jepsen/shared" 49 | - ./klein-jepsen-test:/jepsen/klein 50 | %%DBS%% 51 | -------------------------------------------------------------------------------- /klein-spi/src/main/java/com/ofcoder/klein/spi/SpiException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.spi; 18 | 19 | /** 20 | * SPI exception. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class SpiException extends RuntimeException { 25 | public SpiException(final String message) { 26 | super(message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /klein-jepsen/docker/control/init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | : "${SSH_PRIVATE_KEY?SSH_PRIVATE_KEY is empty, please use up.sh}" 4 | : "${SSH_PUBLIC_KEY?SSH_PUBLIC_KEY is empty, please use up.sh}" 5 | 6 | if [ ! -f ~/.ssh/known_hosts ]; then 7 | mkdir -m 700 ~/.ssh 8 | echo $SSH_PRIVATE_KEY | perl -p -e 's/↩/\n/g' > ~/.ssh/id_rsa 9 | chmod 600 ~/.ssh/id_rsa 10 | echo $SSH_PUBLIC_KEY > ~/.ssh/id_rsa.pub 11 | echo > ~/.ssh/known_hosts 12 | # Get nodes list 13 | sort -V /var/jepsen/shared/nodes > ~/nodes 14 | # Scan SSH keys 15 | while read node; do 16 | ssh-keyscan -t rsa $node >> ~/.ssh/known_hosts 17 | ssh-keyscan -t ed25519 $node >> ~/.ssh/known_hosts 18 | done <~/nodes 19 | fi 20 | 21 | # TODO: assert that SSH_PRIVATE_KEY==~/.ssh/id_rsa 22 | 23 | cat < 2 | 5 | 6 | klein-storage 7 | com.ofcoder.klein.storage 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.storage.file 13 | klein-storage-file 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | com.ofcoder.klein.storage.api 26 | klein-storage-facade 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/sm/AbstractSM.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.sm; 18 | 19 | /** 20 | * Add Snapshot and Checkpoint in SM. 21 | * 22 | * @author 释慧利 23 | */ 24 | public abstract class AbstractSM implements SM { 25 | 26 | @Override 27 | public void close() { 28 | 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | import java.io.Serializable; 20 | 21 | import com.ofcoder.klein.storage.facade.LogManager; 22 | 23 | /** 24 | * Node. 25 | * 26 | * @author 释慧利 27 | */ 28 | public abstract class Node implements Serializable, LogManager.MetaData { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein-storage 7 | com.ofcoder.klein.storage 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.storage.api 13 | klein-storage-facade 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | 26 | com.ofcoder.klein.serializer.facade 27 | klein-serializer-facade 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/Role.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common; 18 | 19 | /** 20 | * Lifecycle. 21 | * 22 | * @author 释慧利 23 | */ 24 | public interface Role { 25 | 26 | /** 27 | * Bean init. 28 | * 29 | * @param op property 30 | */ 31 | void init(O op); 32 | 33 | /** 34 | * Bean shutdown. 35 | */ 36 | void shutdown(); 37 | } 38 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/exception/NoImplementationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.exception; 18 | 19 | /** 20 | * No Implementation Exception, Used to mark abstract methods. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class NoImplementationException extends KleinException { 25 | public NoImplementationException(final String message) { 26 | super(message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /klein-jepsen/docker/bin/build-docker-compose: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Builds a docker-compose file. You'd THINK we could do this with `replicas` 4 | # but nooooooo, down that path lies madness. Instead we're going to do some 5 | # janky templating with sed and awk. I am so, so sorry. 6 | 7 | # Takes a number of nodes to generate a file for, and emits a file 8 | # `docker-compose.yml`. 9 | 10 | NODE_COUNT=$1 11 | 12 | DEPS="" 13 | DBS="" 14 | member="" 15 | 16 | for ((n=1;n<=NODE_COUNT;n++)); do 17 | member+=${n}:n${n}:1218:false\; 18 | done; 19 | 20 | # For each node 21 | for ((n=1;n<=NODE_COUNT;n++)); do 22 | # Build up deps for control 23 | LINE=`cat template/depends.yml | sed s/%%N%%/${n}/g` 24 | DEPS="${DEPS}${LINE}"$'\n' 25 | 26 | # Build up DB service 27 | DB=`cat template/db.yml | sed s/%%N%%/${n}/g | sed s/%%MEMBER%%/${member}/g` 28 | DBS="${DBS}${DB}"$'\n' 29 | 30 | mkdir -p node/n${n} 31 | cat template/server.properties | sed s/%%N%%/${n}/g | sed s/%%MEMBER%%/${member}/g > node/n${n}/server.properties 32 | done 33 | 34 | # Build docker-compose file 35 | export DEPS 36 | export DBS 37 | cat template/docker-compose.yml | 38 | awk ' {gsub(/%%DEPS%%/, ENVIRON["DEPS"]); print} ' | 39 | awk ' {gsub(/%%DBS%%/, ENVIRON["DBS"]); print} ' \ 40 | > docker-compose.yml 41 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/exception/ChangeMemberException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.exception; 18 | 19 | /** 20 | * ChangeMemberException. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class ChangeMemberException extends ConsensusException { 25 | 26 | public ChangeMemberException(final String message) { 27 | super(message); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/Command.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * consensus content. 23 | */ 24 | public interface Command extends Serializable { 25 | String getGroup(); 26 | 27 | /** 28 | * client's input data. 29 | * 30 | * @return data 31 | */ 32 | byte[] getData(); 33 | 34 | boolean getIfSystemOp(); 35 | } 36 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/exception/KleinException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.exception; 18 | 19 | /** 20 | * Common exception. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class KleinException extends RuntimeException { 25 | public KleinException(final String message) { 26 | super(message); 27 | } 28 | 29 | public KleinException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-h2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein-storage 7 | com.ofcoder.klein.storage 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.storage.h2 13 | klein-storage-h2 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | com.h2database 26 | h2 27 | 28 | 29 | com.ofcoder.klein.storage.api 30 | klein-storage-facade 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /klein-spi/src/main/java/com/ofcoder/klein/spi/ExtensionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.spi; 19 | 20 | /** 21 | * The interface Extension factory. 22 | */ 23 | @SPI("spi") 24 | public interface ExtensionFactory { 25 | 26 | /** 27 | * Gets Extension. 28 | * 29 | * @param the type parameter 30 | * @param key the key 31 | * @param clazz the clazz 32 | * @return the extension 33 | */ 34 | T getExtension(String key, Class clazz); 35 | } 36 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/exception/StartupException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.exception; 18 | 19 | /** 20 | * Startup Occur Exception. 21 | * 22 | * @author far.liu 23 | */ 24 | public class StartupException extends KleinException { 25 | public StartupException(final String message) { 26 | super(message); 27 | } 28 | 29 | public StartupException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/RpcContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade; 18 | 19 | /** 20 | * Rpc Context. 21 | * 22 | * @author 释慧利 23 | */ 24 | public interface RpcContext { 25 | /** 26 | * response caller. 27 | * 28 | * @param msg response data 29 | */ 30 | void response(byte[] msg); 31 | 32 | /** 33 | * get caller info. 34 | * 35 | * @return ip:port 36 | */ 37 | String getRemoteAddress(); 38 | } 39 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/exception/ShutdownException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.exception; 18 | 19 | /** 20 | * Shutdown Occur Exception. 21 | * 22 | * @author far.liu 23 | */ 24 | public class ShutdownException extends KleinException { 25 | public ShutdownException(final String message) { 26 | super(message); 27 | } 28 | 29 | public ShutdownException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-hessian2/src/main/java/com/ofcoder/klein/serializer/hessian2/KleinHessian2Output.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.serializer.hessian2; 18 | 19 | import com.caucho.hessian.io.Hessian2Output; 20 | import java.io.OutputStream; 21 | 22 | /** 23 | * implements AutoCloseable for Hessian2Output. 24 | */ 25 | public class KleinHessian2Output extends Hessian2Output implements AutoCloseable { 26 | public KleinHessian2Output(final OutputStream os) { 27 | super(os); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-grpc/src/main/java/com/ofcoder/klein/rpc/grpc/GrpcConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.rpc.grpc; 19 | 20 | /** 21 | * Grpc constants. 22 | */ 23 | public class GrpcConstants { 24 | 25 | /** 26 | * json descriptor proto name. 27 | */ 28 | public static final String JSON_DESCRIPTOR_PROTO_NAME = "JsonMessage"; 29 | 30 | /** 31 | * json descriptor proto field name. 32 | */ 33 | public static final String JSON_DESCRIPTOR_PROTO_FIELD_NAME = "data"; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/main/java/com/ofcoder/klein/storage/facade/exception/LockException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.storage.facade.exception; 18 | 19 | /** 20 | * Lock Exception. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class LockException extends StorageException { 25 | public LockException(final String message) { 26 | super(message); 27 | } 28 | 29 | public LockException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/exception/SerializationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.exception; 18 | 19 | /** 20 | * Serialization Exception. 21 | * 22 | * @author far.liu 23 | */ 24 | public class SerializationException extends KleinException { 25 | public SerializationException(final String message) { 26 | super(message); 27 | } 28 | 29 | public SerializationException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/InvokeCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade; 18 | 19 | /** 20 | * Rpc Invoke Callback. 21 | * 22 | * @author 释慧利 23 | */ 24 | public interface InvokeCallback { 25 | /** 26 | * occur error. 27 | * 28 | * @param err error detail 29 | */ 30 | void error(Throwable err); 31 | 32 | /** 33 | * invoke completed. 34 | * 35 | * @param result response 36 | */ 37 | void complete(byte[] result); 38 | } 39 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/exception/ConnectionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade.exception; 18 | 19 | /** 20 | * Connection Exception. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class ConnectionException extends RpcException { 25 | public ConnectionException(final String message) { 26 | super(message); 27 | } 28 | 29 | public ConnectionException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/nwr/FastWriteNwr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.nwr; 18 | 19 | import com.ofcoder.klein.spi.Join; 20 | 21 | /** 22 | * FastWriteNwr: w = 1, r = n. 23 | * 24 | * @author 释慧利 25 | */ 26 | @Join 27 | public class FastWriteNwr implements Nwr { 28 | @Override 29 | public int r(final int n) { 30 | return n; 31 | } 32 | 33 | @Override 34 | public int w(final int n) { 35 | return 1; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-hessian2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ofcoder.klein.serializer 8 | klein-serializer 9 | 0.0.8 10 | 11 | 12 | klein-serializer-hessian2 13 | com.ofcoder.klein.serializer.hessian2 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | com.caucho 26 | hessian 27 | 28 | 29 | com.ofcoder.klein.serializer.facade 30 | klein-serializer-facade 31 | 32 | 33 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/exception/StateMachineException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.exception; 18 | 19 | /** 20 | * StateMachine Exception. 21 | */ 22 | public class StateMachineException extends ConsensusException { 23 | public StateMachineException(final String message) { 24 | super(message); 25 | } 26 | 27 | public StateMachineException(final String message, final Throwable cause) { 28 | super(message, cause); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/exception/InvokeTimeoutException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade.exception; 18 | 19 | /** 20 | * Invoke Timeout Exception. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class InvokeTimeoutException extends RpcException { 25 | public InvokeTimeoutException(final String message) { 26 | super(message); 27 | } 28 | 29 | public InvokeTimeoutException(final String message, final Throwable cause) { 30 | super(message, cause); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/nwr/MajorityNwr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.nwr; 18 | 19 | import com.ofcoder.klein.spi.Join; 20 | 21 | /** 22 | * Majority NWR. 23 | * w = n / 2 + 1 24 | * 25 | * @author 释慧利 26 | */ 27 | @Join 28 | public class MajorityNwr implements Nwr { 29 | @Override 30 | public int r(final int n) { 31 | return n / 2 + 1; 32 | } 33 | 34 | @Override 35 | public int w(final int n) { 36 | return n / 2 + 1; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/main/java/com/ofcoder/klein/storage/facade/TraceManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.storage.facade; 18 | 19 | import java.util.List; 20 | 21 | import com.ofcoder.klein.spi.SPI; 22 | 23 | @SPI 24 | public interface TraceManager { 25 | /** 26 | * save trace record. 27 | * 28 | * @param name file name 29 | * @param contents trace entry 30 | */ 31 | void save(String name, List contents); 32 | 33 | /** 34 | * Bean shutdown. 35 | */ 36 | void shutdown(); 37 | } 38 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/exception/RpcException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade.exception; 18 | 19 | import com.ofcoder.klein.common.exception.KleinException; 20 | 21 | /** 22 | * Rpc Common Exception. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class RpcException extends KleinException { 27 | public RpcException(final String message) { 28 | super(message); 29 | } 30 | 31 | public RpcException(final String message, final Throwable cause) { 32 | super(message, cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /klein-rpc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.rpc 13 | klein-rpc 14 | pom 15 | 0.0.8 16 | 17 | 18 | klein-rpc-grpc 19 | klein-rpc-facade 20 | 21 | 22 | 23 | 8 24 | 8 25 | UTF-8 26 | 27 | 28 | 29 | 30 | com.ofcoder.klein.common 31 | klein-common 32 | 33 | 34 | com.ofcoder.klein.spi 35 | klein-spi 36 | 37 | 38 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/RpcServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade; 18 | 19 | import com.ofcoder.klein.spi.SPI; 20 | 21 | /** 22 | * Rpc Server. 23 | * 24 | * @author far.liu 25 | */ 26 | @SPI 27 | public interface RpcServer { 28 | 29 | /** 30 | * Register user processor. 31 | * 32 | * @param processor the user processor which has a interest 33 | */ 34 | void registerProcessor(RpcProcessor processor); 35 | 36 | /** 37 | * Bean shutdown. 38 | */ 39 | void shutdown(); 40 | } 41 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/core/sm/ElectionOp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.core.sm; 18 | 19 | import com.ofcoder.klein.consensus.facade.sm.SystemOp; 20 | 21 | /** 22 | * system operator for eletion master. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class ElectionOp implements SystemOp { 27 | private String nodeId; 28 | 29 | public String getNodeId() { 30 | return nodeId; 31 | } 32 | 33 | public void setNodeId(final String nodeId) { 34 | this.nodeId = nodeId; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/main/java/com/ofcoder/klein/storage/facade/exception/StorageException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.storage.facade.exception; 18 | 19 | import com.ofcoder.klein.common.exception.KleinException; 20 | 21 | /** 22 | * Storage Exception. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class StorageException extends KleinException { 27 | public StorageException(final String message) { 28 | super(message); 29 | } 30 | 31 | public StorageException(final String message, final Throwable cause) { 32 | super(message, cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /klein-consensus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.consensus 13 | klein-consensus 14 | pom 15 | 0.0.8 16 | 17 | 18 | klein-consensus-paxos 19 | klein-consensus-facade 20 | 21 | 22 | 23 | 8 24 | 8 25 | UTF-8 26 | 27 | 28 | 29 | 30 | com.ofcoder.klein.spi 31 | klein-spi 32 | 33 | 34 | com.ofcoder.klein.common 35 | klein-common 36 | 37 | 38 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/exception/ConsensusException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.exception; 18 | 19 | import com.ofcoder.klein.common.exception.KleinException; 20 | 21 | /** 22 | * Consensus Exception. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class ConsensusException extends KleinException { 27 | public ConsensusException(final String message) { 28 | super(message); 29 | } 30 | 31 | public ConsensusException(final String message, final Throwable cause) { 32 | super(message, cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/exception/SnapshotException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.consensus.facade.exception; 19 | 20 | import com.ofcoder.klein.common.exception.KleinException; 21 | 22 | /** 23 | * Snapshot Exception. 24 | * 25 | * @author hang.li 26 | */ 27 | public class SnapshotException extends KleinException { 28 | public SnapshotException(final String message) { 29 | super(message); 30 | } 31 | 32 | public SnapshotException(final String message, final Throwable cause) { 33 | super(message, cause); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/config/SnapshotStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.config; 18 | 19 | /** 20 | * Snapshot Strategy. 21 | */ 22 | public class SnapshotStrategy { 23 | private int second; 24 | private int reqCount; 25 | 26 | public SnapshotStrategy(final int second, final int reqCount) { 27 | this.second = second; 28 | this.reqCount = reqCount; 29 | } 30 | 31 | public int getSecond() { 32 | return second; 33 | } 34 | 35 | public int getReqCount() { 36 | return reqCount; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/vo/ElasticRes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc.vo; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * Join Cluster Response. 23 | * It just means that the request has been received, and the cluster will do its best to complete it 24 | */ 25 | public class ElasticRes implements Serializable { 26 | 27 | private boolean result; 28 | 29 | public boolean isResult() { 30 | return result; 31 | } 32 | 33 | public void setResult(final boolean result) { 34 | this.result = result; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/LatchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.concurrent.CountDownLatch; 22 | import java.util.stream.Collectors; 23 | 24 | import org.junit.Test; 25 | 26 | /** 27 | * @author 释慧利 28 | */ 29 | public class LatchTest { 30 | @Test 31 | public void testCountDownTwo() throws InterruptedException { 32 | CountDownLatch latch = new CountDownLatch(1); 33 | latch.countDown(); 34 | latch.countDown(); 35 | latch.await(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /klein-storage/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.storage 13 | klein-storage 14 | pom 15 | 0.0.8 16 | 17 | 18 | klein-storage-facade 19 | klein-storage-h2 20 | klein-storage-file 21 | 22 | 23 | 24 | 8 25 | 8 26 | UTF-8 27 | 28 | 29 | 30 | 31 | 32 | com.ofcoder.klein.spi 33 | klein-spi 34 | 35 | 36 | com.ofcoder.klein.common 37 | klein-common 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /klein-serializer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.ofcoder.klein 8 | klein 9 | 0.0.8 10 | 11 | 12 | klein-serializer 13 | 0.0.8 14 | com.ofcoder.klein.serializer 15 | 16 | pom 17 | 18 | klein-serializer-protobuf 19 | klein-serializer-hessian2 20 | klein-serializer-facade 21 | 22 | 23 | 24 | 8 25 | 8 26 | UTF-8 27 | 28 | 29 | 30 | 31 | com.ofcoder.klein.common 32 | klein-common 33 | 34 | 35 | com.ofcoder.klein.spi 36 | klein-spi 37 | 38 | 39 | -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-facade/src/main/java/com/ofcoder/klein/serializer/Serializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.serializer; 19 | 20 | import com.ofcoder.klein.spi.SPI; 21 | 22 | /** 23 | * Serializer. 24 | * 25 | */ 26 | @SPI 27 | public interface Serializer { 28 | /** 29 | * Encode object to byte[]. 30 | * 31 | * @param t the t 32 | * @return the byte [ ] 33 | */ 34 | byte[] serialize(Object t); 35 | 36 | /** 37 | * Decode t from byte[]. 38 | * 39 | * @param result type 40 | * @param bytes the bytes 41 | * @return the t 42 | */ 43 | R deserialize(byte[] bytes); 44 | } 45 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/nwr/Nwr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.nwr; 18 | 19 | import com.ofcoder.klein.spi.SPI; 20 | 21 | /** 22 | * NWR: N > W + R. 23 | * R is read quorum, W is write quorum. 24 | * 25 | * @author 释慧利 26 | */ 27 | @SPI 28 | public interface Nwr { 29 | /** 30 | * calculate read quorum. 31 | * 32 | * @param n total size 33 | * @return read quorum 34 | */ 35 | int r(int n); 36 | 37 | /** 38 | * calculate write quorum. 39 | * 40 | * @param n total size 41 | * @return write quorum 42 | */ 43 | int w(int n); 44 | } 45 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/util/timer/TimerTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.util.timer; 18 | 19 | /** 20 | * A task which is executed after the delay specified with 21 | * Timer#newTimeout(TimerTask, long, TimeUnit). 22 | * 23 | *

24 | * Forked from Netty. 25 | */ 26 | public interface TimerTask { 27 | 28 | /** 29 | * Executed after the delay specified with 30 | * Timer#newTimeout(TimerTask, long, TimeUnit). 31 | * 32 | * @param timeout a handle which is associated with this task 33 | * @throws Exception exception 34 | */ 35 | void run(Timeout timeout) throws Exception; 36 | } 37 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/BaseReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * Cache request base req. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class BaseReq implements Serializable { 27 | private String key; 28 | private String seq; 29 | 30 | public String getSeq() { 31 | return seq; 32 | } 33 | 34 | public void setSeq(final String seq) { 35 | this.seq = seq; 36 | } 37 | 38 | public String getKey() { 39 | return key; 40 | } 41 | 42 | public void setKey(final String key) { 43 | this.key = key; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /klein-spi/src/main/java/com/ofcoder/klein/spi/SpiExtensionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.spi; 19 | 20 | import java.util.Optional; 21 | 22 | /** 23 | * SpiExtensionFactory. 24 | */ 25 | @Join 26 | public class SpiExtensionFactory implements ExtensionFactory { 27 | 28 | @Override 29 | public T getExtension(final String key, final Class clazz) { 30 | return Optional.ofNullable(clazz) 31 | .filter(Class::isInterface) 32 | .filter(cls -> cls.isAnnotationPresent(SPI.class)) 33 | .map(ExtensionLoader::getExtensionLoader) 34 | .map(ExtensionLoader::getDefaultJoin) 35 | .orElse(null); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/core/PhaseCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.core; 18 | 19 | import com.ofcoder.klein.consensus.paxos.rpc.vo.NodeState; 20 | 21 | /** 22 | * Phase Callback. 23 | * 24 | * @author 释慧利 25 | */ 26 | public interface PhaseCallback { 27 | 28 | interface PreparePhaseCallback { 29 | void granted(long grantedProposalNo, ProposeContext context); 30 | 31 | void refused(ProposeContext context); 32 | } 33 | 34 | interface AcceptPhaseCallback { 35 | void granted(ProposeContext context); 36 | 37 | void learn(ProposeContext context, NodeState target); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein-consensus 7 | com.ofcoder.klein.consensus 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.consensus.facade 13 | klein-consensus-facade 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | com.ofcoder.klein.rpc.facade 25 | klein-rpc-facade 26 | 27 | 28 | com.ofcoder.klein.storage.api 29 | klein-storage-facade 30 | 31 | 32 | klein-serializer-facade 33 | com.ofcoder.klein.serializer.facade 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /klein-spi/src/main/java/com/ofcoder/klein/spi/Join.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.spi; 19 | 20 | import java.lang.annotation.Documented; 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * Join 28 | * Adding this annotation to a class indicates joining the extension mechanism. 29 | */ 30 | @Documented 31 | @Retention(RetentionPolicy.RUNTIME) 32 | @Target(ElementType.TYPE) 33 | public @interface Join { 34 | 35 | /** 36 | * It will be sorted according to the current serial number.. 37 | * @return int. 38 | */ 39 | int order() default 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein-consensus 7 | com.ofcoder.klein.consensus 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.consensus.paxos 13 | klein-consensus-paxos 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | com.ofcoder.klein.consensus.facade 25 | klein-consensus-facade 26 | 27 | 28 | com.ofcoder.klein.serializer.hessian2 29 | klein-serializer-hessian2 30 | test 31 | 32 | 33 | com.h2database 34 | h2 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/test/java/com/ofcoder/klein/storage/facade/trace/TracerTest.java: -------------------------------------------------------------------------------- 1 | //package com.ofcoder.klein.storage.facade.trace; 2 | // 3 | //import org.junit.Before; 4 | //import org.junit.Test; 5 | //import org.mockito.Mock; 6 | //import org.mockito.MockedStatic; 7 | // 8 | //import com.ofcoder.klein.spi.ExtensionLoader; 9 | //import com.ofcoder.klein.storage.facade.TraceManager; 10 | //import com.ofcoder.klein.storage.facade.config.StorageProp; 11 | // 12 | //import static org.mockito.Mockito.mockStatic; 13 | //import static org.mockito.Mockito.times; 14 | //import static org.mockito.Mockito.verify; 15 | //import static org.mockito.Mockito.when; 16 | // 17 | //public class TracerTest { 18 | // @Mock 19 | // TraceManager mockManager; 20 | // @Mock 21 | // ExtensionLoader traceManagerExtensionLoader; 22 | // 23 | // @Before 24 | // public void setUp() { 25 | // try (MockedStatic mocked = mockStatic(ExtensionLoader.class)) { 26 | // mocked.when(ExtensionLoader::getExtensionLoader).thenReturn(traceManagerExtensionLoader); 27 | // when(traceManagerExtensionLoader.getJoin("test")).thenReturn(mockManager); 28 | // } 29 | // } 30 | // 31 | // @Test 32 | // public void testTrace() { 33 | // final StorageProp mockProp = new StorageProp(); 34 | // 35 | // final Tracer tracer = new Tracer("test", mockProp); 36 | // 37 | // for (int i = 0; i < 100; i++) { 38 | // tracer.trace("data"); 39 | // } 40 | // 41 | // verify(mockManager, times(100)); 42 | // } 43 | //} 44 | -------------------------------------------------------------------------------- /klein-jepsen/docker/README.md: -------------------------------------------------------------------------------- 1 | # Dockerized Jepsen 2 | 3 | This docker image attempts to simplify the setup required by Jepsen. 4 | It is intended to be used by a CI tool or anyone with Docker who wants to try Jepsen themselves. 5 | 6 | It contains all the jepsen dependencies and code. It uses [Docker 7 | Compose](https://github.com/docker/compose) to spin up the five containers used 8 | by Jepsen. A script builds a `docker-compose.yml` file out of fragments in 9 | `template/`, because this is the future, and using `awk` to generate YAML to 10 | generate computers is *cloud native*. 11 | 12 | ## Quickstart 13 | 14 | Assuming you have docker-compose set up already, run: 15 | 16 | ``` 17 | bin/up 18 | bin/console 19 | ``` 20 | 21 | ... which will drop you into a console on the Jepsen control node. 22 | 23 | Your DB nodes are `n1`, `n2`, `n3`, `n4`, and `n5`. You can open as many shells 24 | as you like using `bin/console`. If your test includes a web server (try `lein 25 | run serve` on the control node, in your test directory), you can open it 26 | locally by running using `bin/web`. This can be a handy way to browse test 27 | results. 28 | 29 | ## Advanced 30 | 31 | You can change the number of DB nodes by running (e.g.) `bin/up -n 9`. 32 | 33 | If you need to log into a DB node (e.g. to debug a test), you can `ssh n1` (or n2, n3, ...) from inside the control node, or: 34 | 35 | ``` 36 | docker exec -it jepsen-n1 bash 37 | ``` 38 | 39 | During development, it's convenient to run with `--dev` option, which mounts `$JEPSEN_ROOT` dir as `/jepsen` on Jepsen control container. 40 | 41 | Run `./bin/up --help` for more info. 42 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/Holder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common; 18 | 19 | /** 20 | * Bean Holder. 21 | * 22 | * @author 释慧利 23 | */ 24 | public abstract class Holder { 25 | private volatile T t; 26 | private final Object lock = new Object(); 27 | 28 | /** 29 | * Create Bean. 30 | * 31 | * @return bean 32 | */ 33 | protected abstract T create(); 34 | 35 | /** 36 | * Get Bean. 37 | * 38 | * @return bean 39 | */ 40 | public T get() { 41 | if (t == null) { 42 | synchronized (lock) { 43 | if (t == null) { 44 | t = create(); 45 | } 46 | } 47 | } 48 | return t; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/sm/SM.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.sm; 18 | 19 | /** 20 | * State machine. 21 | * 22 | * @author 释慧利 23 | */ 24 | public interface SM { 25 | 26 | /** 27 | * apply instance. 28 | * 29 | * @param data proposal's data 30 | * @return apply result 31 | */ 32 | byte[] apply(byte[] data); 33 | 34 | /** 35 | * make a snapshot. 36 | * 37 | * @return snapshot 38 | */ 39 | byte[] makeImage(); 40 | 41 | /** 42 | * load snapshot. 43 | * 44 | * @param snap snapshot 45 | */ 46 | void loadImage(byte[] snap); 47 | 48 | /** 49 | * close sm. 50 | */ 51 | void close(); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/disruptor/DisruptorEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.disruptor; 18 | 19 | import java.util.concurrent.CountDownLatch; 20 | 21 | /** 22 | * Disruptor Event. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class DisruptorEvent { 27 | private CountDownLatch shutdownLatch; 28 | 29 | /** 30 | * Get ShutdownLatch. 31 | * 32 | * @return ShutdownLatch 33 | */ 34 | public CountDownLatch getShutdownLatch() { 35 | return shutdownLatch; 36 | } 37 | 38 | /** 39 | * Set ShutdownLatch. 40 | * 41 | * @param shutdownLatch ShutdownLatch 42 | */ 43 | public void setShutdownLatch(final CountDownLatch shutdownLatch) { 44 | this.shutdownLatch = shutdownLatch; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /klein-rpc/klein-rpc-facade/src/main/java/com/ofcoder/klein/rpc/facade/RpcProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.rpc.facade; 18 | 19 | /** 20 | * message processor. 21 | * 22 | * @author 释慧利 23 | */ 24 | public interface RpcProcessor { 25 | 26 | String KLEIN = "klein"; 27 | 28 | /** 29 | * get service name. 30 | * 31 | * @return service name 32 | */ 33 | String service(); 34 | 35 | /** 36 | * get method name. 37 | * 38 | * @return service name 39 | */ 40 | default String method() { 41 | return KLEIN; 42 | } 43 | 44 | /** 45 | * handle request. 46 | * 47 | * @param request request param 48 | * @param context rpc context 49 | */ 50 | void handleRequest(byte[] request, RpcContext context); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/PaxosNodeTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.paxos; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.ofcoder.klein.consensus.facade.nwr.Nwr; 5 | import com.ofcoder.klein.consensus.paxos.core.sm.MemberRegistry; 6 | import com.ofcoder.klein.rpc.facade.Endpoint; 7 | import com.ofcoder.klein.spi.ExtensionLoader; 8 | import junit.framework.TestCase; 9 | 10 | /** 11 | * @author far.liu 12 | */ 13 | public class PaxosNodeTest extends TestCase { 14 | 15 | public void testGenerateNextProposalNo() { 16 | 17 | ExtensionLoader.getExtensionLoader(Nwr.class).register("majority"); 18 | MemberRegistry.getInstance().init( 19 | null, 20 | Lists.newArrayList(new Endpoint("1", "127.0.0.1", 1218, false), new Endpoint("2", "127.0.0.1", 1219, false), new Endpoint("3", "127.0.0.1", 1220, false)) 21 | ); 22 | PaxosNode node = PaxosNode.Builder.aPaxosNode() 23 | .curProposalNo(0) 24 | .self(new Endpoint("3", "127.0.0.1", 1220, false)) 25 | .build(); 26 | 27 | long pno = node.generateNextProposalNo(); 28 | assertEquals(ProposalNoUtil.getCounterFromPno(pno), 3); 29 | assertEquals(ProposalNoUtil.getEpochFromPno(pno), MemberRegistry.getInstance().getMemberConfiguration().getVersion()); 30 | 31 | pno = node.generateNextProposalNo(); 32 | assertEquals(ProposalNoUtil.getCounterFromPno(pno), 6); 33 | assertEquals(ProposalNoUtil.getEpochFromPno(pno), MemberRegistry.getInstance().getMemberConfiguration().getVersion()); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /klein-core/src/main/java/com/ofcoder/klein/core/lock/KleinLock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.core.lock; 18 | 19 | import java.util.concurrent.TimeUnit; 20 | 21 | /** 22 | * lock. 23 | * 24 | * @author 释慧利 25 | */ 26 | public interface KleinLock { 27 | /** 28 | * Acquires the lock if it is free. 29 | * 30 | * @param ttl ttl 31 | * @param unit the time unit of the {@code ttl} argument 32 | * @return {@code true} if the lock was acquired else the {@code false} 33 | */ 34 | boolean acquire(long ttl, TimeUnit unit); 35 | 36 | /** 37 | * Acquires the lock if it is free. 38 | * @return {@code true} if the lock was acquired else the {@code false} 39 | */ 40 | boolean acquire(); 41 | 42 | /** 43 | * Releases the lock. 44 | */ 45 | void release(); 46 | } 47 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/NoopCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | /** 20 | * noop command. 21 | * A no-operation command implementation that represents a null operation in the consensus system. 22 | * This command is typically used as a placeholder in the consensus protocol when there are no actual 23 | * commands to process, helping maintain the consensus mechanism's continuity. 24 | */ 25 | public enum NoopCommand implements Command { 26 | NOOP; 27 | 28 | @Override 29 | public String getGroup() { 30 | return "NOOP"; 31 | } 32 | 33 | @Override 34 | public byte[] getData() { 35 | return new byte[0]; 36 | } 37 | 38 | @Override 39 | public boolean getIfSystemOp() { 40 | return true; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /klein-core/src/main/java/com/ofcoder/klein/core/cache/CacheSnap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.core.cache; 18 | 19 | import java.io.Serializable; 20 | import java.util.Map; 21 | import java.util.Set; 22 | 23 | /** 24 | * Cache Snap Model. 25 | * 26 | * @author 释慧利 27 | */ 28 | public class CacheSnap implements Serializable { 29 | private Object cache; 30 | private Map> expiryBuckets; 31 | 32 | public CacheSnap(final Object cache, final Map> expiryBuckets) { 33 | this.cache = cache; 34 | this.expiryBuckets = expiryBuckets; 35 | } 36 | 37 | public CacheSnap() { 38 | } 39 | 40 | public Object getCache() { 41 | return cache; 42 | } 43 | 44 | public Map> getExpiryBuckets() { 45 | return expiryBuckets; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/util/StreamUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.util; 18 | 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | /** 26 | * Stream Util. 27 | * @author far.liu 28 | */ 29 | public class StreamUtil { 30 | 31 | private static final Logger LOG = LoggerFactory.getLogger(StreamUtil.class); 32 | 33 | /** 34 | * Close Closeable. 35 | * 36 | * @param closeable need close object 37 | */ 38 | public static void close(final Closeable closeable) { 39 | if (closeable != null) { 40 | try { 41 | closeable.close(); 42 | } catch (IOException e) { 43 | LOG.warn(e.getMessage(), e); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/quorum/Quorum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade.quorum; 18 | 19 | import com.ofcoder.klein.rpc.facade.Endpoint; 20 | 21 | /** 22 | * Quorum check. 23 | * 24 | * @author 释慧利 25 | */ 26 | public interface Quorum { 27 | /** 28 | * the node refuse, refuse current request. 29 | * 30 | * @param node refuse node 31 | * @return refuse result 32 | */ 33 | boolean refuse(Endpoint node); 34 | 35 | /** 36 | * the node pass, grant current request. 37 | * 38 | * @param node pass node 39 | * @return grant result 40 | */ 41 | boolean grant(Endpoint node); 42 | 43 | GrantResult isGranted(); 44 | 45 | enum GrantResult { 46 | PASS, 47 | REFUSE, 48 | GRANTING 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /klein-core/src/main/java/com/ofcoder/klein/core/cache/MetaData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.core.cache; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * MetaData. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class MetaData implements Serializable { 27 | private long expire = -1; 28 | private Object data; 29 | 30 | public MetaData() { 31 | } 32 | 33 | public MetaData(final long expire, final Object data) { 34 | this.expire = expire; 35 | this.data = data; 36 | } 37 | 38 | public long getExpire() { 39 | return expire; 40 | } 41 | 42 | public void setExpire(final long expire) { 43 | this.expire = expire; 44 | } 45 | 46 | public Object getData() { 47 | return data; 48 | } 49 | 50 | public void setData(final Object data) { 51 | this.data = data; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/ProposeProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos; 18 | 19 | import com.ofcoder.klein.consensus.facade.Result; 20 | 21 | /** 22 | * Proxy is used for forwarding write requests. 23 | * 24 | * @author 释慧利 25 | */ 26 | public interface ProposeProxy { 27 | /** 28 | * propose proposal. 29 | * 30 | * @param data proposal 31 | * @param apply Whether you need to wait until the state machine is applied 32 | * If true, wait until the state machine is applied before returning 33 | * @return propose result 34 | */ 35 | Result propose(Proposal data, boolean apply); 36 | 37 | /** 38 | * todo: read index. 39 | * 40 | * @param group group name 41 | * @return instance id 42 | */ 43 | Long readIndex(String group); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /klein-spi/src/main/java/com/ofcoder/klein/spi/SPI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package com.ofcoder.klein.spi; 19 | 20 | import java.lang.annotation.Documented; 21 | import java.lang.annotation.ElementType; 22 | import java.lang.annotation.Retention; 23 | import java.lang.annotation.RetentionPolicy; 24 | import java.lang.annotation.Target; 25 | 26 | /** 27 | * SPI Extend the processing. 28 | * All spi system reference the apache implementation of 29 | * Apache Dubbo Common Extension. 30 | * 31 | * @see ExtensionFactory 32 | * @see ExtensionLoader 33 | */ 34 | @Documented 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.TYPE) 37 | public @interface SPI { 38 | 39 | /** 40 | * Value string. 41 | * 42 | * @return the string 43 | */ 44 | String value() default ""; 45 | } 46 | -------------------------------------------------------------------------------- /klein-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.example 13 | klein-example 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 2.16.1 22 | 2.16.1 23 | 24 | 25 | 26 | 27 | com.ofcoder.klein.core 28 | klein-core 29 | 30 | 31 | com.fasterxml.jackson.dataformat 32 | jackson-dataformat-yaml 33 | ${jackson.dataformat.version} 34 | 35 | 36 | com.fasterxml.jackson.core 37 | jackson-databind 38 | ${jackson.databind.version} 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/OnlyForTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | import javax.annotation.concurrent.NotThreadSafe; 25 | 26 | /** 27 | * The type/method/field etc. to which this annotation is applied is only for unit test. 28 | * It means that user should not use them in business code except test code. 29 | * 30 | * @see NotThreadSafe 31 | */ 32 | @Documented 33 | @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR}) 34 | @Retention(RetentionPolicy.CLASS) 35 | public @interface OnlyForTest { 36 | /** 37 | * test description. 38 | * 39 | * @return desc 40 | */ 41 | String value() default ""; 42 | } 43 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/ExistsProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | import com.ofcoder.klein.consensus.facade.AbstractRpcProcessor; 20 | import com.ofcoder.klein.core.cache.KleinCache; 21 | import com.ofcoder.klein.rpc.facade.RpcContext; 22 | 23 | /** 24 | * cache exists request processor. 25 | * 26 | * @author 释慧利 27 | */ 28 | public class ExistsProcessor extends AbstractRpcProcessor { 29 | private KleinCache cache; 30 | 31 | public ExistsProcessor(final KleinCache cache) { 32 | this.cache = cache; 33 | } 34 | 35 | @Override 36 | public void handleRequest(final ExistsReq request, final RpcContext rpcContext) { 37 | boolean exist = cache.exist(request.getKey()); 38 | response(exist, rpcContext); 39 | } 40 | 41 | @Override 42 | public String service() { 43 | return ExistsReq.class.getSimpleName(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/PutReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | import java.io.Serializable; 20 | import java.util.concurrent.TimeUnit; 21 | 22 | /** 23 | * cache put request. 24 | * 25 | * @author 释慧利 26 | */ 27 | public class PutReq extends BaseReq { 28 | private Serializable data; 29 | private long ttl = -1; 30 | private TimeUnit unit = TimeUnit.SECONDS; 31 | 32 | public Serializable getData() { 33 | return data; 34 | } 35 | 36 | public void setData(final Serializable data) { 37 | this.data = data; 38 | } 39 | 40 | public long getTtl() { 41 | return ttl; 42 | } 43 | 44 | public void setTtl(final long ttl) { 45 | this.ttl = ttl; 46 | } 47 | 48 | public TimeUnit getUnit() { 49 | return unit; 50 | } 51 | 52 | public void setUnit(final TimeUnit unit) { 53 | this.unit = unit; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /klein-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | klein 7 | com.ofcoder.klein 8 | 0.0.8 9 | 10 | 4.0.0 11 | 12 | com.ofcoder.klein.core 13 | klein-core 14 | 0.0.8 15 | jar 16 | 17 | 18 | 8 19 | 8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | com.ofcoder.klein.consensus.paxos 26 | klein-consensus-paxos 27 | 28 | 29 | com.ofcoder.klein.rpc.grpc 30 | klein-rpc-grpc 31 | 32 | 33 | com.ofcoder.klein.storage.file 34 | klein-storage-file 35 | 36 | 37 | com.ofcoder.klein.serializer.hessian2 38 | klein-serializer-hessian2 39 | 40 | 41 | org.mapdb 42 | mapdb 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /klein-jepsen/docker/node/Dockerfile: -------------------------------------------------------------------------------- 1 | # See https://github.com/jgoerzen/docker-debian-base 2 | # See https://hub.docker.com/r/jgoerzen/debian-base-standard 3 | FROM ubuntu:latest 4 | 5 | # I think this is a bug--debian-base-setup crashes because policy-rc.d isn't 6 | # present in this image, and if you create it, exim crashes... do we actually NEED this? Maybe not... 7 | #RUN mkdir /usr/sbin/policy-rc.d 8 | #RUN run-parts --exit-on-error --verbose /usr/local/debian-base-setup 9 | 10 | # Basic system stuff 11 | RUN apt-get update 12 | RUN apt-get install -y apt-transport-https 13 | 14 | # Install packages 15 | RUN apt-get -qy update && \ 16 | apt-get -qy install \ 17 | dos2unix \ 18 | openssh-server \ 19 | less \ 20 | pwgen 21 | 22 | # When run, boot-debian-base will call this script, which does final 23 | # per-db-node setup stuff. 24 | ADD setup-jepsen.sh /usr/local/preinit/03-setup-jepsen 25 | RUN chmod +x /usr/local/preinit/03-setup-jepsen 26 | 27 | # Configure SSHD 28 | RUN sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config 29 | 30 | # Enable SSH server 31 | ENV DEBBASE_SSH enabled 32 | 33 | RUN mkdir -p /klein/config 34 | 35 | # Install Jepsen deps 36 | RUN apt-get -y -q update && apt-get install -qy openjdk-8-jdk 37 | RUN apt-get -y -q update && apt-get install -qy build-essential bzip2 curl dnsutils faketime iproute2 iptables iputils-ping libzip4 logrotate man man-db net-tools ntpdate psmisc python3 rsyslog sudo tar unzip vim wget ca-certificates 38 | # && apt-get update && apt-get install -yq software-properties-common && apt-get update && add-apt-repository ppa:openjdk-r/ppa && apt-get -y -q update && apt-get install -qy openjdk-8-jdk 39 | 40 | RUN echo "root:123456" | chpasswd 41 | 42 | EXPOSE 22 43 | EXPOSE 1218 44 | CMD ["/usr/local/preinit/03-setup-jepsen"] 45 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/InvalidateProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | import com.ofcoder.klein.consensus.facade.AbstractRpcProcessor; 20 | import com.ofcoder.klein.core.cache.KleinCache; 21 | import com.ofcoder.klein.rpc.facade.RpcContext; 22 | 23 | /** 24 | * cache invalidate request processor. 25 | * 26 | * @author 释慧利 27 | */ 28 | public class InvalidateProcessor extends AbstractRpcProcessor { 29 | 30 | private KleinCache cache; 31 | 32 | public InvalidateProcessor(final KleinCache cache) { 33 | this.cache = cache; 34 | } 35 | 36 | @Override 37 | public void handleRequest(final InvalidateReq request, final RpcContext rpcContext) { 38 | cache.invalidate(request.getKey()); 39 | response(true, rpcContext); 40 | } 41 | 42 | @Override 43 | public String service() { 44 | return InvalidateReq.class.getSimpleName(); 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-jepsen/klein-jepsen-server/src/main/java/com/ofcoder/klein/jepsen/server/rpc/Resp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.jepsen.server.rpc; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * GetResp. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class Resp implements Serializable { 27 | private boolean s; 28 | private Object v; 29 | 30 | public Resp() { 31 | } 32 | 33 | public Resp(final boolean s, final Object v) { 34 | this.s = s; 35 | this.v = v; 36 | } 37 | 38 | public boolean isS() { 39 | return s; 40 | } 41 | 42 | public void setS(final boolean s) { 43 | this.s = s; 44 | } 45 | 46 | public Object getV() { 47 | return v; 48 | } 49 | 50 | public void setV(final Object v) { 51 | this.v = v; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Resp{" 57 | + "s=" + s 58 | + ", v=" + v 59 | + '}'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/AbstractInvokeCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | import com.ofcoder.klein.rpc.facade.InvokeCallback; 20 | import com.ofcoder.klein.serializer.Serializer; 21 | import com.ofcoder.klein.spi.ExtensionLoader; 22 | 23 | /** 24 | * Invoke callback. 25 | * 26 | * @author far.liu 27 | */ 28 | public abstract class AbstractInvokeCallback implements InvokeCallback { 29 | private final Serializer serializer; 30 | 31 | public AbstractInvokeCallback() { 32 | serializer = ExtensionLoader.getExtensionLoader(Serializer.class).register("hessian2"); 33 | } 34 | 35 | /** 36 | * invoke completed. 37 | * 38 | * @param result invoke result 39 | */ 40 | public abstract void complete(RES result); 41 | 42 | @Override 43 | public void complete(final byte[] result) { 44 | RES deserialize = serializer.deserialize(result); 45 | complete(deserialize); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-core/src/main/java/com/ofcoder/klein/core/lock/LockMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.core.lock; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * Message. 23 | * 24 | * @author far.liu 25 | */ 26 | public class LockMessage implements Serializable { 27 | 28 | public static final byte LOCK = 0x01; 29 | public static final byte UNLOCK = 0x02; 30 | public static final long TTL_PERPETUITY = -1; 31 | 32 | private byte op; 33 | private String key; 34 | private long expire = TTL_PERPETUITY; 35 | 36 | public byte getOp() { 37 | return op; 38 | } 39 | 40 | public void setOp(final byte op) { 41 | this.op = op; 42 | } 43 | 44 | public String getKey() { 45 | return key; 46 | } 47 | 48 | public void setKey(final String key) { 49 | this.key = key; 50 | } 51 | 52 | public long getExpire() { 53 | return expire; 54 | } 55 | 56 | public void setExpire(final long expire) { 57 | this.expire = expire; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/core/Proposer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.core; 18 | 19 | import com.ofcoder.klein.common.Role; 20 | import com.ofcoder.klein.consensus.facade.Command; 21 | import com.ofcoder.klein.consensus.facade.config.ConsensusProp; 22 | 23 | /** 24 | * Proposer Role. 25 | * 26 | * @author 释慧利 27 | */ 28 | public interface Proposer extends Role { 29 | /** 30 | * Propose proposal. 31 | * 32 | * @param data client's data 33 | * @param done client's callback 34 | * @param now execute right now 35 | */ 36 | void propose(Command data, ProposeDone done, boolean now); 37 | 38 | /** 39 | * Try to boost instance right now. 40 | * Boost the copy of the proposal to the majority and the confirm status is reached 41 | * 42 | * @param instanceId id of the instance that you want to boost 43 | * @param done boost callback 44 | */ 45 | void tryBoost(Long instanceId, ProposeDone done); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/Cluster.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | import java.util.List; 20 | 21 | import com.ofcoder.klein.rpc.facade.Endpoint; 22 | 23 | /** 24 | * Cluster info. 25 | * 26 | * @author 释慧利 27 | */ 28 | public interface Cluster { 29 | 30 | /** 31 | * Get member list. 32 | * 33 | * @return member list. 34 | */ 35 | MemberConfiguration getMemberConfig(); 36 | 37 | /** 38 | * Change member list by Join-Consensus. 39 | * e.g. old version = 0, new version = 1 40 | * 1. Accept phase → send new config to old quorum, enter Join-Consensus 41 | * 2. Copy instance(version = 0, confirmed) to new quorum. (TODO check implementation) 42 | * 3. Confirm phase → new config take effect 43 | * 44 | * @param add endpoint to the cluster. 45 | * @param remove endpoint to the cluster. 46 | * @return true if success. 47 | */ 48 | boolean changeMember(List add, List remove); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/core/ProposalWithDone.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.core; 18 | 19 | import com.ofcoder.klein.common.disruptor.DisruptorEvent; 20 | import com.ofcoder.klein.consensus.facade.Command; 21 | 22 | /** 23 | * Proposal and Propose Callback. 24 | * 25 | * @author 释慧利 26 | */ 27 | public class ProposalWithDone extends DisruptorEvent { 28 | private Command proposal; 29 | private ProposeDone done; 30 | 31 | public ProposalWithDone() { 32 | } 33 | 34 | public ProposalWithDone(final Command proposal, final ProposeDone done) { 35 | this.proposal = proposal; 36 | this.done = done; 37 | } 38 | 39 | public Command getProposal() { 40 | return proposal; 41 | } 42 | 43 | public void setProposal(final Command proposal) { 44 | this.proposal = proposal; 45 | } 46 | 47 | public ProposeDone getDone() { 48 | return done; 49 | } 50 | 51 | public void setDone(final ProposeDone done) { 52 | this.done = done; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /klein-serializer/klein-serializer-hessian2/src/main/java/com/ofcoder/klein/serializer/hessian2/Hessian2Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.serializer.hessian2; 18 | 19 | import com.ofcoder.klein.serializer.Serializer; 20 | 21 | /** 22 | * Hessian util. 23 | * 24 | * @author far.liu 25 | */ 26 | public class Hessian2Util { 27 | 28 | private static final Serializer HESSIAN2 = new Hessian2Serializer(); 29 | 30 | /** 31 | * serialize object. 32 | * 33 | * @param javaBean object 34 | * @param java object type 35 | * @return serialized data 36 | */ 37 | public static byte[] serialize(final T javaBean) { 38 | return HESSIAN2.serialize(javaBean); 39 | } 40 | 41 | /** 42 | * deserialize data to java object. 43 | * 44 | * @param serializeData serialized data 45 | * @param java object type 46 | * @return java object 47 | */ 48 | @SuppressWarnings("unchecked") 49 | public static T deserialize(final byte[] serializeData) { 50 | return HESSIAN2.deserialize(serializeData); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/vo/BaseReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc.vo; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * Base request data. 23 | * @author 释慧利 24 | */ 25 | public class BaseReq implements Serializable { 26 | private String nodeId; 27 | private long proposalNo; 28 | private int memberConfigurationVersion; 29 | 30 | public String getNodeId() { 31 | return nodeId; 32 | } 33 | 34 | public void setNodeId(final String nodeId) { 35 | this.nodeId = nodeId; 36 | } 37 | 38 | public long getProposalNo() { 39 | return proposalNo; 40 | } 41 | 42 | public void setProposalNo(final long proposalNo) { 43 | this.proposalNo = proposalNo; 44 | } 45 | 46 | public int getMemberConfigurationVersion() { 47 | return memberConfigurationVersion; 48 | } 49 | 50 | public void setMemberConfigurationVersion(final int memberConfigurationVersion) { 51 | this.memberConfigurationVersion = memberConfigurationVersion; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/test/java/com/ofcoder/klein/consensus/paxos/PropertyChangeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos; 18 | 19 | import java.beans.PropertyChangeEvent; 20 | import java.beans.PropertyChangeListener; 21 | import java.beans.PropertyChangeSupport; 22 | import java.io.IOException; 23 | 24 | import org.junit.Test; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | /** 29 | * @author 释慧利 30 | */ 31 | public class PropertyChangeTest { 32 | private static final Logger LOG = LoggerFactory.getLogger(PropertyChangeTest.class); 33 | private Boolean test = false; 34 | 35 | @Test 36 | public void testMonitoringVariables() throws Exception { 37 | PropertyChangeSupport changes = new PropertyChangeSupport(test); 38 | changes.addPropertyChangeListener(new PropertyChangeListener() { 39 | @Override 40 | public void propertyChange(PropertyChangeEvent evt) { 41 | LOG.info("propertyChange"); 42 | } 43 | }); 44 | test = true; 45 | Thread.sleep(200L); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/test/java/com/ofcoder/klein/consensus/facade/quorum/SingleQuorumTest.java: -------------------------------------------------------------------------------- 1 | package com.ofcoder.klein.consensus.facade.quorum; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import com.ofcoder.klein.rpc.facade.Endpoint; 7 | import com.ofcoder.klein.rpc.facade.util.RpcUtil; 8 | import junit.framework.TestCase; 9 | 10 | public class SingleQuorumTest extends TestCase { 11 | 12 | public void testIsGranted() { 13 | Set members = new HashSet<>(); 14 | members.add(RpcUtil.parseEndpoint("1:127.0.0.1:1218:false")); 15 | members.add(RpcUtil.parseEndpoint("2:127.0.0.1:1219:false")); 16 | members.add(RpcUtil.parseEndpoint("3:127.0.0.1:1220:false")); 17 | 18 | SingleQuorum passQuorum = new SingleQuorum(members, 2); 19 | passQuorum.grant(RpcUtil.parseEndpoint("1:127.0.0.1:1218:false")); 20 | assertEquals(passQuorum.isGranted(), Quorum.GrantResult.GRANTING); 21 | 22 | passQuorum.grant(RpcUtil.parseEndpoint("4:127.0.0.1:1221:false")); 23 | assertEquals(passQuorum.isGranted(), Quorum.GrantResult.GRANTING); 24 | 25 | passQuorum.grant(RpcUtil.parseEndpoint("2:127.0.0.1:1219:false")); 26 | assertEquals(passQuorum.isGranted(), Quorum.GrantResult.PASS); 27 | 28 | SingleQuorum refuseQuorum = new SingleQuorum(members, 2); 29 | refuseQuorum.refuse(RpcUtil.parseEndpoint("1:127.0.0.1:1218:false")); 30 | assertEquals(refuseQuorum.isGranted(), Quorum.GrantResult.GRANTING); 31 | 32 | refuseQuorum.refuse(RpcUtil.parseEndpoint("1:127.0.0.1:1218:false")); 33 | assertEquals(refuseQuorum.isGranted(), Quorum.GrantResult.GRANTING); 34 | 35 | refuseQuorum.refuse(RpcUtil.parseEndpoint("2:127.0.0.1:1219:false")); 36 | assertEquals(refuseQuorum.isGranted(), Quorum.GrantResult.REFUSE); 37 | 38 | } 39 | 40 | public void testGrant() { 41 | } 42 | 43 | public void testRefuse() { 44 | } 45 | } -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/ProposalNoUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos; 18 | 19 | /** 20 | * generate proposal no by config.version and local.counter. 21 | * 22 | * @author 释慧利 23 | */ 24 | public class ProposalNoUtil { 25 | 26 | public static long getEpochFromPno(final long pno) { 27 | return pno >> 32L; 28 | } 29 | 30 | public static long getCounterFromPno(final long pno) { 31 | return pno & 0xffffffffL; 32 | } 33 | 34 | /** 35 | * generate proposal no by config.version and local.counter. 36 | * 37 | * @param epoch config.version 38 | * @param counter local.counter 39 | * @return proposal no 40 | */ 41 | public static long makePno(final long epoch, final long counter) { 42 | return (epoch << 32L) | (counter & 0xffffffffL); 43 | } 44 | 45 | /** 46 | * pno convert to Hex. 47 | * 48 | * @param pno pno 49 | * @return hex string 50 | */ 51 | public static String pnoToString(final long pno) { 52 | return Long.toHexString(pno); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/util/TrueTime.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.util; 18 | 19 | /** 20 | * TrueTime is used to solve time skew. 21 | * 22 | * @author 释慧利 23 | */ 24 | public final class TrueTime { 25 | private static final long HEARTBEAT_OFFSET = 0; 26 | private static long trueTimestamp = System.currentTimeMillis(); 27 | private static long localTimestamp = trueTimestamp; 28 | 29 | /** 30 | * update time. 31 | * 32 | * @param trueTimestamp target time 33 | */ 34 | public static void heartbeat(final long trueTimestamp) { 35 | long local = System.currentTimeMillis(); 36 | if (trueTimestamp <= local) { 37 | return; 38 | } 39 | TrueTime.trueTimestamp = trueTimestamp + HEARTBEAT_OFFSET; 40 | TrueTime.localTimestamp = local; 41 | } 42 | 43 | /** 44 | * get current time. 45 | * 46 | * @return current time 47 | */ 48 | public static long currentTimeMillis() { 49 | long local = System.currentTimeMillis(); 50 | long diff = local - TrueTime.localTimestamp; 51 | return trueTimestamp + diff; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/core/Acceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.core; 18 | 19 | import com.ofcoder.klein.common.Role; 20 | import com.ofcoder.klein.consensus.facade.config.ConsensusProp; 21 | import com.ofcoder.klein.consensus.paxos.rpc.vo.AcceptReq; 22 | import com.ofcoder.klein.consensus.paxos.rpc.vo.AcceptRes; 23 | import com.ofcoder.klein.consensus.paxos.rpc.vo.PrepareReq; 24 | import com.ofcoder.klein.consensus.paxos.rpc.vo.PrepareRes; 25 | 26 | /** 27 | * Acceptor Role. 28 | * 29 | * @author 释慧利 30 | */ 31 | public interface Acceptor extends Role { 32 | /** 33 | * Process the Accept message from Proposer. 34 | * 35 | * @param req request content 36 | * @param isSelf from self 37 | * @return response 38 | */ 39 | AcceptRes handleAcceptRequest(AcceptReq req, boolean isSelf); 40 | 41 | /** 42 | * Process the Prepare message from Proposer. 43 | * 44 | * @param req request content 45 | * @param isSelf from self 46 | * @return response 47 | */ 48 | PrepareRes handlePrepareRequest(PrepareReq req, boolean isSelf); 49 | } 50 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/HeartbeatProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc; 18 | 19 | import com.ofcoder.klein.consensus.facade.AbstractRpcProcessor; 20 | import com.ofcoder.klein.consensus.paxos.PaxosNode; 21 | import com.ofcoder.klein.consensus.paxos.core.RuntimeAccessor; 22 | import com.ofcoder.klein.consensus.paxos.rpc.vo.Ping; 23 | import com.ofcoder.klein.consensus.paxos.rpc.vo.Pong; 24 | import com.ofcoder.klein.rpc.facade.RpcContext; 25 | 26 | /** 27 | * Heartbeat Processor. 28 | * 29 | * @author 释慧利 30 | */ 31 | public class HeartbeatProcessor extends AbstractRpcProcessor { 32 | private PaxosNode self; 33 | 34 | public HeartbeatProcessor(final PaxosNode self) { 35 | this.self = self; 36 | } 37 | 38 | @Override 39 | public void handleRequest(final Ping request, final RpcContext context) { 40 | if (RuntimeAccessor.getMaster().onReceiveHeartbeat(request, false)) { 41 | response(new Pong(), context); 42 | } 43 | } 44 | 45 | @Override 46 | public String service() { 47 | return Ping.class.getSimpleName(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/SnapSyncProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc; 18 | 19 | import com.ofcoder.klein.consensus.facade.AbstractRpcProcessor; 20 | import com.ofcoder.klein.consensus.paxos.PaxosNode; 21 | import com.ofcoder.klein.consensus.paxos.core.RuntimeAccessor; 22 | import com.ofcoder.klein.consensus.paxos.rpc.vo.SnapSyncReq; 23 | import com.ofcoder.klein.consensus.paxos.rpc.vo.SnapSyncRes; 24 | import com.ofcoder.klein.rpc.facade.RpcContext; 25 | 26 | /** 27 | * SnapSync Request Processor. 28 | * 29 | * @author 释慧利 30 | */ 31 | public class SnapSyncProcessor extends AbstractRpcProcessor { 32 | 33 | public SnapSyncProcessor(final PaxosNode self) { 34 | // do nothing. 35 | } 36 | 37 | @Override 38 | public void handleRequest(final SnapSyncReq request, final RpcContext context) { 39 | SnapSyncRes snapSyncRes = RuntimeAccessor.getLearner().handleSnapSyncRequest(request); 40 | response(snapSyncRes, context); 41 | } 42 | 43 | @Override 44 | public String service() { 45 | return SnapSyncReq.class.getSimpleName(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-common/src/main/java/com/ofcoder/klein/common/util/ChecksumUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.common.util; 18 | 19 | import java.security.MessageDigest; 20 | 21 | /** 22 | * SumcheckUtil. 23 | * 24 | * @author 释慧利 25 | */ 26 | public class ChecksumUtil { 27 | 28 | /** 29 | * md5. 30 | * 31 | * @param btInput resource 32 | * @return md5 string 33 | */ 34 | public static String md5(final byte[] btInput) { 35 | 36 | char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 37 | try { 38 | MessageDigest mdInst = MessageDigest.getInstance("MD5"); 39 | mdInst.update(btInput); 40 | byte[] md = mdInst.digest(); 41 | int j = md.length; 42 | char[] str = new char[j * 2]; 43 | int k = 0; 44 | for (int i = 0; i < j; i++) { 45 | byte byte0 = md[i]; 46 | str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 47 | str[k++] = hexDigits[byte0 & 0xf]; 48 | } 49 | return new String(str); 50 | } catch (Exception e) { 51 | return null; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-facade/src/main/java/com/ofcoder/klein/storage/facade/StorageEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.storage.facade; 18 | 19 | import com.ofcoder.klein.spi.ExtensionLoader; 20 | import com.ofcoder.klein.storage.facade.config.StorageProp; 21 | 22 | /** 23 | * Storage Engine. 24 | * 25 | * @author far.liu 26 | */ 27 | public final class StorageEngine { 28 | private static LogManager logManager; 29 | private static TraceManager traceManager; 30 | 31 | /** 32 | * start up. 33 | * 34 | * @param type storage type 35 | * @param prop property 36 | */ 37 | public static void startup(final String type, final StorageProp prop) { 38 | logManager = ExtensionLoader.getExtensionLoader(LogManager.class).register(type, prop); 39 | traceManager = ExtensionLoader.getExtensionLoader(TraceManager.class).register(type, prop); 40 | } 41 | 42 | /** 43 | * shutdown. 44 | */ 45 | public static void shutdown() { 46 | if (logManager != null) { 47 | logManager.shutdown(); 48 | } 49 | if (traceManager != null) { 50 | traceManager.shutdown(); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-paxos/src/main/java/com/ofcoder/klein/consensus/paxos/rpc/NewMasterProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.paxos.rpc; 18 | 19 | import com.ofcoder.klein.consensus.facade.AbstractRpcProcessor; 20 | import com.ofcoder.klein.consensus.paxos.PaxosNode; 21 | import com.ofcoder.klein.consensus.paxos.core.RuntimeAccessor; 22 | import com.ofcoder.klein.consensus.paxos.rpc.vo.NewMasterReq; 23 | import com.ofcoder.klein.consensus.paxos.rpc.vo.NewMasterRes; 24 | import com.ofcoder.klein.rpc.facade.RpcContext; 25 | 26 | /** 27 | * NewMaster Request Processor. 28 | * 29 | * @author 释慧利 30 | */ 31 | public class NewMasterProcessor extends AbstractRpcProcessor { 32 | 33 | public NewMasterProcessor(final PaxosNode self) { 34 | // do nothing. 35 | } 36 | 37 | @Override 38 | public void handleRequest(final NewMasterReq request, final RpcContext context) { 39 | NewMasterRes newMasterRes = RuntimeAccessor.getMaster().onReceiveNewMaster(request, false); 40 | response(newMasterRes, context); 41 | } 42 | 43 | @Override 44 | public String service() { 45 | return NewMasterReq.class.getSimpleName(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /klein-storage/klein-storage-h2/src/main/java/com/ofcoder/klein/storage/h2/ConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.storage.h2; 18 | 19 | import java.sql.Connection; 20 | import java.sql.SQLException; 21 | 22 | import org.h2.jdbcx.JdbcConnectionPool; 23 | 24 | /** 25 | * ConnectionPool. 26 | * 27 | * @author 释慧利 28 | */ 29 | public final class ConnectionPool { 30 | private static ConnectionPool cp; 31 | private JdbcConnectionPool jdbcCP; 32 | 33 | private ConnectionPool() { 34 | String dbPath = "./config/test"; 35 | jdbcCP = JdbcConnectionPool.create("jdbc:h2:" + dbPath, "sa", ""); 36 | jdbcCP.setMaxConnections(50); 37 | } 38 | 39 | /** 40 | * get connection pool object. 41 | * 42 | * @return connection pool 43 | */ 44 | public static ConnectionPool getInstance() { 45 | if (cp == null) { 46 | cp = new ConnectionPool(); 47 | } 48 | return cp; 49 | } 50 | 51 | /** 52 | * get connection. 53 | * 54 | * @return connection 55 | * @throws SQLException sql 56 | */ 57 | public Connection getConnection() throws SQLException { 58 | return jdbcCP.getConnection(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /klein-consensus/klein-consensus-facade/src/main/java/com/ofcoder/klein/consensus/facade/ConsensusEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.consensus.facade; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import com.ofcoder.klein.consensus.facade.config.ConsensusProp; 23 | import com.ofcoder.klein.spi.ExtensionLoader; 24 | 25 | /** 26 | * Consensus Engine. 27 | * 28 | * @author far.liu 29 | */ 30 | public final class ConsensusEngine { 31 | private static final Logger LOG = LoggerFactory.getLogger(ConsensusEngine.class); 32 | private static Consensus consensus; 33 | 34 | /** 35 | * engine start up. 36 | * 37 | * @param algorithm consensus type 38 | * @param prop property 39 | */ 40 | public static void startup(final String algorithm, final ConsensusProp prop) { 41 | LOG.info("start consensus engine"); 42 | consensus = ExtensionLoader.getExtensionLoader(Consensus.class).register(algorithm, prop); 43 | consensus.preheating(); 44 | } 45 | 46 | /** 47 | * shutdown. 48 | */ 49 | public static void shutdown() { 50 | if (consensus != null) { 51 | consensus.shutdown(); 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/nwr/Main2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.example.nwr; 18 | 19 | import java.io.IOException; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import com.ofcoder.klein.Klein; 25 | import com.ofcoder.klein.KleinProp; 26 | import com.ofcoder.klein.core.cache.KleinCache; 27 | import com.ofcoder.klein.core.cache.KleinCacheImpl; 28 | 29 | /** 30 | * Main2: cluster member. 31 | * 32 | * @author 释慧利 33 | */ 34 | public class Main2 { 35 | private static final Logger LOG = LoggerFactory.getLogger(Main2.class); 36 | 37 | public static void main(final String[] args) throws IOException { 38 | System.setProperty("klein.id", "2"); 39 | System.setProperty("klein.port", "1219"); 40 | System.setProperty("klein.ip", "127.0.0.1"); 41 | System.setProperty("klein.members", "1:127.0.0.1:1218:false;2:127.0.0.1:1219:false;3:127.0.0.1:1220:false"); 42 | System.setProperty("klein.consensus.nwr", "fastWrite"); 43 | 44 | KleinProp prop2 = KleinProp.loadIfPresent(); 45 | 46 | Klein instance2 = Klein.startup(); 47 | KleinCache klein = new KleinCacheImpl("klein"); 48 | 49 | System.in.read(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /klein-example/src/main/java/com/ofcoder/klein/example/nwr/Main3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.ofcoder.klein.example.nwr; 18 | 19 | import java.io.IOException; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import com.ofcoder.klein.Klein; 25 | import com.ofcoder.klein.KleinProp; 26 | import com.ofcoder.klein.core.cache.KleinCache; 27 | import com.ofcoder.klein.core.cache.KleinCacheImpl; 28 | 29 | /** 30 | * Main3: cluster member. 31 | * 32 | * @author 释慧利 33 | */ 34 | public class Main3 { 35 | private static final Logger LOG = LoggerFactory.getLogger(Main3.class); 36 | 37 | public static void main(final String[] args) throws IOException { 38 | System.setProperty("klein.id", "3"); 39 | System.setProperty("klein.port", "1220"); 40 | System.setProperty("klein.ip", "127.0.0.1"); 41 | System.setProperty("klein.members", "1:127.0.0.1:1218:false;2:127.0.0.1:1219:false;3:127.0.0.1:1220:false"); 42 | System.setProperty("klein.consensus.nwr", "fastWrite"); 43 | 44 | KleinProp prop3 = KleinProp.loadIfPresent(); 45 | 46 | Klein instance3 = Klein.startup(); 47 | KleinCache klein = new KleinCacheImpl("klein"); 48 | 49 | System.in.read(); 50 | } 51 | } 52 | --------------------------------------------------------------------------------