|
34 | 34 | import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
|
35 | 35 | import org.opensearch.core.xcontent.XContentBuilder;
|
36 | 36 | import org.opensearch.threadpool.ThreadPool;
|
37 |
| -import org.opensearch.timeseries.common.exception.EndRunException; |
38 | 37 | import org.opensearch.timeseries.common.exception.TimeSeriesException;
|
39 | 38 | import org.opensearch.timeseries.indices.IndexManagement;
|
40 | 39 | import org.opensearch.timeseries.indices.TimeSeriesIndex;
|
@@ -109,88 +108,78 @@ public void setFixedDoc(boolean fixedDoc) {
|
109 | 108 | }
|
110 | 109 |
|
111 | 110 | // TODO: check if user has permission to index.
|
112 |
| - public void index(ResultType toSave, String detectorId, String indexOrAliasName) { |
113 |
| - try { |
114 |
| - if (indexOrAliasName != null) { |
115 |
| - if (indexUtils.checkIndicesBlocked(clusterService.state(), ClusterBlockLevel.WRITE, indexOrAliasName)) { |
116 |
| - LOG.warn(String.format(Locale.ROOT, CANNOT_SAVE_ERR_MSG, detectorId)); |
117 |
| - return; |
118 |
| - } |
119 |
| - // We create custom result index when creating a detector. Custom result index can be rolled over and thus we may need to |
120 |
| - // create a new one. |
121 |
| - if (!timeSeriesIndices.doesIndexExist(indexOrAliasName) && !timeSeriesIndices.doesAliasExist(indexOrAliasName)) { |
122 |
| - timeSeriesIndices.initCustomResultIndexDirectly(indexOrAliasName, ActionListener.wrap(response -> { |
123 |
| - if (response.isAcknowledged()) { |
124 |
| - save(toSave, detectorId, indexOrAliasName); |
125 |
| - } else { |
126 |
| - throw new TimeSeriesException( |
127 |
| - detectorId, |
| 111 | + /** |
| 112 | + * Run async index operation. Cannot guarantee index is done after finishing executing the function as several calls |
| 113 | + * in the method are asynchronous. |
| 114 | + * @param toSave Result to save |
| 115 | + * @param configId config id |
| 116 | + * @param indexOrAliasName custom index or alias name |
| 117 | + */ |
| 118 | + public void index(ResultType toSave, String configId, String indexOrAliasName) { |
| 119 | + if (indexOrAliasName != null) { |
| 120 | + if (indexUtils.checkIndicesBlocked(clusterService.state(), ClusterBlockLevel.WRITE, indexOrAliasName)) { |
| 121 | + LOG.warn(String.format(Locale.ROOT, CANNOT_SAVE_ERR_MSG, configId)); |
| 122 | + return; |
| 123 | + } |
| 124 | + // We create custom result index when creating a detector. Custom result index can be rolled over and thus we may need to |
| 125 | + // create a new one. |
| 126 | + if (!timeSeriesIndices.doesIndexExist(indexOrAliasName) && !timeSeriesIndices.doesAliasExist(indexOrAliasName)) { |
| 127 | + timeSeriesIndices.initCustomResultIndexDirectly(indexOrAliasName, ActionListener.wrap(response -> { |
| 128 | + if (response.isAcknowledged()) { |
| 129 | + save(toSave, configId, indexOrAliasName); |
| 130 | + } else { |
| 131 | + LOG |
| 132 | + .error( |
128 | 133 | String
|
129 | 134 | .format(
|
130 | 135 | Locale.ROOT,
|
131 | 136 | "Creating custom result index %s with mappings call not acknowledged",
|
132 | 137 | indexOrAliasName
|
133 | 138 | )
|
134 | 139 | );
|
135 |
| - } |
136 |
| - }, exception -> { |
137 |
| - if (ExceptionsHelper.unwrapCause(exception) instanceof ResourceAlreadyExistsException) { |
138 |
| - // It is possible the index has been created while we sending the create request |
139 |
| - save(toSave, detectorId, indexOrAliasName); |
140 |
| - } else { |
141 |
| - throw new TimeSeriesException( |
142 |
| - detectorId, |
143 |
| - String.format(Locale.ROOT, "cannot create result index %s", indexOrAliasName), |
144 |
| - exception |
145 |
| - ); |
146 |
| - } |
147 |
| - })); |
148 |
| - } else { |
149 |
| - timeSeriesIndices.validateResultIndexMapping(indexOrAliasName, ActionListener.wrap(valid -> { |
150 |
| - if (!valid) { |
151 |
| - throw new EndRunException(detectorId, "wrong index mapping of custom AD result index", true); |
152 |
| - } else { |
153 |
| - save(toSave, detectorId, indexOrAliasName); |
154 |
| - } |
155 |
| - }, exception -> { |
156 |
| - throw new TimeSeriesException( |
157 |
| - detectorId, |
158 |
| - String.format(Locale.ROOT, "cannot validate result index %s", indexOrAliasName), |
159 |
| - exception |
160 |
| - ); |
161 |
| - })); |
162 |
| - } |
| 140 | + } |
| 141 | + }, exception -> { |
| 142 | + if (ExceptionsHelper.unwrapCause(exception) instanceof ResourceAlreadyExistsException) { |
| 143 | + // It is possible the index has been created while we sending the create request |
| 144 | + save(toSave, configId, indexOrAliasName); |
| 145 | + } else { |
| 146 | + LOG.error(String.format(Locale.ROOT, "cannot create result index %s", indexOrAliasName), exception); |
| 147 | + } |
| 148 | + })); |
163 | 149 | } else {
|
164 |
| - if (indexUtils.checkIndicesBlocked(clusterService.state(), ClusterBlockLevel.WRITE, this.defaultResultIndexName)) { |
165 |
| - LOG.warn(String.format(Locale.ROOT, CANNOT_SAVE_ERR_MSG, detectorId)); |
166 |
| - return; |
167 |
| - } |
168 |
| - if (!timeSeriesIndices.doesDefaultResultIndexExist()) { |
169 |
| - timeSeriesIndices |
170 |
| - .initDefaultResultIndexDirectly( |
171 |
| - ActionListener.wrap(initResponse -> onCreateIndexResponse(initResponse, toSave, detectorId), exception -> { |
172 |
| - if (ExceptionsHelper.unwrapCause(exception) instanceof ResourceAlreadyExistsException) { |
173 |
| - // It is possible the index has been created while we sending the create request |
174 |
| - save(toSave, detectorId); |
175 |
| - } else { |
176 |
| - throw new TimeSeriesException( |
177 |
| - detectorId, |
| 150 | + timeSeriesIndices.validateResultIndexMapping(indexOrAliasName, ActionListener.wrap(valid -> { |
| 151 | + if (!valid) { |
| 152 | + LOG.error("wrong index mapping of custom result index"); |
| 153 | + } else { |
| 154 | + save(toSave, configId, indexOrAliasName); |
| 155 | + } |
| 156 | + }, exception -> { LOG.error(String.format(Locale.ROOT, "cannot validate result index %s", indexOrAliasName), exception); }) |
| 157 | + ); |
| 158 | + } |
| 159 | + } else { |
| 160 | + if (indexUtils.checkIndicesBlocked(clusterService.state(), ClusterBlockLevel.WRITE, this.defaultResultIndexName)) { |
| 161 | + LOG.warn(String.format(Locale.ROOT, CANNOT_SAVE_ERR_MSG, configId)); |
| 162 | + return; |
| 163 | + } |
| 164 | + if (!timeSeriesIndices.doesDefaultResultIndexExist()) { |
| 165 | + timeSeriesIndices |
| 166 | + .initDefaultResultIndexDirectly( |
| 167 | + ActionListener.wrap(initResponse -> onCreateIndexResponse(initResponse, toSave, configId), exception -> { |
| 168 | + if (ExceptionsHelper.unwrapCause(exception) instanceof ResourceAlreadyExistsException) { |
| 169 | + // It is possible the index has been created while we sending the create request |
| 170 | + save(toSave, configId); |
| 171 | + } else { |
| 172 | + LOG |
| 173 | + .error( |
178 | 174 | String.format(Locale.ROOT, "Unexpected error creating index %s", defaultResultIndexName),
|
179 | 175 | exception
|
180 | 176 | );
|
181 |
| - } |
182 |
| - }) |
183 |
| - ); |
184 |
| - } else { |
185 |
| - save(toSave, detectorId); |
186 |
| - } |
| 177 | + } |
| 178 | + }) |
| 179 | + ); |
| 180 | + } else { |
| 181 | + save(toSave, configId); |
187 | 182 | }
|
188 |
| - } catch (Exception e) { |
189 |
| - throw new TimeSeriesException( |
190 |
| - detectorId, |
191 |
| - String.format(Locale.ROOT, "Error in saving %s for detector %s", defaultResultIndexName, detectorId), |
192 |
| - e |
193 |
| - ); |
194 | 183 | }
|
195 | 184 | }
|
196 | 185 |
|
|
0 commit comments