public void setFileInfo(FileInfo fileInfo){ this.fd = fileInfo.getFd(); this.type = fileInfo.getType(); this.dirOffset = fileInfo.getDirOffset(); this.capacity.set(fileInfo.getCapacity()); this.token = fileInfo.getToken(); this.modificationTime = fileInfo.getModificationTime(); }
void setCapacity(long currentCapacity) { fileInfo.setCapacity(currentCapacity); }
public synchronized CrailOutputStream getDirectOutputStream(long writeHint) throws Exception { if (fileInfo.getType().isDirectory()){ throw new Exception("Cannot open stream for directory"); } if (fileInfo.getToken() == 0){ throw new Exception("File is in read mode, cannot create outputstream, fd " + fileInfo.getFd()); } if (!outputStreams.tryAcquire()){ throw new Exception("Only one concurrent output stream per file allowed"); } return super.getOutputStream(writeHint); }
final void prefetchMetadata() throws Exception { long key = CoreSubOperation.createKey(fileInfo.getFd(), position); if (blockCache.containsKey(key)){ return; } if (nextBlockCache.containsKey(key)){ return; } this.syncedCapacity = fileInfo.getCapacity(); RpcFuture<RpcGetBlock> nextBlock = namenodeClientRpc.getBlock(fileInfo.getFd(), fileInfo.getToken(), position, syncedCapacity); nextBlock.setPrefetched(true); nextBlockCache.put(key, nextBlock); this.ioStats.incPrefetchedOps(); }
@Override public short setFile(RpcRequestMessage.SetFileReq request, RpcResponseMessage.VoidRes response, RpcNameNodeState errorState) throws Exception { //check protocol if (!RpcProtocol.verifyProtocol(RpcProtocol.CMD_SET_FILE, request, response)){ return RpcErrors.ERR_PROTOCOL_MISMATCH; } //get params FileInfo fileInfo = request.getFileInfo(); boolean close = request.isClose(); //rpc AbstractNode storedFile = fileTable.get(fileInfo.getFd()); if (storedFile == null){ return RpcErrors.ERR_FILE_NOT_OPEN; } if (storedFile.getToken() > 0 && storedFile.getToken() == fileInfo.getToken()){ storedFile.setCapacity(fileInfo.getCapacity()); } if (close){ storedFile.resetToken(); } if (CrailConstants.DEBUG){ LOG.info("setFile: " + fileInfo.toString() + ", close " + close); } return RpcErrors.ERR_OK; }
throw new IOException("createFile: " + RpcErrors.messages[RpcErrors.ERR_UNKNOWN]); if (fileInfo.getType() != type){ throw new IOException("createFile: " + "file type mismatch"); blockCache.remove(fileInfo.getFd()); nextBlockCache.remove(fileInfo.getFd()); CoreNode node = CoreNode.create(this, fileInfo, path); getBlockCache(fileInfo.getFd()).put(CoreSubOperation.createKey(fileInfo.getFd(), 0), fileBlock); if (fileInfo.getDirOffset() >= 0){ BlockInfo dirBlock = fileRes.getDirBlock(); getBlockCache(dirInfo.getFd()).put(CoreSubOperation.createKey(dirInfo.getFd(), fileInfo.getDirOffset()), dirBlock); CoreSyncOperation syncOperation = getSyncOperation(dirInfo, fileInfo, path, true); node.addSyncOperation(syncOperation); LOG.info("createFile: name " + path + ", success, fd " + fileInfo.getFd() + ", token " + fileInfo.getToken());
throw new IOException(RpcErrors.messages[renameRes.getError()]); if (renameRes.getDstParent().getCapacity() < renameRes.getDstFile().getDirOffset() + CrailConstants.DIRECTORY_RECORD){ LOG.info("rename: parent capacity does not match dst file offset, capacity " + renameRes.getDstParent().getCapacity() + ", offset " + renameRes.getDstFile().getDirOffset()); getBlockCache(srcParent.getFd()).put(CoreSubOperation.createKey(srcParent.getFd(), srcFile.getDirOffset()), srcBlock); BlockInfo dirBlock = renameRes.getDstBlock(); getBlockCache(dstDir.getFd()).put(CoreSubOperation.createKey(dstDir.getFd(), dstFile.getDirOffset()), dirBlock); blockCache.remove(srcFile.getFd());
public long getFd() { return fileInfo.getFd(); }
case RpcProtocol.CMD_CREATE_FILE: error = service.createFile(record.createFile(), response.createFile(), response); long fd = response.createFile().getFile().getFd(); long token = response.createFile().getFile().getToken(); tokens.put(response.createFile().getFile().getFd(), response.createFile().getFile().getToken()); tokens.put(response.createFile().getParent().getFd(), response.createFile().getParent().getToken()); break; case RpcProtocol.CMD_SET_FILE: record.setFile().getFileInfo().setToken(tokens.get(record.setFile().getFileInfo().getFd())); error = service.setFile(record.setFile(), response.getVoid(), response); break;
CoreSyncOperation getSyncOperation(FileInfo dirInfo, FileInfo fileInfo, String path, boolean valid) throws Exception{ long adjustedCapacity = fileInfo.getDirOffset()*CrailConstants.DIRECTORY_RECORD + CrailConstants.DIRECTORY_RECORD; dirInfo.setCapacity(Math.max(dirInfo.getCapacity(), adjustedCapacity)); CoreDirectory dirFile = new CoreDirectory(this, dirInfo, CrailUtils.getParent(path)); DirectoryOutputStream stream = dirFile.getDirectoryOutputStream(); DirectoryRecord record = new DirectoryRecord(valid, path); Future<CrailResult> future = stream.writeRecord(record, fileInfo.getDirOffset()); CoreSyncOperation syncOperation = new CoreSyncOperation(stream, future); return syncOperation; } }
CoreStream(CoreNode node, long streamId, long fileOffset) throws Exception { this.node = node; this.fs = node.getFileSystem(); this.fileInfo = node.getFileInfo(); this.endpointCache = fs.getDatanodeEndpointCache(); this.namenodeClientRpc = fs.getNamenodeClientRpc(); this.blockCache = fs.getBlockCache(fileInfo.getFd()); this.nextBlockCache = fs.getNextBlockCache(fileInfo.getFd()); this.bufferCheckpoint = fs.getBufferCheckpoint(); this.position = fileOffset; this.syncedCapacity = fileInfo.getCapacity(); this.streamId = streamId; this.ioStats = new CoreIOStatistics("core"); this.blockMap = new HashMap<Integer, CoreSubOperation>(); this.pendingBlocks = new LinkedList<RpcFuture<RpcGetBlock>>(); }
Future<Void> sync() throws IOException { Future<Void> future = null; if (fileInfo.getToken() > 0 && syncedCapacity < fileInfo.getCapacity()){ syncedCapacity = fileInfo.getCapacity(); future = new SyncNodeFuture(namenodeClientRpc.setFile(fileInfo, false)); } else { future = new NoOperation(); } return future; }
response.setDstBlock(dstBlock); if (response.getDstParent().getCapacity() < response.getDstFile().getDirOffset() + CrailConstants.DIRECTORY_RECORD){ LOG.info("rename: parent capacity does not match dst file offset, capacity " + response.getDstParent().getCapacity() + ", offset " + response.getDstFile().getDirOffset() + ", capacity " + dstParent.getCapacity() + ", offset " + dstFile.getDirOffset());
@Override public int files() { return (int) fileInfo.getCapacity()/CrailConstants.DIRECTORY_RECORD; }
public long getToken() { return fileInfo.getToken(); }
public CrailNodeType getType() { return fileInfo.getType(); }
@Override protected void setDirOffset(long dirOffset) { super.setDirOffset(dirOffset); }
public long getModificationTime() { return fileInfo.getModificationTime(); }
long blockRemaining = blockRemaining(); int opLen = CrailUtils.minFileBuf(blockRemaining, multiOperation.remaining()); CoreSubOperation subOperation = new CoreSubOperation(fileInfo.getFd(), position, multiOperation.getCurrentBufferPosition(), opLen); pendingBlocks.add(rpcFuture); } else { this.syncedCapacity = fileInfo.getCapacity(); RpcFuture<RpcGetBlock> rpcFuture = namenodeClientRpc.getBlock(fileInfo.getFd(), fileInfo.getToken(), position, syncedCapacity); blockMap.put(rpcFuture.getTicket(), subOperation); pendingBlocks.add(rpcFuture);