没有不值得去解决的问题,也没有不值得去学习的技术!

Troubleshooting Database Migration Errors Caused by Duplicate Execution When Upgrading Containers in Rancher

The time to wait before starting the probe was changed from 10 seconds to 100 seconds.

1. When upgrading the container in Rancher for the first time, the database migration executed repeatedly, causing an error and resulting in an upgrade failure. See Figure 1.

When upgrading the container in Rancher for the first time, the database migration executed repeatedly, causing an error and resulting in an upgrade failure.
Figure 1
Plaintext
*** applying m210106_122427_add_columns_to_task_table

> add column article_type_id integer NOT NULL DEFAULT 0 COMMENT '文章类型ID' AFTER `type` to table {{%task}} ...Exception: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'article_type_id'

The SQL being executed was: ALTER TABLE `cpa_task` ADD `article_type_id` int(11) NOT NULL DEFAULT 0 COMMENT '文章类型ID' AFTER `type` (/mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Schema.php:678)

#0 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Command.php(1304): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE `cp...')

#1 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Command.php(1099): yii\db\Command->internalExecute('ALTER TABLE `cp...')

#2 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Migration.php(377): yii\db\Command->execute()

#3 /mcloud/www/ccp_api/console/migrations/m210106_122427_add_columns_to_task_table.php(15): yii\db\Migration->addColumn('{{%task}}', 'article_type_id', Object(yii\db\mysql\ColumnSchemaBuilder))

#4 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Migration.php(114): m210106_122427_add_columns_to_task_table->safeUp()

#5 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(744): yii\db\Migration->up()

#6 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(202): yii\console\controllers\BaseMigrateController->migrateUp('m210106_122427_...')

#7 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)

#8 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#9 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)

#10 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('', Array)

#11 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('', Array)

#12 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('migrate', Array)

#13 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('migrate', Array)

#14 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))

#15 /mcloud/www/ccp_api/yii(23): yii\base\Application->run()

#16 {main}

*** failed to apply m210106_122427_add_columns_to_task_table (time: 0.006s)



2. I confirmed that the database structure had not been modified manually. I then analyzed the container’s health checks. Requests are sent periodically to the container to detect its health status. Under the default settings, readiness and liveness use the same configuration parameters. For containers with a long application initialization time, the readiness probe timeout needs to be increased. Preliminary analysis conclusion: Because the database migration took too long to execute, the container initialization time was prolonged, causing the health check to fail and the container to restart repeatedly, which in turn caused the database migration to execute repeatedly. I decided to increase the readiness probe timeout. The time to wait before starting the probe was changed from 10 seconds to 100 seconds. See Figure 2.

I confirmed that the database structure had not been modified manually. I then analyzed the container's health checks. Requests are sent periodically to the container to detect its health status. Under the default settings, readiness and liveness use the same configuration parameters. For containers with a long application initialization time, the readiness probe timeout needs to be increased. Preliminary analysis conclusion: Because the database migration took too long to execute, the container initialization time was prolonged, causing the health check to fail and the container to restart repeatedly, which in turn caused the database migration to execute repeatedly. I decided to increase the readiness probe timeout. The time to wait before starting the probe was changed from 10 seconds to 100 seconds.
Figure 2

3. During the initial deployment, the error still occurred: ReplicaSet “api-77dfcc7cfb” has timed out progressing.; Deployment does not have minimum availability. See Figure 3.

During the initial deployment, the error still occurred: ReplicaSet "api-77dfcc7cfb" has timed out progressing.; Deployment does not have minimum availability.
Figure 3

4. I checked the logs, and the database migration was still executing repeatedly. I examined the table structure in MySQL and the records in the migration table, and determined that the migration record m210402_052030_add_columns_to_channel_app_source_table did not exist in the migration table, but the table structure in MySQL already reflected the result of executing this migration. See Figure 4.

I checked the logs, and the database migration was still executing repeatedly. I examined the table structure in MySQL and the records in the migration table, and determined that the migration record m210402_052030_add_columns_to_channel_app_source_table did not exist in the migration table, but the table structure in MySQL already reflected the result of executing this migration.
Figure 4
Plaintext
m210402_052030_add_columns_to_channel_app_source_table

*** applying m201105_020453_add_article_read_count_column_to_channel_app_task_table

> add column article_read_count integer NOT NULL DEFAULT 0 COMMENT '文章的阅读数' AFTER `have_pub_number` to table {{%channel_app_task}} ...Exception: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'article_read_count'

The SQL being executed was: ALTER TABLE `cpa_channel_app_task` ADD `article_read_count` int(11) NOT NULL DEFAULT 0 COMMENT '文章的阅读数' AFTER `have_pub_number` (/mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Schema.php:678)

#0 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Command.php(1304): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE `cp...')

#1 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Command.php(1099): yii\db\Command->internalExecute('ALTER TABLE `cp...')

#2 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Migration.php(377): yii\db\Command->execute()

#3 /mcloud/www/ccp_api/console/migrations/m201105_020453_add_article_read_count_column_to_channel_app_task_table.php(15): yii\db\Migration->addColumn('{{%channel_app_...', 'article_read_co...', Object(yii\db\mysql\ColumnSchemaBuilder))

#4 /mcloud/www/ccp_api/vendor/yiisoft/yii2/db/Migration.php(114): m201105_020453_add_article_read_count_column_to_channel_app_task_table->safeUp()

#5 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(744): yii\db\Migration->up()

#6 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(202): yii\console\controllers\BaseMigrateController->migrateUp('m201105_020453_...')

#7 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)

#8 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

#9 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)

#10 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('', Array)

#11 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('', Array)

#12 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('migrate', Array)

#13 /mcloud/www/ccp_api/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('migrate', Array)

#14 /mcloud/www/ccp_api/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))

#15 /mcloud/www/ccp_api/yii(23): yii\base\Application->run()

#16 {main}

*** failed to apply m201105_020453_add_article_read_count_column_to_channel_app_task_table (time: 0.006s)

0 from 27 migrations were applied.

Migration failed. The rest of the migrations are canceled.

5. The time to wait before starting the probe was changed from 10 seconds to 100 seconds. See Figure 5.

The time to wait before starting the probe was changed from 10 seconds to 100 seconds.
Figure 5

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系