use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class Replicator method notifyReplicatorStatusListener.
/**
* Notify replicator event(such as created, error, destroyed) to replicatorStateListener which is implemented by users.
*
* @param replicator replicator object
* @param event replicator's state listener event type
* @param status replicator's error detailed status
*/
private static void notifyReplicatorStatusListener(final Replicator replicator, final ReplicatorEvent event, final Status status, final ReplicatorStateListener.ReplicatorState newState) {
final ReplicatorOptions replicatorOpts = Requires.requireNonNull(replicator.getOpts(), "replicatorOptions");
final Node node = Requires.requireNonNull(replicatorOpts.getNode(), "node");
final PeerId peer = Requires.requireNonNull(replicatorOpts.getPeerId(), "peer");
final List<ReplicatorStateListener> listenerList = node.getReplicatorStatueListeners();
for (int i = 0; i < listenerList.size(); i++) {
final ReplicatorStateListener listener = listenerList.get(i);
if (listener != null) {
try {
switch(event) {
case CREATED:
RpcUtils.runInThread(() -> listener.onCreated(peer));
break;
case ERROR:
RpcUtils.runInThread(() -> listener.onError(peer, status));
break;
case DESTROYED:
RpcUtils.runInThread(() -> listener.onDestroyed(peer));
break;
case STATE_CHANGED:
RpcUtils.runInThread(() -> listener.stateChanged(peer, newState));
default:
break;
}
} catch (final Exception e) {
LOG.error("Fail to notify ReplicatorStatusListener, listener={}, event={}.", listener, event);
}
}
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ReplicatorGroupImpl method findTheNextCandidate.
@Override
public PeerId findTheNextCandidate(final ConfigurationEntry conf) {
PeerId peerId = null;
int priority = Integer.MIN_VALUE;
long maxIndex = -1L;
for (final Map.Entry<PeerId, ThreadId> entry : this.replicatorMap.entrySet()) {
if (!conf.contains(entry.getKey())) {
continue;
}
final int nextPriority = entry.getKey().getPriority();
if (nextPriority == ElectionPriority.NotElected) {
continue;
}
final long nextIndex = Replicator.getNextIndex(entry.getValue());
if (nextIndex > maxIndex) {
maxIndex = nextIndex;
peerId = entry.getKey();
priority = peerId.getPriority();
} else if (nextIndex == maxIndex && nextPriority > priority) {
peerId = entry.getKey();
priority = peerId.getPriority();
}
}
if (maxIndex == -1L) {
return null;
} else {
return peerId;
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class V1Decoder method decode.
public void decode(final LogEntry log, final byte[] content) {
// 1-5 type
final int iType = Bits.getInt(content, 1);
log.setType(EnumOutter.EntryType.forNumber(iType));
// 5-13 index
// 13-21 term
final long index = Bits.getLong(content, 5);
final long term = Bits.getLong(content, 13);
log.setId(new LogId(index, term));
// 21-25 peer count
int peerCount = Bits.getInt(content, 21);
// peers
int pos = 25;
if (peerCount > 0) {
List<PeerId> peers = new ArrayList<>(peerCount);
while (peerCount-- > 0) {
final short len = Bits.getShort(content, pos);
final byte[] bs = new byte[len];
System.arraycopy(content, pos + 2, bs, 0, len);
// peer len (short in 2 bytes)
// peer str
pos += 2 + len;
final PeerId peer = new PeerId();
peer.parse(AsciiStringUtil.unsafeDecode(bs));
peers.add(peer);
}
log.setPeers(peers);
}
// old peers
int oldPeerCount = Bits.getInt(content, pos);
pos += 4;
if (oldPeerCount > 0) {
List<PeerId> oldPeers = new ArrayList<>(oldPeerCount);
while (oldPeerCount-- > 0) {
final short len = Bits.getShort(content, pos);
final byte[] bs = new byte[len];
System.arraycopy(content, pos + 2, bs, 0, len);
// peer len (short in 2 bytes)
// peer str
pos += 2 + len;
final PeerId peer = new PeerId();
peer.parse(AsciiStringUtil.unsafeDecode(bs));
oldPeers.add(peer);
}
log.setOldPeers(oldPeers);
}
// data
if (content.length > pos) {
final int len = content.length - pos;
ByteBuffer data = ByteBuffer.allocate(len);
data.put(content, pos, len);
data.flip();
log.setData(data);
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class V1Encoder method encode.
@Override
public byte[] encode(final LogEntry log) {
if (log.hasLearners()) {
throw new IllegalArgumentException("V1 log entry encoder doesn't support learners");
}
EntryType type = log.getType();
LogId id = log.getId();
List<PeerId> peers = log.getPeers();
List<PeerId> oldPeers = log.getOldPeers();
ByteBuffer data = log.getData();
// magic number 1 byte
int totalLen = 1;
final int iType = type.getNumber();
final long index = id.getIndex();
final long term = id.getTerm();
// type(4) + index(8) + term(8)
totalLen += 4 + 8 + 8;
int peerCount = 0;
// peer count
totalLen += 4;
final List<String> peerStrs = new ArrayList<>(peerCount);
if (peers != null) {
peerCount = peers.size();
for (final PeerId peer : peers) {
final String peerStr = peer.toString();
// peer len (short in 2 bytes)
// peer str
totalLen += 2 + peerStr.length();
peerStrs.add(peerStr);
}
}
int oldPeerCount = 0;
// old peer count
totalLen += 4;
final List<String> oldPeerStrs = new ArrayList<>(oldPeerCount);
if (oldPeers != null) {
oldPeerCount = oldPeers.size();
for (final PeerId peer : oldPeers) {
final String peerStr = peer.toString();
// peer len (short in 2 bytes)
// peer str
totalLen += 2 + peerStr.length();
oldPeerStrs.add(peerStr);
}
}
final int bodyLen = data != null ? data.remaining() : 0;
totalLen += bodyLen;
final byte[] content = new byte[totalLen];
// {0} magic
content[0] = LogEntryV1CodecFactory.MAGIC;
// 1-5 type
Bits.putInt(content, 1, iType);
// 5-13 index
Bits.putLong(content, 5, index);
// 13-21 term
Bits.putLong(content, 13, term);
// peers
// 21-25 peer count
Bits.putInt(content, 21, peerCount);
int pos = 25;
for (final String peerStr : peerStrs) {
final byte[] ps = AsciiStringUtil.unsafeEncode(peerStr);
Bits.putShort(content, pos, (short) peerStr.length());
System.arraycopy(ps, 0, content, pos + 2, ps.length);
pos += 2 + ps.length;
}
// old peers
// old peers count
Bits.putInt(content, pos, oldPeerCount);
pos += 4;
for (final String peerStr : oldPeerStrs) {
final byte[] ps = AsciiStringUtil.unsafeEncode(peerStr);
Bits.putShort(content, pos, (short) peerStr.length());
System.arraycopy(ps, 0, content, pos + 2, ps.length);
pos += 2 + ps.length;
}
// data
if (data != null) {
System.arraycopy(data.array(), data.position(), content, pos, data.remaining());
}
return content;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class StoreEngine method inConfiguration.
private boolean inConfiguration(final String curr, final String all) {
final PeerId currPeer = new PeerId();
if (!currPeer.parse(curr)) {
return false;
}
final Configuration allConf = new Configuration();
if (!allConf.parse(all)) {
return false;
}
return allConf.contains(currPeer) || allConf.getLearners().contains(currPeer);
}
Aggregations