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);
}
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());
}
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());
}
Aggregations