summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/RpcTester.java
blob: dd66f720b1f2e0fb2b369955deac9fb1feabeaf7 (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
// Copyright 2017 Yahoo Holdings. 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.HostLivenessTracker;
import com.yahoo.config.provision.TenantName;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.net.HostName;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.GenerationCounter;
import com.yahoo.vespa.config.server.*;
import com.yahoo.vespa.config.server.filedistribution.FileServer;
import com.yahoo.vespa.config.server.host.ConfigRequestHostLivenessTracker;
import com.yahoo.vespa.config.server.host.HostRegistries;
import com.yahoo.vespa.config.server.monitoring.Metrics;
import com.yahoo.vespa.config.server.tenant.MockTenantProvider;
import com.yahoo.vespa.config.server.tenant.TenantHandlerProvider;
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 com.yahoo.vespa.config.server.SuperModelRequestHandlerTest.emptyNodeFlavors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
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 String myHostname = HostName.getLocalhost();
    private final HostLivenessTracker hostLivenessTracker = new ConfigRequestHostLivenessTracker(clock);

    private RpcServer rpcServer;
    private MockTenantProvider tenantProvider;
    private GenerationCounter generationCounter;
    private Thread t;
    private Supervisor sup;
    private Spec spec;

    private List<Integer> allocatedPorts;

    private final TemporaryFolder temporaryFolder;
    private final ConfigserverConfig configserverConfig;

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

    RpcTester(TemporaryFolder temporaryFolder, ConfigserverConfig.Builder configBuilder) throws InterruptedException, IOException {
        this.temporaryFolder = temporaryFolder;
        allocatedPorts = new ArrayList<>();
        int port = allocatePort();
        spec = createSpec(port);
        tenantProvider = new MockTenantProvider();
        generationCounter = new MemoryGenerationCounter();
        configBuilder.rpcport(port);
        configserverConfig = new ConfigserverConfig(configBuilder);
        createAndStartRpcServer();
        assertFalse(hostLivenessTracker.lastRequestFrom(myHostname).isPresent());
    }

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

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

    void createAndStartRpcServer() throws IOException {
        rpcServer = new RpcServer(configserverConfig,
                                  new SuperModelRequestHandler(new TestConfigDefinitionRepo(),
                                                               configserverConfig,
                                                               new SuperModelManager(
                                                                       configserverConfig,
                                                                       emptyNodeFlavors(),
                                                                       generationCounter,
                                                                       new InMemoryFlagSource())),
                                  Metrics.createTestMetrics(), new HostRegistries(),
                                  hostLivenessTracker, new FileServer(temporaryFolder.newFolder()));
        rpcServer.onTenantCreate(TenantName.from("default"), tenantProvider);
        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);
        assertThat(req.returnValues().get(0).asInt32(), is(0));
    }

    void performRequest(Request req) {
        clock.advance(Duration.ofMillis(10));
        sup.connect(spec).invokeSync(req, 120.0);
        if (req.methodName().equals(RpcServer.getConfigMethodName))
            assertEquals(clock.instant(), hostLivenessTracker.lastRequestFrom(myHostname).get());
    }

    RpcServer rpcServer() {
        return rpcServer;
    }

    TenantHandlerProvider tenantProvider() {
        return tenantProvider;
    }

    GenerationCounter generationCounter() {
        return generationCounter;
    }
}