summaryrefslogtreecommitdiffstats
path: root/flags
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2019-01-26 02:30:54 +0100
committerHåkon Hallingstad <hakon@oath.com>2019-01-26 02:30:54 +0100
commit5b51470fdf2eb245d509984047dc84ef3a7fae83 (patch)
treea310e85085feaf5624f826215af766dd4e704b42 /flags
parent5d126afb33df13888c58e5b43a79c893c23fb65b (diff)
Read override file flags from file only once for config server
Up until now, every lookup of a flag in the ConfigServerFlagSource would 1. try to read 2 flag files under /etc/vespa/flags, causing exceptions because they are typically not set, and 2. then read flag from ZooKeeper through ZooKeeperFlagSource Optimization was deliberately held off until later (now). This PR fixes (1). Changes the ConfigServerFlagSource to: 1'. Read VESPA_HOME/var/vespa/flag.db once during component graph construction. As before, if a flag is defined on file, the flag is not looked up in ZK, which may be useful in emergencies. 2. As before. Also, removes the last usages of FileFlagSource and its reading of flags in /etc/vespa/flags.
Diffstat (limited to 'flags')
-rw-r--r--flags/src/main/java/com/yahoo/vespa/flags/FileFlagSource.java86
-rw-r--r--flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java93
2 files changed, 0 insertions, 179 deletions
diff --git a/flags/src/main/java/com/yahoo/vespa/flags/FileFlagSource.java b/flags/src/main/java/com/yahoo/vespa/flags/FileFlagSource.java
deleted file mode 100644
index 1a31ecd713e..00000000000
--- a/flags/src/main/java/com/yahoo/vespa/flags/FileFlagSource.java
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.flags;
-
-import com.google.inject.Inject;
-import com.yahoo.vespa.flags.json.FlagData;
-import com.yahoo.vespa.flags.json.Rule;
-
-import java.io.IOException;
-import java.io.UncheckedIOException;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystem;
-import java.nio.file.FileSystems;
-import java.nio.file.Files;
-import java.nio.file.NoSuchFileException;
-import java.nio.file.Path;
-import java.util.Collections;
-import java.util.Optional;
-
-/**
- * @author hakonhall
- */
-public class FileFlagSource implements FlagSource {
- static final String FLAGS_DIRECTORY = "/etc/vespa/flags";
-
- private final Path flagsDirectory;
-
- @Inject
- public FileFlagSource() {
- this(FileSystems.getDefault());
- }
-
- public FileFlagSource(FileSystem fileSystem) {
- this(fileSystem.getPath(FLAGS_DIRECTORY));
- }
-
- public FileFlagSource(Path flagsDirectory) {
- this.flagsDirectory = flagsDirectory;
- }
-
- @Override
- public Optional<RawFlag> fetch(FlagId flagId, FetchVector vector) {
- return getResolver(flagId).resolve(vector);
- }
-
- private FlagData getResolver(FlagId flagId) {
- Optional<String> v2String = getString(flagId, ".2");
- if (v2String.isPresent()) {
- return FlagData.deserialize(v2String.get());
- }
-
- Optional<String> v1String = getString(flagId, "");
- if (v1String.isPresent()) {
- // version 1: File contains value as a JSON
- // version 2: File contains FileResolver as a JSON (which may contain many values, one for each rule)
- // version 1 files should probably be discontinued
- Rule rule = new Rule(Optional.of(JsonNodeRawFlag.fromJson(v1String.get())), Collections.emptyList());
- return new FlagData(flagId, new FetchVector(), Collections.singletonList(rule));
- }
-
- // Will eventually resolve to empty RawFlag
- return new FlagData(flagId);
- }
-
- private Optional<String> getString(FlagId id, String suffix) {
- return getBytes(id, suffix).map(bytes -> new String(bytes, StandardCharsets.UTF_8));
- }
-
- private Optional<byte[]> getBytes(FlagId id, String suffix) {
- try {
- return Optional.of(Files.readAllBytes(getPath(id, suffix)));
- } catch (NoSuchFileException e) {
- return Optional.empty();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- }
-
- private Path getPath(FlagId id, String suffix) {
- return flagsDirectory.resolve(id.toString() + suffix);
- }
-
- @Override
- public String toString() {
- return "FileFlagSource{" + flagsDirectory + '}';
- }
-}
diff --git a/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java b/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java
deleted file mode 100644
index e19436069b5..00000000000
--- a/flags/src/test/java/com/yahoo/vespa/flags/FileFlagSourceTest.java
+++ /dev/null
@@ -1,93 +0,0 @@
-// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.flags;
-
-import com.yahoo.vespa.test.file.TestFileSystem;
-import org.junit.Test;
-
-import java.io.IOException;
-import java.io.UncheckedIOException;
-import java.nio.file.FileSystem;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public class FileFlagSourceTest {
- private final FileSystem fileSystem = TestFileSystem.create();
- private final FileFlagSource source = new FileFlagSource(fileSystem);
- private final FlagId id = new FlagId("foo");
-
- @Test
- public void testFeatureLikeFlags() throws IOException {
- BooleanFlag booleanFlag = new UnboundBooleanFlag(id).bindTo(source);
- BooleanFlag byDefaultTrue = new UnboundBooleanFlag(id, true).bindTo(source);
-
- assertFalse(booleanFlag.value());
- assertTrue(byDefaultTrue.value());
-
- writeFlag(id.toString(), "true\n");
-
- assertTrue(booleanFlag.value());
- assertTrue(byDefaultTrue.value());
-
- writeFlag(id.toString(), "false\n");
-
- assertFalse(booleanFlag.value());
- assertFalse(byDefaultTrue.value());
- }
-
- @Test
- public void testIntegerLikeFlags() throws IOException {
- IntFlag intFlag = new UnboundIntFlag(id, -1).bindTo(source);
- LongFlag longFlag = new UnboundLongFlag(id, -2L).bindTo(source);
-
- assertFalse(fetch().isPresent());
- assertFalse(fetch().isPresent());
- assertEquals(-1, intFlag.value());
- assertEquals(-2L, longFlag.value());
-
- writeFlag(id.toString(), "1\n");
-
- assertTrue(fetch().isPresent());
- assertTrue(fetch().isPresent());
- assertEquals(1, intFlag.value());
- assertEquals(1L, longFlag.value());
- }
-
- @Test
- public void testStringFlag() throws IOException {
- StringFlag stringFlag = new UnboundStringFlag(id, "default").bindTo(source);
- assertFalse(fetch().isPresent());
- assertEquals("default", stringFlag.value());
-
- writeFlag(id.toString(), "\"1\\n\"\n");
- assertEquals("1\n", stringFlag.value());
- }
-
- @Test
- public void parseFailure() throws IOException {
- BooleanFlag booleanFlag = new UnboundBooleanFlag(id).bindTo(source);
- writeFlag(booleanFlag.id().toString(), "garbage");
-
- try {
- booleanFlag.value();
- } catch (UncheckedIOException e) {
- assertThat(e.getMessage(), containsString("garbage"));
- }
- }
-
- private Optional<RawFlag> fetch() {
- return source.fetch(id, new FetchVector());
- }
-
- private void writeFlag(String flagId, String value) throws IOException {
- Path featurePath = fileSystem.getPath(FileFlagSource.FLAGS_DIRECTORY).resolve(flagId);
- Files.createDirectories(featurePath.getParent());
- Files.write(featurePath, value.getBytes());
- }
-} \ No newline at end of file