Belle II Software development
ERecoMaster.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include "daq/expreco/ERecoMaster.h"
10
11#include "daq/rfarm/manager/RFNSM.h"
12
13#include <nsm2/belle2nsm.h>
14
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <unistd.h>
18
19#include <cstring>
20
21using namespace std;
22using namespace Belle2;
23
24//#define DESY
25
26
27// Main
28ERecoMaster::ERecoMaster(const string& conffile)
29{
30 // 0. Initialize configuration manager
31 m_conf = new RFConf(conffile.c_str());
32 char* nodename = m_conf->getconf("master", "nodename");
33 // char nodename[256];
34 // gethostname ( nodename, sizeof(nodename) );
35
36 // 1. Initialize local shared memory
37 // m_shm = new RFSharedMem(nodename);
38
39 // 2. Set execution directory
40 string execdir = string(m_conf->getconf("system", "execdir_base")) + "/master";
41
42 mkdir(execdir.c_str(), 0755);
43 chdir(execdir.c_str());
44
45 // 3. Initialize LogManager
46 m_log = new RFLogManager(nodename, m_conf->getconf("system", "lognode"));
47
48 // 4. Leave PID file
49 FILE* f = fopen("pid.data", "w");
50 if (f != NULL) {
51 fprintf(f, "%d", getpid());
52 fclose(f);
53 }
54
55
56}
57
58ERecoMaster::~ERecoMaster()
59{
60 delete m_log;
61 // delete m_shm;
62 delete m_conf;
63}
64
65void ERecoMaster::Hook_Message_Handlers()
66{
67 // 5. Hook message handlers
68 if (b2nsm_callback("LOG", Log_Handler) < 0) {
69 fprintf(stderr, "ERecoMaster : hooking INFO handler failed, %s\n",
70 b2nsm_strerror());
71 }
72 printf("ERecoMaster: Message Handlers - Ready\n");
73
74}
75
76// NSM callback functions for message
77
78void ERecoMaster::Log_Handler(NSMmsg* /*msg*/, NSMcontext* /*ctx*/)
79{
80 // printf ( "ERecoMaster : [INFO] received\n" );
81 // b2nsm_ok ( msg, "INFO!!", NULL );
82 // fflush ( stdout );
83}
84
85
86// Functions hooked up by NSM2
87
88int ERecoMaster::Configure(NSMmsg*, NSMcontext*)
89{
90 int* pars = nullptr;
91
92 // 0. Configure DqmServer
93 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
94 RFNSM_Status::Instance().set_flag(0);
95 // b2nsm_sendreq(dqmserver, "RF_CONFIGURE", 0, pars);
96 b2nsm_sendreq(dqmserver, "RC_LOAD", 0, pars);
97 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
98 printf("ERecoMaster:: dqmserver configured\n");
99 sleep(2);
100
101 // 1. Configure distributor
102 const char* distributor = m_conf->getconf("distributor", "nodename");
103 RFNSM_Status::Instance().set_flag(0);
104 // b2nsm_sendreq(distributor, "RF_CONFIGURE", 0, pars);
105 b2nsm_sendreq(distributor, "RC_LOAD", 0, pars);
106 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
107 printf("ERecoMaster:: distributor configured\n");
108
109 sleep(2);
110
111 // 2. Configure event processors
112 int maxnodes = m_conf->getconfi("processor", "nnodes");
113 int idbase = m_conf->getconfi("processor", "idbase");
114 const char* hostbase = m_conf->getconf("processor", "nodebase");
115 char* badlist = m_conf->getconf("processor", "badlist");
116
117 char hostnode[512], idname[3];
118 int nnodes = 0;
119 RFNSM_Status::Instance().set_flag(0);
120 for (int i = 0; i < maxnodes; i++) {
121 sprintf(idname, "%2.2d", idbase + i);
122 if (badlist == NULL ||
123 strstr(badlist, idname) == 0) {
124 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
125 // b2nsm_sendreq(hostnode, "RF_CONFIGURE", 0, pars);
126 b2nsm_sendreq(hostnode, "RC_LOAD", 0, pars);
127 nnodes++;
128 }
129 }
130 while (RFNSM_Status::Instance().get_flag() != nnodes) b2nsm_wait(1);
131
132 sleep(10);
133
134 // 3. Configure event sampler
135 const char* sampler = m_conf->getconf("eventsampler", "nodename");
136 RFNSM_Status::Instance().set_flag(0);
137 // b2nsm_sendreq(distributor, "RF_CONFIGURE", 0, pars);
138 b2nsm_sendreq(sampler, "RC_LOAD", 0, pars);
139 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
140 printf("ERecoMaster:: sampler configured\n");
141
142 sleep(2);
143
144
145 printf("ERecoMaster:: event processors configured\n");
146
147 return 0;
148}
149
150int ERecoMaster::UnConfigure(NSMmsg*, NSMcontext*)
151{
152 int* pars = nullptr;
153
154 // Unconfigure sampler
155 const char* sampler = m_conf->getconf("eventsampler", "nodename");
156 RFNSM_Status::Instance().set_flag(0);
157 // b2nsm_sendreq(distributor, "RF_UNCONFIGURE", 0, pars);
158 b2nsm_sendreq(sampler, "RC_ABORT", 0, pars);
159 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
160 printf("ErecoMaster: sampler unconfigured.\n");
161
162 // Unconfigure event processors
163 int maxnodes = m_conf->getconfi("processor", "nnodes");
164 int idbase = m_conf->getconfi("processor", "idbase");
165 const char* hostbase = m_conf->getconf("processor", "nodebase");
166 char* badlist = m_conf->getconf("processor", "badlist");
167
168 char hostnode[512], idname[3];
169 RFNSM_Status::Instance().set_flag(0);
170 int nnodes = 0;
171 for (int i = 0; i < maxnodes; i++) {
172 sprintf(idname, "%2.2d", idbase + i);
173 if (badlist == NULL ||
174 strstr(badlist, idname) == 0) {
175 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
176 // b2nsm_sendreq(hostnode, "RF_UNCONFIGURE", 0, pars);
177 b2nsm_sendreq(hostnode, "RC_ABORT", 0, pars);
178 nnodes++;
179 }
180 }
181#ifdef DESY
182 b2nsm_wait(5);
183#else
184 while (RFNSM_Status::Instance().get_flag() != nnodes) b2nsm_wait(1);
185#endif
186 printf("ERecoMaster: eventprocessors unconfigured.\n");
187
188 // Unconfigure distributor
189 const char* distributor = m_conf->getconf("distributor", "nodename");
190 RFNSM_Status::Instance().set_flag(0);
191 // b2nsm_sendreq(distributor, "RF_UNCONFIGURE", 0, pars);
192 b2nsm_sendreq(distributor, "RC_ABORT", 0, pars);
193 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
194 printf("ErecoMaster: distributor unconfigured.\n");
195
196 // Unconfigure DqmServer
197 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
198 RFNSM_Status::Instance().set_flag(0);
199 // b2nsm_sendreq(dqmserver, "RF_UNCONFIGURE", 0, pars);
200 b2nsm_sendreq(dqmserver, "RC_ABORT", 0, pars);
201 // while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
202 sleep(5);
203 printf("ErecoMaster: dqmserver unconfigured.\n");
204
205 return 0;
206
207}
208
209int ERecoMaster::Stop(NSMmsg* msg, NSMcontext*)
210{
211 int pars[10];
212 pars[0] = msg->pars[0];
213 pars[1] = msg->pars[1];
214
215 // 1. Stop worker nodes
216 // Unconfigure event processors
217 int maxnodes = m_conf->getconfi("processor", "nnodes");
218 int idbase = m_conf->getconfi("processor", "idbase");
219 const char* hostbase = m_conf->getconf("processor", "nodebase");
220 char* badlist = m_conf->getconf("processor", "badlist");
221
222 char hostnode[512], idname[3];
223 RFNSM_Status::Instance().set_flag(0);
224 int nnodes = 0;
225 for (int i = 0; i < maxnodes; i++) {
226 sprintf(idname, "%2.2d", idbase + i);
227 if (badlist == NULL ||
228 strstr(badlist, idname) == 0) {
229 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
230 // b2nsm_sendreq(hostnode, "RF_STOP", 0, pars);
231 b2nsm_sendreq(hostnode, "RC_STOP", 0, pars);
232 nnodes++;
233 }
234 }
235#ifdef DESY
236 b2nsm_wait(5);
237#else
238 while (RFNSM_Status::Instance().get_flag() != nnodes) b2nsm_wait(1);
239#endif
240
241 // 2. Stop DqmServer node
242 // Unconfigure DqmServer
243 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
244 RFNSM_Status::Instance().set_flag(0);
245 // b2nsm_sendreq(dqmserver, "RF_STOP", 0, pars);
246 b2nsm_sendreq(dqmserver, "RC_STOP", 0, pars);
247 while (RFNSM_Status::Instance().get_flag() == 0) b2nsm_wait(1);
248
249 return 0;
250}
251
252
253int ERecoMaster::Restart(NSMmsg*, NSMcontext*)
254{
255 int* pars = nullptr;
256
257 // 0. Configure DqmServer
258 const char* dqmserver = m_conf->getconf("dqmserver", "nodename");
259 // b2nsm_sendreq(dqmserver, "RF_RESTART", 0, pars);
260 b2nsm_sendreq(dqmserver, "RC_RECOVER", 0, pars);
261 sleep(2);
262
263 // 1. Configure distributor
264 const char* distributor = m_conf->getconf("distributor", "nodename");
265 // b2nsm_sendreq(distributor, "RF_RESTART", 0, pars);
266 b2nsm_sendreq(distributor, "RC_RECOVER", 0, pars);
267 sleep(2);
268
269 // 2. Configure event processors
270 int maxnodes = m_conf->getconfi("processor", "nnodes");
271 int idbase = m_conf->getconfi("processor", "idbase");
272 const char* hostbase = m_conf->getconf("processor", "hostbase");
273 char* badlist = m_conf->getconf("processor", "badlist");
274
275 char hostnode[512], idname[3];
276 for (int i = 0; i < maxnodes; i++) {
277 sprintf(idname, "%2.2d", idbase + i);
278 if (badlist == NULL ||
279 strstr(badlist, idname) == 0) {
280 sprintf(hostnode, "evp_%s%2.2d", hostbase, idbase + i);
281 // b2nsm_sendreq(hostnode, "RF_RESTART", 0, pars);
282 b2nsm_sendreq(hostnode, "RC_RECOVER", 0, pars);
283 }
284 }
285 sleep(2);
286
287 return 0;
288}
289
290// Server function
291
292void ERecoMaster::monitor_loop()
293{
294 while (true) {
295 b2nsm_wait(10);
296 }
297}
298
299
300
Abstract base class for different kinds of events.
STL namespace.