File: LearnerTest.java

package info (click to toggle)
zookeeper 3.4.9-3%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,388 kB
  • sloc: java: 57,077; xml: 17,979; cpp: 11,970; ansic: 9,383; sh: 2,662; python: 2,236; makefile: 257; perl: 114
file content (135 lines) | stat: -rw-r--r-- 4,765 bytes parent folder | download | duplicates (4)
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
/**
 * 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.quorum;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

import org.apache.jute.BinaryInputArchive;
import org.apache.jute.BinaryOutputArchive;
import org.apache.jute.Index;
import org.apache.jute.InputArchive;
import org.apache.jute.Record;
import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.server.ZKDatabase;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
import org.apache.zookeeper.server.quorum.Learner;
import org.apache.zookeeper.server.quorum.QuorumPeer;
import org.apache.zookeeper.txn.CreateTxn;
import org.apache.zookeeper.txn.TxnHeader;
import org.junit.Assert;
import org.junit.Test;

public class LearnerTest extends ZKTestCase {
  private static final File testData = new File(
    System.getProperty("test.data.dir", "build/test/data"));

	class SimpleLearnerZooKeeperServer extends LearnerZooKeeperServer {
		boolean startupCalled;
		
		public SimpleLearnerZooKeeperServer(FileTxnSnapLog ftsl, QuorumPeer self) throws IOException {
			super(ftsl, 2000, 2000, 2000, null, new ZKDatabase(ftsl), self);
		}
		Learner learner;
		@Override
		public Learner getLearner() {
			return learner;
		}
		
		@Override
		public void startup() {
			startupCalled = true;
		}
	}
	class SimpleLearner extends Learner {
		SimpleLearner(FileTxnSnapLog ftsl) throws IOException {
            self = QuorumPeer.testingQuorumPeer();
            zk = new SimpleLearnerZooKeeperServer(ftsl, self);
			((SimpleLearnerZooKeeperServer)zk).learner = this;
		}
	}
	static private void recursiveDelete(File dir) {
		if (dir == null || !dir.exists()) {
			return;
		}
		if (!dir.isDirectory()) {
			dir.delete();
		}
		for(File child: dir.listFiles()) {
			recursiveDelete(child);
		}
	}
	@Test
	public void syncTest() throws Exception {
		File tmpFile = File.createTempFile("test", ".dir", testData);
		tmpFile.delete();
		try {
			FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpFile, tmpFile);
			SimpleLearner sl = new SimpleLearner(ftsl);
			long startZxid = sl.zk.getLastProcessedZxid();
			
			// Set up bogus streams
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
			sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream());
			
			// make streams and socket do something innocuous
			sl.bufferedOutput = new BufferedOutputStream(System.out);
			sl.sock = new Socket();
			
			// fake messages from the server
			QuorumPacket qp = new QuorumPacket(Leader.SNAP, 0, null, null);
			oa.writeRecord(qp, null);
			sl.zk.getZKDatabase().serializeSnapshot(oa);
			oa.writeString("BenWasHere", "signature");
			TxnHeader hdr = new TxnHeader(0, 0, 0, 0, ZooDefs.OpCode.create);
			CreateTxn txn = new CreateTxn("/foo", new byte[0], new ArrayList<ACL>(), false, sl.zk.getZKDatabase().getNode("/").stat.getCversion());
	        ByteArrayOutputStream tbaos = new ByteArrayOutputStream();
	        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(tbaos);
	        hdr.serialize(boa, "hdr");
	        txn.serialize(boa, "txn");
	        tbaos.close();
			qp = new QuorumPacket(Leader.PROPOSAL, 1, tbaos.toByteArray(), null);
			oa.writeRecord(qp, null);

			// setup the messages to be streamed to follower
			sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
			
			try {
				sl.syncWithLeader(3);
			} catch(EOFException e) {}
			
			sl.zk.shutdown();
			sl = new SimpleLearner(ftsl);
			Assert.assertEquals(startZxid, sl.zk.getLastProcessedZxid());
		} finally {
			recursiveDelete(tmpFile);
		}
	}
}