Skip to content

Commit a51df29

Browse files
committed
feat: add logging to ElasticsearchService for better error tracking and index existence checks
1 parent 313e446 commit a51df29

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

backend/src/main/java/com/park/utmstack/service/elasticsearch/ElasticsearchService.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.utmstack.opensearch_connector.types.IndexSort;
2121
import com.utmstack.opensearch_connector.types.SearchSqlResponse;
2222
import com.utmstack.opensearch_connector.types.SqlQueryRequest;
23+
import lombok.extern.slf4j.Slf4j;
2324
import org.opensearch.client.opensearch._types.SortOrder;
2425
import org.opensearch.client.opensearch._types.query_dsl.Query;
2526
import org.opensearch.client.opensearch.cat.indices.IndicesRecord;
@@ -47,6 +48,7 @@
4748
* @author Leonardo M. López
4849
*/
4950
@Service
51+
@Slf4j
5052
public class ElasticsearchService {
5153
private static final String CLASSNAME = "ElasticsearchService";
5254
private final Logger log = LoggerFactory.getLogger(ElasticsearchService.class);
@@ -78,7 +80,7 @@ public List<String> getFieldValues(String keyword, String indexPattern) {
7880
final String ctx = CLASSNAME + ".getFieldValues";
7981
try {
8082
return new ArrayList<>(client.getClient().getFieldValues(keyword, indexPattern,
81-
null, 10000, TermOrder.Count, SortOrder.Desc).keySet());
83+
null, 10000, TermOrder.Count, SortOrder.Desc).keySet());
8284
} catch (Exception e) {
8385
throw new RuntimeException(ctx + ": " + e.getLocalizedMessage());
8486
}
@@ -98,7 +100,7 @@ public Map<String, Long> getFieldValuesWithCount(String field, String index, Lis
98100
final String ctx = CLASSNAME + ".getFieldValuesWithCount";
99101
try {
100102
return client.getClient().getFieldValues(field, index, SearchUtil.toQuery(filters), top,
101-
orderByCount ? TermOrder.Count : TermOrder.Key, sortAsc ? SortOrder.Asc : SortOrder.Desc);
103+
orderByCount ? TermOrder.Count : TermOrder.Key, sortAsc ? SortOrder.Asc : SortOrder.Desc);
102104
} catch (Exception e) {
103105
throw new RuntimeException(ctx + ": " + e.getLocalizedMessage());
104106
}
@@ -143,15 +145,17 @@ public <T> IndexResponse index(String index, T document) {
143145
public List<IndexPropertyType> getIndexProperties(String indexPattern) {
144146
final String ctx = CLASSNAME + ".getIndexProperties";
145147

146-
if (!indexExist(indexPattern))
147-
throw new OpenSearchIndexNotFoundException(ctx + ": Index [" + indexPattern + "] not found");
148+
if (!indexExist(indexPattern)) {
149+
log.info("{} Index pattern {} does not exist", ctx, indexPattern);
150+
return Collections.emptyList();
151+
}
148152

149153
try {
150154
Map<String, String> properties = client.getClient().getIndexProperties(indexPattern);
151155
if (CollectionUtils.isEmpty(properties))
152156
return Collections.emptyList();
153157
return properties.entrySet()
154-
.stream().map(e -> new IndexPropertyType(e.getKey(), e.getValue())).collect(Collectors.toList());
158+
.stream().map(e -> new IndexPropertyType(e.getKey(), e.getValue())).collect(Collectors.toList());
155159
} catch (Exception e) {
156160
throw new RuntimeException(ctx + ": " + e.getMessage());
157161
}
@@ -167,7 +171,7 @@ public List<IndexPropertyType> getIndexProperties(String indexPattern) {
167171
* @throws UtmElasticsearchException In case of any error
168172
*/
169173
public Page<IndicesRecord> getAllIndexes(boolean includeSystemIndex, String pattern, Pageable pageable) throws
170-
UtmElasticsearchException {
174+
UtmElasticsearchException {
171175
final String ctx = CLASSNAME + ".getAllIndexes";
172176
try {
173177
Assert.notNull(pageable, "Argument pageable can't be null");
@@ -179,7 +183,7 @@ public Page<IndicesRecord> getAllIndexes(boolean includeSystemIndex, String patt
179183

180184
if (!includeSystemIndex)
181185
indices = indices.stream().filter(index -> !index.index().startsWith("."))
182-
.collect(Collectors.toList());
186+
.collect(Collectors.toList());
183187

184188
PagedListHolder<IndicesRecord> pageDefinition = new PagedListHolder<>();
185189
pageDefinition.setSource(indices);
@@ -198,7 +202,7 @@ private IndexSort from(Sort sort) {
198202
return IndexSort.unSorted();
199203
IndexSort.Builder sortBuilder = IndexSort.builder();
200204
sort.forEach(order -> sortBuilder.with(IndexSortableProperty.fromJsonValue(order.getProperty()),
201-
order.getDirection().isAscending() ? SortOrder.Asc : SortOrder.Desc));
205+
order.getDirection().isAscending() ? SortOrder.Asc : SortOrder.Desc));
202206
return sortBuilder.build();
203207
} catch (Exception e) {
204208
throw new RuntimeException(ctx + ": " + e.getLocalizedMessage());
@@ -239,14 +243,14 @@ public void preventSystemCrashBySpace() {
239243
return;
240244

241245
UtmSpaceNotificationControl notificationControl = spaceNotificationControlService.findById(1L)
242-
.orElse(new UtmSpaceNotificationControl());
246+
.orElse(new UtmSpaceNotificationControl());
243247
if (Objects.isNull(notificationControl.getId()))
244248
notificationControl.setId(1L);
245249

246250
Instant now = LocalDateTime.now().toInstant(ZoneOffset.UTC);
247251

248252
if (Objects.isNull(notificationControl.getNextNotification()) ||
249-
now.isAfter(notificationControl.getNextNotification())) {
253+
now.isAfter(notificationControl.getNextNotification())) {
250254
mailService.sendLowSpaceEmail(admins, clusterStatus);
251255
notificationControl.setNextNotification(now.plus(24, ChronoUnit.HOURS));
252256
spaceNotificationControlService.save(notificationControl);
@@ -266,7 +270,7 @@ private void deleteOldestIndices() {
266270
final String ctx = CLASSNAME + ".deleteOldestIndices";
267271
try {
268272
List<IndicesRecord> indices = client.getClient().getIndices(Constants.SYS_INDEX_PATTERN.get(SystemIndexPattern.LOGS), IndexSort.builder()
269-
.with(IndexSortableProperty.CreationDate, SortOrder.Asc).build());
273+
.with(IndexSortableProperty.CreationDate, SortOrder.Asc).build());
270274

271275
// If no index that match with log-* was found then te function is terminated
272276
if (CollectionUtils.isEmpty(indices))
@@ -278,10 +282,10 @@ private void deleteOldestIndices() {
278282
// Delete oldest indices
279283
deleteIndex(Collections.singletonList(index.index()));
280284
eventService.createEvent(String.format("Index %1$s was deleted to avoid system crash by space:\n" +
281-
"Creation Date: %2$s\n" +
282-
"Docs Count: %3$s\n" +
283-
"Size: %4$s",
284-
index.index(), index.creationDateString(), index.docsCount(), index.storeSize()), ApplicationEventType.INFO);
285+
"Creation Date: %2$s\n" +
286+
"Docs Count: %3$s\n" +
287+
"Size: %4$s",
288+
index.index(), index.creationDateString(), index.docsCount(), index.storeSize()), ApplicationEventType.INFO);
285289
} catch (Exception e) {
286290
String msg = String.format("%1$s: Fail to delete index: %2$s with message: %3$s", ctx, index.index(), e.getMessage());
287291
eventService.createEvent(msg, ApplicationEventType.WARNING);

0 commit comments

Comments
 (0)