XRootD
XrdOssArcStopMon.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O s s S t o p M o n . h h */
4 /* */
5 /* (c) 2025 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <stdexcept>
32 #include <cstdlib>
33 #include <unistd.h>
34 
35 #include "Xrd/XrdScheduler.hh"
36 
38 #include "XrdOssArc/XrdOssArc.hh"
39 
40 #include "XrdSys/XrdSysError.hh"
41 #include "XrdSys/XrdSysFD.hh"
42 #include "XrdSys/XrdSysTimer.hh"
43 
44 /******************************************************************************/
45 /* G l o b a l O b j e c t s */
46 /******************************************************************************/
47 
48 namespace XrdOssArcGlobals
49 {
50 extern XrdScheduler* schedP;
51 
52 extern XrdSysError Elog;
53 
54 extern XrdSysTrace ArcTrace;
55 
56 static const char* StopFN = "STOP";
57 static const char* IdleFN = "IDLE";
58 }
59 using namespace XrdOssArcGlobals;
60 
61 /******************************************************************************/
62 /* C o n s t r u c t o r */
63 /******************************************************************************/
64 
65 XrdOssArcStopMon::XrdOssArcStopMon(const char* aPath, int chkT, bool& aOK)
66  : XrdJob("StopMon"),
67  xsLock(*(new XrdSysXSLock())),
68  admPath(aPath), chkInterval(chkT)
69 {
70 // Tty to open the admin path. This is where the STOP file should appear and
71 // where we plce the IDLE file once we have safely stopped all the archiving
72 // and restores.
73 //
74  if ((admDirFD = XrdSysFD_Open(aPath, O_DIRECTORY|O_RDONLY)) < 0)
75  {Elog.Emsg("Config", errno, "open admin path", aPath);
76  aOK = false;
77  return;
78  }
79 
80 // Remove any idle file left over from previous run
81 //
82  if (unlinkat(admDirFD, IdleFN, 0) && errno != ENOENT)
83  {Elog.Emsg("Config", errno, "remove IDLE file in admin path", aPath);
84  aOK = false;
85  close(admDirFD);
86  admDirFD = -1;
87  return;
88  }
89 
90 // Now schedule ourselves to see if a stop file appears
91 //
92  schedP->Schedule(this, time(0)+chkT);
93 
94 // All done
95 //
96  aOK = true;
97 }
98 
99 /******************************************************************************/
100 /* D e s t r u c t o r */
101 /******************************************************************************/
102 
104 {
105 // Parent instances of this class may not be destroyed. A parent instance
106 // is indicated by admDirFD being non-negative.
107 //
108  if (admDirFD >= 0)
109  {Elog.Emsg("StopMon", "Invalid deletion of StopMon parent instance for",
110  admPath);
111  std::abort();
112  }
113 
114 // We are not a parent, release the lock since the constructor obtained it.
115 //
116  Deactivate();
117 }
118 
119 /******************************************************************************/
120 /* D o I t */
121 /******************************************************************************/
122 
124 {
125  static const mode_t idleMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
126  static const int idleOflg = O_CREAT | O_RDWR | O_CLOEXEC;
127  struct stat Stat;
128 
129 // This method is only valid for the parent class.
130 //
131  if (admDirFD < 0)
132  {Elog.Emsg("StopMon","Invalid DoIt() call on child instance for",admPath);
133  return;
134  }
135 
136 // Check if a STOP file exists in the admpath. If it does, try to obtain a
137 // write lock. Only one thread may it and when we get it we know no other
138 // threads are actively executing a backup or restore. Once we have it,
139 // Issue a message and create an IDLE file.
140 //
141  if (fstatat(admDirFD, StopFN, &Stat, 0) == 0)
142  {Elog.Emsg("StopMon", "STOP file found in", admPath);
143  xsLock.Lock(xs_Exclusive);
144  Elog.Emsg("StopMon", "Drain complete; entering idle state...");
145  int iFD = openat(admDirFD, IdleFN, idleOflg, idleMode);
146  if (iFD < 0) Elog.Emsg("StopMon",errno,"create IDLE file in",admPath);
147  else close(iFD);
148  do {XrdSysTimer::Snooze(10);} while(!fstatat(admDirFD,StopFN,&Stat,0));
149  unlinkat(admDirFD, IdleFN, 0);
150  xsLock.UnLock(xs_Exclusive);
151  Elog.Emsg("StopMon", "Resuming execution; STOP file removed!");
152  }
153 
154 // Reschedule ourselves
155 //
156  schedP->Schedule(this, time(0)+chkInterval);
157 }
struct stat Stat
Definition: XrdCks.cc:49
int openat(int dirfd, const char *path, int flag,...)
#define close(a)
Definition: XrdPosix.hh:48
#define fstatat(a, b, c, d)
Definition: XrdPosix.hh:64
#define stat(a, b)
Definition: XrdPosix.hh:105
@ xs_Exclusive
Definition: XrdSysXSLock.hh:38
Definition: XrdJob.hh:43
virtual ~XrdOssArcStopMon()
XrdOssArcStopMon(const char *apath, int chkT, bool &aOK)
virtual void DoIt() override
void Schedule(XrdJob *jp)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:116
static void Snooze(int seconds)
Definition: XrdSysTimer.cc:168
void Lock(const XrdSysXS_Type usage)
Definition: XrdSysXSLock.cc:55
void UnLock(const XrdSysXS_Type usage=xs_None)
Definition: XrdSysXSLock.cc:95
XrdScheduler * schedP
static const char * StopFN
static const char * IdleFN
XrdSysTrace ArcTrace("OssArc")
XrdScheduler * schedP
Definition: XrdOssArc.cc:66
XrdSysError Elog(0, "OssArc_")