/
BookieWriteLedgerTest.java
230 lines (201 loc) · 7.83 KB
/
BookieWriteLedgerTest.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
*
* 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.bookkeeper.client;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Random;
import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
import org.apache.bookkeeper.client.BookKeeper.DigestType;
import org.apache.bookkeeper.test.MultiLedgerManagerMultiDigestTestCase;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Testing ledger write entry cases
*/
public class BookieWriteLedgerTest extends
MultiLedgerManagerMultiDigestTestCase implements AddCallback {
private static Logger LOG = LoggerFactory
.getLogger(BookieWriteLedgerTest.class);
byte[] ledgerPassword = "aaa".getBytes();
LedgerHandle lh, lh2;
Enumeration<LedgerEntry> ls;
// test related variables
int numEntriesToWrite = 100;
int maxInt = Integer.MAX_VALUE;
Random rng; // Random Number Generator
ArrayList<byte[]> entries1; // generated entries
ArrayList<byte[]> entries2; // generated entries
DigestType digestType;
private static class SyncObj {
volatile int counter;
public SyncObj() {
counter = 0;
}
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
rng = new Random(System.currentTimeMillis()); // Initialize the Random
// Number Generator
entries1 = new ArrayList<byte[]>(); // initialize the entries list
entries2 = new ArrayList<byte[]>(); // initialize the entries list
}
public BookieWriteLedgerTest(String ledgerManagerFactory,
DigestType digestType) {
super(5);
this.digestType = digestType;
// set ledger manager
baseConf.setLedgerManagerFactoryClassName(ledgerManagerFactory);
baseClientConf.setLedgerManagerFactoryClassName(ledgerManagerFactory);
}
/**
* Verify write when few bookie failures in last ensemble and forcing
* ensemble reformation
*/
@Test(timeout=60000)
public void testWithMultipleBookieFailuresInLastEnsemble() throws Exception {
// Create a ledger
lh = bkc.createLedger(5, 4, digestType, ledgerPassword);
LOG.info("Ledger ID: " + lh.getId());
for (int i = 0; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(entry.array());
}
// Start three more bookies
startNewBookie();
startNewBookie();
startNewBookie();
// Shutdown three bookies in the last ensemble and continue writing
ArrayList<InetSocketAddress> ensemble = lh.getLedgerMetadata()
.getEnsembles().entrySet().iterator().next().getValue();
killBookie(ensemble.get(0));
killBookie(ensemble.get(1));
killBookie(ensemble.get(2));
int i = numEntriesToWrite;
numEntriesToWrite = numEntriesToWrite + 50;
for (; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(entry.array());
}
readEntries(lh, entries1);
lh.close();
}
/**
* Verify asynchronous writing when few bookie failures in last ensemble.
*/
@Test(timeout=60000)
public void testAsyncWritesWithMultipleFailuresInLastEnsemble()
throws Exception {
// Create ledgers
lh = bkc.createLedger(5, 4, digestType, ledgerPassword);
lh2 = bkc.createLedger(5, 4, digestType, ledgerPassword);
LOG.info("Ledger ID-1: " + lh.getId());
LOG.info("Ledger ID-2: " + lh2.getId());
for (int i = 0; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
entries2.add(entry.array());
lh.addEntry(entry.array());
lh2.addEntry(entry.array());
}
// Start three more bookies
startNewBookie();
startNewBookie();
startNewBookie();
// Shutdown three bookies in the last ensemble and continue writing
ArrayList<InetSocketAddress> ensemble = lh.getLedgerMetadata()
.getEnsembles().entrySet().iterator().next().getValue();
killBookie(ensemble.get(0));
killBookie(ensemble.get(1));
killBookie(ensemble.get(2));
// adding one more entry to both the ledgers async after multiple bookie
// failures. This will do asynchronously modifying the ledger metadata
// simultaneously.
numEntriesToWrite++;
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
entries2.add(entry.array());
SyncObj syncObj1 = new SyncObj();
SyncObj syncObj2 = new SyncObj();
lh.asyncAddEntry(entry.array(), this, syncObj1);
lh2.asyncAddEntry(entry.array(), this, syncObj2);
// wait for all entries to be acknowledged for the first ledger
synchronized (syncObj1) {
while (syncObj1.counter < 1) {
LOG.debug("Entries counter = " + syncObj1.counter);
syncObj1.wait();
}
}
// wait for all entries to be acknowledged for the second ledger
synchronized (syncObj2) {
while (syncObj2.counter < 1) {
LOG.debug("Entries counter = " + syncObj2.counter);
syncObj2.wait();
}
}
// reading ledger till the last entry
readEntries(lh, entries1);
readEntries(lh2, entries2);
lh.close();
lh2.close();
}
private void readEntries(LedgerHandle lh, ArrayList<byte[]> entries)
throws InterruptedException, BKException {
ls = lh.readEntries(0, numEntriesToWrite - 1);
int index = 0;
while (ls.hasMoreElements()) {
ByteBuffer origbb = ByteBuffer.wrap(entries.get(index++));
Integer origEntry = origbb.getInt();
ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
LOG.debug("Length of result: " + result.capacity());
LOG.debug("Original entry: " + origEntry);
Integer retrEntry = result.getInt();
LOG.debug("Retrieved entry: " + retrEntry);
assertTrue("Checking entry " + index + " for equality", origEntry
.equals(retrEntry));
}
}
@Override
public void addComplete(int rc, LedgerHandle lh, long entryId, Object ctx) {
if (rc != BKException.Code.OK)
fail("Return code is not OK: " + rc);
SyncObj x = (SyncObj) ctx;
synchronized (x) {
x.counter++;
x.notify();
}
}
}