Search in sources :

Example 56 with PoolImpl

use of org.apache.geode.cache.client.internal.PoolImpl in project geode by apache.

the class CacheCreation method sameAs.

/**
   * Returns whether or not this {@code CacheCreation} is equivalent to another {@code Cache}.
   */
public boolean sameAs(Cache other) {
    boolean sameConfig = other.getLockLease() == this.getLockLease() && other.getLockTimeout() == this.getLockTimeout() && other.getSearchTimeout() == this.getSearchTimeout() && other.getMessageSyncInterval() == this.getMessageSyncInterval() && other.getCopyOnRead() == this.getCopyOnRead() && other.isServer() == this.isServer();
    if (!sameConfig) {
        throw new RuntimeException(LocalizedStrings.CacheCreation_SAMECONFIG.toLocalizedString());
    } else {
        DynamicRegionFactory.Config drc1 = this.getDynamicRegionFactoryConfig();
        if (drc1 != null) {
            // we have a dynamic region factory
            DynamicRegionFactory.Config drc2 = null;
            if (other instanceof CacheCreation) {
                drc2 = ((CacheCreation) other).getDynamicRegionFactoryConfig();
            } else {
                drc2 = DynamicRegionFactory.get().getConfig();
            }
            if (drc2 == null) {
                return false;
            }
            if (!drc1.equals(drc2)) {
                return false;
            }
        } else {
            // we have no dynamic region factory; how about other?
            if (other instanceof CacheCreation) {
                if (((CacheCreation) other).getDynamicRegionFactoryConfig() != null) {
                    return false;
                }
            } else {
                // other must be real cache in which case we compare to DynamicRegionFactory
                if (DynamicRegionFactory.get().isOpen()) {
                    return false;
                }
            }
        }
        Collection<CacheServer> myBridges = this.getCacheServers();
        Collection<CacheServer> otherBridges = other.getCacheServers();
        if (myBridges.size() != otherBridges.size()) {
            throw new RuntimeException(LocalizedStrings.CacheCreation_CACHESERVERS_SIZE.toLocalizedString());
        }
        for (CacheServer myBridge1 : myBridges) {
            CacheServerCreation myBridge = (CacheServerCreation) myBridge1;
            boolean found = false;
            for (CacheServer otherBridge : otherBridges) {
                if (myBridge.sameAs(otherBridge)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new RuntimeException(LocalizedStrings.CacheCreation_CACHE_SERVER_0_NOT_FOUND.toLocalizedString(myBridge));
            }
        }
        // compare connection pools
        Map<String, Pool> m1 = getPools();
        Map<String, Pool> m2 = other instanceof CacheCreation ? ((CacheCreation) other).getPools() : PoolManager.getAll();
        int m1Size = m1.size();
        // ignore any gateway instances
        for (Pool cp : m1.values()) {
            if (((PoolImpl) cp).isUsedByGateway()) {
                m1Size--;
            }
        }
        int m2Size = m2.size();
        // ignore any gateway instances
        for (Pool cp : m2.values()) {
            if (((PoolImpl) cp).isUsedByGateway()) {
                m2Size--;
            }
        }
        if (m2Size == 1) {
            // if it is just the DEFAULT pool then ignore it
            Pool p = (Pool) m2.values().iterator().next();
            if (p.getName().equals("DEFAULT")) {
                m2Size = 0;
            }
        }
        if (m1Size != m2Size) {
            throw new RuntimeException("pool sizes differ m1Size=" + m1Size + " m2Size=" + m2Size + " m1=" + m1.values() + " m2=" + m2.values());
        }
        if (m1Size > 0) {
            for (Pool pool : m1.values()) {
                PoolImpl poolImpl = (PoolImpl) pool;
                // ignore any gateway instances
                if (!poolImpl.isUsedByGateway()) {
                    poolImpl.sameAs(m2.get(poolImpl.getName()));
                }
            }
        }
        // compare disk stores
        for (DiskStore diskStore : this.diskStores.values()) {
            DiskStoreAttributesCreation dsac = (DiskStoreAttributesCreation) diskStore;
            String name = dsac.getName();
            DiskStore ds = other.findDiskStore(name);
            if (ds == null) {
                getLogger().fine("Disk store " + name + " not found.");
                throw new RuntimeException(LocalizedStrings.CacheCreation_DISKSTORE_NOTFOUND_0.toLocalizedString(name));
            } else {
                if (!dsac.sameAs(ds)) {
                    getLogger().fine("Attributes for disk store " + name + " do not match");
                    throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_DISKSTORE_0_DO_NOT_MATCH.toLocalizedString(name));
                }
            }
        }
        Map<String, RegionAttributes<?, ?>> myNamedAttributes = this.listRegionAttributes();
        Map<String, RegionAttributes<Object, Object>> otherNamedAttributes = other.listRegionAttributes();
        if (myNamedAttributes.size() != otherNamedAttributes.size()) {
            throw new RuntimeException(LocalizedStrings.CacheCreation_NAMEDATTRIBUTES_SIZE.toLocalizedString());
        }
        for (Object object : myNamedAttributes.entrySet()) {
            Entry myEntry = (Entry) object;
            String myId = (String) myEntry.getKey();
            Assert.assertTrue(myEntry.getValue() instanceof RegionAttributesCreation, "Entry value is a " + myEntry.getValue().getClass().getName());
            RegionAttributesCreation myAttrs = (RegionAttributesCreation) myEntry.getValue();
            RegionAttributes<Object, Object> otherAttrs = other.getRegionAttributes(myId);
            if (otherAttrs == null) {
                getLogger().fine("No attributes for " + myId);
                throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ATTRIBUTES_FOR_0.toLocalizedString(myId));
            } else {
                if (!myAttrs.sameAs(otherAttrs)) {
                    getLogger().fine("Attributes for " + myId + " do not match");
                    throw new RuntimeException(LocalizedStrings.CacheCreation_ATTRIBUTES_FOR_0_DO_NOT_MATCH.toLocalizedString(myId));
                }
            }
        }
        Collection<Region<?, ?>> myRoots = this.roots.values();
        Collection<Region<?, ?>> otherRoots = other.rootRegions();
        if (myRoots.size() != otherRoots.size()) {
            throw new RuntimeException(LocalizedStrings.CacheCreation_ROOTS_SIZE.toLocalizedString());
        }
        for (final Region<?, ?> myRoot : myRoots) {
            RegionCreation rootRegion = (RegionCreation) myRoot;
            Region<Object, Object> otherRegion = other.getRegion(rootRegion.getName());
            if (otherRegion == null) {
                throw new RuntimeException(LocalizedStrings.CacheCreation_NO_ROOT_0.toLocalizedString(rootRegion.getName()));
            } else if (!rootRegion.sameAs(otherRegion)) {
                throw new RuntimeException(LocalizedStrings.CacheCreation_REGIONS_DIFFER.toLocalizedString());
            }
        }
        // If both have a listener, make sure they are equal.
        if (getCacheTransactionManager() != null) {
            // Currently the GemFireCache always has a CacheTransactionManager,
            // whereas that is not true for CacheTransactionManagerCreation.
            List<TransactionListener> otherTxListeners = Arrays.asList(other.getCacheTransactionManager().getListeners());
            List<TransactionListener> thisTxListeners = Arrays.asList(getCacheTransactionManager().getListeners());
            if (!thisTxListeners.equals(otherTxListeners)) {
                throw new RuntimeException(LocalizedStrings.CacheCreation_TXLISTENER.toLocalizedString());
            }
        }
    }
    if (hasResourceManager()) {
        getResourceManager().sameAs(other.getResourceManager());
    }
    return true;
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) RegionAttributes(org.apache.geode.cache.RegionAttributes) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) Entry(java.util.Map.Entry) CacheServer(org.apache.geode.cache.server.CacheServer) Pool(org.apache.geode.cache.client.Pool) DynamicRegionFactory(org.apache.geode.cache.DynamicRegionFactory) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) DiskStore(org.apache.geode.cache.DiskStore) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion)

Example 57 with PoolImpl

use of org.apache.geode.cache.client.internal.PoolImpl in project geode by apache.

the class GemFireCacheImpl method determineDefaultPool.

/**
   * Used to see if a existing cache's pool is compatible with us.
   *
   * @return the default pool that is right for us
   */
public Pool determineDefaultPool(PoolFactory poolFactory) {
    Pool pool;
    // create the pool if it does not already exist
    if (poolFactory == null) {
        Map<String, Pool> pools = PoolManager.getAll();
        if (pools.isEmpty()) {
            throw new IllegalStateException("Since a cache already existed a pool should also exist.");
        } else if (pools.size() == 1) {
            // otherwise use a singleton.
            pool = pools.values().iterator().next();
            if (getDefaultPool() != pool) {
                throw new IllegalStateException("Existing cache's default pool was not the same as the only existing pool");
            }
        } else {
            // just use the current default pool if one exists
            pool = getDefaultPool();
            if (pool == null) {
                // act as if the default pool was configured
                // and see if we can find an existing one that is compatible
                PoolFactoryImpl pfi = (PoolFactoryImpl) createDefaultPF();
                for (Pool p : pools.values()) {
                    if (((PoolImpl) p).isCompatible(pfi.getPoolAttributes())) {
                        pool = p;
                        break;
                    }
                }
                if (pool == null) {
                    // if pool is still null then we will not have a default pool for this ClientCache
                    return null;
                }
            }
        }
    } else {
        PoolFactoryImpl poolFactoryImpl = (PoolFactoryImpl) poolFactory;
        if (poolFactoryImpl.getPoolAttributes().locators.isEmpty() && poolFactoryImpl.getPoolAttributes().servers.isEmpty()) {
            try {
                String localHostName = SocketCreator.getHostName(SocketCreator.getLocalHost());
                poolFactoryImpl.addServer(localHostName, CacheServer.DEFAULT_PORT);
            } catch (UnknownHostException ex) {
                throw new IllegalStateException("Could not determine local host name", ex);
            }
        }
        PoolImpl defaultPool = (PoolImpl) getDefaultPool();
        if (defaultPool != null && defaultPool.isCompatible(poolFactoryImpl.getPoolAttributes())) {
            pool = defaultPool;
        } else {
            throw new IllegalStateException("Existing cache's default pool was not compatible");
        }
    }
    return pool;
}
Also used : UnknownHostException(java.net.UnknownHostException) Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Example 58 with PoolImpl

use of org.apache.geode.cache.client.internal.PoolImpl in project geode by apache.

the class GemFireCacheImpl method determineDefaultPool.

/**
   * Used to set the default pool on a new GemFireCache.
   */
public void determineDefaultPool() {
    if (!isClient()) {
        throw new UnsupportedOperationException();
    }
    Pool pool = null;
    // create the pool if it does not already exist
    if (this.poolFactory == null) {
        Map<String, Pool> pools = PoolManager.getAll();
        if (pools.isEmpty()) {
            this.poolFactory = createDefaultPF();
        } else if (pools.size() == 1) {
            // otherwise use a singleton.
            pool = pools.values().iterator().next();
        } else {
            // act as if the default pool was configured
            // and see if we can find an existing one that is compatible
            PoolFactoryImpl pfi = (PoolFactoryImpl) createDefaultPF();
            for (Pool p : pools.values()) {
                if (((PoolImpl) p).isCompatible(pfi.getPoolAttributes())) {
                    pool = p;
                    break;
                }
            }
            if (pool == null) {
                // if pool is still null then we will not have a default pool for this ClientCache
                this.defaultPool = null;
                return;
            }
        }
    } else {
        PoolFactoryImpl pfi = (PoolFactoryImpl) this.poolFactory;
        if (pfi.getPoolAttributes().locators.isEmpty() && pfi.getPoolAttributes().servers.isEmpty()) {
            try {
                String localHostName = SocketCreator.getHostName(SocketCreator.getLocalHost());
                pfi.addServer(localHostName, CacheServer.DEFAULT_PORT);
            } catch (UnknownHostException ex) {
                throw new IllegalStateException("Could not determine local host name", ex);
            }
        }
        // look for a pool that already exists that is compatible with
        // our PoolFactory.
        // If we don't find one we will create a new one that meets our needs.
        Map<String, Pool> pools = PoolManager.getAll();
        for (Pool p : pools.values()) {
            if (((PoolImpl) p).isCompatible(pfi.getPoolAttributes())) {
                pool = p;
                break;
            }
        }
    }
    if (pool == null) {
        // create our pool with a unique name
        String poolName = "DEFAULT";
        int count = 1;
        Map<String, Pool> pools = PoolManager.getAll();
        while (pools.containsKey(poolName)) {
            poolName = "DEFAULT" + count;
            count++;
        }
        pool = this.poolFactory.create(poolName);
    }
    this.defaultPool = pool;
}
Also used : UnknownHostException(java.net.UnknownHostException) Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint)

Example 59 with PoolImpl

use of org.apache.geode.cache.client.internal.PoolImpl in project geode by apache.

the class PoolManagerImpl method allPoolsRegisterInstantiator.

/**
   * @param instantiator
   */
public static void allPoolsRegisterInstantiator(Instantiator instantiator) {
    Instantiator[] instantiators = new Instantiator[1];
    instantiators[0] = instantiator;
    for (Iterator<Pool> itr = PoolManager.getAll().values().iterator(); itr.hasNext(); ) {
        PoolImpl next = (PoolImpl) itr.next();
        try {
            EventID eventId = InternalInstantiator.generateEventId();
            if (eventId == null) {
            // cache must not exist, do nothing
            } else {
                RegisterInstantiatorsOp.execute(next, instantiators, InternalInstantiator.generateEventId());
            }
        } catch (RuntimeException e) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.PoolmanagerImpl_ERROR_REGISTERING_INSTANTIATOR_ON_POOL), e);
        } finally {
            next.releaseThreadLocalConnection();
        }
    }
}
Also used : InternalInstantiator(org.apache.geode.internal.InternalInstantiator) Instantiator(org.apache.geode.Instantiator) Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Example 60 with PoolImpl

use of org.apache.geode.cache.client.internal.PoolImpl in project geode by apache.

the class PoolManagerImpl method allPoolsRegisterInstantiator.

public static void allPoolsRegisterInstantiator(InstantiatorAttributesHolder holder) {
    InstantiatorAttributesHolder[] holders = new InstantiatorAttributesHolder[1];
    holders[0] = holder;
    for (Iterator<Pool> itr = PoolManager.getAll().values().iterator(); itr.hasNext(); ) {
        PoolImpl next = (PoolImpl) itr.next();
        try {
            EventID eventId = InternalInstantiator.generateEventId();
            if (eventId == null) {
            // cache must not exist, do nothing
            } else {
                RegisterInstantiatorsOp.execute(next, holders, InternalInstantiator.generateEventId());
            }
        } catch (RuntimeException e) {
            logger.warn(LocalizedMessage.create(LocalizedStrings.PoolmanagerImpl_ERROR_REGISTERING_INSTANTIATOR_ON_POOL), e);
        } finally {
            next.releaseThreadLocalConnection();
        }
    }
}
Also used : InstantiatorAttributesHolder(org.apache.geode.internal.InternalInstantiator.InstantiatorAttributesHolder) Pool(org.apache.geode.cache.client.Pool) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Aggregations

PoolImpl (org.apache.geode.cache.client.internal.PoolImpl)91 Properties (java.util.Properties)43 AttributesFactory (org.apache.geode.cache.AttributesFactory)40 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)39 Region (org.apache.geode.cache.Region)30 RegionAttributes (org.apache.geode.cache.RegionAttributes)29 Test (org.junit.Test)17 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 IOException (java.io.IOException)14 Pool (org.apache.geode.cache.client.Pool)14 VM (org.apache.geode.test.dunit.VM)14 Host (org.apache.geode.test.dunit.Host)13 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)12 LocalRegion (org.apache.geode.internal.cache.LocalRegion)11 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)11 CacheException (org.apache.geode.cache.CacheException)10 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)9 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)9 NoAvailableServersException (org.apache.geode.cache.client.NoAvailableServersException)8 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)8