Search in sources :

Example 6 with RequestedTokenMap

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));
}
Also used : RequestedTokenMap(com.emc.storageos.db.client.model.RequestedTokenMap) DbClient(com.emc.storageos.db.client.DbClient) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) RequestedTokenHelper(com.emc.storageos.security.geo.RequestedTokenHelper) StringSet(com.emc.storageos.db.client.model.StringSet) CoordinatorClient(com.emc.storageos.coordinator.client.service.CoordinatorClient) Test(org.junit.Test)

Example 7 with RequestedTokenMap

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()));
}
Also used : RequestedTokenMap(com.emc.storageos.db.client.model.RequestedTokenMap) StorageOSUserDAO(com.emc.storageos.db.client.model.StorageOSUserDAO) TokenOnWire(com.emc.storageos.security.authentication.TokenOnWire) SignedToken(com.emc.storageos.security.authentication.Base64TokenEncoder.SignedToken) ProxyToken(com.emc.storageos.db.client.model.ProxyToken) Token(com.emc.storageos.db.client.model.Token) BaseToken(com.emc.storageos.db.client.model.BaseToken) Test(org.junit.Test)

Aggregations

RequestedTokenMap (com.emc.storageos.db.client.model.RequestedTokenMap)7 Test (org.junit.Test)2 CoordinatorClient (com.emc.storageos.coordinator.client.service.CoordinatorClient)1 DbClient (com.emc.storageos.db.client.DbClient)1 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)1 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)1 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)1 BaseToken (com.emc.storageos.db.client.model.BaseToken)1 ProxyToken (com.emc.storageos.db.client.model.ProxyToken)1 StorageOSUserDAO (com.emc.storageos.db.client.model.StorageOSUserDAO)1 StringSet (com.emc.storageos.db.client.model.StringSet)1 Token (com.emc.storageos.db.client.model.Token)1 SignedToken (com.emc.storageos.security.authentication.Base64TokenEncoder.SignedToken)1 TokenOnWire (com.emc.storageos.security.authentication.TokenOnWire)1 RetryableSecurityException (com.emc.storageos.security.exceptions.RetryableSecurityException)1 SecurityException (com.emc.storageos.security.exceptions.SecurityException)1 RequestedTokenHelper (com.emc.storageos.security.geo.RequestedTokenHelper)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1