📄 chunkmanager.h
字号:
//---------------------------------------------------------- -*- Mode: C++ -*-// $Id: ChunkManager.h 235 2009-01-07 23:34:55Z sriramsrao $ //// Created 2006/03/28// Author: Sriram Rao//// Copyright 2008 Quantcast Corp.// Copyright 2006-2008 Kosmix Corp.//// This file is part of Kosmos File System (KFS).//// Licensed under the Apache License, Version 2.0// (the "License"); you may not use this file except in compliance with// the License. You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or// implied. See the License for the specific language governing// permissions and limitations under the License.//// \file ChunkManager.h// \brief Handles all chunk related ops.////----------------------------------------------------------------------------#ifndef _CHUNKMANAGER_H#define _CHUNKMANAGER_H#include <tr1/unordered_map>#include <vector>#include <string>#include "libkfsIO/ITimeout.h"#include "libkfsIO/DiskManager.h"#include "libkfsIO/Globals.h"#include "Chunk.h"#include "KfsOps.h"#include "Logger.h"#include "common/cxxutil.h"namespace KFS{/// We allow a chunk header upto 16K in sizeconst size_t KFS_CHUNK_HEADER_SIZE = 16384;/// Encapsulate a chunk file descriptor and information about the/// chunk such as name and version #.struct ChunkInfoHandle_t { ChunkInfoHandle_t() : lastIOTime(0), isBeingReplicated(false), isMetadataReadOngoing(false), readChunkMetaOp(NULL) { }; struct ChunkInfo_t chunkInfo; /// Chunks are stored as files in he underlying filesystem; each /// chunk file is named by the chunkId. Each chunk has a header; /// this header is hidden from clients; all the client I/O is /// offset by the header amount FileHandlePtr dataFH; time_t lastIOTime; // when was the last I/O done on this chunk bool isBeingReplicated; // is the chunk being replicated from // another server /// set if a read request for the chunk meta-data has been issued to disk. bool isMetadataReadOngoing; /// keep track of the op that is doing the read ReadChunkMetaOp *readChunkMetaOp; void Release() { chunkInfo.UnloadChecksums(); dataFH->Close(); libkfsio::globals().ctrOpenDiskFds.Update(-1); } void Init(int fd) { dataFH.reset(new FileHandle_t(fd)); }};/// Map from a chunk id to a chunk handle///typedef std::tr1::unordered_map<kfsChunkId_t, ChunkInfoHandle_t *> CMap;typedef std::tr1::unordered_map<kfsChunkId_t, ChunkInfoHandle_t *>::const_iterator CMI;/// Periodically write out the chunk manager state to diskclass ChunkManagerTimeoutImpl;struct ChunkDirInfo_t { ChunkDirInfo_t() : usedSpace(0), availableSpace(0) { } std::string dirname; int64_t usedSpace; int64_t availableSpace;};/// The chunk manager writes out chunks as individual files on disk./// The location of the chunk directory is defined by chunkBaseDir./// The file names of chunks is a string representation of the chunk/// id. The chunk manager performs disk I/O asynchronously---that is,/// it schedules disk requests to the Disk manager which uses aio() to/// perform the operations.///class ChunkManager {public: ChunkManager(); ~ChunkManager(); /// Init function to configure the chunk manager object. void Init(const std::vector<std::string> &chunkDirs, int64_t totalSpace); /// Allocate a file to hold a chunk on disk. The filename is the /// chunk id itself. /// @param[in] fileId id of the file that has chunk chunkId /// @param[in] chunkId id of the chunk being allocated. /// @param[in] chunkVersion the version assigned by the metaserver to this chunk /// @param[in] isBeingReplicated is the allocation for replicating a chunk? /// @retval status code int AllocChunk(kfsFileId_t fileId, kfsChunkId_t chunkId, int64_t chunkVersion, bool isBeingReplicated = false); /// Delete a previously allocated chunk file. /// @param[in] chunkId id of the chunk being deleted. /// @retval status code int DeleteChunk(kfsChunkId_t chunkId); /// Dump chunk map with information about chunkID and chunkSize void DumpChunkMap(); /// Dump chunk map with information about chunkID and chunkSize /// to a string stream void DumpChunkMap(std::ostringstream &ofs); /// A previously created chunk is stale; move it to stale chunks /// dir; space can be reclaimed later /// /// @param[in] chunkId id of the chunk being moved /// @retval status code int StaleChunk(kfsChunkId_t chunkId); /// Truncate a chunk to the specified size /// @param[in] chunkId id of the chunk being truncated. /// @param[in] chunkSize size to which chunk should be truncated. /// @retval status code int TruncateChunk(kfsChunkId_t chunkId, off_t chunkSize); /// Change a chunk's version # to what the server says it should be. /// @param[in] fileId id of the file that has chunk chunkId /// @param[in] chunkId id of the chunk being allocated. /// @param[in] chunkVersion the version assigned by the metaserver to this chunk /// @retval status code int ChangeChunkVers(kfsFileId_t fileId, kfsChunkId_t chunkId, int64_t chunkVersion); /// Open a chunk for I/O. /// @param[in] chunkId id of the chunk being opened. /// @param[in] openFlags O_RDONLY, O_WRONLY /// @retval status code int OpenChunk(kfsChunkId_t chunkId, int openFlags); /// Close a previously opened chunk and release resources. /// @param[in] chunkId id of the chunk being closed. void CloseChunk(kfsChunkId_t chunkId); /// Schedule a read on a chunk. /// @param[in] op The read operation being scheduled. /// @retval 0 if op was successfully scheduled; -1 otherwise int ReadChunk(ReadOp *op); /// Schedule a write on a chunk. /// @param[in] op The write operation being scheduled. /// @retval 0 if op was successfully scheduled; -1 otherwise int WriteChunk(WriteOp *op); /// Write/read out/in the chunk meta-data and notify the cb when the op /// is done. /// @retval 0 if op was successfully scheduled; -errno otherwise int WriteChunkMetadata(kfsChunkId_t chunkId, KfsOp *cb); int ReadChunkMetadata(kfsChunkId_t chunkId, KfsOp *cb); /// Notification that read is finished void ReadChunkMetadataDone(kfsChunkId_t chunkId); /// We read the chunk metadata out of disk; we update the chunk /// table with this info. /// @retval 0 if successful (i.e., valid chunkid); -EINVAL otherwise int SetChunkMetadata(const DiskChunkInfo_t &dci); bool IsChunkMetadataLoaded(kfsChunkId_t chunkId) { ChunkInfoHandle_t *cih = NULL; if (GetChunkInfoHandle(chunkId, &cih) < 0) return false; return cih->chunkInfo.AreChecksumsLoaded(); } /// A previously scheduled write op just finished. Update chunk /// size and the amount of used space. /// @param[in] op The write op that just finished /// void ReadChunkDone(ReadOp *op); void ReplicationDone(kfsChunkId_t chunkId); /// Determine the size of a chunk. /// @param[in] chunkId The chunk whose size is needed /// @param[out] chunkSize The size of the chunk /// @retval status code int ChunkSize(kfsChunkId_t chunkId, off_t *chunkSize); /// Cancel a previously scheduled chunk operation. /// @param[in] cont The callback object that scheduled the /// operation /// @param[in] chunkId The chunk on which ops were scheduled void CancelChunkOp(KfsCallbackObj *cont, kfsChunkId_t chunkId); /// Register a timeout handler with the net manager for taking /// checkpoints. Also, get the logger going void Start(); /// Write out the chunk table data structure to disk void Checkpoint(); /// Read the chunk table from disk following a restart. See /// comments in the method for issues relating to validation (such /// as, checkpoint contains a chunk name, but the associated file /// is not there on disk, etc.). void Restart(); /// On a restart following a dirty shutdown, do log replay. This /// involves updating the Chunk table map to reflect operations /// that are in the log. /// When a checkpoint file is read, update the mChunkTable[] to /// include a mapping for cih->chunkInfo.chunkId. void AddMapping(ChunkInfoHandle_t *cih); /// Replay a chunk allocation. /// /// @param[in] fileId id of the file that has chunk chunkId /// @param[in] chunkId Update the mChunkTable[] to include this /// chunk id /// @param[in] chunkVersion the version assigned by the /// metaserver to this chunk. void ReplayAllocChunk(kfsFileId_t fileId, kfsChunkId_t chunkId, int64_t chunkVersion); /// Replay a chunk version # change. /// /// @param[in] fileId id of the file that has chunk chunkId /// @param[in] chunkId Update the mChunkTable[] with the changed /// version # for this chunkId /// @param[in] chunkVersion the version assigned by the /// metaserver to this chunk. void ReplayChangeChunkVers(kfsFileId_t fileId, kfsChunkId_t chunkId, int64_t chunkVersion);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -