Skip to content

Commit d170c63

Browse files
committed
Add alias to index if required
Signed-off-by: Divya Madala <divyaasm@amazon.com>
1 parent bcb0693 commit d170c63

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

src/main/java/org/opensearchmetrics/lambda/GithubEventsLambda.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.HashMap;
3232
import java.util.List;
3333
import java.util.Map;
34+
import java.util.Optional;
35+
3436

3537
@Slf4j
3638
public class GithubEventsLambda implements RequestHandler<Map<String, String>, Void> {
@@ -96,7 +98,7 @@ public Void handleRequest(Map<String, String> input, Context context) {
9698
}
9799
}
98100
String indexName = "github-user-activity-events-" + collectionCurrentDate.format(DateTimeFormatter.ofPattern("MM-yyyy"));
99-
openSearchUtil.createIndexIfNotExists(indexName);
101+
openSearchUtil.createIndexIfNotExists(indexName, Optional.empty());
100102
openSearchUtil.bulkIndex(indexName, finalEventData);
101103
collectionCurrentDate = collectionCurrentDate.plusDays(1);
102104
}

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void generateGeneralMetrics(List<String> repositories) {
122122
return Stream.of(metricsData);
123123
}))
124124
.collect(Collectors.toMap(MetricsData::getId, metricsData -> metricsData.getJson(metricsData, objectMapper)));
125-
openSearchUtil.createIndexIfNotExists("opensearch_general_metrics");
125+
openSearchUtil.createIndexIfNotExists("opensearch_general_metrics", Optional.empty());
126126
openSearchUtil.bulkIndex("opensearch_general_metrics", metricFinalData);
127127
}
128128

@@ -157,7 +157,7 @@ public void generateLabelMetrics(List<String> repositories) {
157157
});
158158
}))
159159
.collect(Collectors.toMap(LabelData::getId, labelData -> labelData.getJson(labelData, objectMapper)));
160-
openSearchUtil.createIndexIfNotExists("opensearch_label_metrics");
160+
openSearchUtil.createIndexIfNotExists("opensearch_label_metrics", Optional.empty());
161161
openSearchUtil.bulkIndex("opensearch_label_metrics", metricFinalData);
162162
}
163163

@@ -207,7 +207,7 @@ public void generateReleaseMetrics() {
207207
}))
208208
.collect(Collectors.toMap(ReleaseMetricsData::getId,
209209
releaseMetricsData -> releaseMetricsData.getJson(releaseMetricsData, objectMapper)));
210-
openSearchUtil.createIndexIfNotExists("opensearch_release_metrics");
210+
openSearchUtil.createIndexIfNotExists("opensearch_release_metrics", Optional.empty());
211211
openSearchUtil.bulkIndex("opensearch_release_metrics", metricFinalData);
212212
}
213213

@@ -245,7 +245,7 @@ public void generateCodeCovMetrics() {
245245
.collect(Collectors.toMap(CodeCovResult::getId,
246246
codeCovResult -> codeCovResult.getJson(codeCovResult, objectMapper)));
247247
String codeCovIndexName = "opensearch-codecov-metrics-" + currentDate.format(DateTimeFormatter.ofPattern("MM-yyyy"));
248-
openSearchUtil.createIndexIfNotExists(codeCovIndexName);
248+
openSearchUtil.createIndexIfNotExists(codeCovIndexName, Optional.empty());
249249
openSearchUtil.bulkIndex(codeCovIndexName, metricFinalData);
250250
}
251251

@@ -351,7 +351,7 @@ public void generateMaintainerMetrics(List<String> repositories) {
351351
})
352352
.collect(Collectors.toMap(MaintainerData::getId, maintainerData -> maintainerData.getJson(maintainerData, objectMapper)));
353353
String indexName = "maintainer-inactivity-" + currentDate.format(DateTimeFormatter.ofPattern("MM-yyyy"));
354-
openSearchUtil.createIndexIfNotExists(indexName);
354+
openSearchUtil.createIndexIfNotExists(indexName, Optional.of("maintainer-inactivity"));
355355
openSearchUtil.bulkIndex(indexName, metricFinalData);
356356
}
357357
}

src/main/java/org/opensearchmetrics/util/OpenSearchUtil.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.opensearch.client.RestHighLevelClient;
2424
import org.opensearch.client.indices.CreateIndexRequest;
2525
import org.opensearch.client.indices.CreateIndexResponse;
26+
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
27+
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
2628
import org.opensearch.client.indices.GetIndexRequest;
2729
import org.opensearch.common.settings.Settings;
2830
import org.opensearch.common.xcontent.XContentType;
@@ -31,6 +33,7 @@
3133
import java.util.List;
3234
import java.util.Map;
3335
import java.util.Set;
36+
import java.util.Optional;
3437
import java.util.concurrent.ExecutionException;
3538
import java.util.concurrent.ForkJoinPool;
3639
import java.util.concurrent.TimeUnit;
@@ -49,7 +52,7 @@ public OpenSearchUtil(RestHighLevelClient client) {
4952
this.client = client;
5053
}
5154

52-
public void createIndexIfNotExists(String index) {
55+
public void createIndexIfNotExists(String index, Optional<String> aliasName) {
5356
GetIndexRequest getIndexRequest = new GetIndexRequest(index);
5457
try {
5558
if (!client.indices().exists(getIndexRequest, RequestOptions.DEFAULT)) {
@@ -66,16 +69,31 @@ public void createIndexIfNotExists(String index) {
6669
throw new RuntimeException(e);
6770
}
6871
System.out.println("Create index " + createIndexResponse.index() + ", acknowledged = " + createIndexResponse.isAcknowledged() + ", shard acknowledged = " + createIndexResponse.isShardsAcknowledged());
72+
//Adds alias if requested to the index after the index is sucessfully created
73+
if(aliasName.isPresent()) {
74+
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest()
75+
.addAliasAction(
76+
IndicesAliasesRequest.AliasActions.add()
77+
.index(index)
78+
.alias(aliasName.get())
79+
);
80+
try {
81+
client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
82+
System.out.println("Alias is added to the index " + index);
83+
} catch (IOException e) {
84+
throw new RuntimeException(e);
85+
}
86+
}
87+
6988
} else {
7089
System.out.println("Index " + index + " already exists, skip creating index.");
7190
}
91+
7292
} catch (IOException e) {
7393
throw new RuntimeException(e);
7494
}
7595
}
7696

77-
78-
7997
/**
8098
* Bulk index json data into an OpenSearch index.
8199
*

src/test/java/org/opensearchmetrics/lambda/GithubEventsLambdaTest.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.HashMap;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Optional;
34+
3335

3436
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3537
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -73,8 +75,8 @@ public void testHandleRequestYesterday() {
7375
// Assert
7476
String indexNameYesterday = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy"));
7577
String indexNameToday = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy"));
76-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday);
77-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday);
78+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday, Optional.empty());
79+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday, Optional.empty());
7880
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameYesterday), any(Map.class));
7981
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameToday), any(Map.class));
8082
}
@@ -101,11 +103,11 @@ public void testHandleRequestMonthAgo() {
101103

102104
// Assert
103105
String indexNameLastMonth = "github-user-activity-events-" + lastMonth.format(DateTimeFormatter.ofPattern("MM-yyyy"));
104-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameLastMonth);
106+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameLastMonth, Optional.empty());
105107
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameLastMonth), any(Map.class));
106108

107109
String indexNameThisMonth = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy"));
108-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameThisMonth);
110+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameThisMonth, Optional.empty());
109111
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameThisMonth), any(Map.class));
110112
}
111113

@@ -131,8 +133,8 @@ public void testHandleRequestDefault() {
131133
// Assert
132134
String indexNameYesterday = "github-user-activity-events-" + yesterday.format(DateTimeFormatter.ofPattern("MM-yyyy"));
133135
String indexNameToday = "github-user-activity-events-" + today.format(DateTimeFormatter.ofPattern("MM-yyyy"));
134-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday);
135-
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday);
136+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameYesterday, Optional.empty());
137+
verify(openSearchUtil, atLeastOnce()).createIndexIfNotExists(indexNameToday, Optional.empty());
136138
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameYesterday), any(Map.class));
137139
verify(openSearchUtil, atLeastOnce()).bulkIndex(eq(indexNameToday), any(Map.class));
138140
}

src/test/java/org/opensearchmetrics/metrics/MetricsCalculationTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void testGenerateGeneralMetrics() throws IOException {
117117
when(untriagedIssues.performSearch(any(), any())).thenReturn(10L);
118118
when(objectMapper.writeValueAsString(any())).thenReturn("json");
119119
metricsCalculation.generateGeneralMetrics(repositories);
120-
verify(openSearchUtil).createIndexIfNotExists("opensearch_general_metrics");
120+
verify(openSearchUtil).createIndexIfNotExists("opensearch_general_metrics", Optional.empty());
121121
verify(openSearchUtil).bulkIndex(eq("opensearch_general_metrics"), any(Map.class));
122122
}
123123

@@ -136,7 +136,7 @@ void testGenerateLabelMetrics() throws IOException {
136136
when(labelMetrics.getLabelInfo(any(), any())).thenReturn(labelInfo);
137137
when(objectMapper.writeValueAsString(any())).thenReturn("json");
138138
metricsCalculation.generateLabelMetrics(repositories);
139-
verify(openSearchUtil).createIndexIfNotExists("opensearch_label_metrics");
139+
verify(openSearchUtil).createIndexIfNotExists("opensearch_label_metrics", Optional.empty());
140140
verify(openSearchUtil).bulkIndex(eq("opensearch_label_metrics"), any(Map.class));
141141
}
142142

@@ -157,9 +157,9 @@ void testGenerateReleaseMetrics() {
157157
when(releaseMetrics.getReleaseOwners(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn(new String[]{"owner1", "owner2"});
158158
when(releaseMetrics.getReleaseIssue(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn("release-123");
159159
metricsCalculation.generateReleaseMetrics();
160-
verify(openSearchUtil).createIndexIfNotExists("opensearch_release_metrics");
160+
verify(openSearchUtil).createIndexIfNotExists("opensearch_release_metrics", Optional.empty());
161161
verify(openSearchUtil).bulkIndex(eq("opensearch_release_metrics"), ArgumentMatchers.anyMap());
162-
verify(openSearchUtil, times(1)).createIndexIfNotExists("opensearch_release_metrics");
162+
verify(openSearchUtil, times(1)).createIndexIfNotExists("opensearch_release_metrics", Optional.empty());
163163
}
164164

165165
@Test
@@ -187,7 +187,7 @@ void testGenerateCodeCovMetrics() {
187187
throw new RuntimeException(e);
188188
}
189189
metricsCalculation.generateCodeCovMetrics();
190-
verify(openSearchUtil).createIndexIfNotExists(matches("opensearch-codecov-metrics-\\d{2}-\\d{4}"));
190+
verify(openSearchUtil).createIndexIfNotExists(matches("opensearch-codecov-metrics-\\d{2}-\\d{4}"), eq(Optional.empty()));
191191
verify(openSearchUtil).bulkIndex(matches("opensearch-codecov-metrics-\\d{2}-\\d{4}"), argThat(map -> !map.isEmpty()));
192192
verify(releaseMetrics).getCodeCoverage("main", "repo1");
193193
verify(releaseMetrics).getReleaseRepos("2.18.0");
@@ -221,7 +221,7 @@ void testGenerateMaintainerMetrics() throws IOException{
221221
when(maintainerMetrics.calculateInactivity(50L, slopeAndIntercept, lowerBound, latestEventData)).thenReturn(false);
222222
when(objectMapper.writeValueAsString(any())).thenReturn("json");
223223
metricsCalculation.generateMaintainerMetrics(repositories);
224-
verify(openSearchUtil).createIndexIfNotExists(matches("maintainer-inactivity-\\d{2}-\\d{4}"));
224+
verify(openSearchUtil).createIndexIfNotExists(matches("maintainer-inactivity-\\d{2}-\\d{4}"), eq(Optional.of("maintainer-inactivity")));
225225
verify(openSearchUtil).bulkIndex(matches("maintainer-inactivity-\\d{2}-\\d{4}"), argThat(map -> !map.isEmpty()));
226226
}
227227
}

src/test/java/org/opensearchmetrics/util/OpenSearchUtilTest.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import org.opensearch.client.indices.CreateIndexRequest;
2222
import org.opensearch.client.indices.CreateIndexResponse;
2323
import org.opensearch.client.indices.GetIndexRequest;
24+
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
25+
2426

2527
import java.io.IOException;
2628
import java.util.Map;
29+
import java.util.Optional;
30+
2731

2832
import static org.mockito.ArgumentMatchers.any;
2933
import static org.mockito.Mockito.*;
@@ -48,22 +52,23 @@ void setUp() {
4852
void WHEN_index_exists_THEN_doNothing() throws IOException {
4953
when(client.indices()).thenReturn(indicesClient);
5054
when(indicesClient.exists(any(GetIndexRequest.class), any())).thenReturn(true);
51-
openSearchUtil.createIndexIfNotExists("some_index");
55+
openSearchUtil.createIndexIfNotExists("some_index", Optional.empty());
5256

5357
verify(indicesClient, times(0)).create(any(CreateIndexRequest.class), any(RequestOptions.class));
5458
}
5559

5660
@Test
57-
void WHEN_index_not_exist_THEN_create_index() throws IOException {
61+
void WHEN_index_not_exist_THEN_create_index_along_with_alias() throws IOException {
5862

5963
when(client.indices()).thenReturn(indicesClient);
6064
when(indicesClient.exists(any(GetIndexRequest.class), any(RequestOptions.class))).thenReturn(false);
6165
when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class)))
6266
.thenReturn(new CreateIndexResponse(true, true, "some_index"));
63-
openSearchUtil.createIndexIfNotExists("some_index");
67+
openSearchUtil.createIndexIfNotExists("some_index", Optional.of("maintainer-activity"));
6468

6569
verify(indicesClient).exists(any(GetIndexRequest.class), any(RequestOptions.class));
6670
verify(indicesClient).create(any(CreateIndexRequest.class), any(RequestOptions.class));
71+
verify(indicesClient).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class));
6772
verifyNoMoreInteractions(indicesClient);
6873
}
6974

0 commit comments

Comments
 (0)