-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add logic to skip partition creation if not needed Signed-off-by: Jesse Nelson <jesse@swirldslabs.com>
- Loading branch information
Showing
5 changed files
with
186 additions
and
111 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
hedera-mirror-importer/src/main/java/com/hedera/mirror/importer/db/PartitionInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed 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 com.hedera.mirror.importer.db; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
class PartitionInfo { | ||
private String parentTable; | ||
private String partitionColumn; | ||
private long nextFrom; | ||
private long nextTo; | ||
private long maxEntityId; | ||
private boolean timePartition; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...porter/src/main/java/com/hedera/mirror/importer/db/PartitionMaintenanceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed 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 com.hedera.mirror.importer.db; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties("hedera.mirror.importer.db.maintenance") | ||
@Data | ||
class PartitionMaintenanceConfiguration { | ||
private String timePartitionInterval = "1 month"; | ||
private String idPartitionInterval = ".001 seconds"; | ||
private double maxEntityIdRatio = 2.0; | ||
} |
114 changes: 114 additions & 0 deletions
114
...ror-importer/src/main/java/com/hedera/mirror/importer/db/PartitionMaintenanceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed 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 com.hedera.mirror.importer.db; | ||
|
||
import static jakarta.transaction.Transactional.TxType.REQUIRES_NEW; | ||
|
||
import jakarta.inject.Named; | ||
import jakarta.transaction.Transactional; | ||
import java.time.*; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import lombok.CustomLog; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.DataClassRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.retry.annotation.Retryable; | ||
|
||
@Named | ||
@RequiredArgsConstructor | ||
@CustomLog | ||
public class PartitionMaintenanceService { | ||
private static final String TIMESTAMP_PARTITION_COLUMN_NAME_PATTERN = "^(.*_timestamp|consensus_end)$"; | ||
private static final ZoneId PARTITION_BOUND_TIMEZONE = ZoneId.of("UTC"); | ||
private static final DateTimeFormatter CITUS_TIME_PARTITION_NAME_FORMATTER = | ||
DateTimeFormatter.ofPattern("'_p'yyyy_MM_dd_HHmmss").withZone(PARTITION_BOUND_TIMEZONE); | ||
private static final String TABLE_INFO_QUERY = "SELECT " | ||
+ " tp.parent_table, " | ||
+ " tp.partition_column, " | ||
+ " max(tp.to_value::bigint) as next_from, " | ||
+ " case when tp.partition_column::varchar ~ '" | ||
+ TIMESTAMP_PARTITION_COLUMN_NAME_PATTERN + "'" | ||
+ " then extract(epoch from (to_timestamp(max(tp.to_value::bigint / 1000000000.0)) + (?::interval))) * 1000000000 " | ||
+ " else extract(epoch from (to_timestamp(max(tp.to_value::bigint / 1000000000.0)) + (?::interval))) * 1000000000 " | ||
+ " end as next_to, " | ||
+ " (tp.partition_column::varchar ~ '" + TIMESTAMP_PARTITION_COLUMN_NAME_PATTERN | ||
+ "') as time_partition," | ||
+ " (select COALESCE(max(id), 1) from entity) as max_entity_id" | ||
+ " from time_partitions tp group by tp.parent_table, tp.partition_column"; | ||
|
||
private static final String CREATE_TIME_PARTITIONS_SQL = "select create_time_partitions(table_name := ?," | ||
+ " partition_interval := ?::interval," | ||
+ " start_from := ?," | ||
+ " end_at := ?)"; | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
private final PartitionMaintenanceConfiguration maintenanceConfig; | ||
|
||
protected List<PartitionInfo> getNextPartitions() { | ||
return jdbcTemplate.query( | ||
TABLE_INFO_QUERY, | ||
new DataClassRowMapper<>(PartitionInfo.class), | ||
maintenanceConfig.getTimePartitionInterval(), | ||
maintenanceConfig.getIdPartitionInterval()); | ||
} | ||
|
||
@Transactional(REQUIRES_NEW) | ||
@Retryable(retryFor = DataAccessException.class) | ||
protected boolean createPartition(PartitionInfo partitionInfo) { | ||
LocalDateTime start = LocalDateTime.ofInstant( | ||
Instant.ofEpochSecond(0L, partitionInfo.getNextFrom()), PARTITION_BOUND_TIMEZONE); | ||
LocalDateTime end = | ||
LocalDateTime.ofInstant(Instant.ofEpochSecond(0L, partitionInfo.getNextTo()), PARTITION_BOUND_TIMEZONE); | ||
Duration partitionDuration = Duration.between(start, end); | ||
|
||
// will always premake one timePartition | ||
boolean makeTimePartition = partitionInfo.isTimePartition() | ||
&& LocalDateTime.now(PARTITION_BOUND_TIMEZONE) | ||
.plus(partitionDuration) | ||
.isAfter(start); | ||
boolean makeIdPartition = !partitionInfo.isTimePartition() | ||
&& partitionInfo.getMaxEntityId() * maintenanceConfig.getMaxEntityIdRatio() | ||
>= partitionInfo.getNextFrom(); | ||
boolean skipPartition = !(makeIdPartition || makeTimePartition); | ||
if (skipPartition) { | ||
log.info("No new partition needed. Skipping creation for partition {}", partitionInfo); | ||
return false; | ||
} | ||
String interval = partitionInfo.isTimePartition() | ||
? maintenanceConfig.getTimePartitionInterval() | ||
: maintenanceConfig.getIdPartitionInterval(); | ||
boolean created = BooleanUtils.isTrue(jdbcTemplate.queryForObject( | ||
CREATE_TIME_PARTITIONS_SQL, Boolean.class, partitionInfo.getParentTable(), interval, start, end)); | ||
|
||
if (created && !partitionInfo.isTimePartition()) { | ||
// Work around granularity issue of citus partitionInfo names | ||
String createdPartitionName = | ||
partitionInfo.getParentTable() + CITUS_TIME_PARTITION_NAME_FORMATTER.format(start); | ||
long partitionCount = partitionInfo.getNextFrom() / partitionDuration.toNanos(); | ||
String updatePartitionNameSql = String.format( | ||
"alter table %s rename to %s_p%d", | ||
createdPartitionName, partitionInfo.getParentTable(), partitionCount); | ||
jdbcTemplate.execute(updatePartitionNameSql); | ||
log.info("Renamed partition: {}", updatePartitionNameSql); | ||
} | ||
log.info("Partition {} created {}", partitionInfo, created); | ||
return created; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters