You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by "jkevan (via GitHub)" <gi...@apache.org> on 2023/02/09 13:50:29 UTC

[GitHub] [unomi] jkevan commented on a diff in pull request #575: Unomi 728 index migration

jkevan commented on code in PR #575:
URL: https://github.com/apache/unomi/pull/575#discussion_r1101457622


##########
tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-10-rolloverAndMigrateSession.groovy:
##########
@@ -0,0 +1,74 @@
+import org.apache.unomi.shell.migration.service.MigrationContext
+import org.apache.unomi.shell.migration.utils.HttpUtils
+import org.apache.unomi.shell.migration.utils.MigrationUtils
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+MigrationContext context = migrationContext
+String esAddress = context.getConfigString("esAddress")
+String indexPrefix = context.getConfigString("indexPrefix")
+String newSessionIndex = indexPrefix + "-session-000001"
+String rolloverPolicyName = indexPrefix + "-unomi-rollover-policy"
+String rolloverSessionAlias = indexPrefix + "-session"
+
+
+context.performMigrationStep("2.2.0-10-update-lifecyle-poll-interval", () -> {
+    String updatePollIntervalBody = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/update_settings_poll_interval.json")
+            .replace("#pollIntervalValue", "\"2s\"")
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_cluster/settings", updatePollIntervalBody, null)
+})
+
+context.performMigrationStep("2.2.0-10-create-rollover-policy", () -> {
+    String createRolloverPolicyQuery = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/create_rollover_policy_query.json")
+    String rolloverQueryBody = MigrationUtils.buildRolloverPolicyCreationRequest(createRolloverPolicyQuery, context)
+
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_ilm/policy/" + rolloverPolicyName, rolloverQueryBody, null)
+})

Review Comment:
   There is no need to create the ILM twice, if the event migration already did it.
   It's shared by both sessions and events. (actually it could make sense to handle session and events in the same migration script)



##########
tools/shell-commands/src/main/resources/requestBody/2.2.0/create_rollover_policy_query.json:
##########
@@ -0,0 +1,19 @@
+{
+  "policy": {
+    "phases": {
+      "hot": {
+        "actions": {
+          "rollover": {
+            #rolloverHotActions
+          }
+        }
+      },
+      "delete": {
+        "min_age": "90d",
+        "actions": {
+          "delete": {}
+        }
+      }

Review Comment:
   We really need to not forget about that during the purge story



##########
tools/shell-commands/src/main/resources/requestBody/2.2.0/base_index_mapping.json:
##########
@@ -0,0 +1,30 @@
+{

Review Comment:
   Could be rename to base_index_withRollover_request.json ?



##########
tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-10-rolloverAndMigrateSession.groovy:
##########
@@ -0,0 +1,74 @@
+import org.apache.unomi.shell.migration.service.MigrationContext
+import org.apache.unomi.shell.migration.utils.HttpUtils
+import org.apache.unomi.shell.migration.utils.MigrationUtils
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+MigrationContext context = migrationContext
+String esAddress = context.getConfigString("esAddress")
+String indexPrefix = context.getConfigString("indexPrefix")
+String newSessionIndex = indexPrefix + "-session-000001"
+String rolloverPolicyName = indexPrefix + "-unomi-rollover-policy"
+String rolloverSessionAlias = indexPrefix + "-session"
+
+
+context.performMigrationStep("2.2.0-10-update-lifecyle-poll-interval", () -> {
+    String updatePollIntervalBody = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/update_settings_poll_interval.json")
+            .replace("#pollIntervalValue", "\"2s\"")
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_cluster/settings", updatePollIntervalBody, null)
+})
+
+context.performMigrationStep("2.2.0-10-create-rollover-policy", () -> {
+    String createRolloverPolicyQuery = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/create_rollover_policy_query.json")
+    String rolloverQueryBody = MigrationUtils.buildRolloverPolicyCreationRequest(createRolloverPolicyQuery, context)
+
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_ilm/policy/" + rolloverPolicyName, rolloverQueryBody, null)
+})
+
+context.performMigrationStep("2.2.0-10-create-session-index", () -> {
+    if (!MigrationUtils.indexExists(context.getHttpClient(), esAddress, newSessionIndex)) {
+        String baseRequest = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/base_index_mapping.json")
+        String mapping = MigrationUtils.extractMappingFromBundles(bundleContext, "session.json")
+
+        String newIndexSettings = MigrationUtils.buildIndexCreationRequestWithRollover(baseRequest, mapping, context, rolloverPolicyName, rolloverSessionAlias)
+        HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/" + newSessionIndex, newIndexSettings, null)
+    }
+})
+
+Set<String> sessionIndices = MigrationUtils.getIndexesPrefixedBy(context.getHttpClient(), esAddress, indexPrefix + "-session-date-")
+List<String> sortedIndices = new ArrayList<>(sessionIndices)
+Collections.sort(sortedIndices)
+
+context.performMigrationStep("2.2.0-10-migrate-existing-sessions", () -> {
+    sortedIndices.each { sessionIndex ->
+        MigrationUtils.moveToIndex(context.getHttpClient(), bundleContext, esAddress, sessionIndex, indexPrefix + "-session")
+        sleep(3000)
+    }
+})

Review Comment:
   if something fail in this step it will retry and potentially move again items that have been already moved.
   I think we need a safer retry strategy on this overall reindex steps.
   at the begining of this step we could delete by query all sessions/events in the new indices: context-session-00*



##########
itests/src/test/java/org/apache/unomi/itests/migration/Migrate16xTo220IT.java:
##########
@@ -84,8 +84,16 @@ public void checkMigratedData() throws Exception {
         checkViewEventRestructured();
         checkEventTypesNotPersistedAnymore();
         checkForMappingUpdates();
+        checkNewIndexesExists();
     }
 
+    /**
+     * Checks if at least the new index for events and sessions exists.
+     */
+    private void checkNewIndexesExists() throws IOException {
+        Assert.assertTrue(MigrationUtils.indexExists(httpClient, "http://localhost:9400", "context-event-000001"));
+        Assert.assertTrue(MigrationUtils.indexExists(httpClient, "http://localhost:9400", "context-session-000001"));
+    }

Review Comment:
   Would be great to check that the number of sessions/events is same as before the migration.
   We could do a count query on context-session-* and context-event-* before starting the migration and compare after the migration
   Also we could check that old indices do not exists anymore: context-session-date-* and same for events.



##########
itests/src/test/java/org/apache/unomi/itests/migration/Migrate16xTo220IT.java:
##########
@@ -84,8 +84,16 @@ public void checkMigratedData() throws Exception {
         checkViewEventRestructured();
         checkEventTypesNotPersistedAnymore();
         checkForMappingUpdates();
+        checkNewIndexesExists();

Review Comment:
   would be greate to have more info in the method name: checkEventSessionRollover2_2_0 something like this



##########
tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-10-rolloverAndMigrateSession.groovy:
##########
@@ -0,0 +1,74 @@
+import org.apache.unomi.shell.migration.service.MigrationContext
+import org.apache.unomi.shell.migration.utils.HttpUtils
+import org.apache.unomi.shell.migration.utils.MigrationUtils
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+MigrationContext context = migrationContext
+String esAddress = context.getConfigString("esAddress")
+String indexPrefix = context.getConfigString("indexPrefix")
+String newSessionIndex = indexPrefix + "-session-000001"
+String rolloverPolicyName = indexPrefix + "-unomi-rollover-policy"
+String rolloverSessionAlias = indexPrefix + "-session"
+
+
+context.performMigrationStep("2.2.0-10-update-lifecyle-poll-interval", () -> {
+    String updatePollIntervalBody = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/update_settings_poll_interval.json")
+            .replace("#pollIntervalValue", "\"2s\"")
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_cluster/settings", updatePollIntervalBody, null)
+})
+
+context.performMigrationStep("2.2.0-10-create-rollover-policy", () -> {
+    String createRolloverPolicyQuery = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/create_rollover_policy_query.json")
+    String rolloverQueryBody = MigrationUtils.buildRolloverPolicyCreationRequest(createRolloverPolicyQuery, context)
+
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_ilm/policy/" + rolloverPolicyName, rolloverQueryBody, null)
+})
+
+context.performMigrationStep("2.2.0-10-create-session-index", () -> {
+    if (!MigrationUtils.indexExists(context.getHttpClient(), esAddress, newSessionIndex)) {
+        String baseRequest = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/base_index_mapping.json")
+        String mapping = MigrationUtils.extractMappingFromBundles(bundleContext, "session.json")
+
+        String newIndexSettings = MigrationUtils.buildIndexCreationRequestWithRollover(baseRequest, mapping, context, rolloverPolicyName, rolloverSessionAlias)
+        HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/" + newSessionIndex, newIndexSettings, null)
+    }
+})
+
+Set<String> sessionIndices = MigrationUtils.getIndexesPrefixedBy(context.getHttpClient(), esAddress, indexPrefix + "-session-date-")
+List<String> sortedIndices = new ArrayList<>(sessionIndices)
+Collections.sort(sortedIndices)
+
+context.performMigrationStep("2.2.0-10-migrate-existing-sessions", () -> {
+    sortedIndices.each { sessionIndex ->
+        MigrationUtils.moveToIndex(context.getHttpClient(), bundleContext, esAddress, sessionIndex, indexPrefix + "-session")
+        sleep(3000)
+    }
+})
+
+context.performMigrationStep("2.2.0-10-remove-old-sessions-indices", () -> {
+    sortedIndices.each { sessionIndex ->
+        MigrationUtils.deleteIndex(context.getHttpClient(), esAddress, sessionIndex)
+    }
+})
+
+context.performMigrationStep("2.2.0-10-reset-poll-interval", () -> {
+    String updatePollIntervalBody = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.2.0/update_settings_poll_interval.json")
+            .replace("#pollIntervalValue", "null")
+    HttpUtils.executePutRequest(context.getHttpClient(), esAddress + "/_cluster/settings", updatePollIntervalBody, null)
+})

Review Comment:
   Could be done once if sessions/events are handle in same migration script.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@unomi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org