aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java
blob: c95fee00c9c7324d2e9dc7463a8ac1e0b582a87d (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.tenant;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.cloud.config.ZookeeperServerConfig;
import com.yahoo.component.Version;
import com.yahoo.concurrent.InThreadExecutorService;
import com.yahoo.concurrent.StripedExecutor;
import com.yahoo.config.model.api.OnnxModelCost;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.config.server.ConfigServerDB;
import com.yahoo.vespa.config.server.MockSecretStore;
import com.yahoo.vespa.config.server.ServerCache;
import com.yahoo.vespa.config.server.TestConfigDefinitionRepo;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.application.ApplicationVersions;
import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.application.TenantApplicationsTest;
import com.yahoo.vespa.config.server.filedistribution.FileDirectory;
import com.yahoo.vespa.config.server.filedistribution.FileDistributionFactory;
import com.yahoo.vespa.config.server.host.HostRegistry;
import com.yahoo.vespa.config.server.modelfactory.ModelFactoryRegistry;
import com.yahoo.vespa.config.server.monitoring.MetricUpdater;
import com.yahoo.vespa.config.server.monitoring.Metrics;
import com.yahoo.vespa.config.server.provision.HostProvisionerProvider;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;
import com.yahoo.vespa.flags.FlagSource;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.VespaModelFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.time.Clock;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class TenantRepositoryTest {
    private static final TenantName tenant1 = TenantName.from("tenant1");
    private static final TenantName tenant2 = TenantName.from("tenant2");
    private static final TenantName tenant3 = TenantName.from("tenant3");

    private TenantRepository tenantRepository;
    private TenantApplicationsTest.MockConfigActivationListener listener;
    private MockTenantListener tenantListener;
    private Curator curator;
    private ConfigserverConfig configserverConfig;

    @SuppressWarnings("deprecation")
    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setupSessions() throws IOException {
        curator = new MockCurator();
        listener = new TenantApplicationsTest.MockConfigActivationListener();
        tenantListener = new MockTenantListener();
        assertFalse(tenantListener.tenantsLoaded);
        configserverConfig = new ConfigserverConfig.Builder()
                .configServerDBDir(temporaryFolder.newFolder().getAbsolutePath())
                .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath())
                .build();
        tenantRepository = new TestTenantRepository.Builder().withConfigserverConfig(configserverConfig)
                                                             .withCurator(curator)
                                                             .withConfigActivationListener(listener)
                                                             .withTenantListener(tenantListener)
                                                             .build();
        assertTrue(tenantListener.tenantsLoaded);
        tenantRepository.addTenant(tenant1);
        tenantRepository.addTenant(tenant2);
    }

    @After
    public void closeSessions() {
        tenantRepository.close();
    }

    @Test
    public void testStartUp() {
        assertEquals(tenantRepository.getTenant(tenant1).getName(), tenant1);
        assertEquals(tenantRepository.getTenant(tenant2).getName(), tenant2);
    }

    @Test
    public void testListenersAdded() throws IOException, SAXException {
        TenantApplications applicationRepo = tenantRepository.getTenant(tenant1).getApplicationRepo();
        ApplicationId id = ApplicationId.from(tenant1, ApplicationName.defaultName(), InstanceName.defaultName());
        applicationRepo.createApplication(id);
        try (var transaction = new CuratorTransaction(curator)) {
            applicationRepo.createWriteActiveTransaction(transaction, id, 4).commit();
        }
        applicationRepo.activateApplication(ApplicationVersions.from(new Application(new VespaModel(MockApplicationPackage.createEmpty()),
                                                                                     new ServerCache(),
                                                                                     4L,
                                                                                     new Version(1, 2, 3),
                                                                                     MetricUpdater.createTestUpdater(),
                                                                                     id)),
                                            4);
        assertEquals(1, listener.activated.get());
    }

    @Test
    public void testTenantListenersNotified() throws Exception {
        tenantRepository.addTenant(tenant3);
        assertEquals("tenant3 not the last created tenant. Tenants: " + tenantRepository.getAllTenantNames() +
                             ", /config/v2/tenants: " + readZKChildren("/config/v2/tenants"),
                     tenant3, tenantListener.tenantCreatedName);
        tenantRepository.deleteTenant(tenant2);
        assertFalse(tenantRepository.getAllTenantNames().contains(tenant2));
        assertEquals(tenant2, tenantListener.tenantDeletedName);
    }

    @Test
    public void testAddTenant() throws Exception {
        Set<TenantName> allTenants = tenantRepository.getAllTenantNames();
        assertTrue(allTenants.contains(tenant1));
        assertTrue(allTenants.contains(tenant2));
        tenantRepository.addTenant(tenant3);
        assertZooKeeperTenantPathExists(tenant3);
        allTenants = tenantRepository.getAllTenantNames();
        assertTrue(allTenants.contains(tenant1));
        assertTrue(allTenants.contains(tenant2));
        assertTrue(allTenants.contains(tenant3));
    }

    @Test
    public void testDeleteTenant() throws Exception {
        assertZooKeeperTenantPathExists(tenant1);
        tenantRepository.deleteTenant(tenant1);
        assertFalse(tenantRepository.getAllTenantNames().contains(tenant1));

        try {
            tenantRepository.deleteTenant(TenantName.from("non-existing"));
            fail("deletion of non-existing tenant should fail");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDeletingDefaultTenant()  {
        try {
            assertZooKeeperTenantPathExists(TenantName.defaultName());
        } catch (Exception e) {
            fail("default tenant does not exist");
        }
        tenantRepository.deleteTenant(TenantName.defaultName());
    }
    
    @Test
    public void testTenantWatching() throws Exception {
        TenantName newTenant = TenantName.from("newTenant");
        List<TenantName> expectedTenants = Arrays.asList(TenantName.defaultName(), newTenant);
        try {
            tenantRepository.addTenant(newTenant);
            // Poll for the watcher to pick up the tenant from zk, and add it
            int tries=0;
            while(true) {
                if (tries > 5000) fail("Didn't react on watch");
                if (tenantRepository.getAllTenantNames().containsAll(expectedTenants)) {
                    break;
                }
                tries++;
                Thread.sleep(10);
            }
        } finally {
            assertTrue(tenantRepository.getAllTenantNames().containsAll(expectedTenants));
            tenantRepository.close();
        }
    }

    @Test
    public void testFailingBootstrap() {
        tenantRepository.close(); // stop using the one setup in Before method

        expectedException.expect(RuntimeException.class);
        expectedException.expectMessage("Could not create all tenants when bootstrapping, failed to create: [default]");
        new FailingDuringBootstrapTenantRepository(configserverConfig);
    }

    private List<String> readZKChildren(String path) throws Exception {
        return curator.framework().getChildren().forPath(path);
    }

    private void assertZooKeeperTenantPathExists(TenantName tenantName) throws Exception {
        assertNotNull(curator.framework().checkExists().forPath(TenantRepository.getTenantPath(tenantName).getAbsolute()));
    }

    private static class FailingDuringBootstrapTenantRepository extends TenantRepository {

        private static final FlagSource flagSource = new InMemoryFlagSource();

        public FailingDuringBootstrapTenantRepository(ConfigserverConfig configserverConfig) {
            super(new HostRegistry(),
                  new MockCurator(),
                  Metrics.createTestMetrics(),
                  new StripedExecutor<>(new InThreadExecutorService()),
                  new StripedExecutor<>(new InThreadExecutorService()),
                  new FileDistributionFactory(configserverConfig, new FileDirectory(configserverConfig)),
                  flagSource,
                  new InThreadExecutorService(),
                  new MockSecretStore(),
                  HostProvisionerProvider.empty(),
                  configserverConfig,
                  new ConfigServerDB(configserverConfig),
                  Zone.defaultZone(),
                  Clock.systemUTC(),
                  new ModelFactoryRegistry(List.of(VespaModelFactory.createTestFactory())),
                  new TestConfigDefinitionRepo(),
                  new TenantApplicationsTest.MockConfigActivationListener(),
                  new MockTenantListener(),
                  new ZookeeperServerConfig.Builder().myid(0).build(),
                  OnnxModelCost.disabled());
        }

        @Override
        public void bootstrapTenant(TenantName tenantName) {
            throw new RuntimeException("Failed to create: " + tenantName);
        }
    }

}