forked from voldemort/voldemort
/
DynamicTimeoutStoreClientTest.java
161 lines (144 loc) · 7.36 KB
/
DynamicTimeoutStoreClientTest.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
/*
* Copyright 2013 LinkedIn, Inc
*
* Licensed 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 voldemort.coordinator;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import voldemort.ServerTestUtils;
import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.cluster.Cluster;
import voldemort.server.VoldemortServer;
import voldemort.store.CompositeGetVoldemortRequest;
import voldemort.store.CompositePutVoldemortRequest;
import voldemort.store.InsufficientOperationalNodesException;
import voldemort.store.socket.SocketStoreFactory;
import voldemort.store.socket.clientrequest.ClientRequestExecutorPool;
import voldemort.utils.ByteArray;
import voldemort.versioning.Versioned;
import voldemort.xml.ClusterMapper;
/**
* Class to test the Fat Client wrapper
*/
public class DynamicTimeoutStoreClientTest {
private VoldemortServer[] servers;
private Cluster cluster;
public static String socketUrl = "";
private static final String STORE_NAME = "slow-store-test";
private static final String STORES_XML = "test/common/voldemort/config/single-slow-store.xml";
private static final String SLOW_STORE_DELAY = "500";
private final SocketStoreFactory socketStoreFactory = new ClientRequestExecutorPool(2,
10000,
100000,
32 * 1024);
private DynamicTimeoutStoreClient<ByteArray, byte[]> dynamicTimeoutClient = null;
/**
* Setup a one node Voldemort cluster with a 'slow' store
* (SlowStorageEngine) with a delay of 500 ms for get and put.
*
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
int numServers = 1;
servers = new VoldemortServer[numServers];
int partitionMap[][] = { { 0, 2, 4, 6, 1, 3, 5, 7 } };
Properties props = new Properties();
props.setProperty("storage.configs",
"voldemort.store.bdb.BdbStorageConfiguration,voldemort.store.slow.SlowStorageConfiguration");
props.setProperty("testing.slow.queueing.get.ms", SLOW_STORE_DELAY);
props.setProperty("testing.slow.queueing.put.ms", SLOW_STORE_DELAY);
cluster = ServerTestUtils.startVoldemortCluster(numServers,
servers,
partitionMap,
socketStoreFactory,
true, // useNio
null,
STORES_XML,
props);
socketUrl = servers[0].getIdentityNode().getSocketUrl().toString();
String bootstrapUrl = socketUrl;
ClientConfig clientConfig = new ClientConfig().setBootstrapUrls(bootstrapUrl)
.setEnableCompressionLayer(false)
.setEnableSerializationLayer(false)
.enableDefaultClient(true)
.setEnableLazy(false);
String storesXml = FileUtils.readFileToString(new File(STORES_XML), "UTF-8");
ClusterMapper mapper = new ClusterMapper();
this.dynamicTimeoutClient = new DynamicTimeoutStoreClient<ByteArray, byte[]>(STORE_NAME,
new SocketStoreClientFactory(clientConfig),
1,
storesXml,
mapper.writeCluster(cluster));
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
if(this.socketStoreFactory != null) {
this.socketStoreFactory.close();
}
}
/**
* Test the dynamic per call timeout. We do a regular put with the default
* configured timeout. We then do a put with a dynamic timeout of 200 ms
* which is less than the delay at the server side. After this we do a get
* with a dynamic timeout of 1500 ms which should succeed and return the
* value from the first put.
*/
@Test
public void test() {
long incorrectTimeout = 200;
long correctTimeout = 1500;
String key = "a";
String value = "First";
String newValue = "Second";
try {
this.dynamicTimeoutClient.put(new ByteArray(key.getBytes()), value.getBytes());
} catch(Exception e) {
fail("Error in regular put.");
}
long startTime = System.currentTimeMillis();
try {
this.dynamicTimeoutClient.putWithCustomTimeout(new CompositePutVoldemortRequest<ByteArray, byte[]>(new ByteArray(key.getBytes()),
newValue.getBytes(),
incorrectTimeout));
fail("Should not reach this point. The small (incorrect) timeout did not work.");
} catch(InsufficientOperationalNodesException ion) {
System.out.println("This failed as Expected.");
}
try {
Versioned<byte[]> versionedValue = this.dynamicTimeoutClient.getWithCustomTimeout(new CompositeGetVoldemortRequest<ByteArray, byte[]>(new ByteArray(key.getBytes()),
correctTimeout,
true));
long endTime = System.currentTimeMillis();
System.out.println("Total time taken = " + (endTime - startTime));
String response = new String(versionedValue.getValue());
if(!response.equals(value)) {
fail("The returned value does not match. Expected: " + value + " but Received: "
+ response);
}
} catch(Exception e) {
e.printStackTrace();
fail("The dynamic per call timeout did not work !");
}
}
}