Copycat’s primary role is as a framework for building highly consistent, fault-tolerant replicated state machines.

Copycat servers receive state machine operations from clients, log and replicate the operations as necessary, and apply them to a state machine on each server.

State machine operations are guaranteed to be applied in the same order on all servers, and Copycat handles the persistence and replication of the state machine state internally.

Copycat是用来管理分布式状态机的,要保证所有操作以相同的顺序在每个server上被执行,从而得到一直的状态机的状态

为了做fault-tolerant,当状态机crash可以恢复,所以要先把operation写入log,并保证所有server上的log是一致的,这样只需要按log回放就可以得到一致的状态

这种replication技术,成为operation transfer

还有一种是state transfer

参考,Data replication 同步技术

也可以参考kudu的论文,kudu

Kudu does not replicate the on-disk storage of a tablet, but rather just its operation log.
The physical storage of each replica of a tablet is fully decoupled.

这样做对于server的状态机,或kudu所说的tablet存储是不感知分布式的,fully decoupled;

用户使用Copycat,

首先需要创建一个statemachine类,这就是用户需要同步的对象,

public class MapStateMachine extends StateMachine {
}

Copycat replicated state machines are modified and queried by defining operations through which a client and state machine can communicate.

Operations are replicated by the Copycat cluster and are translated into arguments to methods on the replicated state machine.
Users must define the interface between the client and the cluster by implementing Operation classes that clients will submit to the replicated state machine.

然后用户要定义,这个StateMachine之上的操作,

操作分为两类,

Command,可以修改状态机

Query,只读

Command

For example, in a map state machine some commands might include put and remove. To implement a state machine command, simply implement theCommand interface.

public class PutCommand implements Command<Object> {
private final Object key;
private final Object value; public PutCommand(Object key, Object value) {
this.key = key;
this.value = value;
} public Object key() {
return key;
} public Object value() {
return value;
}
}

上面就定义一个put command,这个命令就是要把key:value put到状态机

Query

Queries are state machine operations that read the system’s state but do not modify it. For example, in a map state machine some queries might include get, size, and isEmpty. To implement a state machine query, implement the Query interface.

public class GetQuery implements Query<Object> {
private final Object key; public GetQuery(Object key) {
this.key = key;
} public Object key() {
return key;
}
}

再者,要在状态机上实现这些操作,

Implementing State Machine Operations

State machine operations are implemented as public methods on the state machine class which accept a singleCommit parameter where the generic argument for the commit is the operation accepted by the method. Copycat automatically detects the command or query that applies to a given state machine methods based on the generic argument to the Commit parameter.

public class MapStateMachine extends StateMachine {
private Map<Object, Object> map = new HashMap<>(); public Object put(Commit<PutCommand> commit) {
try {
map.put(commit.operation().key(), commit.operation().value());
} finally {
commit.close();
}
} public Object get(Commit<GetQuery> commit) {
try {
return map.get(commit.operation().key());
} finally {
commit.close();
}
}
}

Commit可以认为是command的封装

snapshot逻辑的实现,

State machine operations are replicated and written to a log on disk on each server in the cluster.

As commands are submitted to the cluster over time, the disk capacity will eventually be consumed.
Copycat must periodically remove unneeded commands from the replicated log to conserve disk space. This is known as log compaction.

log越来越大就需要删掉老的log,但是为了保证数据不丢,就需要把当前的statemachine做snapshot存储下来;这样就可以把当前状态以前的log给删除掉

public class MapStateMachine extends StateMachine implements Snapshottable {
private Map<Object, Object> map = new HashMap<>(); @Override
public void snapshot(SnapshotWriter writer) {
writer.writeObject(map);
} @Override
public void install(SnapshotReader reader) {
map = reader.readObject();
}
}

For snapshottable state machines, Copycat will periodically request a binary snapshot of the state machine’s state and write the snapshot to disk. If the server is restarted, the state machine’s state will be recovered from the on-disk snapshot. When a new server joins the cluster, the snapshot of the state machine will be replicated to the joining server to catch up its state. This allows Copycat to remove commits that contributed to the snapshot from the replicated log, thus conserving disk space.

最后,创建cluster

1. 先建立一个server,

Once a state machine and its operations have been defined, we can create a CopycatServer to manage the state machine.

Address address = new Address("123.456.789.0", 5000);
CopycatServer.Builder builder = CopycatServer.builder(address);
builder.withStateMachine(MapStateMachine::new);

用我们上面定义的MapStateMachine,拉起server

builder.withTransport(NettyTransport.builder()
.withThreads(4)
.build()); builder.withStorage(Storage.builder()
.withDirectory(new File("logs"))
.withStorageLevel(StorageLevel.DISK)
.build()); CopycatServer server = builder.build();

可以自定义的,transport和storage

注册我们定义的command

One final task is necessary to complete the configuration of the server. We’ve created two state machine operations - PutCommand and GetQuery - which are Serializable. By default, Copycat’s serialization framework will serialize these operations using Java’s serialization. However, users can explicitly register serializable classes and implement custom binary serializers for more efficient serialization.

server.serializer().register(PutCommand.class);
server.serializer().register(GetQuery.class);

serializer默认是Java’s serialization,如果对性能有要求,可以自己实现序列化

2. 拉起集群

Bootstrapping the Cluster

Once the server has been built, we can bootstrap a new cluster by calling the bootstrap() method:

CompletableFuture<CopycatServer> future = server.bootstrap();
future.join();

When a server is bootstrapped, it forms a new single node cluster to which additional servers can be joined.

3. 加入已有集群

Joining an Existing Cluster

Once an initial cluster has been bootstrapped, additional servers can be added to the cluster via the join()method. When joining an existing cluster, the existing cluster configuration must be provided to the joinmethod:

Collection<Address> cluster = Collections.singleton(new Address("127.0.0.1", 8700))
server.join(cluster).join();

Copycat - Overview的更多相关文章

  1. [原] KVM 虚拟化原理探究(1)— overview

    KVM 虚拟化原理探究- overview 标签(空格分隔): KVM 写在前面的话 本文不介绍kvm和qemu的基本安装操作,希望读者具有一定的KVM实践经验.同时希望借此系列博客,能够对KVM底层 ...

  2. Activity之概览屏幕(Overview Screen)

    概览屏幕 概览屏幕(也称为最新动态屏幕.最近任务列表或最近使用的应用)是一个系统级别 UI,其中列出了最近访问过的 Activity 和任务. 用户可以浏览该列表并选择要恢复的任务,也可以通过滑动清除 ...

  3. Atitit.自然语言处理--摘要算法---圣经章节旧约39卷概览bible overview v2 qa1.docx

    Atitit.自然语言处理--摘要算法---圣经章节旧约39卷概览bible overview v2 qa1.docx 1. 摘要算法的大概流程2 2. 旧约圣经 (39卷)2 2.1. 与古兰经的对 ...

  4. Overview of OpenCascade Library

    Overview of OpenCascade Library eryar@163.com 摘要Abstract:对OpenCascade库的功能及其实现做简要介绍. 关键字Key Words:Ope ...

  5. Apache Sqoop - Overview——Sqoop 概述

    Apache Sqoop - Overview Apache Sqoop 概述 使用Hadoop来分析和处理数据需要将数据加载到集群中并且将它和企业生产数据库中的其他数据进行结合处理.从生产系统加载大 ...

  6. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  7. Spring overview

    引子 接触Java很多年了,各种framework,却从未系统的去了解过.最近突然想清楚一件事,就是当下的目标——Focus on Java-based RESTful WS & JS.而之于 ...

  8. overview

    [1] Don’t panic! All will become clear in time; [2] You don’t have to know every detail of C++ to wr ...

  9. Atitit 知识图谱解决方案:提供完整知识体系架构的搜索与知识结果overview

    Atitit 知识图谱解决方案:提供完整知识体系架构的搜索与知识结果overview   知识图谱的表示和在搜索中的展1 提升Google搜索效果3 1.找到最想要的信息.3 2.提供最全面的摘要.4 ...

随机推荐

  1. C++11 列表初始化

    在我们实际编程中,我们经常会碰到变量初始化的问题,对于不同的变量初始化的手段多种多样,比如说对于一个数组我们可以使用 int arr[] = {1,2,3}的方式初始化,又比如对于一个简单的结构体: ...

  2. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  3. c++命名空间---namespace

    C++ 命名空间 C++ 应用程序中.例如,您可能会写一个名为 func() 的函数,在另一个可用的库中也存在一个相同的函数 func().这样,编译器就无法判断您所使用的是哪一个 func() 函数 ...

  4. 【平差软件学习---科傻】四、科傻二等水准平差(参数设置和in1文件讲解)

    [平差软件学习---科傻]四.科傻二等水准平差(参数设置和in1文件讲解) 这个算是最后一集了,也可能不是如果我想到不足的地方我会在补上一集视频,或者是文章页.总感觉自己操作的很熟练,到自己真正讲的时 ...

  5. Public key for ambari-server-2.4.2.0-136.x86_64.rpm is not installed 安装ambari报错总结

    提示;# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release 就是导入这个安装包的key 可以使用http的协议 比如我用的就是 rpm ...

  6. java反编译工具(Java Decompiler)

    1,下载地址,包括GUI,Eclipse插件 http://jd.benow.ca/ 2,Eclipse插件的安装参看 https://blog.csdn.net/yh_zeng2/article/d ...

  7. 【css】css 中文字体 unicode 对照表

    css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...

  8. 【HTTPS】自签CA证书 && nginx配置https服务

    首先,搭建https服务肯定需要一个https证书.这个证书可以看做是一个应用层面的证书.之所以这么说是因为https证书是基于CA证书生成的.对于正式的网站,CA证书需要到有资质的第三方证书颁发机构 ...

  9. elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or()Ljava/util/concu‌​rrent/Executor

    错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or() ...

  10. java-信息安全(十一)-非对称加密算法ECC

    概述 信息安全基本概念: ECC算法(Elliptic curve cryptography,椭圆曲线密码学) ECC 椭圆加密算法(ECC)是一种公钥加密体制,最初由Koblitz和Miller两人 ...