-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BlockStoreContextTest.java
159 lines (139 loc) · 6.31 KB
/
BlockStoreContextTest.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
/*
* Licensed to the University of California, Berkeley 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 tachyon.client.block;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.Lists;
import tachyon.Constants;
import tachyon.client.ClientContext;
import tachyon.client.worker.BlockWorkerClient;
/**
* Tests {@link BlockStoreContext}.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({BlockStoreContext.class, BlockWorkerClient.class, BlockWorkerClientPool.class})
public final class BlockStoreContextTest {
/**
* This test ensures acquiring all the available BlockStore master clients blocks further
* requests for clients. It also ensures clients are available for reuse after they are released
* by the previous owners. If the test takes longer than 10 seconds, a deadlock most likely
* occurred preventing the release of the master clients.
*
* @throws Exception if an unexpected error occurs during the test
*/
@Test(timeout = 10000)
public void acquireMasterLimitTest() throws Exception {
final List<BlockMasterClient> clients = Lists.newArrayList();
// Acquire all the clients
for (int i = 0; i < ClientContext.getConf()
.getInt(Constants.USER_BLOCK_MASTER_CLIENT_THREADS); i ++) {
clients.add(BlockStoreContext.INSTANCE.acquireMasterClient());
}
// Spawn another thread to acquire a master client
Thread acquireThread = new Thread(new AcquireMasterClient());
acquireThread.start();
// Wait for the spawned thread to complete. If it is able to acquire a master client before
// the defined timeout, fail
long timeoutMs = Constants.SECOND_MS / 2;
long start = System.currentTimeMillis();
acquireThread.join(timeoutMs);
if (System.currentTimeMillis() - start < timeoutMs) {
Assert.fail("Acquired a master client when the client pool was full.");
}
// Release all the clients
for (BlockMasterClient client : clients) {
BlockStoreContext.INSTANCE.releaseMasterClient(client);
}
// Wait for the spawned thread to complete. If it is unable to acquire a master client before
// the defined timeout, fail.
timeoutMs = 5 * Constants.SECOND_MS;
start = System.currentTimeMillis();
acquireThread.join(timeoutMs);
if (System.currentTimeMillis() - start >= timeoutMs) {
Assert.fail("Failed to acquire a master client within " + timeoutMs + "ms. Deadlock?");
}
}
class AcquireMasterClient implements Runnable {
@Override
public void run() {
BlockMasterClient client = BlockStoreContext.INSTANCE.acquireMasterClient();
BlockStoreContext.INSTANCE.releaseMasterClient(client);
}
}
/**
* This test ensures acquiring all the available BlockStore worker clients blocks further requests
* for clients. It also ensures clients are available for reuse after they are released by the
* previous owners. If the test takes longer than 10 seconds, a deadlock most likely occurred
* preventing the release of the worker clients.
*
* @throws Exception if an unexpected error occurs during the test
*/
@Test(timeout = 10000)
public void acquireWorkerLimitTest() throws Exception {
// Use mocks for the block worker client to prevent it from trying to invoke the session
// heartbeat RPC.
BlockWorkerClient mock = PowerMockito.mock(BlockWorkerClient.class);
PowerMockito.doNothing().when(mock).sessionHeartbeat();
PowerMockito.doReturn(true).when(mock).isLocal();
PowerMockito
.whenNew(BlockWorkerClient.class)
.withArguments(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyLong(),
Mockito.anyBoolean(), Mockito.any()).thenReturn(mock);
final List<BlockWorkerClient> clients = Lists.newArrayList();
// Acquire all the clients
for (int i = 0; i < ClientContext.getConf()
.getInt(Constants.USER_BLOCK_WORKER_CLIENT_THREADS); i ++) {
clients.add(BlockStoreContext.INSTANCE.acquireWorkerClient());
}
// Spawn another thread to acquire a worker client
Thread acquireThread = new Thread(new AcquireWorkerClient());
acquireThread.start();
// Wait for the spawned thread to complete. If it is able to acquire a worker client before
// the defined timeout, fail
long timeoutMs = Constants.SECOND_MS / 2;
long start = System.currentTimeMillis();
acquireThread.join(timeoutMs);
if (System.currentTimeMillis() - start < timeoutMs) {
Assert.fail("Acquired a worker client when the client pool was full.");
}
// Release all the clients
// Set the RPC number of retries to -1 to prevent the worker client from trying to send a
// heartbeat message when it is released.
for (BlockWorkerClient client : clients) {
BlockStoreContext.INSTANCE.releaseWorkerClient(client);
}
// Wait for the spawned thread to complete. If it is unable to acquire a worker client before
// the defined timeout, fail.
timeoutMs = 5 * Constants.SECOND_MS;
start = System.currentTimeMillis();
acquireThread.join(timeoutMs);
if (System.currentTimeMillis() - start >= timeoutMs) {
Assert.fail("Failed to acquire a worker client within " + timeoutMs + "ms. Deadlock?");
}
}
class AcquireWorkerClient implements Runnable {
@Override
public void run() {
BlockWorkerClient client = BlockStoreContext.INSTANCE.acquireWorkerClient();
BlockStoreContext.INSTANCE.releaseWorkerClient(client);
}
}
}