Search in sources :

Example 1 with Session

use of tezc.core.cluster.Session in project tezcRaft by tezc.

the class RaftState method loadState.

/**
 * Deserialize state from an inputstream
 *
 * @param in             inputstream
 * @throws IOException   on any IO error
 */
@Override
public void loadState(InputStream in) throws IOException {
    int len = Util.readVarInt(in);
    Buffer buf = new Buffer(len);
    in.read(buf.array(), 0, len);
    buf.position(len);
    buf.flip();
    int count = buf.getVarInt();
    for (int i = 0; i < count; i++) {
        Session session = new Session(buf);
        sessions.put(session.getName(), session);
    }
    record = new ClusterRecord(buf);
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer) ClusterRecord(tezc.core.record.ClusterRecord) Session(tezc.core.cluster.Session)

Example 2 with Session

use of tezc.core.cluster.Session in project tezcRaft by tezc.

the class RaftState method saveState.

/**
 * Serialize state machine and write to outputstream
 *
 * @param out          outputstream
 * @throws IOException on any IO error
 */
@Override
public void saveState(OutputStream out) throws IOException {
    int len = ProtoUtil.varIntLen(sessions.size());
    for (Session session : sessions.values()) {
        len += session.encodedLen();
    }
    len += record.encodedLen();
    Buffer buf = new Buffer(len + ProtoUtil.varIntLen(len));
    buf.putVarInt(len);
    buf.putVarInt(sessions.size());
    for (Session session : sessions.values()) {
        session.encode(buf);
    }
    record.encode(buf);
    buf.flip();
    out.write(buf.backend().array());
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer) Session(tezc.core.cluster.Session)

Example 3 with Session

use of tezc.core.cluster.Session in project tezcRaft by tezc.

the class RaftState method handleRegisterCommand.

/**
 * Register command, create session for required id, if exists, return
 * existing record
 *
 * @param cmd register command
 */
@Override
public void handleRegisterCommand(RegisterCommand cmd) {
    Session session = sessions.get(cmd.getName());
    if (session == null) {
        for (int i = 0; i < sessions.size() + 1; i++) {
            boolean found = false;
            for (Session existing : sessions.values()) {
                if (existing.getId() == i) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                session = new Session(cmd.getName(), i, 0);
                sessions.put(cmd.getName(), session);
            }
        }
    }
    assert (session != null);
    cluster.handleRegisterCompleted(session.getId(), session.getSequence());
}
Also used : Session(tezc.core.cluster.Session)

Aggregations

Session (tezc.core.cluster.Session)3 ByteBuffer (java.nio.ByteBuffer)2 Buffer (tezc.base.common.Buffer)2 ClusterRecord (tezc.core.record.ClusterRecord)1