Search in sources :

Example 1 with ReplicaFilesRequest

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

the class ReplicationManager method requestReplicaFiles.

//Recovery Method
@Override
public void requestReplicaFiles(String selectedReplicaId, Set<Integer> partitionsToRecover, Set<String> existingFiles) throws IOException {
    ReplicaFilesRequest request = new ReplicaFilesRequest(partitionsToRecover, existingFiles);
    dataBuffer = ReplicationProtocol.writeGetReplicaFilesRequest(dataBuffer, request);
    try (SocketChannel socketChannel = getReplicaSocket(selectedReplicaId)) {
        //transfer request
        NetworkingUtil.transferBufferToChannel(socketChannel, dataBuffer);
        String indexPath;
        String destFilePath;
        ReplicationRequestType responseFunction = ReplicationProtocol.getRequestType(socketChannel, dataBuffer);
        LSMIndexFileProperties fileProperties;
        while (responseFunction != ReplicationRequestType.GOODBYE) {
            dataBuffer = ReplicationProtocol.readRequest(socketChannel, dataBuffer);
            fileProperties = ReplicationProtocol.readFileReplicationRequest(dataBuffer);
            //get index path
            indexPath = replicaResourcesManager.getIndexPath(fileProperties);
            destFilePath = indexPath + File.separator + fileProperties.getFileName();
            //create file
            File destFile = new File(destFilePath);
            destFile.createNewFile();
            try (RandomAccessFile fileOutputStream = new RandomAccessFile(destFile, "rw");
                FileChannel fileChannel = fileOutputStream.getChannel()) {
                fileOutputStream.setLength(fileProperties.getFileSize());
                NetworkingUtil.downloadFile(fileChannel, socketChannel);
                fileChannel.force(true);
            }
            //we need to create LSN map for .metadata files that belong to remote replicas
            if (!fileProperties.isLSMComponentFile() && !fileProperties.getNodeId().equals(nodeId)) {
                //replica index
                replicaResourcesManager.initializeReplicaIndexLSNMap(indexPath, logManager.getAppendLSN());
            }
            responseFunction = ReplicationProtocol.getRequestType(socketChannel, dataBuffer);
        }
        //send goodbye
        ReplicationProtocol.sendGoodbye(socketChannel);
    }
}
Also used : ReplicaFilesRequest(org.apache.asterix.replication.functions.ReplicaFilesRequest) SocketChannel(java.nio.channels.SocketChannel) RandomAccessFile(java.io.RandomAccessFile) ReplicationRequestType(org.apache.asterix.replication.functions.ReplicationProtocol.ReplicationRequestType) LSMIndexFileProperties(org.apache.asterix.replication.storage.LSMIndexFileProperties) FileChannel(java.nio.channels.FileChannel) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 FileChannel (java.nio.channels.FileChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 ReplicaFilesRequest (org.apache.asterix.replication.functions.ReplicaFilesRequest)1 ReplicationRequestType (org.apache.asterix.replication.functions.ReplicationProtocol.ReplicationRequestType)1 LSMIndexFileProperties (org.apache.asterix.replication.storage.LSMIndexFileProperties)1