summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/RpcTester.java
blob: 8770970308ad0c9f5eaa93c6e6f8e623b1156f89 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.rpc;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.MemoryGenerationCounter;
import com.yahoo.vespa.config.server.PortRangeAllocator;
import com.yahoo.vespa.config.server.SuperModelManager;
import com.yahoo.vespa.config.server.SuperModelRequestHandler;
import com.yahoo.vespa.config.server.TestConfigDefinitionRepo;
import com.yahoo.vespa.config.server.application.OrchestratorMock;
import com.yahoo.vespa.config.server.filedistribution.FileDirectory;
import com.yahoo.vespa.config.server.filedistribution.FileServer;
import com.yahoo.vespa.config.server.host.HostRegistry;
import com.yahoo.vespa.config.server.monitoring.Metrics;
import com.yahoo.vespa.config.server.rpc.security.NoopRpcAuthorizer;
import com.yahoo.vespa.config.server.tenant.Tenant;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.config.server.tenant.TestTenantRepository;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import org.junit.After;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

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

/**
 * Tester for running rpc server.
 *
 * @author Ulf Lilleengen
 */
public class RpcTester implements AutoCloseable {

    private final ManualClock clock = new ManualClock(Instant.ofEpochMilli(100));
    private final Spec spec;

    private final RpcServer rpcServer;
    private Thread t;
    private Supervisor sup;
    private final ApplicationId applicationId;
    private final TenantName tenantName;
    private final TenantRepository tenantRepository;
    final HostRegistry hostRegistry = new HostRegistry();

    private final ApplicationRepository applicationRepository;
    private final List<Integer> allocatedPorts = new ArrayList<>();
    private final TemporaryFolder temporaryFolder;
    private final ConfigserverConfig configserverConfig;

    RpcTester(ApplicationId applicationId, TemporaryFolder temporaryFolder) throws InterruptedException, IOException {
        this(applicationId, temporaryFolder, new ConfigserverConfig.Builder());
    }

    RpcTester(ApplicationId applicationId, TemporaryFolder temporaryFolder, ConfigserverConfig.Builder configBuilder) throws InterruptedException, IOException {
        this.temporaryFolder = temporaryFolder;
        this.applicationId = applicationId;
        this.tenantName = applicationId.tenant();
        int port = allocatePort();
        spec = createSpec(port);
        configBuilder.rpcport(port)
                .configServerDBDir(temporaryFolder.newFolder().getAbsolutePath())
                .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath())
                .fileReferencesDir(temporaryFolder.newFolder().getAbsolutePath());
        configserverConfig = new ConfigserverConfig(configBuilder);
        rpcServer = createRpcServer(configserverConfig);
        tenantRepository = new TestTenantRepository.Builder()
                .withHostRegistry(hostRegistry)
                .withConfigserverConfig(configserverConfig)
                .build();
        tenantRepository.addTenant(tenantName);
        startRpcServer();
        applicationRepository = new ApplicationRepository.Builder()
                .withTenantRepository(tenantRepository)
                .withConfigserverConfig(configserverConfig)
                .withOrchestrator(new OrchestratorMock())
                .build();
    }

    public void close() {
        rpcServer.stop();
        for (Integer port : allocatedPorts) {
            PortRangeAllocator.releasePort(port);
        }
    }

    private int allocatePort() throws InterruptedException {
        int port = PortRangeAllocator.findAvailablePort();
        allocatedPorts.add(port);
        return port;
    }

    RpcServer createRpcServer(ConfigserverConfig config) throws IOException {
        InMemoryFlagSource flagSource = new InMemoryFlagSource();
        RpcServer rpcServer = new RpcServer(config,
                                            new SuperModelRequestHandler(new TestConfigDefinitionRepo(),
                                                          configserverConfig,
                                                          new SuperModelManager(
                                                                  config,
                                                                  Zone.defaultZone(),
                                                                  new MemoryGenerationCounter(),
                                                                  flagSource)),
                                            Metrics.createTestMetrics(),
                                            hostRegistry,
                                            new FileServer(configserverConfig, new FileDirectory(temporaryFolder.newFolder())),
                                            new NoopRpcAuthorizer(),
                                            new RpcRequestHandlerProvider());
        rpcServer.setUpGetConfigHandlers();
        return rpcServer;
    }

    void startRpcServer()  {
        hostRegistry.update(applicationId, List.of("localhost"));
        rpcServer.onTenantCreate(tenantRepository.getTenant(tenantName));
        t = new Thread(rpcServer);
        t.start();
        sup = new Supervisor(new Transport());
        pingServer();
    }

    @After
    public void stopRpc() throws InterruptedException {
        rpcServer.stop();
        t.join();
    }

    private Spec createSpec(int port) {
        return new Spec("tcp/localhost:" + port);
    }

    private void pingServer() {
        long endTime = System.currentTimeMillis() + 60_000;
        Request req = new Request("ping");
        while (System.currentTimeMillis() < endTime) {
            performRequest(req);
            if (!req.isError() && req.returnValues().size() > 0 && req.returnValues().get(0).asInt32() == 0) {
                break;
            }
            req = new Request("ping");
        }
        assertFalse(req.isError());
        assertTrue(req.returnValues().size() > 0);
        assertEquals(0, req.returnValues().get(0).asInt32());
    }

    void performRequest(Request req) {
        clock.advance(Duration.ofMillis(10));
        sup.connect(spec).invokeSync(req, Duration.ofSeconds(10));
    }

    RpcServer rpcServer() {
        return rpcServer;
    }

    Tenant tenant() { return tenantRepository.getTenant(tenantName); }

    public ApplicationRepository applicationRepository() { return applicationRepository; }

}