Skip to content

Commit a3bf0f0

Browse files
committed
Increase JavaDoc coverage and update PR based comments
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent 6f0ccba commit a3bf0f0

20 files changed

+429
-101
lines changed

gradle/missing-javadoc.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ configure([
141141
project(":plugins:mapper-annotated-text"),
142142
project(":plugins:mapper-murmur3"),
143143
project(":plugins:mapper-size"),
144-
project(":plugins:query-insights"),
145144
project(":plugins:repository-azure"),
146145
project(":plugins:repository-gcs"),
147146
project(":plugins:repository-hdfs"),

plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporter.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
* @opensearch.internal
2121
*/
2222
public abstract class QueryInsightsExporter<T extends SearchQueryRecord<?>> {
23-
private QueryInsightsExporterType type;
24-
private String identifier;
23+
private final String identifier;
2524

26-
QueryInsightsExporter(QueryInsightsExporterType type, String identifier) {
27-
this.type = type;
25+
QueryInsightsExporter(String identifier) {
2826
this.identifier = identifier;
2927
}
3028

@@ -35,18 +33,10 @@ public abstract class QueryInsightsExporter<T extends SearchQueryRecord<?>> {
3533
*/
3634
public abstract void export(List<T> records) throws Exception;
3735

38-
public void setType(QueryInsightsExporterType type) {
39-
this.type = type;
40-
}
41-
42-
public QueryInsightsExporterType getType() {
43-
return type;
44-
}
45-
46-
public void setIdentifier(String identifier) {
47-
this.identifier = identifier;
48-
}
49-
36+
/**
37+
* Get the identifier of this exporter
38+
* @return identifier of this exporter
39+
*/
5040
public String getIdentifier() {
5141
return identifier;
5242
}

plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterType.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
* @opensearch.internal
1717
*/
1818
public enum QueryInsightsExporterType {
19-
/* local index exporter */
20-
LOCAL_INDEX("local_index");
19+
/** local index exporter */
20+
LOCAL_INDEX;
2121

22-
private final String type;
23-
24-
QueryInsightsExporterType(String type) {
25-
this.type = type;
22+
@Override
23+
public String toString() {
24+
return super.toString().toLowerCase(Locale.ROOT);
2625
}
2726

27+
/**
28+
* Parse QueryInsightsExporterType from String
29+
* @param type the String representation of the QueryInsightsExporterType
30+
* @return QueryInsightsExporterType
31+
*/
2832
public static QueryInsightsExporterType parse(String type) {
2933
return valueOf(type.toUpperCase(Locale.ROOT));
3034
}

plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsLocalIndexExporter.java

+42-36
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ public class QueryInsightsLocalIndexExporter<T extends SearchQueryRecord<?>> ext
5151
/** The mapping for the local index that holds the data */
5252
private final InputStream localIndexMapping;
5353

54+
/**
55+
* Create a QueryInsightsLocalIndexExporter Object
56+
* @param clusterService The clusterService of the node
57+
* @param client The OpenSearch Client to support index operations
58+
* @param localIndexName The local index name to export the data to
59+
* @param localIndexMapping The mapping for the local index
60+
*/
5461
public QueryInsightsLocalIndexExporter(
5562
ClusterService clusterService,
5663
Client client,
5764
String localIndexName,
5865
InputStream localIndexMapping
5966
) {
60-
super(QueryInsightsExporterType.LOCAL_INDEX, localIndexName);
67+
super(localIndexName);
6168
this.clusterService = clusterService;
6269
this.client = client;
6370
this.localIndexMapping = localIndexMapping;
@@ -70,42 +77,35 @@ public QueryInsightsLocalIndexExporter(
7077
* @throws IOException if an error occurs
7178
*/
7279
@Override
73-
public synchronized void export(List<T> records) throws IOException {
80+
public void export(List<T> records) throws IOException {
7481
if (records.size() == 0) {
7582
return;
7683
}
77-
if (checkIfIndexExists()) {
78-
bulkRecord(records);
79-
} else {
80-
// local index not exist
81-
initLocalIndex(new ActionListener<>() {
82-
@Override
83-
public void onResponse(CreateIndexResponse response) {
84-
if (response.isAcknowledged()) {
85-
log.debug(
86-
String.format(Locale.ROOT, "successfully initialized local index %s for query insight.", getIdentifier())
87-
);
88-
try {
89-
bulkRecord(records);
90-
} catch (IOException e) {
91-
log.error(String.format(Locale.ROOT, "fail to ingest query insight data to local index, error: %s", e));
92-
}
93-
} else {
94-
log.error(
95-
String.format(
96-
Locale.ROOT,
97-
"request to created local index %s for query insight not acknowledged.",
98-
getIdentifier()
99-
)
100-
);
84+
boolean indexExists = checkAndInitLocalIndex(new ActionListener<>() {
85+
@Override
86+
public void onResponse(CreateIndexResponse response) {
87+
if (response.isAcknowledged()) {
88+
log.debug(String.format(Locale.ROOT, "successfully initialized local index %s for query insight.", getIdentifier()));
89+
try {
90+
bulkRecord(records);
91+
} catch (IOException e) {
92+
log.error(String.format(Locale.ROOT, "fail to ingest query insight data to local index, error: %s", e));
10193
}
94+
} else {
95+
log.error(
96+
String.format(Locale.ROOT, "request to created local index %s for query insight not acknowledged.", getIdentifier())
97+
);
10298
}
99+
}
103100

104-
@Override
105-
public void onFailure(Exception e) {
106-
log.error(String.format(Locale.ROOT, "error creating local index for query insight: %s", e));
107-
}
108-
});
101+
@Override
102+
public void onFailure(Exception e) {
103+
log.error(String.format(Locale.ROOT, "error creating local index for query insight: %s", e));
104+
}
105+
});
106+
107+
if (indexExists) {
108+
bulkRecord(records);
109109
}
110110
}
111111

@@ -120,15 +120,21 @@ private boolean checkIfIndexExists() {
120120
}
121121

122122
/**
123-
* Initialize the local OpenSearch Index for the exporter
123+
* Check and initialize the local OpenSearch Index for the exporter
124124
*
125125
* @param listener the listener to be notified upon completion
126+
* @return boolean to represent if the index has already been created before calling this function
126127
* @throws IOException if an error occurs
127128
*/
128-
private synchronized void initLocalIndex(ActionListener<CreateIndexResponse> listener) throws IOException {
129-
CreateIndexRequest createIndexRequest = new CreateIndexRequest(this.getIdentifier()).mapping(getIndexMappings())
130-
.settings(Settings.builder().put("index.hidden", false).build());
131-
client.admin().indices().create(createIndexRequest, listener);
129+
private synchronized boolean checkAndInitLocalIndex(ActionListener<CreateIndexResponse> listener) throws IOException {
130+
if (!checkIfIndexExists()) {
131+
CreateIndexRequest createIndexRequest = new CreateIndexRequest(this.getIdentifier()).mapping(getIndexMappings())
132+
.settings(Settings.builder().put("index.hidden", false).build());
133+
client.admin().indices().create(createIndexRequest, listener);
134+
return false;
135+
} else {
136+
return true;
137+
}
132138
}
133139

134140
/**

plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/listener/SearchQueryLatencyListener.java

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public final class SearchQueryLatencyListener extends SearchRequestOperationsLis
4343

4444
private final TopQueriesByLatencyService topQueriesByLatencyService;
4545

46+
/**
47+
* Constructor for SearchQueryLatencyListener
48+
*
49+
* @param clusterService The Node's cluster service.
50+
* @param topQueriesByLatencyService The topQueriesByLatencyService associated with this listener
51+
*/
4652
@Inject
4753
public SearchQueryLatencyListener(ClusterService clusterService, TopQueriesByLatencyService topQueriesByLatencyService) {
4854
this.topQueriesByLatencyService = topQueriesByLatencyService;

plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public abstract class QueryInsightsService<R extends SearchQueryRecord<?>, S ext
4646
/** enable insight data export */
4747
private boolean enableExport;
4848

49-
/** The internal store that holds the query insight data */
49+
/** The internal thread-safe store that holds the query insight data */
5050
@Nullable
5151
protected S store;
5252

@@ -59,8 +59,19 @@ public abstract class QueryInsightsService<R extends SearchQueryRecord<?>, S ext
5959

6060
/** The internal OpenSearch thread pool that execute async processing and exporting tasks*/
6161
protected final ThreadPool threadPool;
62+
63+
/**
64+
* Holds a reference to delayed operation {@link Scheduler.Cancellable} so it can be cancelled when
65+
* the service closed concurrently.
66+
*/
6267
protected volatile Scheduler.Cancellable scheduledFuture;
6368

69+
/**
70+
* Create the Query Insights Service object
71+
* @param threadPool The OpenSearch thread pool to run async tasks
72+
* @param store The in memory store to keep the Query Insights data
73+
* @param exporter The optional {@link QueryInsightsExporter} to export the Query Insights data
74+
*/
6475
@Inject
6576
public QueryInsightsService(ThreadPool threadPool, @Nullable S store, @Nullable E exporter) {
6677
this.threadPool = threadPool;
@@ -102,7 +113,13 @@ public List<R> getQueryData() throws IllegalArgumentException {
102113
public abstract void clearOutdatedData();
103114

104115
/**
105-
* Restart the exporter with new config
116+
* Reset the exporter with new config
117+
*
118+
* This function can be used to enable/disable an exporter, change the type of the exporter,
119+
* or change the identifier of the exporter.
120+
* @param enabled the enable flag to set on the exporter
121+
* @param type The QueryInsightsExporterType to set on the exporter
122+
* @param identifier the Identifier to set on the exporter
106123
*/
107124
public abstract void resetExporter(boolean enabled, QueryInsightsExporterType type, String identifier);
108125

@@ -113,18 +130,34 @@ public void clearAllData() {
113130
store.clear();
114131
}
115132

133+
/**
134+
* Set flag to enable or disable Query Insights data collection
135+
* @param enableCollect Flag to enable or disable Query Insights data collection
136+
*/
116137
public void setEnableCollect(boolean enableCollect) {
117138
this.enableCollect = enableCollect;
118139
}
119140

141+
/**
142+
* Get if the Query Insights data collection is enabled
143+
* @return if the Query Insights data collection is enabled
144+
*/
120145
public boolean getEnableCollect() {
121146
return this.enableCollect;
122147
}
123148

149+
/**
150+
* Set flag to enable or disable Query Insights data export
151+
* @param enableExport
152+
*/
124153
public void setEnableExport(boolean enableExport) {
125154
this.enableExport = enableExport;
126155
}
127156

157+
/**
158+
* Get if the Query Insights data export is enabled
159+
* @return if the Query Insights data export is enabled
160+
*/
128161
public boolean getEnableExport() {
129162
return this.enableExport;
130163
}
@@ -156,7 +189,6 @@ private void doExport() {
156189
List<R> storedData = getQueryData();
157190
try {
158191
exporter.export(storedData);
159-
log.debug(String.format(Locale.ROOT, "finish exporting query insight data to sink %s", storedData));
160192
} catch (Exception e) {
161193
throw new RuntimeException(String.format(Locale.ROOT, "failed to export query insight data to sink, error: %s", e));
162194
}
@@ -165,6 +197,10 @@ private void doExport() {
165197
@Override
166198
protected void doClose() {}
167199

200+
/**
201+
* Get the export interval set for the {@link QueryInsightsExporter}
202+
* @return export interval
203+
*/
168204
public TimeValue getExportInterval() {
169205
return exportInterval;
170206
}

0 commit comments

Comments
 (0)