summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/test/java/org/apache/zookeeper/server/SnapshotDigestTest.java
blob: 4e8c6f8e2c5228b38585a51d590e0c6f7400f315 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.zookeeper.server;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.server.metric.SimpleCounter;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
import org.apache.zookeeper.server.quorum.QuorumPeerMainTest;
import org.apache.zookeeper.test.ClientBase;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SnapshotDigestTest extends ClientBase {

    private static final Logger LOG = LoggerFactory.getLogger(SnapshotDigestTest.class);

    private ZooKeeper zk;
    private ZooKeeperServer server;

    @BeforeEach
    public void setUp() throws Exception {
        super.setUp();
        server = serverFactory.getZooKeeperServer();
        zk = createClient();
    }

    @AfterEach
    public void tearDown() throws Exception {
        // server will be closed in super.tearDown
        super.tearDown();

        if (zk != null) {
            zk.close();
        }
    }

    @Override
    public void setupCustomizedEnv() {
        ZooKeeperServer.setDigestEnabled(true);
        System.setProperty(ZooKeeperServer.SNAP_COUNT, "100");
    }

    @Override
    public void cleanUpCustomizedEnv() {
        ZooKeeperServer.setDigestEnabled(false);
        System.clearProperty(ZooKeeperServer.SNAP_COUNT);
    }

    /**
     * Check snapshot digests when loading a fuzzy or non-fuzzy snapshot.
     */
    @Test
    public void testSnapshotDigest() throws Exception {
        // take a empty snapshot without creating any txn and make sure
        // there is no digest mismatch issue
        server.takeSnapshot();
        reloadSnapshotAndCheckDigest();

        // trigger various write requests
        String pathPrefix = "/testSnapshotDigest";
        for (int i = 0; i < 1000; i++) {
            String path = pathPrefix + i;
            zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        // update the data of first node
        String firstNode = pathPrefix + 0;
        zk.setData(firstNode, "new_setdata".getBytes(), -1);

        // delete the first node
        zk.delete(firstNode, -1);

        // trigger multi op
        List<Op> subTxns = new ArrayList<Op>();
        for (int i = 0; i < 3; i++) {
            String path = pathPrefix + "-m" + i;
            subTxns.add(Op.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        }
        zk.multi(subTxns);

        reloadSnapshotAndCheckDigest();

        // Take a snapshot and test the logic when loading a non-fuzzy snapshot
        server = serverFactory.getZooKeeperServer();
        server.takeSnapshot();

        reloadSnapshotAndCheckDigest();
    }

    /**
     * Make sure the code will skip digest check when it's comparing
     * digest with different version.
     *
     * This enables us to smoonthly add new fields into digest or using
     * new digest calculation.
     */
    @Test
    public void testDifferentDigestVersion() throws Exception {
        // check the current digest version
        int currentVersion = new DigestCalculator().getDigestVersion();

        // create a node
        String path = "/testDifferentDigestVersion";
        zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        // take a full snapshot
        server.takeSnapshot();

        //increment the digest version
        int newVersion = currentVersion + 1;
        DigestCalculator newVersionDigestCalculator = Mockito.spy(DigestCalculator.class);
        Mockito.when(newVersionDigestCalculator.getDigestVersion()).thenReturn(newVersion);
        assertEquals(newVersion, newVersionDigestCalculator.getDigestVersion());

        // using mock to return different digest value when the way we
        // calculate digest changed
        FileTxnSnapLog txnSnapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        DataTree dataTree = Mockito.spy(new DataTree(newVersionDigestCalculator));
        Mockito.when(dataTree.getTreeDigest()).thenReturn(0L);
        txnSnapLog.restore(dataTree, new ConcurrentHashMap<>(), Mockito.mock(FileTxnSnapLog.PlayBackListener.class));

        // make sure the reportDigestMismatch function is never called
        Mockito.verify(dataTree, Mockito.never()).reportDigestMismatch(Mockito.anyLong());
    }

    /**
     * Make sure it's backward compatible, and also we can rollback this
     * feature without corrupt the database.
     */
    @Test
    public void testBackwardCompatible() throws Exception {
        testCompatibleHelper(false, true);

        testCompatibleHelper(true, false);
    }

    private void testCompatibleHelper(Boolean enabledBefore, Boolean enabledAfter) throws Exception {

        ZooKeeperServer.setDigestEnabled(enabledBefore);
        ZooKeeperServer.setSerializeLastProcessedZxidEnabled(enabledBefore);

        // restart the server to cache the option change
        reloadSnapshotAndCheckDigest();

        // create a node
        String path = "/testCompatible" + "-" + enabledBefore + "-" + enabledAfter;
        zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        // take a full snapshot
        server.takeSnapshot();

        ZooKeeperServer.setDigestEnabled(enabledAfter);
        ZooKeeperServer.setSerializeLastProcessedZxidEnabled(enabledAfter);

        reloadSnapshotAndCheckDigest();

        assertEquals(path, new String(zk.getData(path, false, null)));
    }

    private void reloadSnapshotAndCheckDigest() throws Exception {
        stopServer();
        QuorumPeerMainTest.waitForOne(zk, States.CONNECTING);

        ((SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT).reset();

        startServer();
        QuorumPeerMainTest.waitForOne(zk, States.CONNECTED);

        server = serverFactory.getZooKeeperServer();

        // Snapshot digests always match
        assertEquals(0L, ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT.get());

        // reset the digestFromLoadedSnapshot after comparing
        assertNull(server.getZKDatabase().getDataTree().getDigestFromLoadedSnapshot());
    }

}