aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/ZooKeeperDeployerTest.java
blob: 17344e94c5104a1466e9c34695990892219d6c7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.deploy;

import com.google.common.collect.ImmutableSet;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ApplicationMetaData;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.application.provider.DeployData;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.application.provider.MockFileRegistry;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.io.IOUtils;
import com.yahoo.path.Path;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.zookeeper.ZKApplicationPackage;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;

import static com.yahoo.config.provision.serialization.AllocatedHostsSerializer.fromJson;
import static com.yahoo.vespa.config.server.session.SessionZooKeeperClient.getSessionPath;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.DEFCONFIGS_ZK_SUBPATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.META_ZK_PATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.USERAPP_ZK_SUBPATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.USER_DEFCONFIGS_ZK_SUBPATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Ulf Lilleengen
 */
public class ZooKeeperDeployerTest {

    private Curator zk;
    private final Path appPath = Path.fromString("/1");

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();
    private static final String defFile = "test2.def";

    @Before
    public void setupZK() throws IOException {
        zk = new MockCurator();
        ZooKeeperDeployer.Client zkc = new ZooKeeperDeployer.Client(zk, new BaseDeployLogger(), appPath);
        ApplicationPackage app = FilesApplicationPackage.fromFileWithDeployData(new File("src/test/apps/zkfeed"),
                                                                                new DeployData("/bar/baz",
                                                                                               ApplicationId.from("default", "appName", "default"),
                                                                                               1345L,
                                                                                               true,
                                                                                               3L,
                                                                                               2L));
        Map<Version, FileRegistry> fileRegistries = createFileRegistries();
        app.writeMetaData();
        zkc.initialize();
        zkc.writeApplicationPackage(app);
        zkc.write(fileRegistries);
    }

    @Test
    public void require_that_deployer_is_initialized() throws IOException {
        Curator curator = new MockCurator();
        File serverdbDir = folder.newFolder("serverdb");
        File defsDir = new File(serverdbDir, "serverdefs");
        try {
            IOUtils.createWriter(new File(defsDir, defFile), true);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        deploy(FilesApplicationPackage.fromFile(new File("src/test/apps/content")), curator, 1);
        deploy(FilesApplicationPackage.fromFile(new File("src/test/apps/content")), curator, 2);
    }

    private Map<Version, FileRegistry> createFileRegistries() {
        FileRegistry a = new MockFileRegistry();
        a.addFile("fileA");
        FileRegistry b = new MockFileRegistry();
        b.addFile("fileB");
        Map<Version, FileRegistry> registryMap = new HashMap<>();
        registryMap.put(new Version(1, 2, 3), a);
        registryMap.put(new Version(3, 2, 1), b);
        return registryMap;
    }

    @Test
    public void testInitZooKeeper() {
        Curator zk = new MockCurator();
        BaseDeployLogger logger = new BaseDeployLogger();
        long generation = 1L;
        ZooKeeperDeployer.Client client = new ZooKeeperDeployer.Client(zk, logger, Path.fromString("/1"));
        client.initialize();
        Path appPath = Path.fromString("/");
        assertEquals(1, zk.getChildren(appPath).size());
        Path currentAppPath = appPath.append(String.valueOf(generation));
        assertTrue(zk.exists(currentAppPath));
        assertTrue(zk.exists(currentAppPath.append(DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", ""))));
        assertEquals(4, zk.getChildren(currentAppPath).size());
    }

    @Test
    public void testFeedDefFilesToZooKeeper() {
        Path defsPath = appPath.append(DEFCONFIGS_ZK_SUBPATH);
        assertTrue(zk.exists(appPath.append(DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", ""))));
        List<String> children = zk.getChildren(defsPath);
        assertEquals(defsPath + " children", 1, children.size());
        Collections.sort(children);
        assertEquals("a.b.test2", children.get(0));

        assertTrue(zk.exists(appPath.append(USER_DEFCONFIGS_ZK_SUBPATH.replaceFirst("/", ""))));
        Path userDefsPath = appPath.append(USER_DEFCONFIGS_ZK_SUBPATH);
        children = zk.getChildren(userDefsPath);
        assertEquals(1, children.size());
        Collections.sort(children);
        assertEquals("a.b.test2", children.get(0));
    }

    @Test
    public void testFeedAppMetaDataToZooKeeper() {
        assertTrue(zk.exists(appPath.append(META_ZK_PATH)));
        ApplicationMetaData metaData = ApplicationMetaData.fromJsonString(
                Utf8.toString(zk.getData(appPath.append(META_ZK_PATH)).get()));
        assertTrue(metaData.getChecksum().length() > 0);
        assertTrue(metaData.isInternalRedeploy());
        assertEquals("/bar/baz", metaData.getDeployPath());
        assertEquals(1345, metaData.getDeployTimestamp().longValue());
        assertEquals(3, metaData.getGeneration().longValue());
        assertEquals(2, metaData.getPreviousActiveGeneration());
    }

    @Test
    public void testVersionedFileRegistry() {
        Path fileRegPath = appPath.append(ZKApplicationPackage.fileRegistryNode);
        assertTrue(zk.exists(fileRegPath));
        assertTrue(zk.exists(fileRegPath.append("/1.2.3")));
        assertTrue(zk.exists(fileRegPath.append("/3.2.1")));
        // assertNull("Data at " + fileRegPath, zk.getData(fileRegPath)); Not null any more .. hm
    }

    @Test
    public void include_dirs_are_written_to_ZK() {
        assertTrue(zk.exists(appPath.append(USERAPP_ZK_SUBPATH).append("dir1").append("default.xml")));
        assertTrue(zk.exists(appPath.append(USERAPP_ZK_SUBPATH).append("nested").append("dir2").append("chain2.xml")));
        assertTrue(zk.exists(appPath.append(USERAPP_ZK_SUBPATH).append("nested").append("dir2").append("chain3.xml")));
    }

    @Test
    public void search_chain_dir_written_to_ZK() {
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir1").append("default.xml")));
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir2").append("chain2.xml")));
        assertTrue(zk.exists(appPath().append("search").append("chains").append("dir2").append("chain3.xml")));
    }

    private Path appPath() {
        return appPath.append(USERAPP_ZK_SUBPATH);
    }

    @Test
    public void testWritingHostNamesToZooKeeper() throws IOException {
        Curator zk = new MockCurator();
        BaseDeployLogger logger = new BaseDeployLogger();
        Path app = Path.fromString("/1");
        ZooKeeperDeployer.Client client = new ZooKeeperDeployer.Client(zk, logger, app);
        client.initialize();
        HostSpec host1 = new HostSpec("host1.yahoo.com", Optional.empty());
        HostSpec host2 = new HostSpec("host2.yahoo.com", Optional.empty());
        ImmutableSet<HostSpec> hosts = ImmutableSet.of(host1, host2);
        client.write(AllocatedHosts.withHosts(hosts));
        Path hostsPath = app.append(ZKApplicationPackage.allocatedHostsNode);
        assertTrue(zk.exists(hostsPath));

        AllocatedHosts deserialized = fromJson(zk.getData(hostsPath).get());
        assertEquals(hosts, deserialized.getHosts());
    }

    public void deploy(ApplicationPackage applicationPackage, Curator curator, long sessionId) throws IOException {
        ZooKeeperDeployer deployer = new ZooKeeperDeployer(curator, new MockDeployLogger(), applicationPackage.getApplicationId(), sessionId);
        deployer.deploy(applicationPackage, Map.of(new Version(1, 0, 0), new MockFileRegistry()), AllocatedHosts.withHosts(Set.of()));

        Path sessionPath = getSessionPath(applicationPackage.getApplicationId().tenant(), sessionId);
        assertTrue(curator.exists(sessionPath));
    }

    private static class MockDeployLogger implements DeployLogger {
        @Override
        public void log(Level level, String message) { }
    }

}