yandrey321 commented on code in PR #9258: URL: https://github.com/apache/ozone/pull/9258#discussion_r2833847553
########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/persistence/ContainerHealthSchemaManagerV2.java: ########## @@ -0,0 +1,351 @@ +/* + * 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. + */ + +package org.apache.hadoop.ozone.recon.persistence; + +import static org.apache.ozone.recon.schema.ContainerSchemaDefinitionV2.UNHEALTHY_CONTAINERS_V2_TABLE_NAME; +import static org.apache.ozone.recon.schema.generated.tables.UnhealthyContainersV2Table.UNHEALTHY_CONTAINERS_V2; +import static org.jooq.impl.DSL.count; + +import com.google.common.annotations.VisibleForTesting; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import java.sql.Connection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.ozone.recon.schema.ContainerSchemaDefinitionV2; +import org.apache.ozone.recon.schema.ContainerSchemaDefinitionV2.UnHealthyContainerStates; +import org.apache.ozone.recon.schema.generated.tables.daos.UnhealthyContainersV2Dao; +import org.apache.ozone.recon.schema.generated.tables.pojos.UnhealthyContainersV2; +import org.apache.ozone.recon.schema.generated.tables.records.UnhealthyContainersV2Record; +import org.jooq.Condition; +import org.jooq.DSLContext; +import org.jooq.OrderField; +import org.jooq.Record; +import org.jooq.SelectQuery; +import org.jooq.exception.DataAccessException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager for UNHEALTHY_CONTAINERS_V2 table used by ContainerHealthTaskV2. + */ +@Singleton +public class ContainerHealthSchemaManagerV2 { + private static final Logger LOG = + LoggerFactory.getLogger(ContainerHealthSchemaManagerV2.class); + private static final int BATCH_INSERT_CHUNK_SIZE = 1000; + + private final UnhealthyContainersV2Dao unhealthyContainersV2Dao; + private final ContainerSchemaDefinitionV2 containerSchemaDefinitionV2; + + @Inject + public ContainerHealthSchemaManagerV2( + ContainerSchemaDefinitionV2 containerSchemaDefinitionV2, + UnhealthyContainersV2Dao unhealthyContainersV2Dao) { + this.unhealthyContainersV2Dao = unhealthyContainersV2Dao; + this.containerSchemaDefinitionV2 = containerSchemaDefinitionV2; + } + + /** + * Insert or update unhealthy container records in V2 table using TRUE batch insert. + * Uses JOOQ's batch API for optimal performance (single SQL statement for all records). + * Falls back to individual insert-or-update if batch insert fails (e.g., duplicate keys). + */ + public void insertUnhealthyContainerRecords(List<UnhealthyContainerRecordV2> recs) { + if (recs == null || recs.isEmpty()) { + return; + } + + if (LOG.isDebugEnabled()) { + recs.forEach(rec -> LOG.debug("rec.getContainerId() : {}, rec.getContainerState(): {}", + rec.getContainerId(), rec.getContainerState())); + } + + DSLContext dslContext = containerSchemaDefinitionV2.getDSLContext(); + + try { + // Try batch insert first in chunks to keep memory/transaction pressure bounded. + dslContext.transaction(configuration -> { + DSLContext txContext = configuration.dsl(); + + for (int from = 0; from < recs.size(); from += BATCH_INSERT_CHUNK_SIZE) { + int to = Math.min(from + BATCH_INSERT_CHUNK_SIZE, recs.size()); + List<UnhealthyContainersV2Record> records = new ArrayList<>(to - from); Review Comment: do we need a new list inside the loop? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
