Search in sources :

Example 1 with ReadRequest

use of org.monkey.mmq.core.entity.ReadRequest in project mmqtt by MrHKing.

the class StandalonePersistentServiceProcessor method get.

@Override
public Datum get(String key) throws MmqException {
    final List<byte[]> keys = Collections.singletonList(ByteUtils.toBytes(key));
    final ReadRequest req = ReadRequest.newBuilder().setGroup(this.raftGroup).setData(ByteString.copyFrom(serializer.serialize(keys))).build();
    try {
        final Response resp = onRequest(req);
        if (resp.getSuccess()) {
            BatchReadResponse response = serializer.deserialize(resp.getData().toByteArray(), BatchReadResponse.class);
            final List<byte[]> rValues = response.getValues();
            return rValues.isEmpty() ? null : serializer.deserialize(rValues.get(0), getDatumTypeFromKey(key));
        }
        throw new MmqException(ErrorCode.ProtoReadError.getCode(), resp.getErrMsg());
    } catch (Throwable e) {
        throw new MmqException(ErrorCode.ProtoReadError.getCode(), e.getMessage());
    }
}
Also used : Response(org.monkey.mmq.core.entity.Response) MmqException(org.monkey.mmq.core.exception.MmqException) ReadRequest(org.monkey.mmq.core.entity.ReadRequest)

Example 2 with ReadRequest

use of org.monkey.mmq.core.entity.ReadRequest in project mmqtt by MrHKing.

the class PersistentServiceProcessor method get.

@Override
public Datum get(String key) throws MmqException {
    final List<byte[]> keys = new ArrayList<>(1);
    keys.add(ByteUtils.toBytes(key));
    final ReadRequest req = ReadRequest.newBuilder().setGroup(this.raftGroup).setData(ByteString.copyFrom(serializer.serialize(keys))).build();
    try {
        Response resp = protocol.getData(req);
        if (resp.getSuccess()) {
            BatchReadResponse response = serializer.deserialize(resp.getData().toByteArray(), BatchReadResponse.class);
            final List<byte[]> rValues = response.getValues();
            return rValues.isEmpty() ? null : serializer.deserialize(rValues.get(0), getDatumTypeFromKey(key));
        }
        throw new MmqException(ErrorCode.ProtoReadError.getCode(), resp.getErrMsg());
    } catch (Throwable e) {
        throw new MmqException(ErrorCode.ProtoReadError.getCode(), e.getMessage());
    }
}
Also used : Response(org.monkey.mmq.core.entity.Response) MmqException(org.monkey.mmq.core.exception.MmqException) ArrayList(java.util.ArrayList) ReadRequest(org.monkey.mmq.core.entity.ReadRequest)

Example 3 with ReadRequest

use of org.monkey.mmq.core.entity.ReadRequest in project mmqtt by MrHKing.

the class MmqStateMachine method onApply.

@Override
public void onApply(Iterator iter) {
    int index = 0;
    int applied = 0;
    Message message;
    MmqClosure closure = null;
    try {
        while (iter.hasNext()) {
            Status status = Status.OK();
            try {
                if (iter.done() != null) {
                    closure = (MmqClosure) iter.done();
                    message = closure.getMessage();
                } else {
                    final ByteBuffer data = iter.getData();
                    message = ProtoMessageUtil.parse(data.array());
                }
                Loggers.RAFT.info("receive log : {}", message);
                LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", message);
                if (message instanceof WriteRequest) {
                    Response response = processor.onApply((WriteRequest) message);
                    postProcessor(response, closure);
                }
                if (message instanceof ReadRequest) {
                    Response response = processor.onRequest((ReadRequest) message);
                    postProcessor(response, closure);
                }
            } catch (Throwable e) {
                index++;
                status.setError(RaftError.UNKNOWN, e.toString());
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.setThrowable(e));
                throw e;
            } finally {
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.run(status));
            }
            applied++;
            index++;
            iter.next();
        }
    } catch (Throwable t) {
        Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t);
        iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t)));
    }
}
Also used : Response(org.monkey.mmq.core.entity.Response) Response(org.monkey.mmq.core.entity.Response) java.util(java.util) StateMachineAdapter(com.alipay.sofa.jraft.core.StateMachineAdapter) LocalFileMeta(org.monkey.mmq.core.consistency.snapshot.LocalFileMeta) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) SnapshotOperation(org.monkey.mmq.core.consistency.snapshot.SnapshotOperation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Reader(org.monkey.mmq.core.consistency.snapshot.Reader) RequestProcessor4CP(org.monkey.mmq.core.consistency.cp.RequestProcessor4CP) ByteBuffer(java.nio.ByteBuffer) JRaftUtils(org.monkey.mmq.core.distributed.raft.utils.JRaftUtils) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) com.alipay.sofa.jraft(com.alipay.sofa.jraft) BiConsumer(java.util.function.BiConsumer) Writer(org.monkey.mmq.core.consistency.snapshot.Writer) RaftError(com.alipay.sofa.jraft.error.RaftError) WriteRequest(org.monkey.mmq.core.entity.WriteRequest) Iterator(com.alipay.sofa.jraft.Iterator) org.monkey.mmq.core.utils(org.monkey.mmq.core.utils) NotifyCenter(org.monkey.mmq.core.notify.NotifyCenter) Configuration(com.alipay.sofa.jraft.conf.Configuration) ReadRequest(org.monkey.mmq.core.entity.ReadRequest) Objects(java.util.Objects) ConsistencyException(org.monkey.mmq.core.distributed.raft.exception.ConsistencyException) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) Message(com.google.protobuf.Message) RequestProcessor(org.monkey.mmq.core.consistency.RequestProcessor) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) WriteRequest(org.monkey.mmq.core.entity.WriteRequest) ByteBuffer(java.nio.ByteBuffer) ReadRequest(org.monkey.mmq.core.entity.ReadRequest)

Aggregations

ReadRequest (org.monkey.mmq.core.entity.ReadRequest)3 Response (org.monkey.mmq.core.entity.Response)3 MmqException (org.monkey.mmq.core.exception.MmqException)2 com.alipay.sofa.jraft (com.alipay.sofa.jraft)1 Iterator (com.alipay.sofa.jraft.Iterator)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 StateMachineAdapter (com.alipay.sofa.jraft.core.StateMachineAdapter)1 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)1 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)1 RaftError (com.alipay.sofa.jraft.error.RaftError)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)1 Message (com.google.protobuf.Message)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Objects (java.util.Objects)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BiConsumer (java.util.function.BiConsumer)1