Skip to content

Commit 24ec71f

Browse files
committed
update create template methods for elastic8 compatibility
1 parent 75cccb2 commit 24ec71f

File tree

4 files changed

+183
-142
lines changed

4 files changed

+183
-142
lines changed

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/ElasticClientHelper.java

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
import org.phoebus.applications.alarm.messages.AlarmCommandMessage;
2323
import org.phoebus.applications.alarm.messages.AlarmConfigMessage;
2424
import org.phoebus.applications.alarm.messages.AlarmStateMessage;
25+
import org.springframework.beans.factory.annotation.Configurable;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.context.annotation.ComponentScan;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.PropertySource;
2530

2631
import java.io.IOException;
2732
import java.io.InputStream;
@@ -111,7 +116,6 @@ private ElasticClientHelper() {
111116
logger.log(Level.WARNING, "Failed to close the elastic client", ex);
112117
}
113118
}
114-
115119
}
116120

117121
public static ElasticClientHelper getInstance() {
@@ -125,6 +129,11 @@ public ElasticsearchClient getClient() {
125129
return client;
126130
}
127131

132+
/**
133+
* Index an alarm state message
134+
* @param indexName
135+
* @param alarmStateMessage
136+
*/
128137
public void indexAlarmStateDocuments(String indexName, AlarmStateMessage alarmStateMessage) {
129138
try {
130139
stateMessagedQueue.put(new SimpleImmutableEntry<>(indexName,alarmStateMessage));
@@ -133,6 +142,12 @@ public void indexAlarmStateDocuments(String indexName, AlarmStateMessage alarmSt
133142
}
134143
}
135144

145+
/**
146+
* Index an alarm command message
147+
* @param indexName
148+
* @param alarmCommandMessage
149+
* @return
150+
*/
136151
public boolean indexAlarmCmdDocument(String indexName, AlarmCommandMessage alarmCommandMessage) {
137152
IndexRequest<AlarmCommandMessage> indexRequest = new IndexRequest.Builder<AlarmCommandMessage>()
138153
.index(indexName.toLowerCase())
@@ -147,6 +162,11 @@ public boolean indexAlarmCmdDocument(String indexName, AlarmCommandMessage alarm
147162
}
148163
}
149164

165+
/**
166+
* Index an alarm config message
167+
* @param indexName
168+
* @param alarmConfigMessage
169+
*/
150170
public void indexAlarmConfigDocuments(String indexName, AlarmConfigMessage alarmConfigMessage) {
151171
try {
152172
configMessagedQueue.put(new SimpleImmutableEntry<>(indexName,alarmConfigMessage));
@@ -155,6 +175,9 @@ public void indexAlarmConfigDocuments(String indexName, AlarmConfigMessage alarm
155175
}
156176
}
157177

178+
/**
179+
* A helper class which implements 2 queues for allowing bulk logging of state and config messages
180+
*/
158181
private static class flush2Elastic implements Runnable {
159182

160183
private final BlockingQueue<SimpleImmutableEntry<String,AlarmStateMessage>> stateMessagedQueue;
@@ -184,12 +207,12 @@ public void run() {
184207
configMessagedQueue.drainTo(configPairs);
185208
statePairs.forEach( pair -> bulkRequest.operations(op -> op
186209
.index(idx -> idx
187-
.index(pair.getKey())
188-
.document(pair.getValue()))));
210+
.index(pair.getKey().toLowerCase())
211+
.document(pair.getValue().sourceMap()))));
189212
configPairs.forEach( pair -> bulkRequest.operations(op->op
190213
.index(idx->idx
191-
.index(pair.getKey())
192-
.document(pair.getValue()))));
214+
.index(pair.getKey().toLowerCase())
215+
.document(pair.getValue().sourceMap()))));
193216
try {
194217
BulkResponse bulkResponse = client.bulk(bulkRequest.build());
195218
bulkResponse.items().forEach(item -> {
@@ -204,72 +227,78 @@ public void run() {
204227
}
205228
}
206229
}
230+
private static Properties props = new Properties();
231+
{
232+
props.putAll(PropertiesHelper.getProperties());
233+
}
234+
private String ALARM_STATE_TEMPLATE = props.getProperty("elasticsearch.alarm.state.template","alarms_state_template");
235+
private String ALARM_STATE_TEMPLATE_PATTERN = props.getProperty("elasticsearch.alarm.state.template.pattern","*_alarms_state*");
207236

208-
private static final String ALARM_STATE_TEMPLATE = "alarms_state_template";
209-
private static final String ALARM_STATE_TEMPLATE_PATTERN = "*_alarms_state*";
210-
211-
private static final String ALARM_CMD_TEMPLATE = "alarms_cmd_template";
212-
private static final String ALARM_CMD_TEMPLATE_PATTERN = "*_alarms_cmd*";
237+
private String ALARM_CMD_TEMPLATE = props.getProperty("elasticsearch.alarm.cmd.template","alarms_cmd_template");
238+
private String ALARM_CMD_TEMPLATE_PATTERN = props.getProperty("elasticsearch.alarm.cmd.template.pattern","*_alarms_cmd*");
213239

214-
private static final String ALARM_CONFIG_TEMPLATE = "alarms_config_template";
215-
private static final String ALARM_CONFIG_TEMPLATE_PATTERN = "*_alarms_config*";
240+
private String ALARM_CONFIG_TEMPLATE = props.getProperty("elasticsearch.alarm.config.template","alarms_config_template");
241+
private String ALARM_CONFIG_TEMPLATE_PATTERN = props.getProperty("elasticsearch.alarm.config.template.pattern","*_alarms_config*");
216242

217243
public void initializeIndices() throws IOException {
218244
// Create the alarm state messages index template
219-
ExistsTemplateRequest request = new ExistsTemplateRequest.Builder()
220-
.name(ALARM_STATE_TEMPLATE)
221-
.build();
222-
boolean exists = client.indices().existsTemplate(request).value();
223-
224-
if(!exists) {
225-
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_state_template.json");
226-
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
227-
.name(ALARM_STATE_TEMPLATE)
228-
.indexPatterns(Arrays.asList(ALARM_STATE_TEMPLATE_PATTERN))
229-
.withJson(is)
230-
.create(true)
231-
.build();
232-
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
233-
putTemplateResponse.acknowledged();
234-
logger.log( Level.INFO, "Created " + ALARM_STATE_TEMPLATE + " template.");
245+
boolean exists = client.indices().existsIndexTemplate(ExistsIndexTemplateRequest.of(i -> i.name(ALARM_STATE_TEMPLATE))).value();
246+
247+
if (!exists) {
248+
try (InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_state_template.json")) {
249+
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
250+
.name(ALARM_STATE_TEMPLATE)
251+
.indexPatterns(Arrays.asList(ALARM_STATE_TEMPLATE_PATTERN))
252+
.withJson(is)
253+
.priority(1)
254+
.create(true)
255+
.build();
256+
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
257+
putTemplateResponse.acknowledged();
258+
logger.log(Level.INFO, "Created " + ALARM_STATE_TEMPLATE + " template.");
259+
} catch (Exception e) {
260+
logger.log(Level.INFO, "Failed to create template " + ALARM_STATE_TEMPLATE + " template.", e);
261+
}
235262
}
236263

237264
// Create the alarm command messages index template
238-
request = new ExistsTemplateRequest.Builder()
239-
.name(ALARM_CMD_TEMPLATE)
240-
.build();
241-
exists = client.indices().existsTemplate(request).value();
242-
243-
if(!exists) {
244-
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json");
245-
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
246-
.name(ALARM_CMD_TEMPLATE)
247-
.indexPatterns(Arrays.asList(ALARM_CMD_TEMPLATE_PATTERN))
248-
.withJson(is)
249-
.create(true)
250-
.build();
251-
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
252-
putTemplateResponse.acknowledged();
253-
logger.log( Level.INFO, "Created " + ALARM_STATE_TEMPLATE + " template.");
265+
exists = client.indices().existsIndexTemplate(ExistsIndexTemplateRequest.of(i -> i.name(ALARM_CMD_TEMPLATE))).value();
266+
267+
if (!exists) {
268+
try (InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json")) {
269+
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
270+
.name(ALARM_CMD_TEMPLATE)
271+
.indexPatterns(Arrays.asList(ALARM_CMD_TEMPLATE_PATTERN))
272+
.withJson(is)
273+
.priority(2)
274+
.create(true)
275+
.build();
276+
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
277+
putTemplateResponse.acknowledged();
278+
logger.log(Level.INFO, "Created " + ALARM_CMD_TEMPLATE + " template.");
279+
} catch (Exception e) {
280+
logger.log(Level.INFO, "Failed to create template " + ALARM_CMD_TEMPLATE + " template.", e);
281+
}
254282
}
255283

256284
// Create the alarm config messages index template
257-
request = new ExistsTemplateRequest.Builder()
258-
.name(ALARM_CONFIG_TEMPLATE)
259-
.build();
260-
exists = client.indices().existsTemplate(request).value();
261-
262-
if(!exists) {
263-
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json");
264-
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
265-
.name(ALARM_CONFIG_TEMPLATE)
266-
.indexPatterns(Arrays.asList(ALARM_CONFIG_TEMPLATE_PATTERN))
267-
.withJson(is)
268-
.create(true)
269-
.build();
270-
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
271-
putTemplateResponse.acknowledged();
272-
logger.log( Level.INFO, "Created " + ALARM_CONFIG_TEMPLATE + " template.");
285+
exists = client.indices().existsIndexTemplate(ExistsIndexTemplateRequest.of(i -> i.name(ALARM_CONFIG_TEMPLATE))).value();
286+
287+
if (!exists) {
288+
try (InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json")) {
289+
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
290+
.name(ALARM_CONFIG_TEMPLATE)
291+
.indexPatterns(Arrays.asList(ALARM_CONFIG_TEMPLATE_PATTERN))
292+
.withJson(is)
293+
.priority(3)
294+
.create(true)
295+
.build();
296+
PutIndexTemplateResponse putTemplateResponse = client.indices().putIndexTemplate(templateRequest);
297+
putTemplateResponse.acknowledged();
298+
logger.log(Level.INFO, "Created " + ALARM_CONFIG_TEMPLATE + " template.");
299+
} catch (Exception e) {
300+
logger.log(Level.INFO, "Failed to create template " + ALARM_CONFIG_TEMPLATE + " template.", e);
301+
}
273302
}
274303

275304
}
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
{
2-
"properties": {
3-
"APPLICATION-ID": {
4-
"type": "text"
5-
},
6-
"config": {
7-
"type": "keyword"
8-
},
9-
"user": {
10-
"type": "keyword"
11-
},
12-
"host": {
13-
"type": "keyword"
14-
},
15-
"command": {
16-
"type": "keyword"
17-
},
18-
"message_time": {
19-
"type": "date",
20-
"format": "yyyy-MM-dd HH:mm:ss.SSS"
2+
"template": {
3+
"mappings": {
4+
"properties": {
5+
"APPLICATION-ID": {
6+
"type": "text"
7+
},
8+
"config": {
9+
"type": "keyword"
10+
},
11+
"user": {
12+
"type": "keyword"
13+
},
14+
"host": {
15+
"type": "keyword"
16+
},
17+
"command": {
18+
"type": "keyword"
19+
},
20+
"message_time": {
21+
"type": "date",
22+
"format": "yyyy-MM-dd HH:mm:ss.SSS"
23+
}
24+
}
2125
}
2226
}
2327
}
Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
{
2-
"properties": {
3-
"APPLICATION-ID": {
4-
"type": "text"
5-
},
6-
"config": {
7-
"type": "keyword"
8-
},
9-
"user": {
10-
"type": "keyword"
11-
},
12-
"host": {
13-
"type": "keyword"
14-
},
15-
"enabled": {
16-
"type": "keyword"
17-
},
18-
"latching": {
19-
"type": "keyword"
20-
},
21-
"config_msg": {
22-
"type": "keyword"
23-
},
24-
"message_time": {
25-
"type": "date",
26-
"format": "yyyy-MM-dd HH:mm:ss.SSS"
2+
"template": {
3+
"mappings": {
4+
"properties": {
5+
"APPLICATION-ID": {
6+
"type": "text"
7+
},
8+
"config": {
9+
"type": "keyword"
10+
},
11+
"user": {
12+
"type": "keyword"
13+
},
14+
"host": {
15+
"type": "keyword"
16+
},
17+
"enabled": {
18+
"type": "keyword"
19+
},
20+
"latching": {
21+
"type": "keyword"
22+
},
23+
"config_msg": {
24+
"type": "keyword"
25+
},
26+
"message_time": {
27+
"type": "date",
28+
"format": "yyyy-MM-dd HH:mm:ss.SSS"
29+
}
30+
}
2731
}
2832
}
2933
}

0 commit comments

Comments
 (0)