00001 #ifndef __XRDFILECACHE_CACHE_HH__
00002 #define __XRDFILECACHE_CACHE_HH__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <string>
00021 #include <list>
00022 #include <set>
00023
00024 #include "Xrd/XrdScheduler.hh"
00025 #include "XrdVersion.hh"
00026 #include "XrdSys/XrdSysPthread.hh"
00027 #include "XrdOuc/XrdOucCache2.hh"
00028 #include "XrdOuc/XrdOucCallBack.hh"
00029 #include "XrdCl/XrdClDefaultEnv.hh"
00030 #include "XrdFileCacheFile.hh"
00031 #include "XrdFileCacheDecision.hh"
00032
00033 class XrdOucStream;
00034 class XrdSysError;
00035 class XrdSysTrace;
00036
00037 namespace XrdCl {
00038 class Log;
00039 }
00040 namespace XrdFileCache {
00041 class File;
00042 class IO;
00043 }
00044
00045
00046 namespace XrdFileCache
00047 {
00048
00050
00051 struct Configuration
00052 {
00053 Configuration() :
00054 m_hdfsmode(false),
00055 m_allow_xrdpfc_command(false),
00056 m_data_space("public"),
00057 m_meta_space("public"),
00058 m_diskTotalSpace(-1),
00059 m_diskUsageLWM(-1),
00060 m_diskUsageHWM(-1),
00061 m_fileUsageBaseline(-1),
00062 m_fileUsageNominal(-1),
00063 m_fileUsageMax(-1),
00064 m_purgeInterval(300),
00065 m_purgeColdFilesAge(-1),
00066 m_purgeColdFilesPeriod(-1),
00067 m_bufferSize(1024*1024),
00068 m_RamAbsAvailable(0),
00069 m_NRamBuffers(-1),
00070 m_prefetch_max_blocks(10),
00071 m_hdfsbsize(128*1024*1024),
00072 m_flushCnt(100)
00073 {}
00074
00075 bool are_file_usage_limits_set() const { return m_fileUsageMax > 0; }
00076 bool is_age_based_purge_in_effect() const { return m_purgeColdFilesAge > 0; }
00077 bool is_purge_plugin_set_up() const { return false; }
00078
00079 void calculate_fractional_usages(long long du, long long fu, double &frac_du, double &frac_fu);
00080
00081 bool m_hdfsmode;
00082 bool m_allow_xrdpfc_command;
00083
00084 std::string m_username;
00085 std::string m_data_space;
00086 std::string m_meta_space;
00087
00088 long long m_diskTotalSpace;
00089 long long m_diskUsageLWM;
00090 long long m_diskUsageHWM;
00091 long long m_fileUsageBaseline;
00092 long long m_fileUsageNominal;
00093 long long m_fileUsageMax;
00094 int m_purgeInterval;
00095 int m_purgeColdFilesAge;
00096 int m_purgeColdFilesPeriod;
00097
00098 long long m_bufferSize;
00099 long long m_RamAbsAvailable;
00100 int m_NRamBuffers;
00101 int m_prefetch_max_blocks;
00102
00103 long long m_hdfsbsize;
00104 long long m_flushCnt;
00105 };
00106
00107 struct TmpConfiguration
00108 {
00109 std::string m_diskUsageLWM;
00110 std::string m_diskUsageHWM;
00111 std::string m_fileUsageBaseline;
00112 std::string m_fileUsageNominal;
00113 std::string m_fileUsageMax;
00114 std::string m_flushRaw;
00115
00116 TmpConfiguration() :
00117 m_diskUsageLWM("0.90"), m_diskUsageHWM("0.95"),
00118 m_flushRaw("100")
00119 {}
00120 };
00121
00122
00124
00125 class Cache : public XrdOucCache2
00126 {
00127 public:
00128
00130
00131 Cache(XrdSysLogger *logger);
00132
00133
00135
00136 using XrdOucCache2::Attach;
00137
00138 virtual XrdOucCacheIO2 *Attach(XrdOucCacheIO2 *, int Options = 0);
00139
00140
00142
00143 virtual int isAttached();
00144
00145
00146
00147 virtual void EnvInfo(XrdOucEnv &theEnv);
00148
00149
00150
00151
00152 virtual int LocalFilePath(const char *url, char *buff=0, int blen=0,
00153 LFP_Reason why=ForAccess);
00154
00155
00156
00157 virtual int Prepare(const char *url, int oflags, mode_t mode);
00158
00159
00160 virtual int Stat(const char *url, struct stat &sbuff);
00161
00162
00168
00169 bool Decide(XrdOucCacheIO*);
00170
00171
00173
00174 const Configuration& RefConfiguration() const { return m_configuration; }
00175
00176
00183
00184 bool Config(const char *config_filename, const char *parameters);
00185
00186
00188
00189 static Cache &CreateInstance(XrdSysLogger *logger);
00190
00191
00193
00194 static Cache &GetInstance();
00195
00196
00198
00199 static bool VCheck(XrdVersionInfo &urVersion) { return true; }
00200
00201
00203
00204 void Purge();
00205
00206
00208
00209 void AddWriteTask(Block* b, bool from_read);
00210
00211
00214
00215 void RemoveWriteQEntriesFor(File *f);
00216
00217
00219
00220 void ProcessWriteTasks();
00221
00222 bool RequestRAMBlock();
00223
00224 void RAMBlockReleased();
00225
00226 void RegisterPrefetchFile(File*);
00227 void DeRegisterPrefetchFile(File*);
00228
00229 File* GetNextFileToPrefetch();
00230
00231 void Prefetch();
00232
00233 XrdOss* GetOss() const { return m_output_fs; }
00234
00235 bool IsFileActiveOrPurgeProtected(const std::string&);
00236
00237 File* GetFile(const std::string&, IO*, long long off = 0, long long filesize = 0);
00238
00239 void ReleaseFile(File*, IO*);
00240
00241 void ScheduleFileSync(File* f) { schedule_file_sync(f, false, false); }
00242
00243 void FileSyncDone(File*, bool high_debug);
00244
00245 XrdSysError* GetLog() { return &m_log; }
00246 XrdSysTrace* GetTrace() { return m_trace; }
00247
00248 void ExecuteCommandUrl(const std::string& command_url);
00249
00250 private:
00251 bool ConfigParameters(std::string, XrdOucStream&, TmpConfiguration &tmpc);
00252 bool ConfigXeq(char *, XrdOucStream &);
00253 bool xdlib(XrdOucStream &);
00254 bool xtrace(XrdOucStream &);
00255
00256 bool cfg2bytes(const std::string &str, long long &store, long long totalSpace, const char *name);
00257
00258 static Cache *m_factory;
00259 static XrdScheduler *schedP;
00260
00261 XrdSysError m_log;
00262 XrdSysTrace *m_trace;
00263 const char *m_traceID;
00264
00265 XrdOucCacheStats m_stats;
00266 XrdOss *m_output_fs;
00267
00268 std::vector<XrdFileCache::Decision*> m_decisionpoints;
00269
00270 std::map<std::string, long long> m_filesInQueue;
00271
00272 Configuration m_configuration;
00273
00274 XrdSysCondVar m_prefetch_condVar;
00275 bool m_prefetch_enabled;
00276
00277 XrdSysMutex m_RAMblock_mutex;
00278 int m_RAMblocks_used;
00279 bool m_isClient;
00280
00281 struct WriteQ
00282 {
00283 WriteQ() : condVar(0), size(0), writes_between_purges(0) {}
00284
00285 XrdSysCondVar condVar;
00286 size_t size;
00287 std::list<Block*> queue;
00288 long long writes_between_purges;
00289 };
00290
00291 WriteQ m_writeQ;
00292
00293
00294 typedef std::map<std::string, File*> ActiveMap_t;
00295 typedef ActiveMap_t::iterator ActiveMap_i;
00296 typedef std::set<std::string> FNameSet_t;
00297
00298 ActiveMap_t m_active;
00299 FNameSet_t m_purge_delay_set;
00300 bool m_in_purge;
00301 XrdSysCondVar m_active_cond;
00302
00303 void inc_ref_cnt(File*, bool lock, bool high_debug);
00304 void dec_ref_cnt(File*, bool high_debug);
00305
00306 void schedule_file_sync(File*, bool ref_cnt_already_set, bool high_debug);
00307
00308
00309 typedef std::vector<File*> PrefetchList;
00310 PrefetchList m_prefetchList;
00311 };
00312
00313 }
00314
00315 #endif