summaryrefslogtreecommitdiff
path: root/test/java/rep/upgrades/v47/repmgrtests/ConnectScript.java
blob: 718a805b7bfd60ca0c21ca936790bff3f17ad6df (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 * 
 * Copyright (c) 2010, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package repmgrtests;

import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.EventHandlerAdapter;
import com.sleepycat.db.ReplicationConfig;
import com.sleepycat.db.ReplicationHostAddress;
import com.sleepycat.db.ReplicationManagerAckPolicy;
import com.sleepycat.db.ReplicationManagerStartPolicy;
import com.sleepycat.db.ReplicationTimeoutType;
import com.sleepycat.db.VerboseConfig;

import static org.junit.Assert.*;

public class ConnectScript implements SimpleConnectTest.Ops47 {
    SimpleConnectTest.Config conf;
    private Environment master;

    public void setConfig(SimpleConnectTest.Config c) {
        conf = c;
    }

    public void createGroup() throws Exception {
        startMaster();

        int[] remotePorts;
        if (conf.reverse)
            remotePorts = SimpleConnectTest.noRemotePorts;
        else {
            remotePorts = new int[1];
            remotePorts[0] = conf.masterPort;
        }
        EnvironmentConfig ec = makeBasicConfig(conf.clientPort, remotePorts);
        MyEventHandler mon = new MyEventHandler();
        ec.setEventHandler(mon);
        Environment client = new Environment(conf.clientDir, ec);
        configEachEnv(client);
        client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);

        // wait for startup done
        mon.awaitStartupDone();

        // create a database
        DatabaseConfig dc = new DatabaseConfig();
        dc.setTransactional(true);
        dc.setAllowCreate(true);
        dc.setType(DatabaseType.BTREE);
        Database db = master.openDatabase(null, "test.db", null, dc);

        DatabaseEntry key = new DatabaseEntry("some key".getBytes());
        DatabaseEntry data = new DatabaseEntry("some data".getBytes());
        db.put(null, key, data);
        db.close();

        // shut down
        master.close();
        client.close();

        // run recovery on client
        client = new Environment(conf.clientDir, ec);
        client.close();
        Environment.remove(conf.clientDir, false, ec);
    }

    public void startMaster() throws Exception {
        int[] remotePorts;
        if (conf.reverse) {
            remotePorts = new int[1];
            remotePorts[0] = conf.clientPort;
        } else
            remotePorts = SimpleConnectTest.noRemotePorts;
        EnvironmentConfig ec = makeBasicConfig(conf.masterPort, remotePorts);
        master = new Environment(conf.masterDir, ec);
        configEachEnv(master);
        master.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_MASTER);
    }

    public void shutdownMaster() throws Exception {
        master.close();
    }

    private EnvironmentConfig makeBasicConfig(int myPort, int[] remotePorts)
        throws Exception
    {
        EnvironmentConfig ec = new EnvironmentConfig();
        ec.setAllowCreate(true);
        ec.setInitializeCache(true);
        ec.setInitializeLocking(true);
        ec.setInitializeLogging(true);
        ec.setInitializeReplication(true);
        ec.setTransactional(true);
        ec.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);
        ec.setRunRecovery(true);
        ec.setThreaded(true);
        ec.setReplicationNumSites(2);

        ec.setReplicationManagerLocalSite(new ReplicationHostAddress("localhost", myPort));
        for (int p : remotePorts) {
            ec.replicationManagerAddRemoteSite(new ReplicationHostAddress("localhost", p),
                                               false);
        }
        
        if (Boolean.getBoolean("VERB_REPLICATION"))
            ec.setVerbose(VerboseConfig.REPLICATION, true);
        return (ec);
    }

    private void configEachEnv(Environment e) throws Exception {
        e.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY,
                                1000000); // be impatient
        e.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
    }

    class MyEventHandler extends EventHandlerAdapter {
        private boolean done = false;
        private boolean panic = false;

        @Override synchronized public void handleRepStartupDoneEvent() {
            done = true;
            notifyAll();
        }

        @Override synchronized public void handlePanicEvent() {
            panic = true;
            done = true;
            notifyAll();
        }

        synchronized void awaitStartupDone() throws Exception {
            long deadline = System.currentTimeMillis() + 10000;
            while (!done) {
                long now = System.currentTimeMillis();
                if (now >= deadline)
                    throw new Exception("timeout expired");
                long duration = deadline - now;
                wait(duration);
            }
            if (panic)
                throw new Exception("aborted by panic in DB");
        }
    }
}