Copycat - Overview

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 getsize, and isEmpty. To implement a state machine query, implement the Queryinterface.

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 singleCommitparameter 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 Commitparameter.

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();
时间: 2024-10-25 21:15:37

Copycat - Overview的相关文章

An Overview of RMI Applications

application An Overview of RMI Applications RMI applications are often comprised of two separate programs: a server and a client. A typical server application creates some remote objects, makes references to them accessible, and waits for clients to

A Comparative Overview of C#中文版(一)

中文 A Comparative Overview of C#中文版(上篇)作者:Ben Albahari公司:Genamics日期:2000年7月31日初版,2000年8月10日修订.感谢以下人士支持和反馈(按字母先后顺序):Don Box. C.R. Manning. Joe Nalewabau. John Osborn. Thomas Rhode & Daryl Richter.译者:荣耀[译序:C#入门经典!希望文中针对新手的译注不会影响阅读的流畅性.限于译者时间和能力,文中倘有讹误,当

HTML及CSS基础课(七) CSS: An Overview

What CSS is(什么是CSS) CSS(Cascading Style Sheets级联样式表)是一种描述你html的外观和格式的语言. 一个样式表(style sheet)是一个描述html页面看起来怎么样的文件. 我们说这些样式表是级联(cascading)的是因为这些表格可以应用超过一种的样式.例如,你想让所有的段落<p>的字都是蓝色的,但是只有其中某一个单词是红色的,CSS也可以做到这点. 只要这样子设置css文件: p { color: red; } span { /*Wri

LINQ TO DataSet Overview

LINQ TO DataSet Overview 相对而言,LINQ TO DataSet是LINQ技术中最小的一块,虽然是DB中抽取出来 的一个离线的操作模型,但毕竟对象也是个内存里面的object而已.所以和LINQ TO Object相比,大多数的操作都是一样的,不同只是要根据DataSet,DataTable的结构标明字段而已.下面简单的列出LINQ TO DataSet相比LINQ TO Object一些要注意的特色. Query UnTyped DataSet 和一般的LINQ相比,

Enterprise Library Policy Injection Application Block 之一: PIAB Overview

在过去的半年里,定期或者不定期地写点东西已经成为了我的一种习惯.可是最近两个月来一直忙于工作的事情一直足够的时间留给自己,虽然给自己列了很长一串写作计划,可是心有余而力不足.这一段工作主要是帮助公司开发一套分布式的开发框架,对一些技术和设计方法有了一些新的认识.这两天的工作主要是如何把Enterprise Library V3.1的PIAB(Policy Injection Application Block)引入到我们自己的框架中,为次对PIAB进行了一些研究,借此机会与大家一起分享. 一.B

阿里移动技术Overview,详解阿里云移动云Apsara Mobile前世今生

2017杭州云栖大会阿里移动云峰会暨Apsara Mobile品牌发布会上,阿里资深技术专家天施带来<阿里移动技术Overview>的演讲.本文主要对移动十年进行总结,进而回顾了阿里这几年的移动技术变迁,包括Mobile First & Super APP基本原则.技术创新推动组织升级和阿里云移动云Apsara Mobile整个体系介绍和专有云EMAX服务的开启,最后对移动未来进行了展望.   以下是精彩内容整理: 去年阿里做了一年的Weex开源,业界也有很多声音,一直到今天,大家都说

Android NDK Overview ---- Android 4.4

Android NDK Overview === Introduction: --- The Android NDK is a set of tools that allows Android application developers to embed native machine code compiled from C and/or C++ source files into their application packages. IMPORTANT: > The Android NDK

Android开发者指南(18) —— Web Apps Overview

前言 本章内容为开发者指南(Dev Guide)/Web Applications/Web Apps Overview,版本为Android 3.2 r1,翻译来自:"happyjiahan",审核员为:"铁骑_PuLee",再次感谢"happyjiahan" !期待你一起参与翻译Android的相关资料,联系我over140@gmail.com.   声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.co

Java NIO Overview

Java Nio  1 Java NIO Tutorial 2 Java NIO Overview 3 Java NIO Channel 4 Java NIO Buffer 5 Java NIO Scatter / Gather 6 Java NIO Channel to Channel Transfers 7 Java NIO Selector 8 Java NIO FileChannel 9 Java NIO SocketChannel 10 Java NIO ServerSocketCha