Search in sources :

Example 1 with ReplicaIndexFlushRequest

use of org.apache.asterix.replication.functions.ReplicaIndexFlushRequest in project asterixdb by apache.

the class ReplicationManager method requestFlushLaggingReplicaIndexes.

@Override
public void requestFlushLaggingReplicaIndexes(long nonSharpCheckpointTargetLSN) throws IOException {
    long startLSN = logManager.getAppendLSN();
    Set<String> replicaIds = getActiveReplicasIds();
    if (replicaIds.isEmpty()) {
        return;
    }
    ByteBuffer requestBuffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE);
    for (String replicaId : replicaIds) {
        //1. identify replica indexes with LSN less than nonSharpCheckpointTargetLSN.
        Map<Long, String> laggingIndexes = replicaResourcesManager.getLaggingReplicaIndexesId2PathMap(replicaId, nonSharpCheckpointTargetLSN);
        if (laggingIndexes.size() > 0) {
            //2. send request to remote replicas that have lagging indexes.
            ReplicaIndexFlushRequest laggingIndexesResponse = null;
            try (SocketChannel socketChannel = getReplicaSocket(replicaId)) {
                ReplicaIndexFlushRequest laggingIndexesRequest = new ReplicaIndexFlushRequest(laggingIndexes.keySet());
                requestBuffer = ReplicationProtocol.writeGetReplicaIndexFlushRequest(requestBuffer, laggingIndexesRequest);
                NetworkingUtil.transferBufferToChannel(socketChannel, requestBuffer);
                //3. remote replicas will respond with indexes that were not flushed.
                ReplicationRequestType responseFunction = waitForResponse(socketChannel, requestBuffer);
                if (responseFunction == ReplicationRequestType.FLUSH_INDEX) {
                    requestBuffer = ReplicationProtocol.readRequest(socketChannel, requestBuffer);
                    //returning the indexes that were not flushed
                    laggingIndexesResponse = ReplicationProtocol.readReplicaIndexFlushRequest(requestBuffer);
                }
                //send goodbye
                ReplicationProtocol.sendGoodbye(socketChannel);
            }
            /**
                 * 4. update the LSN_MAP for indexes that were not flushed
                 * to the current append LSN to indicate no operations happened
                 * since the checkpoint start.
                 */
            if (laggingIndexesResponse != null) {
                for (Long resouceId : laggingIndexesResponse.getLaggingRescouresIds()) {
                    String indexPath = laggingIndexes.get(resouceId);
                    Map<Long, Long> indexLSNMap = replicaResourcesManager.getReplicaIndexLSNMap(indexPath);
                    indexLSNMap.put(ReplicaResourcesManager.REPLICA_INDEX_CREATION_LSN, startLSN);
                    replicaResourcesManager.updateReplicaIndexLSNMap(indexPath, indexLSNMap);
                }
            }
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ReplicationRequestType(org.apache.asterix.replication.functions.ReplicationProtocol.ReplicationRequestType) ReplicaIndexFlushRequest(org.apache.asterix.replication.functions.ReplicaIndexFlushRequest) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 ReplicaIndexFlushRequest (org.apache.asterix.replication.functions.ReplicaIndexFlushRequest)1 ReplicationRequestType (org.apache.asterix.replication.functions.ReplicationProtocol.ReplicationRequestType)1