Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.slf4j.LoggerFactory;

/**
* Upgrade action for TASK_STATUS_STATISTICS feature layout change, which adds
* Upgrade action for TASK_STATUS_STATISTICS version, which adds
* <code>last_task_run_status</code> and <code>current_task_run_status</code> columns to
* {@link ReconTaskSchemaDefinition} in case it is missing .
* <p>
* It also applies the UNHEALTHY_CONTAINERS check constraint which must be run with Recon's first version increase.
*/
@ReconUpgradeActionForVersion(version = ReconVersion.TASK_STATUS_STATISTICS)
public class ReconTaskStatusTableUpgradeAction implements ReconUpgradeAction {
Expand Down Expand Up @@ -73,6 +75,8 @@ private void setColumnAsNonNullableIfNeeded(Connection conn, DSLContext dslConte

@Override
public void execute(DataSource dataSource) throws DataAccessException, SQLException {
ReconUpgradeAction.updateUnhealthyContainerStatesConstraint(dataSource, LOG);

try (Connection conn = dataSource.getConnection()) {
if (!TABLE_EXISTS_CHECK.test(conn, RECON_TASK_STATUS_TABLE_NAME)) {
return;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.hadoop.ozone.recon.upgrade;

import static org.apache.ozone.recon.schema.ContainerSchemaDefinition.UNHEALTHY_CONTAINERS_TABLE_NAME;
import static org.apache.ozone.recon.schema.ReconTaskSchemaDefinition.RECON_TASK_STATUS_TABLE_NAME;
import static org.apache.ozone.recon.schema.SqlDbUtils.TABLE_EXISTS_CHECK;
import static org.apache.ozone.recon.schema.SqlDbUtils.columnExists;
import static org.apache.ozone.recon.schema.SqlDbUtils.constraintExists;
import static org.apache.ozone.recon.schema.SqlDbUtils.isColumnNullable;
import static org.jooq.impl.DSL.name;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -42,6 +45,7 @@ public class TestReconTaskStatusTableUpgradeAction extends AbstractReconSqlDBTes

private static final String LAST_TASK_RUN_STATUS = "last_task_run_status";
private static final String IS_CURRENT_TASK_RUNNING = "is_current_task_running";
private static final String UNHEALTHY_CONTAINERS_CONSTRAINT = UNHEALTHY_CONTAINERS_TABLE_NAME + "ck1";

private DSLContext dslContext;
private DataSource dataSource;
Expand Down Expand Up @@ -98,6 +102,17 @@ public void testNoOpWhenTableMissing() throws SQLException {
assertDoesNotThrow(() -> upgradeAction.execute(dataSource));
}

@Test
public void testExecuteAppliesUnhealthyContainersConstraint() throws Exception {
createUnhealthyContainersTableWithoutCheckConstraint();

upgradeAction.execute(dataSource);

try (Connection conn = dataSource.getConnection()) {
assertTrue(constraintExists(conn, UNHEALTHY_CONTAINERS_TABLE_NAME, UNHEALTHY_CONTAINERS_CONSTRAINT));
}
}

private void createLegacyTaskStatusTable() throws SQLException {
dropTaskStatusTableIfPresent();
try (Connection conn = dataSource.getConnection()) {
Expand All @@ -124,4 +139,18 @@ private void dropTaskStatusTableIfPresent() throws SQLException {
}
}
}

private void createUnhealthyContainersTableWithoutCheckConstraint() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
if (TABLE_EXISTS_CHECK.test(conn, UNHEALTHY_CONTAINERS_TABLE_NAME)) {
dslContext.dropTable(UNHEALTHY_CONTAINERS_TABLE_NAME).execute();
}
}
dslContext.createTable(UNHEALTHY_CONTAINERS_TABLE_NAME)
.column("container_id", SQLDataType.BIGINT.nullable(false))
.column("container_state", SQLDataType.VARCHAR(16).nullable(false))
.constraint(DSL.constraint("pk_container_id")
.primaryKey(name("container_id"), name("container_state")))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
import static org.apache.ozone.recon.schema.ContainerSchemaDefinition.UNHEALTHY_CONTAINERS_TABLE_NAME;
import static org.apache.ozone.recon.schema.SqlDbUtils.TABLE_EXISTS_CHECK;
import static org.apache.ozone.recon.schema.SqlDbUtils.constraintExists;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.hadoop.ozone.recon.persistence.AbstractReconSqlDBTest;
import org.apache.ozone.recon.schema.ContainerSchemaDefinition.UnHealthyContainerStates;
import org.jooq.DSLContext;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -68,6 +73,30 @@ public void testNoOpWhenTableMissing() throws SQLException {
assertDoesNotThrow(() -> upgradeAction.execute(dataSource));
}

@Test
public void testConstraintAdmitsAllStatesAndRejectsInvalid() throws Exception {
upgradeAction.execute(dataSource);

UnHealthyContainerStates[] states = UnHealthyContainerStates.values();
for (int i = 0; i < states.length; i++) {
insertContainerState(i, states[i].name());
}
assertEquals(states.length,
dslContext.fetchCount(DSL.table(UNHEALTHY_CONTAINERS_TABLE_NAME)),
"Expected one record for each valid state");

assertThrows(DataAccessException.class,
() -> insertContainerState(states.length, "INVALID_STATE"),
"Inserting an invalid container_state should fail due to the check constraint");
}

private void insertContainerState(long containerId, String containerState) {
dslContext.insertInto(DSL.table(UNHEALTHY_CONTAINERS_TABLE_NAME))
.columns(field(name("container_id")), field(name("container_state")))
.values(containerId, containerState)
.execute();
}

private void createTableWithoutCheckConstraint() throws SQLException {
dropTableIfPresent();
dslContext.createTable(UNHEALTHY_CONTAINERS_TABLE_NAME)
Expand Down
Loading