-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliDip2BK.java
More file actions
352 lines (287 loc) · 10.6 KB
/
AliDip2BK.java
File metadata and controls
352 lines (287 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
package alice.dip;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import alice.dip.beam.mode.BeamModeEventsKafkaProducer;
public class AliDip2BK implements Runnable {
public static String Version = "3.0.0 13-Oct-2025";
public static String DNSnode = "dipnsdev.cern.ch";
public static String[] endFillCases = {"CUCU"};
public static boolean LIST_PARAM = false;
static public String LIST_PARAM_PAT = "*";
static public int DEBUG_LEVEL = 1;
static public String OUTPUT_FILE = null;
public static String bookkeepingUrl = "http://localhost:4000";
public static String bookkeepingToken = null;
public static boolean SAVE_PARAMETERS_HISTORY_PER_RUN = false;
public static String KEEP_RUNS_HISTORY_DIRECTORY = null;
public static String KEEP_FILLS_HISTORY_DIRECTORY = null;
public static String KEEP_STATE_DIR = "STATE/";
public static String bootstrapServers = "127.0.0.1:9092";
public static String KAFKAtopic_SOR = "aliecs.env_state.RUNNING";
public static String KAFKAtopic_EOR = "aliecs.env_leave_state.RUNNING";
public static String KAFKA_group_id = "AliDip";
public static String STORE_HIST_FILE_DIR = "HistFiles";
private static boolean simulateDipEvents = false;
public static SimpleDateFormat myDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm");
public static SimpleDateFormat logDateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
public static double DIFF_ENERGY = 5;
public static double DIFF_BETA = 0.001;
public static double DIFF_CURRENT = 5;
public static String ProgPath;
private final long startDate;
public String DipParametersFile = null;
String confFile = "AliDip2BK.properties";
DipClient client;
DipMessagesProcessor dipMessagesProcessor;
BookkeepingClient bookkeepingClient;
StartOfRunKafkaConsumer kcs;
EndOfRunKafkaConsumer kce;
BeamModeEventsKafkaProducer beamModeEventsKafkaProducer;
public AliDip2BK() {
startDate = (new Date()).getTime();
ProgPath = getClass().getClassLoader().getResource(".").getPath();
loadConf(confFile);
showConfig();
verifyDirs();
bookkeepingClient = new BookkeepingClient(bookkeepingUrl, bookkeepingToken);
var luminosityManager = new LuminosityManager();
dipMessagesProcessor = new DipMessagesProcessor(bookkeepingClient, luminosityManager);
if (AliDip2BK.simulateDipEvents) {
new SimDipEventsFill(dipMessagesProcessor);
}
client = new DipClient(DipParametersFile, dipMessagesProcessor);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
kcs = new StartOfRunKafkaConsumer(dipMessagesProcessor);
kce = new EndOfRunKafkaConsumer(dipMessagesProcessor);
beamModeEventsKafkaProducer = new BeamModeEventsKafkaProducer(AliDip2BK.bootstrapServers);
dipMessagesProcessor.setEventsProducer(beamModeEventsKafkaProducer);
shutdownProc();
Thread t = new Thread(this);
t.start();
}
static public void log(int level, String module, String mess) {
if (level >= DEBUG_LEVEL) {
String date = logDateFormat.format((new Date()).getTime());
System.out.println("#" + level + " [" + date + "] " + module + " =>" + mess);
}
}
public static void main(String[] args) {
@SuppressWarnings("unused") AliDip2BK service = new AliDip2BK();
}
public void run() {
int stat_count = 0;
for (; ; ) {
try {
Thread.sleep(10000);
stat_count = stat_count + 10;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (stat_count >= 3600) {
writeStat("StatHist.txt", false);
stat_count = 0;
}
}
}
public void shutdownProc() {
Runtime r = Runtime.getRuntime();
r.addShutdownHook(new Thread() {
public void run() {
log(4, "AliDip2BK", " Main class ENTERS in Shutdown hook");
client.closeSubscriptions();
dipMessagesProcessor.closeInputQueue();
if (dipMessagesProcessor.queueSize() > 0) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (dipMessagesProcessor.queueSize() == 0) break;
}
}
if (dipMessagesProcessor.queueSize() != 0) {
log(4, "AliDip2BK Shutdown", " Data Proc queue is not EMPTY ! Close it anyway ");
} else {
log(2, "AliDip2BK Shutdown", " Data Proc queue is EMPTY and it was correctly closed ");
}
dipMessagesProcessor.saveState();
writeStat("AliDip2BK.stat", true);
beamModeEventsKafkaProducer.close();
log(4, "AliDip2BK", "Beam Mode Events Kafka Producer closed");
}
});
}
public void showConfig() {
String con = "*************************************************\n";
con = con + "* \n";
con = con + "* AkiDip2BK Version =" + Version + "\n";
con = con + "* DIP/DIM =" + DNSnode + "\n";
con = con + "* KAFKA Server = " + bootstrapServers + "\n";
con = con + "* KAFKA Group ID=" + KAFKA_group_id + "\n";
con = con + "* Bookkeeping URL =" + bookkeepingUrl + "\n";
con = con + "* \n";
con = con + "*************************************************\n";
System.out.println(con);
}
private void loadConf(String filename) {
String input = ProgPath + "/" + filename;
Properties prop = new Properties();
try {
prop.load(new FileInputStream(input));
String dns1 = prop.getProperty("DNSnode");
if (dns1 != null) {
DNSnode = dns1;
} else {
log(4, "AliDip2BK.loadConf", " DNSnode is undefined in the conf file ! Use defult=" + DNSnode);
}
String para_file_name = prop.getProperty("DipDataProvidersSubscritionFile");
if (para_file_name != null) {
DipParametersFile = ProgPath + para_file_name;
} else {
log(
4,
"AliDip2BK.loadConf",
" Dip Data Providers Subscription file name is undefined in the conf file "
);
}
String list_param = prop.getProperty("ListDataProvidersPattern");
if (list_param != null) {
LIST_PARAM = true;
LIST_PARAM_PAT = list_param;
} else {
log(
4,
"AliDip2BK.loadConf ",
" List DIP Data Providers Pattern is undefined ! The DIP broswer will not start "
);
}
String debug_n = prop.getProperty("DEBUG_LEVEL");
if (debug_n != null) {
DEBUG_LEVEL = Integer.parseInt(debug_n);
log(1, "AliDip2BK.loadConf ", " Debug Level = " + DEBUG_LEVEL);
}
String out = prop.getProperty("DIP_SUBSCRIPTION_OUTPUT_FILE");
if (out != null) {
OUTPUT_FILE = out;
}
String keh = prop.getProperty("SAVE_PARAMETERS_HISTORY_PER_RUN");
if (keh != null) {
keh = keh.trim();
SAVE_PARAMETERS_HISTORY_PER_RUN = keh.equalsIgnoreCase("Y");
if (keh.equalsIgnoreCase("YES")) SAVE_PARAMETERS_HISTORY_PER_RUN = true;
if (keh.equalsIgnoreCase("true")) SAVE_PARAMETERS_HISTORY_PER_RUN = true;
}
String kfhd = prop.getProperty("KEEP_FILLS_HISTORY_DIRECTORY");
if (kfhd != null) {
KEEP_FILLS_HISTORY_DIRECTORY = kfhd.trim();
}
String krhd = prop.getProperty("KEEP_RUNS_HISTORY_DIRECTORY");
if (krhd != null) {
KEEP_RUNS_HISTORY_DIRECTORY = krhd.trim();
}
String sde = prop.getProperty("SIMULATE_DIP_EVENTS");
if (sde != null) {
if (sde.equalsIgnoreCase("Y")) simulateDipEvents = true;
if (sde.equalsIgnoreCase("YES")) simulateDipEvents = true;
if (sde.equalsIgnoreCase("true")) simulateDipEvents = true;
}
String kgid = prop.getProperty("KAFKA_group_id");
if (kgid != null) {
KAFKA_group_id = kgid;
}
String kbs = prop.getProperty("bootstrapServers");
if (kbs != null) {
bootstrapServers = kbs;
}
String kt1 = prop.getProperty("KAFKAtopic_SOR");
if (kt1 != null) {
KAFKAtopic_SOR = kt1;
}
String kt2 = prop.getProperty("KAFKAtopic_EOR");
if (kt2 != null) {
KAFKAtopic_EOR = kt2;
}
String bkurl = prop.getProperty("BookkeepingURL");
if (bkurl != null) {
bookkeepingUrl = bkurl;
}
String bkpToken = prop.getProperty(("BKP_TOKEN"));
if (bkpToken != null) {
bookkeepingToken = bkpToken;
}
} catch (IOException ex) {
log(4, "AliDip2BK.loadCong", "Failed to access properties file " + ex);
}
}
public void writeStat(String file, boolean final_report) {
String full_file = ProgPath + AliDip2BK.KEEP_STATE_DIR + file;
var stopDate = (new Date()).getTime();
double dur = (double) (stopDate - startDate) / (1000 * 60 * 60);
Runtime rt = Runtime.getRuntime();
long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024;
String mess = "\n\n AliDip2BK Statistics \n";
mess = mess + " Started :" + AliDip2BK.myDateFormat.format(startDate) + "\n";
if (final_report) {
mess = mess + " Stopped :" + AliDip2BK.myDateFormat.format(stopDate) + "\n";
}
mess = mess + " Duration [h]=" + dur + "\n";
mess = mess + " Memory Used [MB]=" + usedMB + "\n";
mess = mess + " No of DIP messages=" + dipMessagesProcessor.statNoDipMess + "\n";
mess = mess + " No of KAFKA messages=" + dipMessagesProcessor.statNoKafMess + "\n";
mess = mess + " No of KAFKA SOR messages=" + kcs.NoMess + "\n";
mess = mess + " No of KAFKA EOR messages=" + kce.NoMess + "\n";
mess = mess + " No of new Fill messgaes =" + dipMessagesProcessor.statNoNewFills + "\n";
mess = mess + " No of new Run messgaes =" + dipMessagesProcessor.statNoNewRuns + "\n";
mess = mess + " No of end Run messages =" + dipMessagesProcessor.statNoEndRuns + "\n";
mess = mess + " No of Duplicated end Run messages =" + dipMessagesProcessor.statNoDuplicateEndRuns + "\n";
try {
File of = new File(full_file);
if (!of.exists()) {
of.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(full_file, true));
writer.write(mess);
writer.close();
} catch (IOException e) {
AliDip2BK.log(4, "ProcData.writeStat", " ERROR writing file=" + full_file + " ex=" + e);
}
}
public void verifyDirs() {
verifyDir(KEEP_RUNS_HISTORY_DIRECTORY);
verifyDir(KEEP_FILLS_HISTORY_DIRECTORY);
verifyDir(STORE_HIST_FILE_DIR);
verifyDir(KEEP_STATE_DIR);
}
public void verifyDir(String name) {
if (name != null) {
File directory = new File(ProgPath + "/" + name);
if (!directory.exists()) {
directory.mkdir();
AliDip2BK.log(2, "AliDip2BK->verifyDir", "created new Directory=" + name);
}
}
}
}