use of com.emc.storageos.db.client.model.RequestedTokenMap in project coprhd-controller by CoprHD.
the class TokenManagerTests method concurrentRequestedTokenMapUpdates.
/**
* This test updates two objects in the RequestedTokenMap CF. One for token1,
* one for token2. Each one is initially loaded with 10 entries:
* token1: vdc1, vdc2, vdc3 ...
* token2: vdc1, vdc2, vdc3 ...
* 10 adder threads are adding 10 more entries to each token. vdc-11, vdc-12 ...
* 10 remover threads are removing the 10 entries created initially
* The result is each token should end up with 10 entries, from the adder threads.
*
* @throws Exception
*/
@Test
public void concurrentRequestedTokenMapUpdates() throws Exception {
final DbClient sharedDbClient = getDbClient();
final CoordinatorClient sharedCoordinator = new TestCoordinator();
final RequestedTokenHelper requestedTokenMap = new RequestedTokenHelper();
requestedTokenMap.setDbClient(sharedDbClient);
requestedTokenMap.setCoordinator(sharedCoordinator);
// pre load the map with 10 entries for a given token
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.ADD_VDC, "token1", String.format("vdc%d", i));
}
// pre load the map with 10 entries for another token
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.ADD_VDC, "token2", String.format("vdc%d", i));
}
int numAdderThreads = 10;
int numRemoverThreads = 10;
int numThreads = numAdderThreads + numRemoverThreads;
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
final CountDownLatch waiter = new CountDownLatch(numThreads);
final class Adders implements Callable {
@Override
public Object call() throws Exception {
// synchronize all threads
waiter.countDown();
waiter.await();
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.ADD_VDC, "token1", String.format("vdc-1%d", i));
}
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.ADD_VDC, "token2", String.format("vdc-2%d", i));
}
return null;
}
}
final class Removers implements Callable {
@Override
public Object call() throws Exception {
// synchronize all threads
waiter.countDown();
waiter.await();
// pre load the map with 10 entries for a given token
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.REMOVE_VDC, "token1", String.format("vdc%d", i));
}
// pre load the map with 10 entries for another token
for (int i = 0; i < 10; i++) {
requestedTokenMap.addOrRemoveRequestingVDC(Operation.REMOVE_VDC, "token2", String.format("vdc%d", i));
}
return null;
}
}
for (int i = 0; i < numAdderThreads; i++) {
executor.submit(new Adders());
}
for (int i = 0; i < numRemoverThreads; i++) {
executor.submit(new Removers());
}
executor.shutdown();
Assert.assertTrue(executor.awaitTermination(60, TimeUnit.SECONDS));
// Verification. We should be left with just 10 entries for each token
// The 10 entries that were added by the adders.
RequestedTokenMap mapToken1 = requestedTokenMap.getTokenMap("token1");
Assert.assertEquals(10, mapToken1.getVDCIDs().size());
StringSet entries = mapToken1.getVDCIDs();
ArrayList<String> checkList = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
checkList.add(String.format("vdc-1%d", i));
}
Assert.assertTrue(entries.containsAll(checkList));
RequestedTokenMap mapToken2 = requestedTokenMap.getTokenMap("token2");
Assert.assertEquals(10, mapToken2.getVDCIDs().size());
StringSet entries2 = mapToken2.getVDCIDs();
ArrayList<String> checkList2 = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
checkList2.add(String.format("vdc-2%d", i));
}
Assert.assertTrue(entries2.containsAll(checkList2));
}
use of com.emc.storageos.db.client.model.RequestedTokenMap in project coprhd-controller by CoprHD.
the class TokenManagerTests method testRequestedTokenMapCleanup.
/**
* This test checks that when the TokenManager's cleanup thread is called,
* it deletes not only expired tokens but also their related RequestedTokenMap
* entry if it exists (and doesn't crash if there isn't one).
*/
@Test
public void testRequestedTokenMapCleanup() throws Exception {
commonDefaultSetupForSingleNodeTests();
// create a token
StorageOSUserDAO userDAO = new StorageOSUserDAO();
userDAO.setUserName("user1");
userDAO.setIsLocal(true);
final String token = _tokenManager.getToken(userDAO);
Assert.assertNotNull(token);
TokenOnWire tw1 = _encoder.decode(token);
Token tokenObj = _dbClient.queryObject(Token.class, tw1.getTokenId());
Assert.assertNotNull(tokenObj);
// add a requested map for this token
RequestedTokenMap map = new RequestedTokenMap();
map.setId(URIUtil.createId(RequestedTokenMap.class));
map.setTokenID(tokenObj.getId().toString());
map.addVDCID("vdc1");
_dbClient.persistObject(map);
// create a second token, no requested map entry this time.
final String token2 = _tokenManager.getToken(userDAO);
Assert.assertNotNull(token2);
TokenOnWire tw2 = _encoder.decode(token2);
Token tokenObj2 = _dbClient.queryObject(Token.class, tw2.getTokenId());
Assert.assertNotNull(tokenObj2);
Thread.sleep(3 * 60 * 1000);
_tokenManager.runCleanupNow();
Assert.assertNull(_dbClient.queryObject(Token.class, tw1.getTokenId()));
Assert.assertNull(_requestedTokenMapHelper.getTokenMap(tw1.getTokenId().toString()));
Assert.assertNull(_dbClient.queryObject(RequestedTokenMap.class, map.getId()));
Assert.assertNull(_dbClient.queryObject(Token.class, tw2.getTokenId()));
Assert.assertNull(_requestedTokenMapHelper.getTokenMap(tw2.getTokenId().toString()));
}
Aggregations