Actor Persistence&Snapshot 快照与持久化

快照与持久化算是Actor 进行数据保存的一种设计


可以将消息数据保存(本地或内存),而进行actor恢复时,则只需要重放消息,则可以将actor恢复至停止前的状态。同时可以对数据进行快照存储,快照的好处,则是恢复时避免每次都是从第一消息进行重放。


示例:

package com.zte.sunquan.demo.actor.persist;

import akka.actor.Props;
import akka.persistence.AbstractPersistentActor;
import akka.persistence.RecoveryCompleted;
import akka.persistence.SnapshotOffer;

/**
 * Created by x on 2017/9/30.
 */
public class AlgorithmActor extends AbstractPersistentActor {
    private static final String PERSISTENCE_ID = "AlgorithmActor-1";
    private int result = 0;
    private String name;

    public AlgorithmActor() {
    }

    public AlgorithmActor(String name) {
        this.name = name;
    }

    public static Props props() {
        return Props.create(AlgorithmActor.class);
    }

    public static Props props(String name) {
        return Props.create(AlgorithmActor.class, name);
    }

    @Override
    public Receive createReceiveRecover() {
        return receiveBuilder().match(SnapshotOffer.class, p -> {
            result = (int) ((SnapshotOffer) p).snapshot();
            System.out.println("got snapshot:" + result);
        }).match(String.class, p -> {
            System.out.println("got recover msg and cal:" + p);
            calResult(p);
        }).match(RecoveryCompleted.class, p -> {
            System.out.println(getContext().self() + " has recovery complete.");
        })
                //必须
                .build();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().match(String.class, p -> {
            if ("snapshot".equalsIgnoreCase(p)) {
                saveSnapshot(result);
            } else if ("print".equalsIgnoreCase(p)) {
                System.out.println("result:" + result);
            } else {
                calResult(p);
                //save log
                persist(p, m -> {
                    //after persist
                    System.out.println("persist msg:" + p);
//                getContext().system().eventStream().publish(m);
                });
            }


        })
                .build();
    }

    @Override
    public String persistenceId() {
        return PERSISTENCE_ID;
    }

    private void calResult(String p) {
        int num = Integer.parseInt(p.substring(1));
        String symbol = p.substring(0, 1);
        switch (symbol) {
            case "+":
                result += num;
                break;
            case "-":
                result -= num;
                break;
            case "*":
                result *= num;
                break;
            case "/":
                result /= num;
                break;
            default:

                break;
        }
    }
}


测试用例:
package com.zte.sunquan.demo.actor.persist;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by x on 2017/9/30.
 * akka.persistence.journal.leveldb.dir = "target/example/journal"
 * akka.persistence.journal.plugin = "akka.persistence.journal.leveldb"
 * akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
 * akka.persistence.snapshot-store.local.dir = "target/example/snapshots"
 */
public class ActorTest {
    private ActorSystem system;

    @Before
    public void init() throws IOException {
        system = ActorSystem.create("test-sq");
        //delete snapshot and journal
        String projectPath = System.getProperty("user.dir");
        File journalDir = new File(projectPath + File.separator + "target/example/journal");
        File snapshotDir = new File(projectPath + File.separator + "target/example/snapshots");
        FileUtils.cleanDirectory(journalDir);
        FileUtils.cleanDirectory(snapshotDir);
    }

    @Test
    public void testLocalPersistActor() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        ActorRef calActor = system.actorOf(AlgorithmActor.props());
        calActor.tell("+1", ActorRef.noSender());
        calActor.tell("*2", ActorRef.noSender());
        calActor.tell("*5", ActorRef.noSender());
        calActor.tell("print", ActorRef.noSender());//print 10
        Thread.sleep(2000);
        system.stop(calActor);
        ActorRef calActor2 = system.actorOf(AlgorithmActor.props("alg"));
        calActor2.tell("*5", ActorRef.noSender());//first repeat +1 *2 *5 msg
        calActor2.tell("print", ActorRef.noSender());//print 50

        calActor2.tell("snapshot", ActorRef.noSender());
        calActor2.tell("/2", ActorRef.noSender());
        calActor2.tell("print", ActorRef.noSender());//print 25
        Thread.sleep(2000);
        system.stop(calActor2);

        calActor = system.actorOf(AlgorithmActor.props());
        calActor.tell("+1", ActorRef.noSender());// repeat only one /2 msg after get snapshot
        calActor.tell("print", ActorRef.noSender());//print 26
        latch.await();
    }
}
========================================================

自定义内存里保存事件示例:

增加

package com.zte.sunquan.demo.actor.persist.inmem;

import akka.dispatch.Futures;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.persistence.AtomicWrite;
import akka.persistence.PersistentImpl;
import akka.persistence.PersistentRepr;
import akka.persistence.journal.japi.AsyncWriteJournal;
import com.google.common.collect.Maps;
import org.apache.commons.lang.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.concurrent.Future;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class InMemoryJournal extends AsyncWriteJournal {

//    static final Logger LOG = LoggerFactory.getLogger(InMemoryJournal.class);
    private LoggingAdapter LOG = Logging.getLogger(context().system(), this);
    private static final Map<String, Map<Long, Object>> journals = new ConcurrentHashMap<>();

    @Override
    public Future<Void> doAsyncReplayMessages(final String persistenceId, final long fromSequenceNr,
                                              final long toSequenceNr, final long max, final Consumer<PersistentRepr> replayCallback) {
        LOG.info("doAsyncReplayMessages for {}: fromSequenceNr: {}, toSequenceNr: {}", persistenceId,
                fromSequenceNr, toSequenceNr);

        return Futures.future(() -> {

            Map<Long, Object> journal = journals.get(persistenceId);
            if (journal == null) {
                return null;
            }
            synchronized (journal) {
                int count = 0;
                Set<Long> needRecover = journal.keySet().stream().filter(sq -> sq >= fromSequenceNr && sq <= toSequenceNr)
                        .limit(max).collect(Collectors.toSet());
                needRecover.stream().forEach(sq -> {
                    Object data = journal.get(sq);
                    PersistentRepr persistentMessage =
                            new PersistentImpl(deserialize(data), sq, persistenceId,
                                    null, false, null, null);
                    replayCallback.accept(persistentMessage);
                });
            }
            return null;
        }, context().dispatcher());
    }

    private static Object deserialize(Object data) {
        return data instanceof byte[] ? SerializationUtils.deserialize((byte[]) data) : data;
    }

    @Override
    public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
        LOG.info("doAsyncReadHighestSequenceNr for {}: fromSequenceNr: {}", persistenceId, fromSequenceNr);

        Map<Long, Object> journal = journals.get(persistenceId);
        if (journal == null) {
            return Futures.successful(fromSequenceNr);
        }

        long sequenceNr = -1;
        synchronized (journal) {
            OptionalLong max = journal.keySet().stream().mapToLong(Long::longValue).max();
            if (max.isPresent() && max.getAsLong() >= fromSequenceNr) {
                sequenceNr = max.getAsLong();
            }
        }
        return Futures.successful(sequenceNr);
    }

    @Override
    public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(Iterable<AtomicWrite> messages) {
        return Futures.future(() -> {
            for (AtomicWrite write : messages) {
                PersistentRepr[] array = new PersistentRepr[write.payload().size()];
                write.payload().copyToArray(array);
                for (PersistentRepr repr : array) {
                    LOG.info("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
                            repr.sequenceNr(), repr.payload());
                    journals.putIfAbsent(repr.persistenceId(), Maps.newLinkedHashMap());
                    journals.get(repr.persistenceId()).put(repr.sequenceNr(),
                            repr.payload() instanceof Serializable ?
                                    SerializationUtils.serialize((Serializable) repr.payload()) : repr.payload());
                }
            }
            return Collections.emptyList();
        }, context().dispatcher());
    }

    @Override
    public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr) {
        LOG.info("doAsyncDeleteMessagesTo: {}", toSequenceNr);
        Map<Long, Object> journal = journals.get(persistenceId);
        Set<Long> needDel = journals.get(persistenceId).keySet().stream()
                .filter(sq -> sq <= toSequenceNr)
                .collect(Collectors.toSet());
        needDel.stream().forEach(journal::remove);
        return Futures.successful(null);
    }
}

配置jouranl保存使用上面类

akka.persistence.journal.plugin = "in-memory-journal"
in-memory-journal {
  class = "com.zte.sunquan.demo.actor.persist.inmem.InMemoryJournal"
  replay-dispatcher = "akka.persistence.dispatchers.default-replay-dispatcher"
}

akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
akka.persistence.snapshot-store.local.dir = "target/example/snapshots"


启用相同测试用例,本地目录jouranl不会创建,所有事件都存储在了内存当前,即上面journals中。



输出打印分析:

[INFO] [10/16/2017 15:43:44.199] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncReadHighestSequenceNr for AlgorithmActor-1: fromSequenceNr: 0
[INFO] [10/16/2017 15:43:44.222] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 1, payload: +1
persist msg:+1 //写
[INFO] [10/16/2017 15:43:44.236] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 2, payload: *2
persist msg:*2
[INFO] [10/16/2017 15:43:44.236] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 3, payload: *5
persist msg:*5
result:10
[INFO] [10/16/2017 15:43:45.993] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncReadHighestSequenceNr for AlgorithmActor-1: fromSequenceNr: 0
[INFO] [10/16/2017 15:43:46.003] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncReplayMessages for AlgorithmActor-1: fromSequenceNr: 1, toSequenceNr: 3 //恢复
got recover msg and cal:+1
got recover msg and cal:*2
got recover msg and cal:*5
[INFO] [10/16/2017 15:43:46.018] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 4, payload: *5
persist msg:*5
result:50
[INFO] [10/16/2017 15:43:46.019] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 5, payload: /2
persist msg:/2
result:25
got snapshot:50
got recover msg and cal:/2
[INFO] [10/16/2017 15:43:48.002] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncReadHighestSequenceNr for AlgorithmActor-1: fromSequenceNr: 4
[INFO] [10/16/2017 15:43:48.002] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncReplayMessages for AlgorithmActor-1: fromSequenceNr: 5, toSequenceNr: 5
persist msg:+1
result:26
[INFO] [10/16/2017 15:43:48.004] [test-sq-akka.persistence.dispatchers.default-plugin-dispatcher-8] [akka://test-sq/system/in-memory-journal] doAsyncWriteMessages: id: AlgorithmActor-1: seqNr: 6, payload: +1






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值