From 40a11a146e6cb094aa80dbfa0d97879006c2c486 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 19 Aug 2021 20:01:37 +0200 Subject: Allow set of distributable files to change after initial deploy. This is necessary as bootstrapping the configserver with a new version might change set of files to distribute. --- .../application/provider/MockFileRegistry.java | 3 -- .../provider/PreGeneratedFileRegistry.java | 58 +++++++++++----------- .../provider/PreGeneratedFileRegistryTestCase.java | 1 - 3 files changed, 28 insertions(+), 34 deletions(-) (limited to 'config-application-package/src') diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java index f024f2ac270..358d08ded25 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/MockFileRegistry.java @@ -25,9 +25,6 @@ public class MockFileRegistry implements FileRegistry { return fileReference; } - @Override - public String fileSourceHost() { return HostName.getLocalhost(); } - public List export() { return entries; } @Override diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java index ac73a0100dc..5a7f2db1968 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistry.java @@ -3,12 +3,14 @@ package com.yahoo.config.model.application.provider; import com.yahoo.config.FileReference; import com.yahoo.config.application.api.FileRegistry; +import com.yahoo.net.HostName; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -23,42 +25,43 @@ import java.util.regex.Pattern; public class PreGeneratedFileRegistry implements FileRegistry { private final String fileSourceHost; - private final Map path2Hash = new LinkedHashMap<>(); + private final Map path2Hash; private static final String entryDelimiter = "\t"; private static final Pattern entryDelimiterPattern = Pattern.compile(entryDelimiter, Pattern.LITERAL); + public static Map decode(BufferedReader reader) { + Map refs = new HashMap<>(); + try { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = entryDelimiterPattern.split(line); + if (parts.length < 2) + throw new IllegalArgumentException("Cannot split '" + line + "' into two parts"); + refs.put(parts[0], new FileReference(parts[1])); + } + } catch (IOException e) { + throw new RuntimeException("Error while reading pre-generated file registry", e); + } + return refs; + } private PreGeneratedFileRegistry(Reader readerArg) { try (BufferedReader reader = new BufferedReader(readerArg)) { fileSourceHost = reader.readLine(); if (fileSourceHost == null) throw new RuntimeException("Error while reading pre-generated file registry"); - String line; - while ((line = reader.readLine()) != null) { - addFromLine(line); - } + path2Hash = decode(reader); } catch (IOException e) { throw new RuntimeException("Error while reading pre-generated file registry", e); } } - private void addFromLine(String line) { - String[] parts = entryDelimiterPattern.split(line); - if (parts.length < 2) - throw new IllegalArgumentException("Cannot split '" + line + "' into two parts"); - addEntry(parts[0], parts[1]); - } - - private void addEntry(String relativePath, String hash) { - path2Hash.put(relativePath, hash); - } - public static String exportRegistry(FileRegistry registry) { List entries = registry.export(); StringBuilder builder = new StringBuilder(); - builder.append(registry.fileSourceHost()).append('\n'); + builder.append(HostName.getLocalhost()).append('\n'); for (FileRegistry.Entry entry : entries) { builder.append(entry.relativePath).append(entryDelimiter).append(entry.reference.value()).append('\n'); } @@ -71,34 +74,29 @@ public class PreGeneratedFileRegistry implements FileRegistry { } public FileReference addFile(String relativePath) { - String reference = path2Hash.get(relativePath); + FileReference reference = path2Hash.get(relativePath); if (reference == null) { throw new IllegalArgumentException("File '" + relativePath + "' not found"); } - return new FileReference(reference); + return reference; } @Override public FileReference addUri(String uri) { - String reference = path2Hash.get(uri); + FileReference reference = path2Hash.get(uri); if (reference == null) { throw new IllegalArgumentException("Uri '" + uri + "' not found"); } - return new FileReference(reference); + return reference; } @Override public FileReference addBlob(ByteBuffer blob) { String blobName = FileRegistry.blobName(blob); - String reference = path2Hash.get(blobName); + FileReference reference = path2Hash.get(blobName); if (reference == null) { throw new IllegalArgumentException("Blob '" + blobName + "(" + blob.remaining()+ ")' not found"); } - return new FileReference(reference); - } - - @Override - public String fileSourceHost() { - return fileSourceHost; + return reference; } public Set getPaths() { @@ -108,8 +106,8 @@ public class PreGeneratedFileRegistry implements FileRegistry { @Override public List export() { List entries = new ArrayList<>(); - for (Map.Entry entry : path2Hash.entrySet()) { - entries.add(new Entry(entry.getKey(), new FileReference(entry.getValue()))); + for (Map.Entry entry : path2Hash.entrySet()) { + entries.add(new Entry(entry.getKey(), entry.getValue())); } return entries; } diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java index 7996efaa60e..532ebb1ab81 100644 --- a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java @@ -32,7 +32,6 @@ public class PreGeneratedFileRegistryTestCase { checkConsistentEntry(fileRegistry.export().get(0), importedRegistry); checkConsistentEntry(fileRegistry.export().get(1), importedRegistry); - assertEquals(fileRegistry.fileSourceHost(), importedRegistry.fileSourceHost()); } void checkConsistentEntry(FileRegistry.Entry entry, FileRegistry registry) { -- cgit v1.2.3