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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import com.yahoo.path.Path;
import com.yahoo.text.Utf8;
import com.yahoo.transaction.Transaction;
import com.yahoo.vespa.config.server.*;

import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.tenant.Tenant;
import com.yahoo.vespa.config.server.tenant.TenantBuilder;
import com.yahoo.vespa.curator.Curator;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.vespa.config.server.zookeeper.ConfigCurator;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.LongPredicate;

/**
 * @author lulf
 * @since 5.1
 */
public class RemoteSessionRepoTest extends TestWithCurator {

    private RemoteSessionRepo remoteSessionRepo;

    @Before
    public void setupFacade() throws Exception {
        createSession(2l, false);
        createSession(3l, false);
        curator.create(Path.fromString("/applications"));
        curator.create(Path.fromString("/sessions"));
        Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(),
                                             TenantName.defaultName(),
                                             Path.createRoot()).build();
        this.remoteSessionRepo = tenant.getRemoteSessionRepo();
    }

    private void createSession(long sessionId, boolean wait) {
        createSession("", sessionId, wait);
    }


    private void createSession(String root, long sessionId, boolean wait) {
        Path rootPath = Path.fromString(root).append("sessions");
        curator.create(rootPath);
        SessionZooKeeperClient zkc = new SessionZooKeeperClient(curator, rootPath.append(String.valueOf(sessionId)));
        zkc.createNewSession(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        if (wait) {
            Curator.CompletionWaiter waiter = zkc.getUploadWaiter();
            waiter.awaitCompletion(Duration.ofSeconds(120));
        }
    }

    @Test
    public void testInitialize() {
        assertSessionExists(2l);
        assertSessionExists(3l);
    }

    @Test
    public void testCreateSession() throws Exception {
        createSession(0l, true);
        assertSessionExists(0l);
    }

    @Test
    public void testSessionStateChange() throws Exception {
        Path session = Path.fromString("/sessions/0");
        createSession(0l, true);
        assertSessionStatus(0l, Session.Status.NEW);
        assertStatusChange(0l, Session.Status.PREPARE);
        assertStatusChange(0l, Session.Status.ACTIVATE);

        curator.delete(session);
        assertSessionRemoved(0l);
        assertNull(remoteSessionRepo.getSession(0l));
    }

    @Test
    public void testBadApplicationRepoOnActivate() throws Exception {
        TenantApplications applicationRepo = new FailingTenantApplications();
        curator.framework().create().forPath("/mytenant");
        Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(),
                                             TenantName.from("mytenant"),
                                             Path.fromString("mytenant"))
                .withApplicationRepo(applicationRepo)
                .build();
        remoteSessionRepo = tenant.getRemoteSessionRepo();
        createSession("/mytenant", 2l, true);
        assertThat(remoteSessionRepo.listSessions().size(), is(1));
    }

    private void assertStatusChange(long sessionId, Session.Status status) throws Exception {
        Path statePath = Path.fromString("/sessions/" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH);
        curator.create(statePath);
        curatorFramework.setData().forPath(statePath.getAbsolute(), Utf8.toBytes(status.toString()));
        System.out.println("Setting status " + status + " for " + sessionId);
        assertSessionStatus(0l, status);
    }

    private void assertSessionRemoved(long sessionId) {
        waitFor(p -> remoteSessionRepo.getSession(sessionId) == null, sessionId);
        assertNull(remoteSessionRepo.getSession(sessionId));
    }

    private void assertSessionExists(long sessionId) {
        assertSessionStatus(sessionId, Session.Status.NEW);
    }

    private void assertSessionStatus(long sessionId, Session.Status status) {
        waitFor(p -> remoteSessionRepo.getSession(sessionId) != null &&
                remoteSessionRepo.getSession(sessionId).getStatus() == status, sessionId);
        assertNotNull(remoteSessionRepo.getSession(sessionId));
        assertThat(remoteSessionRepo.getSession(sessionId).getStatus(), is(status));
    }

    private void waitFor(LongPredicate predicate, long sessionId) {
        long endTime = System.currentTimeMillis() + 60_000;
        boolean ok;
        do {
            ok = predicate.test(sessionId);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (System.currentTimeMillis() < endTime && !ok);
    }

    private class FailingTenantApplications implements TenantApplications {

        @Override
        public List<ApplicationId> listApplications() {
            return Collections.singletonList(ApplicationId.defaultId());
        }

        @Override
        public Transaction createPutApplicationTransaction(ApplicationId applicationId, long sessionId) {
            return null;
        }

        @Override
        public long getSessionIdForApplication(ApplicationId applicationId) {
            throw new IllegalArgumentException("Bad id " + applicationId);
        }

        @Override
        public Transaction deleteApplication(ApplicationId applicationId) {
            return null;
        }

        @Override
        public void close() {

        }
    }
}