aboutsummaryrefslogtreecommitdiffstats
path: root/zookeeper-server/zookeeper-server/src/main/java/com/yahoo/vespa/zookeeper/VespaZooKeeperAdminImpl.java
blob: 891a35582b3b33080ff3cfc727f15c11f994ea56 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.zookeeper;

import com.yahoo.cloud.config.ZookeeperServerConfig;
import com.yahoo.net.HostName;
import com.yahoo.vespa.zookeeper.client.ZkClientConfigBuilder;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.admin.ZooKeeperAdmin;
import org.apache.zookeeper.data.ACL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.yolean.Exceptions.uncheck;

/**
 * @author hmusum
 */
@SuppressWarnings("unused") // Created by injection
public class VespaZooKeeperAdminImpl implements VespaZooKeeperAdmin {

    private static final Logger log = java.util.logging.Logger.getLogger(VespaZooKeeperAdminImpl.class.getName());

    @SuppressWarnings("try")
    @Override
    public void reconfigure(String connectionSpec, String servers) throws ReconfigException {
        try (ZooKeeperAdmin zooKeeperAdmin = createAdmin(connectionSpec)) {
            long fromConfig = -1;
            // Using string parameters because the List variant of reconfigure fails to join empty lists (observed on 3.5.6, fixed in 3.7.0).
            log.log(Level.INFO, "Applying ZooKeeper config: " + servers);
            byte[] appliedConfig = zooKeeperAdmin.reconfigure(null, null, servers, fromConfig, null);
            log.log(Level.INFO, "Applied ZooKeeper config: " + new String(appliedConfig, StandardCharsets.UTF_8));

            // Verify by issuing a write operation; this is only accepted once new quorum is obtained.
            List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
            String node = zooKeeperAdmin.create("/reconfigure-dummy-node", new byte[0], acl, CreateMode.EPHEMERAL_SEQUENTIAL);
            zooKeeperAdmin.delete(node, -1);

            log.log(Level.INFO, "Verified ZooKeeper config: " + new String(appliedConfig, StandardCharsets.UTF_8));
        }
        catch (   KeeperException.ReconfigInProgress
                | KeeperException.ConnectionLossException
                | KeeperException.NewConfigNoQuorum  e) {
            throw new ReconfigException(e);
        }
        catch (KeeperException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private ZooKeeperAdmin createAdmin(String connectionSpec) {
        return uncheck(() -> new ZooKeeperAdmin(connectionSpec, (int) sessionTimeout().toMillis(),
                                                (event) -> log.log(Level.FINE, event.toString()), new ZkClientConfigBuilder().toConfig()));
    }

    /** Creates a node in zookeeper, with hostname as part of node name, this ensures that server is up and working before returning */
    @SuppressWarnings("try")
    void createDummyNode(ZookeeperServerConfig zookeeperServerConfig) {
        int sleepTime = 2_000;
        try (ZooKeeperAdmin zooKeeperAdmin = createAdmin(localConnectionSpec(zookeeperServerConfig))) {
            Instant end = Instant.now().plus(Duration.ofMinutes(5));
            Exception exception = null;
            do {
                try {
                    zooKeeperAdmin.create("/dummy-node-" + HostName.getLocalhost(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    return;
                } catch (KeeperException e) {
                    if (e instanceof KeeperException.NodeExistsException) {
                        try {
                            zooKeeperAdmin.setData("/dummy-node-" + HostName.getLocalhost(), new byte[0], -1);
                            return;
                        } catch (KeeperException ex) {
                            log.log(Level.FINE, e.getMessage());
                            Thread.sleep(sleepTime);
                            continue;
                        }
                    }
                    log.log(Level.FINE, e.getMessage());
                    exception = e;
                    Thread.sleep(sleepTime);
                }
            } while (Instant.now().isBefore(end));
            throw new RuntimeException("Unable to create dummy node: ", exception);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}