Skip to content

Commit 974de6b

Browse files
author
Seung Yeon Joo
committed
Fixing Lint Check
Signed-off-by: Seung Yeon Joo <[email protected]>
1 parent 5a9f2c3 commit 974de6b

File tree

3 files changed

+107
-92
lines changed

3 files changed

+107
-92
lines changed

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ConvertIndexToRemoteAction.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class ConvertIndexToRemoteAction(
4545
builder.field(SNAPSHOT_FIELD, snapshot)
4646
builder.field(INCLUDE_ALIASES_FIELD, includeAliases)
4747
builder.field(IGNORE_INDEX_SETTINGS_FIELD, ignoreIndexSettings)
48-
if (numberOfReplicas != 0) {
49-
builder.field(NUMBER_OF_REPLICAS_FIELD, numberOfReplicas)
50-
}
48+
builder.field(NUMBER_OF_REPLICAS_FIELD, numberOfReplicas)
5149
builder.endObject()
5250
}
5351

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/restore/AttemptRestoreStep.kt

Lines changed: 85 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.opensearch.indexmanagement.opensearchapi.suspendUntil
2424
import org.opensearch.indexmanagement.spi.indexstatemanagement.Step
2525
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionProperties
2626
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData
27+
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext
2728
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepMetaData
2829
import org.opensearch.script.Script
2930
import org.opensearch.script.ScriptService
@@ -40,7 +41,7 @@ class AttemptRestoreStep(private val action: ConvertIndexToRemoteAction) : Step(
4041
private var info: Map<String, Any>? = null
4142
private var snapshotName: String? = null
4243

43-
@Suppress("TooGenericExceptionCaught", "ComplexMethod", "ReturnCount", "LongMethod")
44+
@Suppress("TooGenericExceptionCaught", "ComplexMethod", "ReturnCount", "LongMethod", "NestedBlockDepth")
4445
override suspend fun execute(): Step {
4546
val context = this.context ?: return this
4647
val managedIndexMetadata = context.metadata
@@ -95,74 +96,14 @@ class AttemptRestoreStep(private val action: ConvertIndexToRemoteAction) : Step(
9596
val remoteIndexName = "${indexName}_remote"
9697

9798
// Check if remote index exists
98-
var remoteIndexExists = false
99-
try {
100-
val existsResponse: IndicesExistsResponse = context.client.admin().indices().suspendUntil {
101-
exists(IndicesExistsRequest(remoteIndexName), it)
102-
}
103-
remoteIndexExists = existsResponse.isExists
104-
} catch (e: Exception) {
105-
// Index doesn't exist yet
106-
}
99+
val remoteIndexExists = checkRemoteIndexExists(context, remoteIndexName)
107100

108101
if (remoteIndexExists) {
109102
// Restore completed, mark as completed
110103
stepStatus = StepStatus.COMPLETED
111104
mutableInfo["message"] = getSuccessMessage(indexName)
112105
} else {
113-
// Proceed with the restore operation
114-
val restoreSnapshotRequest = RestoreSnapshotRequest(repository, snapshotName)
115-
.indices(indexName)
116-
.storageType(RestoreSnapshotRequest.StorageType.REMOTE_SNAPSHOT)
117-
.renamePattern("^(.*)\$")
118-
.renameReplacement("$1_remote")
119-
.waitForCompletion(false)
120-
.includeAliases(action.includeAliases)
121-
.ignoreIndexSettings(action.ignoreIndexSettings)
122-
123-
// Set number_of_replicas (defaults to 0 if not specified)
124-
val indexSettings = Settings.builder()
125-
.put(SETTING_NUMBER_OF_REPLICAS, action.numberOfReplicas)
126-
.build()
127-
restoreSnapshotRequest.indexSettings(indexSettings)
128-
129-
val response: RestoreSnapshotResponse = context.client.admin().cluster().suspendUntil {
130-
restoreSnapshot(restoreSnapshotRequest, it)
131-
}
132-
133-
when (response.status()) {
134-
RestStatus.ACCEPTED, RestStatus.OK -> {
135-
// Restore accepted, delete original index
136-
try {
137-
val deleteResponse: AcknowledgedResponse = context.client.admin().indices().suspendUntil {
138-
delete(DeleteIndexRequest(indexName), it)
139-
}
140-
if (deleteResponse.isAcknowledged) {
141-
logger.info("Successfully deleted original index [$indexName] after restore was accepted")
142-
mutableInfo["deleted_original_index"] = true
143-
} else {
144-
logger.warn("Delete request for original index [$indexName] was not acknowledged")
145-
mutableInfo["deleted_original_index"] = false
146-
}
147-
} catch (e: Exception) {
148-
logger.warn("Failed to delete original index [$indexName] after restore was accepted: ${e.message}", e)
149-
mutableInfo["deleted_original_index"] = false
150-
mutableInfo["delete_error"] = e.message ?: "Unknown error"
151-
}
152-
153-
// Mark as waiting for completion
154-
stepStatus = StepStatus.CONDITION_NOT_MET
155-
mutableInfo["message"] = "Waiting for remote index [$remoteIndexName] to be created"
156-
logger.info("Restore accepted for snapshot [$snapshotName], waiting for remote index [$remoteIndexName] to be created")
157-
}
158-
else -> {
159-
val message = getFailedMessage(indexName, "Unexpected response status: ${response.status()}")
160-
logger.warn("$message - $response")
161-
stepStatus = StepStatus.FAILED
162-
mutableInfo["message"] = message
163-
mutableInfo["cause"] = response.toString()
164-
}
165-
}
106+
performRestore(context, indexName, remoteIndexName, repository, snapshotName, mutableInfo)
166107
}
167108
info = mutableInfo.toMap()
168109
} catch (e: RemoteTransportException) {
@@ -181,6 +122,87 @@ class AttemptRestoreStep(private val action: ConvertIndexToRemoteAction) : Step(
181122
return this
182123
}
183124

125+
private suspend fun checkRemoteIndexExists(context: StepContext, remoteIndexName: String): Boolean =
126+
try {
127+
val existsResponse: IndicesExistsResponse = context.client.admin().indices().suspendUntil {
128+
exists(IndicesExistsRequest(remoteIndexName), it)
129+
}
130+
existsResponse.isExists
131+
} catch (e: Exception) {
132+
// Index doesn't exist yet
133+
false
134+
}
135+
136+
private suspend fun performRestore(
137+
context: StepContext,
138+
indexName: String,
139+
remoteIndexName: String,
140+
repository: String,
141+
snapshotName: String?,
142+
mutableInfo: MutableMap<String, Any>,
143+
) {
144+
// Proceed with the restore operation
145+
val restoreSnapshotRequest = RestoreSnapshotRequest(repository, snapshotName)
146+
.indices(indexName)
147+
.storageType(RestoreSnapshotRequest.StorageType.REMOTE_SNAPSHOT)
148+
.renamePattern("^(.*)\$")
149+
.renameReplacement("$1_remote")
150+
.waitForCompletion(false)
151+
.includeAliases(action.includeAliases)
152+
.ignoreIndexSettings(action.ignoreIndexSettings)
153+
154+
// Set number_of_replicas (defaults to 0 if not specified)
155+
val indexSettings = Settings.builder()
156+
.put(SETTING_NUMBER_OF_REPLICAS, action.numberOfReplicas)
157+
.build()
158+
restoreSnapshotRequest.indexSettings(indexSettings)
159+
160+
val response: RestoreSnapshotResponse = context.client.admin().cluster().suspendUntil {
161+
restoreSnapshot(restoreSnapshotRequest, it)
162+
}
163+
164+
when (response.status()) {
165+
RestStatus.ACCEPTED, RestStatus.OK -> {
166+
deleteOriginalIndex(context, indexName, mutableInfo)
167+
// Mark as waiting for completion
168+
stepStatus = StepStatus.CONDITION_NOT_MET
169+
mutableInfo["message"] = "Waiting for remote index [$remoteIndexName] to be created"
170+
logger.info("Restore accepted for snapshot [$snapshotName], waiting for remote index [$remoteIndexName] to be created")
171+
}
172+
else -> {
173+
val message = getFailedMessage(indexName, "Unexpected response status: ${response.status()}")
174+
logger.warn("$message - $response")
175+
stepStatus = StepStatus.FAILED
176+
mutableInfo["message"] = message
177+
mutableInfo["cause"] = response.toString()
178+
}
179+
}
180+
}
181+
182+
private suspend fun deleteOriginalIndex(
183+
context: StepContext,
184+
indexName: String,
185+
mutableInfo: MutableMap<String, Any>,
186+
) {
187+
// Restore accepted, delete original index
188+
try {
189+
val deleteResponse: AcknowledgedResponse = context.client.admin().indices().suspendUntil {
190+
delete(DeleteIndexRequest(indexName), it)
191+
}
192+
if (deleteResponse.isAcknowledged) {
193+
logger.info("Successfully deleted original index [$indexName] after restore was accepted")
194+
mutableInfo["deleted_original_index"] = true
195+
} else {
196+
logger.warn("Delete request for original index [$indexName] was not acknowledged")
197+
mutableInfo["deleted_original_index"] = false
198+
}
199+
} catch (e: Exception) {
200+
logger.warn("Failed to delete original index [$indexName] after restore was accepted: ${e.message}", e)
201+
mutableInfo["deleted_original_index"] = false
202+
mutableInfo["delete_error"] = e.message ?: "Unknown error"
203+
}
204+
}
205+
184206
private fun compileTemplate(
185207
template: Script,
186208
managedIndexMetaData: ManagedIndexMetaData,

src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/ConvertIndexToRemoteActionIT.kt

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,36 +131,17 @@ class ConvertIndexToRemoteActionIT : IndexStateManagementRestTestCase() {
131131
explainMetaData.info?.get("message"),
132132
)
133133
} catch (e: ResponseException) {
134-
// If we get a 400 "no documents to get", the index was deleted (expected after restore)
135-
if (e.response.restStatus() == RestStatus.BAD_REQUEST) {
136-
val errorBody = e.response.asMap()
137-
val error = errorBody["error"] as? Map<*, *>
138-
val reason = error?.get("reason") as? String
139-
if (reason?.contains("no documents to get") == true) {
140-
// Index was deleted, which is expected - restore succeeded
141-
// Just verify remote index exists below
142-
return@waitFor
143-
}
144-
}
145-
throw e // Re-throw if it's a different error
134+
handleIndexDeletedException(e)
135+
// Index was deleted, which is expected - restore succeeded
136+
// Just verify remote index exists below
137+
return@waitFor
146138
}
147139
}
148140
}
149141
} catch (e: ResponseException) {
150-
// If we get a 400 "no documents to get", the index was deleted (expected after restore)
151-
if (e.response.restStatus() == RestStatus.BAD_REQUEST) {
152-
val errorBody = e.response.asMap()
153-
val error = errorBody["error"] as? Map<*, *>
154-
val reason = error?.get("reason") as? String
155-
if (reason?.contains("no documents to get") == true) {
156-
// Index was deleted, which is expected - restore succeeded
157-
// Continue to verify remote index exists
158-
} else {
159-
throw e // Re-throw if it's a different error
160-
}
161-
} else {
162-
throw e // Re-throw if it's not a 400
163-
}
142+
handleIndexDeletedException(e)
143+
// Index was deleted, which is expected - restore succeeded
144+
// Continue to verify remote index exists
164145
}
165146

166147
val remoteIndexName = "${indexName}_remote"
@@ -169,4 +150,18 @@ class ConvertIndexToRemoteActionIT : IndexStateManagementRestTestCase() {
169150
val isRemote = isIndexRemote(remoteIndexName)
170151
assertTrue("Index $remoteIndexName is not a remote index", isRemote)
171152
}
153+
154+
private fun handleIndexDeletedException(e: ResponseException) {
155+
// If we get a 400 "no documents to get", the index was deleted (expected after restore)
156+
if (e.response.restStatus() == RestStatus.BAD_REQUEST) {
157+
val errorBody = e.response.asMap()
158+
val error = errorBody["error"] as? Map<*, *>
159+
val reason = error?.get("reason") as? String
160+
if (reason?.contains("no documents to get") != true) {
161+
throw e // Re-throw if it's a different error
162+
}
163+
} else {
164+
throw e // Re-throw if it's not a 400
165+
}
166+
}
172167
}

0 commit comments

Comments
 (0)