|
| 1 | +/* |
| 2 | + * Copyright 2021 Red Hat |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.apicurio.registry.storage.impl.sql.upgrader; |
| 18 | + |
| 19 | +import io.apicurio.registry.content.ContentHandle; |
| 20 | +import io.apicurio.registry.content.canon.ContentCanonicalizer; |
| 21 | +import io.apicurio.registry.storage.RegistryStorage; |
| 22 | +import io.apicurio.registry.storage.dto.ArtifactReferenceDto; |
| 23 | +import io.apicurio.registry.storage.impl.sql.IDbUpgrader; |
| 24 | +import io.apicurio.registry.storage.impl.sql.SqlUtil; |
| 25 | +import io.apicurio.registry.storage.impl.sql.jdb.Handle; |
| 26 | +import io.apicurio.registry.storage.impl.sql.jdb.RowMapper; |
| 27 | +import io.apicurio.registry.storage.impl.sql.mappers.ContentEntityMapper; |
| 28 | +import io.apicurio.registry.types.ArtifactType; |
| 29 | +import io.apicurio.registry.types.provider.ArtifactTypeUtilProviderFactory; |
| 30 | +import io.apicurio.registry.types.provider.DefaultArtifactTypeUtilProviderImpl; |
| 31 | +import io.apicurio.registry.utils.impexp.ContentEntity; |
| 32 | +import io.quarkus.runtime.annotations.RegisterForReflection; |
| 33 | +import org.apache.commons.codec.digest.DigestUtils; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +import java.io.ByteArrayOutputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.nio.charset.StandardCharsets; |
| 40 | +import java.sql.ResultSet; |
| 41 | +import java.sql.SQLException; |
| 42 | +import java.util.List; |
| 43 | +import java.util.stream.Stream; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Carles Arnal |
| 47 | + */ |
| 48 | +@RegisterForReflection |
| 49 | +public class AvroCanonicalHashUpgrader implements IDbUpgrader { |
| 50 | + |
| 51 | + private static final Logger logger = LoggerFactory.getLogger(ReferencesContentHashUpgrader.class); |
| 52 | + |
| 53 | + private static final ArtifactTypeUtilProviderFactory factory = new DefaultArtifactTypeUtilProviderImpl(); |
| 54 | + |
| 55 | + private RegistryStorage storage; |
| 56 | + |
| 57 | + /** |
| 58 | + * @see io.apicurio.registry.storage.impl.sql.IDbUpgrader#upgrade(io.apicurio.registry.storage.impl.sql.jdb.Handle) |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public void upgrade(Handle dbHandle) throws Exception { |
| 62 | + //Do nothing, this is just implemented for backward compatibility reasons. |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void upgrade(RegistryStorage registryStorage, Handle dbHandle) throws Exception { |
| 67 | + this.storage = registryStorage; |
| 68 | + String sql = "SELECT c.contentId, c.content, c.canonicalHash, c.contentHash, c.artifactreferences, a.type " |
| 69 | + + "FROM versions v " |
| 70 | + + "JOIN content c on c.contentId = v.contentId " |
| 71 | + + "JOIN artifacts a ON v.tenantId = a.tenantId AND v.groupId = a.groupId AND v.artifactId = a.artifactId " |
| 72 | + + "WHERE a.type = ?"; |
| 73 | + |
| 74 | + Stream<TypeContentEntity> stream = dbHandle.createQuery(sql) |
| 75 | + .setFetchSize(50) |
| 76 | + .bind(0, ArtifactType.AVRO) |
| 77 | + .map(new TenantContentEntityRowMapper()) |
| 78 | + .stream(); |
| 79 | + try (stream) { |
| 80 | + stream.forEach(entity -> updateCanonicalHash(entity, dbHandle)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void updateCanonicalHash(TypeContentEntity contentEntity, Handle dbHandle) { |
| 85 | + try { |
| 86 | + String canonicalContentHash; |
| 87 | + byte[] referencesBytes = contentEntity.contentEntity.serializedReferences.getBytes(StandardCharsets.UTF_8); |
| 88 | + canonicalContentHash = DigestUtils.sha256Hex(concatContentAndReferences(this.canonicalizeContent(contentEntity.contentEntity, contentEntity.type).bytes(), referencesBytes)); |
| 89 | + |
| 90 | + if (canonicalContentHash.equals(contentEntity.contentEntity.canonicalHash)) { |
| 91 | + logger.debug("Skipping content because the canonical hash is up to date, updating contentId {}", contentEntity.contentEntity.contentId); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + logger.debug("Avro content canonicalHash outdated value detected, updating contentId {}", contentEntity.contentEntity.contentId); |
| 96 | + |
| 97 | + String update = "UPDATE content SET canonicalHash = ? WHERE contentId = ? AND contentHash = ?"; |
| 98 | + int rowCount = dbHandle.createUpdate(update) |
| 99 | + .bind(0, canonicalContentHash) |
| 100 | + .bind(1, contentEntity.contentEntity.contentId) |
| 101 | + .bind(2, contentEntity.contentEntity.contentHash) |
| 102 | + .execute(); |
| 103 | + |
| 104 | + if (rowCount == 0) { |
| 105 | + logger.warn("content row not matched for canonical hash upgrade contentId {} contentHash {}", contentEntity.contentEntity.contentId, contentEntity.contentEntity.contentHash); |
| 106 | + } |
| 107 | + |
| 108 | + } catch (Exception e) { |
| 109 | + logger.warn("Error found processing content with id {} and hash {}", contentEntity.contentEntity.contentId, contentEntity.contentEntity.contentHash, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private byte[] concatContentAndReferences(byte[] contentBytes, byte[] referencesBytes) throws IOException { |
| 114 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(contentBytes.length + referencesBytes.length); |
| 115 | + outputStream.write(contentBytes); |
| 116 | + outputStream.write(referencesBytes); |
| 117 | + return outputStream.toByteArray(); |
| 118 | + } |
| 119 | + |
| 120 | + private ContentHandle canonicalizeContent(ContentEntity contentEntity, String type) { |
| 121 | + ContentHandle contentHandle = ContentHandle.create(contentEntity.contentBytes); |
| 122 | + List<ArtifactReferenceDto> artifactReferenceDtos = SqlUtil.deserializeReferences(contentEntity.serializedReferences); |
| 123 | + ContentCanonicalizer canonicalizer = factory.getArtifactTypeProvider(type).getContentCanonicalizer(); |
| 124 | + return canonicalizer.canonicalize(contentHandle, storage.resolveReferences(artifactReferenceDtos)); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public static class TypeContentEntity { |
| 129 | + String type; |
| 130 | + ContentEntity contentEntity; |
| 131 | + } |
| 132 | + |
| 133 | + public static class TenantContentEntityRowMapper implements RowMapper<TypeContentEntity> { |
| 134 | + @Override |
| 135 | + public TypeContentEntity map(ResultSet rs) throws SQLException { |
| 136 | + TypeContentEntity e = new TypeContentEntity(); |
| 137 | + e.type = rs.getString("type"); |
| 138 | + e.contentEntity = ContentEntityMapper.instance.map(rs); |
| 139 | + return e; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments